[MMC] Cleanup 385e3227d4d83ab13d7767c4bb3593b0256bf246
[safe/jmp/linux-2.6] / drivers / mmc / mmc.c
index da52839..74eaaee 100644 (file)
@@ -9,7 +9,6 @@
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
 
 #include "mmc.h"
 
-#ifdef CONFIG_MMC_DEBUG
-#define DBG(x...)      printk(KERN_DEBUG x)
-#else
-#define DBG(x...)      do { } while (0)
-#endif
-
 #define CMD_RETRIES    3
 
 /*
@@ -65,20 +58,23 @@ static const unsigned int tacc_mant[] = {
 
 
 /**
- *     mmc_request_done - finish processing an MMC command
- *     @host: MMC host which completed command
- *     @mrq: MMC request which completed
+ *     mmc_request_done - finish processing an MMC request
+ *     @host: MMC host which completed request
+ *     @mrq: MMC request which request
  *
  *     MMC drivers should call this function when they have completed
- *     their processing of a command.  This should be called before the
- *     data part of the command has completed.
+ *     their processing of a request.
  */
 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
 {
        struct mmc_command *cmd = mrq->cmd;
-       int err = mrq->cmd->error;
-       DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode,
-           err, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
+       int err = cmd->error;
+
+       pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
+                mmc_hostname(host), cmd->opcode, err,
+                mrq->data ? mrq->data->error : 0,
+                mrq->stop ? mrq->stop->error : 0,
+                cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
 
        if (err && cmd->retries) {
                cmd->retries--;
@@ -102,8 +98,9 @@ EXPORT_SYMBOL(mmc_request_done);
 void
 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
 {
-       DBG("MMC: starting cmd %02x arg %08x flags %08x\n",
-           mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
+       pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
+                mmc_hostname(host), mrq->cmd->opcode,
+                mrq->cmd->arg, mrq->cmd->flags);
 
        WARN_ON(host->card_busy == NULL);
 
@@ -131,7 +128,7 @@ static void mmc_wait_done(struct mmc_request *mrq)
 
 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
 {
-       DECLARE_COMPLETION(complete);
+       DECLARE_COMPLETION_ONSTACK(complete);
 
        mrq->done_data = &complete;
        mrq->done = mmc_wait_done;
@@ -211,7 +208,7 @@ int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
 
                appcmd.opcode = MMC_APP_CMD;
                appcmd.arg = rca << 16;
-               appcmd.flags = MMC_RSP_R1;
+               appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
                appcmd.retries = 0;
                memset(appcmd.resp, 0, sizeof(appcmd.resp));
                appcmd.data = NULL;
@@ -250,6 +247,55 @@ int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
 
 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
 
+/**
+ *     mmc_set_data_timeout - set the timeout for a data command
+ *     @data: data phase for command
+ *     @card: the MMC card associated with the data transfer
+ *     @write: flag to differentiate reads from writes
+ */
+void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
+                         int write)
+{
+       unsigned int mult;
+
+       /*
+        * SD cards use a 100 multiplier rather than 10
+        */
+       mult = mmc_card_sd(card) ? 100 : 10;
+
+       /*
+        * Scale up the multiplier (and therefore the timeout) by
+        * the r2w factor for writes.
+        */
+       if (write)
+               mult <<= card->csd.r2w_factor;
+
+       data->timeout_ns = card->csd.tacc_ns * mult;
+       data->timeout_clks = card->csd.tacc_clks * mult;
+
+       /*
+        * SD cards also have an upper limit on the timeout.
+        */
+       if (mmc_card_sd(card)) {
+               unsigned int timeout_us, limit_us;
+
+               timeout_us = data->timeout_ns / 1000;
+               timeout_us += data->timeout_clks * 1000 /
+                       (card->host->ios.clock / 1000);
+
+               if (write)
+                       limit_us = 250000;
+               else
+                       limit_us = 100000;
+
+               if (timeout_us > limit_us) {
+                       data->timeout_ns = limit_us * 1000;
+                       data->timeout_clks = 0;
+               }
+       }
+}
+EXPORT_SYMBOL(mmc_set_data_timeout);
+
 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
 
 /**
@@ -317,6 +363,18 @@ void mmc_release_host(struct mmc_host *host)
 
 EXPORT_SYMBOL(mmc_release_host);
 
+static inline void mmc_set_ios(struct mmc_host *host)
+{
+       struct mmc_ios *ios = &host->ios;
+
+       pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
+                mmc_hostname(host), ios->clock, ios->bus_mode,
+                ios->power_mode, ios->chip_select, ios->vdd,
+                ios->bus_width);
+       
+       host->ops->set_ios(host, ios);
+}
+
 static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
 {
        int err;
@@ -331,7 +389,7 @@ static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
 
        cmd.opcode = MMC_SELECT_CARD;
        cmd.arg = card->rca << 16;
-       cmd.flags = MMC_RSP_R1;
+       cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 
        err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
        if (err != MMC_ERR_NONE)
@@ -358,7 +416,7 @@ static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
                        struct mmc_command cmd;
                        cmd.opcode = SD_APP_SET_BUS_WIDTH;
                        cmd.arg = SD_BUS_WIDTH_4;
-                       cmd.flags = MMC_RSP_R1;
+                       cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 
                        err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
                                CMD_RETRIES);
@@ -369,7 +427,7 @@ static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
                }
        }
 
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 
        return MMC_ERR_NONE;
 }
@@ -386,7 +444,7 @@ static void mmc_deselect_cards(struct mmc_host *host)
 
                cmd.opcode = MMC_SELECT_CARD;
                cmd.arg = 0;
-               cmd.flags = MMC_RSP_NONE;
+               cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
 
                mmc_wait_for_cmd(host, &cmd, 0);
        }
@@ -420,7 +478,7 @@ static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
                ocr = 3 << bit;
 
                host->ios.vdd = bit;
-               host->ops->set_ios(host, &host->ios);
+               mmc_set_ios(host);
        } else {
                ocr = 0;
        }
@@ -495,6 +553,7 @@ static void mmc_decode_cid(struct mmc_card *card)
 
                case 2: /* MMC v2.0 - v2.2 */
                case 3: /* MMC v3.1 - v3.3 */
+               case 4: /* MMC v4 */
                        card->cid.manfid        = UNSTUFF_BITS(resp, 120, 8);
                        card->cid.oemid         = UNSTUFF_BITS(resp, 104, 16);
                        card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
@@ -550,6 +609,12 @@ static void mmc_decode_csd(struct mmc_card *card)
                csd->capacity     = (1 + m) << (e + 2);
 
                csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
+               csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
+               csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
+               csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
+               csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
+               csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
+               csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
        } else {
                /*
                 * We only understand CSD structure v1.1 and v1.2.
@@ -579,6 +644,12 @@ static void mmc_decode_csd(struct mmc_card *card)
                csd->capacity     = (1 + m) << (e + 2);
 
                csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
+               csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
+               csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
+               csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
+               csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
+               csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
+               csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
        }
 }
 
@@ -660,26 +731,34 @@ static void mmc_idle_cards(struct mmc_host *host)
        struct mmc_command cmd;
 
        host->ios.chip_select = MMC_CS_HIGH;
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 
        mmc_delay(1);
 
        cmd.opcode = MMC_GO_IDLE_STATE;
        cmd.arg = 0;
-       cmd.flags = MMC_RSP_NONE;
+       cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
 
        mmc_wait_for_cmd(host, &cmd, 0);
 
        mmc_delay(1);
 
        host->ios.chip_select = MMC_CS_DONTCARE;
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 
        mmc_delay(1);
 }
 
 /*
- * Apply power to the MMC stack.
+ * Apply power to the MMC stack.  This is a two-stage process.
+ * First, we enable power to the card without the clock running.
+ * We then wait a bit for the power to stabilise.  Finally,
+ * enable the bus drivers and clock to the card.
+ *
+ * We must _NOT_ enable the clock prior to power stablising.
+ *
+ * If a host does all the power sequencing itself, ignore the
+ * initial MMC_POWER_UP stage.
  */
 static void mmc_power_up(struct mmc_host *host)
 {
@@ -690,13 +769,13 @@ static void mmc_power_up(struct mmc_host *host)
        host->ios.chip_select = MMC_CS_DONTCARE;
        host->ios.power_mode = MMC_POWER_UP;
        host->ios.bus_width = MMC_BUS_WIDTH_1;
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 
        mmc_delay(1);
 
        host->ios.clock = host->f_min;
        host->ios.power_mode = MMC_POWER_ON;
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 
        mmc_delay(2);
 }
@@ -709,7 +788,7 @@ static void mmc_power_off(struct mmc_host *host)
        host->ios.chip_select = MMC_CS_DONTCARE;
        host->ios.power_mode = MMC_POWER_OFF;
        host->ios.bus_width = MMC_BUS_WIDTH_1;
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 }
 
 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
@@ -719,7 +798,7 @@ static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
 
        cmd.opcode = MMC_SEND_OP_COND;
        cmd.arg = ocr;
-       cmd.flags = MMC_RSP_R3;
+       cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
 
        for (i = 100; i; i--) {
                err = mmc_wait_for_cmd(host, &cmd, 0);
@@ -747,7 +826,7 @@ static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
 
        cmd.opcode = SD_APP_OP_COND;
        cmd.arg = ocr;
-       cmd.flags = MMC_RSP_R3;
+       cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
 
        for (i = 100; i; i--) {
                err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
@@ -786,7 +865,7 @@ static void mmc_discover_cards(struct mmc_host *host)
 
                cmd.opcode = MMC_ALL_SEND_CID;
                cmd.arg = 0;
-               cmd.flags = MMC_RSP_R2;
+               cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
 
                err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
                if (err == MMC_ERR_TIMEOUT) {
@@ -816,7 +895,7 @@ static void mmc_discover_cards(struct mmc_host *host)
 
                        cmd.opcode = SD_SEND_RELATIVE_ADDR;
                        cmd.arg = 0;
-                       cmd.flags = MMC_RSP_R1;
+                       cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
 
                        err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
                        if (err != MMC_ERR_NONE)
@@ -837,7 +916,7 @@ static void mmc_discover_cards(struct mmc_host *host)
                } else {
                        cmd.opcode = MMC_SET_RELATIVE_ADDR;
                        cmd.arg = card->rca << 16;
-                       cmd.flags = MMC_RSP_R1;
+                       cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 
                        err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
                        if (err != MMC_ERR_NONE)
@@ -859,7 +938,7 @@ static void mmc_read_csds(struct mmc_host *host)
 
                cmd.opcode = MMC_SEND_CSD;
                cmd.arg = card->rca << 16;
-               cmd.flags = MMC_RSP_R2;
+               cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
 
                err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
                if (err != MMC_ERR_NONE) {
@@ -878,11 +957,9 @@ static void mmc_read_scrs(struct mmc_host *host)
 {
        int err;
        struct mmc_card *card;
-
        struct mmc_request mrq;
        struct mmc_command cmd;
        struct mmc_data data;
-
        struct scatterlist sg;
 
        list_for_each_entry(card, &host->cards, node) {
@@ -901,7 +978,7 @@ static void mmc_read_scrs(struct mmc_host *host)
 
                cmd.opcode = MMC_APP_CMD;
                cmd.arg = card->rca << 16;
-               cmd.flags = MMC_RSP_R1;
+               cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 
                err = mmc_wait_for_cmd(host, &cmd, 0);
                if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
@@ -913,13 +990,14 @@ static void mmc_read_scrs(struct mmc_host *host)
 
                cmd.opcode = SD_APP_SEND_SCR;
                cmd.arg = 0;
-               cmd.flags = MMC_RSP_R1;
+               cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
 
                memset(&data, 0, sizeof(struct mmc_data));
 
-               data.timeout_ns = card->csd.tacc_ns * 10;
-               data.timeout_clks = card->csd.tacc_clks * 10;
+               mmc_set_data_timeout(&data, card, 0);
+
                data.blksz_bits = 3;
+               data.blksz = 1 << 3;
                data.blocks = 1;
                data.flags = MMC_DATA_READ;
                data.sg = &sg;
@@ -932,8 +1010,9 @@ static void mmc_read_scrs(struct mmc_host *host)
 
                sg_init_one(&sg, (u8*)card->raw_scr, 8);
 
-               err = mmc_wait_for_req(host, &mrq);
-               if (err != MMC_ERR_NONE) {
+               mmc_wait_for_req(host, &mrq);
+
+               if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
                        mmc_card_set_dead(card);
                        continue;
                }
@@ -956,8 +1035,9 @@ static unsigned int mmc_calculate_clock(struct mmc_host *host)
                if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
                        max_dtr = card->csd.max_dtr;
 
-       DBG("MMC: selected %d.%03dMHz transfer rate\n",
-           max_dtr / 1000000, (max_dtr / 1000) % 1000);
+       pr_debug("%s: selected %d.%03dMHz transfer rate\n",
+                mmc_hostname(host),
+                max_dtr / 1000000, (max_dtr / 1000) % 1000);
 
        return max_dtr;
 }
@@ -983,7 +1063,7 @@ static void mmc_check_cards(struct mmc_host *host)
 
                cmd.opcode = MMC_SEND_STATUS;
                cmd.arg = card->rca << 16;
-               cmd.flags = MMC_RSP_R1;
+               cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 
                err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
                if (err == MMC_ERR_NONE)
@@ -1031,7 +1111,7 @@ static void mmc_setup(struct mmc_host *host)
        } else {
                host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
                host->ios.clock = host->f_min;
-               host->ops->set_ios(host, &host->ios);
+               mmc_set_ios(host);
 
                /*
                 * We should remember the OCR mask from the existing
@@ -1067,7 +1147,7 @@ static void mmc_setup(struct mmc_host *host)
         * Ok, now switch to push-pull mode.
         */
        host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
-       host->ops->set_ios(host, &host->ios);
+       mmc_set_ios(host);
 
        mmc_read_csds(host);
 
@@ -1113,7 +1193,7 @@ static void mmc_rescan(void *data)
                 * attached cards and the host support.
                 */
                host->ios.clock = mmc_calculate_clock(host);
-               host->ops->set_ios(host, &host->ios);
+               mmc_set_ios(host);
        }
 
        mmc_release_host(host);