]> 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 e8d61c307db3dcaa9a5831b27eb7092624f80ebc..f6d31f58488f427c8099be59e7624533efcd492e 100644 (file)
@@ -30,7 +30,6 @@
 #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*/
@@ -50,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)
@@ -87,6 +179,7 @@ mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
 {
        struct mmc_cmd cmd;
        struct mmc_data data;
+       int timeout = 1000;
 
        if ((start + blkcnt) > mmc->block_dev.lba) {
                printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
@@ -129,6 +222,9 @@ mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
                        printf("mmc fail to send stop cmd\n");
                        return 0;
                }
+
+               /* Waiting for the ready status */
+               mmc_send_status(mmc, timeout);
        }
 
        return blkcnt;
@@ -147,8 +243,7 @@ mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
                return 0;
 
        do {
-               cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
-                      CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
+               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;
@@ -163,6 +258,7 @@ 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;
 
        if (blkcnt > 1)
                cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
@@ -194,6 +290,9 @@ int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
                        printf("mmc fail to send stop cmd\n");
                        return 0;
                }
+
+               /* Waiting for the ready status */
+               mmc_send_status(mmc, timeout);
        }
 
        return blkcnt;
@@ -220,8 +319,7 @@ static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
                return 0;
 
        do {
-               cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
-                      CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
+               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;
@@ -324,18 +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_host_is_spi(mmc) ? 0 :
-                                       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);
@@ -397,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)
@@ -430,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);
@@ -644,6 +762,7 @@ int mmc_startup(struct mmc *mmc)
        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 */
@@ -700,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;
 
@@ -905,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);