Merge remote-tracking branch 'remotes/kernel-2.6.32/develop' into develop-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / core / core.c
index de01a899d01f83124064a794fd6e964579bb5796..3c7e63b416ef96c0b1bc3ee8fb62733fbac52ff8 100644 (file)
@@ -49,6 +49,22 @@ static struct wake_lock mmc_delayed_work_wake_lock;
 int use_spi_crc = 1;
 module_param(use_spi_crc, bool, 0);
 
+/*
+ * We normally treat cards as removed during suspend if they are not
+ * known to be on a non-removable bus, to avoid the risk of writing
+ * back data to a different card after resume.  Allow this to be
+ * overridden if necessary.
+ */
+#ifdef CONFIG_MMC_UNSAFE_RESUME
+int mmc_assume_removable;
+#else
+int mmc_assume_removable = 1;
+#endif
+module_param_named(removable, mmc_assume_removable, bool, 0644);
+MODULE_PARM_DESC(
+       removable,
+       "MMC/SD cards are removable and may be removed during suspend");
+
 /*
  * Internal function. Schedule delayed work in the MMC work queue.
  */
@@ -202,7 +218,11 @@ void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
 
        mmc_start_request(host, mrq);
 
+#if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
+    wait_for_completion_timeout(&complete,HZ*3); //for cmd dead. Modifyed by xbw at 2011-06-02
+#else    
        wait_for_completion(&complete);
+#endif
 }
 
 EXPORT_SYMBOL(mmc_wait_for_req);
@@ -532,9 +552,12 @@ void mmc_host_deeper_disable(struct work_struct *work)
 
        /* If the host is claimed then we do not want to disable it anymore */
        if (!mmc_try_claim_host(host))
-               return;
+               goto out;
        mmc_host_do_disable(host, 1);
        mmc_do_release_host(host);
+
+out:
+       wake_unlock(&mmc_delayed_work_wake_lock);
 }
 
 /**
@@ -894,12 +917,7 @@ static void mmc_power_up(struct mmc_host *host)
         */
        mmc_delay(10);
 
-       if (host->f_min > 400000) {
-               pr_warning("%s: Minimum clock frequency too high for "
-                               "identification mode\n", mmc_hostname(host));
-               host->ios.clock = host->f_min;
-       } else
-               host->ios.clock = 400000;
+       host->ios.clock = host->f_min;
 
        host->ios.power_mode = MMC_POWER_ON;
        mmc_set_ios(host);
@@ -966,11 +984,17 @@ static inline void mmc_bus_put(struct mmc_host *host)
 
 int mmc_resume_bus(struct mmc_host *host)
 {
+       unsigned long flags;
+
        if (!mmc_bus_needs_resume(host))
                return -EINVAL;
 
        printk("%s: Starting deferred resume\n", mmc_hostname(host));
+       spin_lock_irqsave(&host->lock, flags);
        host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
+       host->rescan_disable = 0;
+       spin_unlock_irqrestore(&host->lock, flags);
+
        mmc_bus_get(host);
        if (host->bus_ops && !host->bus_dead) {
                mmc_power_up(host);
@@ -1061,7 +1085,354 @@ void mmc_detect_change(struct mmc_host *host, unsigned long delay)
 
 EXPORT_SYMBOL(mmc_detect_change);
 
+void mmc_init_erase(struct mmc_card *card)
+{
+       unsigned int sz;
+
+       if (is_power_of_2(card->erase_size))
+               card->erase_shift = ffs(card->erase_size) - 1;
+       else
+               card->erase_shift = 0;
+
+       /*
+        * It is possible to erase an arbitrarily large area of an SD or MMC
+        * card.  That is not desirable because it can take a long time
+        * (minutes) potentially delaying more important I/O, and also the
+        * timeout calculations become increasingly hugely over-estimated.
+        * Consequently, 'pref_erase' is defined as a guide to limit erases
+        * to that size and alignment.
+        *
+        * For SD cards that define Allocation Unit size, limit erases to one
+        * Allocation Unit at a time.  For MMC cards that define High Capacity
+        * Erase Size, whether it is switched on or not, limit to that size.
+        * Otherwise just have a stab at a good value.  For modern cards it
+        * will end up being 4MiB.  Note that if the value is too small, it
+        * can end up taking longer to erase.
+        */
+       if (mmc_card_sd(card) && card->ssr.au) {
+               card->pref_erase = card->ssr.au;
+               card->erase_shift = ffs(card->ssr.au) - 1;
+       } else if (card->ext_csd.hc_erase_size) {
+               card->pref_erase = card->ext_csd.hc_erase_size;
+       } else {
+               sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
+               if (sz < 128)
+                       card->pref_erase = 512 * 1024 / 512;
+               else if (sz < 512)
+                       card->pref_erase = 1024 * 1024 / 512;
+               else if (sz < 1024)
+                       card->pref_erase = 2 * 1024 * 1024 / 512;
+               else
+                       card->pref_erase = 4 * 1024 * 1024 / 512;
+               if (card->pref_erase < card->erase_size)
+                       card->pref_erase = card->erase_size;
+               else {
+                       sz = card->pref_erase % card->erase_size;
+                       if (sz)
+                               card->pref_erase += card->erase_size - sz;
+               }
+       }
+}
+
+static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
+                                     struct mmc_command *cmd,
+                                     unsigned int arg, unsigned int qty)
+{
+       unsigned int erase_timeout;
+
+       if (card->ext_csd.erase_group_def & 1) {
+               /* High Capacity Erase Group Size uses HC timeouts */
+               if (arg == MMC_TRIM_ARG)
+                       erase_timeout = card->ext_csd.trim_timeout;
+               else
+                       erase_timeout = card->ext_csd.hc_erase_timeout;
+       } else {
+               /* CSD Erase Group Size uses write timeout */
+               unsigned int mult = (10 << card->csd.r2w_factor);
+               unsigned int timeout_clks = card->csd.tacc_clks * mult;
+               unsigned int timeout_us;
+
+               /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
+               if (card->csd.tacc_ns < 1000000)
+                       timeout_us = (card->csd.tacc_ns * mult) / 1000;
+               else
+                       timeout_us = (card->csd.tacc_ns / 1000) * mult;
+
+               /*
+                * ios.clock is only a target.  The real clock rate might be
+                * less but not that much less, so fudge it by multiplying by 2.
+                */
+               timeout_clks <<= 1;
+               timeout_us += (timeout_clks * 1000) /
+                             (card->host->ios.clock / 1000);
 
+               erase_timeout = timeout_us / 1000;
+
+               /*
+                * Theoretically, the calculation could underflow so round up
+                * to 1ms in that case.
+                */
+               if (!erase_timeout)
+                       erase_timeout = 1;
+       }
+
+       /* Multiplier for secure operations */
+       if (arg & MMC_SECURE_ARGS) {
+               if (arg == MMC_SECURE_ERASE_ARG)
+                       erase_timeout *= card->ext_csd.sec_erase_mult;
+               else
+                       erase_timeout *= card->ext_csd.sec_trim_mult;
+       }
+
+       erase_timeout *= qty;
+
+       /*
+        * Ensure at least a 1 second timeout for SPI as per
+        * 'mmc_set_data_timeout()'
+        */
+       if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
+               erase_timeout = 1000;
+
+       cmd->erase_timeout = erase_timeout;
+}
+
+static void mmc_set_sd_erase_timeout(struct mmc_card *card,
+                                    struct mmc_command *cmd, unsigned int arg,
+                                    unsigned int qty)
+{
+       if (card->ssr.erase_timeout) {
+               /* Erase timeout specified in SD Status Register (SSR) */
+               cmd->erase_timeout = card->ssr.erase_timeout * qty +
+                                    card->ssr.erase_offset;
+       } else {
+               /*
+                * Erase timeout not specified in SD Status Register (SSR) so
+                * use 250ms per write block.
+                */
+               cmd->erase_timeout = 250 * qty;
+       }
+
+       /* Must not be less than 1 second */
+       if (cmd->erase_timeout < 1000)
+               cmd->erase_timeout = 1000;
+}
+
+static void mmc_set_erase_timeout(struct mmc_card *card,
+                                 struct mmc_command *cmd, unsigned int arg,
+                                 unsigned int qty)
+{
+       if (mmc_card_sd(card))
+               mmc_set_sd_erase_timeout(card, cmd, arg, qty);
+       else
+               mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
+}
+
+static int mmc_do_erase(struct mmc_card *card, unsigned int from,
+                       unsigned int to, unsigned int arg)
+{
+       struct mmc_command cmd;
+       unsigned int qty = 0;
+       int err;
+
+       /*
+        * qty is used to calculate the erase timeout which depends on how many
+        * erase groups (or allocation units in SD terminology) are affected.
+        * We count erasing part of an erase group as one erase group.
+        * For SD, the allocation units are always a power of 2.  For MMC, the
+        * erase group size is almost certainly also power of 2, but it does not
+        * seem to insist on that in the JEDEC standard, so we fall back to
+        * division in that case.  SD may not specify an allocation unit size,
+        * in which case the timeout is based on the number of write blocks.
+        *
+        * Note that the timeout for secure trim 2 will only be correct if the
+        * number of erase groups specified is the same as the total of all
+        * preceding secure trim 1 commands.  Since the power may have been
+        * lost since the secure trim 1 commands occurred, it is generally
+        * impossible to calculate the secure trim 2 timeout correctly.
+        */
+       if (card->erase_shift)
+               qty += ((to >> card->erase_shift) -
+                       (from >> card->erase_shift)) + 1;
+       else if (mmc_card_sd(card))
+               qty += to - from + 1;
+       else
+               qty += ((to / card->erase_size) -
+                       (from / card->erase_size)) + 1;
+
+       if (!mmc_card_blockaddr(card)) {
+               from <<= 9;
+               to <<= 9;
+       }
+
+       memset(&cmd, 0, sizeof(struct mmc_command));
+       if (mmc_card_sd(card))
+               cmd.opcode = SD_ERASE_WR_BLK_START;
+       else
+               cmd.opcode = MMC_ERASE_GROUP_START;
+       cmd.arg = from;
+       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       err = mmc_wait_for_cmd(card->host, &cmd, 0);
+       if (err) {
+               printk(KERN_ERR "mmc_erase: group start error %d, "
+                      "status %#x\n", err, cmd.resp[0]);
+               err = -EINVAL;
+               goto out;
+       }
+
+       memset(&cmd, 0, sizeof(struct mmc_command));
+       if (mmc_card_sd(card))
+               cmd.opcode = SD_ERASE_WR_BLK_END;
+       else
+               cmd.opcode = MMC_ERASE_GROUP_END;
+       cmd.arg = to;
+       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+       err = mmc_wait_for_cmd(card->host, &cmd, 0);
+       if (err) {
+               printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
+                      err, cmd.resp[0]);
+               err = -EINVAL;
+               goto out;
+       }
+
+       memset(&cmd, 0, sizeof(struct mmc_command));
+       cmd.opcode = MMC_ERASE;
+       cmd.arg = arg;
+       cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+       mmc_set_erase_timeout(card, &cmd, arg, qty);
+       err = mmc_wait_for_cmd(card->host, &cmd, 0);
+       if (err) {
+               printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
+                      err, cmd.resp[0]);
+               err = -EIO;
+               goto out;
+       }
+
+       if (mmc_host_is_spi(card->host))
+               goto out;
+
+       do {
+               memset(&cmd, 0, sizeof(struct mmc_command));
+               cmd.opcode = MMC_SEND_STATUS;
+               cmd.arg = card->rca << 16;
+               cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
+               /* Do not retry else we can't see errors */
+               err = mmc_wait_for_cmd(card->host, &cmd, 0);
+               if (err || (cmd.resp[0] & 0xFDF92000)) {
+                       printk(KERN_ERR "error %d requesting status %#x\n",
+                               err, cmd.resp[0]);
+                       err = -EIO;
+                       goto out;
+               }
+       } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
+                R1_CURRENT_STATE(cmd.resp[0]) == 7);
+out:
+       return err;
+}
+
+/**
+ * mmc_erase - erase sectors.
+ * @card: card to erase
+ * @from: first sector to erase
+ * @nr: number of sectors to erase
+ * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
+ *
+ * Caller must claim host before calling this function.
+ */
+int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
+             unsigned int arg)
+{
+       unsigned int rem, to = from + nr;
+
+       if (!(card->host->caps & MMC_CAP_ERASE) ||
+           !(card->csd.cmdclass & CCC_ERASE))
+               return -EOPNOTSUPP;
+
+       if (!card->erase_size)
+               return -EOPNOTSUPP;
+
+       if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
+               return -EOPNOTSUPP;
+
+       if ((arg & MMC_SECURE_ARGS) &&
+           !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
+               return -EOPNOTSUPP;
+
+       if ((arg & MMC_TRIM_ARGS) &&
+           !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
+               return -EOPNOTSUPP;
+
+       if (arg == MMC_SECURE_ERASE_ARG) {
+               if (from % card->erase_size || nr % card->erase_size)
+                       return -EINVAL;
+       }
+
+       if (arg == MMC_ERASE_ARG) {
+               rem = from % card->erase_size;
+               if (rem) {
+                       rem = card->erase_size - rem;
+                       from += rem;
+                       if (nr > rem)
+                               nr -= rem;
+                       else
+                               return 0;
+               }
+               rem = nr % card->erase_size;
+               if (rem)
+                       nr -= rem;
+       }
+
+       if (nr == 0)
+               return 0;
+
+       to = from + nr;
+
+       if (to <= from)
+               return -EINVAL;
+
+       /* 'from' and 'to' are inclusive */
+       to -= 1;
+
+       return mmc_do_erase(card, from, to, arg);
+}
+EXPORT_SYMBOL(mmc_erase);
+
+int mmc_can_erase(struct mmc_card *card)
+{
+       if ((card->host->caps & MMC_CAP_ERASE) &&
+           (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
+               return 1;
+       return 0;
+}
+EXPORT_SYMBOL(mmc_can_erase);
+
+int mmc_can_trim(struct mmc_card *card)
+{
+       if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
+               return 1;
+       return 0;
+}
+EXPORT_SYMBOL(mmc_can_trim);
+
+int mmc_can_secure_erase_trim(struct mmc_card *card)
+{
+       if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
+               return 1;
+       return 0;
+}
+EXPORT_SYMBOL(mmc_can_secure_erase_trim);
+
+int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
+                           unsigned int nr)
+{
+       if (!card->erase_size)
+               return 0;
+       if (from % card->erase_size || nr % card->erase_size)
+               return 0;
+       return 1;
+}
+EXPORT_SYMBOL(mmc_erase_group_aligned);
+
+#if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)  
 void mmc_rescan(struct work_struct *work)
 {
        struct mmc_host *host =
@@ -1070,6 +1441,179 @@ void mmc_rescan(struct work_struct *work)
        int err;
        int extend_wakelock = 0;
 
+    
+       mmc_bus_get(host);
+
+       /* if there is a card registered, check whether it is still present */
+       if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
+               host->bus_ops->detect(host);
+
+       /* If the card was removed the bus will be marked
+        * as dead - extend the wakelock so userspace
+        * can respond */
+       if (host->bus_dead)
+               extend_wakelock = 1;
+
+       mmc_bus_put(host);
+
+
+       mmc_bus_get(host);
+
+       /* if there still is a card present, stop here */
+       if (host->bus_ops != NULL) {
+               mmc_bus_put(host);
+               goto out;
+       }
+
+       /* detect a newly inserted card */
+
+       /*
+        * Only we can add a new handler, so it's safe to
+        * release the lock here.
+        */
+       mmc_bus_put(host);
+    printk("\n%s...%d..  ===== mmc_rescan Begin....======xbw[%s]=====\n",__FILE__, __LINE__, mmc_hostname(host));
+
+       if (host->ops->get_cd && host->ops->get_cd(host) == 0)
+       {
+           printk("\n=================\n%s..%d..  ====find no SDMMC host.====xbw[%s]=====\n", \
+               __FUNCTION__, __LINE__, mmc_hostname(host));
+               
+               goto out;
+       }
+
+       mmc_claim_host(host);
+       
+       mmc_power_up(host);
+       
+       mmc_go_idle(host);
+
+    /*
+        In oder to improve the initialization process in rockchip IC, I modify the following code about the the initialization process of SDIO-SD-MMC.
+        So I deleted the CMD8 and add a conditional to distinguish between the two card type,i.e.SDMMC process and SDIO process.
+        For detail,please refer to "RK29XX Technical Reference Manual" and "SD-MMC-SDIO Specifications".
+        Noted by xbw@2011-04-09
+    */
+
+    //mmc_send_if_cond(host, host->ocr_avail); //deleted by xbw@2011-04-09
+
+     if( strncmp( mmc_hostname(host) ,"mmc0" , strlen("mmc0")) ){
+       /*
+        * First we search for SDIO...
+        */
+       err = mmc_send_io_op_cond(host, 0, &ocr);
+       if (!err) {
+               printk("\n%s..%d..  ===== Begin to identify card as SDIO-card===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
+
+               if (mmc_attach_sdio(host, ocr))
+               {
+                   printk("\n=====\n %s..%d..  ===== Initialize SDIO-card unsuccessfully!!! ===xbw[%s]===\n=====\n",\
+                       __FUNCTION__,  __LINE__, mmc_hostname(host));
+                       
+                       mmc_power_off(host);
+               }
+               else
+               {
+                   printk("%s..%d..  ===== Initialize SDIO successfully. ===xbw[%s]===\n",__FUNCTION__,  __LINE__, mmc_hostname(host));
+               }
+               extend_wakelock = 1;
+               goto out;
+       }
+    }
+
+
+    /*
+     * ...then normal SD...
+     */
+    err = mmc_send_app_op_cond(host, 0, &ocr);
+    if (!err) {
+       printk("\n%s..%d..  ===== Begin to identify card as SD-card ===xbw[%s]===\n",__FUNCTION__, __LINE__, mmc_hostname(host));
+
+       if (mmc_attach_sd(host, ocr))
+       {
+           printk("\n=====\n%s..%d..  ===== Initialize SD-card unsuccessfully!!! ===xbw[%s]===\n====\n",\
+               __FUNCTION__,  __LINE__, mmc_hostname(host));
+               
+               mmc_power_off(host);
+       }
+       else
+       {
+           printk("%s..%d..  ===== Initialize SD-card successfully. ===xbw[%s]===\n",__FUNCTION__,  __LINE__, mmc_hostname(host));
+       }
+       extend_wakelock = 1;
+       goto out;
+    }
+
+    /*
+     * ...and finally MMC.
+     */
+    err = mmc_send_op_cond(host, 0, &ocr);
+    if (!err) {
+        printk("\n%s..%d..  ===== Begin to identify card as MMC-card ===xbw[%s]===\n", __FUNCTION__, __LINE__, mmc_hostname(host));
+
+       if (mmc_attach_mmc(host, ocr))
+       {
+           printk("\n =====\n%s..%d..  ===== Initialize MMC-card unsuccessfully!!! ===xbw[%s]===\n======\n",\
+               __FUNCTION__,  __LINE__, mmc_hostname(host));
+               
+               mmc_power_off(host);
+       }
+       else
+       {
+           printk("%s...%d..  ===== Initialize MMC-card successfully. ===xbw[%s]===\n",__FUNCTION__,  __LINE__, mmc_hostname(host));
+       }
+       extend_wakelock = 1;
+       goto out;
+    }
+
+       mmc_release_host(host);
+       mmc_power_off(host);
+
+out:
+
+       if (extend_wakelock)
+               wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
+       else
+               wake_unlock(&mmc_delayed_work_wake_lock);
+
+       if (host->caps & MMC_CAP_NEEDS_POLL)
+               mmc_schedule_delayed_work(&host->detect, HZ);
+}
+
+#else
+void mmc_rescan(struct work_struct *work)
+{
+       struct mmc_host *host =
+               container_of(work, struct mmc_host, detect.work);
+       u32 ocr;
+       int err;
+       unsigned long flags;
+       int extend_wakelock = 0;
+       
+#if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)       
+       unsigned long flags;
+
+       spin_lock_irqsave(&host->lock, flags);
+
+       if (host->rescan_disable) {
+               spin_unlock_irqrestore(&host->lock, flags);
+               return;
+       }
+
+       spin_unlock_irqrestore(&host->lock, flags);
+#endif
+
+
+       spin_lock_irqsave(&host->lock, flags);
+
+       if (host->rescan_disable) {
+               spin_unlock_irqrestore(&host->lock, flags);
+               return;
+       }
+
+       spin_unlock_irqrestore(&host->lock, flags);
+
+
        mmc_bus_get(host);
 
        /* if there is a card registered, check whether it is still present */
@@ -1107,6 +1651,7 @@ void mmc_rescan(struct work_struct *work)
        mmc_claim_host(host);
 
        mmc_power_up(host);
+       sdio_reset(host);
        mmc_go_idle(host);
 
        mmc_send_if_cond(host, host->ocr_avail);
@@ -1116,9 +1661,16 @@ void mmc_rescan(struct work_struct *work)
         */
        err = mmc_send_io_op_cond(host, 0, &ocr);
        if (!err) {
-               if (mmc_attach_sdio(host, ocr))
-                       mmc_power_off(host);
-               extend_wakelock = 1;
+               if (mmc_attach_sdio(host, ocr)) {
+                       mmc_claim_host(host);
+                       /* try SDMEM (but not MMC) even if SDIO is broken */
+                       if (mmc_send_app_op_cond(host, 0, &ocr))
+                               goto out_fail;
+
+                       if (mmc_attach_sd(host, ocr))
+                               mmc_power_off(host);
+                       extend_wakelock = 1;
+               }
                goto out;
        }
 
@@ -1144,6 +1696,7 @@ void mmc_rescan(struct work_struct *work)
                goto out;
        }
 
+out_fail:
        mmc_release_host(host);
        mmc_power_off(host);
 
@@ -1156,6 +1709,7 @@ out:
        if (host->caps & MMC_CAP_NEEDS_POLL)
                mmc_schedule_delayed_work(&host->detect, HZ);
 }
+#endif
 
 void mmc_start_host(struct mmc_host *host)
 {
@@ -1174,9 +1728,12 @@ void mmc_stop_host(struct mmc_host *host)
 
        if (host->caps & MMC_CAP_DISABLE)
                cancel_delayed_work(&host->disable);
-       cancel_delayed_work(&host->detect);
+       cancel_delayed_work_sync(&host->detect);
        mmc_flush_scheduled_work();
 
+       /* clear pm flags now and let card drivers set them as needed */
+       host->pm_flags = 0;
+
        mmc_bus_get(host);
        if (host->bus_ops && !host->bus_dead) {
                if (host->bus_ops->remove)
@@ -1274,9 +1831,8 @@ EXPORT_SYMBOL(mmc_card_can_sleep);
 /**
  *     mmc_suspend_host - suspend a host
  *     @host: mmc host
- *     @state: suspend mode (PM_SUSPEND_xxx)
  */
-int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
+int mmc_suspend_host(struct mmc_host *host)
 {
        int err = 0;
 
@@ -1292,6 +1848,10 @@ int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
        if (host->bus_ops && !host->bus_dead) {
                if (host->bus_ops->suspend)
                        err = host->bus_ops->suspend(host);
+                       
+#if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
+        //deleted all detail code.
+#else
                if (err == -ENOSYS || !host->bus_ops->resume) {
                        /*
                         * We simply "remove" the card in this case.
@@ -1302,12 +1862,14 @@ int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
                        mmc_claim_host(host);
                        mmc_detach_bus(host);
                        mmc_release_host(host);
+                       host->pm_flags = 0;
                        err = 0;
                }
+#endif         
        }
        mmc_bus_put(host);
 
-       if (!err)
+       if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
                mmc_power_off(host);
 
        return err;
@@ -1324,43 +1886,103 @@ int mmc_resume_host(struct mmc_host *host)
        int err = 0;
 
        mmc_bus_get(host);
-       if (host->bus_resume_flags & MMC_BUSRESUME_MANUAL_RESUME) {
+       if (mmc_bus_manual_resume(host)) {
                host->bus_resume_flags |= MMC_BUSRESUME_NEEDS_RESUME;
                mmc_bus_put(host);
                return 0;
        }
 
        if (host->bus_ops && !host->bus_dead) {
-               mmc_power_up(host);
-               mmc_select_voltage(host, host->ocr);
+               if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
+                       mmc_power_up(host);
+                       mmc_select_voltage(host, host->ocr);
+               }
                BUG_ON(!host->bus_ops->resume);
+               
+#if defined(CONFIG_SDMMC_RK29) && defined(CONFIG_SDMMC_RK29_OLD)
                err = host->bus_ops->resume(host);
                if (err) {
                        printk(KERN_WARNING "%s: error %d during resume "
                                            "(card was removed?)\n",
                                            mmc_hostname(host), err);
-                       if (host->bus_ops->remove)
-                               host->bus_ops->remove(host);
-                       mmc_claim_host(host);
-                       mmc_detach_bus(host);
-                       mmc_release_host(host);
-                       /* no need to bother upper layers */
                        err = 0;
                }
+
+#elif defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)  
+        //panic if the card is being removed during the resume, deleted by xbw at 2011-06-20
+               host->bus_ops->resume(host);
+
+#else
+               err = host->bus_ops->resume(host);
+               if (err) {
+                       printk(KERN_WARNING "%s: error %d during resume "
+                                           "(card was removed?)\n",
+                                           mmc_hostname(host), err);
+                       err = 0;
+               }               
+#endif
        }
        mmc_bus_put(host);
 
-       /*
-        * We add a slight delay here so that resume can progress
-        * in parallel.
-        */
-       mmc_detect_change(host, 1);
-
        return err;
 }
-
 EXPORT_SYMBOL(mmc_resume_host);
 
+/* Do the card removal on suspend if card is assumed removeable
+ * Do that in pm notifier while userspace isn't yet frozen, so we will be able
+   to sync the card.
+*/
+int mmc_pm_notify(struct notifier_block *notify_block,
+                                       unsigned long mode, void *unused)
+{
+       struct mmc_host *host = container_of(
+               notify_block, struct mmc_host, pm_notify);
+       unsigned long flags;
+
+
+       switch (mode) {
+       case PM_HIBERNATION_PREPARE:
+       case PM_SUSPEND_PREPARE:
+
+               spin_lock_irqsave(&host->lock, flags);
+               if (mmc_bus_needs_resume(host)) {
+                       spin_unlock_irqrestore(&host->lock, flags);
+                       break;
+               }
+               host->rescan_disable = 1;
+               spin_unlock_irqrestore(&host->lock, flags);
+               cancel_delayed_work_sync(&host->detect);
+
+               if (!host->bus_ops || host->bus_ops->suspend)
+                       break;
+
+               mmc_claim_host(host);
+
+               if (host->bus_ops->remove)
+                       host->bus_ops->remove(host);
+
+               mmc_detach_bus(host);
+               mmc_release_host(host);
+               host->pm_flags = 0;
+               break;
+
+       case PM_POST_SUSPEND:
+       case PM_POST_HIBERNATION:
+       case PM_POST_RESTORE:
+
+               spin_lock_irqsave(&host->lock, flags);
+               if (mmc_bus_manual_resume(host)) {
+                       spin_unlock_irqrestore(&host->lock, flags);
+                       break;
+               }
+               host->rescan_disable = 0;
+               spin_unlock_irqrestore(&host->lock, flags);
+               mmc_detect_change(host, 0);
+
+       }
+
+       return 0;
+}
 #endif
 
 #ifdef CONFIG_MMC_EMBEDDED_SDIO