]> git.sur5r.net Git - u-boot/blobdiff - drivers/mmc/mmc.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[u-boot] / drivers / mmc / mmc.c
index 23928c1356e76fcb472a138ad4e6a3e51f864f63..f6d31f58488f427c8099be59e7624533efcd492e 100644 (file)
 #include <part.h>
 #include <malloc.h>
 #include <linux/list.h>
-#include <mmc.h>
 #include <div64.h>
 
+/* Set block count limit because of 16 bit register limit on some hardware*/
+#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
+#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
+#endif
+
 static struct list_head mmc_devices;
 static int cur_dev_num = -1;
 
@@ -45,7 +49,100 @@ int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
 
 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
 {
+#ifdef CONFIG_MMC_TRACE
+       int ret;
+       int i;
+       u8 *ptr;
+
+       printf("CMD_SEND:%d\n", cmd->cmdidx);
+       printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
+       printf("\t\tFLAG\t\t\t %d\n", cmd->flags);
+       ret = mmc->send_cmd(mmc, cmd, data);
+       switch (cmd->resp_type) {
+               case MMC_RSP_NONE:
+                       printf("\t\tMMC_RSP_NONE\n");
+                       break;
+               case MMC_RSP_R1:
+                       printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
+                               cmd->response[0]);
+                       break;
+               case MMC_RSP_R1b:
+                       printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
+                               cmd->response[0]);
+                       break;
+               case MMC_RSP_R2:
+                       printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
+                               cmd->response[0]);
+                       printf("\t\t          \t\t 0x%08X \n",
+                               cmd->response[1]);
+                       printf("\t\t          \t\t 0x%08X \n",
+                               cmd->response[2]);
+                       printf("\t\t          \t\t 0x%08X \n",
+                               cmd->response[3]);
+                       printf("\n");
+                       printf("\t\t\t\t\tDUMPING DATA\n");
+                       for (i = 0; i < 4; i++) {
+                               int j;
+                               printf("\t\t\t\t\t%03d - ", i*4);
+                               ptr = &cmd->response[i];
+                               ptr += 3;
+                               for (j = 0; j < 4; j++)
+                                       printf("%02X ", *ptr--);
+                               printf("\n");
+                       }
+                       break;
+               case MMC_RSP_R3:
+                       printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
+                               cmd->response[0]);
+                       break;
+               default:
+                       printf("\t\tERROR MMC rsp not supported\n");
+                       break;
+       }
+       return ret;
+#else
        return mmc->send_cmd(mmc, cmd, data);
+#endif
+}
+
+int mmc_send_status(struct mmc *mmc, int timeout)
+{
+       struct mmc_cmd cmd;
+       int err;
+#ifdef CONFIG_MMC_TRACE
+       int status;
+#endif
+
+       cmd.cmdidx = MMC_CMD_SEND_STATUS;
+       cmd.resp_type = MMC_RSP_R1;
+       cmd.cmdarg = 0;
+       cmd.flags = 0;
+
+       do {
+               err = mmc_send_cmd(mmc, &cmd, NULL);
+               if (err)
+                       return err;
+               else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
+                       break;
+
+               udelay(1000);
+
+               if (cmd.response[0] & MMC_STATUS_MASK) {
+                       printf("Status Error: 0x%08X\n", cmd.response[0]);
+                       return COMM_ERR;
+               }
+       } while (timeout--);
+
+#ifdef CONFIG_MMC_TRACE
+       status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
+       printf("CURR STATE:%d\n", status);
+#endif
+       if (!timeout) {
+               printf("Timeout waiting card ready\n");
+               return TIMEOUT;
+       }
+
+       return 0;
 }
 
 int mmc_set_blocklen(struct mmc *mmc, int len)
@@ -78,31 +175,17 @@ struct mmc *find_mmc_device(int dev_num)
 }
 
 static ulong
-mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
+mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
 {
        struct mmc_cmd cmd;
        struct mmc_data data;
-       int err;
-       int stoperr = 0;
-       struct mmc *mmc = find_mmc_device(dev_num);
-       int blklen;
-
-       if (!mmc)
-               return -1;
-
-       blklen = mmc->write_bl_len;
+       int timeout = 1000;
 
        if ((start + blkcnt) > mmc->block_dev.lba) {
-               printf("MMC: block number 0x%x exceeds max(0x%x)",
+               printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
                        start + blkcnt, mmc->block_dev.lba);
                return 0;
        }
-       err = mmc_set_blocklen(mmc, mmc->write_bl_len);
-
-       if (err) {
-               printf("set write bl len failed\n\r");
-               return err;
-       }
 
        if (blkcnt > 1)
                cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
@@ -112,138 +195,137 @@ mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
        if (mmc->high_capacity)
                cmd.cmdarg = start;
        else
-               cmd.cmdarg = start * blklen;
+               cmd.cmdarg = start * mmc->write_bl_len;
 
        cmd.resp_type = MMC_RSP_R1;
        cmd.flags = 0;
 
        data.src = src;
        data.blocks = blkcnt;
-       data.blocksize = blklen;
+       data.blocksize = mmc->write_bl_len;
        data.flags = MMC_DATA_WRITE;
 
-       err = mmc_send_cmd(mmc, &cmd, &data);
-
-       if (err) {
-               printf("mmc write failed\n\r");
-               return err;
+       if (mmc_send_cmd(mmc, &cmd, &data)) {
+               printf("mmc write failed\n");
+               return 0;
        }
 
-       if (blkcnt > 1) {
+       /* SPI multiblock writes terminate using a special
+        * token, not a STOP_TRANSMISSION request.
+        */
+       if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
                cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
                cmd.cmdarg = 0;
                cmd.resp_type = MMC_RSP_R1b;
                cmd.flags = 0;
-               stoperr = mmc_send_cmd(mmc, &cmd, NULL);
+               if (mmc_send_cmd(mmc, &cmd, NULL)) {
+                       printf("mmc fail to send stop cmd\n");
+                       return 0;
+               }
+
+               /* Waiting for the ready status */
+               mmc_send_status(mmc, timeout);
        }
 
        return blkcnt;
 }
 
-int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
+static ulong
+mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
+{
+       lbaint_t cur, blocks_todo = blkcnt;
+
+       struct mmc *mmc = find_mmc_device(dev_num);
+       if (!mmc)
+               return 0;
+
+       if (mmc_set_blocklen(mmc, mmc->write_bl_len))
+               return 0;
+
+       do {
+               cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
+               if(mmc_write_blocks(mmc, start, cur, src) != cur)
+                       return 0;
+               blocks_todo -= cur;
+               start += cur;
+               src += cur * mmc->write_bl_len;
+       } while (blocks_todo > 0);
+
+       return blkcnt;
+}
+
+int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
 {
        struct mmc_cmd cmd;
        struct mmc_data data;
+       int timeout = 1000;
 
-       cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
+       if (blkcnt > 1)
+               cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
+       else
+               cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
 
        if (mmc->high_capacity)
-               cmd.cmdarg = blocknum;
+               cmd.cmdarg = start;
        else
-               cmd.cmdarg = blocknum * mmc->read_bl_len;
+               cmd.cmdarg = start * mmc->read_bl_len;
 
        cmd.resp_type = MMC_RSP_R1;
        cmd.flags = 0;
 
        data.dest = dst;
-       data.blocks = 1;
+       data.blocks = blkcnt;
        data.blocksize = mmc->read_bl_len;
        data.flags = MMC_DATA_READ;
 
-       return mmc_send_cmd(mmc, &cmd, &data);
-}
-
-int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
-{
-       char *buffer;
-       int i;
-       int blklen = mmc->read_bl_len;
-       int startblock = lldiv(src, mmc->read_bl_len);
-       int endblock = lldiv(src + size - 1, mmc->read_bl_len);
-       int err = 0;
-
-       /* Make a buffer big enough to hold all the blocks we might read */
-       buffer = malloc(blklen);
-
-       if (!buffer) {
-               printf("Could not allocate buffer for MMC read!\n");
-               return -1;
-       }
-
-       /* We always do full block reads from the card */
-       err = mmc_set_blocklen(mmc, mmc->read_bl_len);
-
-       if (err)
-               goto free_buffer;
-
-       for (i = startblock; i <= endblock; i++) {
-               int segment_size;
-               int offset;
-
-               err = mmc_read_block(mmc, buffer, i);
-
-               if (err)
-                       goto free_buffer;
-
-               /*
-                * The first block may not be aligned, so we
-                * copy from the desired point in the block
-                */
-               offset = (src & (blklen - 1));
-               segment_size = MIN(blklen - offset, size);
+       if (mmc_send_cmd(mmc, &cmd, &data))
+               return 0;
 
-               memcpy(dst, buffer + offset, segment_size);
+       if (blkcnt > 1) {
+               cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
+               cmd.cmdarg = 0;
+               cmd.resp_type = MMC_RSP_R1b;
+               cmd.flags = 0;
+               if (mmc_send_cmd(mmc, &cmd, NULL)) {
+                       printf("mmc fail to send stop cmd\n");
+                       return 0;
+               }
 
-               dst += segment_size;
-               src += segment_size;
-               size -= segment_size;
+               /* Waiting for the ready status */
+               mmc_send_status(mmc, timeout);
        }
 
-free_buffer:
-       free(buffer);
-
-       return err;
+       return blkcnt;
 }
 
 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
 {
-       int err;
-       int i;
-       struct mmc *mmc = find_mmc_device(dev_num);
+       lbaint_t cur, blocks_todo = blkcnt;
+
+       if (blkcnt == 0)
+               return 0;
 
+       struct mmc *mmc = find_mmc_device(dev_num);
        if (!mmc)
                return 0;
 
        if ((start + blkcnt) > mmc->block_dev.lba) {
-               printf("MMC: block number 0x%x exceeds max(0x%x)",
+               printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
                        start + blkcnt, mmc->block_dev.lba);
                return 0;
        }
-       /* We always do full block reads from the card */
-       err = mmc_set_blocklen(mmc, mmc->read_bl_len);
 
-       if (err) {
+       if (mmc_set_blocklen(mmc, mmc->read_bl_len))
                return 0;
-       }
 
-       for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
-               err = mmc_read_block(mmc, dst, i);
-
-               if (err) {
-                       printf("block read failed: %d\n", err);
-                       return i - start;
-               }
-       }
+       do {
+               cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
+               if(mmc_read_blocks(mmc, dst, start, cur) != cur)
+                       return 0;
+               blocks_todo -= cur;
+               start += cur;
+               dst += cur * mmc->read_bl_len;
+       } while (blocks_todo > 0);
 
        return blkcnt;
 }
@@ -298,7 +380,8 @@ sd_send_op_cond(struct mmc *mmc)
                 * how to manage low voltages SD card is not yet
                 * specified.
                 */
-               cmd.cmdarg = mmc->voltages & 0xff8000;
+               cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
+                       (mmc->voltages & 0xff8000);
 
                if (mmc->version == SD_VERSION_2)
                        cmd.cmdarg |= OCR_HCS;
@@ -317,6 +400,18 @@ sd_send_op_cond(struct mmc *mmc)
        if (mmc->version != SD_VERSION_2)
                mmc->version = SD_VERSION_1_0;
 
+       if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
+               cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
+               cmd.resp_type = MMC_RSP_R3;
+               cmd.cmdarg = 0;
+               cmd.flags = 0;
+
+               err = mmc_send_cmd(mmc, &cmd, NULL);
+
+               if (err)
+                       return err;
+       }
+
        mmc->ocr = cmd.response[0];
 
        mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
@@ -327,17 +422,33 @@ sd_send_op_cond(struct mmc *mmc)
 
 int mmc_send_op_cond(struct mmc *mmc)
 {
-       int timeout = 1000;
+       int timeout = 10000;
        struct mmc_cmd cmd;
        int err;
 
        /* Some cards seem to need this */
        mmc_go_idle(mmc);
 
+       /* Asking to the card its capabilities */
+       cmd.cmdidx = MMC_CMD_SEND_OP_COND;
+       cmd.resp_type = MMC_RSP_R3;
+       cmd.cmdarg = 0;
+       cmd.flags = 0;
+       err = mmc_send_cmd(mmc, &cmd, NULL);
+       if (err)
+               return err;
+       udelay(1000);
        do {
                cmd.cmdidx = MMC_CMD_SEND_OP_COND;
                cmd.resp_type = MMC_RSP_R3;
-               cmd.cmdarg = OCR_HCS | mmc->voltages;
+               cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
+                               (mmc->voltages &
+                               (cmd.response[0] & OCR_VOLTAGE_MASK)) |
+                               (cmd.response[0] & OCR_ACCESS_MODE));
                cmd.flags = 0;
 
                err = mmc_send_cmd(mmc, &cmd, NULL);
@@ -351,6 +462,18 @@ int mmc_send_op_cond(struct mmc *mmc)
        if (timeout <= 0)
                return UNUSABLE_ERR;
 
+       if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
+               cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
+               cmd.resp_type = MMC_RSP_R3;
+               cmd.cmdarg = 0;
+               cmd.flags = 0;
+
+               err = mmc_send_cmd(mmc, &cmd, NULL);
+
+               if (err)
+                       return err;
+       }
+
        mmc->version = MMC_VERSION_UNKNOWN;
        mmc->ocr = cmd.response[0];
 
@@ -387,15 +510,23 @@ int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
 {
        struct mmc_cmd cmd;
+       int timeout = 1000;
+       int ret;
 
        cmd.cmdidx = MMC_CMD_SWITCH;
        cmd.resp_type = MMC_RSP_R1b;
        cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
-               (index << 16) |
-               (value << 8);
+                                (index << 16) |
+                                (value << 8);
        cmd.flags = 0;
 
-       return mmc_send_cmd(mmc, &cmd, NULL);
+       ret = mmc_send_cmd(mmc, &cmd, NULL);
+
+       /* Waiting for the ready status */
+       mmc_send_status(mmc, timeout);
+
+       return ret;
+
 }
 
 int mmc_change_freq(struct mmc *mmc)
@@ -406,6 +537,9 @@ int mmc_change_freq(struct mmc *mmc)
 
        mmc->card_caps = 0;
 
+       if (mmc_host_is_spi(mmc))
+               return 0;
+
        /* Only version 4 supports high-speed */
        if (mmc->version < MMC_VERSION_4)
                return 0;
@@ -417,9 +551,6 @@ int mmc_change_freq(struct mmc *mmc)
        if (err)
                return err;
 
-       if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
-               mmc->high_capacity = 1;
-
        cardtype = ext_csd[196] & 0xf;
 
        err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
@@ -479,6 +610,9 @@ int sd_change_freq(struct mmc *mmc)
 
        mmc->card_caps = 0;
 
+       if (mmc_host_is_spi(mmc))
+               return 0;
+
        /* Read the SCR to find out if this card supports higher speeds */
        cmd.cmdidx = MMC_CMD_APP_CMD;
        cmd.resp_type = MMC_RSP_R1;
@@ -530,6 +664,9 @@ retry_scr:
                        break;
        }
 
+       if (mmc->scr[0] & SD_DATA_4BIT)
+               mmc->card_caps |= MMC_MODE_4BIT;
+
        /* Version 1.0 doesn't support switching */
        if (mmc->version == SD_VERSION_1_0)
                return 0;
@@ -547,9 +684,6 @@ retry_scr:
                        break;
        }
 
-       if (mmc->scr[0] & SD_DATA_4BIT)
-               mmc->card_caps |= MMC_MODE_4BIT;
-
        /* If high-speed isn't supported, we return */
        if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
                return 0;
@@ -567,7 +701,7 @@ retry_scr:
 
 /* frequency bases */
 /* divided by 10 to be nice to platforms without floating point */
-int fbase[] = {
+static const int fbase[] = {
        10000,
        100000,
        1000000,
@@ -577,7 +711,7 @@ int fbase[] = {
 /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
  * to platforms without floating point.
  */
-int multipliers[] = {
+static const int multipliers[] = {
        0,      /* reserved */
        10,
        12,
@@ -627,9 +761,25 @@ int mmc_startup(struct mmc *mmc)
        uint mult, freq;
        u64 cmult, csize;
        struct mmc_cmd cmd;
+       char ext_csd[512];
+       int timeout = 1000;
+
+#ifdef CONFIG_MMC_SPI_CRC_ON
+       if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
+               cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
+               cmd.resp_type = MMC_RSP_R1;
+               cmd.cmdarg = 1;
+               cmd.flags = 0;
+               err = mmc_send_cmd(mmc, &cmd, NULL);
+
+               if (err)
+                       return err;
+       }
+#endif
 
        /* Put the Card in Identify Mode */
-       cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
+       cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
+               MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
        cmd.resp_type = MMC_RSP_R2;
        cmd.cmdarg = 0;
        cmd.flags = 0;
@@ -646,18 +796,20 @@ int mmc_startup(struct mmc *mmc)
         * For SD cards, get the Relatvie Address.
         * This also puts the cards into Standby State
         */
-       cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
-       cmd.cmdarg = mmc->rca << 16;
-       cmd.resp_type = MMC_RSP_R6;
-       cmd.flags = 0;
+       if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
+               cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
+               cmd.cmdarg = mmc->rca << 16;
+               cmd.resp_type = MMC_RSP_R6;
+               cmd.flags = 0;
 
-       err = mmc_send_cmd(mmc, &cmd, NULL);
+               err = mmc_send_cmd(mmc, &cmd, NULL);
 
-       if (err)
-               return err;
+               if (err)
+                       return err;
 
-       if (IS_SD(mmc))
-               mmc->rca = (cmd.response[0] >> 16) & 0xffff;
+               if (IS_SD(mmc))
+                       mmc->rca = (cmd.response[0] >> 16) & 0xffff;
+       }
 
        /* Get the Card-Specific Data */
        cmd.cmdidx = MMC_CMD_SEND_CSD;
@@ -667,6 +819,9 @@ int mmc_startup(struct mmc *mmc)
 
        err = mmc_send_cmd(mmc, &cmd, NULL);
 
+       /* Waiting for the ready status */
+       mmc_send_status(mmc, timeout);
+
        if (err)
                return err;
 
@@ -733,14 +888,26 @@ int mmc_startup(struct mmc *mmc)
                mmc->write_bl_len = 512;
 
        /* Select the card, and put it into Transfer Mode */
-       cmd.cmdidx = MMC_CMD_SELECT_CARD;
-       cmd.resp_type = MMC_RSP_R1b;
-       cmd.cmdarg = mmc->rca << 16;
-       cmd.flags = 0;
-       err = mmc_send_cmd(mmc, &cmd, NULL);
+       if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
+               cmd.cmdidx = MMC_CMD_SELECT_CARD;
+               cmd.resp_type = MMC_RSP_R1b;
+               cmd.cmdarg = mmc->rca << 16;
+               cmd.flags = 0;
+               err = mmc_send_cmd(mmc, &cmd, NULL);
 
-       if (err)
-               return err;
+               if (err)
+                       return err;
+       }
+
+       if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
+               /* check  ext_csd version and capacity */
+               err = mmc_send_ext_csd(mmc, ext_csd);
+               if (!err & (ext_csd[192] >= 2)) {
+                       mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
+                                       ext_csd[214] << 16 | ext_csd[215] << 24;
+                       mmc->capacity *= 512;
+               }
+       }
 
        if (IS_SD(mmc))
                err = sd_change_freq(mmc);
@@ -860,6 +1027,8 @@ int mmc_register(struct mmc *mmc)
        mmc->block_dev.removable = 1;
        mmc->block_dev.block_read = mmc_bread;
        mmc->block_dev.block_write = mmc_bwrite;
+       if (!mmc->b_max)
+               mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
 
        INIT_LIST_HEAD (&mmc->link);