]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/mmc/mmc.c
Merge branch 'master' of git://git.denx.de/u-boot-microblaze
[karo-tx-uboot.git] / drivers / mmc / mmc.c
index 95ed21eb0ad2fa8c4c2107f19ab3336abf71fef7..e5fedb395192895da9094b97c3c21919ba6a3a6c 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;
 
+int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
+       return -1;
+}
+
+int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
+       alias("__board_mmc_getcd")));
+
 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;
+       if (!mmc_host_is_spi(mmc))
+               cmd.cmdarg = mmc->rca << 16;
+       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)
@@ -70,26 +175,99 @@ struct mmc *find_mmc_device(int dev_num)
        return NULL;
 }
 
-static ulong
-mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
+static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
 {
        struct mmc_cmd cmd;
-       struct mmc_data data;
-       int err;
-       int stoperr = 0;
+       ulong end;
+       int err, start_cmd, end_cmd;
+
+       if (mmc->high_capacity)
+               end = start + blkcnt - 1;
+       else {
+               end = (start + blkcnt - 1) * mmc->write_bl_len;
+               start *= mmc->write_bl_len;
+       }
+
+       if (IS_SD(mmc)) {
+               start_cmd = SD_CMD_ERASE_WR_BLK_START;
+               end_cmd = SD_CMD_ERASE_WR_BLK_END;
+       } else {
+               start_cmd = MMC_CMD_ERASE_GROUP_START;
+               end_cmd = MMC_CMD_ERASE_GROUP_END;
+       }
+
+       cmd.cmdidx = start_cmd;
+       cmd.cmdarg = start;
+       cmd.resp_type = MMC_RSP_R1;
+       cmd.flags = 0;
+
+       err = mmc_send_cmd(mmc, &cmd, NULL);
+       if (err)
+               goto err_out;
+
+       cmd.cmdidx = end_cmd;
+       cmd.cmdarg = end;
+
+       err = mmc_send_cmd(mmc, &cmd, NULL);
+       if (err)
+               goto err_out;
+
+       cmd.cmdidx = MMC_CMD_ERASE;
+       cmd.cmdarg = SECURE_ERASE;
+       cmd.resp_type = MMC_RSP_R1b;
+
+       err = mmc_send_cmd(mmc, &cmd, NULL);
+       if (err)
+               goto err_out;
+
+       return 0;
+
+err_out:
+       puts("mmc erase failed\n");
+       return err;
+}
+
+static unsigned long
+mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
+{
+       int err = 0;
        struct mmc *mmc = find_mmc_device(dev_num);
-       int blklen;
+       lbaint_t blk = 0, blk_r = 0;
 
        if (!mmc)
                return -1;
 
-       blklen = mmc->write_bl_len;
+       if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
+               printf("\n\nCaution! Your devices Erase group is 0x%x\n"
+                       "The erase range would be change to 0x%lx~0x%lx\n\n",
+                      mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
+                      ((start + blkcnt + mmc->erase_grp_size)
+                      & ~(mmc->erase_grp_size - 1)) - 1);
+
+       while (blk < blkcnt) {
+               blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
+                       mmc->erase_grp_size : (blkcnt - blk);
+               err = mmc_erase_t(mmc, start + blk, blk_r);
+               if (err)
+                       break;
 
-       err = mmc_set_blocklen(mmc, mmc->write_bl_len);
+               blk += blk_r;
+       }
 
-       if (err) {
-               printf("set write bl len failed\n\r");
-               return err;
+       return blk;
+}
+
+static ulong
+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",
+                       start + blkcnt, mmc->block_dev.lba);
+               return 0;
        }
 
        if (blkcnt > 1)
@@ -100,133 +278,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)
-               return err;
-
-       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 (!mmc)
+       if (blkcnt == 0)
                return 0;
 
-       /* We always do full block reads from the card */
-       err = mmc_set_blocklen(mmc, mmc->read_bl_len);
+       struct mmc *mmc = find_mmc_device(dev_num);
+       if (!mmc)
+               return 0;
 
-       if (err) {
+       if ((start + blkcnt) > mmc->block_dev.lba) {
+               printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
+                       start + blkcnt, mmc->block_dev.lba);
                return 0;
        }
 
-       for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
-               err = mmc_read_block(mmc, dst, i);
+       if (mmc_set_blocklen(mmc, mmc->read_bl_len))
+               return 0;
 
-               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;
 }
@@ -273,7 +455,16 @@ sd_send_op_cond(struct mmc *mmc)
 
                cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
                cmd.resp_type = MMC_RSP_R3;
-               cmd.cmdarg = mmc->voltages;
+
+               /*
+                * Most cards do not answer if some reserved bits
+                * in the ocr are set. However, Some controller
+                * can set bit 7 (reserved for low voltages), but
+                * how to manage low voltages SD card is not yet
+                * specified.
+                */
+               cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
+                       (mmc->voltages & 0xff8000);
 
                if (mmc->version == SD_VERSION_2)
                        cmd.cmdarg |= OCR_HCS;
@@ -292,7 +483,19 @@ sd_send_op_cond(struct mmc *mmc)
        if (mmc->version != SD_VERSION_2)
                mmc->version = SD_VERSION_1_0;
 
-       mmc->ocr = ((uint *)(cmd.response))[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);
        mmc->rca = 0;
@@ -302,17 +505,37 @@ 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));
+
+               if (mmc->host_caps & MMC_MODE_HC)
+                       cmd.cmdarg |= OCR_HCS;
+
                cmd.flags = 0;
 
                err = mmc_send_cmd(mmc, &cmd, NULL);
@@ -326,8 +549,20 @@ 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 = ((uint *)(cmd.response))[0];
+       mmc->ocr = cmd.response[0];
 
        mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
        mmc->rca = 0;
@@ -362,25 +597,36 @@ 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)
 {
-       char ext_csd[512];
+       ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
        char cardtype;
        int err;
 
        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;
@@ -392,9 +638,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);
@@ -421,6 +664,18 @@ int mmc_change_freq(struct mmc *mmc)
        return 0;
 }
 
+int mmc_switch_part(int dev_num, unsigned int part_num)
+{
+       struct mmc *mmc = find_mmc_device(dev_num);
+
+       if (!mmc)
+               return -1;
+
+       return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
+                         (mmc->part_config & ~PART_ACCESS_MASK)
+                         | (part_num & PART_ACCESS_MASK));
+}
+
 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
 {
        struct mmc_cmd cmd;
@@ -447,13 +702,16 @@ int sd_change_freq(struct mmc *mmc)
 {
        int err;
        struct mmc_cmd cmd;
-       uint scr[2];
-       uint switch_status[16];
+       ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
+       ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
        struct mmc_data data;
        int timeout;
 
        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;
@@ -473,7 +731,7 @@ int sd_change_freq(struct mmc *mmc)
        timeout = 3;
 
 retry_scr:
-       data.dest = (char *)&scr;
+       data.dest = (char *)scr;
        data.blocksize = 8;
        data.blocks = 1;
        data.flags = MMC_DATA_READ;
@@ -487,8 +745,8 @@ retry_scr:
                return err;
        }
 
-       mmc->scr[0] = scr[0];
-       mmc->scr[1] = scr[1];
+       mmc->scr[0] = __be32_to_cpu(scr[0]);
+       mmc->scr[1] = __be32_to_cpu(scr[1]);
 
        switch ((mmc->scr[0] >> 24) & 0xf) {
                case 0:
@@ -505,6 +763,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;
@@ -512,29 +773,26 @@ retry_scr:
        timeout = 4;
        while (timeout--) {
                err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
-                               (u8 *)&switch_status);
+                               (u8 *)switch_status);
 
                if (err)
                        return err;
 
                /* The high-speed function is busy.  Try again */
-               if (!switch_status[7] & SD_HIGHSPEED_BUSY)
+               if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
                        break;
        }
 
-       if (mmc->scr[0] & SD_DATA_4BIT)
-               mmc->card_caps |= MMC_MODE_4BIT;
-
        /* If high-speed isn't supported, we return */
-       if (!(switch_status[3] & SD_HIGHSPEED_SUPPORTED))
+       if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
                return 0;
 
-       err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
+       err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
 
        if (err)
                return err;
 
-       if ((switch_status[4] & 0x0f000000) == 0x01000000)
+       if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
                mmc->card_caps |= MMC_MODE_HS;
 
        return 0;
@@ -542,7 +800,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,
@@ -552,7 +810,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,
@@ -600,11 +858,27 @@ int mmc_startup(struct mmc *mmc)
 {
        int err;
        uint mult, freq;
-       u64 cmult, csize;
+       u64 cmult, csize, capacity;
        struct mmc_cmd cmd;
+       ALLOC_CACHE_ALIGN_BUFFER(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;
@@ -621,18 +895,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 = (((uint *)(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;
@@ -642,16 +918,19 @@ 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;
 
-       mmc->csd[0] = ((uint *)(cmd.response))[0];
-       mmc->csd[1] = ((uint *)(cmd.response))[1];
-       mmc->csd[2] = ((uint *)(cmd.response))[2];
-       mmc->csd[3] = ((uint *)(cmd.response))[3];
+       mmc->csd[0] = cmd.response[0];
+       mmc->csd[1] = cmd.response[1];
+       mmc->csd[2] = cmd.response[2];
+       mmc->csd[3] = cmd.response[3];
 
        if (mmc->version == MMC_VERSION_UNKNOWN) {
-               int version = (cmd.response[0] >> 2) & 0xf;
+               int version = (cmd.response[0] >> 26) & 0xf;
 
                switch (version) {
                        case 0:
@@ -676,17 +955,17 @@ int mmc_startup(struct mmc *mmc)
        }
 
        /* divide frequency by 10, since the mults are 10x bigger */
-       freq = fbase[(cmd.response[3] & 0x7)];
-       mult = multipliers[((cmd.response[3] >> 3) & 0xf)];
+       freq = fbase[(cmd.response[0] & 0x7)];
+       mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
 
        mmc->tran_speed = freq * mult;
 
-       mmc->read_bl_len = 1 << ((((uint *)(cmd.response))[1] >> 16) & 0xf);
+       mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
 
        if (IS_SD(mmc))
                mmc->write_bl_len = mmc->read_bl_len;
        else
-               mmc->write_bl_len = 1 << ((((uint *)(cmd.response))[3] >> 22) & 0xf);
+               mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
 
        if (mmc->high_capacity) {
                csize = (mmc->csd[1] & 0x3f) << 16
@@ -708,14 +987,57 @@ 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;
+       }
+
+       /*
+        * For SD, its erase group is always one sector
+        */
+       mmc->erase_grp_size = 1;
+       mmc->part_config = MMCPART_NOAVAILABLE;
+       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)) {
+                       /*
+                        * According to the JEDEC Standard, the value of
+                        * ext_csd's capacity is valid if the value is more
+                        * than 2GB
+                        */
+                       capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
+                                  ext_csd[214] << 16 | ext_csd[215] << 24;
+                       capacity *= 512;
+                       if ((capacity >> 20) > 2 * 1024)
+                               mmc->capacity = capacity;
+               }
+
+               /*
+                * Check whether GROUP_DEF is set, if yes, read out
+                * group size from ext_csd directly, or calculate
+                * the group size from the csd value.
+                */
+               if (ext_csd[175])
+                       mmc->erase_grp_size = ext_csd[224] * 512 * 1024;
+               else {
+                       int erase_gsz, erase_gmul;
+                       erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
+                       erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
+                       mmc->erase_grp_size = (erase_gsz + 1)
+                               * (erase_gmul + 1);
+               }
+
+               /* store the partition info of emmc */
+               if (ext_csd[160] & PART_SUPPORT)
+                       mmc->part_config = ext_csd[179];
+       }
 
        if (IS_SD(mmc))
                err = sd_change_freq(mmc);
@@ -791,13 +1113,13 @@ int mmc_startup(struct mmc *mmc)
        mmc->block_dev.type = 0;
        mmc->block_dev.blksz = mmc->read_bl_len;
        mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
-       sprintf(mmc->block_dev.vendor,"Man %02x%02x%02x Snr %02x%02x%02x%02x",
-                       mmc->cid[0], mmc->cid[1], mmc->cid[2],
-                       mmc->cid[9], mmc->cid[10], mmc->cid[11], mmc->cid[12]);
-       sprintf(mmc->block_dev.product,"%c%c%c%c%c", mmc->cid[3],
-                       mmc->cid[4], mmc->cid[5], mmc->cid[6], mmc->cid[7]);
-       sprintf(mmc->block_dev.revision,"%d.%d", mmc->cid[8] >> 4,
-                       mmc->cid[8] & 0xf);
+       sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
+                       (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
+       sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
+                       (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
+                       (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
+       sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
+                       (mmc->cid[2] >> 24) & 0xf);
        init_part(&mmc->block_dev);
 
        return 0;
@@ -819,7 +1141,7 @@ int mmc_send_if_cond(struct mmc *mmc)
        if (err)
                return err;
 
-       if ((((uint *)(cmd.response))[0] & 0xff) != 0xaa)
+       if ((cmd.response[0] & 0xff) != 0xaa)
                return UNUSABLE_ERR;
        else
                mmc->version = SD_VERSION_2;
@@ -835,6 +1157,9 @@ 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;
+       mmc->block_dev.block_erase = mmc_berase;
+       if (!mmc->b_max)
+               mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
 
        INIT_LIST_HEAD (&mmc->link);
 
@@ -843,35 +1168,42 @@ int mmc_register(struct mmc *mmc)
        return 0;
 }
 
+#ifdef CONFIG_PARTITIONS
 block_dev_desc_t *mmc_get_dev(int dev)
 {
        struct mmc *mmc = find_mmc_device(dev);
 
        return mmc ? &mmc->block_dev : NULL;
 }
+#endif
 
 int mmc_init(struct mmc *mmc)
 {
        int err;
 
+       if (mmc->has_init)
+               return 0;
+
        err = mmc->init(mmc);
 
        if (err)
                return err;
 
+       mmc_set_bus_width(mmc, 1);
+       mmc_set_clock(mmc, 1);
+
        /* Reset the Card */
        err = mmc_go_idle(mmc);
 
        if (err)
                return err;
 
+       /* The internal partition reset to user partition(0) at every CMD0*/
+       mmc->part_num = 0;
+
        /* Test for SD version 2 */
        err = mmc_send_if_cond(mmc);
 
-       /* If we got an error other than timeout, we bail */
-       if (err && err != TIMEOUT)
-               return err;
-
        /* Now try to get the SD card's operating condition */
        err = sd_send_op_cond(mmc);
 
@@ -885,7 +1217,12 @@ int mmc_init(struct mmc *mmc)
                }
        }
 
-       return mmc_startup(mmc);
+       err = mmc_startup(mmc);
+       if (err)
+               mmc->has_init = 0;
+       else
+               mmc->has_init = 1;
+       return err;
 }
 
 /*
@@ -917,6 +1254,11 @@ void print_mmc_devices(char separator)
        printf("\n");
 }
 
+int get_mmc_num(void)
+{
+       return cur_dev_num;
+}
+
 int mmc_initialize(bd_t *bis)
 {
        INIT_LIST_HEAD (&mmc_devices);