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>
18 #include "mmc_private.h"
20 static struct list_head mmc_devices;
21 static int cur_dev_num = -1;
23 int __weak board_mmc_getwp(struct mmc *mmc)
28 int mmc_getwp(struct mmc *mmc)
32 wp = board_mmc_getwp(mmc);
35 if (mmc->cfg->ops->getwp)
36 wp = mmc->cfg->ops->getwp(mmc);
44 int __board_mmc_getcd(struct mmc *mmc) {
48 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
49 alias("__board_mmc_getcd")));
51 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
55 #ifdef CONFIG_MMC_TRACE
59 printf("CMD_SEND:%d\n", cmd->cmdidx);
60 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
61 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
62 switch (cmd->resp_type) {
64 printf("\t\tMMC_RSP_NONE\n");
67 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
71 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
75 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
77 printf("\t\t \t\t 0x%08X \n",
79 printf("\t\t \t\t 0x%08X \n",
81 printf("\t\t \t\t 0x%08X \n",
84 printf("\t\t\t\t\tDUMPING DATA\n");
85 for (i = 0; i < 4; i++) {
87 printf("\t\t\t\t\t%03d - ", i*4);
88 ptr = (u8 *)&cmd->response[i];
90 for (j = 0; j < 4; j++)
91 printf("%02X ", *ptr--);
96 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
100 printf("\t\tERROR MMC rsp not supported\n");
104 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
109 int mmc_send_status(struct mmc *mmc, int timeout)
112 int err, retries = 5;
113 #ifdef CONFIG_MMC_TRACE
117 cmd.cmdidx = MMC_CMD_SEND_STATUS;
118 cmd.resp_type = MMC_RSP_R1;
119 if (!mmc_host_is_spi(mmc))
120 cmd.cmdarg = mmc->rca << 16;
123 err = mmc_send_cmd(mmc, &cmd, NULL);
125 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
126 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
129 else if (cmd.response[0] & MMC_STATUS_MASK) {
130 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
131 printf("Status Error: 0x%08X\n",
136 } else if (--retries < 0)
143 #ifdef CONFIG_MMC_TRACE
144 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
145 printf("CURR STATE:%d\n", status);
148 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
149 printf("Timeout waiting card ready\n");
157 int mmc_set_blocklen(struct mmc *mmc, int len)
161 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
162 cmd.resp_type = MMC_RSP_R1;
165 return mmc_send_cmd(mmc, &cmd, NULL);
168 struct mmc *find_mmc_device(int dev_num)
171 struct list_head *entry;
173 list_for_each(entry, &mmc_devices) {
174 m = list_entry(entry, struct mmc, link);
176 if (m->block_dev.dev == dev_num)
180 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
181 printf("MMC Device %d not found\n", dev_num);
187 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
191 struct mmc_data data;
194 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
196 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
198 if (mmc->high_capacity)
201 cmd.cmdarg = start * mmc->read_bl_len;
203 cmd.resp_type = MMC_RSP_R1;
206 data.blocks = blkcnt;
207 data.blocksize = mmc->read_bl_len;
208 data.flags = MMC_DATA_READ;
210 if (mmc_send_cmd(mmc, &cmd, &data))
214 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
216 cmd.resp_type = MMC_RSP_R1b;
217 if (mmc_send_cmd(mmc, &cmd, NULL)) {
218 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
219 printf("mmc fail to send stop cmd\n");
228 static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
230 lbaint_t cur, blocks_todo = blkcnt;
235 struct mmc *mmc = find_mmc_device(dev_num);
239 if ((start + blkcnt) > mmc->block_dev.lba) {
240 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
241 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
242 start + blkcnt, mmc->block_dev.lba);
247 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
251 cur = (blocks_todo > mmc->cfg->b_max) ?
252 mmc->cfg->b_max : blocks_todo;
253 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
257 dst += cur * mmc->read_bl_len;
258 } while (blocks_todo > 0);
263 static int mmc_go_idle(struct mmc *mmc)
270 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
272 cmd.resp_type = MMC_RSP_NONE;
274 err = mmc_send_cmd(mmc, &cmd, NULL);
284 static int sd_send_op_cond(struct mmc *mmc)
291 cmd.cmdidx = MMC_CMD_APP_CMD;
292 cmd.resp_type = MMC_RSP_R1;
295 err = mmc_send_cmd(mmc, &cmd, NULL);
300 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
301 cmd.resp_type = MMC_RSP_R3;
304 * Most cards do not answer if some reserved bits
305 * in the ocr are set. However, Some controller
306 * can set bit 7 (reserved for low voltages), but
307 * how to manage low voltages SD card is not yet
310 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
311 (mmc->cfg->voltages & 0xff8000);
313 if (mmc->version == SD_VERSION_2)
314 cmd.cmdarg |= OCR_HCS;
316 err = mmc_send_cmd(mmc, &cmd, NULL);
322 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
327 if (mmc->version != SD_VERSION_2)
328 mmc->version = SD_VERSION_1_0;
330 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
331 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
332 cmd.resp_type = MMC_RSP_R3;
335 err = mmc_send_cmd(mmc, &cmd, NULL);
341 mmc->ocr = cmd.response[0];
343 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
349 /* We pass in the cmd since otherwise the init seems to fail */
350 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
355 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
356 cmd->resp_type = MMC_RSP_R3;
358 if (use_arg && !mmc_host_is_spi(mmc)) {
360 (mmc->cfg->voltages &
361 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
362 (mmc->op_cond_response & OCR_ACCESS_MODE);
364 if (mmc->cfg->host_caps & MMC_MODE_HC)
365 cmd->cmdarg |= OCR_HCS;
367 err = mmc_send_cmd(mmc, cmd, NULL);
370 mmc->op_cond_response = cmd->response[0];
374 int mmc_send_op_cond(struct mmc *mmc)
379 /* Some cards seem to need this */
382 /* Asking to the card its capabilities */
383 mmc->op_cond_pending = 1;
384 for (i = 0; i < 2; i++) {
385 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
389 /* exit if not busy (flag seems to be inverted) */
390 if (mmc->op_cond_response & OCR_BUSY)
396 int mmc_complete_op_cond(struct mmc *mmc)
403 mmc->op_cond_pending = 0;
404 start = get_timer(0);
406 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
409 if (get_timer(start) > timeout)
412 } while (!(mmc->op_cond_response & OCR_BUSY));
414 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
415 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
416 cmd.resp_type = MMC_RSP_R3;
419 err = mmc_send_cmd(mmc, &cmd, NULL);
425 mmc->version = MMC_VERSION_UNKNOWN;
426 mmc->ocr = cmd.response[0];
428 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
435 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
438 struct mmc_data data;
441 /* Get the Card Status Register */
442 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
443 cmd.resp_type = MMC_RSP_R1;
446 data.dest = (char *)ext_csd;
448 data.blocksize = MMC_MAX_BLOCK_LEN;
449 data.flags = MMC_DATA_READ;
451 err = mmc_send_cmd(mmc, &cmd, &data);
457 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
463 cmd.cmdidx = MMC_CMD_SWITCH;
464 cmd.resp_type = MMC_RSP_R1b;
465 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
469 ret = mmc_send_cmd(mmc, &cmd, NULL);
471 /* Waiting for the ready status */
473 ret = mmc_send_status(mmc, timeout);
479 static int mmc_change_freq(struct mmc *mmc)
481 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
487 if (mmc_host_is_spi(mmc))
490 /* Only version 4 supports high-speed */
491 if (mmc->version < MMC_VERSION_4)
494 err = mmc_send_ext_csd(mmc, ext_csd);
499 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
501 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
506 /* Now check to see that it worked */
507 err = mmc_send_ext_csd(mmc, ext_csd);
512 /* No high-speed support */
513 if (!ext_csd[EXT_CSD_HS_TIMING])
516 /* High Speed is set, there are two types: 52MHz and 26MHz */
517 if (cardtype & MMC_HS_52MHZ)
518 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
520 mmc->card_caps |= MMC_MODE_HS;
525 static int mmc_set_capacity(struct mmc *mmc, int part_num)
529 mmc->capacity = mmc->capacity_user;
533 mmc->capacity = mmc->capacity_boot;
536 mmc->capacity = mmc->capacity_rpmb;
542 mmc->capacity = mmc->capacity_gp[part_num - 4];
548 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
553 int mmc_switch_part(int dev_num, unsigned int part_num)
555 struct mmc *mmc = find_mmc_device(dev_num);
561 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
562 (mmc->part_config & ~PART_ACCESS_MASK)
563 | (part_num & PART_ACCESS_MASK));
567 return mmc_set_capacity(mmc, part_num);
570 int mmc_getcd(struct mmc *mmc)
574 cd = board_mmc_getcd(mmc);
577 if (mmc->cfg->ops->getcd)
578 cd = mmc->cfg->ops->getcd(mmc);
586 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
589 struct mmc_data data;
591 /* Switch the frequency */
592 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
593 cmd.resp_type = MMC_RSP_R1;
594 cmd.cmdarg = (mode << 31) | 0xffffff;
595 cmd.cmdarg &= ~(0xf << (group * 4));
596 cmd.cmdarg |= value << (group * 4);
598 data.dest = (char *)resp;
601 data.flags = MMC_DATA_READ;
603 return mmc_send_cmd(mmc, &cmd, &data);
607 static int sd_change_freq(struct mmc *mmc)
611 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
612 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
613 struct mmc_data data;
618 if (mmc_host_is_spi(mmc))
621 /* Read the SCR to find out if this card supports higher speeds */
622 cmd.cmdidx = MMC_CMD_APP_CMD;
623 cmd.resp_type = MMC_RSP_R1;
624 cmd.cmdarg = mmc->rca << 16;
626 err = mmc_send_cmd(mmc, &cmd, NULL);
631 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
632 cmd.resp_type = MMC_RSP_R1;
638 data.dest = (char *)scr;
641 data.flags = MMC_DATA_READ;
643 err = mmc_send_cmd(mmc, &cmd, &data);
652 mmc->scr[0] = __be32_to_cpu(scr[0]);
653 mmc->scr[1] = __be32_to_cpu(scr[1]);
655 switch ((mmc->scr[0] >> 24) & 0xf) {
657 mmc->version = SD_VERSION_1_0;
660 mmc->version = SD_VERSION_1_10;
663 mmc->version = SD_VERSION_2;
664 if ((mmc->scr[0] >> 15) & 0x1)
665 mmc->version = SD_VERSION_3;
668 mmc->version = SD_VERSION_1_0;
672 if (mmc->scr[0] & SD_DATA_4BIT)
673 mmc->card_caps |= MMC_MODE_4BIT;
675 /* Version 1.0 doesn't support switching */
676 if (mmc->version == SD_VERSION_1_0)
681 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
682 (u8 *)switch_status);
687 /* The high-speed function is busy. Try again */
688 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
692 /* If high-speed isn't supported, we return */
693 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
697 * If the host doesn't support SD_HIGHSPEED, do not switch card to
698 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
699 * This can avoid furthur problem when the card runs in different
700 * mode between the host.
702 if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
703 (mmc->cfg->host_caps & MMC_MODE_HS)))
706 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
711 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
712 mmc->card_caps |= MMC_MODE_HS;
717 /* frequency bases */
718 /* divided by 10 to be nice to platforms without floating point */
719 static const int fbase[] = {
726 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
727 * to platforms without floating point.
729 static const int multipliers[] = {
748 static void mmc_set_ios(struct mmc *mmc)
750 if (mmc->cfg->ops->set_ios)
751 mmc->cfg->ops->set_ios(mmc);
754 void mmc_set_clock(struct mmc *mmc, uint clock)
756 if (clock > mmc->cfg->f_max)
757 clock = mmc->cfg->f_max;
759 if (clock < mmc->cfg->f_min)
760 clock = mmc->cfg->f_min;
767 static void mmc_set_bus_width(struct mmc *mmc, uint width)
769 mmc->bus_width = width;
774 static int mmc_startup(struct mmc *mmc)
778 u64 cmult, csize, capacity;
780 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
781 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
784 #ifdef CONFIG_MMC_SPI_CRC_ON
785 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
786 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
787 cmd.resp_type = MMC_RSP_R1;
789 err = mmc_send_cmd(mmc, &cmd, NULL);
796 /* Put the Card in Identify Mode */
797 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
798 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
799 cmd.resp_type = MMC_RSP_R2;
802 err = mmc_send_cmd(mmc, &cmd, NULL);
807 memcpy(mmc->cid, cmd.response, 16);
810 * For MMC cards, set the Relative Address.
811 * For SD cards, get the Relatvie Address.
812 * This also puts the cards into Standby State
814 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
815 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
816 cmd.cmdarg = mmc->rca << 16;
817 cmd.resp_type = MMC_RSP_R6;
819 err = mmc_send_cmd(mmc, &cmd, NULL);
825 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
828 /* Get the Card-Specific Data */
829 cmd.cmdidx = MMC_CMD_SEND_CSD;
830 cmd.resp_type = MMC_RSP_R2;
831 cmd.cmdarg = mmc->rca << 16;
833 err = mmc_send_cmd(mmc, &cmd, NULL);
835 /* Waiting for the ready status */
836 mmc_send_status(mmc, timeout);
841 mmc->csd[0] = cmd.response[0];
842 mmc->csd[1] = cmd.response[1];
843 mmc->csd[2] = cmd.response[2];
844 mmc->csd[3] = cmd.response[3];
846 if (mmc->version == MMC_VERSION_UNKNOWN) {
847 int version = (cmd.response[0] >> 26) & 0xf;
851 mmc->version = MMC_VERSION_1_2;
854 mmc->version = MMC_VERSION_1_4;
857 mmc->version = MMC_VERSION_2_2;
860 mmc->version = MMC_VERSION_3;
863 mmc->version = MMC_VERSION_4;
866 mmc->version = MMC_VERSION_1_2;
871 /* divide frequency by 10, since the mults are 10x bigger */
872 freq = fbase[(cmd.response[0] & 0x7)];
873 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
875 mmc->tran_speed = freq * mult;
877 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
878 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
881 mmc->write_bl_len = mmc->read_bl_len;
883 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
885 if (mmc->high_capacity) {
886 csize = (mmc->csd[1] & 0x3f) << 16
887 | (mmc->csd[2] & 0xffff0000) >> 16;
890 csize = (mmc->csd[1] & 0x3ff) << 2
891 | (mmc->csd[2] & 0xc0000000) >> 30;
892 cmult = (mmc->csd[2] & 0x00038000) >> 15;
895 mmc->capacity_user = (csize + 1) << (cmult + 2);
896 mmc->capacity_user *= mmc->read_bl_len;
897 mmc->capacity_boot = 0;
898 mmc->capacity_rpmb = 0;
899 for (i = 0; i < 4; i++)
900 mmc->capacity_gp[i] = 0;
902 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
903 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
905 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
906 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
908 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
909 cmd.cmdidx = MMC_CMD_SET_DSR;
910 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
911 cmd.resp_type = MMC_RSP_NONE;
912 if (mmc_send_cmd(mmc, &cmd, NULL))
913 printf("MMC: SET_DSR failed\n");
916 /* Select the card, and put it into Transfer Mode */
917 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
918 cmd.cmdidx = MMC_CMD_SELECT_CARD;
919 cmd.resp_type = MMC_RSP_R1;
920 cmd.cmdarg = mmc->rca << 16;
921 err = mmc_send_cmd(mmc, &cmd, NULL);
928 * For SD, its erase group is always one sector
930 mmc->erase_grp_size = 1;
931 mmc->part_config = MMCPART_NOAVAILABLE;
932 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
933 /* check ext_csd version and capacity */
934 err = mmc_send_ext_csd(mmc, ext_csd);
935 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
937 * According to the JEDEC Standard, the value of
938 * ext_csd's capacity is valid if the value is more
941 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
942 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
943 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
944 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
945 capacity *= MMC_MAX_BLOCK_LEN;
946 if ((capacity >> 20) > 2 * 1024)
947 mmc->capacity_user = capacity;
950 switch (ext_csd[EXT_CSD_REV]) {
952 mmc->version = MMC_VERSION_4_1;
955 mmc->version = MMC_VERSION_4_2;
958 mmc->version = MMC_VERSION_4_3;
961 mmc->version = MMC_VERSION_4_41;
964 mmc->version = MMC_VERSION_4_5;
969 * Host needs to enable ERASE_GRP_DEF bit if device is
970 * partitioned. This bit will be lost every time after a reset
971 * or power off. This will affect erase size.
973 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
974 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
975 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
976 EXT_CSD_ERASE_GROUP_DEF, 1);
981 /* Read out group size from ext_csd */
982 mmc->erase_grp_size =
983 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
984 MMC_MAX_BLOCK_LEN * 1024;
986 /* Calculate the group size from the csd value. */
987 int erase_gsz, erase_gmul;
988 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
989 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
990 mmc->erase_grp_size = (erase_gsz + 1)
994 /* store the partition info of emmc */
995 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
996 ext_csd[EXT_CSD_BOOT_MULT])
997 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
999 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1001 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1003 for (i = 0; i < 4; i++) {
1004 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1005 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1006 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1007 mmc->capacity_gp[i] *=
1008 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1009 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1013 err = mmc_set_capacity(mmc, mmc->part_num);
1018 err = sd_change_freq(mmc);
1020 err = mmc_change_freq(mmc);
1025 /* Restrict card's capabilities by what the host can do */
1026 mmc->card_caps &= mmc->cfg->host_caps;
1029 if (mmc->card_caps & MMC_MODE_4BIT) {
1030 cmd.cmdidx = MMC_CMD_APP_CMD;
1031 cmd.resp_type = MMC_RSP_R1;
1032 cmd.cmdarg = mmc->rca << 16;
1034 err = mmc_send_cmd(mmc, &cmd, NULL);
1038 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1039 cmd.resp_type = MMC_RSP_R1;
1041 err = mmc_send_cmd(mmc, &cmd, NULL);
1045 mmc_set_bus_width(mmc, 4);
1048 if (mmc->card_caps & MMC_MODE_HS)
1049 mmc->tran_speed = 50000000;
1051 mmc->tran_speed = 25000000;
1055 /* An array of possible bus widths in order of preference */
1056 static unsigned ext_csd_bits[] = {
1057 EXT_CSD_BUS_WIDTH_8,
1058 EXT_CSD_BUS_WIDTH_4,
1059 EXT_CSD_BUS_WIDTH_1,
1062 /* An array to map CSD bus widths to host cap bits */
1063 static unsigned ext_to_hostcaps[] = {
1064 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1065 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1068 /* An array to map chosen bus width to an integer */
1069 static unsigned widths[] = {
1073 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1074 unsigned int extw = ext_csd_bits[idx];
1077 * Check to make sure the controller supports
1078 * this bus width, if it's more than 1
1080 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1081 !(mmc->cfg->host_caps & ext_to_hostcaps[extw]))
1084 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1085 EXT_CSD_BUS_WIDTH, extw);
1090 mmc_set_bus_width(mmc, widths[idx]);
1092 err = mmc_send_ext_csd(mmc, test_csd);
1093 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1094 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1095 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1096 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1097 && ext_csd[EXT_CSD_REV] \
1098 == test_csd[EXT_CSD_REV]
1099 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1100 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1101 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1102 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1104 mmc->card_caps |= ext_to_hostcaps[extw];
1109 if (mmc->card_caps & MMC_MODE_HS) {
1110 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1111 mmc->tran_speed = 52000000;
1113 mmc->tran_speed = 26000000;
1117 mmc_set_clock(mmc, mmc->tran_speed);
1119 /* fill in device description */
1120 mmc->block_dev.lun = 0;
1121 mmc->block_dev.type = 0;
1122 mmc->block_dev.blksz = mmc->read_bl_len;
1123 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1124 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1125 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1126 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1127 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1128 (mmc->cid[3] >> 16) & 0xffff);
1129 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1130 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1131 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1132 (mmc->cid[2] >> 24) & 0xff);
1133 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1134 (mmc->cid[2] >> 16) & 0xf);
1136 mmc->block_dev.vendor[0] = 0;
1137 mmc->block_dev.product[0] = 0;
1138 mmc->block_dev.revision[0] = 0;
1140 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1141 init_part(&mmc->block_dev);
1147 static int mmc_send_if_cond(struct mmc *mmc)
1152 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1153 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1154 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1155 cmd.resp_type = MMC_RSP_R7;
1157 err = mmc_send_cmd(mmc, &cmd, NULL);
1162 if ((cmd.response[0] & 0xff) != 0xaa)
1163 return UNUSABLE_ERR;
1165 mmc->version = SD_VERSION_2;
1170 /* not used any more */
1171 int __deprecated mmc_register(struct mmc *mmc)
1173 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1174 printf("%s is deprecated! use mmc_create() instead.\n", __func__);
1179 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
1183 /* quick validation */
1184 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
1185 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
1188 mmc = calloc(1, sizeof(*mmc));
1195 /* the following chunk was mmc_register() */
1197 /* Setup dsr related values */
1199 mmc->dsr = 0xffffffff;
1200 /* Setup the universal parts of the block interface just once */
1201 mmc->block_dev.if_type = IF_TYPE_MMC;
1202 mmc->block_dev.dev = cur_dev_num++;
1203 mmc->block_dev.removable = 1;
1204 mmc->block_dev.block_read = mmc_bread;
1205 mmc->block_dev.block_write = mmc_bwrite;
1206 mmc->block_dev.block_erase = mmc_berase;
1208 /* setup initial part type */
1209 mmc->block_dev.part_type = mmc->cfg->part_type;
1211 INIT_LIST_HEAD(&mmc->link);
1213 list_add_tail(&mmc->link, &mmc_devices);
1218 void mmc_destroy(struct mmc *mmc)
1220 /* only freeing memory for now */
1224 #ifdef CONFIG_PARTITIONS
1225 block_dev_desc_t *mmc_get_dev(int dev)
1227 struct mmc *mmc = find_mmc_device(dev);
1228 if (!mmc || mmc_init(mmc))
1231 return &mmc->block_dev;
1235 int mmc_start_init(struct mmc *mmc)
1239 /* we pretend there's no card when init is NULL */
1240 if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
1242 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1243 printf("MMC: no card present\n");
1251 /* made sure it's not NULL earlier */
1252 err = mmc->cfg->ops->init(mmc);
1257 mmc_set_bus_width(mmc, 1);
1258 mmc_set_clock(mmc, 1);
1260 /* Reset the Card */
1261 err = mmc_go_idle(mmc);
1266 /* The internal partition reset to user partition(0) at every CMD0*/
1269 /* Test for SD version 2 */
1270 err = mmc_send_if_cond(mmc);
1272 /* Now try to get the SD card's operating condition */
1273 err = sd_send_op_cond(mmc);
1275 /* If the command timed out, we check for an MMC card */
1276 if (err == TIMEOUT) {
1277 err = mmc_send_op_cond(mmc);
1279 if (err && err != IN_PROGRESS) {
1280 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1281 printf("Card did not respond to voltage select!\n");
1283 return UNUSABLE_ERR;
1287 if (err == IN_PROGRESS)
1288 mmc->init_in_progress = 1;
1293 static int mmc_complete_init(struct mmc *mmc)
1297 if (mmc->op_cond_pending)
1298 err = mmc_complete_op_cond(mmc);
1301 err = mmc_startup(mmc);
1306 mmc->init_in_progress = 0;
1310 int mmc_init(struct mmc *mmc)
1312 int err = IN_PROGRESS;
1318 start = get_timer(0);
1320 if (!mmc->init_in_progress)
1321 err = mmc_start_init(mmc);
1323 if (!err || err == IN_PROGRESS)
1324 err = mmc_complete_init(mmc);
1325 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1329 int mmc_set_dsr(struct mmc *mmc, u16 val)
1336 * CPU and board-specific MMC initializations. Aliased function
1337 * signals caller to move on
1339 static int __def_mmc_init(bd_t *bis)
1344 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1345 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1347 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1349 void print_mmc_devices(char separator)
1352 struct list_head *entry;
1354 list_for_each(entry, &mmc_devices) {
1355 m = list_entry(entry, struct mmc, link);
1357 printf("%s: %d", m->cfg->name, m->block_dev.dev);
1359 if (entry->next != &mmc_devices)
1360 printf("%c ", separator);
1367 void print_mmc_devices(char separator) { }
1370 int get_mmc_num(void)
1375 void mmc_set_preinit(struct mmc *mmc, int preinit)
1377 mmc->preinit = preinit;
1380 static void do_preinit(void)
1383 struct list_head *entry;
1385 list_for_each(entry, &mmc_devices) {
1386 m = list_entry(entry, struct mmc, link);
1394 int mmc_initialize(bd_t *bis)
1396 INIT_LIST_HEAD (&mmc_devices);
1399 if (board_mmc_init(bis) < 0)
1402 #ifndef CONFIG_SPL_BUILD
1403 print_mmc_devices(',');
1410 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1412 * This function changes the size of boot partition and the size of rpmb
1413 * partition present on EMMC devices.
1416 * struct *mmc: pointer for the mmc device strcuture
1417 * bootsize: size of boot partition
1418 * rpmbsize: size of rpmb partition
1420 * Returns 0 on success.
1423 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1424 unsigned long rpmbsize)
1429 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1430 cmd.cmdidx = MMC_CMD_RES_MAN;
1431 cmd.resp_type = MMC_RSP_R1b;
1432 cmd.cmdarg = MMC_CMD62_ARG1;
1434 err = mmc_send_cmd(mmc, &cmd, NULL);
1436 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1440 /* Boot partition changing mode */
1441 cmd.cmdidx = MMC_CMD_RES_MAN;
1442 cmd.resp_type = MMC_RSP_R1b;
1443 cmd.cmdarg = MMC_CMD62_ARG2;
1445 err = mmc_send_cmd(mmc, &cmd, NULL);
1447 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1450 /* boot partition size is multiple of 128KB */
1451 bootsize = (bootsize * 1024) / 128;
1453 /* Arg: boot partition size */
1454 cmd.cmdidx = MMC_CMD_RES_MAN;
1455 cmd.resp_type = MMC_RSP_R1b;
1456 cmd.cmdarg = bootsize;
1458 err = mmc_send_cmd(mmc, &cmd, NULL);
1460 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1463 /* RPMB partition size is multiple of 128KB */
1464 rpmbsize = (rpmbsize * 1024) / 128;
1465 /* Arg: RPMB partition size */
1466 cmd.cmdidx = MMC_CMD_RES_MAN;
1467 cmd.resp_type = MMC_RSP_R1b;
1468 cmd.cmdarg = rpmbsize;
1470 err = mmc_send_cmd(mmc, &cmd, NULL);
1472 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1479 * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1480 * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1483 * Returns 0 on success.
1485 int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
1489 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
1490 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
1491 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
1492 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
1500 * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1501 * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1504 * Returns 0 on success.
1506 int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1510 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1511 EXT_CSD_BOOT_ACK(ack) |
1512 EXT_CSD_BOOT_PART_NUM(part_num) |
1513 EXT_CSD_PARTITION_ACCESS(access));
1521 * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1522 * for enable. Note that this is a write-once field for non-zero values.
1524 * Returns 0 on success.
1526 int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
1528 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,