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--);
488 if (!(cmd.response[0] & OCR_BUSY))
491 if (mmc->version != SD_VERSION_2)
492 mmc->version = SD_VERSION_1_0;
494 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
495 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
496 cmd.resp_type = MMC_RSP_R3;
499 err = mmc_send_cmd(mmc, &cmd, NULL);
505 mmc->ocr = cmd.response[0];
507 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
513 /* We pass in the cmd since otherwise the init seems to fail */
514 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
519 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
520 cmd->resp_type = MMC_RSP_R3;
522 if (use_arg && !mmc_host_is_spi(mmc)) {
525 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
526 (mmc->op_cond_response & OCR_ACCESS_MODE);
528 if (mmc->host_caps & MMC_MODE_HC)
529 cmd->cmdarg |= OCR_HCS;
531 err = mmc_send_cmd(mmc, cmd, NULL);
534 mmc->op_cond_response = cmd->response[0];
538 int mmc_send_op_cond(struct mmc *mmc)
543 /* Some cards seem to need this */
546 /* Asking to the card its capabilities */
547 mmc->op_cond_pending = 1;
548 for (i = 0; i < 2; i++) {
549 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
553 /* exit if not busy (flag seems to be inverted) */
554 if (mmc->op_cond_response & OCR_BUSY)
560 int mmc_complete_op_cond(struct mmc *mmc)
567 mmc->op_cond_pending = 0;
568 start = get_timer(0);
570 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
573 if (get_timer(start) > timeout)
576 } while (!(mmc->op_cond_response & OCR_BUSY));
577 if (!(mmc->op_cond_response & OCR_BUSY)) {
578 debug("%s: timeout\n", __func__);
582 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
583 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
584 cmd.resp_type = MMC_RSP_R3;
587 err = mmc_send_cmd(mmc, &cmd, NULL);
593 mmc->version = MMC_VERSION_UNKNOWN;
594 mmc->ocr = cmd.response[0];
596 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
603 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
606 struct mmc_data data;
609 /* Get the Card Status Register */
610 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
611 cmd.resp_type = MMC_RSP_R1;
614 data.dest = (char *)ext_csd;
616 data.blocksize = MMC_MAX_BLOCK_LEN;
617 data.flags = MMC_DATA_READ;
619 err = mmc_send_cmd(mmc, &cmd, &data);
625 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
631 cmd.cmdidx = MMC_CMD_SWITCH;
632 cmd.resp_type = MMC_RSP_R1b;
633 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
637 ret = mmc_send_cmd(mmc, &cmd, NULL);
639 /* Waiting for the ready status */
641 ret = mmc_send_status(mmc, timeout);
647 static int mmc_change_freq(struct mmc *mmc)
649 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
655 if (mmc_host_is_spi(mmc))
658 /* Only version 4 supports high-speed */
659 if (mmc->version < MMC_VERSION_4)
662 err = mmc_send_ext_csd(mmc, ext_csd);
667 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
669 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
674 /* Now check to see that it worked */
675 err = mmc_send_ext_csd(mmc, ext_csd);
680 /* No high-speed support */
681 if (!ext_csd[EXT_CSD_HS_TIMING])
684 /* High Speed is set, there are two types: 52MHz and 26MHz */
685 if (cardtype & MMC_HS_52MHZ)
686 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
688 mmc->card_caps |= MMC_MODE_HS;
693 static int mmc_set_capacity(struct mmc *mmc, int part_num)
697 mmc->capacity = mmc->capacity_user;
701 mmc->capacity = mmc->capacity_boot;
704 mmc->capacity = mmc->capacity_rpmb;
710 mmc->capacity = mmc->capacity_gp[part_num - 4];
716 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
721 int mmc_switch_part(int dev_num, unsigned int part_num)
723 struct mmc *mmc = find_mmc_device(dev_num);
729 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
730 (mmc->part_config & ~PART_ACCESS_MASK)
731 | (part_num & PART_ACCESS_MASK));
735 return mmc_set_capacity(mmc, part_num);
738 int mmc_getcd(struct mmc *mmc)
742 cd = board_mmc_getcd(mmc);
746 cd = mmc->getcd(mmc);
754 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
757 struct mmc_data data;
759 /* Switch the frequency */
760 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
761 cmd.resp_type = MMC_RSP_R1;
762 cmd.cmdarg = (mode << 31) | 0xffffff;
763 cmd.cmdarg &= ~(0xf << (group * 4));
764 cmd.cmdarg |= value << (group * 4);
766 data.dest = (char *)resp;
769 data.flags = MMC_DATA_READ;
771 return mmc_send_cmd(mmc, &cmd, &data);
775 static int sd_change_freq(struct mmc *mmc)
779 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
780 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
781 struct mmc_data data;
786 if (mmc_host_is_spi(mmc))
789 /* Read the SCR to find out if this card supports higher speeds */
790 cmd.cmdidx = MMC_CMD_APP_CMD;
791 cmd.resp_type = MMC_RSP_R1;
792 cmd.cmdarg = mmc->rca << 16;
794 err = mmc_send_cmd(mmc, &cmd, NULL);
799 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
800 cmd.resp_type = MMC_RSP_R1;
806 data.dest = (char *)scr;
809 data.flags = MMC_DATA_READ;
811 err = mmc_send_cmd(mmc, &cmd, &data);
820 mmc->scr[0] = __be32_to_cpu(scr[0]);
821 mmc->scr[1] = __be32_to_cpu(scr[1]);
823 switch ((mmc->scr[0] >> 24) & 0xf) {
825 mmc->version = SD_VERSION_1_0;
828 mmc->version = SD_VERSION_1_10;
831 mmc->version = SD_VERSION_2;
832 if ((mmc->scr[0] >> 15) & 0x1)
833 mmc->version = SD_VERSION_3;
836 mmc->version = SD_VERSION_1_0;
840 if (mmc->scr[0] & SD_DATA_4BIT)
841 mmc->card_caps |= MMC_MODE_4BIT;
843 /* Version 1.0 doesn't support switching */
844 if (mmc->version == SD_VERSION_1_0)
849 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
850 (u8 *)switch_status);
855 /* The high-speed function is busy. Try again */
856 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
860 /* If high-speed isn't supported, we return */
861 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
865 * If the host doesn't support SD_HIGHSPEED, do not switch card to
866 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
867 * This can avoid furthur problem when the card runs in different
868 * mode between the host.
870 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
871 (mmc->host_caps & MMC_MODE_HS)))
874 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
879 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
880 mmc->card_caps |= MMC_MODE_HS;
885 /* frequency bases */
886 /* divided by 10 to be nice to platforms without floating point */
887 static const int fbase[] = {
894 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
895 * to platforms without floating point.
897 static const int multipliers[] = {
916 static void mmc_set_ios(struct mmc *mmc)
921 void mmc_set_clock(struct mmc *mmc, uint clock)
923 if (clock > mmc->f_max)
926 if (clock < mmc->f_min)
934 static void mmc_set_bus_width(struct mmc *mmc, uint width)
936 mmc->bus_width = width;
941 static int mmc_startup(struct mmc *mmc)
945 u64 cmult, csize, capacity;
947 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
948 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
951 #ifdef CONFIG_MMC_SPI_CRC_ON
952 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
953 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
954 cmd.resp_type = MMC_RSP_R1;
956 err = mmc_send_cmd(mmc, &cmd, NULL);
963 /* Put the Card in Identify Mode */
964 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
965 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
966 cmd.resp_type = MMC_RSP_R2;
969 err = mmc_send_cmd(mmc, &cmd, NULL);
974 memcpy(mmc->cid, cmd.response, 16);
977 * For MMC cards, set the Relative Address.
978 * For SD cards, get the Relatvie Address.
979 * This also puts the cards into Standby State
981 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
982 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
983 cmd.cmdarg = mmc->rca << 16;
984 cmd.resp_type = MMC_RSP_R6;
986 err = mmc_send_cmd(mmc, &cmd, NULL);
992 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
995 /* Get the Card-Specific Data */
996 cmd.cmdidx = MMC_CMD_SEND_CSD;
997 cmd.resp_type = MMC_RSP_R2;
998 cmd.cmdarg = mmc->rca << 16;
1000 err = mmc_send_cmd(mmc, &cmd, NULL);
1002 /* Waiting for the ready status */
1003 mmc_send_status(mmc, timeout);
1008 mmc->csd[0] = cmd.response[0];
1009 mmc->csd[1] = cmd.response[1];
1010 mmc->csd[2] = cmd.response[2];
1011 mmc->csd[3] = cmd.response[3];
1013 if (mmc->version == MMC_VERSION_UNKNOWN) {
1014 int version = (cmd.response[0] >> 26) & 0xf;
1018 mmc->version = MMC_VERSION_1_2;
1021 mmc->version = MMC_VERSION_1_4;
1024 mmc->version = MMC_VERSION_2_2;
1027 mmc->version = MMC_VERSION_3;
1030 mmc->version = MMC_VERSION_4;
1033 mmc->version = MMC_VERSION_1_2;
1038 /* divide frequency by 10, since the mults are 10x bigger */
1039 freq = fbase[(cmd.response[0] & 0x7)];
1040 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1042 mmc->tran_speed = freq * mult;
1044 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1047 mmc->write_bl_len = mmc->read_bl_len;
1049 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1051 if (mmc->high_capacity) {
1052 csize = (mmc->csd[1] & 0x3f) << 16
1053 | (mmc->csd[2] & 0xffff0000) >> 16;
1056 csize = (mmc->csd[1] & 0x3ff) << 2
1057 | (mmc->csd[2] & 0xc0000000) >> 30;
1058 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1061 mmc->capacity_user = (csize + 1) << (cmult + 2);
1062 mmc->capacity_user *= mmc->read_bl_len;
1063 mmc->capacity_boot = 0;
1064 mmc->capacity_rpmb = 0;
1065 for (i = 0; i < 4; i++)
1066 mmc->capacity_gp[i] = 0;
1068 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1069 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1071 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1072 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1074 /* Select the card, and put it into Transfer Mode */
1075 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1076 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1077 cmd.resp_type = MMC_RSP_R1;
1078 cmd.cmdarg = mmc->rca << 16;
1079 err = mmc_send_cmd(mmc, &cmd, NULL);
1086 * For SD, its erase group is always one sector
1088 mmc->erase_grp_size = 1;
1089 mmc->part_config = MMCPART_NOAVAILABLE;
1090 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1091 /* check ext_csd version and capacity */
1092 err = mmc_send_ext_csd(mmc, ext_csd);
1093 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1095 * According to the JEDEC Standard, the value of
1096 * ext_csd's capacity is valid if the value is more
1099 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1100 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1101 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1102 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1103 capacity *= MMC_MAX_BLOCK_LEN;
1104 if ((capacity >> 20) > 2 * 1024)
1105 mmc->capacity_user = capacity;
1108 switch (ext_csd[EXT_CSD_REV]) {
1110 mmc->version = MMC_VERSION_4_1;
1113 mmc->version = MMC_VERSION_4_2;
1116 mmc->version = MMC_VERSION_4_3;
1119 mmc->version = MMC_VERSION_4_41;
1122 mmc->version = MMC_VERSION_4_5;
1127 * Check whether GROUP_DEF is set, if yes, read out
1128 * group size from ext_csd directly, or calculate
1129 * the group size from the csd value.
1131 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
1132 mmc->erase_grp_size =
1133 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1134 MMC_MAX_BLOCK_LEN * 1024;
1136 int erase_gsz, erase_gmul;
1137 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1138 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1139 mmc->erase_grp_size = (erase_gsz + 1)
1143 /* store the partition info of emmc */
1144 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1145 ext_csd[EXT_CSD_BOOT_MULT])
1146 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1148 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1150 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1152 for (i = 0; i < 4; i++) {
1153 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1154 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1155 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1156 mmc->capacity_gp[i] *=
1157 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1158 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1162 err = mmc_set_capacity(mmc, mmc->part_num);
1167 err = sd_change_freq(mmc);
1169 err = mmc_change_freq(mmc);
1174 /* Restrict card's capabilities by what the host can do */
1175 mmc->card_caps &= mmc->host_caps;
1178 if (mmc->card_caps & MMC_MODE_4BIT) {
1179 cmd.cmdidx = MMC_CMD_APP_CMD;
1180 cmd.resp_type = MMC_RSP_R1;
1181 cmd.cmdarg = mmc->rca << 16;
1183 err = mmc_send_cmd(mmc, &cmd, NULL);
1187 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1188 cmd.resp_type = MMC_RSP_R1;
1190 err = mmc_send_cmd(mmc, &cmd, NULL);
1194 mmc_set_bus_width(mmc, 4);
1197 if (mmc->card_caps & MMC_MODE_HS)
1198 mmc->tran_speed = 50000000;
1200 mmc->tran_speed = 25000000;
1204 /* An array of possible bus widths in order of preference */
1205 static unsigned ext_csd_bits[] = {
1206 EXT_CSD_BUS_WIDTH_8,
1207 EXT_CSD_BUS_WIDTH_4,
1208 EXT_CSD_BUS_WIDTH_1,
1211 /* An array to map CSD bus widths to host cap bits */
1212 static unsigned ext_to_hostcaps[] = {
1213 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1214 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1217 /* An array to map chosen bus width to an integer */
1218 static unsigned widths[] = {
1222 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1223 unsigned int extw = ext_csd_bits[idx];
1226 * Check to make sure the controller supports
1227 * this bus width, if it's more than 1
1229 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1230 !(mmc->host_caps & ext_to_hostcaps[extw]))
1233 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1234 EXT_CSD_BUS_WIDTH, extw);
1239 mmc_set_bus_width(mmc, widths[idx]);
1241 err = mmc_send_ext_csd(mmc, test_csd);
1242 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1243 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1244 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1245 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1246 && ext_csd[EXT_CSD_REV] \
1247 == test_csd[EXT_CSD_REV]
1248 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1249 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1250 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1251 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1253 mmc->card_caps |= ext_to_hostcaps[extw];
1258 if (mmc->card_caps & MMC_MODE_HS) {
1259 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1260 mmc->tran_speed = 52000000;
1262 mmc->tran_speed = 26000000;
1266 mmc_set_clock(mmc, mmc->tran_speed);
1268 /* fill in device description */
1269 mmc->block_dev.lun = 0;
1270 mmc->block_dev.type = 0;
1271 mmc->block_dev.blksz = mmc->read_bl_len;
1272 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1273 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1274 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1275 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1276 (mmc->cid[3] >> 16) & 0xffff);
1277 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1278 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1279 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1280 (mmc->cid[2] >> 24) & 0xff);
1281 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1282 (mmc->cid[2] >> 16) & 0xf);
1283 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1284 init_part(&mmc->block_dev);
1290 static int mmc_send_if_cond(struct mmc *mmc)
1295 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1296 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1297 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1298 cmd.resp_type = MMC_RSP_R7;
1300 err = mmc_send_cmd(mmc, &cmd, NULL);
1305 if ((cmd.response[0] & 0xff) != 0xaa)
1306 return UNUSABLE_ERR;
1308 mmc->version = SD_VERSION_2;
1313 int mmc_register(struct mmc *mmc)
1315 /* Setup the universal parts of the block interface just once */
1316 mmc->block_dev.if_type = IF_TYPE_MMC;
1317 mmc->block_dev.dev = cur_dev_num++;
1318 mmc->block_dev.removable = 1;
1319 mmc->block_dev.block_read = mmc_bread;
1320 mmc->block_dev.block_write = mmc_bwrite;
1321 mmc->block_dev.block_erase = mmc_berase;
1323 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1325 INIT_LIST_HEAD (&mmc->link);
1327 list_add_tail (&mmc->link, &mmc_devices);
1332 #ifdef CONFIG_PARTITIONS
1333 block_dev_desc_t *mmc_get_dev(int dev)
1335 struct mmc *mmc = find_mmc_device(dev);
1336 if (!mmc || mmc_init(mmc))
1339 return &mmc->block_dev;
1343 int mmc_start_init(struct mmc *mmc)
1347 if (mmc_getcd(mmc) == 0) {
1349 printf("MMC: no card present\n");
1356 err = mmc->init(mmc);
1361 mmc_set_bus_width(mmc, 1);
1362 mmc_set_clock(mmc, 1);
1364 /* Reset the Card */
1365 err = mmc_go_idle(mmc);
1370 /* The internal partition reset to user partition(0) at every CMD0*/
1373 /* Test for SD version 2 */
1374 err = mmc_send_if_cond(mmc);
1376 /* Now try to get the SD card's operating condition */
1377 err = sd_send_op_cond(mmc);
1379 /* If the command timed out, we check for an MMC card */
1380 if (err == TIMEOUT) {
1381 err = mmc_send_op_cond(mmc);
1383 if (err && err != IN_PROGRESS) {
1384 printf("Card did not respond to voltage select!\n");
1385 return UNUSABLE_ERR;
1389 if (err == IN_PROGRESS)
1390 mmc->init_in_progress = 1;
1395 static int mmc_complete_init(struct mmc *mmc)
1399 if (mmc->op_cond_pending)
1400 err = mmc_complete_op_cond(mmc);
1403 err = mmc_startup(mmc);
1408 mmc->init_in_progress = 0;
1412 int mmc_init(struct mmc *mmc)
1414 int err = IN_PROGRESS;
1415 unsigned start = get_timer(0);
1419 if (!mmc->init_in_progress)
1420 err = mmc_start_init(mmc);
1422 if (!err || err == IN_PROGRESS)
1423 err = mmc_complete_init(mmc);
1424 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1429 * CPU and board-specific MMC initializations. Aliased function
1430 * signals caller to move on
1432 static int __def_mmc_init(bd_t *bis)
1437 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1438 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1440 void print_mmc_devices(char separator)
1443 struct list_head *entry;
1445 list_for_each(entry, &mmc_devices) {
1446 m = list_entry(entry, struct mmc, link);
1448 printf("%s: %d", m->name, m->block_dev.dev);
1450 if (entry->next != &mmc_devices)
1451 printf("%c ", separator);
1457 int get_mmc_num(void)
1462 void mmc_set_preinit(struct mmc *mmc, int preinit)
1464 mmc->preinit = preinit;
1467 static void do_preinit(void)
1470 struct list_head *entry;
1472 list_for_each(entry, &mmc_devices) {
1473 m = list_entry(entry, struct mmc, link);
1481 int mmc_initialize(bd_t *bis)
1483 INIT_LIST_HEAD (&mmc_devices);
1486 if (board_mmc_init(bis) < 0)
1489 print_mmc_devices(',');
1495 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1497 * This function changes the size of boot partition and the size of rpmb
1498 * partition present on EMMC devices.
1501 * struct *mmc: pointer for the mmc device strcuture
1502 * bootsize: size of boot partition
1503 * rpmbsize: size of rpmb partition
1505 * Returns 0 on success.
1508 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1509 unsigned long rpmbsize)
1514 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1515 cmd.cmdidx = MMC_CMD_RES_MAN;
1516 cmd.resp_type = MMC_RSP_R1b;
1517 cmd.cmdarg = MMC_CMD62_ARG1;
1519 err = mmc_send_cmd(mmc, &cmd, NULL);
1521 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1525 /* Boot partition changing mode */
1526 cmd.cmdidx = MMC_CMD_RES_MAN;
1527 cmd.resp_type = MMC_RSP_R1b;
1528 cmd.cmdarg = MMC_CMD62_ARG2;
1530 err = mmc_send_cmd(mmc, &cmd, NULL);
1532 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1535 /* boot partition size is multiple of 128KB */
1536 bootsize = (bootsize * 1024) / 128;
1538 /* Arg: boot partition size */
1539 cmd.cmdidx = MMC_CMD_RES_MAN;
1540 cmd.resp_type = MMC_RSP_R1b;
1541 cmd.cmdarg = bootsize;
1543 err = mmc_send_cmd(mmc, &cmd, NULL);
1545 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1548 /* RPMB partition size is multiple of 128KB */
1549 rpmbsize = (rpmbsize * 1024) / 128;
1550 /* Arg: RPMB partition size */
1551 cmd.cmdidx = MMC_CMD_RES_MAN;
1552 cmd.resp_type = MMC_RSP_R1b;
1553 cmd.cmdarg = rpmbsize;
1555 err = mmc_send_cmd(mmc, &cmd, NULL);
1557 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1564 * This function shall form and send the commands to open / close the
1565 * boot partition specified by user.
1568 * ack: 0x0 - No boot acknowledge sent (default)
1569 * 0x1 - Boot acknowledge sent during boot operation
1570 * part_num: User selects boot data that will be sent to master
1571 * 0x0 - Device not boot enabled (default)
1572 * 0x1 - Boot partition 1 enabled for boot
1573 * 0x2 - Boot partition 2 enabled for boot
1574 * access: User selects partitions to access
1575 * 0x0 : No access to boot partition (default)
1576 * 0x1 : R/W boot partition 1
1577 * 0x2 : R/W boot partition 2
1578 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1580 * Returns 0 on success.
1582 int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1587 /* Boot ack enable, boot partition enable , boot partition access */
1588 cmd.cmdidx = MMC_CMD_SWITCH;
1589 cmd.resp_type = MMC_RSP_R1b;
1591 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1592 (EXT_CSD_PART_CONF << 16) |
1593 ((EXT_CSD_BOOT_ACK(ack) |
1594 EXT_CSD_BOOT_PART_NUM(part_num) |
1595 EXT_CSD_PARTITION_ACCESS(access)) << 8);
1597 err = mmc_send_cmd(mmc, &cmd, NULL);
1600 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1603 debug("mmc boot partition#%d close fail:Error = %d\n",
1610 /* 4bit transfer mode at booting time. */
1611 cmd.cmdidx = MMC_CMD_SWITCH;
1612 cmd.resp_type = MMC_RSP_R1b;
1614 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1615 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1618 err = mmc_send_cmd(mmc, &cmd, NULL);
1620 debug("mmc boot partition#%d open fail:Error2 = %d\n",