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)
60 #ifdef CONFIG_MMC_TRACE
64 printf("CMD_SEND:%d\n", cmd->cmdidx);
65 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
66 ret = mmc->send_cmd(mmc, cmd, data);
67 switch (cmd->resp_type) {
69 printf("\t\tMMC_RSP_NONE\n");
72 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
76 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
80 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
82 printf("\t\t \t\t 0x%08X \n",
84 printf("\t\t \t\t 0x%08X \n",
86 printf("\t\t \t\t 0x%08X \n",
89 printf("\t\t\t\t\tDUMPING DATA\n");
90 for (i = 0; i < 4; i++) {
92 printf("\t\t\t\t\t%03d - ", i*4);
93 ptr = (u8 *)&cmd->response[i];
95 for (j = 0; j < 4; j++)
96 printf("%02X ", *ptr--);
101 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
105 printf("\t\tERROR MMC rsp not supported\n");
109 ret = mmc->send_cmd(mmc, cmd, data);
114 static int mmc_send_status(struct mmc *mmc, int timeout)
117 int err, retries = 5;
118 #ifdef CONFIG_MMC_TRACE
122 cmd.cmdidx = MMC_CMD_SEND_STATUS;
123 cmd.resp_type = MMC_RSP_R1;
124 if (!mmc_host_is_spi(mmc))
125 cmd.cmdarg = mmc->rca << 16;
128 err = mmc_send_cmd(mmc, &cmd, NULL);
130 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
131 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
134 else if (cmd.response[0] & MMC_STATUS_MASK) {
135 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
136 printf("Status Error: 0x%08X\n",
141 } else if (--retries < 0)
148 #ifdef CONFIG_MMC_TRACE
149 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
150 printf("CURR STATE:%d\n", status);
153 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
154 printf("Timeout waiting card ready\n");
162 static int mmc_set_blocklen(struct mmc *mmc, int len)
166 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
167 cmd.resp_type = MMC_RSP_R1;
170 return mmc_send_cmd(mmc, &cmd, NULL);
173 struct mmc *find_mmc_device(int dev_num)
176 struct list_head *entry;
178 list_for_each(entry, &mmc_devices) {
179 m = list_entry(entry, struct mmc, link);
181 if (m->block_dev.dev == dev_num)
185 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
186 printf("MMC Device %d not found\n", dev_num);
192 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
196 int err, start_cmd, end_cmd;
198 if (mmc->high_capacity)
199 end = start + blkcnt - 1;
201 end = (start + blkcnt - 1) * mmc->write_bl_len;
202 start *= mmc->write_bl_len;
206 start_cmd = SD_CMD_ERASE_WR_BLK_START;
207 end_cmd = SD_CMD_ERASE_WR_BLK_END;
209 start_cmd = MMC_CMD_ERASE_GROUP_START;
210 end_cmd = MMC_CMD_ERASE_GROUP_END;
213 cmd.cmdidx = start_cmd;
215 cmd.resp_type = MMC_RSP_R1;
217 err = mmc_send_cmd(mmc, &cmd, NULL);
221 cmd.cmdidx = end_cmd;
224 err = mmc_send_cmd(mmc, &cmd, NULL);
228 cmd.cmdidx = MMC_CMD_ERASE;
229 cmd.cmdarg = SECURE_ERASE;
230 cmd.resp_type = MMC_RSP_R1b;
232 err = mmc_send_cmd(mmc, &cmd, NULL);
239 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
240 puts("mmc erase failed\n");
246 mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
249 struct mmc *mmc = find_mmc_device(dev_num);
250 lbaint_t blk = 0, blk_r = 0;
256 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
257 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
258 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
259 "The erase range would be change to "
260 "0x" LBAF "~0x" LBAF "\n\n",
261 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
262 ((start + blkcnt + mmc->erase_grp_size)
263 & ~(mmc->erase_grp_size - 1)) - 1);
266 while (blk < blkcnt) {
267 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
268 mmc->erase_grp_size : (blkcnt - blk);
269 err = mmc_erase_t(mmc, start + blk, blk_r);
275 /* Waiting for the ready status */
276 if (mmc_send_status(mmc, timeout))
284 mmc_write_blocks(struct mmc *mmc, lbaint_t start, lbaint_t blkcnt, const void*src)
287 struct mmc_data data;
290 if ((start + blkcnt) > mmc->block_dev.lba) {
291 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
292 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
293 start + blkcnt, mmc->block_dev.lba);
300 else if (blkcnt == 1)
301 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
303 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
305 if (mmc->high_capacity)
308 cmd.cmdarg = start * mmc->write_bl_len;
310 cmd.resp_type = MMC_RSP_R1;
313 data.blocks = blkcnt;
314 data.blocksize = mmc->write_bl_len;
315 data.flags = MMC_DATA_WRITE;
317 if (mmc_send_cmd(mmc, &cmd, &data)) {
318 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
319 printf("mmc write failed\n");
324 /* SPI multiblock writes terminate using a special
325 * token, not a STOP_TRANSMISSION request.
327 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
328 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
330 cmd.resp_type = MMC_RSP_R1b;
331 if (mmc_send_cmd(mmc, &cmd, NULL)) {
332 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
333 printf("mmc fail to send stop cmd\n");
339 /* Waiting for the ready status */
340 if (mmc_send_status(mmc, timeout))
347 mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void*src)
349 lbaint_t cur, blocks_todo = blkcnt;
351 struct mmc *mmc = find_mmc_device(dev_num);
355 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
359 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
360 if(mmc_write_blocks(mmc, start, cur, src) != cur)
364 src += cur * mmc->write_bl_len;
365 } while (blocks_todo > 0);
370 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
374 struct mmc_data data;
377 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
379 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
381 if (mmc->high_capacity)
384 cmd.cmdarg = start * mmc->read_bl_len;
386 cmd.resp_type = MMC_RSP_R1;
389 data.blocks = blkcnt;
390 data.blocksize = mmc->read_bl_len;
391 data.flags = MMC_DATA_READ;
393 if (mmc_send_cmd(mmc, &cmd, &data))
397 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
399 cmd.resp_type = MMC_RSP_R1b;
400 if (mmc_send_cmd(mmc, &cmd, NULL)) {
401 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
402 printf("mmc fail to send stop cmd\n");
411 static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
413 lbaint_t cur, blocks_todo = blkcnt;
418 struct mmc *mmc = find_mmc_device(dev_num);
422 if ((start + blkcnt) > mmc->block_dev.lba) {
423 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
424 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
425 start + blkcnt, mmc->block_dev.lba);
430 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
434 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
435 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
439 dst += cur * mmc->read_bl_len;
440 } while (blocks_todo > 0);
445 static int mmc_go_idle(struct mmc *mmc)
452 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
454 cmd.resp_type = MMC_RSP_NONE;
456 err = mmc_send_cmd(mmc, &cmd, NULL);
466 static int sd_send_op_cond(struct mmc *mmc)
473 cmd.cmdidx = MMC_CMD_APP_CMD;
474 cmd.resp_type = MMC_RSP_R1;
477 err = mmc_send_cmd(mmc, &cmd, NULL);
482 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
483 cmd.resp_type = MMC_RSP_R3;
486 * Most cards do not answer if some reserved bits
487 * in the ocr are set. However, Some controller
488 * can set bit 7 (reserved for low voltages), but
489 * how to manage low voltages SD card is not yet
492 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
493 (mmc->voltages & 0xff8000);
495 if (mmc->version == SD_VERSION_2)
496 cmd.cmdarg |= OCR_HCS;
498 err = mmc_send_cmd(mmc, &cmd, NULL);
504 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
509 if (mmc->version != SD_VERSION_2)
510 mmc->version = SD_VERSION_1_0;
512 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
513 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
514 cmd.resp_type = MMC_RSP_R3;
517 err = mmc_send_cmd(mmc, &cmd, NULL);
523 mmc->ocr = cmd.response[0];
525 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
531 /* We pass in the cmd since otherwise the init seems to fail */
532 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
537 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
538 cmd->resp_type = MMC_RSP_R3;
540 if (use_arg && !mmc_host_is_spi(mmc)) {
543 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
544 (mmc->op_cond_response & OCR_ACCESS_MODE);
546 if (mmc->host_caps & MMC_MODE_HC)
547 cmd->cmdarg |= OCR_HCS;
549 err = mmc_send_cmd(mmc, cmd, NULL);
552 mmc->op_cond_response = cmd->response[0];
556 int mmc_send_op_cond(struct mmc *mmc)
561 /* Some cards seem to need this */
564 /* Asking to the card its capabilities */
565 mmc->op_cond_pending = 1;
566 for (i = 0; i < 2; i++) {
567 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
571 /* exit if not busy (flag seems to be inverted) */
572 if (mmc->op_cond_response & OCR_BUSY)
578 int mmc_complete_op_cond(struct mmc *mmc)
585 mmc->op_cond_pending = 0;
586 start = get_timer(0);
588 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
591 if (get_timer(start) > timeout)
594 } while (!(mmc->op_cond_response & OCR_BUSY));
596 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
597 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
598 cmd.resp_type = MMC_RSP_R3;
601 err = mmc_send_cmd(mmc, &cmd, NULL);
607 mmc->version = MMC_VERSION_UNKNOWN;
608 mmc->ocr = cmd.response[0];
610 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
617 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
620 struct mmc_data data;
623 /* Get the Card Status Register */
624 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
625 cmd.resp_type = MMC_RSP_R1;
628 data.dest = (char *)ext_csd;
630 data.blocksize = MMC_MAX_BLOCK_LEN;
631 data.flags = MMC_DATA_READ;
633 err = mmc_send_cmd(mmc, &cmd, &data);
639 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
645 cmd.cmdidx = MMC_CMD_SWITCH;
646 cmd.resp_type = MMC_RSP_R1b;
647 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
651 ret = mmc_send_cmd(mmc, &cmd, NULL);
653 /* Waiting for the ready status */
655 ret = mmc_send_status(mmc, timeout);
661 static int mmc_change_freq(struct mmc *mmc)
663 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
669 if (mmc_host_is_spi(mmc))
672 /* Only version 4 supports high-speed */
673 if (mmc->version < MMC_VERSION_4)
676 err = mmc_send_ext_csd(mmc, ext_csd);
681 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
683 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
688 /* Now check to see that it worked */
689 err = mmc_send_ext_csd(mmc, ext_csd);
694 /* No high-speed support */
695 if (!ext_csd[EXT_CSD_HS_TIMING])
698 /* High Speed is set, there are two types: 52MHz and 26MHz */
699 if (cardtype & MMC_HS_52MHZ)
700 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
702 mmc->card_caps |= MMC_MODE_HS;
707 static int mmc_set_capacity(struct mmc *mmc, int part_num)
711 mmc->capacity = mmc->capacity_user;
715 mmc->capacity = mmc->capacity_boot;
718 mmc->capacity = mmc->capacity_rpmb;
724 mmc->capacity = mmc->capacity_gp[part_num - 4];
730 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
735 int mmc_switch_part(int dev_num, unsigned int part_num)
737 struct mmc *mmc = find_mmc_device(dev_num);
743 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
744 (mmc->part_config & ~PART_ACCESS_MASK)
745 | (part_num & PART_ACCESS_MASK));
749 return mmc_set_capacity(mmc, part_num);
752 int mmc_getcd(struct mmc *mmc)
756 cd = board_mmc_getcd(mmc);
760 cd = mmc->getcd(mmc);
768 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
771 struct mmc_data data;
773 /* Switch the frequency */
774 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
775 cmd.resp_type = MMC_RSP_R1;
776 cmd.cmdarg = (mode << 31) | 0xffffff;
777 cmd.cmdarg &= ~(0xf << (group * 4));
778 cmd.cmdarg |= value << (group * 4);
780 data.dest = (char *)resp;
783 data.flags = MMC_DATA_READ;
785 return mmc_send_cmd(mmc, &cmd, &data);
789 static int sd_change_freq(struct mmc *mmc)
793 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
794 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
795 struct mmc_data data;
800 if (mmc_host_is_spi(mmc))
803 /* Read the SCR to find out if this card supports higher speeds */
804 cmd.cmdidx = MMC_CMD_APP_CMD;
805 cmd.resp_type = MMC_RSP_R1;
806 cmd.cmdarg = mmc->rca << 16;
808 err = mmc_send_cmd(mmc, &cmd, NULL);
813 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
814 cmd.resp_type = MMC_RSP_R1;
820 data.dest = (char *)scr;
823 data.flags = MMC_DATA_READ;
825 err = mmc_send_cmd(mmc, &cmd, &data);
834 mmc->scr[0] = __be32_to_cpu(scr[0]);
835 mmc->scr[1] = __be32_to_cpu(scr[1]);
837 switch ((mmc->scr[0] >> 24) & 0xf) {
839 mmc->version = SD_VERSION_1_0;
842 mmc->version = SD_VERSION_1_10;
845 mmc->version = SD_VERSION_2;
846 if ((mmc->scr[0] >> 15) & 0x1)
847 mmc->version = SD_VERSION_3;
850 mmc->version = SD_VERSION_1_0;
854 if (mmc->scr[0] & SD_DATA_4BIT)
855 mmc->card_caps |= MMC_MODE_4BIT;
857 /* Version 1.0 doesn't support switching */
858 if (mmc->version == SD_VERSION_1_0)
863 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
864 (u8 *)switch_status);
869 /* The high-speed function is busy. Try again */
870 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
874 /* If high-speed isn't supported, we return */
875 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
879 * If the host doesn't support SD_HIGHSPEED, do not switch card to
880 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
881 * This can avoid furthur problem when the card runs in different
882 * mode between the host.
884 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
885 (mmc->host_caps & MMC_MODE_HS)))
888 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
893 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
894 mmc->card_caps |= MMC_MODE_HS;
899 /* frequency bases */
900 /* divided by 10 to be nice to platforms without floating point */
901 static const int fbase[] = {
908 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
909 * to platforms without floating point.
911 static const int multipliers[] = {
930 static void mmc_set_ios(struct mmc *mmc)
935 void mmc_set_clock(struct mmc *mmc, uint clock)
937 if (clock > mmc->f_max)
940 if (clock < mmc->f_min)
948 static void mmc_set_bus_width(struct mmc *mmc, uint width)
950 mmc->bus_width = width;
955 static int mmc_startup(struct mmc *mmc)
959 u64 cmult, csize, capacity;
961 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
962 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
965 #ifdef CONFIG_MMC_SPI_CRC_ON
966 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
967 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
968 cmd.resp_type = MMC_RSP_R1;
970 err = mmc_send_cmd(mmc, &cmd, NULL);
977 /* Put the Card in Identify Mode */
978 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
979 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
980 cmd.resp_type = MMC_RSP_R2;
983 err = mmc_send_cmd(mmc, &cmd, NULL);
988 memcpy(mmc->cid, cmd.response, 16);
991 * For MMC cards, set the Relative Address.
992 * For SD cards, get the Relatvie Address.
993 * This also puts the cards into Standby State
995 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
996 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
997 cmd.cmdarg = mmc->rca << 16;
998 cmd.resp_type = MMC_RSP_R6;
1000 err = mmc_send_cmd(mmc, &cmd, NULL);
1006 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1009 /* Get the Card-Specific Data */
1010 cmd.cmdidx = MMC_CMD_SEND_CSD;
1011 cmd.resp_type = MMC_RSP_R2;
1012 cmd.cmdarg = mmc->rca << 16;
1014 err = mmc_send_cmd(mmc, &cmd, NULL);
1016 /* Waiting for the ready status */
1017 mmc_send_status(mmc, timeout);
1022 mmc->csd[0] = cmd.response[0];
1023 mmc->csd[1] = cmd.response[1];
1024 mmc->csd[2] = cmd.response[2];
1025 mmc->csd[3] = cmd.response[3];
1027 if (mmc->version == MMC_VERSION_UNKNOWN) {
1028 int version = (cmd.response[0] >> 26) & 0xf;
1032 mmc->version = MMC_VERSION_1_2;
1035 mmc->version = MMC_VERSION_1_4;
1038 mmc->version = MMC_VERSION_2_2;
1041 mmc->version = MMC_VERSION_3;
1044 mmc->version = MMC_VERSION_4;
1047 mmc->version = MMC_VERSION_1_2;
1052 /* divide frequency by 10, since the mults are 10x bigger */
1053 freq = fbase[(cmd.response[0] & 0x7)];
1054 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1056 mmc->tran_speed = freq * mult;
1058 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1061 mmc->write_bl_len = mmc->read_bl_len;
1063 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1065 if (mmc->high_capacity) {
1066 csize = (mmc->csd[1] & 0x3f) << 16
1067 | (mmc->csd[2] & 0xffff0000) >> 16;
1070 csize = (mmc->csd[1] & 0x3ff) << 2
1071 | (mmc->csd[2] & 0xc0000000) >> 30;
1072 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1075 mmc->capacity_user = (csize + 1) << (cmult + 2);
1076 mmc->capacity_user *= mmc->read_bl_len;
1077 mmc->capacity_boot = 0;
1078 mmc->capacity_rpmb = 0;
1079 for (i = 0; i < 4; i++)
1080 mmc->capacity_gp[i] = 0;
1082 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1083 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1085 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1086 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1088 /* Select the card, and put it into Transfer Mode */
1089 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1090 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1091 cmd.resp_type = MMC_RSP_R1;
1092 cmd.cmdarg = mmc->rca << 16;
1093 err = mmc_send_cmd(mmc, &cmd, NULL);
1100 * For SD, its erase group is always one sector
1102 mmc->erase_grp_size = 1;
1103 mmc->part_config = MMCPART_NOAVAILABLE;
1104 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1105 /* check ext_csd version and capacity */
1106 err = mmc_send_ext_csd(mmc, ext_csd);
1107 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1109 * According to the JEDEC Standard, the value of
1110 * ext_csd's capacity is valid if the value is more
1113 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1114 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1115 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1116 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1117 capacity *= MMC_MAX_BLOCK_LEN;
1118 if ((capacity >> 20) > 2 * 1024)
1119 mmc->capacity_user = capacity;
1122 switch (ext_csd[EXT_CSD_REV]) {
1124 mmc->version = MMC_VERSION_4_1;
1127 mmc->version = MMC_VERSION_4_2;
1130 mmc->version = MMC_VERSION_4_3;
1133 mmc->version = MMC_VERSION_4_41;
1136 mmc->version = MMC_VERSION_4_5;
1141 * Check whether GROUP_DEF is set, if yes, read out
1142 * group size from ext_csd directly, or calculate
1143 * the group size from the csd value.
1145 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
1146 mmc->erase_grp_size =
1147 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1148 MMC_MAX_BLOCK_LEN * 1024;
1150 int erase_gsz, erase_gmul;
1151 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1152 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1153 mmc->erase_grp_size = (erase_gsz + 1)
1157 /* store the partition info of emmc */
1158 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1159 ext_csd[EXT_CSD_BOOT_MULT])
1160 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1162 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1164 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1166 for (i = 0; i < 4; i++) {
1167 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1168 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1169 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1170 mmc->capacity_gp[i] *=
1171 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1172 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1176 err = mmc_set_capacity(mmc, mmc->part_num);
1181 err = sd_change_freq(mmc);
1183 err = mmc_change_freq(mmc);
1188 /* Restrict card's capabilities by what the host can do */
1189 mmc->card_caps &= mmc->host_caps;
1192 if (mmc->card_caps & MMC_MODE_4BIT) {
1193 cmd.cmdidx = MMC_CMD_APP_CMD;
1194 cmd.resp_type = MMC_RSP_R1;
1195 cmd.cmdarg = mmc->rca << 16;
1197 err = mmc_send_cmd(mmc, &cmd, NULL);
1201 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1202 cmd.resp_type = MMC_RSP_R1;
1204 err = mmc_send_cmd(mmc, &cmd, NULL);
1208 mmc_set_bus_width(mmc, 4);
1211 if (mmc->card_caps & MMC_MODE_HS)
1212 mmc->tran_speed = 50000000;
1214 mmc->tran_speed = 25000000;
1218 /* An array of possible bus widths in order of preference */
1219 static unsigned ext_csd_bits[] = {
1220 EXT_CSD_BUS_WIDTH_8,
1221 EXT_CSD_BUS_WIDTH_4,
1222 EXT_CSD_BUS_WIDTH_1,
1225 /* An array to map CSD bus widths to host cap bits */
1226 static unsigned ext_to_hostcaps[] = {
1227 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1228 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1231 /* An array to map chosen bus width to an integer */
1232 static unsigned widths[] = {
1236 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1237 unsigned int extw = ext_csd_bits[idx];
1240 * Check to make sure the controller supports
1241 * this bus width, if it's more than 1
1243 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1244 !(mmc->host_caps & ext_to_hostcaps[extw]))
1247 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1248 EXT_CSD_BUS_WIDTH, extw);
1253 mmc_set_bus_width(mmc, widths[idx]);
1255 err = mmc_send_ext_csd(mmc, test_csd);
1256 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1257 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1258 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1259 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1260 && ext_csd[EXT_CSD_REV] \
1261 == test_csd[EXT_CSD_REV]
1262 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1263 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1264 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1265 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1267 mmc->card_caps |= ext_to_hostcaps[extw];
1272 if (mmc->card_caps & MMC_MODE_HS) {
1273 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1274 mmc->tran_speed = 52000000;
1276 mmc->tran_speed = 26000000;
1280 mmc_set_clock(mmc, mmc->tran_speed);
1282 /* fill in device description */
1283 mmc->block_dev.lun = 0;
1284 mmc->block_dev.type = 0;
1285 mmc->block_dev.blksz = mmc->read_bl_len;
1286 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1287 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1288 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1289 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1290 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1291 (mmc->cid[3] >> 16) & 0xffff);
1292 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1293 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1294 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1295 (mmc->cid[2] >> 24) & 0xff);
1296 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1297 (mmc->cid[2] >> 16) & 0xf);
1299 mmc->block_dev.vendor[0] = 0;
1300 mmc->block_dev.product[0] = 0;
1301 mmc->block_dev.revision[0] = 0;
1303 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1304 init_part(&mmc->block_dev);
1310 static int mmc_send_if_cond(struct mmc *mmc)
1315 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1316 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1317 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1318 cmd.resp_type = MMC_RSP_R7;
1320 err = mmc_send_cmd(mmc, &cmd, NULL);
1325 if ((cmd.response[0] & 0xff) != 0xaa)
1326 return UNUSABLE_ERR;
1328 mmc->version = SD_VERSION_2;
1333 int mmc_register(struct mmc *mmc)
1335 /* Setup the universal parts of the block interface just once */
1336 mmc->block_dev.if_type = IF_TYPE_MMC;
1337 mmc->block_dev.dev = cur_dev_num++;
1338 mmc->block_dev.removable = 1;
1339 mmc->block_dev.block_read = mmc_bread;
1340 mmc->block_dev.block_write = mmc_bwrite;
1341 mmc->block_dev.block_erase = mmc_berase;
1343 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1345 INIT_LIST_HEAD (&mmc->link);
1347 list_add_tail (&mmc->link, &mmc_devices);
1352 #ifdef CONFIG_PARTITIONS
1353 block_dev_desc_t *mmc_get_dev(int dev)
1355 struct mmc *mmc = find_mmc_device(dev);
1356 if (!mmc || mmc_init(mmc))
1359 return &mmc->block_dev;
1363 int mmc_start_init(struct mmc *mmc)
1367 if (mmc_getcd(mmc) == 0) {
1369 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1370 printf("MMC: no card present\n");
1378 err = mmc->init(mmc);
1383 mmc_set_bus_width(mmc, 1);
1384 mmc_set_clock(mmc, 1);
1386 /* Reset the Card */
1387 err = mmc_go_idle(mmc);
1392 /* The internal partition reset to user partition(0) at every CMD0*/
1395 /* Test for SD version 2 */
1396 err = mmc_send_if_cond(mmc);
1398 /* Now try to get the SD card's operating condition */
1399 err = sd_send_op_cond(mmc);
1401 /* If the command timed out, we check for an MMC card */
1402 if (err == TIMEOUT) {
1403 err = mmc_send_op_cond(mmc);
1405 if (err && err != IN_PROGRESS) {
1406 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1407 printf("Card did not respond to voltage select!\n");
1409 return UNUSABLE_ERR;
1413 if (err == IN_PROGRESS)
1414 mmc->init_in_progress = 1;
1419 static int mmc_complete_init(struct mmc *mmc)
1423 if (mmc->op_cond_pending)
1424 err = mmc_complete_op_cond(mmc);
1427 err = mmc_startup(mmc);
1432 mmc->init_in_progress = 0;
1436 int mmc_init(struct mmc *mmc)
1438 int err = IN_PROGRESS;
1439 unsigned start = get_timer(0);
1443 if (!mmc->init_in_progress)
1444 err = mmc_start_init(mmc);
1446 if (!err || err == IN_PROGRESS)
1447 err = mmc_complete_init(mmc);
1448 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1453 * CPU and board-specific MMC initializations. Aliased function
1454 * signals caller to move on
1456 static int __def_mmc_init(bd_t *bis)
1461 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1462 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1464 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1466 void print_mmc_devices(char separator)
1469 struct list_head *entry;
1471 list_for_each(entry, &mmc_devices) {
1472 m = list_entry(entry, struct mmc, link);
1474 printf("%s: %d", m->name, m->block_dev.dev);
1476 if (entry->next != &mmc_devices)
1477 printf("%c ", separator);
1484 void print_mmc_devices(char separator) { }
1487 int get_mmc_num(void)
1492 void mmc_set_preinit(struct mmc *mmc, int preinit)
1494 mmc->preinit = preinit;
1497 static void do_preinit(void)
1500 struct list_head *entry;
1502 list_for_each(entry, &mmc_devices) {
1503 m = list_entry(entry, struct mmc, link);
1511 int mmc_initialize(bd_t *bis)
1513 INIT_LIST_HEAD (&mmc_devices);
1516 if (board_mmc_init(bis) < 0)
1519 #ifndef CONFIG_SPL_BUILD
1520 print_mmc_devices(',');
1527 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1529 * This function changes the size of boot partition and the size of rpmb
1530 * partition present on EMMC devices.
1533 * struct *mmc: pointer for the mmc device strcuture
1534 * bootsize: size of boot partition
1535 * rpmbsize: size of rpmb partition
1537 * Returns 0 on success.
1540 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1541 unsigned long rpmbsize)
1546 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1547 cmd.cmdidx = MMC_CMD_RES_MAN;
1548 cmd.resp_type = MMC_RSP_R1b;
1549 cmd.cmdarg = MMC_CMD62_ARG1;
1551 err = mmc_send_cmd(mmc, &cmd, NULL);
1553 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1557 /* Boot partition changing mode */
1558 cmd.cmdidx = MMC_CMD_RES_MAN;
1559 cmd.resp_type = MMC_RSP_R1b;
1560 cmd.cmdarg = MMC_CMD62_ARG2;
1562 err = mmc_send_cmd(mmc, &cmd, NULL);
1564 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1567 /* boot partition size is multiple of 128KB */
1568 bootsize = (bootsize * 1024) / 128;
1570 /* Arg: boot partition size */
1571 cmd.cmdidx = MMC_CMD_RES_MAN;
1572 cmd.resp_type = MMC_RSP_R1b;
1573 cmd.cmdarg = bootsize;
1575 err = mmc_send_cmd(mmc, &cmd, NULL);
1577 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1580 /* RPMB partition size is multiple of 128KB */
1581 rpmbsize = (rpmbsize * 1024) / 128;
1582 /* Arg: RPMB partition size */
1583 cmd.cmdidx = MMC_CMD_RES_MAN;
1584 cmd.resp_type = MMC_RSP_R1b;
1585 cmd.cmdarg = rpmbsize;
1587 err = mmc_send_cmd(mmc, &cmd, NULL);
1589 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1596 * This function shall form and send the commands to open / close the
1597 * boot partition specified by user.
1600 * ack: 0x0 - No boot acknowledge sent (default)
1601 * 0x1 - Boot acknowledge sent during boot operation
1602 * part_num: User selects boot data that will be sent to master
1603 * 0x0 - Device not boot enabled (default)
1604 * 0x1 - Boot partition 1 enabled for boot
1605 * 0x2 - Boot partition 2 enabled for boot
1606 * access: User selects partitions to access
1607 * 0x0 : No access to boot partition (default)
1608 * 0x1 : R/W boot partition 1
1609 * 0x2 : R/W boot partition 2
1610 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1612 * Returns 0 on success.
1614 int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1619 /* Boot ack enable, boot partition enable , boot partition access */
1620 cmd.cmdidx = MMC_CMD_SWITCH;
1621 cmd.resp_type = MMC_RSP_R1b;
1623 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1624 (EXT_CSD_PART_CONF << 16) |
1625 ((EXT_CSD_BOOT_ACK(ack) |
1626 EXT_CSD_BOOT_PART_NUM(part_num) |
1627 EXT_CSD_PARTITION_ACCESS(access)) << 8);
1629 err = mmc_send_cmd(mmc, &cmd, NULL);
1632 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1635 debug("mmc boot partition#%d close fail:Error = %d\n",
1642 /* 4bit transfer mode at booting time. */
1643 cmd.cmdidx = MMC_CMD_SWITCH;
1644 cmd.resp_type = MMC_RSP_R1b;
1646 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1647 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1650 err = mmc_send_cmd(mmc, &cmd, NULL);
1652 debug("mmc boot partition#%d open fail:Error2 = %d\n",