2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/list.h>
19 /* Set block count limit because of 16 bit register limit on some hardware*/
20 #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
21 #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
24 static struct list_head mmc_devices;
25 static int cur_dev_num = -1;
27 int __weak board_mmc_getwp(struct mmc *mmc)
32 int mmc_getwp(struct mmc *mmc)
36 wp = board_mmc_getwp(mmc);
48 int __board_mmc_getcd(struct mmc *mmc) {
52 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
53 alias("__board_mmc_getcd")));
55 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
56 struct mmc_data *data)
58 struct mmc_data backup;
61 memset(&backup, 0, sizeof(backup));
63 #ifdef CONFIG_MMC_TRACE
67 printf("CMD_SEND:%d\n", cmd->cmdidx);
68 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
69 ret = mmc->send_cmd(mmc, cmd, data);
70 switch (cmd->resp_type) {
72 printf("\t\tMMC_RSP_NONE\n");
75 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
79 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
83 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
85 printf("\t\t \t\t 0x%08X \n",
87 printf("\t\t \t\t 0x%08X \n",
89 printf("\t\t \t\t 0x%08X \n",
92 printf("\t\t\t\t\tDUMPING DATA\n");
93 for (i = 0; i < 4; i++) {
95 printf("\t\t\t\t\t%03d - ", i*4);
96 ptr = (u8 *)&cmd->response[i];
98 for (j = 0; j < 4; j++)
99 printf("%02X ", *ptr--);
104 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
108 printf("\t\tERROR MMC rsp not supported\n");
112 ret = mmc->send_cmd(mmc, cmd, data);
117 static int mmc_send_status(struct mmc *mmc, int timeout)
120 int err, retries = 5;
121 #ifdef CONFIG_MMC_TRACE
125 cmd.cmdidx = MMC_CMD_SEND_STATUS;
126 cmd.resp_type = MMC_RSP_R1;
127 if (!mmc_host_is_spi(mmc))
128 cmd.cmdarg = mmc->rca << 16;
131 err = mmc_send_cmd(mmc, &cmd, NULL);
133 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
134 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
137 else if (cmd.response[0] & MMC_STATUS_MASK) {
138 printf("Status Error: 0x%08X\n",
142 } else if (--retries < 0)
149 #ifdef CONFIG_MMC_TRACE
150 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
151 printf("CURR STATE:%d\n", status);
154 printf("Timeout waiting card ready\n");
161 static int mmc_set_blocklen(struct mmc *mmc, int len)
165 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
166 cmd.resp_type = MMC_RSP_R1;
169 return mmc_send_cmd(mmc, &cmd, NULL);
172 struct mmc *find_mmc_device(int dev_num)
175 struct list_head *entry;
177 list_for_each(entry, &mmc_devices) {
178 m = list_entry(entry, struct mmc, link);
180 if (m->block_dev.dev == dev_num)
184 printf("MMC Device %d not found\n", dev_num);
189 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
193 int err, start_cmd, end_cmd;
195 if (mmc->high_capacity)
196 end = start + blkcnt - 1;
198 end = (start + blkcnt - 1) * mmc->write_bl_len;
199 start *= mmc->write_bl_len;
203 start_cmd = SD_CMD_ERASE_WR_BLK_START;
204 end_cmd = SD_CMD_ERASE_WR_BLK_END;
206 start_cmd = MMC_CMD_ERASE_GROUP_START;
207 end_cmd = MMC_CMD_ERASE_GROUP_END;
210 cmd.cmdidx = start_cmd;
212 cmd.resp_type = MMC_RSP_R1;
214 err = mmc_send_cmd(mmc, &cmd, NULL);
218 cmd.cmdidx = end_cmd;
221 err = mmc_send_cmd(mmc, &cmd, NULL);
225 cmd.cmdidx = MMC_CMD_ERASE;
226 cmd.cmdarg = SECURE_ERASE;
227 cmd.resp_type = MMC_RSP_R1b;
229 err = mmc_send_cmd(mmc, &cmd, NULL);
236 puts("mmc erase failed\n");
241 mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
244 struct mmc *mmc = find_mmc_device(dev_num);
245 lbaint_t blk = 0, blk_r = 0;
251 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
252 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
253 "The erase range would be change to "
254 "0x" LBAF "~0x" LBAF "\n\n",
255 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
256 ((start + blkcnt + mmc->erase_grp_size)
257 & ~(mmc->erase_grp_size - 1)) - 1);
259 while (blk < blkcnt) {
260 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
261 mmc->erase_grp_size : (blkcnt - blk);
262 err = mmc_erase_t(mmc, start + blk, blk_r);
268 /* Waiting for the ready status */
269 if (mmc_send_status(mmc, timeout))
277 mmc_write_blocks(struct mmc *mmc, lbaint_t start, lbaint_t blkcnt, const void*src)
280 struct mmc_data data;
283 if ((start + blkcnt) > mmc->block_dev.lba) {
284 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
285 start + blkcnt, mmc->block_dev.lba);
291 else if (blkcnt == 1)
292 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
294 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
296 if (mmc->high_capacity)
299 cmd.cmdarg = start * mmc->write_bl_len;
301 cmd.resp_type = MMC_RSP_R1;
304 data.blocks = blkcnt;
305 data.blocksize = mmc->write_bl_len;
306 data.flags = MMC_DATA_WRITE;
308 if (mmc_send_cmd(mmc, &cmd, &data)) {
309 printf("mmc write failed\n");
313 /* SPI multiblock writes terminate using a special
314 * token, not a STOP_TRANSMISSION request.
316 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
317 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
319 cmd.resp_type = MMC_RSP_R1b;
320 if (mmc_send_cmd(mmc, &cmd, NULL)) {
321 printf("mmc fail to send stop cmd\n");
326 /* Waiting for the ready status */
327 if (mmc_send_status(mmc, timeout))
334 mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void*src)
336 lbaint_t cur, blocks_todo = blkcnt;
338 struct mmc *mmc = find_mmc_device(dev_num);
342 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
346 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
347 if(mmc_write_blocks(mmc, start, cur, src) != cur)
351 src += cur * mmc->write_bl_len;
352 } while (blocks_todo > 0);
357 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
361 struct mmc_data data;
364 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
366 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
368 if (mmc->high_capacity)
371 cmd.cmdarg = start * mmc->read_bl_len;
373 cmd.resp_type = MMC_RSP_R1;
376 data.blocks = blkcnt;
377 data.blocksize = mmc->read_bl_len;
378 data.flags = MMC_DATA_READ;
380 if (mmc_send_cmd(mmc, &cmd, &data))
384 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
386 cmd.resp_type = MMC_RSP_R1b;
387 if (mmc_send_cmd(mmc, &cmd, NULL)) {
388 printf("mmc fail to send stop cmd\n");
396 static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
398 lbaint_t cur, blocks_todo = blkcnt;
403 struct mmc *mmc = find_mmc_device(dev_num);
407 if ((start + blkcnt) > mmc->block_dev.lba) {
408 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
409 start + blkcnt, mmc->block_dev.lba);
413 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
417 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
418 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
422 dst += cur * mmc->read_bl_len;
423 } while (blocks_todo > 0);
428 static int mmc_go_idle(struct mmc *mmc)
435 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
437 cmd.resp_type = MMC_RSP_NONE;
439 err = mmc_send_cmd(mmc, &cmd, NULL);
449 static int sd_send_op_cond(struct mmc *mmc)
456 cmd.cmdidx = MMC_CMD_APP_CMD;
457 cmd.resp_type = MMC_RSP_R1;
460 err = mmc_send_cmd(mmc, &cmd, NULL);
465 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
466 cmd.resp_type = MMC_RSP_R3;
469 * Most cards do not answer if some reserved bits
470 * in the ocr are set. However, Some controller
471 * can set bit 7 (reserved for low voltages), but
472 * how to manage low voltages SD card is not yet
475 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
476 (mmc->voltages & 0xff8000);
478 if (mmc->version == SD_VERSION_2)
479 cmd.cmdarg |= OCR_HCS;
481 err = mmc_send_cmd(mmc, &cmd, NULL);
487 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
492 if (mmc->version != SD_VERSION_2)
493 mmc->version = SD_VERSION_1_0;
495 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
496 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
497 cmd.resp_type = MMC_RSP_R3;
500 err = mmc_send_cmd(mmc, &cmd, NULL);
506 mmc->ocr = cmd.response[0];
508 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
514 /* We pass in the cmd since otherwise the init seems to fail */
515 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
520 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
521 cmd->resp_type = MMC_RSP_R3;
523 if (use_arg && !mmc_host_is_spi(mmc)) {
526 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
527 (mmc->op_cond_response & OCR_ACCESS_MODE);
529 if (mmc->host_caps & MMC_MODE_HC)
530 cmd->cmdarg |= OCR_HCS;
532 err = mmc_send_cmd(mmc, cmd, NULL);
535 mmc->op_cond_response = cmd->response[0];
539 int mmc_send_op_cond(struct mmc *mmc)
544 /* Some cards seem to need this */
547 /* Asking to the card its capabilities */
548 mmc->op_cond_pending = 1;
549 for (i = 0; i < 2; i++) {
550 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
554 /* exit if not busy (flag seems to be inverted) */
555 if (mmc->op_cond_response & OCR_BUSY)
561 int mmc_complete_op_cond(struct mmc *mmc)
568 mmc->op_cond_pending = 0;
569 start = get_timer(0);
571 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
574 if (get_timer(start) > timeout)
577 } while (!(mmc->op_cond_response & OCR_BUSY));
579 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
580 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
581 cmd.resp_type = MMC_RSP_R3;
584 err = mmc_send_cmd(mmc, &cmd, NULL);
590 mmc->version = MMC_VERSION_UNKNOWN;
591 mmc->ocr = cmd.response[0];
593 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
600 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
603 struct mmc_data data;
606 /* Get the Card Status Register */
607 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
608 cmd.resp_type = MMC_RSP_R1;
611 data.dest = (char *)ext_csd;
613 data.blocksize = MMC_MAX_BLOCK_LEN;
614 data.flags = MMC_DATA_READ;
616 err = mmc_send_cmd(mmc, &cmd, &data);
622 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
628 cmd.cmdidx = MMC_CMD_SWITCH;
629 cmd.resp_type = MMC_RSP_R1b;
630 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
634 ret = mmc_send_cmd(mmc, &cmd, NULL);
636 /* Waiting for the ready status */
638 ret = mmc_send_status(mmc, timeout);
644 static int mmc_change_freq(struct mmc *mmc)
646 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
652 if (mmc_host_is_spi(mmc))
655 /* Only version 4 supports high-speed */
656 if (mmc->version < MMC_VERSION_4)
659 err = mmc_send_ext_csd(mmc, ext_csd);
664 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
666 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
671 /* Now check to see that it worked */
672 err = mmc_send_ext_csd(mmc, ext_csd);
677 /* No high-speed support */
678 if (!ext_csd[EXT_CSD_HS_TIMING])
681 /* High Speed is set, there are two types: 52MHz and 26MHz */
682 if (cardtype & MMC_HS_52MHZ)
683 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
685 mmc->card_caps |= MMC_MODE_HS;
690 static int mmc_set_capacity(struct mmc *mmc, int part_num)
694 mmc->capacity = mmc->capacity_user;
698 mmc->capacity = mmc->capacity_boot;
701 mmc->capacity = mmc->capacity_rpmb;
707 mmc->capacity = mmc->capacity_gp[part_num - 4];
713 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
718 int mmc_switch_part(int dev_num, unsigned int part_num)
720 struct mmc *mmc = find_mmc_device(dev_num);
726 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
727 (mmc->part_config & ~PART_ACCESS_MASK)
728 | (part_num & PART_ACCESS_MASK));
732 return mmc_set_capacity(mmc, part_num);
735 int mmc_getcd(struct mmc *mmc)
739 cd = board_mmc_getcd(mmc);
743 cd = mmc->getcd(mmc);
751 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
754 struct mmc_data data;
756 /* Switch the frequency */
757 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
758 cmd.resp_type = MMC_RSP_R1;
759 cmd.cmdarg = (mode << 31) | 0xffffff;
760 cmd.cmdarg &= ~(0xf << (group * 4));
761 cmd.cmdarg |= value << (group * 4);
763 data.dest = (char *)resp;
766 data.flags = MMC_DATA_READ;
768 return mmc_send_cmd(mmc, &cmd, &data);
772 static int sd_change_freq(struct mmc *mmc)
776 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
777 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
778 struct mmc_data data;
783 if (mmc_host_is_spi(mmc))
786 /* Read the SCR to find out if this card supports higher speeds */
787 cmd.cmdidx = MMC_CMD_APP_CMD;
788 cmd.resp_type = MMC_RSP_R1;
789 cmd.cmdarg = mmc->rca << 16;
791 err = mmc_send_cmd(mmc, &cmd, NULL);
796 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
797 cmd.resp_type = MMC_RSP_R1;
803 data.dest = (char *)scr;
806 data.flags = MMC_DATA_READ;
808 err = mmc_send_cmd(mmc, &cmd, &data);
817 mmc->scr[0] = __be32_to_cpu(scr[0]);
818 mmc->scr[1] = __be32_to_cpu(scr[1]);
820 switch ((mmc->scr[0] >> 24) & 0xf) {
822 mmc->version = SD_VERSION_1_0;
825 mmc->version = SD_VERSION_1_10;
828 mmc->version = SD_VERSION_2;
829 if ((mmc->scr[0] >> 15) & 0x1)
830 mmc->version = SD_VERSION_3;
833 mmc->version = SD_VERSION_1_0;
837 if (mmc->scr[0] & SD_DATA_4BIT)
838 mmc->card_caps |= MMC_MODE_4BIT;
840 /* Version 1.0 doesn't support switching */
841 if (mmc->version == SD_VERSION_1_0)
846 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
847 (u8 *)switch_status);
852 /* The high-speed function is busy. Try again */
853 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
857 /* If high-speed isn't supported, we return */
858 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
862 * If the host doesn't support SD_HIGHSPEED, do not switch card to
863 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
864 * This can avoid furthur problem when the card runs in different
865 * mode between the host.
867 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
868 (mmc->host_caps & MMC_MODE_HS)))
871 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
876 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
877 mmc->card_caps |= MMC_MODE_HS;
882 /* frequency bases */
883 /* divided by 10 to be nice to platforms without floating point */
884 static const int fbase[] = {
891 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
892 * to platforms without floating point.
894 static const int multipliers[] = {
913 static void mmc_set_ios(struct mmc *mmc)
918 void mmc_set_clock(struct mmc *mmc, uint clock)
920 if (clock > mmc->f_max)
923 if (clock < mmc->f_min)
931 static void mmc_set_bus_width(struct mmc *mmc, uint width)
933 mmc->bus_width = width;
938 static int mmc_startup(struct mmc *mmc)
942 u64 cmult, csize, capacity;
944 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
945 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
948 #ifdef CONFIG_MMC_SPI_CRC_ON
949 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
950 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
951 cmd.resp_type = MMC_RSP_R1;
953 err = mmc_send_cmd(mmc, &cmd, NULL);
960 /* Put the Card in Identify Mode */
961 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
962 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
963 cmd.resp_type = MMC_RSP_R2;
966 err = mmc_send_cmd(mmc, &cmd, NULL);
971 memcpy(mmc->cid, cmd.response, 16);
974 * For MMC cards, set the Relative Address.
975 * For SD cards, get the Relatvie Address.
976 * This also puts the cards into Standby State
978 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
979 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
980 cmd.cmdarg = mmc->rca << 16;
981 cmd.resp_type = MMC_RSP_R6;
983 err = mmc_send_cmd(mmc, &cmd, NULL);
989 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
992 /* Get the Card-Specific Data */
993 cmd.cmdidx = MMC_CMD_SEND_CSD;
994 cmd.resp_type = MMC_RSP_R2;
995 cmd.cmdarg = mmc->rca << 16;
997 err = mmc_send_cmd(mmc, &cmd, NULL);
999 /* Waiting for the ready status */
1000 mmc_send_status(mmc, timeout);
1005 mmc->csd[0] = cmd.response[0];
1006 mmc->csd[1] = cmd.response[1];
1007 mmc->csd[2] = cmd.response[2];
1008 mmc->csd[3] = cmd.response[3];
1010 if (mmc->version == MMC_VERSION_UNKNOWN) {
1011 int version = (cmd.response[0] >> 26) & 0xf;
1015 mmc->version = MMC_VERSION_1_2;
1018 mmc->version = MMC_VERSION_1_4;
1021 mmc->version = MMC_VERSION_2_2;
1024 mmc->version = MMC_VERSION_3;
1027 mmc->version = MMC_VERSION_4;
1030 mmc->version = MMC_VERSION_1_2;
1035 /* divide frequency by 10, since the mults are 10x bigger */
1036 freq = fbase[(cmd.response[0] & 0x7)];
1037 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1039 mmc->tran_speed = freq * mult;
1041 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1044 mmc->write_bl_len = mmc->read_bl_len;
1046 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1048 if (mmc->high_capacity) {
1049 csize = (mmc->csd[1] & 0x3f) << 16
1050 | (mmc->csd[2] & 0xffff0000) >> 16;
1053 csize = (mmc->csd[1] & 0x3ff) << 2
1054 | (mmc->csd[2] & 0xc0000000) >> 30;
1055 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1058 mmc->capacity_user = (csize + 1) << (cmult + 2);
1059 mmc->capacity_user *= mmc->read_bl_len;
1060 mmc->capacity_boot = 0;
1061 mmc->capacity_rpmb = 0;
1062 for (i = 0; i < 4; i++)
1063 mmc->capacity_gp[i] = 0;
1065 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1066 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1068 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1069 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1071 /* Select the card, and put it into Transfer Mode */
1072 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1073 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1074 cmd.resp_type = MMC_RSP_R1;
1075 cmd.cmdarg = mmc->rca << 16;
1076 err = mmc_send_cmd(mmc, &cmd, NULL);
1083 * For SD, its erase group is always one sector
1085 mmc->erase_grp_size = 1;
1086 mmc->part_config = MMCPART_NOAVAILABLE;
1087 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1088 /* check ext_csd version and capacity */
1089 err = mmc_send_ext_csd(mmc, ext_csd);
1090 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1092 * According to the JEDEC Standard, the value of
1093 * ext_csd's capacity is valid if the value is more
1096 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1097 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1098 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1099 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1100 capacity *= MMC_MAX_BLOCK_LEN;
1101 if ((capacity >> 20) > 2 * 1024)
1102 mmc->capacity_user = capacity;
1105 switch (ext_csd[EXT_CSD_REV]) {
1107 mmc->version = MMC_VERSION_4_1;
1110 mmc->version = MMC_VERSION_4_2;
1113 mmc->version = MMC_VERSION_4_3;
1116 mmc->version = MMC_VERSION_4_41;
1119 mmc->version = MMC_VERSION_4_5;
1124 * Check whether GROUP_DEF is set, if yes, read out
1125 * group size from ext_csd directly, or calculate
1126 * the group size from the csd value.
1128 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
1129 mmc->erase_grp_size =
1130 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1131 MMC_MAX_BLOCK_LEN * 1024;
1133 int erase_gsz, erase_gmul;
1134 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1135 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1136 mmc->erase_grp_size = (erase_gsz + 1)
1140 /* store the partition info of emmc */
1141 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1142 ext_csd[EXT_CSD_BOOT_MULT])
1143 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1145 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1147 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1149 for (i = 0; i < 4; i++) {
1150 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1151 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1152 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1153 mmc->capacity_gp[i] *=
1154 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1155 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1159 err = mmc_set_capacity(mmc, mmc->part_num);
1164 err = sd_change_freq(mmc);
1166 err = mmc_change_freq(mmc);
1171 /* Restrict card's capabilities by what the host can do */
1172 mmc->card_caps &= mmc->host_caps;
1175 if (mmc->card_caps & MMC_MODE_4BIT) {
1176 cmd.cmdidx = MMC_CMD_APP_CMD;
1177 cmd.resp_type = MMC_RSP_R1;
1178 cmd.cmdarg = mmc->rca << 16;
1180 err = mmc_send_cmd(mmc, &cmd, NULL);
1184 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1185 cmd.resp_type = MMC_RSP_R1;
1187 err = mmc_send_cmd(mmc, &cmd, NULL);
1191 mmc_set_bus_width(mmc, 4);
1194 if (mmc->card_caps & MMC_MODE_HS)
1195 mmc->tran_speed = 50000000;
1197 mmc->tran_speed = 25000000;
1201 /* An array of possible bus widths in order of preference */
1202 static unsigned ext_csd_bits[] = {
1203 EXT_CSD_BUS_WIDTH_8,
1204 EXT_CSD_BUS_WIDTH_4,
1205 EXT_CSD_BUS_WIDTH_1,
1208 /* An array to map CSD bus widths to host cap bits */
1209 static unsigned ext_to_hostcaps[] = {
1210 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1211 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1214 /* An array to map chosen bus width to an integer */
1215 static unsigned widths[] = {
1219 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1220 unsigned int extw = ext_csd_bits[idx];
1223 * Check to make sure the controller supports
1224 * this bus width, if it's more than 1
1226 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1227 !(mmc->host_caps & ext_to_hostcaps[extw]))
1230 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1231 EXT_CSD_BUS_WIDTH, extw);
1236 mmc_set_bus_width(mmc, widths[idx]);
1238 err = mmc_send_ext_csd(mmc, test_csd);
1239 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1240 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1241 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1242 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1243 && ext_csd[EXT_CSD_REV] \
1244 == test_csd[EXT_CSD_REV]
1245 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1246 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1247 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1248 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1250 mmc->card_caps |= ext_to_hostcaps[extw];
1255 if (mmc->card_caps & MMC_MODE_HS) {
1256 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1257 mmc->tran_speed = 52000000;
1259 mmc->tran_speed = 26000000;
1263 mmc_set_clock(mmc, mmc->tran_speed);
1265 /* fill in device description */
1266 mmc->block_dev.lun = 0;
1267 mmc->block_dev.type = 0;
1268 mmc->block_dev.blksz = mmc->read_bl_len;
1269 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1270 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1271 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1272 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1273 (mmc->cid[3] >> 16) & 0xffff);
1274 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1275 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1276 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1277 (mmc->cid[2] >> 24) & 0xff);
1278 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1279 (mmc->cid[2] >> 16) & 0xf);
1280 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1281 init_part(&mmc->block_dev);
1287 static int mmc_send_if_cond(struct mmc *mmc)
1292 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1293 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1294 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1295 cmd.resp_type = MMC_RSP_R7;
1297 err = mmc_send_cmd(mmc, &cmd, NULL);
1302 if ((cmd.response[0] & 0xff) != 0xaa)
1303 return UNUSABLE_ERR;
1305 mmc->version = SD_VERSION_2;
1310 int mmc_register(struct mmc *mmc)
1312 /* Setup the universal parts of the block interface just once */
1313 mmc->block_dev.if_type = IF_TYPE_MMC;
1314 mmc->block_dev.dev = cur_dev_num++;
1315 mmc->block_dev.removable = 1;
1316 mmc->block_dev.block_read = mmc_bread;
1317 mmc->block_dev.block_write = mmc_bwrite;
1318 mmc->block_dev.block_erase = mmc_berase;
1320 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1322 INIT_LIST_HEAD (&mmc->link);
1324 list_add_tail (&mmc->link, &mmc_devices);
1329 #ifdef CONFIG_PARTITIONS
1330 block_dev_desc_t *mmc_get_dev(int dev)
1332 struct mmc *mmc = find_mmc_device(dev);
1333 if (!mmc || mmc_init(mmc))
1336 return &mmc->block_dev;
1340 int mmc_start_init(struct mmc *mmc)
1344 if (mmc_getcd(mmc) == 0) {
1346 printf("MMC: no card present\n");
1353 err = mmc->init(mmc);
1358 mmc_set_bus_width(mmc, 1);
1359 mmc_set_clock(mmc, 1);
1361 /* Reset the Card */
1362 err = mmc_go_idle(mmc);
1367 /* The internal partition reset to user partition(0) at every CMD0*/
1370 /* Test for SD version 2 */
1371 err = mmc_send_if_cond(mmc);
1373 /* Now try to get the SD card's operating condition */
1374 err = sd_send_op_cond(mmc);
1376 /* If the command timed out, we check for an MMC card */
1377 if (err == TIMEOUT) {
1378 err = mmc_send_op_cond(mmc);
1380 if (err && err != IN_PROGRESS) {
1381 printf("Card did not respond to voltage select!\n");
1382 return UNUSABLE_ERR;
1386 if (err == IN_PROGRESS)
1387 mmc->init_in_progress = 1;
1392 static int mmc_complete_init(struct mmc *mmc)
1396 if (mmc->op_cond_pending)
1397 err = mmc_complete_op_cond(mmc);
1400 err = mmc_startup(mmc);
1405 mmc->init_in_progress = 0;
1409 int mmc_init(struct mmc *mmc)
1411 int err = IN_PROGRESS;
1412 unsigned start = get_timer(0);
1416 if (!mmc->init_in_progress)
1417 err = mmc_start_init(mmc);
1419 if (!err || err == IN_PROGRESS)
1420 err = mmc_complete_init(mmc);
1421 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1426 * CPU and board-specific MMC initializations. Aliased function
1427 * signals caller to move on
1429 static int __def_mmc_init(bd_t *bis)
1434 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1435 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1437 void print_mmc_devices(char separator)
1440 struct list_head *entry;
1442 list_for_each(entry, &mmc_devices) {
1443 m = list_entry(entry, struct mmc, link);
1445 printf("%s: %d", m->name, m->block_dev.dev);
1447 if (entry->next != &mmc_devices)
1448 printf("%c ", separator);
1454 int get_mmc_num(void)
1459 void mmc_set_preinit(struct mmc *mmc, int preinit)
1461 mmc->preinit = preinit;
1464 static void do_preinit(void)
1467 struct list_head *entry;
1469 list_for_each(entry, &mmc_devices) {
1470 m = list_entry(entry, struct mmc, link);
1478 int mmc_initialize(bd_t *bis)
1480 INIT_LIST_HEAD (&mmc_devices);
1483 if (board_mmc_init(bis) < 0)
1486 #ifndef CONFIG_SPL_BUILD
1487 print_mmc_devices(',');
1494 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1496 * This function changes the size of boot partition and the size of rpmb
1497 * partition present on EMMC devices.
1500 * struct *mmc: pointer for the mmc device strcuture
1501 * bootsize: size of boot partition
1502 * rpmbsize: size of rpmb partition
1504 * Returns 0 on success.
1507 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1508 unsigned long rpmbsize)
1513 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1514 cmd.cmdidx = MMC_CMD_RES_MAN;
1515 cmd.resp_type = MMC_RSP_R1b;
1516 cmd.cmdarg = MMC_CMD62_ARG1;
1518 err = mmc_send_cmd(mmc, &cmd, NULL);
1520 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1524 /* Boot partition changing mode */
1525 cmd.cmdidx = MMC_CMD_RES_MAN;
1526 cmd.resp_type = MMC_RSP_R1b;
1527 cmd.cmdarg = MMC_CMD62_ARG2;
1529 err = mmc_send_cmd(mmc, &cmd, NULL);
1531 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1534 /* boot partition size is multiple of 128KB */
1535 bootsize = (bootsize * 1024) / 128;
1537 /* Arg: boot partition size */
1538 cmd.cmdidx = MMC_CMD_RES_MAN;
1539 cmd.resp_type = MMC_RSP_R1b;
1540 cmd.cmdarg = bootsize;
1542 err = mmc_send_cmd(mmc, &cmd, NULL);
1544 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1547 /* RPMB partition size is multiple of 128KB */
1548 rpmbsize = (rpmbsize * 1024) / 128;
1549 /* Arg: RPMB partition size */
1550 cmd.cmdidx = MMC_CMD_RES_MAN;
1551 cmd.resp_type = MMC_RSP_R1b;
1552 cmd.cmdarg = rpmbsize;
1554 err = mmc_send_cmd(mmc, &cmd, NULL);
1556 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1563 * This function shall form and send the commands to open / close the
1564 * boot partition specified by user.
1567 * ack: 0x0 - No boot acknowledge sent (default)
1568 * 0x1 - Boot acknowledge sent during boot operation
1569 * part_num: User selects boot data that will be sent to master
1570 * 0x0 - Device not boot enabled (default)
1571 * 0x1 - Boot partition 1 enabled for boot
1572 * 0x2 - Boot partition 2 enabled for boot
1573 * access: User selects partitions to access
1574 * 0x0 : No access to boot partition (default)
1575 * 0x1 : R/W boot partition 1
1576 * 0x2 : R/W boot partition 2
1577 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1579 * Returns 0 on success.
1581 int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1586 /* Boot ack enable, boot partition enable , boot partition access */
1587 cmd.cmdidx = MMC_CMD_SWITCH;
1588 cmd.resp_type = MMC_RSP_R1b;
1590 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1591 (EXT_CSD_PART_CONF << 16) |
1592 ((EXT_CSD_BOOT_ACK(ack) |
1593 EXT_CSD_BOOT_PART_NUM(part_num) |
1594 EXT_CSD_PARTITION_ACCESS(access)) << 8);
1596 err = mmc_send_cmd(mmc, &cmd, NULL);
1599 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1602 debug("mmc boot partition#%d close fail:Error = %d\n",
1609 /* 4bit transfer mode at booting time. */
1610 cmd.cmdidx = MMC_CMD_SWITCH;
1611 cmd.resp_type = MMC_RSP_R1b;
1613 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1614 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1617 err = mmc_send_cmd(mmc, &cmd, NULL);
1619 debug("mmc boot partition#%d open fail:Error2 = %d\n",