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);
54 if ((wp < 0) && mmc->getwp)
60 int __board_mmc_getcd(struct mmc *mmc) {
64 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
65 alias("__board_mmc_getcd")));
67 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
68 struct mmc_data *data)
70 struct mmc_data backup;
73 memset(&backup, 0, sizeof(backup));
75 #ifdef CONFIG_MMC_TRACE
79 printf("CMD_SEND:%d\n", cmd->cmdidx);
80 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
81 ret = mmc->send_cmd(mmc, cmd, data);
82 switch (cmd->resp_type) {
84 printf("\t\tMMC_RSP_NONE\n");
87 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
91 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
95 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
97 printf("\t\t \t\t 0x%08X \n",
99 printf("\t\t \t\t 0x%08X \n",
101 printf("\t\t \t\t 0x%08X \n",
104 printf("\t\t\t\t\tDUMPING DATA\n");
105 for (i = 0; i < 4; i++) {
107 printf("\t\t\t\t\t%03d - ", i*4);
108 ptr = (u8 *)&cmd->response[i];
110 for (j = 0; j < 4; j++)
111 printf("%02X ", *ptr--);
116 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
120 printf("\t\tERROR MMC rsp not supported\n");
124 ret = mmc->send_cmd(mmc, cmd, data);
129 static int mmc_send_status(struct mmc *mmc, int timeout)
132 int err, retries = 5;
133 #ifdef CONFIG_MMC_TRACE
137 cmd.cmdidx = MMC_CMD_SEND_STATUS;
138 cmd.resp_type = MMC_RSP_R1;
139 if (!mmc_host_is_spi(mmc))
140 cmd.cmdarg = mmc->rca << 16;
143 err = mmc_send_cmd(mmc, &cmd, NULL);
145 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
146 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
149 else if (cmd.response[0] & MMC_STATUS_MASK) {
150 printf("Status Error: 0x%08X\n",
154 } else if (--retries < 0)
161 #ifdef CONFIG_MMC_TRACE
162 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
163 printf("CURR STATE:%d\n", status);
166 printf("Timeout waiting card ready\n");
173 static int mmc_set_blocklen(struct mmc *mmc, int len)
177 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
178 cmd.resp_type = MMC_RSP_R1;
181 return mmc_send_cmd(mmc, &cmd, NULL);
184 struct mmc *find_mmc_device(int dev_num)
187 struct list_head *entry;
189 list_for_each(entry, &mmc_devices) {
190 m = list_entry(entry, struct mmc, link);
192 if (m->block_dev.dev == dev_num)
196 printf("MMC Device %d not found\n", dev_num);
201 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
205 int err, start_cmd, end_cmd;
207 if (mmc->high_capacity)
208 end = start + blkcnt - 1;
210 end = (start + blkcnt - 1) * mmc->write_bl_len;
211 start *= mmc->write_bl_len;
215 start_cmd = SD_CMD_ERASE_WR_BLK_START;
216 end_cmd = SD_CMD_ERASE_WR_BLK_END;
218 start_cmd = MMC_CMD_ERASE_GROUP_START;
219 end_cmd = MMC_CMD_ERASE_GROUP_END;
222 cmd.cmdidx = start_cmd;
224 cmd.resp_type = MMC_RSP_R1;
226 err = mmc_send_cmd(mmc, &cmd, NULL);
230 cmd.cmdidx = end_cmd;
233 err = mmc_send_cmd(mmc, &cmd, NULL);
237 cmd.cmdidx = MMC_CMD_ERASE;
238 cmd.cmdarg = SECURE_ERASE;
239 cmd.resp_type = MMC_RSP_R1b;
241 err = mmc_send_cmd(mmc, &cmd, NULL);
248 puts("mmc erase failed\n");
253 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
256 struct mmc *mmc = find_mmc_device(dev_num);
257 lbaint_t blk = 0, blk_r = 0;
263 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
264 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
265 "The erase range would be change to 0x%lx~0x%lx\n\n",
266 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
267 ((start + blkcnt + mmc->erase_grp_size)
268 & ~(mmc->erase_grp_size - 1)) - 1);
270 while (blk < blkcnt) {
271 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
272 mmc->erase_grp_size : (blkcnt - blk);
273 err = mmc_erase_t(mmc, start + blk, blk_r);
279 /* Waiting for the ready status */
280 if (mmc_send_status(mmc, timeout))
288 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
291 struct mmc_data data;
294 if ((start + blkcnt) > mmc->block_dev.lba) {
295 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
296 start + blkcnt, mmc->block_dev.lba);
301 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
303 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
305 if (mmc->high_capacity)
308 cmd.cmdarg = start * mmc->write_bl_len;
310 cmd.resp_type = MMC_RSP_R1;
313 data.blocks = blkcnt;
314 data.blocksize = mmc->write_bl_len;
315 data.flags = MMC_DATA_WRITE;
317 if (mmc_send_cmd(mmc, &cmd, &data)) {
318 printf("mmc write failed\n");
322 /* SPI multiblock writes terminate using a special
323 * token, not a STOP_TRANSMISSION request.
325 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
326 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
328 cmd.resp_type = MMC_RSP_R1b;
329 if (mmc_send_cmd(mmc, &cmd, NULL)) {
330 printf("mmc fail to send stop cmd\n");
335 /* Waiting for the ready status */
336 if (mmc_send_status(mmc, timeout))
343 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
345 lbaint_t cur, blocks_todo = blkcnt;
347 struct mmc *mmc = find_mmc_device(dev_num);
351 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
355 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
356 if(mmc_write_blocks(mmc, start, cur, src) != cur)
360 src += cur * mmc->write_bl_len;
361 } while (blocks_todo > 0);
366 static int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start,
370 struct mmc_data data;
373 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
375 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
377 if (mmc->high_capacity)
380 cmd.cmdarg = start * mmc->read_bl_len;
382 cmd.resp_type = MMC_RSP_R1;
385 data.blocks = blkcnt;
386 data.blocksize = mmc->read_bl_len;
387 data.flags = MMC_DATA_READ;
389 if (mmc_send_cmd(mmc, &cmd, &data))
393 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
395 cmd.resp_type = MMC_RSP_R1b;
396 if (mmc_send_cmd(mmc, &cmd, NULL)) {
397 printf("mmc fail to send stop cmd\n");
405 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
407 lbaint_t cur, blocks_todo = blkcnt;
412 struct mmc *mmc = find_mmc_device(dev_num);
416 if ((start + blkcnt) > mmc->block_dev.lba) {
417 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
418 start + blkcnt, mmc->block_dev.lba);
422 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
426 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
427 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
431 dst += cur * mmc->read_bl_len;
432 } while (blocks_todo > 0);
437 static int mmc_go_idle(struct mmc *mmc)
444 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
446 cmd.resp_type = MMC_RSP_NONE;
448 err = mmc_send_cmd(mmc, &cmd, NULL);
458 static int sd_send_op_cond(struct mmc *mmc)
465 cmd.cmdidx = MMC_CMD_APP_CMD;
466 cmd.resp_type = MMC_RSP_R1;
469 err = mmc_send_cmd(mmc, &cmd, NULL);
474 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
475 cmd.resp_type = MMC_RSP_R3;
478 * Most cards do not answer if some reserved bits
479 * in the ocr are set. However, Some controller
480 * can set bit 7 (reserved for low voltages), but
481 * how to manage low voltages SD card is not yet
484 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
485 (mmc->voltages & 0xff8000);
487 if (mmc->version == SD_VERSION_2)
488 cmd.cmdarg |= OCR_HCS;
490 err = mmc_send_cmd(mmc, &cmd, NULL);
496 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
501 if (mmc->version != SD_VERSION_2)
502 mmc->version = SD_VERSION_1_0;
504 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
505 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
506 cmd.resp_type = MMC_RSP_R3;
509 err = mmc_send_cmd(mmc, &cmd, NULL);
515 mmc->ocr = cmd.response[0];
517 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
523 static int mmc_send_op_cond(struct mmc *mmc)
529 /* Some cards seem to need this */
532 /* Asking to the card its capabilities */
533 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
534 cmd.resp_type = MMC_RSP_R3;
537 err = mmc_send_cmd(mmc, &cmd, NULL);
545 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
546 cmd.resp_type = MMC_RSP_R3;
547 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
549 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
550 (cmd.response[0] & OCR_ACCESS_MODE));
552 if (mmc->host_caps & MMC_MODE_HC)
553 cmd.cmdarg |= OCR_HCS;
555 err = mmc_send_cmd(mmc, &cmd, NULL);
561 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
566 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
567 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
568 cmd.resp_type = MMC_RSP_R3;
571 err = mmc_send_cmd(mmc, &cmd, NULL);
577 mmc->version = MMC_VERSION_UNKNOWN;
578 mmc->ocr = cmd.response[0];
580 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
587 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
590 struct mmc_data data;
593 /* Get the Card Status Register */
594 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
595 cmd.resp_type = MMC_RSP_R1;
598 data.dest = (char *)ext_csd;
600 data.blocksize = 512;
601 data.flags = MMC_DATA_READ;
603 err = mmc_send_cmd(mmc, &cmd, &data);
609 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
615 cmd.cmdidx = MMC_CMD_SWITCH;
616 cmd.resp_type = MMC_RSP_R1b;
617 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
621 ret = mmc_send_cmd(mmc, &cmd, NULL);
623 /* Waiting for the ready status */
625 ret = mmc_send_status(mmc, timeout);
631 static int mmc_change_freq(struct mmc *mmc)
633 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
639 if (mmc_host_is_spi(mmc))
642 /* Only version 4 supports high-speed */
643 if (mmc->version < MMC_VERSION_4)
646 err = mmc_send_ext_csd(mmc, ext_csd);
651 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
653 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
658 /* Now check to see that it worked */
659 err = mmc_send_ext_csd(mmc, ext_csd);
664 /* No high-speed support */
665 if (!ext_csd[EXT_CSD_HS_TIMING])
668 /* High Speed is set, there are two types: 52MHz and 26MHz */
669 if (cardtype & MMC_HS_52MHZ)
670 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
672 mmc->card_caps |= MMC_MODE_HS;
677 int mmc_switch_part(int dev_num, unsigned int part_num)
679 struct mmc *mmc = find_mmc_device(dev_num);
684 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
685 (mmc->part_config & ~PART_ACCESS_MASK)
686 | (part_num & PART_ACCESS_MASK));
689 int mmc_getcd(struct mmc *mmc)
693 cd = board_mmc_getcd(mmc);
695 if ((cd < 0) && mmc->getcd)
696 cd = mmc->getcd(mmc);
701 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
704 struct mmc_data data;
706 /* Switch the frequency */
707 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
708 cmd.resp_type = MMC_RSP_R1;
709 cmd.cmdarg = (mode << 31) | 0xffffff;
710 cmd.cmdarg &= ~(0xf << (group * 4));
711 cmd.cmdarg |= value << (group * 4);
713 data.dest = (char *)resp;
716 data.flags = MMC_DATA_READ;
718 return mmc_send_cmd(mmc, &cmd, &data);
722 static int sd_change_freq(struct mmc *mmc)
726 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
727 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
728 struct mmc_data data;
733 if (mmc_host_is_spi(mmc))
736 /* Read the SCR to find out if this card supports higher speeds */
737 cmd.cmdidx = MMC_CMD_APP_CMD;
738 cmd.resp_type = MMC_RSP_R1;
739 cmd.cmdarg = mmc->rca << 16;
741 err = mmc_send_cmd(mmc, &cmd, NULL);
746 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
747 cmd.resp_type = MMC_RSP_R1;
753 data.dest = (char *)scr;
756 data.flags = MMC_DATA_READ;
758 err = mmc_send_cmd(mmc, &cmd, &data);
767 mmc->scr[0] = __be32_to_cpu(scr[0]);
768 mmc->scr[1] = __be32_to_cpu(scr[1]);
770 switch ((mmc->scr[0] >> 24) & 0xf) {
772 mmc->version = SD_VERSION_1_0;
775 mmc->version = SD_VERSION_1_10;
778 mmc->version = SD_VERSION_2;
781 mmc->version = SD_VERSION_1_0;
785 if (mmc->scr[0] & SD_DATA_4BIT)
786 mmc->card_caps |= MMC_MODE_4BIT;
788 /* Version 1.0 doesn't support switching */
789 if (mmc->version == SD_VERSION_1_0)
794 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
795 (u8 *)switch_status);
800 /* The high-speed function is busy. Try again */
801 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
805 /* If high-speed isn't supported, we return */
806 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
810 * If the host doesn't support SD_HIGHSPEED, do not switch card to
811 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
812 * This can avoid furthur problem when the card runs in different
813 * mode between the host.
815 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
816 (mmc->host_caps & MMC_MODE_HS)))
819 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
824 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
825 mmc->card_caps |= MMC_MODE_HS;
830 /* frequency bases */
831 /* divided by 10 to be nice to platforms without floating point */
832 static const int fbase[] = {
839 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
840 * to platforms without floating point.
842 static const int multipliers[] = {
861 static void mmc_set_ios(struct mmc *mmc)
866 void mmc_set_clock(struct mmc *mmc, uint clock)
868 if (clock > mmc->f_max)
871 if (clock < mmc->f_min)
879 static void mmc_set_bus_width(struct mmc *mmc, uint width)
881 mmc->bus_width = width;
886 static int mmc_startup(struct mmc *mmc)
890 u64 cmult, csize, capacity;
892 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
893 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
896 #ifdef CONFIG_MMC_SPI_CRC_ON
897 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
898 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
899 cmd.resp_type = MMC_RSP_R1;
901 err = mmc_send_cmd(mmc, &cmd, NULL);
908 /* Put the Card in Identify Mode */
909 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
910 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
911 cmd.resp_type = MMC_RSP_R2;
914 err = mmc_send_cmd(mmc, &cmd, NULL);
919 memcpy(mmc->cid, cmd.response, 16);
922 * For MMC cards, set the Relative Address.
923 * For SD cards, get the Relatvie Address.
924 * This also puts the cards into Standby State
926 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
927 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
928 cmd.cmdarg = mmc->rca << 16;
929 cmd.resp_type = MMC_RSP_R6;
931 err = mmc_send_cmd(mmc, &cmd, NULL);
937 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
940 /* Get the Card-Specific Data */
941 cmd.cmdidx = MMC_CMD_SEND_CSD;
942 cmd.resp_type = MMC_RSP_R2;
943 cmd.cmdarg = mmc->rca << 16;
945 err = mmc_send_cmd(mmc, &cmd, NULL);
947 /* Waiting for the ready status */
948 mmc_send_status(mmc, timeout);
953 mmc->csd[0] = cmd.response[0];
954 mmc->csd[1] = cmd.response[1];
955 mmc->csd[2] = cmd.response[2];
956 mmc->csd[3] = cmd.response[3];
958 if (mmc->version == MMC_VERSION_UNKNOWN) {
959 int version = (cmd.response[0] >> 26) & 0xf;
963 mmc->version = MMC_VERSION_1_2;
966 mmc->version = MMC_VERSION_1_4;
969 mmc->version = MMC_VERSION_2_2;
972 mmc->version = MMC_VERSION_3;
975 mmc->version = MMC_VERSION_4;
978 mmc->version = MMC_VERSION_1_2;
983 /* divide frequency by 10, since the mults are 10x bigger */
984 freq = fbase[(cmd.response[0] & 0x7)];
985 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
987 mmc->tran_speed = freq * mult;
989 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
992 mmc->write_bl_len = mmc->read_bl_len;
994 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
996 if (mmc->high_capacity) {
997 csize = (mmc->csd[1] & 0x3f) << 16
998 | (mmc->csd[2] & 0xffff0000) >> 16;
1001 csize = (mmc->csd[1] & 0x3ff) << 2
1002 | (mmc->csd[2] & 0xc0000000) >> 30;
1003 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1006 mmc->capacity = (csize + 1) << (cmult + 2);
1007 mmc->capacity *= mmc->read_bl_len;
1009 if (mmc->read_bl_len > 512)
1010 mmc->read_bl_len = 512;
1012 if (mmc->write_bl_len > 512)
1013 mmc->write_bl_len = 512;
1015 /* Select the card, and put it into Transfer Mode */
1016 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1017 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1018 cmd.resp_type = MMC_RSP_R1;
1019 cmd.cmdarg = mmc->rca << 16;
1020 err = mmc_send_cmd(mmc, &cmd, NULL);
1027 * For SD, its erase group is always one sector
1029 mmc->erase_grp_size = 1;
1030 mmc->part_config = MMCPART_NOAVAILABLE;
1031 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1032 /* check ext_csd version and capacity */
1033 err = mmc_send_ext_csd(mmc, ext_csd);
1034 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1036 * According to the JEDEC Standard, the value of
1037 * ext_csd's capacity is valid if the value is more
1040 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1041 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1042 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1043 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1045 if ((capacity >> 20) > 2 * 1024)
1046 mmc->capacity = capacity;
1050 * Check whether GROUP_DEF is set, if yes, read out
1051 * group size from ext_csd directly, or calculate
1052 * the group size from the csd value.
1054 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1055 mmc->erase_grp_size =
1056 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
1058 int erase_gsz, erase_gmul;
1059 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1060 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1061 mmc->erase_grp_size = (erase_gsz + 1)
1065 /* store the partition info of emmc */
1066 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1067 ext_csd[EXT_CSD_BOOT_MULT])
1068 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1072 err = sd_change_freq(mmc);
1074 err = mmc_change_freq(mmc);
1079 /* Restrict card's capabilities by what the host can do */
1080 mmc->card_caps &= mmc->host_caps;
1083 if (mmc->card_caps & MMC_MODE_4BIT) {
1084 cmd.cmdidx = MMC_CMD_APP_CMD;
1085 cmd.resp_type = MMC_RSP_R1;
1086 cmd.cmdarg = mmc->rca << 16;
1088 err = mmc_send_cmd(mmc, &cmd, NULL);
1092 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1093 cmd.resp_type = MMC_RSP_R1;
1095 err = mmc_send_cmd(mmc, &cmd, NULL);
1099 mmc_set_bus_width(mmc, 4);
1102 if (mmc->card_caps & MMC_MODE_HS)
1103 mmc->tran_speed = 50000000;
1105 mmc->tran_speed = 25000000;
1109 /* An array of possible bus widths in order of preference */
1110 static unsigned ext_csd_bits[] = {
1111 EXT_CSD_BUS_WIDTH_8,
1112 EXT_CSD_BUS_WIDTH_4,
1113 EXT_CSD_BUS_WIDTH_1,
1116 /* An array to map CSD bus widths to host cap bits */
1117 static unsigned ext_to_hostcaps[] = {
1118 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1119 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1122 /* An array to map chosen bus width to an integer */
1123 static unsigned widths[] = {
1127 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1128 unsigned int extw = ext_csd_bits[idx];
1131 * Check to make sure the controller supports
1132 * this bus width, if it's more than 1
1134 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1135 !(mmc->host_caps & ext_to_hostcaps[extw]))
1138 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1139 EXT_CSD_BUS_WIDTH, extw);
1144 mmc_set_bus_width(mmc, widths[idx]);
1146 err = mmc_send_ext_csd(mmc, test_csd);
1147 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1148 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1149 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1150 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1151 && ext_csd[EXT_CSD_REV] \
1152 == test_csd[EXT_CSD_REV]
1153 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1154 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1155 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1156 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1158 mmc->card_caps |= ext_to_hostcaps[extw];
1163 if (mmc->card_caps & MMC_MODE_HS) {
1164 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1165 mmc->tran_speed = 52000000;
1167 mmc->tran_speed = 26000000;
1171 mmc_set_clock(mmc, mmc->tran_speed);
1173 /* fill in device description */
1174 mmc->block_dev.lun = 0;
1175 mmc->block_dev.type = 0;
1176 mmc->block_dev.blksz = mmc->read_bl_len;
1177 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1178 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1179 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1180 (mmc->cid[3] >> 16) & 0xffff);
1181 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1182 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1183 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1184 (mmc->cid[2] >> 24) & 0xff);
1185 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1186 (mmc->cid[2] >> 16) & 0xf);
1187 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1188 init_part(&mmc->block_dev);
1194 static int mmc_send_if_cond(struct mmc *mmc)
1199 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1200 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1201 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1202 cmd.resp_type = MMC_RSP_R7;
1204 err = mmc_send_cmd(mmc, &cmd, NULL);
1209 if ((cmd.response[0] & 0xff) != 0xaa)
1210 return UNUSABLE_ERR;
1212 mmc->version = SD_VERSION_2;
1217 int mmc_register(struct mmc *mmc)
1219 /* Setup the universal parts of the block interface just once */
1220 mmc->block_dev.if_type = IF_TYPE_MMC;
1221 mmc->block_dev.dev = cur_dev_num++;
1222 mmc->block_dev.removable = 1;
1223 mmc->block_dev.block_read = mmc_bread;
1224 mmc->block_dev.block_write = mmc_bwrite;
1225 mmc->block_dev.block_erase = mmc_berase;
1227 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1229 INIT_LIST_HEAD (&mmc->link);
1231 list_add_tail (&mmc->link, &mmc_devices);
1236 #ifdef CONFIG_PARTITIONS
1237 block_dev_desc_t *mmc_get_dev(int dev)
1239 struct mmc *mmc = find_mmc_device(dev);
1240 if (!mmc || mmc_init(mmc))
1243 return &mmc->block_dev;
1247 int mmc_init(struct mmc *mmc)
1251 if (mmc_getcd(mmc) == 0) {
1253 printf("MMC: no card present\n");
1260 err = mmc->init(mmc);
1265 mmc_set_bus_width(mmc, 1);
1266 mmc_set_clock(mmc, 1);
1268 /* Reset the Card */
1269 err = mmc_go_idle(mmc);
1274 /* The internal partition reset to user partition(0) at every CMD0*/
1277 /* Test for SD version 2 */
1278 err = mmc_send_if_cond(mmc);
1280 /* Now try to get the SD card's operating condition */
1281 err = sd_send_op_cond(mmc);
1283 /* If the command timed out, we check for an MMC card */
1284 if (err == TIMEOUT) {
1285 err = mmc_send_op_cond(mmc);
1288 printf("Card did not respond to voltage select!\n");
1289 return UNUSABLE_ERR;
1293 err = mmc_startup(mmc);
1302 * CPU and board-specific MMC initializations. Aliased function
1303 * signals caller to move on
1305 static int __def_mmc_init(bd_t *bis)
1310 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1311 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1313 void print_mmc_devices(char separator)
1316 struct list_head *entry;
1318 list_for_each(entry, &mmc_devices) {
1319 m = list_entry(entry, struct mmc, link);
1321 printf("%s: %d", m->name, m->block_dev.dev);
1323 if (entry->next != &mmc_devices)
1324 printf("%c ", separator);
1330 int get_mmc_num(void)
1335 int mmc_initialize(bd_t *bis)
1337 INIT_LIST_HEAD (&mmc_devices);
1340 if (board_mmc_init(bis) < 0)
1343 print_mmc_devices(',');