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>
36 static struct list_head mmc_devices;
37 static int cur_dev_num = -1;
39 int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
43 int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
44 alias("__board_mmc_getcd")));
46 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
48 return mmc->send_cmd(mmc, cmd, data);
51 int mmc_set_blocklen(struct mmc *mmc, int len)
55 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
56 cmd.resp_type = MMC_RSP_R1;
60 return mmc_send_cmd(mmc, &cmd, NULL);
63 struct mmc *find_mmc_device(int dev_num)
66 struct list_head *entry;
68 list_for_each(entry, &mmc_devices) {
69 m = list_entry(entry, struct mmc, link);
71 if (m->block_dev.dev == dev_num)
75 printf("MMC Device %d not found\n", dev_num);
81 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
87 blklen = mmc->write_bl_len;
89 if ((start + blkcnt) > mmc->block_dev.lba) {
90 printf("MMC: block number 0x%lx exceeds max(0x%lx)",
91 start + blkcnt, mmc->block_dev.lba);
96 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
98 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
100 if (mmc->high_capacity)
103 cmd.cmdarg = start * blklen;
105 cmd.resp_type = MMC_RSP_R1;
109 data.blocks = blkcnt;
110 data.blocksize = blklen;
111 data.flags = MMC_DATA_WRITE;
113 err = mmc_send_cmd(mmc, &cmd, &data);
116 printf("mmc write failed\n\r");
121 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
123 cmd.resp_type = MMC_RSP_R1b;
125 err = mmc_send_cmd(mmc, &cmd, NULL);
127 printf("mmc fail to send stop cmd\n\r");
136 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
139 struct mmc *mmc = find_mmc_device(dev_num);
140 lbaint_t cur, blocks_todo = blkcnt;
145 err = mmc_set_blocklen(mmc, mmc->write_bl_len);
147 printf("set write bl len failed\n\r");
153 * The 65535 constraint comes from some hardware has
154 * only 16 bit width block number counter
156 cur = (blocks_todo > 65535) ? 65535 : blocks_todo;
157 if(mmc_write_blocks(mmc, start, cur, src) != cur)
161 src += cur * mmc->write_bl_len;
162 } while (blocks_todo > 0);
167 int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
170 struct mmc_data data;
172 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
174 if (mmc->high_capacity)
175 cmd.cmdarg = blocknum;
177 cmd.cmdarg = blocknum * mmc->read_bl_len;
179 cmd.resp_type = MMC_RSP_R1;
184 data.blocksize = mmc->read_bl_len;
185 data.flags = MMC_DATA_READ;
187 return mmc_send_cmd(mmc, &cmd, &data);
190 int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
194 int blklen = mmc->read_bl_len;
195 int startblock = lldiv(src, mmc->read_bl_len);
196 int endblock = lldiv(src + size - 1, mmc->read_bl_len);
199 /* Make a buffer big enough to hold all the blocks we might read */
200 buffer = malloc(blklen);
203 printf("Could not allocate buffer for MMC read!\n");
207 /* We always do full block reads from the card */
208 err = mmc_set_blocklen(mmc, mmc->read_bl_len);
213 for (i = startblock; i <= endblock; i++) {
217 err = mmc_read_block(mmc, buffer, i);
223 * The first block may not be aligned, so we
224 * copy from the desired point in the block
226 offset = (src & (blklen - 1));
227 segment_size = MIN(blklen - offset, size);
229 memcpy(dst, buffer + offset, segment_size);
233 size -= segment_size;
242 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
246 struct mmc *mmc = find_mmc_device(dev_num);
251 if ((start + blkcnt) > mmc->block_dev.lba) {
252 printf("MMC: block number 0x%lx exceeds max(0x%lx)",
253 start + blkcnt, mmc->block_dev.lba);
256 /* We always do full block reads from the card */
257 err = mmc_set_blocklen(mmc, mmc->read_bl_len);
263 for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
264 err = mmc_read_block(mmc, dst, i);
267 printf("block read failed: %d\n", err);
275 int mmc_go_idle(struct mmc* mmc)
282 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
284 cmd.resp_type = MMC_RSP_NONE;
287 err = mmc_send_cmd(mmc, &cmd, NULL);
298 sd_send_op_cond(struct mmc *mmc)
305 cmd.cmdidx = MMC_CMD_APP_CMD;
306 cmd.resp_type = MMC_RSP_R1;
310 err = mmc_send_cmd(mmc, &cmd, NULL);
315 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
316 cmd.resp_type = MMC_RSP_R3;
319 * Most cards do not answer if some reserved bits
320 * in the ocr are set. However, Some controller
321 * can set bit 7 (reserved for low voltages), but
322 * how to manage low voltages SD card is not yet
325 cmd.cmdarg = mmc->voltages & 0xff8000;
327 if (mmc->version == SD_VERSION_2)
328 cmd.cmdarg |= OCR_HCS;
330 err = mmc_send_cmd(mmc, &cmd, NULL);
336 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
341 if (mmc->version != SD_VERSION_2)
342 mmc->version = SD_VERSION_1_0;
344 mmc->ocr = cmd.response[0];
346 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
352 int mmc_send_op_cond(struct mmc *mmc)
358 /* Some cards seem to need this */
362 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
363 cmd.resp_type = MMC_RSP_R3;
364 cmd.cmdarg = OCR_HCS | mmc->voltages;
367 err = mmc_send_cmd(mmc, &cmd, NULL);
373 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
378 mmc->version = MMC_VERSION_UNKNOWN;
379 mmc->ocr = cmd.response[0];
381 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
388 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
391 struct mmc_data data;
394 /* Get the Card Status Register */
395 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
396 cmd.resp_type = MMC_RSP_R1;
402 data.blocksize = 512;
403 data.flags = MMC_DATA_READ;
405 err = mmc_send_cmd(mmc, &cmd, &data);
411 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
415 cmd.cmdidx = MMC_CMD_SWITCH;
416 cmd.resp_type = MMC_RSP_R1b;
417 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
422 return mmc_send_cmd(mmc, &cmd, NULL);
425 int mmc_change_freq(struct mmc *mmc)
433 /* Only version 4 supports high-speed */
434 if (mmc->version < MMC_VERSION_4)
437 mmc->card_caps |= MMC_MODE_4BIT;
439 err = mmc_send_ext_csd(mmc, ext_csd);
444 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
445 mmc->high_capacity = 1;
447 cardtype = ext_csd[196] & 0xf;
449 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
454 /* Now check to see that it worked */
455 err = mmc_send_ext_csd(mmc, ext_csd);
460 /* No high-speed support */
464 /* High Speed is set, there are two types: 52MHz and 26MHz */
465 if (cardtype & MMC_HS_52MHZ)
466 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
468 mmc->card_caps |= MMC_MODE_HS;
473 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
476 struct mmc_data data;
478 /* Switch the frequency */
479 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
480 cmd.resp_type = MMC_RSP_R1;
481 cmd.cmdarg = (mode << 31) | 0xffffff;
482 cmd.cmdarg &= ~(0xf << (group * 4));
483 cmd.cmdarg |= value << (group * 4);
486 data.dest = (char *)resp;
489 data.flags = MMC_DATA_READ;
491 return mmc_send_cmd(mmc, &cmd, &data);
495 int sd_change_freq(struct mmc *mmc)
500 uint switch_status[16];
501 struct mmc_data data;
506 /* Read the SCR to find out if this card supports higher speeds */
507 cmd.cmdidx = MMC_CMD_APP_CMD;
508 cmd.resp_type = MMC_RSP_R1;
509 cmd.cmdarg = mmc->rca << 16;
512 err = mmc_send_cmd(mmc, &cmd, NULL);
517 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
518 cmd.resp_type = MMC_RSP_R1;
525 data.dest = (char *)&scr;
528 data.flags = MMC_DATA_READ;
530 err = mmc_send_cmd(mmc, &cmd, &data);
539 mmc->scr[0] = __be32_to_cpu(scr[0]);
540 mmc->scr[1] = __be32_to_cpu(scr[1]);
542 switch ((mmc->scr[0] >> 24) & 0xf) {
544 mmc->version = SD_VERSION_1_0;
547 mmc->version = SD_VERSION_1_10;
550 mmc->version = SD_VERSION_2;
553 mmc->version = SD_VERSION_1_0;
557 /* Version 1.0 doesn't support switching */
558 if (mmc->version == SD_VERSION_1_0)
563 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
564 (u8 *)&switch_status);
569 /* The high-speed function is busy. Try again */
570 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
574 if (mmc->scr[0] & SD_DATA_4BIT)
575 mmc->card_caps |= MMC_MODE_4BIT;
577 /* If high-speed isn't supported, we return */
578 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
581 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
586 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
587 mmc->card_caps |= MMC_MODE_HS;
592 /* frequency bases */
593 /* divided by 10 to be nice to platforms without floating point */
601 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
602 * to platforms without floating point.
604 int multipliers[] = {
623 void mmc_set_ios(struct mmc *mmc)
628 void mmc_set_clock(struct mmc *mmc, uint clock)
630 if (clock > mmc->f_max)
633 if (clock < mmc->f_min)
641 void mmc_set_bus_width(struct mmc *mmc, uint width)
643 mmc->bus_width = width;
648 int mmc_startup(struct mmc *mmc)
656 /* Put the Card in Identify Mode */
657 cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
658 cmd.resp_type = MMC_RSP_R2;
662 err = mmc_send_cmd(mmc, &cmd, NULL);
667 memcpy(mmc->cid, cmd.response, 16);
670 * For MMC cards, set the Relative Address.
671 * For SD cards, get the Relatvie Address.
672 * This also puts the cards into Standby State
674 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
675 cmd.cmdarg = mmc->rca << 16;
676 cmd.resp_type = MMC_RSP_R6;
679 err = mmc_send_cmd(mmc, &cmd, NULL);
685 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
687 /* Get the Card-Specific Data */
688 cmd.cmdidx = MMC_CMD_SEND_CSD;
689 cmd.resp_type = MMC_RSP_R2;
690 cmd.cmdarg = mmc->rca << 16;
693 err = mmc_send_cmd(mmc, &cmd, NULL);
698 mmc->csd[0] = cmd.response[0];
699 mmc->csd[1] = cmd.response[1];
700 mmc->csd[2] = cmd.response[2];
701 mmc->csd[3] = cmd.response[3];
703 if (mmc->version == MMC_VERSION_UNKNOWN) {
704 int version = (cmd.response[0] >> 26) & 0xf;
708 mmc->version = MMC_VERSION_1_2;
711 mmc->version = MMC_VERSION_1_4;
714 mmc->version = MMC_VERSION_2_2;
717 mmc->version = MMC_VERSION_3;
720 mmc->version = MMC_VERSION_4;
723 mmc->version = MMC_VERSION_1_2;
728 /* divide frequency by 10, since the mults are 10x bigger */
729 freq = fbase[(cmd.response[0] & 0x7)];
730 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
732 mmc->tran_speed = freq * mult;
734 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
737 mmc->write_bl_len = mmc->read_bl_len;
739 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
741 if (mmc->high_capacity) {
742 csize = (mmc->csd[1] & 0x3f) << 16
743 | (mmc->csd[2] & 0xffff0000) >> 16;
746 csize = (mmc->csd[1] & 0x3ff) << 2
747 | (mmc->csd[2] & 0xc0000000) >> 30;
748 cmult = (mmc->csd[2] & 0x00038000) >> 15;
751 mmc->capacity = (csize + 1) << (cmult + 2);
752 mmc->capacity *= mmc->read_bl_len;
754 if (mmc->read_bl_len > 512)
755 mmc->read_bl_len = 512;
757 if (mmc->write_bl_len > 512)
758 mmc->write_bl_len = 512;
760 /* Select the card, and put it into Transfer Mode */
761 cmd.cmdidx = MMC_CMD_SELECT_CARD;
762 cmd.resp_type = MMC_RSP_R1b;
763 cmd.cmdarg = mmc->rca << 16;
765 err = mmc_send_cmd(mmc, &cmd, NULL);
770 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
771 /* check ext_csd version and capacity */
772 err = mmc_send_ext_csd(mmc, ext_csd);
773 if (!err & (ext_csd[192] >= 2)) {
774 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
775 ext_csd[214] << 16 | ext_csd[215] << 24;
776 mmc->capacity *= 512;
781 err = sd_change_freq(mmc);
783 err = mmc_change_freq(mmc);
788 /* Restrict card's capabilities by what the host can do */
789 mmc->card_caps &= mmc->host_caps;
792 if (mmc->card_caps & MMC_MODE_4BIT) {
793 cmd.cmdidx = MMC_CMD_APP_CMD;
794 cmd.resp_type = MMC_RSP_R1;
795 cmd.cmdarg = mmc->rca << 16;
798 err = mmc_send_cmd(mmc, &cmd, NULL);
802 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
803 cmd.resp_type = MMC_RSP_R1;
806 err = mmc_send_cmd(mmc, &cmd, NULL);
810 mmc_set_bus_width(mmc, 4);
813 if (mmc->card_caps & MMC_MODE_HS)
814 mmc_set_clock(mmc, 50000000);
816 mmc_set_clock(mmc, 25000000);
818 if (mmc->card_caps & MMC_MODE_4BIT) {
819 /* Set the card to use 4 bit*/
820 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
822 EXT_CSD_BUS_WIDTH_4);
827 mmc_set_bus_width(mmc, 4);
828 } else if (mmc->card_caps & MMC_MODE_8BIT) {
829 /* Set the card to use 8 bit*/
830 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
832 EXT_CSD_BUS_WIDTH_8);
837 mmc_set_bus_width(mmc, 8);
840 if (mmc->card_caps & MMC_MODE_HS) {
841 if (mmc->card_caps & MMC_MODE_HS_52MHz)
842 mmc_set_clock(mmc, 52000000);
844 mmc_set_clock(mmc, 26000000);
846 mmc_set_clock(mmc, 20000000);
849 /* fill in device description */
850 mmc->block_dev.lun = 0;
851 mmc->block_dev.type = 0;
852 mmc->block_dev.blksz = mmc->read_bl_len;
853 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
854 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
855 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
856 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
857 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
858 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
859 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
860 (mmc->cid[2] >> 24) & 0xf);
861 init_part(&mmc->block_dev);
866 int mmc_send_if_cond(struct mmc *mmc)
871 cmd.cmdidx = SD_CMD_SEND_IF_COND;
872 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
873 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
874 cmd.resp_type = MMC_RSP_R7;
877 err = mmc_send_cmd(mmc, &cmd, NULL);
882 if ((cmd.response[0] & 0xff) != 0xaa)
885 mmc->version = SD_VERSION_2;
890 int mmc_register(struct mmc *mmc)
892 /* Setup the universal parts of the block interface just once */
893 mmc->block_dev.if_type = IF_TYPE_MMC;
894 mmc->block_dev.dev = cur_dev_num++;
895 mmc->block_dev.removable = 1;
896 mmc->block_dev.block_read = mmc_bread;
897 mmc->block_dev.block_write = mmc_bwrite;
899 INIT_LIST_HEAD (&mmc->link);
901 list_add_tail (&mmc->link, &mmc_devices);
906 block_dev_desc_t *mmc_get_dev(int dev)
908 struct mmc *mmc = find_mmc_device(dev);
910 return mmc ? &mmc->block_dev : NULL;
913 int mmc_init(struct mmc *mmc)
917 err = mmc->init(mmc);
922 mmc_set_bus_width(mmc, 1);
923 mmc_set_clock(mmc, 1);
926 err = mmc_go_idle(mmc);
931 /* Test for SD version 2 */
932 err = mmc_send_if_cond(mmc);
934 /* Now try to get the SD card's operating condition */
935 err = sd_send_op_cond(mmc);
937 /* If the command timed out, we check for an MMC card */
938 if (err == TIMEOUT) {
939 err = mmc_send_op_cond(mmc);
942 printf("Card did not respond to voltage select!\n");
947 return mmc_startup(mmc);
951 * CPU and board-specific MMC initializations. Aliased function
952 * signals caller to move on
954 static int __def_mmc_init(bd_t *bis)
959 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
960 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
962 void print_mmc_devices(char separator)
965 struct list_head *entry;
967 list_for_each(entry, &mmc_devices) {
968 m = list_entry(entry, struct mmc, link);
970 printf("%s: %d", m->name, m->block_dev.dev);
972 if (entry->next != &mmc_devices)
973 printf("%c ", separator);
979 int mmc_initialize(bd_t *bis)
981 INIT_LIST_HEAD (&mmc_devices);
984 if (board_mmc_init(bis) < 0)
987 print_mmc_devices(',');