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 mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
41 return mmc->send_cmd(mmc, cmd, data);
44 int mmc_set_blocklen(struct mmc *mmc, int len)
48 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
49 cmd.resp_type = MMC_RSP_R1;
53 return mmc_send_cmd(mmc, &cmd, NULL);
56 struct mmc *find_mmc_device(int dev_num)
59 struct list_head *entry;
61 list_for_each(entry, &mmc_devices) {
62 m = list_entry(entry, struct mmc, link);
64 if (m->block_dev.dev == dev_num)
68 printf("MMC Device %d not found\n", dev_num);
74 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
80 struct mmc *mmc = find_mmc_device(dev_num);
86 blklen = mmc->write_bl_len;
88 err = mmc_set_blocklen(mmc, mmc->write_bl_len);
91 printf("set write bl len failed\n\r");
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 stoperr = mmc_send_cmd(mmc, &cmd, NULL);
131 int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
134 struct mmc_data data;
136 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
138 if (mmc->high_capacity)
139 cmd.cmdarg = blocknum;
141 cmd.cmdarg = blocknum * mmc->read_bl_len;
143 cmd.resp_type = MMC_RSP_R1;
148 data.blocksize = mmc->read_bl_len;
149 data.flags = MMC_DATA_READ;
151 return mmc_send_cmd(mmc, &cmd, &data);
154 int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
158 int blklen = mmc->read_bl_len;
159 int startblock = lldiv(src, mmc->read_bl_len);
160 int endblock = lldiv(src + size - 1, mmc->read_bl_len);
163 /* Make a buffer big enough to hold all the blocks we might read */
164 buffer = malloc(blklen);
167 printf("Could not allocate buffer for MMC read!\n");
171 /* We always do full block reads from the card */
172 err = mmc_set_blocklen(mmc, mmc->read_bl_len);
177 for (i = startblock; i <= endblock; i++) {
181 err = mmc_read_block(mmc, buffer, i);
187 * The first block may not be aligned, so we
188 * copy from the desired point in the block
190 offset = (src & (blklen - 1));
191 segment_size = MIN(blklen - offset, size);
193 memcpy(dst, buffer + offset, segment_size);
197 size -= segment_size;
206 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
210 struct mmc *mmc = find_mmc_device(dev_num);
215 /* We always do full block reads from the card */
216 err = mmc_set_blocklen(mmc, mmc->read_bl_len);
222 for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
223 err = mmc_read_block(mmc, dst, i);
226 printf("block read failed: %d\n", err);
234 int mmc_go_idle(struct mmc* mmc)
241 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
243 cmd.resp_type = MMC_RSP_NONE;
246 err = mmc_send_cmd(mmc, &cmd, NULL);
257 sd_send_op_cond(struct mmc *mmc)
264 cmd.cmdidx = MMC_CMD_APP_CMD;
265 cmd.resp_type = MMC_RSP_R1;
269 err = mmc_send_cmd(mmc, &cmd, NULL);
274 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
275 cmd.resp_type = MMC_RSP_R3;
276 cmd.cmdarg = mmc->voltages;
278 if (mmc->version == SD_VERSION_2)
279 cmd.cmdarg |= OCR_HCS;
281 err = mmc_send_cmd(mmc, &cmd, NULL);
287 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
292 if (mmc->version != SD_VERSION_2)
293 mmc->version = SD_VERSION_1_0;
295 mmc->ocr = cmd.response[0];
297 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
303 int mmc_send_op_cond(struct mmc *mmc)
309 /* Some cards seem to need this */
313 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
314 cmd.resp_type = MMC_RSP_R3;
315 cmd.cmdarg = OCR_HCS | mmc->voltages;
318 err = mmc_send_cmd(mmc, &cmd, NULL);
324 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
329 mmc->version = MMC_VERSION_UNKNOWN;
330 mmc->ocr = cmd.response[0];
332 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
339 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
342 struct mmc_data data;
345 /* Get the Card Status Register */
346 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
347 cmd.resp_type = MMC_RSP_R1;
353 data.blocksize = 512;
354 data.flags = MMC_DATA_READ;
356 err = mmc_send_cmd(mmc, &cmd, &data);
362 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
366 cmd.cmdidx = MMC_CMD_SWITCH;
367 cmd.resp_type = MMC_RSP_R1b;
368 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
373 return mmc_send_cmd(mmc, &cmd, NULL);
376 int mmc_change_freq(struct mmc *mmc)
384 /* Only version 4 supports high-speed */
385 if (mmc->version < MMC_VERSION_4)
388 mmc->card_caps |= MMC_MODE_4BIT;
390 err = mmc_send_ext_csd(mmc, ext_csd);
395 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
396 mmc->high_capacity = 1;
398 if (mmc->high_capacity)
399 mmc->capacity = (((ext_csd[215] << 24) | (ext_csd[214] << 16) |
400 (ext_csd[213] << 8) | (ext_csd[21])) / (2 * 1024));
402 cardtype = ext_csd[196] & 0xf;
404 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
409 /* Now check to see that it worked */
410 err = mmc_send_ext_csd(mmc, ext_csd);
415 /* No high-speed support */
419 /* High Speed is set, there are two types: 52MHz and 26MHz */
420 if (cardtype & MMC_HS_52MHZ)
421 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
423 mmc->card_caps |= MMC_MODE_HS;
428 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
431 struct mmc_data data;
433 /* Switch the frequency */
434 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
435 cmd.resp_type = MMC_RSP_R1;
436 cmd.cmdarg = (mode << 31) | 0xffffff;
437 cmd.cmdarg &= ~(0xf << (group * 4));
438 cmd.cmdarg |= value << (group * 4);
441 data.dest = (char *)resp;
444 data.flags = MMC_DATA_READ;
446 return mmc_send_cmd(mmc, &cmd, &data);
450 int sd_change_freq(struct mmc *mmc)
455 uint switch_status[16];
456 struct mmc_data data;
461 /* Read the SCR to find out if this card supports higher speeds */
462 cmd.cmdidx = MMC_CMD_APP_CMD;
463 cmd.resp_type = MMC_RSP_R1;
464 cmd.cmdarg = mmc->rca << 16;
467 err = mmc_send_cmd(mmc, &cmd, NULL);
472 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
473 cmd.resp_type = MMC_RSP_R1;
480 data.dest = (char *)&scr;
483 data.flags = MMC_DATA_READ;
485 err = mmc_send_cmd(mmc, &cmd, &data);
494 mmc->scr[0] = __be32_to_cpu(scr[0]);
495 mmc->scr[1] = __be32_to_cpu(scr[1]);
497 switch ((mmc->scr[0] >> 24) & 0xf) {
499 mmc->version = SD_VERSION_1_0;
502 mmc->version = SD_VERSION_1_10;
505 mmc->version = SD_VERSION_2;
508 mmc->version = SD_VERSION_1_0;
512 /* Version 1.0 doesn't support switching */
513 if (mmc->version == SD_VERSION_1_0)
518 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
519 (u8 *)&switch_status);
524 /* The high-speed function is busy. Try again */
525 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
529 if (mmc->scr[0] & SD_DATA_4BIT)
530 mmc->card_caps |= MMC_MODE_4BIT;
532 /* If high-speed isn't supported, we return */
533 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
536 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
541 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
542 mmc->card_caps |= MMC_MODE_HS;
547 /* frequency bases */
548 /* divided by 10 to be nice to platforms without floating point */
556 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
557 * to platforms without floating point.
559 int multipliers[] = {
578 void mmc_set_ios(struct mmc *mmc)
583 void mmc_set_clock(struct mmc *mmc, uint clock)
585 if (clock > mmc->f_max)
588 if (clock < mmc->f_min)
596 void mmc_set_bus_width(struct mmc *mmc, uint width)
598 mmc->bus_width = width;
603 int mmc_startup(struct mmc *mmc)
610 /* Put the Card in Identify Mode */
611 cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
612 cmd.resp_type = MMC_RSP_R2;
616 err = mmc_send_cmd(mmc, &cmd, NULL);
621 memcpy(mmc->cid, cmd.response, 16);
624 * For MMC cards, set the Relative Address.
625 * For SD cards, get the Relatvie Address.
626 * This also puts the cards into Standby State
628 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
629 cmd.cmdarg = mmc->rca << 16;
630 cmd.resp_type = MMC_RSP_R6;
633 err = mmc_send_cmd(mmc, &cmd, NULL);
639 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
641 /* Get the Card-Specific Data */
642 cmd.cmdidx = MMC_CMD_SEND_CSD;
643 cmd.resp_type = MMC_RSP_R2;
644 cmd.cmdarg = mmc->rca << 16;
647 err = mmc_send_cmd(mmc, &cmd, NULL);
652 mmc->csd[0] = cmd.response[0];
653 mmc->csd[1] = cmd.response[1];
654 mmc->csd[2] = cmd.response[2];
655 mmc->csd[3] = cmd.response[3];
657 if (mmc->version == MMC_VERSION_UNKNOWN) {
658 int version = (cmd.response[0] >> 26) & 0xf;
662 mmc->version = MMC_VERSION_1_2;
665 mmc->version = MMC_VERSION_1_4;
668 mmc->version = MMC_VERSION_2_2;
671 mmc->version = MMC_VERSION_3;
674 mmc->version = MMC_VERSION_4;
677 mmc->version = MMC_VERSION_1_2;
682 /* divide frequency by 10, since the mults are 10x bigger */
683 freq = fbase[(cmd.response[0] & 0x7)];
684 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
686 mmc->tran_speed = freq * mult;
688 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
691 mmc->write_bl_len = mmc->read_bl_len;
693 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
695 if (mmc->high_capacity) {
696 csize = (mmc->csd[1] & 0x3f) << 16
697 | (mmc->csd[2] & 0xffff0000) >> 16;
700 csize = (mmc->csd[1] & 0x3ff) << 2
701 | (mmc->csd[2] & 0xc0000000) >> 30;
702 cmult = (mmc->csd[2] & 0x00038000) >> 15;
705 mmc->capacity = (csize + 1) << (cmult + 2);
706 mmc->capacity *= mmc->read_bl_len;
708 if (mmc->read_bl_len > 512)
709 mmc->read_bl_len = 512;
711 if (mmc->write_bl_len > 512)
712 mmc->write_bl_len = 512;
714 /* Select the card, and put it into Transfer Mode */
715 cmd.cmdidx = MMC_CMD_SELECT_CARD;
716 cmd.resp_type = MMC_RSP_R1b;
717 cmd.cmdarg = mmc->rca << 16;
719 err = mmc_send_cmd(mmc, &cmd, NULL);
725 err = sd_change_freq(mmc);
727 err = mmc_change_freq(mmc);
732 /* Restrict card's capabilities by what the host can do */
733 mmc->card_caps &= mmc->host_caps;
736 if (mmc->card_caps & MMC_MODE_4BIT) {
737 cmd.cmdidx = MMC_CMD_APP_CMD;
738 cmd.resp_type = MMC_RSP_R1;
739 cmd.cmdarg = mmc->rca << 16;
742 err = mmc_send_cmd(mmc, &cmd, NULL);
746 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
747 cmd.resp_type = MMC_RSP_R1;
750 err = mmc_send_cmd(mmc, &cmd, NULL);
754 mmc_set_bus_width(mmc, 4);
757 if (mmc->card_caps & MMC_MODE_HS)
758 mmc_set_clock(mmc, 50000000);
760 mmc_set_clock(mmc, 25000000);
762 if (mmc->card_caps & MMC_MODE_4BIT) {
763 /* Set the card to use 4 bit*/
764 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
766 EXT_CSD_BUS_WIDTH_4);
771 mmc_set_bus_width(mmc, 4);
772 } else if (mmc->card_caps & MMC_MODE_8BIT) {
773 /* Set the card to use 8 bit*/
774 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
776 EXT_CSD_BUS_WIDTH_8);
781 mmc_set_bus_width(mmc, 8);
784 if (mmc->card_caps & MMC_MODE_HS) {
785 if (mmc->card_caps & MMC_MODE_HS_52MHz)
786 mmc_set_clock(mmc, 52000000);
788 mmc_set_clock(mmc, 26000000);
790 mmc_set_clock(mmc, 20000000);
793 /* fill in device description */
794 mmc->block_dev.lun = 0;
795 mmc->block_dev.type = 0;
796 mmc->block_dev.blksz = mmc->read_bl_len;
797 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
798 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
799 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
800 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
801 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
802 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
803 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
804 (mmc->cid[2] >> 24) & 0xf);
805 init_part(&mmc->block_dev);
810 int mmc_send_if_cond(struct mmc *mmc)
815 cmd.cmdidx = SD_CMD_SEND_IF_COND;
816 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
817 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
818 cmd.resp_type = MMC_RSP_R7;
821 err = mmc_send_cmd(mmc, &cmd, NULL);
826 if ((cmd.response[0] & 0xff) != 0xaa)
829 mmc->version = SD_VERSION_2;
834 int mmc_register(struct mmc *mmc)
836 /* Setup the universal parts of the block interface just once */
837 mmc->block_dev.if_type = IF_TYPE_MMC;
838 mmc->block_dev.dev = cur_dev_num++;
839 mmc->block_dev.removable = 1;
840 mmc->block_dev.block_read = mmc_bread;
841 mmc->block_dev.block_write = mmc_bwrite;
843 INIT_LIST_HEAD (&mmc->link);
845 list_add_tail (&mmc->link, &mmc_devices);
850 block_dev_desc_t *mmc_get_dev(int dev)
852 struct mmc *mmc = find_mmc_device(dev);
854 return mmc ? &mmc->block_dev : NULL;
857 int mmc_init(struct mmc *mmc)
861 err = mmc->init(mmc);
866 mmc_set_bus_width(mmc, 1);
867 mmc_set_clock(mmc, 1);
870 err = mmc_go_idle(mmc);
875 /* Test for SD version 2 */
876 err = mmc_send_if_cond(mmc);
878 /* Now try to get the SD card's operating condition */
879 err = sd_send_op_cond(mmc);
881 /* If the command timed out, we check for an MMC card */
882 if (err == TIMEOUT) {
883 err = mmc_send_op_cond(mmc);
886 printf("Card did not respond to voltage select!\n");
891 return mmc_startup(mmc);
895 * CPU and board-specific MMC initializations. Aliased function
896 * signals caller to move on
898 static int __def_mmc_init(bd_t *bis)
903 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
904 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
906 void print_mmc_devices(char separator)
909 struct list_head *entry;
911 list_for_each(entry, &mmc_devices) {
912 m = list_entry(entry, struct mmc, link);
914 printf("%s: %d", m->name, m->block_dev.dev);
916 if (entry->next != &mmc_devices)
917 printf("%c ", separator);
923 int mmc_initialize(bd_t *bis)
925 INIT_LIST_HEAD (&mmc_devices);
928 if (board_mmc_init(bis) < 0)
931 print_mmc_devices(',');