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
= ((uint
*)(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
= ((uint
*)(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 cardtype
= ext_csd
[196] & 0xf;
400 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
405 /* Now check to see that it worked */
406 err
= mmc_send_ext_csd(mmc
, ext_csd
);
411 /* No high-speed support */
415 /* High Speed is set, there are two types: 52MHz and 26MHz */
416 if (cardtype
& MMC_HS_52MHZ
)
417 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
419 mmc
->card_caps
|= MMC_MODE_HS
;
424 int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
427 struct mmc_data data
;
429 /* Switch the frequency */
430 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
431 cmd
.resp_type
= MMC_RSP_R1
;
432 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
433 cmd
.cmdarg
&= ~(0xf << (group
* 4));
434 cmd
.cmdarg
|= value
<< (group
* 4);
437 data
.dest
= (char *)resp
;
440 data
.flags
= MMC_DATA_READ
;
442 return mmc_send_cmd(mmc
, &cmd
, &data
);
446 int sd_change_freq(struct mmc
*mmc
)
451 uint switch_status
[16];
452 struct mmc_data data
;
457 /* Read the SCR to find out if this card supports higher speeds */
458 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
459 cmd
.resp_type
= MMC_RSP_R1
;
460 cmd
.cmdarg
= mmc
->rca
<< 16;
463 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
468 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
469 cmd
.resp_type
= MMC_RSP_R1
;
476 data
.dest
= (char *)&scr
;
479 data
.flags
= MMC_DATA_READ
;
481 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
490 mmc
->scr
[0] = scr
[0];
491 mmc
->scr
[1] = scr
[1];
493 switch ((mmc
->scr
[0] >> 24) & 0xf) {
495 mmc
->version
= SD_VERSION_1_0
;
498 mmc
->version
= SD_VERSION_1_10
;
501 mmc
->version
= SD_VERSION_2
;
504 mmc
->version
= SD_VERSION_1_0
;
508 /* Version 1.0 doesn't support switching */
509 if (mmc
->version
== SD_VERSION_1_0
)
514 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
515 (u8
*)&switch_status
);
520 /* The high-speed function is busy. Try again */
521 if (!switch_status
[7] & SD_HIGHSPEED_BUSY
)
525 if (mmc
->scr
[0] & SD_DATA_4BIT
)
526 mmc
->card_caps
|= MMC_MODE_4BIT
;
528 /* If high-speed isn't supported, we return */
529 if (!(switch_status
[3] & SD_HIGHSPEED_SUPPORTED
))
532 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)&switch_status
);
537 if ((switch_status
[4] & 0x0f000000) == 0x01000000)
538 mmc
->card_caps
|= MMC_MODE_HS
;
543 /* frequency bases */
544 /* divided by 10 to be nice to platforms without floating point */
552 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
553 * to platforms without floating point.
555 int multipliers
[] = {
574 void mmc_set_ios(struct mmc
*mmc
)
579 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
581 if (clock
> mmc
->f_max
)
584 if (clock
< mmc
->f_min
)
592 void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
594 mmc
->bus_width
= width
;
599 int mmc_startup(struct mmc
*mmc
)
606 /* Put the Card in Identify Mode */
607 cmd
.cmdidx
= MMC_CMD_ALL_SEND_CID
;
608 cmd
.resp_type
= MMC_RSP_R2
;
612 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
617 memcpy(mmc
->cid
, cmd
.response
, 16);
620 * For MMC cards, set the Relative Address.
621 * For SD cards, get the Relatvie Address.
622 * This also puts the cards into Standby State
624 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
625 cmd
.cmdarg
= mmc
->rca
<< 16;
626 cmd
.resp_type
= MMC_RSP_R6
;
629 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
635 mmc
->rca
= (((uint
*)(cmd
.response
))[0] >> 16) & 0xffff;
637 /* Get the Card-Specific Data */
638 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
639 cmd
.resp_type
= MMC_RSP_R2
;
640 cmd
.cmdarg
= mmc
->rca
<< 16;
643 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
648 mmc
->csd
[0] = ((uint
*)(cmd
.response
))[0];
649 mmc
->csd
[1] = ((uint
*)(cmd
.response
))[1];
650 mmc
->csd
[2] = ((uint
*)(cmd
.response
))[2];
651 mmc
->csd
[3] = ((uint
*)(cmd
.response
))[3];
653 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
654 int version
= (cmd
.response
[0] >> 26) & 0xf;
658 mmc
->version
= MMC_VERSION_1_2
;
661 mmc
->version
= MMC_VERSION_1_4
;
664 mmc
->version
= MMC_VERSION_2_2
;
667 mmc
->version
= MMC_VERSION_3
;
670 mmc
->version
= MMC_VERSION_4
;
673 mmc
->version
= MMC_VERSION_1_2
;
678 /* divide frequency by 10, since the mults are 10x bigger */
679 freq
= fbase
[(cmd
.response
[0] & 0x7)];
680 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
682 mmc
->tran_speed
= freq
* mult
;
684 mmc
->read_bl_len
= 1 << ((((uint
*)(cmd
.response
))[1] >> 16) & 0xf);
687 mmc
->write_bl_len
= mmc
->read_bl_len
;
689 mmc
->write_bl_len
= 1 << ((((uint
*)(cmd
.response
))[3] >> 22) & 0xf);
691 if (mmc
->high_capacity
) {
692 csize
= (mmc
->csd
[1] & 0x3f) << 16
693 | (mmc
->csd
[2] & 0xffff0000) >> 16;
696 csize
= (mmc
->csd
[1] & 0x3ff) << 2
697 | (mmc
->csd
[2] & 0xc0000000) >> 30;
698 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
701 mmc
->capacity
= (csize
+ 1) << (cmult
+ 2);
702 mmc
->capacity
*= mmc
->read_bl_len
;
704 if (mmc
->read_bl_len
> 512)
705 mmc
->read_bl_len
= 512;
707 if (mmc
->write_bl_len
> 512)
708 mmc
->write_bl_len
= 512;
710 /* Select the card, and put it into Transfer Mode */
711 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
712 cmd
.resp_type
= MMC_RSP_R1b
;
713 cmd
.cmdarg
= mmc
->rca
<< 16;
715 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
721 err
= sd_change_freq(mmc
);
723 err
= mmc_change_freq(mmc
);
728 /* Restrict card's capabilities by what the host can do */
729 mmc
->card_caps
&= mmc
->host_caps
;
732 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
733 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
734 cmd
.resp_type
= MMC_RSP_R1
;
735 cmd
.cmdarg
= mmc
->rca
<< 16;
738 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
742 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
743 cmd
.resp_type
= MMC_RSP_R1
;
746 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
750 mmc_set_bus_width(mmc
, 4);
753 if (mmc
->card_caps
& MMC_MODE_HS
)
754 mmc_set_clock(mmc
, 50000000);
756 mmc_set_clock(mmc
, 25000000);
758 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
759 /* Set the card to use 4 bit*/
760 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
762 EXT_CSD_BUS_WIDTH_4
);
767 mmc_set_bus_width(mmc
, 4);
768 } else if (mmc
->card_caps
& MMC_MODE_8BIT
) {
769 /* Set the card to use 8 bit*/
770 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
772 EXT_CSD_BUS_WIDTH_8
);
777 mmc_set_bus_width(mmc
, 8);
780 if (mmc
->card_caps
& MMC_MODE_HS
) {
781 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
782 mmc_set_clock(mmc
, 52000000);
784 mmc_set_clock(mmc
, 26000000);
786 mmc_set_clock(mmc
, 20000000);
789 /* fill in device description */
790 mmc
->block_dev
.lun
= 0;
791 mmc
->block_dev
.type
= 0;
792 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
793 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
794 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %08x", mmc
->cid
[0] >> 8,
795 (mmc
->cid
[2] << 8) | (mmc
->cid
[3] >> 24));
796 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c", mmc
->cid
[0] & 0xff,
797 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
798 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff);
799 sprintf(mmc
->block_dev
.revision
, "%d.%d", mmc
->cid
[2] >> 28,
800 (mmc
->cid
[2] >> 24) & 0xf);
801 init_part(&mmc
->block_dev
);
806 int mmc_send_if_cond(struct mmc
*mmc
)
811 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
812 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
813 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
814 cmd
.resp_type
= MMC_RSP_R7
;
817 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
822 if ((((uint
*)(cmd
.response
))[0] & 0xff) != 0xaa)
825 mmc
->version
= SD_VERSION_2
;
830 int mmc_register(struct mmc
*mmc
)
832 /* Setup the universal parts of the block interface just once */
833 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
834 mmc
->block_dev
.dev
= cur_dev_num
++;
835 mmc
->block_dev
.removable
= 1;
836 mmc
->block_dev
.block_read
= mmc_bread
;
837 mmc
->block_dev
.block_write
= mmc_bwrite
;
839 INIT_LIST_HEAD (&mmc
->link
);
841 list_add_tail (&mmc
->link
, &mmc_devices
);
846 block_dev_desc_t
*mmc_get_dev(int dev
)
848 struct mmc
*mmc
= find_mmc_device(dev
);
850 return mmc
? &mmc
->block_dev
: NULL
;
853 int mmc_init(struct mmc
*mmc
)
857 err
= mmc
->init(mmc
);
863 err
= mmc_go_idle(mmc
);
868 /* Test for SD version 2 */
869 err
= mmc_send_if_cond(mmc
);
871 /* If we got an error other than timeout, we bail */
872 if (err
&& err
!= TIMEOUT
)
875 /* Now try to get the SD card's operating condition */
876 err
= sd_send_op_cond(mmc
);
878 /* If the command timed out, we check for an MMC card */
879 if (err
== TIMEOUT
) {
880 err
= mmc_send_op_cond(mmc
);
883 printf("Card did not respond to voltage select!\n");
888 return mmc_startup(mmc
);
892 * CPU and board-specific MMC initializations. Aliased function
893 * signals caller to move on
895 static int __def_mmc_init(bd_t
*bis
)
900 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
901 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
903 void print_mmc_devices(char separator
)
906 struct list_head
*entry
;
908 list_for_each(entry
, &mmc_devices
) {
909 m
= list_entry(entry
, struct mmc
, link
);
911 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
913 if (entry
->next
!= &mmc_devices
)
914 printf("%c ", separator
);
920 int mmc_initialize(bd_t
*bis
)
922 INIT_LIST_HEAD (&mmc_devices
);
925 if (board_mmc_init(bis
) < 0)
928 print_mmc_devices(',');