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 static struct list_head mmc_devices;
36 static int cur_dev_num = -1;
38 int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
42 int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
43 alias("__board_mmc_getcd")));
45 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
47 return mmc->send_cmd(mmc, cmd, data);
50 int mmc_set_blocklen(struct mmc *mmc, int len)
54 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
55 cmd.resp_type = MMC_RSP_R1;
59 return mmc_send_cmd(mmc, &cmd, NULL);
62 struct mmc *find_mmc_device(int dev_num)
65 struct list_head *entry;
67 list_for_each(entry, &mmc_devices) {
68 m = list_entry(entry, struct mmc, link);
70 if (m->block_dev.dev == dev_num)
74 printf("MMC Device %d not found\n", dev_num);
80 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
85 if ((start + blkcnt) > mmc->block_dev.lba) {
86 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
87 start + blkcnt, mmc->block_dev.lba);
92 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
94 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
96 if (mmc->high_capacity)
99 cmd.cmdarg = start * mmc->write_bl_len;
101 cmd.resp_type = MMC_RSP_R1;
105 data.blocks = blkcnt;
106 data.blocksize = mmc->write_bl_len;
107 data.flags = MMC_DATA_WRITE;
109 if (mmc_send_cmd(mmc, &cmd, &data)) {
110 printf("mmc write failed\n");
114 #ifndef CONFIG_MMC_ASYNC_WRITE
116 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
118 cmd.resp_type = MMC_RSP_R1b;
120 if (mmc_send_cmd(mmc, &cmd, NULL)) {
121 printf("mmc fail to send stop cmd\n");
131 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
133 lbaint_t cur, blocks_todo = blkcnt;
135 struct mmc *mmc = find_mmc_device(dev_num);
139 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
144 * The 65535 constraint comes from some hardware has
145 * only 16 bit width block number counter
147 cur = (blocks_todo > 65535) ? 65535 : blocks_todo;
148 if(mmc_write_blocks(mmc, start, cur, src) != cur)
152 src += cur * mmc->write_bl_len;
153 } while (blocks_todo > 0);
158 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
161 struct mmc_data data;
164 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
166 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
168 if (mmc->high_capacity)
171 cmd.cmdarg = start * mmc->read_bl_len;
173 cmd.resp_type = MMC_RSP_R1;
177 data.blocks = blkcnt;
178 data.blocksize = mmc->read_bl_len;
179 data.flags = MMC_DATA_READ;
181 if (mmc_send_cmd(mmc, &cmd, &data))
185 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
187 cmd.resp_type = MMC_RSP_R1b;
189 if (mmc_send_cmd(mmc, &cmd, NULL)) {
190 printf("mmc fail to send stop cmd\n");
198 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
200 lbaint_t cur, blocks_todo = blkcnt;
205 struct mmc *mmc = find_mmc_device(dev_num);
209 if ((start + blkcnt) > mmc->block_dev.lba) {
210 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
211 start + blkcnt, mmc->block_dev.lba);
215 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
220 * The 65535 constraint comes from some hardware has
221 * only 16 bit width block number counter
223 cur = (blocks_todo > 65535) ? 65535 : blocks_todo;
224 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
228 dst += cur * mmc->read_bl_len;
229 } while (blocks_todo > 0);
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;
278 * Most cards do not answer if some reserved bits
279 * in the ocr are set. However, Some controller
280 * can set bit 7 (reserved for low voltages), but
281 * how to manage low voltages SD card is not yet
284 cmd.cmdarg = mmc->voltages & 0xff8000;
286 if (mmc->version == SD_VERSION_2)
287 cmd.cmdarg |= OCR_HCS;
289 err = mmc_send_cmd(mmc, &cmd, NULL);
295 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
300 if (mmc->version != SD_VERSION_2)
301 mmc->version = SD_VERSION_1_0;
303 mmc->ocr = cmd.response[0];
305 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
311 int mmc_send_op_cond(struct mmc *mmc)
317 /* Some cards seem to need this */
321 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
322 cmd.resp_type = MMC_RSP_R3;
323 cmd.cmdarg = OCR_HCS | mmc->voltages;
326 err = mmc_send_cmd(mmc, &cmd, NULL);
332 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
337 mmc->version = MMC_VERSION_UNKNOWN;
338 mmc->ocr = cmd.response[0];
340 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
347 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
350 struct mmc_data data;
353 /* Get the Card Status Register */
354 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
355 cmd.resp_type = MMC_RSP_R1;
361 data.blocksize = 512;
362 data.flags = MMC_DATA_READ;
364 err = mmc_send_cmd(mmc, &cmd, &data);
370 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
374 cmd.cmdidx = MMC_CMD_SWITCH;
375 cmd.resp_type = MMC_RSP_R1b;
376 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
381 return mmc_send_cmd(mmc, &cmd, NULL);
384 int mmc_change_freq(struct mmc *mmc)
392 /* Only version 4 supports high-speed */
393 if (mmc->version < MMC_VERSION_4)
396 mmc->card_caps |= MMC_MODE_8BIT | MMC_MODE_4BIT;
398 err = mmc_send_ext_csd(mmc, ext_csd);
403 /* don't check this register to determine high capacity
404 it also contain low capacity */
405 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
406 mmc->high_capacity = 1;
408 cardtype = ext_csd[196] & 0xf;
410 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
415 /* partition configuration */
416 if (mmc->boot_config) {
417 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
418 EXT_CSD_BOOT_CONFIG, mmc->boot_config);
424 * Boot bus width configuration
425 * Bit [1:0] BOOT_BUS_WIDTH
427 * 0x1 : 4-bit bus width
428 * 0x2 : 8-bit bus width
431 if (mmc->boot_config & 0x7) {
432 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
433 EXT_CSD_BOOT_BUS_WIDTH, mmc->boot_buswidth);
438 /* Now check to see that it worked */
439 err = mmc_send_ext_csd(mmc, ext_csd);
444 /* Check MMC version 4.X */
445 switch (ext_csd[EXT_CSD_REV]) {
446 case EXT_CSD_REV_1_0:
448 mmc->version |= EXT_CSD_REV_1_0;
450 case EXT_CSD_REV_1_1:
452 mmc->version |= EXT_CSD_REV_1_1;
454 case EXT_CSD_REV_1_2:
456 mmc->version |= EXT_CSD_REV_1_2;
458 case EXT_CSD_REV_1_3:
460 mmc->version |= EXT_CSD_REV_1_3;
462 case EXT_CSD_REV_1_5:
464 mmc->version |= EXT_CSD_REV_1_5;
468 * if use iNAND 8G flash, this should be entered.
469 * (0x2 => iNAND 8G flash)
470 * Because iNAND 16G should be MMC v4.41,
471 * need not check here. (already checked)
473 if ((mmc->cid[0] >> 24) == 0x2) {
475 mmc->check_rev = MMC_REV_4_4_1;
477 mmc->check_rev = MMC_REV_4_3_PLUS;
481 case EXT_CSD_REV_1_6:
483 mmc->version |= EXT_CSD_REV_1_6;
485 case EXT_CSD_REV_1_4:
488 printf("Unknown revision - %x\n", ext_csd[EXT_CSD_REV]);
492 /* No high-speed support */
496 /* High Speed is set, there are two types: 52MHz and 26MHz */
497 if (cardtype & MMC_HS_52MHZ)
498 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
500 mmc->card_caps |= MMC_MODE_HS;
505 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
508 struct mmc_data data;
510 /* Switch the frequency */
511 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
512 cmd.resp_type = MMC_RSP_R1;
513 cmd.cmdarg = (mode << 31) | 0xffffff;
514 cmd.cmdarg &= ~(0xf << (group * 4));
515 cmd.cmdarg |= value << (group * 4);
518 data.dest = (char *)resp;
521 data.flags = MMC_DATA_READ;
523 return mmc_send_cmd(mmc, &cmd, &data);
527 int sd_change_freq(struct mmc *mmc)
532 uint switch_status[16];
533 struct mmc_data data;
538 /* Read the SCR to find out if this card supports higher speeds */
539 cmd.cmdidx = MMC_CMD_APP_CMD;
540 cmd.resp_type = MMC_RSP_R1;
541 cmd.cmdarg = mmc->rca << 16;
544 err = mmc_send_cmd(mmc, &cmd, NULL);
549 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
550 cmd.resp_type = MMC_RSP_R1;
557 data.dest = (char *)&scr;
560 data.flags = MMC_DATA_READ;
562 err = mmc_send_cmd(mmc, &cmd, &data);
571 mmc->scr[0] = __be32_to_cpu(scr[0]);
572 mmc->scr[1] = __be32_to_cpu(scr[1]);
574 switch ((mmc->scr[0] >> 24) & 0xf) {
576 mmc->version = SD_VERSION_1_0;
579 mmc->version = SD_VERSION_1_10;
582 mmc->version = SD_VERSION_2;
585 mmc->version = SD_VERSION_1_0;
589 /* Version 1.0 doesn't support switching */
590 if (mmc->version == SD_VERSION_1_0)
595 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
596 (u8 *)&switch_status);
601 /* The high-speed function is busy. Try again */
602 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
606 if (mmc->scr[0] & SD_DATA_4BIT)
607 mmc->card_caps |= MMC_MODE_4BIT;
609 /* If high-speed isn't supported, we return */
610 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
613 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
618 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
619 mmc->card_caps |= MMC_MODE_HS;
624 /* frequency bases */
625 /* divided by 10 to be nice to platforms without floating point */
633 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
634 * to platforms without floating point.
636 int multipliers[] = {
655 void mmc_set_ios(struct mmc *mmc)
660 void mmc_set_clock(struct mmc *mmc, uint clock)
662 if (clock > mmc->f_max)
665 if (clock < mmc->f_min)
673 void mmc_set_bus_width(struct mmc *mmc, uint width)
675 mmc->bus_width = width;
680 int mmc_startup(struct mmc *mmc)
688 /* Put the Card in Identify Mode */
689 cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
690 cmd.resp_type = MMC_RSP_R2;
694 err = mmc_send_cmd(mmc, &cmd, NULL);
699 memcpy(mmc->cid, cmd.response, 16);
702 * For MMC cards, set the Relative Address.
703 * For SD cards, get the Relatvie Address.
704 * This also puts the cards into Standby State
706 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
707 cmd.cmdarg = mmc->rca << 16;
708 cmd.resp_type = MMC_RSP_R6;
711 err = mmc_send_cmd(mmc, &cmd, NULL);
717 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
719 /* Get the Card-Specific Data */
720 cmd.cmdidx = MMC_CMD_SEND_CSD;
721 cmd.resp_type = MMC_RSP_R2;
722 cmd.cmdarg = mmc->rca << 16;
725 err = mmc_send_cmd(mmc, &cmd, NULL);
730 mmc->csd[0] = cmd.response[0];
731 mmc->csd[1] = cmd.response[1];
732 mmc->csd[2] = cmd.response[2];
733 mmc->csd[3] = cmd.response[3];
735 if (mmc->version == MMC_VERSION_UNKNOWN) {
736 int version = (cmd.response[0] >> 26) & 0xf;
740 mmc->version = MMC_VERSION_1_2;
743 mmc->version = MMC_VERSION_1_4;
746 mmc->version = MMC_VERSION_2_2;
749 mmc->version = MMC_VERSION_3;
752 mmc->version = MMC_VERSION_4;
755 mmc->version = MMC_VERSION_1_2;
760 /* divide frequency by 10, since the mults are 10x bigger */
761 freq = fbase[(cmd.response[0] & 0x7)];
762 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
764 mmc->tran_speed = freq * mult;
766 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
769 mmc->write_bl_len = mmc->read_bl_len;
771 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
773 if (mmc->high_capacity) {
774 csize = (mmc->csd[1] & 0x3f) << 16
775 | (mmc->csd[2] & 0xffff0000) >> 16;
778 csize = (mmc->csd[1] & 0x3ff) << 2
779 | (mmc->csd[2] & 0xc0000000) >> 30;
780 cmult = (mmc->csd[2] & 0x00038000) >> 15;
783 mmc->capacity = (csize + 1) << (cmult + 2);
784 mmc->capacity *= mmc->read_bl_len;
786 if (mmc->read_bl_len > 512)
787 mmc->read_bl_len = 512;
789 if (mmc->write_bl_len > 512)
790 mmc->write_bl_len = 512;
792 /* Select the card, and put it into Transfer Mode */
793 cmd.cmdidx = MMC_CMD_SELECT_CARD;
794 cmd.resp_type = MMC_RSP_R1b;
795 cmd.cmdarg = mmc->rca << 16;
797 err = mmc_send_cmd(mmc, &cmd, NULL);
802 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
803 /* check ext_csd version and capacity */
804 err = mmc_send_ext_csd(mmc, ext_csd);
805 if (!err && mmc->high_capacity && (ext_csd[EXT_CSD_REV] >= 2)) {
806 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
807 ext_csd[214] << 16 | ext_csd[215] << 24;
808 mmc->capacity *= 512;
814 * removed sd_change_freq(mmc)
817 mmc->card_caps |= MMC_MODE_4BIT;
819 err = mmc_change_freq(mmc);
824 /* Restrict card's capabilities by what the host can do */
825 mmc->card_caps &= mmc->host_caps;
828 if (mmc->card_caps & MMC_MODE_4BIT) {
829 cmd.cmdidx = MMC_CMD_APP_CMD;
830 cmd.resp_type = MMC_RSP_R1;
831 cmd.cmdarg = mmc->rca << 16;
834 err = mmc_send_cmd(mmc, &cmd, NULL);
838 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
839 cmd.resp_type = MMC_RSP_R1;
842 err = mmc_send_cmd(mmc, &cmd, NULL);
846 mmc_set_bus_width(mmc, 4);
849 if (mmc->card_caps & MMC_MODE_HS)
850 mmc_set_clock(mmc, 50000000);
852 mmc_set_clock(mmc, 25000000);
854 if (mmc->card_caps & MMC_MODE_4BIT) {
855 /* Set the card to use 4 bit*/
856 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
858 EXT_CSD_BUS_WIDTH_4);
863 mmc_set_bus_width(mmc, 4);
864 } else if (mmc->card_caps & MMC_MODE_8BIT) {
865 /* Set the card to use 8 bit*/
866 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
868 EXT_CSD_BUS_WIDTH_8);
873 mmc_set_bus_width(mmc, 8);
876 if (mmc->card_caps & MMC_MODE_HS) {
877 if (mmc->card_caps & MMC_MODE_HS_52MHz)
878 mmc->tran_speed = 52000000;
880 mmc->tran_speed = 26000000;
883 mmc_set_clock(mmc, mmc->tran_speed);
886 /* fill in device description */
887 mmc->block_dev.lun = 0;
888 mmc->block_dev.type = 0;
889 mmc->block_dev.blksz = mmc->read_bl_len;
890 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
891 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
892 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
893 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
894 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
895 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
896 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
897 (mmc->cid[2] >> 24) & 0xf);
898 init_part(&mmc->block_dev);
903 int mmc_send_if_cond(struct mmc *mmc)
908 cmd.cmdidx = SD_CMD_SEND_IF_COND;
909 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
910 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
911 cmd.resp_type = MMC_RSP_R7;
914 err = mmc_send_cmd(mmc, &cmd, NULL);
919 if ((cmd.response[0] & 0xff) != 0xaa)
922 mmc->version = SD_VERSION_2;
927 int mmc_register(struct mmc *mmc)
929 /* Setup the universal parts of the block interface just once */
930 mmc->block_dev.if_type = IF_TYPE_MMC;
931 mmc->block_dev.dev = cur_dev_num++;
932 mmc->block_dev.removable = 1;
933 mmc->block_dev.block_read = mmc_bread;
934 mmc->block_dev.block_write = mmc_bwrite;
936 INIT_LIST_HEAD (&mmc->link);
938 list_add_tail (&mmc->link, &mmc_devices);
943 block_dev_desc_t *mmc_get_dev(int dev)
945 struct mmc *mmc = find_mmc_device(dev);
947 return mmc ? &mmc->block_dev : NULL;
950 int mmc_init(struct mmc *mmc)
954 err = mmc->init(mmc);
959 mmc_set_bus_width(mmc, 1);
960 mmc_set_clock(mmc, 1);
963 err = mmc_go_idle(mmc);
967 /* Test for SD version 2 */
968 err = mmc_send_if_cond(mmc);
970 /* Now try to get the SD card's operating condition */
971 err = sd_send_op_cond(mmc);
974 /* If the command timed out, we check for an MMC card */
975 if (err == TIMEOUT) {
977 err = mmc_send_op_cond(mmc);
979 printf("Card did not respond to voltage select!\n");
984 return mmc_startup(mmc);
988 * CPU and board-specific MMC initializations. Aliased function
989 * signals caller to move on
991 static int __def_mmc_init(bd_t *bis)
996 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
997 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
999 void print_mmc_devices(char separator)
1002 struct list_head *entry;
1004 list_for_each(entry, &mmc_devices) {
1005 m = list_entry(entry, struct mmc, link);
1007 printf("%s: %d", m->name, m->block_dev.dev);
1009 #if defined(CONFIG_S5PC110) && defined(CONFIG_UNIVERSAL)
1010 /* FIXME move to c110 universal board file */
1011 /* we are initiazlied mmc dev0 only */
1012 if(m->block_dev.dev == 0)
1016 if (entry->next != &mmc_devices)
1017 printf("%c ", separator);
1023 int mmc_initialize(bd_t *bis)
1025 INIT_LIST_HEAD (&mmc_devices);
1028 if (board_mmc_init(bis) < 0)
1031 print_mmc_devices(',');