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 __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
47 int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
48 alias("__board_mmc_getcd")));
50 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
52 #ifdef CONFIG_MMC_TRACE
57 printf("CMD_SEND:%d\n", cmd->cmdidx);
58 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
59 printf("\t\tFLAG\t\t\t %d\n", cmd->flags);
60 ret = mmc->send_cmd(mmc, cmd, data);
61 switch (cmd->resp_type) {
63 printf("\t\tMMC_RSP_NONE\n");
66 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
70 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
74 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
76 printf("\t\t \t\t 0x%08X \n",
78 printf("\t\t \t\t 0x%08X \n",
80 printf("\t\t \t\t 0x%08X \n",
83 printf("\t\t\t\t\tDUMPING DATA\n");
84 for (i = 0; i < 4; i++) {
86 printf("\t\t\t\t\t%03d - ", i*4);
87 ptr = &cmd->response[i];
89 for (j = 0; j < 4; j++)
90 printf("%02X ", *ptr--);
95 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
99 printf("\t\tERROR MMC rsp not supported\n");
104 return mmc->send_cmd(mmc, cmd, data);
108 int mmc_send_status(struct mmc *mmc, int timeout)
112 #ifdef CONFIG_MMC_TRACE
116 cmd.cmdidx = MMC_CMD_SEND_STATUS;
117 cmd.resp_type = MMC_RSP_R1;
118 if (!mmc_host_is_spi(mmc))
119 cmd.cmdarg = mmc->rca << 16;
123 err = mmc_send_cmd(mmc, &cmd, NULL);
126 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
131 if (cmd.response[0] & MMC_STATUS_MASK) {
132 printf("Status Error: 0x%08X\n", cmd.response[0]);
137 #ifdef CONFIG_MMC_TRACE
138 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
139 printf("CURR STATE:%d\n", status);
142 printf("Timeout waiting card ready\n");
149 int mmc_set_blocklen(struct mmc *mmc, int len)
153 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
154 cmd.resp_type = MMC_RSP_R1;
158 return mmc_send_cmd(mmc, &cmd, NULL);
161 struct mmc *find_mmc_device(int dev_num)
164 struct list_head *entry;
166 list_for_each(entry, &mmc_devices) {
167 m = list_entry(entry, struct mmc, link);
169 if (m->block_dev.dev == dev_num)
173 printf("MMC Device %d not found\n", dev_num);
178 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
182 int err, start_cmd, end_cmd;
184 if (mmc->high_capacity)
185 end = start + blkcnt - 1;
187 end = (start + blkcnt - 1) * mmc->write_bl_len;
188 start *= mmc->write_bl_len;
192 start_cmd = SD_CMD_ERASE_WR_BLK_START;
193 end_cmd = SD_CMD_ERASE_WR_BLK_END;
195 start_cmd = MMC_CMD_ERASE_GROUP_START;
196 end_cmd = MMC_CMD_ERASE_GROUP_END;
199 cmd.cmdidx = start_cmd;
201 cmd.resp_type = MMC_RSP_R1;
204 err = mmc_send_cmd(mmc, &cmd, NULL);
208 cmd.cmdidx = end_cmd;
211 err = mmc_send_cmd(mmc, &cmd, NULL);
215 cmd.cmdidx = MMC_CMD_ERASE;
216 cmd.cmdarg = SECURE_ERASE;
217 cmd.resp_type = MMC_RSP_R1b;
219 err = mmc_send_cmd(mmc, &cmd, NULL);
226 puts("mmc erase failed\n");
231 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
234 struct mmc *mmc = find_mmc_device(dev_num);
235 lbaint_t blk = 0, blk_r = 0;
240 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
241 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
242 "The erase range would be change to 0x%lx~0x%lx\n\n",
243 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
244 ((start + blkcnt + mmc->erase_grp_size)
245 & ~(mmc->erase_grp_size - 1)) - 1);
247 while (blk < blkcnt) {
248 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
249 mmc->erase_grp_size : (blkcnt - blk);
250 err = mmc_erase_t(mmc, start + blk, blk_r);
261 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
264 struct mmc_data data;
267 if ((start + blkcnt) > mmc->block_dev.lba) {
268 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
269 start + blkcnt, mmc->block_dev.lba);
274 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
276 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
278 if (mmc->high_capacity)
281 cmd.cmdarg = start * mmc->write_bl_len;
283 cmd.resp_type = MMC_RSP_R1;
287 data.blocks = blkcnt;
288 data.blocksize = mmc->write_bl_len;
289 data.flags = MMC_DATA_WRITE;
291 if (mmc_send_cmd(mmc, &cmd, &data)) {
292 printf("mmc write failed\n");
296 /* SPI multiblock writes terminate using a special
297 * token, not a STOP_TRANSMISSION request.
299 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
300 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
302 cmd.resp_type = MMC_RSP_R1b;
304 if (mmc_send_cmd(mmc, &cmd, NULL)) {
305 printf("mmc fail to send stop cmd\n");
309 /* Waiting for the ready status */
310 mmc_send_status(mmc, timeout);
317 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
319 lbaint_t cur, blocks_todo = blkcnt;
321 struct mmc *mmc = find_mmc_device(dev_num);
325 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
329 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
330 if(mmc_write_blocks(mmc, start, cur, src) != cur)
334 src += cur * mmc->write_bl_len;
335 } while (blocks_todo > 0);
340 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
343 struct mmc_data data;
347 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
349 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
351 if (mmc->high_capacity)
354 cmd.cmdarg = start * mmc->read_bl_len;
356 cmd.resp_type = MMC_RSP_R1;
360 data.blocks = blkcnt;
361 data.blocksize = mmc->read_bl_len;
362 data.flags = MMC_DATA_READ;
364 if (mmc_send_cmd(mmc, &cmd, &data))
368 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
370 cmd.resp_type = MMC_RSP_R1b;
372 if (mmc_send_cmd(mmc, &cmd, NULL)) {
373 printf("mmc fail to send stop cmd\n");
377 /* Waiting for the ready status */
378 mmc_send_status(mmc, timeout);
384 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
386 lbaint_t cur, blocks_todo = blkcnt;
391 struct mmc *mmc = find_mmc_device(dev_num);
395 if ((start + blkcnt) > mmc->block_dev.lba) {
396 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
397 start + blkcnt, mmc->block_dev.lba);
401 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
405 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
406 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
410 dst += cur * mmc->read_bl_len;
411 } while (blocks_todo > 0);
416 int mmc_go_idle(struct mmc* mmc)
423 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
425 cmd.resp_type = MMC_RSP_NONE;
428 err = mmc_send_cmd(mmc, &cmd, NULL);
439 sd_send_op_cond(struct mmc *mmc)
446 cmd.cmdidx = MMC_CMD_APP_CMD;
447 cmd.resp_type = MMC_RSP_R1;
451 err = mmc_send_cmd(mmc, &cmd, NULL);
456 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
457 cmd.resp_type = MMC_RSP_R3;
460 * Most cards do not answer if some reserved bits
461 * in the ocr are set. However, Some controller
462 * can set bit 7 (reserved for low voltages), but
463 * how to manage low voltages SD card is not yet
466 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
467 (mmc->voltages & 0xff8000);
469 if (mmc->version == SD_VERSION_2)
470 cmd.cmdarg |= OCR_HCS;
472 err = mmc_send_cmd(mmc, &cmd, NULL);
478 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
483 if (mmc->version != SD_VERSION_2)
484 mmc->version = SD_VERSION_1_0;
486 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
487 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
488 cmd.resp_type = MMC_RSP_R3;
492 err = mmc_send_cmd(mmc, &cmd, NULL);
498 mmc->ocr = cmd.response[0];
500 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
506 int mmc_send_op_cond(struct mmc *mmc)
512 /* Some cards seem to need this */
515 /* Asking to the card its capabilities */
516 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
517 cmd.resp_type = MMC_RSP_R3;
521 err = mmc_send_cmd(mmc, &cmd, NULL);
529 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
530 cmd.resp_type = MMC_RSP_R3;
531 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
533 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
534 (cmd.response[0] & OCR_ACCESS_MODE));
536 if (mmc->host_caps & MMC_MODE_HC)
537 cmd.cmdarg |= OCR_HCS;
541 err = mmc_send_cmd(mmc, &cmd, NULL);
547 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
552 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
553 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
554 cmd.resp_type = MMC_RSP_R3;
558 err = mmc_send_cmd(mmc, &cmd, NULL);
564 mmc->version = MMC_VERSION_UNKNOWN;
565 mmc->ocr = cmd.response[0];
567 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
574 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
577 struct mmc_data data;
580 /* Get the Card Status Register */
581 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
582 cmd.resp_type = MMC_RSP_R1;
588 data.blocksize = 512;
589 data.flags = MMC_DATA_READ;
591 err = mmc_send_cmd(mmc, &cmd, &data);
597 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
603 cmd.cmdidx = MMC_CMD_SWITCH;
604 cmd.resp_type = MMC_RSP_R1b;
605 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
610 ret = mmc_send_cmd(mmc, &cmd, NULL);
612 /* Waiting for the ready status */
613 mmc_send_status(mmc, timeout);
619 int mmc_change_freq(struct mmc *mmc)
621 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
627 if (mmc_host_is_spi(mmc))
630 /* Only version 4 supports high-speed */
631 if (mmc->version < MMC_VERSION_4)
634 err = mmc_send_ext_csd(mmc, ext_csd);
639 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
641 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
646 /* Now check to see that it worked */
647 err = mmc_send_ext_csd(mmc, ext_csd);
652 /* No high-speed support */
653 if (!ext_csd[EXT_CSD_HS_TIMING])
656 /* High Speed is set, there are two types: 52MHz and 26MHz */
657 if (cardtype & MMC_HS_52MHZ)
658 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
660 mmc->card_caps |= MMC_MODE_HS;
665 int mmc_switch_part(int dev_num, unsigned int part_num)
667 struct mmc *mmc = find_mmc_device(dev_num);
672 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
673 (mmc->part_config & ~PART_ACCESS_MASK)
674 | (part_num & PART_ACCESS_MASK));
677 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
680 struct mmc_data data;
682 /* Switch the frequency */
683 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
684 cmd.resp_type = MMC_RSP_R1;
685 cmd.cmdarg = (mode << 31) | 0xffffff;
686 cmd.cmdarg &= ~(0xf << (group * 4));
687 cmd.cmdarg |= value << (group * 4);
690 data.dest = (char *)resp;
693 data.flags = MMC_DATA_READ;
695 return mmc_send_cmd(mmc, &cmd, &data);
699 int sd_change_freq(struct mmc *mmc)
703 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
704 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
705 struct mmc_data data;
710 if (mmc_host_is_spi(mmc))
713 /* Read the SCR to find out if this card supports higher speeds */
714 cmd.cmdidx = MMC_CMD_APP_CMD;
715 cmd.resp_type = MMC_RSP_R1;
716 cmd.cmdarg = mmc->rca << 16;
719 err = mmc_send_cmd(mmc, &cmd, NULL);
724 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
725 cmd.resp_type = MMC_RSP_R1;
732 data.dest = (char *)scr;
735 data.flags = MMC_DATA_READ;
737 err = mmc_send_cmd(mmc, &cmd, &data);
746 mmc->scr[0] = __be32_to_cpu(scr[0]);
747 mmc->scr[1] = __be32_to_cpu(scr[1]);
749 switch ((mmc->scr[0] >> 24) & 0xf) {
751 mmc->version = SD_VERSION_1_0;
754 mmc->version = SD_VERSION_1_10;
757 mmc->version = SD_VERSION_2;
760 mmc->version = SD_VERSION_1_0;
764 if (mmc->scr[0] & SD_DATA_4BIT)
765 mmc->card_caps |= MMC_MODE_4BIT;
767 /* Version 1.0 doesn't support switching */
768 if (mmc->version == SD_VERSION_1_0)
773 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
774 (u8 *)switch_status);
779 /* The high-speed function is busy. Try again */
780 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
784 /* If high-speed isn't supported, we return */
785 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
788 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
793 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
794 mmc->card_caps |= MMC_MODE_HS;
799 /* frequency bases */
800 /* divided by 10 to be nice to platforms without floating point */
801 static const int fbase[] = {
808 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
809 * to platforms without floating point.
811 static const int multipliers[] = {
830 void mmc_set_ios(struct mmc *mmc)
835 void mmc_set_clock(struct mmc *mmc, uint clock)
837 if (clock > mmc->f_max)
840 if (clock < mmc->f_min)
848 void mmc_set_bus_width(struct mmc *mmc, uint width)
850 mmc->bus_width = width;
855 int mmc_startup(struct mmc *mmc)
859 u64 cmult, csize, capacity;
861 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
862 ALLOC_CACHE_ALIGN_BUFFER(char, test_csd, 512);
865 #ifdef CONFIG_MMC_SPI_CRC_ON
866 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
867 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
868 cmd.resp_type = MMC_RSP_R1;
871 err = mmc_send_cmd(mmc, &cmd, NULL);
878 /* Put the Card in Identify Mode */
879 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
880 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
881 cmd.resp_type = MMC_RSP_R2;
885 err = mmc_send_cmd(mmc, &cmd, NULL);
890 memcpy(mmc->cid, cmd.response, 16);
893 * For MMC cards, set the Relative Address.
894 * For SD cards, get the Relatvie Address.
895 * This also puts the cards into Standby State
897 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
898 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
899 cmd.cmdarg = mmc->rca << 16;
900 cmd.resp_type = MMC_RSP_R6;
903 err = mmc_send_cmd(mmc, &cmd, NULL);
909 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
912 /* Get the Card-Specific Data */
913 cmd.cmdidx = MMC_CMD_SEND_CSD;
914 cmd.resp_type = MMC_RSP_R2;
915 cmd.cmdarg = mmc->rca << 16;
918 err = mmc_send_cmd(mmc, &cmd, NULL);
920 /* Waiting for the ready status */
921 mmc_send_status(mmc, timeout);
926 mmc->csd[0] = cmd.response[0];
927 mmc->csd[1] = cmd.response[1];
928 mmc->csd[2] = cmd.response[2];
929 mmc->csd[3] = cmd.response[3];
931 if (mmc->version == MMC_VERSION_UNKNOWN) {
932 int version = (cmd.response[0] >> 26) & 0xf;
936 mmc->version = MMC_VERSION_1_2;
939 mmc->version = MMC_VERSION_1_4;
942 mmc->version = MMC_VERSION_2_2;
945 mmc->version = MMC_VERSION_3;
948 mmc->version = MMC_VERSION_4;
951 mmc->version = MMC_VERSION_1_2;
956 /* divide frequency by 10, since the mults are 10x bigger */
957 freq = fbase[(cmd.response[0] & 0x7)];
958 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
960 mmc->tran_speed = freq * mult;
962 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
965 mmc->write_bl_len = mmc->read_bl_len;
967 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
969 if (mmc->high_capacity) {
970 csize = (mmc->csd[1] & 0x3f) << 16
971 | (mmc->csd[2] & 0xffff0000) >> 16;
974 csize = (mmc->csd[1] & 0x3ff) << 2
975 | (mmc->csd[2] & 0xc0000000) >> 30;
976 cmult = (mmc->csd[2] & 0x00038000) >> 15;
979 mmc->capacity = (csize + 1) << (cmult + 2);
980 mmc->capacity *= mmc->read_bl_len;
982 if (mmc->read_bl_len > 512)
983 mmc->read_bl_len = 512;
985 if (mmc->write_bl_len > 512)
986 mmc->write_bl_len = 512;
988 /* Select the card, and put it into Transfer Mode */
989 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
990 cmd.cmdidx = MMC_CMD_SELECT_CARD;
991 cmd.resp_type = MMC_RSP_R1;
992 cmd.cmdarg = mmc->rca << 16;
994 err = mmc_send_cmd(mmc, &cmd, NULL);
1001 * For SD, its erase group is always one sector
1003 mmc->erase_grp_size = 1;
1004 mmc->part_config = MMCPART_NOAVAILABLE;
1005 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1006 /* check ext_csd version and capacity */
1007 err = mmc_send_ext_csd(mmc, ext_csd);
1008 if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
1010 * According to the JEDEC Standard, the value of
1011 * ext_csd's capacity is valid if the value is more
1014 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1015 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1016 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1017 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1019 if ((capacity >> 20) > 2 * 1024)
1020 mmc->capacity = capacity;
1024 * Check whether GROUP_DEF is set, if yes, read out
1025 * group size from ext_csd directly, or calculate
1026 * the group size from the csd value.
1028 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1029 mmc->erase_grp_size =
1030 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
1032 int erase_gsz, erase_gmul;
1033 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1034 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1035 mmc->erase_grp_size = (erase_gsz + 1)
1039 /* store the partition info of emmc */
1040 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT)
1041 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1045 err = sd_change_freq(mmc);
1047 err = mmc_change_freq(mmc);
1052 /* Restrict card's capabilities by what the host can do */
1053 mmc->card_caps &= mmc->host_caps;
1056 if (mmc->card_caps & MMC_MODE_4BIT) {
1057 cmd.cmdidx = MMC_CMD_APP_CMD;
1058 cmd.resp_type = MMC_RSP_R1;
1059 cmd.cmdarg = mmc->rca << 16;
1062 err = mmc_send_cmd(mmc, &cmd, NULL);
1066 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1067 cmd.resp_type = MMC_RSP_R1;
1070 err = mmc_send_cmd(mmc, &cmd, NULL);
1074 mmc_set_bus_width(mmc, 4);
1077 if (mmc->card_caps & MMC_MODE_HS)
1078 mmc_set_clock(mmc, 50000000);
1080 mmc_set_clock(mmc, 25000000);
1082 for (width = EXT_CSD_BUS_WIDTH_8; width >= 0; width--) {
1083 /* Set the card to use 4 bit*/
1084 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1085 EXT_CSD_BUS_WIDTH, width);
1091 mmc_set_bus_width(mmc, 1);
1094 mmc_set_bus_width(mmc, 4 * width);
1096 err = mmc_send_ext_csd(mmc, test_csd);
1097 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1098 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1099 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1100 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1101 && ext_csd[EXT_CSD_REV] \
1102 == test_csd[EXT_CSD_REV]
1103 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1104 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1105 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1106 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1108 mmc->card_caps |= width;
1113 if (mmc->card_caps & MMC_MODE_HS) {
1114 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1115 mmc_set_clock(mmc, 52000000);
1117 mmc_set_clock(mmc, 26000000);
1119 mmc_set_clock(mmc, 20000000);
1122 /* fill in device description */
1123 mmc->block_dev.lun = 0;
1124 mmc->block_dev.type = 0;
1125 mmc->block_dev.blksz = mmc->read_bl_len;
1126 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1127 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1128 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1129 sprintf(mmc->block_dev.product, "%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 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1133 (mmc->cid[2] >> 24) & 0xf);
1134 init_part(&mmc->block_dev);
1139 int mmc_send_if_cond(struct mmc *mmc)
1144 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1145 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1146 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1147 cmd.resp_type = MMC_RSP_R7;
1150 err = mmc_send_cmd(mmc, &cmd, NULL);
1155 if ((cmd.response[0] & 0xff) != 0xaa)
1156 return UNUSABLE_ERR;
1158 mmc->version = SD_VERSION_2;
1163 int mmc_register(struct mmc *mmc)
1165 /* Setup the universal parts of the block interface just once */
1166 mmc->block_dev.if_type = IF_TYPE_MMC;
1167 mmc->block_dev.dev = cur_dev_num++;
1168 mmc->block_dev.removable = 1;
1169 mmc->block_dev.block_read = mmc_bread;
1170 mmc->block_dev.block_write = mmc_bwrite;
1171 mmc->block_dev.block_erase = mmc_berase;
1173 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1175 INIT_LIST_HEAD (&mmc->link);
1177 list_add_tail (&mmc->link, &mmc_devices);
1182 #ifdef CONFIG_PARTITIONS
1183 block_dev_desc_t *mmc_get_dev(int dev)
1185 struct mmc *mmc = find_mmc_device(dev);
1187 return mmc ? &mmc->block_dev : NULL;
1191 int mmc_init(struct mmc *mmc)
1198 err = mmc->init(mmc);
1203 mmc_set_bus_width(mmc, 1);
1204 mmc_set_clock(mmc, 1);
1206 /* Reset the Card */
1207 err = mmc_go_idle(mmc);
1212 /* The internal partition reset to user partition(0) at every CMD0*/
1215 /* Test for SD version 2 */
1217 * retry here for 3 times, as for some controller it has dynamic
1218 * clock gating, and only toggle out clk when the first cmd0 send
1219 * out, while some card strictly obey the 74 clocks rule, the interval
1220 * may not be sufficient between the cmd0 and this cmd8, retry to
1221 * fulfil the clock requirement
1224 err = mmc_send_if_cond(mmc);
1225 } while (--retry > 0 && err);
1230 /* Now try to get the SD card's operating condition */
1231 err = sd_send_op_cond(mmc);
1233 /* If the command timed out, we check for an MMC card */
1234 if (err == TIMEOUT) {
1235 err = mmc_send_op_cond(mmc);
1238 printf("Card did not respond to voltage select!\n");
1239 return UNUSABLE_ERR;
1243 err = mmc_startup(mmc);
1252 * CPU and board-specific MMC initializations. Aliased function
1253 * signals caller to move on
1255 static int __def_mmc_init(bd_t *bis)
1260 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1261 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1263 void print_mmc_devices(char separator)
1266 struct list_head *entry;
1268 list_for_each(entry, &mmc_devices) {
1269 m = list_entry(entry, struct mmc, link);
1271 printf("%s: %d", m->name, m->block_dev.dev);
1273 if (entry->next != &mmc_devices)
1274 printf("%c ", separator);
1280 int get_mmc_num(void)
1285 int mmc_initialize(bd_t *bis)
1287 INIT_LIST_HEAD (&mmc_devices);
1290 if (board_mmc_init(bis) < 0)
1293 print_mmc_devices(',');