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
)
86 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
87 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
88 start
+ blkcnt
, mmc
->block_dev
.lba
);
93 cmd
.cmdidx
= MMC_CMD_WRITE_MULTIPLE_BLOCK
;
95 cmd
.cmdidx
= MMC_CMD_WRITE_SINGLE_BLOCK
;
97 if (mmc
->high_capacity
)
100 cmd
.cmdarg
= start
* mmc
->write_bl_len
;
102 cmd
.resp_type
= MMC_RSP_R1
;
106 data
.blocks
= blkcnt
;
107 data
.blocksize
= mmc
->write_bl_len
;
108 data
.flags
= MMC_DATA_WRITE
;
110 if (mmc_send_cmd(mmc
, &cmd
, &data
)) {
111 printf("mmc write failed\n");
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");
130 mmc_bwrite(int dev_num
, ulong start
, lbaint_t blkcnt
, const void*src
)
132 lbaint_t cur
, blocks_todo
= blkcnt
;
134 struct mmc
*mmc
= find_mmc_device(dev_num
);
138 if (mmc_set_blocklen(mmc
, mmc
->write_bl_len
))
143 * The 65535 constraint comes from some hardware has
144 * only 16 bit width block number counter
146 cur
= (blocks_todo
> 65535) ? 65535 : blocks_todo
;
147 if(mmc_write_blocks(mmc
, start
, cur
, src
) != cur
)
151 src
+= cur
* mmc
->write_bl_len
;
152 } while (blocks_todo
> 0);
157 int mmc_read_blocks(struct mmc
*mmc
, void *dst
, ulong start
, lbaint_t blkcnt
)
160 struct mmc_data data
;
163 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
165 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
167 if (mmc
->high_capacity
)
170 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
172 cmd
.resp_type
= MMC_RSP_R1
;
176 data
.blocks
= blkcnt
;
177 data
.blocksize
= mmc
->read_bl_len
;
178 data
.flags
= MMC_DATA_READ
;
180 if (mmc_send_cmd(mmc
, &cmd
, &data
))
184 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
186 cmd
.resp_type
= MMC_RSP_R1b
;
188 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
189 printf("mmc fail to send stop cmd\n");
197 static ulong
mmc_bread(int dev_num
, ulong start
, lbaint_t blkcnt
, void *dst
)
199 lbaint_t cur
, blocks_todo
= blkcnt
;
204 struct mmc
*mmc
= find_mmc_device(dev_num
);
208 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
209 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
210 start
+ blkcnt
, mmc
->block_dev
.lba
);
214 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
219 * The 65535 constraint comes from some hardware has
220 * only 16 bit width block number counter
222 cur
= (blocks_todo
> 65535) ? 65535 : blocks_todo
;
223 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
227 dst
+= cur
* mmc
->read_bl_len
;
228 } while (blocks_todo
> 0);
233 int mmc_go_idle(struct mmc
* mmc
)
240 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
242 cmd
.resp_type
= MMC_RSP_NONE
;
245 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
256 sd_send_op_cond(struct mmc
*mmc
)
263 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
264 cmd
.resp_type
= MMC_RSP_R1
;
268 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
273 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
274 cmd
.resp_type
= MMC_RSP_R3
;
277 * Most cards do not answer if some reserved bits
278 * in the ocr are set. However, Some controller
279 * can set bit 7 (reserved for low voltages), but
280 * how to manage low voltages SD card is not yet
283 cmd
.cmdarg
= mmc
->voltages
& 0xff8000;
285 if (mmc
->version
== SD_VERSION_2
)
286 cmd
.cmdarg
|= OCR_HCS
;
288 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
294 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
299 if (mmc
->version
!= SD_VERSION_2
)
300 mmc
->version
= SD_VERSION_1_0
;
302 mmc
->ocr
= cmd
.response
[0];
304 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
310 int mmc_send_op_cond(struct mmc
*mmc
)
316 /* Some cards seem to need this */
320 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
321 cmd
.resp_type
= MMC_RSP_R3
;
322 cmd
.cmdarg
= OCR_HCS
| mmc
->voltages
;
325 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
331 } while (!(cmd
.response
[0] & OCR_BUSY
) && timeout
--);
336 mmc
->version
= MMC_VERSION_UNKNOWN
;
337 mmc
->ocr
= cmd
.response
[0];
339 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
346 int mmc_send_ext_csd(struct mmc
*mmc
, char *ext_csd
)
349 struct mmc_data data
;
352 /* Get the Card Status Register */
353 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
354 cmd
.resp_type
= MMC_RSP_R1
;
360 data
.blocksize
= 512;
361 data
.flags
= MMC_DATA_READ
;
363 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
369 int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
373 cmd
.cmdidx
= MMC_CMD_SWITCH
;
374 cmd
.resp_type
= MMC_RSP_R1b
;
375 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
380 return mmc_send_cmd(mmc
, &cmd
, NULL
);
383 int mmc_change_freq(struct mmc
*mmc
)
391 /* Only version 4 supports high-speed */
392 if (mmc
->version
< MMC_VERSION_4
)
395 mmc
->card_caps
|= MMC_MODE_4BIT
;
397 err
= mmc_send_ext_csd(mmc
, ext_csd
);
402 if (ext_csd
[212] || ext_csd
[213] || ext_csd
[214] || ext_csd
[215])
403 mmc
->high_capacity
= 1;
405 cardtype
= ext_csd
[196] & 0xf;
407 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
412 /* Now check to see that it worked */
413 err
= mmc_send_ext_csd(mmc
, ext_csd
);
418 /* No high-speed support */
422 /* High Speed is set, there are two types: 52MHz and 26MHz */
423 if (cardtype
& MMC_HS_52MHZ
)
424 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
426 mmc
->card_caps
|= MMC_MODE_HS
;
431 int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
434 struct mmc_data data
;
436 /* Switch the frequency */
437 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
438 cmd
.resp_type
= MMC_RSP_R1
;
439 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
440 cmd
.cmdarg
&= ~(0xf << (group
* 4));
441 cmd
.cmdarg
|= value
<< (group
* 4);
444 data
.dest
= (char *)resp
;
447 data
.flags
= MMC_DATA_READ
;
449 return mmc_send_cmd(mmc
, &cmd
, &data
);
453 int sd_change_freq(struct mmc
*mmc
)
458 uint switch_status
[16];
459 struct mmc_data data
;
464 /* Read the SCR to find out if this card supports higher speeds */
465 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
466 cmd
.resp_type
= MMC_RSP_R1
;
467 cmd
.cmdarg
= mmc
->rca
<< 16;
470 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
475 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
476 cmd
.resp_type
= MMC_RSP_R1
;
483 data
.dest
= (char *)&scr
;
486 data
.flags
= MMC_DATA_READ
;
488 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
497 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
498 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
500 switch ((mmc
->scr
[0] >> 24) & 0xf) {
502 mmc
->version
= SD_VERSION_1_0
;
505 mmc
->version
= SD_VERSION_1_10
;
508 mmc
->version
= SD_VERSION_2
;
511 mmc
->version
= SD_VERSION_1_0
;
515 /* Version 1.0 doesn't support switching */
516 if (mmc
->version
== SD_VERSION_1_0
)
521 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
522 (u8
*)&switch_status
);
527 /* The high-speed function is busy. Try again */
528 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
532 if (mmc
->scr
[0] & SD_DATA_4BIT
)
533 mmc
->card_caps
|= MMC_MODE_4BIT
;
535 /* If high-speed isn't supported, we return */
536 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
539 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)&switch_status
);
544 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
545 mmc
->card_caps
|= MMC_MODE_HS
;
550 /* frequency bases */
551 /* divided by 10 to be nice to platforms without floating point */
559 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
560 * to platforms without floating point.
562 int multipliers
[] = {
581 void mmc_set_ios(struct mmc
*mmc
)
586 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
588 if (clock
> mmc
->f_max
)
591 if (clock
< mmc
->f_min
)
599 void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
601 mmc
->bus_width
= width
;
606 int mmc_startup(struct mmc
*mmc
)
614 /* Put the Card in Identify Mode */
615 cmd
.cmdidx
= MMC_CMD_ALL_SEND_CID
;
616 cmd
.resp_type
= MMC_RSP_R2
;
620 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
625 memcpy(mmc
->cid
, cmd
.response
, 16);
628 * For MMC cards, set the Relative Address.
629 * For SD cards, get the Relatvie Address.
630 * This also puts the cards into Standby State
632 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
633 cmd
.cmdarg
= mmc
->rca
<< 16;
634 cmd
.resp_type
= MMC_RSP_R6
;
637 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
643 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
645 /* Get the Card-Specific Data */
646 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
647 cmd
.resp_type
= MMC_RSP_R2
;
648 cmd
.cmdarg
= mmc
->rca
<< 16;
651 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
656 mmc
->csd
[0] = cmd
.response
[0];
657 mmc
->csd
[1] = cmd
.response
[1];
658 mmc
->csd
[2] = cmd
.response
[2];
659 mmc
->csd
[3] = cmd
.response
[3];
661 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
662 int version
= (cmd
.response
[0] >> 26) & 0xf;
666 mmc
->version
= MMC_VERSION_1_2
;
669 mmc
->version
= MMC_VERSION_1_4
;
672 mmc
->version
= MMC_VERSION_2_2
;
675 mmc
->version
= MMC_VERSION_3
;
678 mmc
->version
= MMC_VERSION_4
;
681 mmc
->version
= MMC_VERSION_1_2
;
686 /* divide frequency by 10, since the mults are 10x bigger */
687 freq
= fbase
[(cmd
.response
[0] & 0x7)];
688 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
690 mmc
->tran_speed
= freq
* mult
;
692 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
695 mmc
->write_bl_len
= mmc
->read_bl_len
;
697 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
699 if (mmc
->high_capacity
) {
700 csize
= (mmc
->csd
[1] & 0x3f) << 16
701 | (mmc
->csd
[2] & 0xffff0000) >> 16;
704 csize
= (mmc
->csd
[1] & 0x3ff) << 2
705 | (mmc
->csd
[2] & 0xc0000000) >> 30;
706 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
709 mmc
->capacity
= (csize
+ 1) << (cmult
+ 2);
710 mmc
->capacity
*= mmc
->read_bl_len
;
712 if (mmc
->read_bl_len
> 512)
713 mmc
->read_bl_len
= 512;
715 if (mmc
->write_bl_len
> 512)
716 mmc
->write_bl_len
= 512;
718 /* Select the card, and put it into Transfer Mode */
719 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
720 cmd
.resp_type
= MMC_RSP_R1b
;
721 cmd
.cmdarg
= mmc
->rca
<< 16;
723 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
728 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
729 /* check ext_csd version and capacity */
730 err
= mmc_send_ext_csd(mmc
, ext_csd
);
731 if (!err
& (ext_csd
[192] >= 2)) {
732 mmc
->capacity
= ext_csd
[212] << 0 | ext_csd
[213] << 8 |
733 ext_csd
[214] << 16 | ext_csd
[215] << 24;
734 mmc
->capacity
*= 512;
739 err
= sd_change_freq(mmc
);
741 err
= mmc_change_freq(mmc
);
746 /* Restrict card's capabilities by what the host can do */
747 mmc
->card_caps
&= mmc
->host_caps
;
750 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
751 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
752 cmd
.resp_type
= MMC_RSP_R1
;
753 cmd
.cmdarg
= mmc
->rca
<< 16;
756 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
760 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
761 cmd
.resp_type
= MMC_RSP_R1
;
764 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
768 mmc_set_bus_width(mmc
, 4);
771 if (mmc
->card_caps
& MMC_MODE_HS
)
772 mmc_set_clock(mmc
, 50000000);
774 mmc_set_clock(mmc
, 25000000);
776 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
777 /* Set the card to use 4 bit*/
778 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
780 EXT_CSD_BUS_WIDTH_4
);
785 mmc_set_bus_width(mmc
, 4);
786 } else if (mmc
->card_caps
& MMC_MODE_8BIT
) {
787 /* Set the card to use 8 bit*/
788 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
790 EXT_CSD_BUS_WIDTH_8
);
795 mmc_set_bus_width(mmc
, 8);
798 if (mmc
->card_caps
& MMC_MODE_HS
) {
799 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
800 mmc_set_clock(mmc
, 52000000);
802 mmc_set_clock(mmc
, 26000000);
804 mmc_set_clock(mmc
, 20000000);
807 /* fill in device description */
808 mmc
->block_dev
.lun
= 0;
809 mmc
->block_dev
.type
= 0;
810 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
811 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
812 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %08x", mmc
->cid
[0] >> 8,
813 (mmc
->cid
[2] << 8) | (mmc
->cid
[3] >> 24));
814 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c", mmc
->cid
[0] & 0xff,
815 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
816 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff);
817 sprintf(mmc
->block_dev
.revision
, "%d.%d", mmc
->cid
[2] >> 28,
818 (mmc
->cid
[2] >> 24) & 0xf);
819 init_part(&mmc
->block_dev
);
824 int mmc_send_if_cond(struct mmc
*mmc
)
829 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
830 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
831 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
832 cmd
.resp_type
= MMC_RSP_R7
;
835 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
840 if ((cmd
.response
[0] & 0xff) != 0xaa)
843 mmc
->version
= SD_VERSION_2
;
848 int mmc_register(struct mmc
*mmc
)
850 /* Setup the universal parts of the block interface just once */
851 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
852 mmc
->block_dev
.dev
= cur_dev_num
++;
853 mmc
->block_dev
.removable
= 1;
854 mmc
->block_dev
.block_read
= mmc_bread
;
855 mmc
->block_dev
.block_write
= mmc_bwrite
;
857 INIT_LIST_HEAD (&mmc
->link
);
859 list_add_tail (&mmc
->link
, &mmc_devices
);
864 block_dev_desc_t
*mmc_get_dev(int dev
)
866 struct mmc
*mmc
= find_mmc_device(dev
);
868 return mmc
? &mmc
->block_dev
: NULL
;
871 int mmc_init(struct mmc
*mmc
)
875 err
= mmc
->init(mmc
);
880 mmc_set_bus_width(mmc
, 1);
881 mmc_set_clock(mmc
, 1);
884 err
= mmc_go_idle(mmc
);
889 /* Test for SD version 2 */
890 err
= mmc_send_if_cond(mmc
);
892 /* Now try to get the SD card's operating condition */
893 err
= sd_send_op_cond(mmc
);
895 /* If the command timed out, we check for an MMC card */
896 if (err
== TIMEOUT
) {
897 err
= mmc_send_op_cond(mmc
);
900 printf("Card did not respond to voltage select!\n");
905 return mmc_startup(mmc
);
909 * CPU and board-specific MMC initializations. Aliased function
910 * signals caller to move on
912 static int __def_mmc_init(bd_t
*bis
)
917 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
918 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
920 void print_mmc_devices(char separator
)
923 struct list_head
*entry
;
925 list_for_each(entry
, &mmc_devices
) {
926 m
= list_entry(entry
, struct mmc
, link
);
928 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
930 if (entry
->next
!= &mmc_devices
)
931 printf("%c ", separator
);
937 int mmc_initialize(bd_t
*bis
)
939 INIT_LIST_HEAD (&mmc_devices
);
942 if (board_mmc_init(bis
) < 0)
945 print_mmc_devices(',');