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;
122 err = mmc_send_cmd(mmc, &cmd, NULL);
125 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
130 if (cmd.response[0] & MMC_STATUS_MASK) {
131 printf("Status Error: 0x%08X\n", cmd.response[0]);
136 #ifdef CONFIG_MMC_TRACE
137 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
138 printf("CURR STATE:%d\n", status);
141 printf("Timeout waiting card ready\n");
148 int mmc_set_blocklen(struct mmc *mmc, int len)
152 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
153 cmd.resp_type = MMC_RSP_R1;
157 return mmc_send_cmd(mmc, &cmd, NULL);
160 struct mmc *find_mmc_device(int dev_num)
163 struct list_head *entry;
165 list_for_each(entry, &mmc_devices) {
166 m = list_entry(entry, struct mmc, link);
168 if (m->block_dev.dev == dev_num)
172 printf("MMC Device %d not found\n", dev_num);
177 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
181 int err, start_cmd, end_cmd;
183 if (mmc->high_capacity)
184 end = start + blkcnt - 1;
186 end = (start + blkcnt - 1) * mmc->write_bl_len;
187 start *= mmc->write_bl_len;
191 start_cmd = SD_CMD_ERASE_WR_BLK_START;
192 end_cmd = SD_CMD_ERASE_WR_BLK_END;
194 start_cmd = MMC_CMD_ERASE_GROUP_START;
195 end_cmd = MMC_CMD_ERASE_GROUP_END;
198 cmd.cmdidx = start_cmd;
200 cmd.resp_type = MMC_RSP_R1;
203 err = mmc_send_cmd(mmc, &cmd, NULL);
207 cmd.cmdidx = end_cmd;
210 err = mmc_send_cmd(mmc, &cmd, NULL);
214 cmd.cmdidx = MMC_CMD_ERASE;
215 cmd.cmdarg = SECURE_ERASE;
216 cmd.resp_type = MMC_RSP_R1b;
218 err = mmc_send_cmd(mmc, &cmd, NULL);
225 puts("mmc erase failed\n");
230 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
233 struct mmc *mmc = find_mmc_device(dev_num);
234 lbaint_t blk = 0, blk_r = 0;
239 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
240 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
241 "The erase range would be change to 0x%lx~0x%lx\n\n",
242 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
243 ((start + blkcnt + mmc->erase_grp_size)
244 & ~(mmc->erase_grp_size - 1)) - 1);
246 while (blk < blkcnt) {
247 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
248 mmc->erase_grp_size : (blkcnt - blk);
249 err = mmc_erase_t(mmc, start + blk, blk_r);
260 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
263 struct mmc_data data;
266 if ((start + blkcnt) > mmc->block_dev.lba) {
267 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
268 start + blkcnt, mmc->block_dev.lba);
273 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
275 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
277 if (mmc->high_capacity)
280 cmd.cmdarg = start * mmc->write_bl_len;
282 cmd.resp_type = MMC_RSP_R1;
286 data.blocks = blkcnt;
287 data.blocksize = mmc->write_bl_len;
288 data.flags = MMC_DATA_WRITE;
290 if (mmc_send_cmd(mmc, &cmd, &data)) {
291 printf("mmc write failed\n");
295 /* SPI multiblock writes terminate using a special
296 * token, not a STOP_TRANSMISSION request.
298 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
299 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
301 cmd.resp_type = MMC_RSP_R1b;
303 if (mmc_send_cmd(mmc, &cmd, NULL)) {
304 printf("mmc fail to send stop cmd\n");
308 /* Waiting for the ready status */
309 mmc_send_status(mmc, timeout);
316 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
318 lbaint_t cur, blocks_todo = blkcnt;
320 struct mmc *mmc = find_mmc_device(dev_num);
324 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
328 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
329 if(mmc_write_blocks(mmc, start, cur, src) != cur)
333 src += cur * mmc->write_bl_len;
334 } while (blocks_todo > 0);
339 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
342 struct mmc_data data;
346 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
348 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
350 if (mmc->high_capacity)
353 cmd.cmdarg = start * mmc->read_bl_len;
355 cmd.resp_type = MMC_RSP_R1;
359 data.blocks = blkcnt;
360 data.blocksize = mmc->read_bl_len;
361 data.flags = MMC_DATA_READ;
363 if (mmc_send_cmd(mmc, &cmd, &data))
367 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
369 cmd.resp_type = MMC_RSP_R1b;
371 if (mmc_send_cmd(mmc, &cmd, NULL)) {
372 printf("mmc fail to send stop cmd\n");
376 /* Waiting for the ready status */
377 mmc_send_status(mmc, timeout);
383 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
385 lbaint_t cur, blocks_todo = blkcnt;
390 struct mmc *mmc = find_mmc_device(dev_num);
394 if ((start + blkcnt) > mmc->block_dev.lba) {
395 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
396 start + blkcnt, mmc->block_dev.lba);
400 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
404 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
405 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
409 dst += cur * mmc->read_bl_len;
410 } while (blocks_todo > 0);
415 int mmc_go_idle(struct mmc* mmc)
422 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
424 cmd.resp_type = MMC_RSP_NONE;
427 err = mmc_send_cmd(mmc, &cmd, NULL);
438 sd_send_op_cond(struct mmc *mmc)
445 cmd.cmdidx = MMC_CMD_APP_CMD;
446 cmd.resp_type = MMC_RSP_R1;
450 err = mmc_send_cmd(mmc, &cmd, NULL);
455 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
456 cmd.resp_type = MMC_RSP_R3;
459 * Most cards do not answer if some reserved bits
460 * in the ocr are set. However, Some controller
461 * can set bit 7 (reserved for low voltages), but
462 * how to manage low voltages SD card is not yet
465 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
466 (mmc->voltages & 0xff8000);
468 if (mmc->version == SD_VERSION_2)
469 cmd.cmdarg |= OCR_HCS;
471 err = mmc_send_cmd(mmc, &cmd, NULL);
477 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
482 if (mmc->version != SD_VERSION_2)
483 mmc->version = SD_VERSION_1_0;
485 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
486 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
487 cmd.resp_type = MMC_RSP_R3;
491 err = mmc_send_cmd(mmc, &cmd, NULL);
497 mmc->ocr = cmd.response[0];
499 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
505 int mmc_send_op_cond(struct mmc *mmc)
511 /* Some cards seem to need this */
514 /* Asking to the card its capabilities */
515 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
516 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;
540 err = mmc_send_cmd(mmc, &cmd, NULL);
546 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
551 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
552 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
553 cmd.resp_type = MMC_RSP_R3;
557 err = mmc_send_cmd(mmc, &cmd, NULL);
563 mmc->version = MMC_VERSION_UNKNOWN;
564 mmc->ocr = cmd.response[0];
566 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
573 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
576 struct mmc_data data;
579 /* Get the Card Status Register */
580 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
581 cmd.resp_type = MMC_RSP_R1;
587 data.blocksize = 512;
588 data.flags = MMC_DATA_READ;
590 err = mmc_send_cmd(mmc, &cmd, &data);
596 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
602 cmd.cmdidx = MMC_CMD_SWITCH;
603 cmd.resp_type = MMC_RSP_R1b;
604 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
609 ret = mmc_send_cmd(mmc, &cmd, NULL);
611 /* Waiting for the ready status */
612 mmc_send_status(mmc, timeout);
618 int mmc_change_freq(struct mmc *mmc)
626 if (mmc_host_is_spi(mmc))
629 /* Only version 4 supports high-speed */
630 if (mmc->version < MMC_VERSION_4)
633 mmc->card_caps |= MMC_MODE_4BIT;
635 err = mmc_send_ext_csd(mmc, ext_csd);
640 cardtype = ext_csd[196] & 0xf;
642 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
647 /* Now check to see that it worked */
648 err = mmc_send_ext_csd(mmc, ext_csd);
653 /* No high-speed support */
657 /* High Speed is set, there are two types: 52MHz and 26MHz */
658 if (cardtype & MMC_HS_52MHZ)
659 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
661 mmc->card_caps |= MMC_MODE_HS;
666 int mmc_switch_part(int dev_num, unsigned int part_num)
668 struct mmc *mmc = find_mmc_device(dev_num);
673 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
674 (mmc->part_config & ~PART_ACCESS_MASK)
675 | (part_num & PART_ACCESS_MASK));
678 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
681 struct mmc_data data;
683 /* Switch the frequency */
684 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
685 cmd.resp_type = MMC_RSP_R1;
686 cmd.cmdarg = (mode << 31) | 0xffffff;
687 cmd.cmdarg &= ~(0xf << (group * 4));
688 cmd.cmdarg |= value << (group * 4);
691 data.dest = (char *)resp;
694 data.flags = MMC_DATA_READ;
696 return mmc_send_cmd(mmc, &cmd, &data);
700 int sd_change_freq(struct mmc *mmc)
705 uint switch_status[16];
706 struct mmc_data data;
711 if (mmc_host_is_spi(mmc))
714 /* Read the SCR to find out if this card supports higher speeds */
715 cmd.cmdidx = MMC_CMD_APP_CMD;
716 cmd.resp_type = MMC_RSP_R1;
717 cmd.cmdarg = mmc->rca << 16;
720 err = mmc_send_cmd(mmc, &cmd, NULL);
725 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
726 cmd.resp_type = MMC_RSP_R1;
733 data.dest = (char *)&scr;
736 data.flags = MMC_DATA_READ;
738 err = mmc_send_cmd(mmc, &cmd, &data);
747 mmc->scr[0] = __be32_to_cpu(scr[0]);
748 mmc->scr[1] = __be32_to_cpu(scr[1]);
750 switch ((mmc->scr[0] >> 24) & 0xf) {
752 mmc->version = SD_VERSION_1_0;
755 mmc->version = SD_VERSION_1_10;
758 mmc->version = SD_VERSION_2;
761 mmc->version = SD_VERSION_1_0;
765 if (mmc->scr[0] & SD_DATA_4BIT)
766 mmc->card_caps |= MMC_MODE_4BIT;
768 /* Version 1.0 doesn't support switching */
769 if (mmc->version == SD_VERSION_1_0)
774 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
775 (u8 *)&switch_status);
780 /* The high-speed function is busy. Try again */
781 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
785 /* If high-speed isn't supported, we return */
786 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
789 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
794 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
795 mmc->card_caps |= MMC_MODE_HS;
800 /* frequency bases */
801 /* divided by 10 to be nice to platforms without floating point */
802 static const int fbase[] = {
809 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
810 * to platforms without floating point.
812 static const int multipliers[] = {
831 void mmc_set_ios(struct mmc *mmc)
836 void mmc_set_clock(struct mmc *mmc, uint clock)
838 if (clock > mmc->f_max)
841 if (clock < mmc->f_min)
849 void mmc_set_bus_width(struct mmc *mmc, uint width)
851 mmc->bus_width = width;
856 int mmc_startup(struct mmc *mmc)
860 u64 cmult, csize, capacity;
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_R1b;
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[192] >= 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[212] << 0 | ext_csd[213] << 8 |
1015 ext_csd[214] << 16 | ext_csd[215] << 24;
1017 if ((capacity >> 20) > 2 * 1024)
1018 mmc->capacity = capacity;
1022 * Check whether GROUP_DEF is set, if yes, read out
1023 * group size from ext_csd directly, or calculate
1024 * the group size from the csd value.
1027 mmc->erase_grp_size = ext_csd[224] * 512 * 1024;
1029 int erase_gsz, erase_gmul;
1030 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1031 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1032 mmc->erase_grp_size = (erase_gsz + 1)
1036 /* store the partition info of emmc */
1037 if (ext_csd[160] & PART_SUPPORT)
1038 mmc->part_config = ext_csd[179];
1042 err = sd_change_freq(mmc);
1044 err = mmc_change_freq(mmc);
1049 /* Restrict card's capabilities by what the host can do */
1050 mmc->card_caps &= mmc->host_caps;
1053 if (mmc->card_caps & MMC_MODE_4BIT) {
1054 cmd.cmdidx = MMC_CMD_APP_CMD;
1055 cmd.resp_type = MMC_RSP_R1;
1056 cmd.cmdarg = mmc->rca << 16;
1059 err = mmc_send_cmd(mmc, &cmd, NULL);
1063 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1064 cmd.resp_type = MMC_RSP_R1;
1067 err = mmc_send_cmd(mmc, &cmd, NULL);
1071 mmc_set_bus_width(mmc, 4);
1074 if (mmc->card_caps & MMC_MODE_HS)
1075 mmc_set_clock(mmc, 50000000);
1077 mmc_set_clock(mmc, 25000000);
1079 if (mmc->card_caps & MMC_MODE_4BIT) {
1080 /* Set the card to use 4 bit*/
1081 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1083 EXT_CSD_BUS_WIDTH_4);
1088 mmc_set_bus_width(mmc, 4);
1089 } else if (mmc->card_caps & MMC_MODE_8BIT) {
1090 /* Set the card to use 8 bit*/
1091 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1093 EXT_CSD_BUS_WIDTH_8);
1098 mmc_set_bus_width(mmc, 8);
1101 if (mmc->card_caps & MMC_MODE_HS) {
1102 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1103 mmc_set_clock(mmc, 52000000);
1105 mmc_set_clock(mmc, 26000000);
1107 mmc_set_clock(mmc, 20000000);
1110 /* fill in device description */
1111 mmc->block_dev.lun = 0;
1112 mmc->block_dev.type = 0;
1113 mmc->block_dev.blksz = mmc->read_bl_len;
1114 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1115 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1116 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1117 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1118 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1119 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1120 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1121 (mmc->cid[2] >> 24) & 0xf);
1122 init_part(&mmc->block_dev);
1127 int mmc_send_if_cond(struct mmc *mmc)
1132 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1133 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1134 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1135 cmd.resp_type = MMC_RSP_R7;
1138 err = mmc_send_cmd(mmc, &cmd, NULL);
1143 if ((cmd.response[0] & 0xff) != 0xaa)
1144 return UNUSABLE_ERR;
1146 mmc->version = SD_VERSION_2;
1151 int mmc_register(struct mmc *mmc)
1153 /* Setup the universal parts of the block interface just once */
1154 mmc->block_dev.if_type = IF_TYPE_MMC;
1155 mmc->block_dev.dev = cur_dev_num++;
1156 mmc->block_dev.removable = 1;
1157 mmc->block_dev.block_read = mmc_bread;
1158 mmc->block_dev.block_write = mmc_bwrite;
1159 mmc->block_dev.block_erase = mmc_berase;
1161 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1163 INIT_LIST_HEAD (&mmc->link);
1165 list_add_tail (&mmc->link, &mmc_devices);
1170 #ifdef CONFIG_PARTITIONS
1171 block_dev_desc_t *mmc_get_dev(int dev)
1173 struct mmc *mmc = find_mmc_device(dev);
1175 return mmc ? &mmc->block_dev : NULL;
1179 int mmc_init(struct mmc *mmc)
1186 err = mmc->init(mmc);
1191 mmc_set_bus_width(mmc, 1);
1192 mmc_set_clock(mmc, 1);
1194 /* Reset the Card */
1195 err = mmc_go_idle(mmc);
1200 /* The internal partition reset to user partition(0) at every CMD0*/
1203 /* Test for SD version 2 */
1204 err = mmc_send_if_cond(mmc);
1206 /* Now try to get the SD card's operating condition */
1207 err = sd_send_op_cond(mmc);
1209 /* If the command timed out, we check for an MMC card */
1210 if (err == TIMEOUT) {
1211 err = mmc_send_op_cond(mmc);
1214 printf("Card did not respond to voltage select!\n");
1215 return UNUSABLE_ERR;
1219 err = mmc_startup(mmc);
1228 * CPU and board-specific MMC initializations. Aliased function
1229 * signals caller to move on
1231 static int __def_mmc_init(bd_t *bis)
1236 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1237 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1239 void print_mmc_devices(char separator)
1242 struct list_head *entry;
1244 list_for_each(entry, &mmc_devices) {
1245 m = list_entry(entry, struct mmc, link);
1247 printf("%s: %d", m->name, m->block_dev.dev);
1249 if (entry->next != &mmc_devices)
1250 printf("%c ", separator);
1256 int get_mmc_num(void)
1261 int mmc_initialize(bd_t *bis)
1263 INIT_LIST_HEAD (&mmc_devices);
1266 if (board_mmc_init(bis) < 0)
1269 print_mmc_devices(',');