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 if (mmc->card_caps & MMC_MODE_DDR_52MHz)
164 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
165 cmd.resp_type = MMC_RSP_R1;
168 return mmc_send_cmd(mmc, &cmd, NULL);
171 struct mmc *find_mmc_device(int dev_num)
174 struct list_head *entry;
176 list_for_each(entry, &mmc_devices) {
177 m = list_entry(entry, struct mmc, link);
179 if (m->block_dev.dev == dev_num)
183 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
184 printf("MMC Device %d not found\n", dev_num);
190 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
194 struct mmc_data data;
197 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
199 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
201 if (mmc->high_capacity)
204 cmd.cmdarg = start * mmc->read_bl_len;
206 cmd.resp_type = MMC_RSP_R1;
209 data.blocks = blkcnt;
210 data.blocksize = mmc->read_bl_len;
211 data.flags = MMC_DATA_READ;
213 if (mmc_send_cmd(mmc, &cmd, &data))
217 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
219 cmd.resp_type = MMC_RSP_R1b;
220 if (mmc_send_cmd(mmc, &cmd, NULL)) {
221 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
222 printf("mmc fail to send stop cmd\n");
231 static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
233 lbaint_t cur, blocks_todo = blkcnt;
238 struct mmc *mmc = find_mmc_device(dev_num);
242 if ((start + blkcnt) > mmc->block_dev.lba) {
243 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
244 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
245 start + blkcnt, mmc->block_dev.lba);
250 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
254 cur = (blocks_todo > mmc->cfg->b_max) ?
255 mmc->cfg->b_max : blocks_todo;
256 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
260 dst += cur * mmc->read_bl_len;
261 } while (blocks_todo > 0);
266 static int mmc_go_idle(struct mmc *mmc)
273 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
275 cmd.resp_type = MMC_RSP_NONE;
277 err = mmc_send_cmd(mmc, &cmd, NULL);
287 static int sd_send_op_cond(struct mmc *mmc)
294 cmd.cmdidx = MMC_CMD_APP_CMD;
295 cmd.resp_type = MMC_RSP_R1;
298 err = mmc_send_cmd(mmc, &cmd, NULL);
303 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
304 cmd.resp_type = MMC_RSP_R3;
307 * Most cards do not answer if some reserved bits
308 * in the ocr are set. However, Some controller
309 * can set bit 7 (reserved for low voltages), but
310 * how to manage low voltages SD card is not yet
313 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
314 (mmc->cfg->voltages & 0xff8000);
316 if (mmc->version == SD_VERSION_2)
317 cmd.cmdarg |= OCR_HCS;
319 err = mmc_send_cmd(mmc, &cmd, NULL);
325 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
330 if (mmc->version != SD_VERSION_2)
331 mmc->version = SD_VERSION_1_0;
333 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
334 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
335 cmd.resp_type = MMC_RSP_R3;
338 err = mmc_send_cmd(mmc, &cmd, NULL);
344 mmc->ocr = cmd.response[0];
346 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
352 /* We pass in the cmd since otherwise the init seems to fail */
353 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
358 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
359 cmd->resp_type = MMC_RSP_R3;
361 if (use_arg && !mmc_host_is_spi(mmc)) {
363 (mmc->cfg->voltages &
364 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
365 (mmc->op_cond_response & OCR_ACCESS_MODE);
367 if (mmc->cfg->host_caps & MMC_MODE_HC)
368 cmd->cmdarg |= OCR_HCS;
370 err = mmc_send_cmd(mmc, cmd, NULL);
373 mmc->op_cond_response = cmd->response[0];
377 int mmc_send_op_cond(struct mmc *mmc)
382 /* Some cards seem to need this */
385 /* Asking to the card its capabilities */
386 mmc->op_cond_pending = 1;
387 for (i = 0; i < 2; i++) {
388 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
392 /* exit if not busy (flag seems to be inverted) */
393 if (mmc->op_cond_response & OCR_BUSY)
399 int mmc_complete_op_cond(struct mmc *mmc)
406 mmc->op_cond_pending = 0;
407 start = get_timer(0);
409 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
412 if (get_timer(start) > timeout)
415 } while (!(mmc->op_cond_response & OCR_BUSY));
417 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
418 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
419 cmd.resp_type = MMC_RSP_R3;
422 err = mmc_send_cmd(mmc, &cmd, NULL);
428 mmc->version = MMC_VERSION_UNKNOWN;
429 mmc->ocr = cmd.response[0];
431 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
438 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
441 struct mmc_data data;
444 /* Get the Card Status Register */
445 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
446 cmd.resp_type = MMC_RSP_R1;
449 data.dest = (char *)ext_csd;
451 data.blocksize = MMC_MAX_BLOCK_LEN;
452 data.flags = MMC_DATA_READ;
454 err = mmc_send_cmd(mmc, &cmd, &data);
460 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
466 cmd.cmdidx = MMC_CMD_SWITCH;
467 cmd.resp_type = MMC_RSP_R1b;
468 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
472 ret = mmc_send_cmd(mmc, &cmd, NULL);
474 /* Waiting for the ready status */
476 ret = mmc_send_status(mmc, timeout);
482 static int mmc_change_freq(struct mmc *mmc)
484 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
490 if (mmc_host_is_spi(mmc))
493 /* Only version 4 supports high-speed */
494 if (mmc->version < MMC_VERSION_4)
497 err = mmc_send_ext_csd(mmc, ext_csd);
502 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
504 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
509 /* Now check to see that it worked */
510 err = mmc_send_ext_csd(mmc, ext_csd);
515 /* No high-speed support */
516 if (!ext_csd[EXT_CSD_HS_TIMING])
519 /* High Speed is set, there are two types: 52MHz and 26MHz */
520 if (cardtype & EXT_CSD_CARD_TYPE_52) {
521 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
522 mmc->card_caps |= MMC_MODE_DDR_52MHz;
523 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
525 mmc->card_caps |= MMC_MODE_HS;
531 static int mmc_set_capacity(struct mmc *mmc, int part_num)
535 mmc->capacity = mmc->capacity_user;
539 mmc->capacity = mmc->capacity_boot;
542 mmc->capacity = mmc->capacity_rpmb;
548 mmc->capacity = mmc->capacity_gp[part_num - 4];
554 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
559 int mmc_switch_part(int dev_num, unsigned int part_num)
561 struct mmc *mmc = find_mmc_device(dev_num);
567 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
568 (mmc->part_config & ~PART_ACCESS_MASK)
569 | (part_num & PART_ACCESS_MASK));
573 return mmc_set_capacity(mmc, part_num);
576 int mmc_getcd(struct mmc *mmc)
580 cd = board_mmc_getcd(mmc);
583 if (mmc->cfg->ops->getcd)
584 cd = mmc->cfg->ops->getcd(mmc);
592 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
595 struct mmc_data data;
597 /* Switch the frequency */
598 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
599 cmd.resp_type = MMC_RSP_R1;
600 cmd.cmdarg = (mode << 31) | 0xffffff;
601 cmd.cmdarg &= ~(0xf << (group * 4));
602 cmd.cmdarg |= value << (group * 4);
604 data.dest = (char *)resp;
607 data.flags = MMC_DATA_READ;
609 return mmc_send_cmd(mmc, &cmd, &data);
613 static int sd_change_freq(struct mmc *mmc)
617 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
618 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
619 struct mmc_data data;
624 if (mmc_host_is_spi(mmc))
627 /* Read the SCR to find out if this card supports higher speeds */
628 cmd.cmdidx = MMC_CMD_APP_CMD;
629 cmd.resp_type = MMC_RSP_R1;
630 cmd.cmdarg = mmc->rca << 16;
632 err = mmc_send_cmd(mmc, &cmd, NULL);
637 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
638 cmd.resp_type = MMC_RSP_R1;
644 data.dest = (char *)scr;
647 data.flags = MMC_DATA_READ;
649 err = mmc_send_cmd(mmc, &cmd, &data);
658 mmc->scr[0] = __be32_to_cpu(scr[0]);
659 mmc->scr[1] = __be32_to_cpu(scr[1]);
661 switch ((mmc->scr[0] >> 24) & 0xf) {
663 mmc->version = SD_VERSION_1_0;
666 mmc->version = SD_VERSION_1_10;
669 mmc->version = SD_VERSION_2;
670 if ((mmc->scr[0] >> 15) & 0x1)
671 mmc->version = SD_VERSION_3;
674 mmc->version = SD_VERSION_1_0;
678 if (mmc->scr[0] & SD_DATA_4BIT)
679 mmc->card_caps |= MMC_MODE_4BIT;
681 /* Version 1.0 doesn't support switching */
682 if (mmc->version == SD_VERSION_1_0)
687 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
688 (u8 *)switch_status);
693 /* The high-speed function is busy. Try again */
694 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
698 /* If high-speed isn't supported, we return */
699 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
703 * If the host doesn't support SD_HIGHSPEED, do not switch card to
704 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
705 * This can avoid furthur problem when the card runs in different
706 * mode between the host.
708 if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
709 (mmc->cfg->host_caps & MMC_MODE_HS)))
712 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
717 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
718 mmc->card_caps |= MMC_MODE_HS;
723 /* frequency bases */
724 /* divided by 10 to be nice to platforms without floating point */
725 static const int fbase[] = {
732 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
733 * to platforms without floating point.
735 static const int multipliers[] = {
754 static void mmc_set_ios(struct mmc *mmc)
756 if (mmc->cfg->ops->set_ios)
757 mmc->cfg->ops->set_ios(mmc);
760 void mmc_set_clock(struct mmc *mmc, uint clock)
762 if (clock > mmc->cfg->f_max)
763 clock = mmc->cfg->f_max;
765 if (clock < mmc->cfg->f_min)
766 clock = mmc->cfg->f_min;
773 static void mmc_set_bus_width(struct mmc *mmc, uint width)
775 mmc->bus_width = width;
780 static int mmc_startup(struct mmc *mmc)
784 u64 cmult, csize, capacity;
786 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
787 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
790 #ifdef CONFIG_MMC_SPI_CRC_ON
791 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
792 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
793 cmd.resp_type = MMC_RSP_R1;
795 err = mmc_send_cmd(mmc, &cmd, NULL);
802 /* Put the Card in Identify Mode */
803 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
804 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
805 cmd.resp_type = MMC_RSP_R2;
808 err = mmc_send_cmd(mmc, &cmd, NULL);
813 memcpy(mmc->cid, cmd.response, 16);
816 * For MMC cards, set the Relative Address.
817 * For SD cards, get the Relatvie Address.
818 * This also puts the cards into Standby State
820 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
821 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
822 cmd.cmdarg = mmc->rca << 16;
823 cmd.resp_type = MMC_RSP_R6;
825 err = mmc_send_cmd(mmc, &cmd, NULL);
831 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
834 /* Get the Card-Specific Data */
835 cmd.cmdidx = MMC_CMD_SEND_CSD;
836 cmd.resp_type = MMC_RSP_R2;
837 cmd.cmdarg = mmc->rca << 16;
839 err = mmc_send_cmd(mmc, &cmd, NULL);
841 /* Waiting for the ready status */
842 mmc_send_status(mmc, timeout);
847 mmc->csd[0] = cmd.response[0];
848 mmc->csd[1] = cmd.response[1];
849 mmc->csd[2] = cmd.response[2];
850 mmc->csd[3] = cmd.response[3];
852 if (mmc->version == MMC_VERSION_UNKNOWN) {
853 int version = (cmd.response[0] >> 26) & 0xf;
857 mmc->version = MMC_VERSION_1_2;
860 mmc->version = MMC_VERSION_1_4;
863 mmc->version = MMC_VERSION_2_2;
866 mmc->version = MMC_VERSION_3;
869 mmc->version = MMC_VERSION_4;
872 mmc->version = MMC_VERSION_1_2;
877 /* divide frequency by 10, since the mults are 10x bigger */
878 freq = fbase[(cmd.response[0] & 0x7)];
879 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
881 mmc->tran_speed = freq * mult;
883 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
884 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
887 mmc->write_bl_len = mmc->read_bl_len;
889 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
891 if (mmc->high_capacity) {
892 csize = (mmc->csd[1] & 0x3f) << 16
893 | (mmc->csd[2] & 0xffff0000) >> 16;
896 csize = (mmc->csd[1] & 0x3ff) << 2
897 | (mmc->csd[2] & 0xc0000000) >> 30;
898 cmult = (mmc->csd[2] & 0x00038000) >> 15;
901 mmc->capacity_user = (csize + 1) << (cmult + 2);
902 mmc->capacity_user *= mmc->read_bl_len;
903 mmc->capacity_boot = 0;
904 mmc->capacity_rpmb = 0;
905 for (i = 0; i < 4; i++)
906 mmc->capacity_gp[i] = 0;
908 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
909 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
911 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
912 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
914 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
915 cmd.cmdidx = MMC_CMD_SET_DSR;
916 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
917 cmd.resp_type = MMC_RSP_NONE;
918 if (mmc_send_cmd(mmc, &cmd, NULL))
919 printf("MMC: SET_DSR failed\n");
922 /* Select the card, and put it into Transfer Mode */
923 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
924 cmd.cmdidx = MMC_CMD_SELECT_CARD;
925 cmd.resp_type = MMC_RSP_R1;
926 cmd.cmdarg = mmc->rca << 16;
927 err = mmc_send_cmd(mmc, &cmd, NULL);
934 * For SD, its erase group is always one sector
936 mmc->erase_grp_size = 1;
937 mmc->part_config = MMCPART_NOAVAILABLE;
938 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
939 /* check ext_csd version and capacity */
940 err = mmc_send_ext_csd(mmc, ext_csd);
941 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
943 * According to the JEDEC Standard, the value of
944 * ext_csd's capacity is valid if the value is more
947 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
948 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
949 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
950 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
951 capacity *= MMC_MAX_BLOCK_LEN;
952 if ((capacity >> 20) > 2 * 1024)
953 mmc->capacity_user = capacity;
956 switch (ext_csd[EXT_CSD_REV]) {
958 mmc->version = MMC_VERSION_4_1;
961 mmc->version = MMC_VERSION_4_2;
964 mmc->version = MMC_VERSION_4_3;
967 mmc->version = MMC_VERSION_4_41;
970 mmc->version = MMC_VERSION_4_5;
975 * Host needs to enable ERASE_GRP_DEF bit if device is
976 * partitioned. This bit will be lost every time after a reset
977 * or power off. This will affect erase size.
979 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
980 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
981 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
982 EXT_CSD_ERASE_GROUP_DEF, 1);
987 /* Read out group size from ext_csd */
988 mmc->erase_grp_size =
989 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
990 MMC_MAX_BLOCK_LEN * 1024;
992 /* Calculate the group size from the csd value. */
993 int erase_gsz, erase_gmul;
994 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
995 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
996 mmc->erase_grp_size = (erase_gsz + 1)
1000 /* store the partition info of emmc */
1001 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1002 ext_csd[EXT_CSD_BOOT_MULT])
1003 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1005 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1007 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1009 for (i = 0; i < 4; i++) {
1010 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1011 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1012 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1013 mmc->capacity_gp[i] *=
1014 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1015 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1019 err = mmc_set_capacity(mmc, mmc->part_num);
1024 err = sd_change_freq(mmc);
1026 err = mmc_change_freq(mmc);
1031 /* Restrict card's capabilities by what the host can do */
1032 mmc->card_caps &= mmc->cfg->host_caps;
1035 if (mmc->card_caps & MMC_MODE_4BIT) {
1036 cmd.cmdidx = MMC_CMD_APP_CMD;
1037 cmd.resp_type = MMC_RSP_R1;
1038 cmd.cmdarg = mmc->rca << 16;
1040 err = mmc_send_cmd(mmc, &cmd, NULL);
1044 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1045 cmd.resp_type = MMC_RSP_R1;
1047 err = mmc_send_cmd(mmc, &cmd, NULL);
1051 mmc_set_bus_width(mmc, 4);
1054 if (mmc->card_caps & MMC_MODE_HS)
1055 mmc->tran_speed = 50000000;
1057 mmc->tran_speed = 25000000;
1061 /* An array of possible bus widths in order of preference */
1062 static unsigned ext_csd_bits[] = {
1063 EXT_CSD_DDR_BUS_WIDTH_8,
1064 EXT_CSD_DDR_BUS_WIDTH_4,
1065 EXT_CSD_BUS_WIDTH_8,
1066 EXT_CSD_BUS_WIDTH_4,
1067 EXT_CSD_BUS_WIDTH_1,
1070 /* An array to map CSD bus widths to host cap bits */
1071 static unsigned ext_to_hostcaps[] = {
1072 [EXT_CSD_DDR_BUS_WIDTH_4] = MMC_MODE_DDR_52MHz,
1073 [EXT_CSD_DDR_BUS_WIDTH_8] = MMC_MODE_DDR_52MHz,
1074 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1075 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1078 /* An array to map chosen bus width to an integer */
1079 static unsigned widths[] = {
1083 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1084 unsigned int extw = ext_csd_bits[idx];
1087 * Check to make sure the controller supports
1088 * this bus width, if it's more than 1
1090 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1091 !(mmc->cfg->host_caps & ext_to_hostcaps[extw]))
1094 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1095 EXT_CSD_BUS_WIDTH, extw);
1100 mmc_set_bus_width(mmc, widths[idx]);
1102 err = mmc_send_ext_csd(mmc, test_csd);
1103 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1104 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1105 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1106 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1107 && ext_csd[EXT_CSD_REV] \
1108 == test_csd[EXT_CSD_REV]
1109 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1110 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1111 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1112 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1114 mmc->card_caps |= ext_to_hostcaps[extw];
1119 if (mmc->card_caps & MMC_MODE_HS) {
1120 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1121 mmc->tran_speed = 52000000;
1123 mmc->tran_speed = 26000000;
1127 mmc_set_clock(mmc, mmc->tran_speed);
1129 /* fill in device description */
1130 mmc->block_dev.lun = 0;
1131 mmc->block_dev.type = 0;
1132 mmc->block_dev.blksz = mmc->read_bl_len;
1133 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1134 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1135 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1136 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1137 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1138 (mmc->cid[3] >> 16) & 0xffff);
1139 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1140 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1141 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1142 (mmc->cid[2] >> 24) & 0xff);
1143 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1144 (mmc->cid[2] >> 16) & 0xf);
1146 mmc->block_dev.vendor[0] = 0;
1147 mmc->block_dev.product[0] = 0;
1148 mmc->block_dev.revision[0] = 0;
1150 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1151 init_part(&mmc->block_dev);
1157 static int mmc_send_if_cond(struct mmc *mmc)
1162 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1163 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1164 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1165 cmd.resp_type = MMC_RSP_R7;
1167 err = mmc_send_cmd(mmc, &cmd, NULL);
1172 if ((cmd.response[0] & 0xff) != 0xaa)
1173 return UNUSABLE_ERR;
1175 mmc->version = SD_VERSION_2;
1180 /* not used any more */
1181 int __deprecated mmc_register(struct mmc *mmc)
1183 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1184 printf("%s is deprecated! use mmc_create() instead.\n", __func__);
1189 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
1193 /* quick validation */
1194 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
1195 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
1198 mmc = calloc(1, sizeof(*mmc));
1205 /* the following chunk was mmc_register() */
1207 /* Setup dsr related values */
1209 mmc->dsr = 0xffffffff;
1210 /* Setup the universal parts of the block interface just once */
1211 mmc->block_dev.if_type = IF_TYPE_MMC;
1212 mmc->block_dev.dev = cur_dev_num++;
1213 mmc->block_dev.removable = 1;
1214 mmc->block_dev.block_read = mmc_bread;
1215 mmc->block_dev.block_write = mmc_bwrite;
1216 mmc->block_dev.block_erase = mmc_berase;
1218 /* setup initial part type */
1219 mmc->block_dev.part_type = mmc->cfg->part_type;
1221 INIT_LIST_HEAD(&mmc->link);
1223 list_add_tail(&mmc->link, &mmc_devices);
1228 void mmc_destroy(struct mmc *mmc)
1230 /* only freeing memory for now */
1234 #ifdef CONFIG_PARTITIONS
1235 block_dev_desc_t *mmc_get_dev(int dev)
1237 struct mmc *mmc = find_mmc_device(dev);
1238 if (!mmc || mmc_init(mmc))
1241 return &mmc->block_dev;
1245 int mmc_start_init(struct mmc *mmc)
1249 /* we pretend there's no card when init is NULL */
1250 if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
1252 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1253 printf("MMC: no card present\n");
1261 /* made sure it's not NULL earlier */
1262 err = mmc->cfg->ops->init(mmc);
1267 mmc_set_bus_width(mmc, 1);
1268 mmc_set_clock(mmc, 1);
1270 /* Reset the Card */
1271 err = mmc_go_idle(mmc);
1276 /* The internal partition reset to user partition(0) at every CMD0*/
1279 /* Test for SD version 2 */
1280 err = mmc_send_if_cond(mmc);
1282 /* Now try to get the SD card's operating condition */
1283 err = sd_send_op_cond(mmc);
1285 /* If the command timed out, we check for an MMC card */
1286 if (err == TIMEOUT) {
1287 err = mmc_send_op_cond(mmc);
1289 if (err && err != IN_PROGRESS) {
1290 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1291 printf("Card did not respond to voltage select!\n");
1293 return UNUSABLE_ERR;
1297 if (err == IN_PROGRESS)
1298 mmc->init_in_progress = 1;
1303 static int mmc_complete_init(struct mmc *mmc)
1307 if (mmc->op_cond_pending)
1308 err = mmc_complete_op_cond(mmc);
1311 err = mmc_startup(mmc);
1316 mmc->init_in_progress = 0;
1320 int mmc_init(struct mmc *mmc)
1322 int err = IN_PROGRESS;
1323 unsigned start = get_timer(0);
1327 if (!mmc->init_in_progress)
1328 err = mmc_start_init(mmc);
1330 if (!err || err == IN_PROGRESS)
1331 err = mmc_complete_init(mmc);
1332 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1336 int mmc_set_dsr(struct mmc *mmc, u16 val)
1343 * CPU and board-specific MMC initializations. Aliased function
1344 * signals caller to move on
1346 static int __def_mmc_init(bd_t *bis)
1351 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1352 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1354 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1356 void print_mmc_devices(char separator)
1359 struct list_head *entry;
1361 list_for_each(entry, &mmc_devices) {
1362 m = list_entry(entry, struct mmc, link);
1364 printf("%s: %d", m->cfg->name, m->block_dev.dev);
1366 if (entry->next != &mmc_devices)
1367 printf("%c ", separator);
1374 void print_mmc_devices(char separator) { }
1377 int get_mmc_num(void)
1382 void mmc_set_preinit(struct mmc *mmc, int preinit)
1384 mmc->preinit = preinit;
1387 static void do_preinit(void)
1390 struct list_head *entry;
1392 list_for_each(entry, &mmc_devices) {
1393 m = list_entry(entry, struct mmc, link);
1401 int mmc_initialize(bd_t *bis)
1403 INIT_LIST_HEAD (&mmc_devices);
1406 if (board_mmc_init(bis) < 0)
1409 #ifndef CONFIG_SPL_BUILD
1410 print_mmc_devices(',');
1417 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1419 * This function changes the size of boot partition and the size of rpmb
1420 * partition present on EMMC devices.
1423 * struct *mmc: pointer for the mmc device strcuture
1424 * bootsize: size of boot partition
1425 * rpmbsize: size of rpmb partition
1427 * Returns 0 on success.
1430 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1431 unsigned long rpmbsize)
1436 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1437 cmd.cmdidx = MMC_CMD_RES_MAN;
1438 cmd.resp_type = MMC_RSP_R1b;
1439 cmd.cmdarg = MMC_CMD62_ARG1;
1441 err = mmc_send_cmd(mmc, &cmd, NULL);
1443 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1447 /* Boot partition changing mode */
1448 cmd.cmdidx = MMC_CMD_RES_MAN;
1449 cmd.resp_type = MMC_RSP_R1b;
1450 cmd.cmdarg = MMC_CMD62_ARG2;
1452 err = mmc_send_cmd(mmc, &cmd, NULL);
1454 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1457 /* boot partition size is multiple of 128KB */
1458 bootsize = (bootsize * 1024) / 128;
1460 /* Arg: boot partition size */
1461 cmd.cmdidx = MMC_CMD_RES_MAN;
1462 cmd.resp_type = MMC_RSP_R1b;
1463 cmd.cmdarg = bootsize;
1465 err = mmc_send_cmd(mmc, &cmd, NULL);
1467 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1470 /* RPMB partition size is multiple of 128KB */
1471 rpmbsize = (rpmbsize * 1024) / 128;
1472 /* Arg: RPMB partition size */
1473 cmd.cmdidx = MMC_CMD_RES_MAN;
1474 cmd.resp_type = MMC_RSP_R1b;
1475 cmd.cmdarg = rpmbsize;
1477 err = mmc_send_cmd(mmc, &cmd, NULL);
1479 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1486 * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1487 * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1490 * Returns 0 on success.
1492 int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
1496 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
1497 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
1498 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
1499 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
1507 * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1508 * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1511 * Returns 0 on success.
1513 int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1517 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1518 EXT_CSD_BOOT_ACK(ack) |
1519 EXT_CSD_BOOT_PART_NUM(part_num) |
1520 EXT_CSD_PARTITION_ACCESS(access));
1528 * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1529 * for enable. Note that this is a write-once field for non-zero values.
1531 * Returns 0 on success.
1533 int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
1535 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,