2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <linux/list.h>
35 /* Set block count limit because of 16 bit register limit on some hardware*/
36 #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37 #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
40 static struct list_head mmc_devices;
41 static int cur_dev_num = -1;
43 int __weak board_mmc_getwp(struct mmc *mmc)
48 int mmc_getwp(struct mmc *mmc)
52 wp = board_mmc_getwp(mmc);
64 int __board_mmc_getcd(struct mmc *mmc) {
68 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
69 alias("__board_mmc_getcd")));
71 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
72 struct mmc_data *data)
74 struct mmc_data backup;
77 memset(&backup, 0, sizeof(backup));
79 #ifdef CONFIG_MMC_TRACE
83 printf("CMD_SEND:%d\n", cmd->cmdidx);
84 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
85 ret = mmc->send_cmd(mmc, cmd, data);
86 switch (cmd->resp_type) {
88 printf("\t\tMMC_RSP_NONE\n");
91 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
95 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
99 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
101 printf("\t\t \t\t 0x%08X \n",
103 printf("\t\t \t\t 0x%08X \n",
105 printf("\t\t \t\t 0x%08X \n",
108 printf("\t\t\t\t\tDUMPING DATA\n");
109 for (i = 0; i < 4; i++) {
111 printf("\t\t\t\t\t%03d - ", i*4);
112 ptr = (u8 *)&cmd->response[i];
114 for (j = 0; j < 4; j++)
115 printf("%02X ", *ptr--);
120 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
124 printf("\t\tERROR MMC rsp not supported\n");
128 ret = mmc->send_cmd(mmc, cmd, data);
133 static int mmc_send_status(struct mmc *mmc, int timeout)
136 int err, retries = 5;
137 #ifdef CONFIG_MMC_TRACE
141 cmd.cmdidx = MMC_CMD_SEND_STATUS;
142 cmd.resp_type = MMC_RSP_R1;
143 if (!mmc_host_is_spi(mmc))
144 cmd.cmdarg = mmc->rca << 16;
147 err = mmc_send_cmd(mmc, &cmd, NULL);
149 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
150 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
153 else if (cmd.response[0] & MMC_STATUS_MASK) {
154 printf("Status Error: 0x%08X\n",
158 } else if (--retries < 0)
165 #ifdef CONFIG_MMC_TRACE
166 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
167 printf("CURR STATE:%d\n", status);
170 printf("Timeout waiting card ready\n");
177 static int mmc_set_blocklen(struct mmc *mmc, int len)
181 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
182 cmd.resp_type = MMC_RSP_R1;
185 return mmc_send_cmd(mmc, &cmd, NULL);
188 struct mmc *find_mmc_device(int dev_num)
191 struct list_head *entry;
193 list_for_each(entry, &mmc_devices) {
194 m = list_entry(entry, struct mmc, link);
196 if (m->block_dev.dev == dev_num)
200 printf("MMC Device %d not found\n", dev_num);
205 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
209 int err, start_cmd, end_cmd;
211 if (mmc->high_capacity)
212 end = start + blkcnt - 1;
214 end = (start + blkcnt - 1) * mmc->write_bl_len;
215 start *= mmc->write_bl_len;
219 start_cmd = SD_CMD_ERASE_WR_BLK_START;
220 end_cmd = SD_CMD_ERASE_WR_BLK_END;
222 start_cmd = MMC_CMD_ERASE_GROUP_START;
223 end_cmd = MMC_CMD_ERASE_GROUP_END;
226 cmd.cmdidx = start_cmd;
228 cmd.resp_type = MMC_RSP_R1;
230 err = mmc_send_cmd(mmc, &cmd, NULL);
234 cmd.cmdidx = end_cmd;
237 err = mmc_send_cmd(mmc, &cmd, NULL);
241 cmd.cmdidx = MMC_CMD_ERASE;
242 cmd.cmdarg = SECURE_ERASE;
243 cmd.resp_type = MMC_RSP_R1b;
245 err = mmc_send_cmd(mmc, &cmd, NULL);
252 puts("mmc erase failed\n");
257 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
260 struct mmc *mmc = find_mmc_device(dev_num);
261 lbaint_t blk = 0, blk_r = 0;
267 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
268 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
269 "The erase range would be change to 0x%lx~0x%lx\n\n",
270 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
271 ((start + blkcnt + mmc->erase_grp_size)
272 & ~(mmc->erase_grp_size - 1)) - 1);
274 while (blk < blkcnt) {
275 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
276 mmc->erase_grp_size : (blkcnt - blk);
277 err = mmc_erase_t(mmc, start + blk, blk_r);
283 /* Waiting for the ready status */
284 if (mmc_send_status(mmc, timeout))
292 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
295 struct mmc_data data;
298 if ((start + blkcnt) > mmc->block_dev.lba) {
299 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
300 start + blkcnt, mmc->block_dev.lba);
305 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
307 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
309 if (mmc->high_capacity)
312 cmd.cmdarg = start * mmc->write_bl_len;
314 cmd.resp_type = MMC_RSP_R1;
317 data.blocks = blkcnt;
318 data.blocksize = mmc->write_bl_len;
319 data.flags = MMC_DATA_WRITE;
321 if (mmc_send_cmd(mmc, &cmd, &data)) {
322 printf("mmc write failed\n");
326 /* SPI multiblock writes terminate using a special
327 * token, not a STOP_TRANSMISSION request.
329 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
330 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
332 cmd.resp_type = MMC_RSP_R1b;
333 if (mmc_send_cmd(mmc, &cmd, NULL)) {
334 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, ulong 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, ulong 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 printf("mmc fail to send stop cmd\n");
409 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
411 lbaint_t cur, blocks_todo = blkcnt;
416 struct mmc *mmc = find_mmc_device(dev_num);
420 if ((start + blkcnt) > mmc->block_dev.lba) {
421 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
422 start + blkcnt, mmc->block_dev.lba);
426 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
430 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
431 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
435 dst += cur * mmc->read_bl_len;
436 } while (blocks_todo > 0);
441 static int mmc_go_idle(struct mmc *mmc)
448 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
450 cmd.resp_type = MMC_RSP_NONE;
452 err = mmc_send_cmd(mmc, &cmd, NULL);
462 static int sd_send_op_cond(struct mmc *mmc)
469 cmd.cmdidx = MMC_CMD_APP_CMD;
470 cmd.resp_type = MMC_RSP_R1;
473 err = mmc_send_cmd(mmc, &cmd, NULL);
478 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
479 cmd.resp_type = MMC_RSP_R3;
482 * Most cards do not answer if some reserved bits
483 * in the ocr are set. However, Some controller
484 * can set bit 7 (reserved for low voltages), but
485 * how to manage low voltages SD card is not yet
488 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
489 (mmc->voltages & 0xff8000);
491 if (mmc->version == SD_VERSION_2)
492 cmd.cmdarg |= OCR_HCS;
494 err = mmc_send_cmd(mmc, &cmd, NULL);
500 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
505 if (mmc->version != SD_VERSION_2)
506 mmc->version = SD_VERSION_1_0;
508 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
509 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
510 cmd.resp_type = MMC_RSP_R3;
513 err = mmc_send_cmd(mmc, &cmd, NULL);
519 mmc->ocr = cmd.response[0];
521 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
527 static int mmc_send_op_cond(struct mmc *mmc)
533 /* Some cards seem to need this */
536 /* Asking to the card its capabilities */
537 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
538 cmd.resp_type = MMC_RSP_R3;
541 err = mmc_send_cmd(mmc, &cmd, NULL);
549 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
550 cmd.resp_type = MMC_RSP_R3;
551 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
553 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
554 (cmd.response[0] & OCR_ACCESS_MODE));
556 if (mmc->host_caps & MMC_MODE_HC)
557 cmd.cmdarg |= OCR_HCS;
559 err = mmc_send_cmd(mmc, &cmd, NULL);
565 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
570 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
571 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
572 cmd.resp_type = MMC_RSP_R3;
575 err = mmc_send_cmd(mmc, &cmd, NULL);
581 mmc->version = MMC_VERSION_UNKNOWN;
582 mmc->ocr = cmd.response[0];
584 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
591 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
594 struct mmc_data data;
597 /* Get the Card Status Register */
598 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
599 cmd.resp_type = MMC_RSP_R1;
602 data.dest = (char *)ext_csd;
604 data.blocksize = 512;
605 data.flags = MMC_DATA_READ;
607 err = mmc_send_cmd(mmc, &cmd, &data);
613 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
619 cmd.cmdidx = MMC_CMD_SWITCH;
620 cmd.resp_type = MMC_RSP_R1b;
621 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
625 ret = mmc_send_cmd(mmc, &cmd, NULL);
627 /* Waiting for the ready status */
629 ret = mmc_send_status(mmc, timeout);
635 static int mmc_change_freq(struct mmc *mmc)
637 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
643 if (mmc_host_is_spi(mmc))
646 /* Only version 4 supports high-speed */
647 if (mmc->version < MMC_VERSION_4)
650 err = mmc_send_ext_csd(mmc, ext_csd);
655 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
657 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
662 /* Now check to see that it worked */
663 err = mmc_send_ext_csd(mmc, ext_csd);
668 /* No high-speed support */
669 if (!ext_csd[EXT_CSD_HS_TIMING])
672 /* High Speed is set, there are two types: 52MHz and 26MHz */
673 if (cardtype & MMC_HS_52MHZ)
674 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
676 mmc->card_caps |= MMC_MODE_HS;
681 int mmc_switch_part(int dev_num, unsigned int part_num)
683 struct mmc *mmc = find_mmc_device(dev_num);
688 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
689 (mmc->part_config & ~PART_ACCESS_MASK)
690 | (part_num & PART_ACCESS_MASK));
693 int mmc_getcd(struct mmc *mmc)
697 cd = board_mmc_getcd(mmc);
701 cd = mmc->getcd(mmc);
709 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
712 struct mmc_data data;
714 /* Switch the frequency */
715 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
716 cmd.resp_type = MMC_RSP_R1;
717 cmd.cmdarg = (mode << 31) | 0xffffff;
718 cmd.cmdarg &= ~(0xf << (group * 4));
719 cmd.cmdarg |= value << (group * 4);
721 data.dest = (char *)resp;
724 data.flags = MMC_DATA_READ;
726 return mmc_send_cmd(mmc, &cmd, &data);
730 static int sd_change_freq(struct mmc *mmc)
734 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
735 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
736 struct mmc_data data;
741 if (mmc_host_is_spi(mmc))
744 /* Read the SCR to find out if this card supports higher speeds */
745 cmd.cmdidx = MMC_CMD_APP_CMD;
746 cmd.resp_type = MMC_RSP_R1;
747 cmd.cmdarg = mmc->rca << 16;
749 err = mmc_send_cmd(mmc, &cmd, NULL);
754 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
755 cmd.resp_type = MMC_RSP_R1;
761 data.dest = (char *)scr;
764 data.flags = MMC_DATA_READ;
766 err = mmc_send_cmd(mmc, &cmd, &data);
775 mmc->scr[0] = __be32_to_cpu(scr[0]);
776 mmc->scr[1] = __be32_to_cpu(scr[1]);
778 switch ((mmc->scr[0] >> 24) & 0xf) {
780 mmc->version = SD_VERSION_1_0;
783 mmc->version = SD_VERSION_1_10;
786 mmc->version = SD_VERSION_2;
787 if ((mmc->scr[0] >> 15) & 0x1)
788 mmc->version = SD_VERSION_3;
791 mmc->version = SD_VERSION_1_0;
795 if (mmc->scr[0] & SD_DATA_4BIT)
796 mmc->card_caps |= MMC_MODE_4BIT;
798 /* Version 1.0 doesn't support switching */
799 if (mmc->version == SD_VERSION_1_0)
804 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
805 (u8 *)switch_status);
810 /* The high-speed function is busy. Try again */
811 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
815 /* If high-speed isn't supported, we return */
816 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
820 * If the host doesn't support SD_HIGHSPEED, do not switch card to
821 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
822 * This can avoid furthur problem when the card runs in different
823 * mode between the host.
825 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
826 (mmc->host_caps & MMC_MODE_HS)))
829 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
834 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
835 mmc->card_caps |= MMC_MODE_HS;
840 /* frequency bases */
841 /* divided by 10 to be nice to platforms without floating point */
842 static const int fbase[] = {
849 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
850 * to platforms without floating point.
852 static const int multipliers[] = {
871 static void mmc_set_ios(struct mmc *mmc)
876 void mmc_set_clock(struct mmc *mmc, uint clock)
878 if (clock > mmc->f_max)
881 if (clock < mmc->f_min)
889 static void mmc_set_bus_width(struct mmc *mmc, uint width)
891 mmc->bus_width = width;
896 static int mmc_startup(struct mmc *mmc)
900 u64 cmult, csize, capacity;
902 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
903 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
906 #ifdef CONFIG_MMC_SPI_CRC_ON
907 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
908 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
909 cmd.resp_type = MMC_RSP_R1;
911 err = mmc_send_cmd(mmc, &cmd, NULL);
918 /* Put the Card in Identify Mode */
919 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
920 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
921 cmd.resp_type = MMC_RSP_R2;
924 err = mmc_send_cmd(mmc, &cmd, NULL);
929 memcpy(mmc->cid, cmd.response, 16);
932 * For MMC cards, set the Relative Address.
933 * For SD cards, get the Relatvie Address.
934 * This also puts the cards into Standby State
936 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
937 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
938 cmd.cmdarg = mmc->rca << 16;
939 cmd.resp_type = MMC_RSP_R6;
941 err = mmc_send_cmd(mmc, &cmd, NULL);
947 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
950 /* Get the Card-Specific Data */
951 cmd.cmdidx = MMC_CMD_SEND_CSD;
952 cmd.resp_type = MMC_RSP_R2;
953 cmd.cmdarg = mmc->rca << 16;
955 err = mmc_send_cmd(mmc, &cmd, NULL);
957 /* Waiting for the ready status */
958 mmc_send_status(mmc, timeout);
963 mmc->csd[0] = cmd.response[0];
964 mmc->csd[1] = cmd.response[1];
965 mmc->csd[2] = cmd.response[2];
966 mmc->csd[3] = cmd.response[3];
968 if (mmc->version == MMC_VERSION_UNKNOWN) {
969 int version = (cmd.response[0] >> 26) & 0xf;
973 mmc->version = MMC_VERSION_1_2;
976 mmc->version = MMC_VERSION_1_4;
979 mmc->version = MMC_VERSION_2_2;
982 mmc->version = MMC_VERSION_3;
985 mmc->version = MMC_VERSION_4;
988 mmc->version = MMC_VERSION_1_2;
993 /* divide frequency by 10, since the mults are 10x bigger */
994 freq = fbase[(cmd.response[0] & 0x7)];
995 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
997 mmc->tran_speed = freq * mult;
999 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1002 mmc->write_bl_len = mmc->read_bl_len;
1004 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1006 if (mmc->high_capacity) {
1007 csize = (mmc->csd[1] & 0x3f) << 16
1008 | (mmc->csd[2] & 0xffff0000) >> 16;
1011 csize = (mmc->csd[1] & 0x3ff) << 2
1012 | (mmc->csd[2] & 0xc0000000) >> 30;
1013 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1016 mmc->capacity = (csize + 1) << (cmult + 2);
1017 mmc->capacity *= mmc->read_bl_len;
1019 if (mmc->read_bl_len > 512)
1020 mmc->read_bl_len = 512;
1022 if (mmc->write_bl_len > 512)
1023 mmc->write_bl_len = 512;
1025 /* Select the card, and put it into Transfer Mode */
1026 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1027 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1028 cmd.resp_type = MMC_RSP_R1;
1029 cmd.cmdarg = mmc->rca << 16;
1030 err = mmc_send_cmd(mmc, &cmd, NULL);
1037 * For SD, its erase group is always one sector
1039 mmc->erase_grp_size = 1;
1040 mmc->part_config = MMCPART_NOAVAILABLE;
1041 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1042 /* check ext_csd version and capacity */
1043 err = mmc_send_ext_csd(mmc, ext_csd);
1044 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1046 * According to the JEDEC Standard, the value of
1047 * ext_csd's capacity is valid if the value is more
1050 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1051 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1052 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1053 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1055 if ((capacity >> 20) > 2 * 1024)
1056 mmc->capacity = capacity;
1059 switch (ext_csd[EXT_CSD_REV]) {
1061 mmc->version = MMC_VERSION_4_1;
1064 mmc->version = MMC_VERSION_4_2;
1067 mmc->version = MMC_VERSION_4_3;
1070 mmc->version = MMC_VERSION_4_41;
1073 mmc->version = MMC_VERSION_4_5;
1078 * Check whether GROUP_DEF is set, if yes, read out
1079 * group size from ext_csd directly, or calculate
1080 * the group size from the csd value.
1082 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1083 mmc->erase_grp_size =
1084 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
1086 int erase_gsz, erase_gmul;
1087 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1088 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1089 mmc->erase_grp_size = (erase_gsz + 1)
1093 /* store the partition info of emmc */
1094 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1095 ext_csd[EXT_CSD_BOOT_MULT])
1096 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1100 err = sd_change_freq(mmc);
1102 err = mmc_change_freq(mmc);
1107 /* Restrict card's capabilities by what the host can do */
1108 mmc->card_caps &= mmc->host_caps;
1111 if (mmc->card_caps & MMC_MODE_4BIT) {
1112 cmd.cmdidx = MMC_CMD_APP_CMD;
1113 cmd.resp_type = MMC_RSP_R1;
1114 cmd.cmdarg = mmc->rca << 16;
1116 err = mmc_send_cmd(mmc, &cmd, NULL);
1120 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1121 cmd.resp_type = MMC_RSP_R1;
1123 err = mmc_send_cmd(mmc, &cmd, NULL);
1127 mmc_set_bus_width(mmc, 4);
1130 if (mmc->card_caps & MMC_MODE_HS)
1131 mmc->tran_speed = 50000000;
1133 mmc->tran_speed = 25000000;
1137 /* An array of possible bus widths in order of preference */
1138 static unsigned ext_csd_bits[] = {
1139 EXT_CSD_BUS_WIDTH_8,
1140 EXT_CSD_BUS_WIDTH_4,
1141 EXT_CSD_BUS_WIDTH_1,
1144 /* An array to map CSD bus widths to host cap bits */
1145 static unsigned ext_to_hostcaps[] = {
1146 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1147 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1150 /* An array to map chosen bus width to an integer */
1151 static unsigned widths[] = {
1155 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1156 unsigned int extw = ext_csd_bits[idx];
1159 * Check to make sure the controller supports
1160 * this bus width, if it's more than 1
1162 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1163 !(mmc->host_caps & ext_to_hostcaps[extw]))
1166 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1167 EXT_CSD_BUS_WIDTH, extw);
1172 mmc_set_bus_width(mmc, widths[idx]);
1174 err = mmc_send_ext_csd(mmc, test_csd);
1175 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1176 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1177 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1178 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1179 && ext_csd[EXT_CSD_REV] \
1180 == test_csd[EXT_CSD_REV]
1181 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1182 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1183 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1184 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1186 mmc->card_caps |= ext_to_hostcaps[extw];
1191 if (mmc->card_caps & MMC_MODE_HS) {
1192 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1193 mmc->tran_speed = 52000000;
1195 mmc->tran_speed = 26000000;
1199 mmc_set_clock(mmc, mmc->tran_speed);
1201 /* fill in device description */
1202 mmc->block_dev.lun = 0;
1203 mmc->block_dev.type = 0;
1204 mmc->block_dev.blksz = mmc->read_bl_len;
1205 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1206 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1207 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1208 (mmc->cid[3] >> 16) & 0xffff);
1209 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1210 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1211 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1212 (mmc->cid[2] >> 24) & 0xff);
1213 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1214 (mmc->cid[2] >> 16) & 0xf);
1215 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1216 init_part(&mmc->block_dev);
1222 static int mmc_send_if_cond(struct mmc *mmc)
1227 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1228 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1229 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1230 cmd.resp_type = MMC_RSP_R7;
1232 err = mmc_send_cmd(mmc, &cmd, NULL);
1237 if ((cmd.response[0] & 0xff) != 0xaa)
1238 return UNUSABLE_ERR;
1240 mmc->version = SD_VERSION_2;
1245 int mmc_register(struct mmc *mmc)
1247 /* Setup the universal parts of the block interface just once */
1248 mmc->block_dev.if_type = IF_TYPE_MMC;
1249 mmc->block_dev.dev = cur_dev_num++;
1250 mmc->block_dev.removable = 1;
1251 mmc->block_dev.block_read = mmc_bread;
1252 mmc->block_dev.block_write = mmc_bwrite;
1253 mmc->block_dev.block_erase = mmc_berase;
1255 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1257 INIT_LIST_HEAD (&mmc->link);
1259 list_add_tail (&mmc->link, &mmc_devices);
1264 #ifdef CONFIG_PARTITIONS
1265 block_dev_desc_t *mmc_get_dev(int dev)
1267 struct mmc *mmc = find_mmc_device(dev);
1268 if (!mmc || mmc_init(mmc))
1271 return &mmc->block_dev;
1275 int mmc_init(struct mmc *mmc)
1279 if (mmc_getcd(mmc) == 0) {
1281 printf("MMC: no card present\n");
1288 err = mmc->init(mmc);
1293 mmc_set_bus_width(mmc, 1);
1294 mmc_set_clock(mmc, 1);
1296 /* Reset the Card */
1297 err = mmc_go_idle(mmc);
1302 /* The internal partition reset to user partition(0) at every CMD0*/
1305 /* Test for SD version 2 */
1306 err = mmc_send_if_cond(mmc);
1308 /* Now try to get the SD card's operating condition */
1309 err = sd_send_op_cond(mmc);
1311 /* If the command timed out, we check for an MMC card */
1312 if (err == TIMEOUT) {
1313 err = mmc_send_op_cond(mmc);
1316 printf("Card did not respond to voltage select!\n");
1317 return UNUSABLE_ERR;
1321 err = mmc_startup(mmc);
1330 * CPU and board-specific MMC initializations. Aliased function
1331 * signals caller to move on
1333 static int __def_mmc_init(bd_t *bis)
1338 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1339 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1341 void print_mmc_devices(char separator)
1344 struct list_head *entry;
1346 list_for_each(entry, &mmc_devices) {
1347 m = list_entry(entry, struct mmc, link);
1349 printf("%s: %d", m->name, m->block_dev.dev);
1351 if (entry->next != &mmc_devices)
1352 printf("%c ", separator);
1358 int get_mmc_num(void)
1363 int mmc_initialize(bd_t *bis)
1365 INIT_LIST_HEAD (&mmc_devices);
1368 if (board_mmc_init(bis) < 0)
1371 print_mmc_devices(',');