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 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
51 struct mmc_data *data)
53 struct mmc_data backup;
56 memset(&backup, 0, sizeof(backup));
58 #ifdef CONFIG_MMC_TRACE
62 printf("CMD_SEND:%d\n", cmd->cmdidx);
63 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
64 ret = mmc->send_cmd(mmc, cmd, data);
65 switch (cmd->resp_type) {
67 printf("\t\tMMC_RSP_NONE\n");
70 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
74 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
78 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
80 printf("\t\t \t\t 0x%08X \n",
82 printf("\t\t \t\t 0x%08X \n",
84 printf("\t\t \t\t 0x%08X \n",
87 printf("\t\t\t\t\tDUMPING DATA\n");
88 for (i = 0; i < 4; i++) {
90 printf("\t\t\t\t\t%03d - ", i*4);
91 ptr = (u8 *)&cmd->response[i];
93 for (j = 0; j < 4; j++)
94 printf("%02X ", *ptr--);
99 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
103 printf("\t\tERROR MMC rsp not supported\n");
107 ret = mmc->send_cmd(mmc, cmd, data);
112 static int mmc_send_status(struct mmc *mmc, int timeout)
115 int err, retries = 5;
116 #ifdef CONFIG_MMC_TRACE
120 cmd.cmdidx = MMC_CMD_SEND_STATUS;
121 cmd.resp_type = MMC_RSP_R1;
122 if (!mmc_host_is_spi(mmc))
123 cmd.cmdarg = mmc->rca << 16;
126 err = mmc_send_cmd(mmc, &cmd, NULL);
128 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
129 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
132 else if (cmd.response[0] & MMC_STATUS_MASK) {
133 printf("Status Error: 0x%08X\n",
137 } else if (--retries < 0)
144 #ifdef CONFIG_MMC_TRACE
145 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
146 printf("CURR STATE:%d\n", status);
149 printf("Timeout waiting card ready\n");
156 static int mmc_set_blocklen(struct mmc *mmc, int len)
160 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
161 cmd.resp_type = MMC_RSP_R1;
164 return mmc_send_cmd(mmc, &cmd, NULL);
167 struct mmc *find_mmc_device(int dev_num)
170 struct list_head *entry;
172 list_for_each(entry, &mmc_devices) {
173 m = list_entry(entry, struct mmc, link);
175 if (m->block_dev.dev == dev_num)
179 printf("MMC Device %d not found\n", dev_num);
184 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
188 int err, start_cmd, end_cmd;
190 if (mmc->high_capacity)
191 end = start + blkcnt - 1;
193 end = (start + blkcnt - 1) * mmc->write_bl_len;
194 start *= mmc->write_bl_len;
198 start_cmd = SD_CMD_ERASE_WR_BLK_START;
199 end_cmd = SD_CMD_ERASE_WR_BLK_END;
201 start_cmd = MMC_CMD_ERASE_GROUP_START;
202 end_cmd = MMC_CMD_ERASE_GROUP_END;
205 cmd.cmdidx = start_cmd;
207 cmd.resp_type = MMC_RSP_R1;
209 err = mmc_send_cmd(mmc, &cmd, NULL);
213 cmd.cmdidx = end_cmd;
216 err = mmc_send_cmd(mmc, &cmd, NULL);
220 cmd.cmdidx = MMC_CMD_ERASE;
221 cmd.cmdarg = SECURE_ERASE;
222 cmd.resp_type = MMC_RSP_R1b;
224 err = mmc_send_cmd(mmc, &cmd, NULL);
231 puts("mmc erase failed\n");
236 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
239 struct mmc *mmc = find_mmc_device(dev_num);
240 lbaint_t blk = 0, blk_r = 0;
246 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
247 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
248 "The erase range would be change to 0x%lx~0x%lx\n\n",
249 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
250 ((start + blkcnt + mmc->erase_grp_size)
251 & ~(mmc->erase_grp_size - 1)) - 1);
253 while (blk < blkcnt) {
254 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
255 mmc->erase_grp_size : (blkcnt - blk);
256 err = mmc_erase_t(mmc, start + blk, blk_r);
262 /* Waiting for the ready status */
263 if (mmc_send_status(mmc, timeout))
271 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
274 struct mmc_data data;
277 if ((start + blkcnt) > mmc->block_dev.lba) {
278 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
279 start + blkcnt, mmc->block_dev.lba);
284 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
286 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
288 if (mmc->high_capacity)
291 cmd.cmdarg = start * mmc->write_bl_len;
293 cmd.resp_type = MMC_RSP_R1;
296 data.blocks = blkcnt;
297 data.blocksize = mmc->write_bl_len;
298 data.flags = MMC_DATA_WRITE;
300 if (mmc_send_cmd(mmc, &cmd, &data)) {
301 printf("mmc write failed\n");
305 /* SPI multiblock writes terminate using a special
306 * token, not a STOP_TRANSMISSION request.
308 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
309 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
311 cmd.resp_type = MMC_RSP_R1b;
312 if (mmc_send_cmd(mmc, &cmd, NULL)) {
313 printf("mmc fail to send stop cmd\n");
318 /* Waiting for the ready status */
319 if (mmc_send_status(mmc, timeout))
326 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
328 lbaint_t cur, blocks_todo = blkcnt;
330 struct mmc *mmc = find_mmc_device(dev_num);
334 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
338 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
339 if(mmc_write_blocks(mmc, start, cur, src) != cur)
343 src += cur * mmc->write_bl_len;
344 } while (blocks_todo > 0);
349 static int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start,
353 struct mmc_data data;
356 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
358 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
360 if (mmc->high_capacity)
363 cmd.cmdarg = start * mmc->read_bl_len;
365 cmd.resp_type = MMC_RSP_R1;
368 data.blocks = blkcnt;
369 data.blocksize = mmc->read_bl_len;
370 data.flags = MMC_DATA_READ;
372 if (mmc_send_cmd(mmc, &cmd, &data))
376 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
378 cmd.resp_type = MMC_RSP_R1b;
379 if (mmc_send_cmd(mmc, &cmd, NULL)) {
380 printf("mmc fail to send stop cmd\n");
388 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
390 lbaint_t cur, blocks_todo = blkcnt;
395 struct mmc *mmc = find_mmc_device(dev_num);
399 if ((start + blkcnt) > mmc->block_dev.lba) {
400 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
401 start + blkcnt, mmc->block_dev.lba);
405 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
409 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
410 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
414 dst += cur * mmc->read_bl_len;
415 } while (blocks_todo > 0);
420 static int mmc_go_idle(struct mmc *mmc)
427 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
429 cmd.resp_type = MMC_RSP_NONE;
431 err = mmc_send_cmd(mmc, &cmd, NULL);
441 static int sd_send_op_cond(struct mmc *mmc)
448 cmd.cmdidx = MMC_CMD_APP_CMD;
449 cmd.resp_type = MMC_RSP_R1;
452 err = mmc_send_cmd(mmc, &cmd, NULL);
457 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
458 cmd.resp_type = MMC_RSP_R3;
461 * Most cards do not answer if some reserved bits
462 * in the ocr are set. However, Some controller
463 * can set bit 7 (reserved for low voltages), but
464 * how to manage low voltages SD card is not yet
467 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
468 (mmc->voltages & 0xff8000);
470 if (mmc->version == SD_VERSION_2)
471 cmd.cmdarg |= OCR_HCS;
473 err = mmc_send_cmd(mmc, &cmd, NULL);
479 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
484 if (mmc->version != SD_VERSION_2)
485 mmc->version = SD_VERSION_1_0;
487 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
488 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
489 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 static 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;
520 err = mmc_send_cmd(mmc, &cmd, NULL);
528 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
529 cmd.resp_type = MMC_RSP_R3;
530 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
532 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
533 (cmd.response[0] & OCR_ACCESS_MODE));
535 if (mmc->host_caps & MMC_MODE_HC)
536 cmd.cmdarg |= OCR_HCS;
538 err = mmc_send_cmd(mmc, &cmd, NULL);
544 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
549 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
550 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
551 cmd.resp_type = MMC_RSP_R3;
554 err = mmc_send_cmd(mmc, &cmd, NULL);
560 mmc->version = MMC_VERSION_UNKNOWN;
561 mmc->ocr = cmd.response[0];
563 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
570 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
573 struct mmc_data data;
576 /* Get the Card Status Register */
577 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
578 cmd.resp_type = MMC_RSP_R1;
581 data.dest = (char *)ext_csd;
583 data.blocksize = 512;
584 data.flags = MMC_DATA_READ;
586 err = mmc_send_cmd(mmc, &cmd, &data);
592 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
598 cmd.cmdidx = MMC_CMD_SWITCH;
599 cmd.resp_type = MMC_RSP_R1b;
600 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
604 ret = mmc_send_cmd(mmc, &cmd, NULL);
606 /* Waiting for the ready status */
608 ret = mmc_send_status(mmc, timeout);
614 static int mmc_change_freq(struct mmc *mmc)
616 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
622 if (mmc_host_is_spi(mmc))
625 /* Only version 4 supports high-speed */
626 if (mmc->version < MMC_VERSION_4)
629 err = mmc_send_ext_csd(mmc, ext_csd);
634 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
636 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
641 /* Now check to see that it worked */
642 err = mmc_send_ext_csd(mmc, ext_csd);
647 /* No high-speed support */
648 if (!ext_csd[EXT_CSD_HS_TIMING])
651 /* High Speed is set, there are two types: 52MHz and 26MHz */
652 if (cardtype & MMC_HS_52MHZ)
653 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
655 mmc->card_caps |= MMC_MODE_HS;
660 int mmc_switch_part(int dev_num, unsigned int part_num)
662 struct mmc *mmc = find_mmc_device(dev_num);
667 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
668 (mmc->part_config & ~PART_ACCESS_MASK)
669 | (part_num & PART_ACCESS_MASK));
672 int mmc_getcd(struct mmc *mmc)
676 cd = board_mmc_getcd(mmc);
678 if ((cd < 0) && mmc->getcd)
679 cd = mmc->getcd(mmc);
684 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
687 struct mmc_data data;
689 /* Switch the frequency */
690 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
691 cmd.resp_type = MMC_RSP_R1;
692 cmd.cmdarg = (mode << 31) | 0xffffff;
693 cmd.cmdarg &= ~(0xf << (group * 4));
694 cmd.cmdarg |= value << (group * 4);
696 data.dest = (char *)resp;
699 data.flags = MMC_DATA_READ;
701 return mmc_send_cmd(mmc, &cmd, &data);
705 static int sd_change_freq(struct mmc *mmc)
709 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
710 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
711 struct mmc_data data;
716 if (mmc_host_is_spi(mmc))
719 /* Read the SCR to find out if this card supports higher speeds */
720 cmd.cmdidx = MMC_CMD_APP_CMD;
721 cmd.resp_type = MMC_RSP_R1;
722 cmd.cmdarg = mmc->rca << 16;
724 err = mmc_send_cmd(mmc, &cmd, NULL);
729 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
730 cmd.resp_type = MMC_RSP_R1;
736 data.dest = (char *)scr;
739 data.flags = MMC_DATA_READ;
741 err = mmc_send_cmd(mmc, &cmd, &data);
750 mmc->scr[0] = __be32_to_cpu(scr[0]);
751 mmc->scr[1] = __be32_to_cpu(scr[1]);
753 switch ((mmc->scr[0] >> 24) & 0xf) {
755 mmc->version = SD_VERSION_1_0;
758 mmc->version = SD_VERSION_1_10;
761 mmc->version = SD_VERSION_2;
764 mmc->version = SD_VERSION_1_0;
768 if (mmc->scr[0] & SD_DATA_4BIT)
769 mmc->card_caps |= MMC_MODE_4BIT;
771 /* Version 1.0 doesn't support switching */
772 if (mmc->version == SD_VERSION_1_0)
777 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
778 (u8 *)switch_status);
783 /* The high-speed function is busy. Try again */
784 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
788 /* If high-speed isn't supported, we return */
789 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
793 * If the host doesn't support SD_HIGHSPEED, do not switch card to
794 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
795 * This can avoid furthur problem when the card runs in different
796 * mode between the host.
798 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
799 (mmc->host_caps & MMC_MODE_HS)))
802 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
807 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
808 mmc->card_caps |= MMC_MODE_HS;
813 /* frequency bases */
814 /* divided by 10 to be nice to platforms without floating point */
815 static const int fbase[] = {
822 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
823 * to platforms without floating point.
825 static const int multipliers[] = {
844 static void mmc_set_ios(struct mmc *mmc)
849 void mmc_set_clock(struct mmc *mmc, uint clock)
851 if (clock > mmc->f_max)
854 if (clock < mmc->f_min)
862 static void mmc_set_bus_width(struct mmc *mmc, uint width)
864 mmc->bus_width = width;
869 static int mmc_startup(struct mmc *mmc)
873 u64 cmult, csize, capacity;
875 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
876 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
879 #ifdef CONFIG_MMC_SPI_CRC_ON
880 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
881 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
882 cmd.resp_type = MMC_RSP_R1;
884 err = mmc_send_cmd(mmc, &cmd, NULL);
891 /* Put the Card in Identify Mode */
892 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
893 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
894 cmd.resp_type = MMC_RSP_R2;
897 err = mmc_send_cmd(mmc, &cmd, NULL);
902 memcpy(mmc->cid, cmd.response, 16);
905 * For MMC cards, set the Relative Address.
906 * For SD cards, get the Relatvie Address.
907 * This also puts the cards into Standby State
909 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
910 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
911 cmd.cmdarg = mmc->rca << 16;
912 cmd.resp_type = MMC_RSP_R6;
914 err = mmc_send_cmd(mmc, &cmd, NULL);
920 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
923 /* Get the Card-Specific Data */
924 cmd.cmdidx = MMC_CMD_SEND_CSD;
925 cmd.resp_type = MMC_RSP_R2;
926 cmd.cmdarg = mmc->rca << 16;
928 err = mmc_send_cmd(mmc, &cmd, NULL);
930 /* Waiting for the ready status */
931 mmc_send_status(mmc, timeout);
936 mmc->csd[0] = cmd.response[0];
937 mmc->csd[1] = cmd.response[1];
938 mmc->csd[2] = cmd.response[2];
939 mmc->csd[3] = cmd.response[3];
941 if (mmc->version == MMC_VERSION_UNKNOWN) {
942 int version = (cmd.response[0] >> 26) & 0xf;
946 mmc->version = MMC_VERSION_1_2;
949 mmc->version = MMC_VERSION_1_4;
952 mmc->version = MMC_VERSION_2_2;
955 mmc->version = MMC_VERSION_3;
958 mmc->version = MMC_VERSION_4;
961 mmc->version = MMC_VERSION_1_2;
966 /* divide frequency by 10, since the mults are 10x bigger */
967 freq = fbase[(cmd.response[0] & 0x7)];
968 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
970 mmc->tran_speed = freq * mult;
972 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
975 mmc->write_bl_len = mmc->read_bl_len;
977 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
979 if (mmc->high_capacity) {
980 csize = (mmc->csd[1] & 0x3f) << 16
981 | (mmc->csd[2] & 0xffff0000) >> 16;
984 csize = (mmc->csd[1] & 0x3ff) << 2
985 | (mmc->csd[2] & 0xc0000000) >> 30;
986 cmult = (mmc->csd[2] & 0x00038000) >> 15;
989 mmc->capacity = (csize + 1) << (cmult + 2);
990 mmc->capacity *= mmc->read_bl_len;
992 if (mmc->read_bl_len > 512)
993 mmc->read_bl_len = 512;
995 if (mmc->write_bl_len > 512)
996 mmc->write_bl_len = 512;
998 /* Select the card, and put it into Transfer Mode */
999 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1000 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1001 cmd.resp_type = MMC_RSP_R1;
1002 cmd.cmdarg = mmc->rca << 16;
1003 err = mmc_send_cmd(mmc, &cmd, NULL);
1010 * For SD, its erase group is always one sector
1012 mmc->erase_grp_size = 1;
1013 mmc->part_config = MMCPART_NOAVAILABLE;
1014 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1015 /* check ext_csd version and capacity */
1016 err = mmc_send_ext_csd(mmc, ext_csd);
1017 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1019 * According to the JEDEC Standard, the value of
1020 * ext_csd's capacity is valid if the value is more
1023 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1024 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1025 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1026 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1028 if ((capacity >> 20) > 2 * 1024)
1029 mmc->capacity = capacity;
1033 * Check whether GROUP_DEF is set, if yes, read out
1034 * group size from ext_csd directly, or calculate
1035 * the group size from the csd value.
1037 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1038 mmc->erase_grp_size =
1039 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
1041 int erase_gsz, erase_gmul;
1042 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1043 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1044 mmc->erase_grp_size = (erase_gsz + 1)
1048 /* store the partition info of emmc */
1049 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1050 ext_csd[EXT_CSD_BOOT_MULT])
1051 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1055 err = sd_change_freq(mmc);
1057 err = mmc_change_freq(mmc);
1062 /* Restrict card's capabilities by what the host can do */
1063 mmc->card_caps &= mmc->host_caps;
1066 if (mmc->card_caps & MMC_MODE_4BIT) {
1067 cmd.cmdidx = MMC_CMD_APP_CMD;
1068 cmd.resp_type = MMC_RSP_R1;
1069 cmd.cmdarg = mmc->rca << 16;
1071 err = mmc_send_cmd(mmc, &cmd, NULL);
1075 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1076 cmd.resp_type = MMC_RSP_R1;
1078 err = mmc_send_cmd(mmc, &cmd, NULL);
1082 mmc_set_bus_width(mmc, 4);
1085 if (mmc->card_caps & MMC_MODE_HS)
1086 mmc->tran_speed = 50000000;
1088 mmc->tran_speed = 25000000;
1092 /* An array of possible bus widths in order of preference */
1093 static unsigned ext_csd_bits[] = {
1094 EXT_CSD_BUS_WIDTH_8,
1095 EXT_CSD_BUS_WIDTH_4,
1096 EXT_CSD_BUS_WIDTH_1,
1099 /* An array to map CSD bus widths to host cap bits */
1100 static unsigned ext_to_hostcaps[] = {
1101 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1102 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1105 /* An array to map chosen bus width to an integer */
1106 static unsigned widths[] = {
1110 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1111 unsigned int extw = ext_csd_bits[idx];
1114 * Check to make sure the controller supports
1115 * this bus width, if it's more than 1
1117 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1118 !(mmc->host_caps & ext_to_hostcaps[extw]))
1121 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1122 EXT_CSD_BUS_WIDTH, extw);
1127 mmc_set_bus_width(mmc, widths[idx]);
1129 err = mmc_send_ext_csd(mmc, test_csd);
1130 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1131 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1132 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1133 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1134 && ext_csd[EXT_CSD_REV] \
1135 == test_csd[EXT_CSD_REV]
1136 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1137 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1138 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1139 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1141 mmc->card_caps |= ext_to_hostcaps[extw];
1146 if (mmc->card_caps & MMC_MODE_HS) {
1147 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1148 mmc->tran_speed = 52000000;
1150 mmc->tran_speed = 26000000;
1154 mmc_set_clock(mmc, mmc->tran_speed);
1156 /* fill in device description */
1157 mmc->block_dev.lun = 0;
1158 mmc->block_dev.type = 0;
1159 mmc->block_dev.blksz = mmc->read_bl_len;
1160 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1161 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1162 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1163 (mmc->cid[3] >> 16) & 0xffff);
1164 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1165 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1166 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1167 (mmc->cid[2] >> 24) & 0xff);
1168 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1169 (mmc->cid[2] >> 16) & 0xf);
1170 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1171 init_part(&mmc->block_dev);
1177 static int mmc_send_if_cond(struct mmc *mmc)
1182 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1183 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1184 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1185 cmd.resp_type = MMC_RSP_R7;
1187 err = mmc_send_cmd(mmc, &cmd, NULL);
1192 if ((cmd.response[0] & 0xff) != 0xaa)
1193 return UNUSABLE_ERR;
1195 mmc->version = SD_VERSION_2;
1200 int mmc_register(struct mmc *mmc)
1202 /* Setup the universal parts of the block interface just once */
1203 mmc->block_dev.if_type = IF_TYPE_MMC;
1204 mmc->block_dev.dev = cur_dev_num++;
1205 mmc->block_dev.removable = 1;
1206 mmc->block_dev.block_read = mmc_bread;
1207 mmc->block_dev.block_write = mmc_bwrite;
1208 mmc->block_dev.block_erase = mmc_berase;
1210 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1212 INIT_LIST_HEAD (&mmc->link);
1214 list_add_tail (&mmc->link, &mmc_devices);
1219 #ifdef CONFIG_PARTITIONS
1220 block_dev_desc_t *mmc_get_dev(int dev)
1222 struct mmc *mmc = find_mmc_device(dev);
1223 if (!mmc || mmc_init(mmc))
1226 return &mmc->block_dev;
1230 int mmc_init(struct mmc *mmc)
1234 if (mmc_getcd(mmc) == 0) {
1236 printf("MMC: no card present\n");
1243 err = mmc->init(mmc);
1248 mmc_set_bus_width(mmc, 1);
1249 mmc_set_clock(mmc, 1);
1251 /* Reset the Card */
1252 err = mmc_go_idle(mmc);
1257 /* The internal partition reset to user partition(0) at every CMD0*/
1260 /* Test for SD version 2 */
1261 err = mmc_send_if_cond(mmc);
1263 /* Now try to get the SD card's operating condition */
1264 err = sd_send_op_cond(mmc);
1266 /* If the command timed out, we check for an MMC card */
1267 if (err == TIMEOUT) {
1268 err = mmc_send_op_cond(mmc);
1271 printf("Card did not respond to voltage select!\n");
1272 return UNUSABLE_ERR;
1276 err = mmc_startup(mmc);
1285 * CPU and board-specific MMC initializations. Aliased function
1286 * signals caller to move on
1288 static int __def_mmc_init(bd_t *bis)
1293 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1294 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1296 void print_mmc_devices(char separator)
1299 struct list_head *entry;
1301 list_for_each(entry, &mmc_devices) {
1302 m = list_entry(entry, struct mmc, link);
1304 printf("%s: %d", m->name, m->block_dev.dev);
1306 if (entry->next != &mmc_devices)
1307 printf("%c ", separator);
1313 int get_mmc_num(void)
1318 int mmc_initialize(bd_t *bis)
1320 INIT_LIST_HEAD (&mmc_devices);
1323 if (board_mmc_init(bis) < 0)
1326 print_mmc_devices(',');