mmc: core: add eMMC hardware reset support
[pandora-kernel.git] / drivers / mmc / core / core.c
index b27b940..da6bd95 100644 (file)
@@ -24,6 +24,8 @@
 #include <linux/regulator/consumer.h>
 #include <linux/pm_runtime.h>
 #include <linux/suspend.h>
+#include <linux/fault-inject.h>
+#include <linux/random.h>
 
 #include <linux/mmc/card.h>
 #include <linux/mmc/host.h>
@@ -83,6 +85,43 @@ static void mmc_flush_scheduled_work(void)
        flush_workqueue(workqueue);
 }
 
+#ifdef CONFIG_FAIL_MMC_REQUEST
+
+/*
+ * Internal function. Inject random data errors.
+ * If mmc_data is NULL no errors are injected.
+ */
+static void mmc_should_fail_request(struct mmc_host *host,
+                                   struct mmc_request *mrq)
+{
+       struct mmc_command *cmd = mrq->cmd;
+       struct mmc_data *data = mrq->data;
+       static const int data_errors[] = {
+               -ETIMEDOUT,
+               -EILSEQ,
+               -EIO,
+       };
+
+       if (!data)
+               return;
+
+       if (cmd->error || data->error ||
+           !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
+               return;
+
+       data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
+       data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
+}
+
+#else /* CONFIG_FAIL_MMC_REQUEST */
+
+static inline void mmc_should_fail_request(struct mmc_host *host,
+                                          struct mmc_request *mrq)
+{
+}
+
+#endif /* CONFIG_FAIL_MMC_REQUEST */
+
 /**
  *     mmc_request_done - finish processing an MMC request
  *     @host: MMC host which completed request
@@ -109,6 +148,8 @@ void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
                cmd->error = 0;
                host->ops->request(host, mrq);
        } else {
+               mmc_should_fail_request(host, mrq);
+
                led_trigger_event(host->led, LED_OFF);
 
                pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
@@ -279,8 +320,14 @@ struct mmc_async_req *mmc_start_req(struct mmc_host *host,
                mmc_wait_for_req_done(host, host->areq->mrq);
                err = host->areq->err_check(host->card, host->areq);
                if (err) {
+                       /* post process the completed failed request */
                        mmc_post_req(host, host->areq->mrq, 0);
                        if (areq)
+                               /*
+                                * Cancel the new prepared request, because
+                                * it can't run until the failed
+                                * request has been properly handled.
+                                */
                                mmc_post_req(host, areq->mrq, -EINVAL);
 
                        host->areq = NULL;
@@ -330,7 +377,7 @@ EXPORT_SYMBOL(mmc_wait_for_req);
  */
 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
 {
-       struct mmc_request mrq = {0};
+       struct mmc_request mrq = {NULL};
 
        WARN_ON(!host->claimed);
 
@@ -1119,13 +1166,11 @@ static void mmc_power_up(struct mmc_host *host)
                bit = fls(host->ocr_avail) - 1;
 
        host->ios.vdd = bit;
-       if (mmc_host_is_spi(host)) {
+       if (mmc_host_is_spi(host))
                host->ios.chip_select = MMC_CS_HIGH;
-               host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
-       } else {
+       else
                host->ios.chip_select = MMC_CS_DONTCARE;
-               host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
-       }
+       host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
        host->ios.power_mode = MMC_POWER_UP;
        host->ios.bus_width = MMC_BUS_WIDTH_1;
        host->ios.timing = MMC_TIMING_LEGACY;
@@ -1151,7 +1196,7 @@ static void mmc_power_up(struct mmc_host *host)
        mmc_host_clk_release(host);
 }
 
-static void mmc_power_off(struct mmc_host *host)
+void mmc_power_off(struct mmc_host *host)
 {
        mmc_host_clk_hold(host);
 
@@ -1173,6 +1218,13 @@ static void mmc_power_off(struct mmc_host *host)
        host->ios.timing = MMC_TIMING_LEGACY;
        mmc_set_ios(host);
 
+       /*
+        * Some configurations, such as the 802.11 SDIO card in the OLPC
+        * XO-1.5, require a short delay after poweroff before the card
+        * can be successfully turned on again.
+        */
+       mmc_delay(1);
+
        mmc_host_clk_release(host);
 }
 
@@ -1241,8 +1293,7 @@ void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
 }
 
 /*
- * Remove the current bus handler from a host. Assumes that there are
- * no interesting cards left, so the bus is powered down.
+ * Remove the current bus handler from a host.
  */
 void mmc_detach_bus(struct mmc_host *host)
 {
@@ -1259,8 +1310,6 @@ void mmc_detach_bus(struct mmc_host *host)
 
        spin_unlock_irqrestore(&host->lock, flags);
 
-       mmc_power_off(host);
-
        mmc_bus_put(host);
 }
 
@@ -1727,6 +1776,94 @@ int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
 }
 EXPORT_SYMBOL(mmc_set_blocklen);
 
+static void mmc_hw_reset_for_init(struct mmc_host *host)
+{
+       if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
+               return;
+       mmc_host_clk_hold(host);
+       host->ops->hw_reset(host);
+       mmc_host_clk_release(host);
+}
+
+int mmc_can_reset(struct mmc_card *card)
+{
+       u8 rst_n_function;
+
+       if (!mmc_card_mmc(card))
+               return 0;
+       rst_n_function = card->ext_csd.rst_n_function;
+       if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
+               return 0;
+       return 1;
+}
+EXPORT_SYMBOL(mmc_can_reset);
+
+static int mmc_do_hw_reset(struct mmc_host *host, int check)
+{
+       struct mmc_card *card = host->card;
+
+       if (!host->bus_ops->power_restore)
+               return -EOPNOTSUPP;
+
+       if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
+               return -EOPNOTSUPP;
+
+       if (!card)
+               return -EINVAL;
+
+       if (!mmc_can_reset(card))
+               return -EOPNOTSUPP;
+
+       mmc_host_clk_hold(host);
+       mmc_set_clock(host, host->f_init);
+
+       host->ops->hw_reset(host);
+
+       /* If the reset has happened, then a status command will fail */
+       if (check) {
+               struct mmc_command cmd = {0};
+               int err;
+
+               cmd.opcode = MMC_SEND_STATUS;
+               if (!mmc_host_is_spi(card->host))
+                       cmd.arg = card->rca << 16;
+               cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
+               err = mmc_wait_for_cmd(card->host, &cmd, 0);
+               if (!err) {
+                       mmc_host_clk_release(host);
+                       return -ENOSYS;
+               }
+       }
+
+       host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
+       if (mmc_host_is_spi(host)) {
+               host->ios.chip_select = MMC_CS_HIGH;
+               host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
+       } else {
+               host->ios.chip_select = MMC_CS_DONTCARE;
+               host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
+       }
+       host->ios.bus_width = MMC_BUS_WIDTH_1;
+       host->ios.timing = MMC_TIMING_LEGACY;
+       mmc_set_ios(host);
+
+       mmc_host_clk_release(host);
+
+       return host->bus_ops->power_restore(host);
+}
+
+int mmc_hw_reset(struct mmc_host *host)
+{
+       return mmc_do_hw_reset(host, 0);
+}
+EXPORT_SYMBOL(mmc_hw_reset);
+
+int mmc_hw_reset_check(struct mmc_host *host)
+{
+       return mmc_do_hw_reset(host, 1);
+}
+EXPORT_SYMBOL(mmc_hw_reset_check);
+
 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
 {
        host->f_init = freq;
@@ -1737,6 +1874,12 @@ static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
 #endif
        mmc_power_up(host);
 
+       /*
+        * Some eMMCs (with VCCQ always on) may not be reset after power up, so
+        * do a hardware reset if possible.
+        */
+       mmc_hw_reset_for_init(host);
+
        /*
         * sdio_reset sends CMD52 to reset card.  Since we do not know
         * if the card is being re-initialized, just send it.  CMD52
@@ -1845,6 +1988,7 @@ void mmc_stop_host(struct mmc_host *host)
 
                mmc_claim_host(host);
                mmc_detach_bus(host);
+               mmc_power_off(host);
                mmc_release_host(host);
                mmc_bus_put(host);
                return;
@@ -1974,6 +2118,7 @@ int mmc_suspend_host(struct mmc_host *host)
                                host->bus_ops->remove(host);
                        mmc_claim_host(host);
                        mmc_detach_bus(host);
+                       mmc_power_off(host);
                        mmc_release_host(host);
                        host->pm_flags = 0;
                        err = 0;
@@ -2061,6 +2206,7 @@ int mmc_pm_notify(struct notifier_block *notify_block,
                        host->bus_ops->remove(host);
 
                mmc_detach_bus(host);
+               mmc_power_off(host);
                mmc_release_host(host);
                host->pm_flags = 0;
                break;