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(struct mmc *mmc) {
47 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
48 alias("__board_mmc_getcd")));
50 #ifdef CONFIG_MMC_BOUNCE_BUFFER
51 static int mmc_bounce_need_bounce(struct mmc_data *orig)
55 if (orig->flags & MMC_DATA_READ)
56 addr = (ulong)orig->dest;
58 addr = (ulong)orig->src;
60 if (addr % ARCH_DMA_MINALIGN) {
61 debug("MMC: Unaligned data destination address %08lx!\n", addr);
65 len = (ulong)(orig->blocksize * orig->blocks);
66 if (len % ARCH_DMA_MINALIGN) {
67 debug("MMC: Unaligned data destination length %08lx!\n", len);
74 static int mmc_bounce_buffer_start(struct mmc_data *backup,
75 struct mmc_data *orig)
83 if (!mmc_bounce_need_bounce(orig))
86 memcpy(backup, orig, sizeof(struct mmc_data));
88 origlen = orig->blocksize * orig->blocks;
89 len = roundup(origlen, ARCH_DMA_MINALIGN);
90 buffer = memalign(ARCH_DMA_MINALIGN, len);
92 puts("MMC: Error allocating MMC bounce buffer!\n");
96 if (orig->flags & MMC_DATA_READ) {
99 memcpy(buffer, orig->src, origlen);
106 static void mmc_bounce_buffer_stop(struct mmc_data *backup,
107 struct mmc_data *orig)
114 if (!mmc_bounce_need_bounce(backup))
117 if (backup->flags & MMC_DATA_READ) {
118 len = backup->blocksize * backup->blocks;
119 memcpy(backup->dest, orig->dest, len);
121 orig->dest = backup->dest;
123 free((void *)orig->src);
124 orig->src = backup->src;
131 static inline int mmc_bounce_buffer_start(struct mmc_data *backup,
132 struct mmc_data *orig) { return 0; }
133 static inline void mmc_bounce_buffer_stop(struct mmc_data *backup,
134 struct mmc_data *orig) { }
137 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
139 struct mmc_data backup;
142 memset(&backup, 0, sizeof(backup));
144 ret = mmc_bounce_buffer_start(&backup, data);
148 #ifdef CONFIG_MMC_TRACE
152 printf("CMD_SEND:%d\n", cmd->cmdidx);
153 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
154 ret = mmc->send_cmd(mmc, cmd, data);
155 switch (cmd->resp_type) {
157 printf("\t\tMMC_RSP_NONE\n");
160 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
164 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
168 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
170 printf("\t\t \t\t 0x%08X \n",
172 printf("\t\t \t\t 0x%08X \n",
174 printf("\t\t \t\t 0x%08X \n",
177 printf("\t\t\t\t\tDUMPING DATA\n");
178 for (i = 0; i < 4; i++) {
180 printf("\t\t\t\t\t%03d - ", i*4);
181 ptr = (u8 *)&cmd->response[i];
183 for (j = 0; j < 4; j++)
184 printf("%02X ", *ptr--);
189 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
193 printf("\t\tERROR MMC rsp not supported\n");
197 ret = mmc->send_cmd(mmc, cmd, data);
199 mmc_bounce_buffer_stop(&backup, data);
203 int mmc_send_status(struct mmc *mmc, int timeout)
206 int err, retries = 5;
207 #ifdef CONFIG_MMC_TRACE
211 cmd.cmdidx = MMC_CMD_SEND_STATUS;
212 cmd.resp_type = MMC_RSP_R1;
213 if (!mmc_host_is_spi(mmc))
214 cmd.cmdarg = mmc->rca << 16;
217 err = mmc_send_cmd(mmc, &cmd, NULL);
219 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
220 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
223 else if (cmd.response[0] & MMC_STATUS_MASK) {
224 printf("Status Error: 0x%08X\n",
228 } else if (--retries < 0)
235 #ifdef CONFIG_MMC_TRACE
236 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
237 printf("CURR STATE:%d\n", status);
240 printf("Timeout waiting card ready\n");
247 int mmc_set_blocklen(struct mmc *mmc, int len)
251 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
252 cmd.resp_type = MMC_RSP_R1;
255 return mmc_send_cmd(mmc, &cmd, NULL);
258 struct mmc *find_mmc_device(int dev_num)
261 struct list_head *entry;
263 list_for_each(entry, &mmc_devices) {
264 m = list_entry(entry, struct mmc, link);
266 if (m->block_dev.dev == dev_num)
270 printf("MMC Device %d not found\n", dev_num);
275 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
279 int err, start_cmd, end_cmd;
281 if (mmc->high_capacity)
282 end = start + blkcnt - 1;
284 end = (start + blkcnt - 1) * mmc->write_bl_len;
285 start *= mmc->write_bl_len;
289 start_cmd = SD_CMD_ERASE_WR_BLK_START;
290 end_cmd = SD_CMD_ERASE_WR_BLK_END;
292 start_cmd = MMC_CMD_ERASE_GROUP_START;
293 end_cmd = MMC_CMD_ERASE_GROUP_END;
296 cmd.cmdidx = start_cmd;
298 cmd.resp_type = MMC_RSP_R1;
300 err = mmc_send_cmd(mmc, &cmd, NULL);
304 cmd.cmdidx = end_cmd;
307 err = mmc_send_cmd(mmc, &cmd, NULL);
311 cmd.cmdidx = MMC_CMD_ERASE;
312 cmd.cmdarg = SECURE_ERASE;
313 cmd.resp_type = MMC_RSP_R1b;
315 err = mmc_send_cmd(mmc, &cmd, NULL);
322 puts("mmc erase failed\n");
327 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
330 struct mmc *mmc = find_mmc_device(dev_num);
331 lbaint_t blk = 0, blk_r = 0;
337 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
338 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
339 "The erase range would be change to 0x%lx~0x%lx\n\n",
340 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
341 ((start + blkcnt + mmc->erase_grp_size)
342 & ~(mmc->erase_grp_size - 1)) - 1);
344 while (blk < blkcnt) {
345 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
346 mmc->erase_grp_size : (blkcnt - blk);
347 err = mmc_erase_t(mmc, start + blk, blk_r);
353 /* Waiting for the ready status */
354 if (mmc_send_status(mmc, timeout))
362 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
365 struct mmc_data data;
368 if ((start + blkcnt) > mmc->block_dev.lba) {
369 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
370 start + blkcnt, mmc->block_dev.lba);
375 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
377 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
379 if (mmc->high_capacity)
382 cmd.cmdarg = start * mmc->write_bl_len;
384 cmd.resp_type = MMC_RSP_R1;
387 data.blocks = blkcnt;
388 data.blocksize = mmc->write_bl_len;
389 data.flags = MMC_DATA_WRITE;
391 if (mmc_send_cmd(mmc, &cmd, &data)) {
392 printf("mmc write failed\n");
396 /* SPI multiblock writes terminate using a special
397 * token, not a STOP_TRANSMISSION request.
399 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
400 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
402 cmd.resp_type = MMC_RSP_R1b;
403 if (mmc_send_cmd(mmc, &cmd, NULL)) {
404 printf("mmc fail to send stop cmd\n");
409 /* Waiting for the ready status */
410 if (mmc_send_status(mmc, timeout))
417 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
419 lbaint_t cur, blocks_todo = blkcnt;
421 struct mmc *mmc = find_mmc_device(dev_num);
425 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
429 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
430 if(mmc_write_blocks(mmc, start, cur, src) != cur)
434 src += cur * mmc->write_bl_len;
435 } while (blocks_todo > 0);
440 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
443 struct mmc_data data;
446 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
448 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
450 if (mmc->high_capacity)
453 cmd.cmdarg = start * mmc->read_bl_len;
455 cmd.resp_type = MMC_RSP_R1;
458 data.blocks = blkcnt;
459 data.blocksize = mmc->read_bl_len;
460 data.flags = MMC_DATA_READ;
462 if (mmc_send_cmd(mmc, &cmd, &data))
466 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
468 cmd.resp_type = MMC_RSP_R1b;
469 if (mmc_send_cmd(mmc, &cmd, NULL)) {
470 printf("mmc fail to send stop cmd\n");
478 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
480 lbaint_t cur, blocks_todo = blkcnt;
485 struct mmc *mmc = find_mmc_device(dev_num);
489 if ((start + blkcnt) > mmc->block_dev.lba) {
490 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
491 start + blkcnt, mmc->block_dev.lba);
495 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
499 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
500 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
504 dst += cur * mmc->read_bl_len;
505 } while (blocks_todo > 0);
510 int mmc_go_idle(struct mmc* mmc)
517 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
519 cmd.resp_type = MMC_RSP_NONE;
521 err = mmc_send_cmd(mmc, &cmd, NULL);
532 sd_send_op_cond(struct mmc *mmc)
539 cmd.cmdidx = MMC_CMD_APP_CMD;
540 cmd.resp_type = MMC_RSP_R1;
543 err = mmc_send_cmd(mmc, &cmd, NULL);
548 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
549 cmd.resp_type = MMC_RSP_R3;
552 * Most cards do not answer if some reserved bits
553 * in the ocr are set. However, Some controller
554 * can set bit 7 (reserved for low voltages), but
555 * how to manage low voltages SD card is not yet
558 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
559 (mmc->voltages & 0xff8000);
561 if (mmc->version == SD_VERSION_2)
562 cmd.cmdarg |= OCR_HCS;
564 err = mmc_send_cmd(mmc, &cmd, NULL);
570 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
575 if (mmc->version != SD_VERSION_2)
576 mmc->version = SD_VERSION_1_0;
578 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
579 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
580 cmd.resp_type = MMC_RSP_R3;
583 err = mmc_send_cmd(mmc, &cmd, NULL);
589 mmc->ocr = cmd.response[0];
591 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
597 int mmc_send_op_cond(struct mmc *mmc)
603 /* Some cards seem to need this */
606 /* Asking to the card its capabilities */
607 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
608 cmd.resp_type = MMC_RSP_R3;
611 err = mmc_send_cmd(mmc, &cmd, NULL);
619 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
620 cmd.resp_type = MMC_RSP_R3;
621 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
623 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
624 (cmd.response[0] & OCR_ACCESS_MODE));
626 if (mmc->host_caps & MMC_MODE_HC)
627 cmd.cmdarg |= OCR_HCS;
629 err = mmc_send_cmd(mmc, &cmd, NULL);
635 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
640 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
641 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
642 cmd.resp_type = MMC_RSP_R3;
645 err = mmc_send_cmd(mmc, &cmd, NULL);
651 mmc->version = MMC_VERSION_UNKNOWN;
652 mmc->ocr = cmd.response[0];
654 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
661 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
664 struct mmc_data data;
667 /* Get the Card Status Register */
668 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
669 cmd.resp_type = MMC_RSP_R1;
674 data.blocksize = 512;
675 data.flags = MMC_DATA_READ;
677 err = mmc_send_cmd(mmc, &cmd, &data);
683 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
689 cmd.cmdidx = MMC_CMD_SWITCH;
690 cmd.resp_type = MMC_RSP_R1b;
691 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
695 ret = mmc_send_cmd(mmc, &cmd, NULL);
697 /* Waiting for the ready status */
699 ret = mmc_send_status(mmc, timeout);
705 int mmc_change_freq(struct mmc *mmc)
707 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
713 if (mmc_host_is_spi(mmc))
716 /* Only version 4 supports high-speed */
717 if (mmc->version < MMC_VERSION_4)
720 err = mmc_send_ext_csd(mmc, ext_csd);
725 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
727 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
732 /* Now check to see that it worked */
733 err = mmc_send_ext_csd(mmc, ext_csd);
738 /* No high-speed support */
739 if (!ext_csd[EXT_CSD_HS_TIMING])
742 /* High Speed is set, there are two types: 52MHz and 26MHz */
743 if (cardtype & MMC_HS_52MHZ)
744 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
746 mmc->card_caps |= MMC_MODE_HS;
751 int mmc_switch_part(int dev_num, unsigned int part_num)
753 struct mmc *mmc = find_mmc_device(dev_num);
758 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
759 (mmc->part_config & ~PART_ACCESS_MASK)
760 | (part_num & PART_ACCESS_MASK));
763 int mmc_getcd(struct mmc *mmc)
767 cd = board_mmc_getcd(mmc);
769 if ((cd < 0) && mmc->getcd)
770 cd = mmc->getcd(mmc);
775 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
778 struct mmc_data data;
780 /* Switch the frequency */
781 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
782 cmd.resp_type = MMC_RSP_R1;
783 cmd.cmdarg = (mode << 31) | 0xffffff;
784 cmd.cmdarg &= ~(0xf << (group * 4));
785 cmd.cmdarg |= value << (group * 4);
787 data.dest = (char *)resp;
790 data.flags = MMC_DATA_READ;
792 return mmc_send_cmd(mmc, &cmd, &data);
796 int sd_change_freq(struct mmc *mmc)
800 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
801 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
802 struct mmc_data data;
807 if (mmc_host_is_spi(mmc))
810 /* Read the SCR to find out if this card supports higher speeds */
811 cmd.cmdidx = MMC_CMD_APP_CMD;
812 cmd.resp_type = MMC_RSP_R1;
813 cmd.cmdarg = mmc->rca << 16;
815 err = mmc_send_cmd(mmc, &cmd, NULL);
820 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
821 cmd.resp_type = MMC_RSP_R1;
827 data.dest = (char *)scr;
830 data.flags = MMC_DATA_READ;
832 err = mmc_send_cmd(mmc, &cmd, &data);
841 mmc->scr[0] = __be32_to_cpu(scr[0]);
842 mmc->scr[1] = __be32_to_cpu(scr[1]);
844 switch ((mmc->scr[0] >> 24) & 0xf) {
846 mmc->version = SD_VERSION_1_0;
849 mmc->version = SD_VERSION_1_10;
852 mmc->version = SD_VERSION_2;
855 mmc->version = SD_VERSION_1_0;
859 if (mmc->scr[0] & SD_DATA_4BIT)
860 mmc->card_caps |= MMC_MODE_4BIT;
862 /* Version 1.0 doesn't support switching */
863 if (mmc->version == SD_VERSION_1_0)
868 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
869 (u8 *)switch_status);
874 /* The high-speed function is busy. Try again */
875 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
879 /* If high-speed isn't supported, we return */
880 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
884 * If the host doesn't support SD_HIGHSPEED, do not switch card to
885 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
886 * This can avoid furthur problem when the card runs in different
887 * mode between the host.
889 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
890 (mmc->host_caps & MMC_MODE_HS)))
893 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
898 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
899 mmc->card_caps |= MMC_MODE_HS;
904 /* frequency bases */
905 /* divided by 10 to be nice to platforms without floating point */
906 static const int fbase[] = {
913 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
914 * to platforms without floating point.
916 static const int multipliers[] = {
935 void mmc_set_ios(struct mmc *mmc)
940 void mmc_set_clock(struct mmc *mmc, uint clock)
942 if (clock > mmc->f_max)
945 if (clock < mmc->f_min)
953 void mmc_set_bus_width(struct mmc *mmc, uint width)
955 mmc->bus_width = width;
960 int mmc_startup(struct mmc *mmc)
964 u64 cmult, csize, capacity;
966 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512);
967 ALLOC_CACHE_ALIGN_BUFFER(char, test_csd, 512);
970 #ifdef CONFIG_MMC_SPI_CRC_ON
971 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
972 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
973 cmd.resp_type = MMC_RSP_R1;
975 err = mmc_send_cmd(mmc, &cmd, NULL);
982 /* Put the Card in Identify Mode */
983 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
984 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
985 cmd.resp_type = MMC_RSP_R2;
988 err = mmc_send_cmd(mmc, &cmd, NULL);
993 memcpy(mmc->cid, cmd.response, 16);
996 * For MMC cards, set the Relative Address.
997 * For SD cards, get the Relatvie Address.
998 * This also puts the cards into Standby State
1000 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1001 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1002 cmd.cmdarg = mmc->rca << 16;
1003 cmd.resp_type = MMC_RSP_R6;
1005 err = mmc_send_cmd(mmc, &cmd, NULL);
1011 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1014 /* Get the Card-Specific Data */
1015 cmd.cmdidx = MMC_CMD_SEND_CSD;
1016 cmd.resp_type = MMC_RSP_R2;
1017 cmd.cmdarg = mmc->rca << 16;
1019 err = mmc_send_cmd(mmc, &cmd, NULL);
1021 /* Waiting for the ready status */
1022 mmc_send_status(mmc, timeout);
1027 mmc->csd[0] = cmd.response[0];
1028 mmc->csd[1] = cmd.response[1];
1029 mmc->csd[2] = cmd.response[2];
1030 mmc->csd[3] = cmd.response[3];
1032 if (mmc->version == MMC_VERSION_UNKNOWN) {
1033 int version = (cmd.response[0] >> 26) & 0xf;
1037 mmc->version = MMC_VERSION_1_2;
1040 mmc->version = MMC_VERSION_1_4;
1043 mmc->version = MMC_VERSION_2_2;
1046 mmc->version = MMC_VERSION_3;
1049 mmc->version = MMC_VERSION_4;
1052 mmc->version = MMC_VERSION_1_2;
1057 /* divide frequency by 10, since the mults are 10x bigger */
1058 freq = fbase[(cmd.response[0] & 0x7)];
1059 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1061 mmc->tran_speed = freq * mult;
1063 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1066 mmc->write_bl_len = mmc->read_bl_len;
1068 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1070 if (mmc->high_capacity) {
1071 csize = (mmc->csd[1] & 0x3f) << 16
1072 | (mmc->csd[2] & 0xffff0000) >> 16;
1075 csize = (mmc->csd[1] & 0x3ff) << 2
1076 | (mmc->csd[2] & 0xc0000000) >> 30;
1077 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1080 mmc->capacity = (csize + 1) << (cmult + 2);
1081 mmc->capacity *= mmc->read_bl_len;
1083 if (mmc->read_bl_len > 512)
1084 mmc->read_bl_len = 512;
1086 if (mmc->write_bl_len > 512)
1087 mmc->write_bl_len = 512;
1089 /* Select the card, and put it into Transfer Mode */
1090 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1091 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1092 cmd.resp_type = MMC_RSP_R1;
1093 cmd.cmdarg = mmc->rca << 16;
1094 err = mmc_send_cmd(mmc, &cmd, NULL);
1101 * For SD, its erase group is always one sector
1103 mmc->erase_grp_size = 1;
1104 mmc->part_config = MMCPART_NOAVAILABLE;
1105 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1106 /* check ext_csd version and capacity */
1107 err = mmc_send_ext_csd(mmc, ext_csd);
1108 if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
1110 * According to the JEDEC Standard, the value of
1111 * ext_csd's capacity is valid if the value is more
1114 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1115 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1116 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1117 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1119 if ((capacity >> 20) > 2 * 1024)
1120 mmc->capacity = capacity;
1124 * Check whether GROUP_DEF is set, if yes, read out
1125 * group size from ext_csd directly, or calculate
1126 * the group size from the csd value.
1128 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1129 mmc->erase_grp_size =
1130 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
1132 int erase_gsz, erase_gmul;
1133 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1134 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1135 mmc->erase_grp_size = (erase_gsz + 1)
1139 /* store the partition info of emmc */
1140 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT)
1141 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1145 err = sd_change_freq(mmc);
1147 err = mmc_change_freq(mmc);
1152 /* Restrict card's capabilities by what the host can do */
1153 mmc->card_caps &= mmc->host_caps;
1156 if (mmc->card_caps & MMC_MODE_4BIT) {
1157 cmd.cmdidx = MMC_CMD_APP_CMD;
1158 cmd.resp_type = MMC_RSP_R1;
1159 cmd.cmdarg = mmc->rca << 16;
1161 err = mmc_send_cmd(mmc, &cmd, NULL);
1165 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1166 cmd.resp_type = MMC_RSP_R1;
1168 err = mmc_send_cmd(mmc, &cmd, NULL);
1172 mmc_set_bus_width(mmc, 4);
1175 if (mmc->card_caps & MMC_MODE_HS)
1176 mmc->tran_speed = 50000000;
1178 mmc->tran_speed = 25000000;
1180 width = ((mmc->host_caps & MMC_MODE_MASK_WIDTH_BITS) >>
1181 MMC_MODE_WIDTH_BITS_SHIFT);
1182 for (; width >= 0; width--) {
1183 /* Set the card to use 4 bit*/
1184 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1185 EXT_CSD_BUS_WIDTH, width);
1191 mmc_set_bus_width(mmc, 1);
1194 mmc_set_bus_width(mmc, 4 * width);
1196 err = mmc_send_ext_csd(mmc, test_csd);
1197 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1198 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1199 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1200 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1201 && ext_csd[EXT_CSD_REV] \
1202 == test_csd[EXT_CSD_REV]
1203 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1204 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1205 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1206 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1208 mmc->card_caps |= width;
1213 if (mmc->card_caps & MMC_MODE_HS) {
1214 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1215 mmc->tran_speed = 52000000;
1217 mmc->tran_speed = 26000000;
1221 mmc_set_clock(mmc, mmc->tran_speed);
1223 /* fill in device description */
1224 mmc->block_dev.lun = 0;
1225 mmc->block_dev.type = 0;
1226 mmc->block_dev.blksz = mmc->read_bl_len;
1227 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1228 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1229 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1230 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1231 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1232 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1233 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1234 (mmc->cid[2] >> 24) & 0xf);
1235 init_part(&mmc->block_dev);
1240 int mmc_send_if_cond(struct mmc *mmc)
1245 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1246 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1247 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1248 cmd.resp_type = MMC_RSP_R7;
1250 err = mmc_send_cmd(mmc, &cmd, NULL);
1255 if ((cmd.response[0] & 0xff) != 0xaa)
1256 return UNUSABLE_ERR;
1258 mmc->version = SD_VERSION_2;
1263 int mmc_register(struct mmc *mmc)
1265 /* Setup the universal parts of the block interface just once */
1266 mmc->block_dev.if_type = IF_TYPE_MMC;
1267 mmc->block_dev.dev = cur_dev_num++;
1268 mmc->block_dev.removable = 1;
1269 mmc->block_dev.block_read = mmc_bread;
1270 mmc->block_dev.block_write = mmc_bwrite;
1271 mmc->block_dev.block_erase = mmc_berase;
1273 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1275 INIT_LIST_HEAD (&mmc->link);
1277 list_add_tail (&mmc->link, &mmc_devices);
1282 #ifdef CONFIG_PARTITIONS
1283 block_dev_desc_t *mmc_get_dev(int dev)
1285 struct mmc *mmc = find_mmc_device(dev);
1290 return &mmc->block_dev;
1294 int mmc_init(struct mmc *mmc)
1298 if (mmc_getcd(mmc) == 0) {
1300 printf("MMC: no card present\n");
1307 err = mmc->init(mmc);
1312 mmc_set_bus_width(mmc, 1);
1313 mmc_set_clock(mmc, 1);
1315 /* Reset the Card */
1316 err = mmc_go_idle(mmc);
1321 /* The internal partition reset to user partition(0) at every CMD0*/
1324 /* Test for SD version 2 */
1325 err = mmc_send_if_cond(mmc);
1327 /* Now try to get the SD card's operating condition */
1328 err = sd_send_op_cond(mmc);
1330 /* If the command timed out, we check for an MMC card */
1331 if (err == TIMEOUT) {
1332 err = mmc_send_op_cond(mmc);
1335 printf("Card did not respond to voltage select!\n");
1336 return UNUSABLE_ERR;
1340 err = mmc_startup(mmc);
1349 * CPU and board-specific MMC initializations. Aliased function
1350 * signals caller to move on
1352 static int __def_mmc_init(bd_t *bis)
1357 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1358 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1360 void print_mmc_devices(char separator)
1363 struct list_head *entry;
1365 list_for_each(entry, &mmc_devices) {
1366 m = list_entry(entry, struct mmc, link);
1368 printf("%s: %d", m->name, m->block_dev.dev);
1370 if (entry->next != &mmc_devices)
1371 printf("%c ", separator);
1377 int get_mmc_num(void)
1382 int mmc_initialize(bd_t *bis)
1384 INIT_LIST_HEAD (&mmc_devices);
1387 if (board_mmc_init(bis) < 0)
1390 print_mmc_devices(',');