Merge branch 'stable/bug.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / mmc / core / core.c
index 1f453ac..f091b43 100644 (file)
@@ -198,9 +198,109 @@ mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
 
 static void mmc_wait_done(struct mmc_request *mrq)
 {
-       complete(mrq->done_data);
+       complete(&mrq->completion);
 }
 
+static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
+{
+       init_completion(&mrq->completion);
+       mrq->done = mmc_wait_done;
+       mmc_start_request(host, mrq);
+}
+
+static void mmc_wait_for_req_done(struct mmc_host *host,
+                                 struct mmc_request *mrq)
+{
+       wait_for_completion(&mrq->completion);
+}
+
+/**
+ *     mmc_pre_req - Prepare for a new request
+ *     @host: MMC host to prepare command
+ *     @mrq: MMC request to prepare for
+ *     @is_first_req: true if there is no previous started request
+ *                     that may run in parellel to this call, otherwise false
+ *
+ *     mmc_pre_req() is called in prior to mmc_start_req() to let
+ *     host prepare for the new request. Preparation of a request may be
+ *     performed while another request is running on the host.
+ */
+static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
+                bool is_first_req)
+{
+       if (host->ops->pre_req)
+               host->ops->pre_req(host, mrq, is_first_req);
+}
+
+/**
+ *     mmc_post_req - Post process a completed request
+ *     @host: MMC host to post process command
+ *     @mrq: MMC request to post process for
+ *     @err: Error, if non zero, clean up any resources made in pre_req
+ *
+ *     Let the host post process a completed request. Post processing of
+ *     a request may be performed while another reuqest is running.
+ */
+static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
+                        int err)
+{
+       if (host->ops->post_req)
+               host->ops->post_req(host, mrq, err);
+}
+
+/**
+ *     mmc_start_req - start a non-blocking request
+ *     @host: MMC host to start command
+ *     @areq: async request to start
+ *     @error: out parameter returns 0 for success, otherwise non zero
+ *
+ *     Start a new MMC custom command request for a host.
+ *     If there is on ongoing async request wait for completion
+ *     of that request and start the new one and return.
+ *     Does not wait for the new request to complete.
+ *
+ *      Returns the completed request, NULL in case of none completed.
+ *     Wait for the an ongoing request (previoulsy started) to complete and
+ *     return the completed request. If there is no ongoing request, NULL
+ *     is returned without waiting. NULL is not an error condition.
+ */
+struct mmc_async_req *mmc_start_req(struct mmc_host *host,
+                                   struct mmc_async_req *areq, int *error)
+{
+       int err = 0;
+       struct mmc_async_req *data = host->areq;
+
+       /* Prepare a new request */
+       if (areq)
+               mmc_pre_req(host, areq->mrq, !host->areq);
+
+       if (host->areq) {
+               mmc_wait_for_req_done(host, host->areq->mrq);
+               err = host->areq->err_check(host->card, host->areq);
+               if (err) {
+                       mmc_post_req(host, host->areq->mrq, 0);
+                       if (areq)
+                               mmc_post_req(host, areq->mrq, -EINVAL);
+
+                       host->areq = NULL;
+                       goto out;
+               }
+       }
+
+       if (areq)
+               __mmc_start_req(host, areq->mrq);
+
+       if (host->areq)
+               mmc_post_req(host, host->areq->mrq, 0);
+
+       host->areq = areq;
+ out:
+       if (error)
+               *error = err;
+       return data;
+}
+EXPORT_SYMBOL(mmc_start_req);
+
 /**
  *     mmc_wait_for_req - start a request and wait for completion
  *     @host: MMC host to start command
@@ -212,16 +312,9 @@ static void mmc_wait_done(struct mmc_request *mrq)
  */
 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
 {
-       DECLARE_COMPLETION_ONSTACK(complete);
-
-       mrq->done_data = &complete;
-       mrq->done = mmc_wait_done;
-
-       mmc_start_request(host, mrq);
-
-       wait_for_completion(&complete);
+       __mmc_start_req(host, mrq);
+       mmc_wait_for_req_done(host, mrq);
 }
-
 EXPORT_SYMBOL(mmc_wait_for_req);
 
 /**
@@ -236,12 +329,10 @@ 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;
+       struct mmc_request mrq = {0};
 
        WARN_ON(!host->claimed);
 
-       memset(&mrq, 0, sizeof(struct mmc_request));
-
        memset(cmd->resp, 0, sizeof(cmd->resp));
        cmd->retries = retries;
 
@@ -719,23 +810,13 @@ void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
        mmc_set_ios(host);
 }
 
-/*
- * Change data bus width and DDR mode of a host.
- */
-void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
-                          unsigned int ddr)
-{
-       host->ios.bus_width = width;
-       host->ios.ddr = ddr;
-       mmc_set_ios(host);
-}
-
 /*
  * Change data bus width of a host.
  */
 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
 {
-       mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
+       host->ios.bus_width = width;
+       mmc_set_ios(host);
 }
 
 /**
@@ -944,6 +1025,38 @@ u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
        return ocr;
 }
 
+int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
+{
+       struct mmc_command cmd = {0};
+       int err = 0;
+
+       BUG_ON(!host);
+
+       /*
+        * Send CMD11 only if the request is to switch the card to
+        * 1.8V signalling.
+        */
+       if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
+               cmd.opcode = SD_SWITCH_VOLTAGE;
+               cmd.arg = 0;
+               cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
+
+               err = mmc_wait_for_cmd(host, &cmd, 0);
+               if (err)
+                       return err;
+
+               if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
+                       return -EIO;
+       }
+
+       host->ios.signal_voltage = signal_voltage;
+
+       if (host->ops->start_signal_voltage_switch)
+               err = host->ops->start_signal_voltage_switch(host, &host->ios);
+
+       return err;
+}
+
 /*
  * Select timing parameters for host.
  */
@@ -953,6 +1066,15 @@ void mmc_set_timing(struct mmc_host *host, unsigned int timing)
        mmc_set_ios(host);
 }
 
+/*
+ * Select appropriate driver type for host.
+ */
+void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
+{
+       host->ios.drv_type = drv_type;
+       mmc_set_ios(host);
+}
+
 /*
  * Apply power to the MMC stack.  This is a two-stage process.
  * First, we enable power to the card without the clock running.
@@ -1187,9 +1309,8 @@ void mmc_init_erase(struct mmc_card *card)
        }
 }
 
-static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
-                                     struct mmc_command *cmd,
-                                     unsigned int arg, unsigned int qty)
+static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
+                                         unsigned int arg, unsigned int qty)
 {
        unsigned int erase_timeout;
 
@@ -1217,7 +1338,7 @@ static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
                 */
                timeout_clks <<= 1;
                timeout_us += (timeout_clks * 1000) /
-                             (card->host->ios.clock / 1000);
+                             (mmc_host_clk_rate(card->host) / 1000);
 
                erase_timeout = timeout_us / 1000;
 
@@ -1246,44 +1367,48 @@ static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
        if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
                erase_timeout = 1000;
 
-       cmd->erase_timeout = erase_timeout;
+       return erase_timeout;
 }
 
-static void mmc_set_sd_erase_timeout(struct mmc_card *card,
-                                    struct mmc_command *cmd, unsigned int arg,
-                                    unsigned int qty)
+static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
+                                        unsigned int arg,
+                                        unsigned int qty)
 {
+       unsigned int erase_timeout;
+
        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;
+               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;
+               erase_timeout = 250 * qty;
        }
 
        /* Must not be less than 1 second */
-       if (cmd->erase_timeout < 1000)
-               cmd->erase_timeout = 1000;
+       if (erase_timeout < 1000)
+               erase_timeout = 1000;
+
+       return erase_timeout;
 }
 
-static void mmc_set_erase_timeout(struct mmc_card *card,
-                                 struct mmc_command *cmd, unsigned int arg,
-                                 unsigned int qty)
+static unsigned int mmc_erase_timeout(struct mmc_card *card,
+                                     unsigned int arg,
+                                     unsigned int qty)
 {
        if (mmc_card_sd(card))
-               mmc_set_sd_erase_timeout(card, cmd, arg, qty);
+               return mmc_sd_erase_timeout(card, arg, qty);
        else
-               mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
+               return mmc_mmc_erase_timeout(card, arg, qty);
 }
 
 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
                        unsigned int to, unsigned int arg)
 {
-       struct mmc_command cmd;
+       struct mmc_command cmd = {0};
        unsigned int qty = 0;
        int err;
 
@@ -1317,7 +1442,6 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from,
                to <<= 9;
        }
 
-       memset(&cmd, 0, sizeof(struct mmc_command));
        if (mmc_card_sd(card))
                cmd.opcode = SD_ERASE_WR_BLK_START;
        else
@@ -1351,7 +1475,7 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from,
        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);
+       cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
        err = mmc_wait_for_cmd(card->host, &cmd, 0);
        if (err) {
                printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
@@ -1485,14 +1609,89 @@ int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
 }
 EXPORT_SYMBOL(mmc_erase_group_aligned);
 
+static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
+                                           unsigned int arg)
+{
+       struct mmc_host *host = card->host;
+       unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
+       unsigned int last_timeout = 0;
+
+       if (card->erase_shift)
+               max_qty = UINT_MAX >> card->erase_shift;
+       else if (mmc_card_sd(card))
+               max_qty = UINT_MAX;
+       else
+               max_qty = UINT_MAX / card->erase_size;
+
+       /* Find the largest qty with an OK timeout */
+       do {
+               y = 0;
+               for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
+                       timeout = mmc_erase_timeout(card, arg, qty + x);
+                       if (timeout > host->max_discard_to)
+                               break;
+                       if (timeout < last_timeout)
+                               break;
+                       last_timeout = timeout;
+                       y = x;
+               }
+               qty += y;
+       } while (y);
+
+       if (!qty)
+               return 0;
+
+       if (qty == 1)
+               return 1;
+
+       /* Convert qty to sectors */
+       if (card->erase_shift)
+               max_discard = --qty << card->erase_shift;
+       else if (mmc_card_sd(card))
+               max_discard = qty;
+       else
+               max_discard = --qty * card->erase_size;
+
+       return max_discard;
+}
+
+unsigned int mmc_calc_max_discard(struct mmc_card *card)
+{
+       struct mmc_host *host = card->host;
+       unsigned int max_discard, max_trim;
+
+       if (!host->max_discard_to)
+               return UINT_MAX;
+
+       /*
+        * Without erase_group_def set, MMC erase timeout depends on clock
+        * frequence which can change.  In that case, the best choice is
+        * just the preferred erase size.
+        */
+       if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
+               return card->pref_erase;
+
+       max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
+       if (mmc_can_trim(card)) {
+               max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
+               if (max_trim < max_discard)
+                       max_discard = max_trim;
+       } else if (max_discard < card->erase_size) {
+               max_discard = 0;
+       }
+       pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
+                mmc_hostname(host), max_discard, host->max_discard_to);
+       return max_discard;
+}
+EXPORT_SYMBOL(mmc_calc_max_discard);
+
 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
 {
-       struct mmc_command cmd;
+       struct mmc_command cmd = {0};
 
        if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
                return 0;
 
-       memset(&cmd, 0, sizeof(struct mmc_command));
        cmd.opcode = MMC_SET_BLOCKLEN;
        cmd.arg = blocklen;
        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
@@ -1578,7 +1777,7 @@ void mmc_rescan(struct work_struct *work)
        for (i = 0; i < ARRAY_SIZE(freqs); i++) {
                if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
                        break;
-               if (freqs[i] < host->f_min)
+               if (freqs[i] <= host->f_min)
                        break;
        }
        mmc_release_host(host);
@@ -1633,6 +1832,10 @@ int mmc_power_save_host(struct mmc_host *host)
 {
        int ret = 0;
 
+#ifdef CONFIG_MMC_DEBUG
+       pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
+#endif
+
        mmc_bus_get(host);
 
        if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
@@ -1655,6 +1858,10 @@ int mmc_power_restore_host(struct mmc_host *host)
 {
        int ret;
 
+#ifdef CONFIG_MMC_DEBUG
+       pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
+#endif
+
        mmc_bus_get(host);
 
        if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
@@ -1746,7 +1953,7 @@ int mmc_suspend_host(struct mmc_host *host)
        }
        mmc_bus_put(host);
 
-       if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
+       if (!err && !mmc_card_keep_power(host))
                mmc_power_off(host);
 
        return err;
@@ -1764,7 +1971,7 @@ int mmc_resume_host(struct mmc_host *host)
 
        mmc_bus_get(host);
        if (host->bus_ops && !host->bus_dead) {
-               if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
+               if (!mmc_card_keep_power(host)) {
                        mmc_power_up(host);
                        mmc_select_voltage(host, host->ocr);
                        /*
@@ -1789,6 +1996,7 @@ int mmc_resume_host(struct mmc_host *host)
                        err = 0;
                }
        }
+       host->pm_flags &= ~MMC_PM_KEEP_POWER;
        mmc_bus_put(host);
 
        return err;