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 mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
40 return mmc
->send_cmd(mmc
, cmd
, data
);
43 int mmc_set_blocklen(struct mmc
*mmc
, int len
)
47 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
48 cmd
.resp_type
= MMC_RSP_R1
;
52 return mmc_send_cmd(mmc
, &cmd
, NULL
);
55 struct mmc
*find_mmc_device(int dev_num
)
58 struct list_head
*entry
;
60 list_for_each(entry
, &mmc_devices
) {
61 m
= list_entry(entry
, struct mmc
, link
);
63 if (m
->block_dev
.dev
== dev_num
)
67 printf("MMC Device %d not found\n", dev_num
);
73 mmc_bwrite(int dev_num
, ulong start
, lbaint_t blkcnt
, const void*src
)
79 struct mmc
*mmc
= find_mmc_device(dev_num
);
85 blklen
= mmc
->write_bl_len
;
87 err
= mmc_set_blocklen(mmc
, mmc
->write_bl_len
);
90 printf("set write bl len failed\n\r");
95 cmd
.cmdidx
= MMC_CMD_WRITE_MULTIPLE_BLOCK
;
97 cmd
.cmdidx
= MMC_CMD_WRITE_SINGLE_BLOCK
;
99 if (mmc
->high_capacity
)
102 cmd
.cmdarg
= start
* blklen
;
104 cmd
.resp_type
= MMC_RSP_R1
;
108 data
.blocks
= blkcnt
;
109 data
.blocksize
= blklen
;
110 data
.flags
= MMC_DATA_WRITE
;
112 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
115 printf("mmc write failed\n\r");
120 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
122 cmd
.resp_type
= MMC_RSP_R1b
;
124 stoperr
= mmc_send_cmd(mmc
, &cmd
, NULL
);
130 int mmc_read_block(struct mmc
*mmc
, void *dst
, uint blocknum
)
133 struct mmc_data data
;
135 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
137 if (mmc
->high_capacity
)
138 cmd
.cmdarg
= blocknum
;
140 cmd
.cmdarg
= blocknum
* mmc
->read_bl_len
;
142 cmd
.resp_type
= MMC_RSP_R1
;
147 data
.blocksize
= mmc
->read_bl_len
;
148 data
.flags
= MMC_DATA_READ
;
150 return mmc_send_cmd(mmc
, &cmd
, &data
);
153 int mmc_read(struct mmc
*mmc
, u64 src
, uchar
*dst
, int size
)
157 int blklen
= mmc
->read_bl_len
;
158 int startblock
= src
/ blklen
;
159 int endblock
= (src
+ size
- 1) / blklen
;
162 /* Make a buffer big enough to hold all the blocks we might read */
163 buffer
= malloc(blklen
);
166 printf("Could not allocate buffer for MMC read!\n");
170 /* We always do full block reads from the card */
171 err
= mmc_set_blocklen(mmc
, mmc
->read_bl_len
);
176 for (i
= startblock
; i
<= endblock
; i
++) {
180 err
= mmc_read_block(mmc
, buffer
, i
);
186 * The first block may not be aligned, so we
187 * copy from the desired point in the block
189 offset
= (src
& (blklen
- 1));
190 segment_size
= MIN(blklen
- offset
, size
);
192 memcpy(dst
, buffer
+ offset
, segment_size
);
196 size
-= segment_size
;
205 static ulong
mmc_bread(int dev_num
, ulong start
, lbaint_t blkcnt
, void *dst
)
209 struct mmc
*mmc
= find_mmc_device(dev_num
);
214 /* We always do full block reads from the card */
215 err
= mmc_set_blocklen(mmc
, mmc
->read_bl_len
);
221 for (i
= start
; i
< start
+ blkcnt
; i
++, dst
+= mmc
->read_bl_len
) {
222 err
= mmc_read_block(mmc
, dst
, i
);
225 printf("block read failed: %d\n", err
);
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
;
275 cmd
.cmdarg
= mmc
->voltages
;
277 if (mmc
->version
== SD_VERSION_2
)
278 cmd
.cmdarg
|= OCR_HCS
;
280 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
286 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
291 if (mmc
->version
!= SD_VERSION_2
)
292 mmc
->version
= SD_VERSION_1_0
;
294 mmc
->ocr
= ((uint
*)(cmd
.response
))[0];
296 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
302 int mmc_send_op_cond(struct mmc
*mmc
)
308 /* Some cards seem to need this */
312 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
313 cmd
.resp_type
= MMC_RSP_R3
;
314 cmd
.cmdarg
= OCR_HCS
| mmc
->voltages
;
317 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
323 } while (!(cmd
.response
[0] & OCR_BUSY
) && timeout
--);
328 mmc
->version
= MMC_VERSION_UNKNOWN
;
329 mmc
->ocr
= ((uint
*)(cmd
.response
))[0];
331 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
338 int mmc_send_ext_csd(struct mmc
*mmc
, char *ext_csd
)
341 struct mmc_data data
;
344 /* Get the Card Status Register */
345 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
346 cmd
.resp_type
= MMC_RSP_R1
;
352 data
.blocksize
= 512;
353 data
.flags
= MMC_DATA_READ
;
355 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
361 int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
365 cmd
.cmdidx
= MMC_CMD_SWITCH
;
366 cmd
.resp_type
= MMC_RSP_R1b
;
367 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
372 return mmc_send_cmd(mmc
, &cmd
, NULL
);
375 int mmc_change_freq(struct mmc
*mmc
)
383 /* Only version 4 supports high-speed */
384 if (mmc
->version
< MMC_VERSION_4
)
387 mmc
->card_caps
|= MMC_MODE_4BIT
;
389 err
= mmc_send_ext_csd(mmc
, ext_csd
);
394 if (ext_csd
[212] || ext_csd
[213] || ext_csd
[214] || ext_csd
[215])
395 mmc
->high_capacity
= 1;
397 cardtype
= ext_csd
[196] & 0xf;
399 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
404 /* Now check to see that it worked */
405 err
= mmc_send_ext_csd(mmc
, ext_csd
);
410 /* No high-speed support */
414 /* High Speed is set, there are two types: 52MHz and 26MHz */
415 if (cardtype
& MMC_HS_52MHZ
)
416 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
418 mmc
->card_caps
|= MMC_MODE_HS
;
423 int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
426 struct mmc_data data
;
428 /* Switch the frequency */
429 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
430 cmd
.resp_type
= MMC_RSP_R1
;
431 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
432 cmd
.cmdarg
&= ~(0xf << (group
* 4));
433 cmd
.cmdarg
|= value
<< (group
* 4);
436 data
.dest
= (char *)resp
;
439 data
.flags
= MMC_DATA_READ
;
441 return mmc_send_cmd(mmc
, &cmd
, &data
);
445 int sd_change_freq(struct mmc
*mmc
)
450 uint switch_status
[16];
451 struct mmc_data data
;
456 /* Read the SCR to find out if this card supports higher speeds */
457 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
458 cmd
.resp_type
= MMC_RSP_R1
;
459 cmd
.cmdarg
= mmc
->rca
<< 16;
462 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
467 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
468 cmd
.resp_type
= MMC_RSP_R1
;
475 data
.dest
= (char *)&scr
;
478 data
.flags
= MMC_DATA_READ
;
480 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
489 mmc
->scr
[0] = scr
[0];
490 mmc
->scr
[1] = scr
[1];
492 switch ((mmc
->scr
[0] >> 24) & 0xf) {
494 mmc
->version
= SD_VERSION_1_0
;
497 mmc
->version
= SD_VERSION_1_10
;
500 mmc
->version
= SD_VERSION_2
;
503 mmc
->version
= SD_VERSION_1_0
;
507 /* Version 1.0 doesn't support switching */
508 if (mmc
->version
== SD_VERSION_1_0
)
513 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
514 (u8
*)&switch_status
);
519 /* The high-speed function is busy. Try again */
520 if (!switch_status
[7] & SD_HIGHSPEED_BUSY
)
524 if (mmc
->scr
[0] & SD_DATA_4BIT
)
525 mmc
->card_caps
|= MMC_MODE_4BIT
;
527 /* If high-speed isn't supported, we return */
528 if (!(switch_status
[3] & SD_HIGHSPEED_SUPPORTED
))
531 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)&switch_status
);
536 if ((switch_status
[4] & 0x0f000000) == 0x01000000)
537 mmc
->card_caps
|= MMC_MODE_HS
;
542 /* frequency bases */
543 /* divided by 10 to be nice to platforms without floating point */
551 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
552 * to platforms without floating point.
554 int multipliers
[] = {
573 void mmc_set_ios(struct mmc
*mmc
)
578 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
580 if (clock
> mmc
->f_max
)
583 if (clock
< mmc
->f_min
)
591 void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
593 mmc
->bus_width
= width
;
598 int mmc_startup(struct mmc
*mmc
)
605 /* Put the Card in Identify Mode */
606 cmd
.cmdidx
= MMC_CMD_ALL_SEND_CID
;
607 cmd
.resp_type
= MMC_RSP_R2
;
611 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
616 memcpy(mmc
->cid
, cmd
.response
, 16);
619 * For MMC cards, set the Relative Address.
620 * For SD cards, get the Relatvie Address.
621 * This also puts the cards into Standby State
623 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
624 cmd
.cmdarg
= mmc
->rca
<< 16;
625 cmd
.resp_type
= MMC_RSP_R6
;
628 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
634 mmc
->rca
= (((uint
*)(cmd
.response
))[0] >> 16) & 0xffff;
636 /* Get the Card-Specific Data */
637 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
638 cmd
.resp_type
= MMC_RSP_R2
;
639 cmd
.cmdarg
= mmc
->rca
<< 16;
642 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
647 mmc
->csd
[0] = ((uint
*)(cmd
.response
))[0];
648 mmc
->csd
[1] = ((uint
*)(cmd
.response
))[1];
649 mmc
->csd
[2] = ((uint
*)(cmd
.response
))[2];
650 mmc
->csd
[3] = ((uint
*)(cmd
.response
))[3];
652 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
653 int version
= (cmd
.response
[0] >> 2) & 0xf;
657 mmc
->version
= MMC_VERSION_1_2
;
660 mmc
->version
= MMC_VERSION_1_4
;
663 mmc
->version
= MMC_VERSION_2_2
;
666 mmc
->version
= MMC_VERSION_3
;
669 mmc
->version
= MMC_VERSION_4
;
672 mmc
->version
= MMC_VERSION_1_2
;
677 /* divide frequency by 10, since the mults are 10x bigger */
678 freq
= fbase
[(cmd
.response
[3] & 0x7)];
679 mult
= multipliers
[((cmd
.response
[3] >> 3) & 0xf)];
681 mmc
->tran_speed
= freq
* mult
;
683 mmc
->read_bl_len
= 1 << ((((uint
*)(cmd
.response
))[1] >> 16) & 0xf);
686 mmc
->write_bl_len
= mmc
->read_bl_len
;
688 mmc
->write_bl_len
= 1 << ((((uint
*)(cmd
.response
))[3] >> 22) & 0xf);
690 if (mmc
->high_capacity
) {
691 csize
= (mmc
->csd
[1] & 0x3f) << 16
692 | (mmc
->csd
[2] & 0xffff0000) >> 16;
695 csize
= (mmc
->csd
[1] & 0x3ff) << 2
696 | (mmc
->csd
[2] & 0xc0000000) >> 30;
697 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
700 mmc
->capacity
= (csize
+ 1) << (cmult
+ 2);
701 mmc
->capacity
*= mmc
->read_bl_len
;
703 if (mmc
->read_bl_len
> 512)
704 mmc
->read_bl_len
= 512;
706 if (mmc
->write_bl_len
> 512)
707 mmc
->write_bl_len
= 512;
709 /* Select the card, and put it into Transfer Mode */
710 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
711 cmd
.resp_type
= MMC_RSP_R1b
;
712 cmd
.cmdarg
= mmc
->rca
<< 16;
714 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
720 err
= sd_change_freq(mmc
);
722 err
= mmc_change_freq(mmc
);
727 /* Restrict card's capabilities by what the host can do */
728 mmc
->card_caps
&= mmc
->host_caps
;
731 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
732 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
733 cmd
.resp_type
= MMC_RSP_R1
;
734 cmd
.cmdarg
= mmc
->rca
<< 16;
737 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
741 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
742 cmd
.resp_type
= MMC_RSP_R1
;
745 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
749 mmc_set_bus_width(mmc
, 4);
752 if (mmc
->card_caps
& MMC_MODE_HS
)
753 mmc_set_clock(mmc
, 50000000);
755 mmc_set_clock(mmc
, 25000000);
757 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
758 /* Set the card to use 4 bit*/
759 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
761 EXT_CSD_BUS_WIDTH_4
);
766 mmc_set_bus_width(mmc
, 4);
767 } else if (mmc
->card_caps
& MMC_MODE_8BIT
) {
768 /* Set the card to use 8 bit*/
769 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
771 EXT_CSD_BUS_WIDTH_8
);
776 mmc_set_bus_width(mmc
, 8);
779 if (mmc
->card_caps
& MMC_MODE_HS
) {
780 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
781 mmc_set_clock(mmc
, 52000000);
783 mmc_set_clock(mmc
, 26000000);
785 mmc_set_clock(mmc
, 20000000);
788 /* fill in device description */
789 mmc
->block_dev
.lun
= 0;
790 mmc
->block_dev
.type
= 0;
791 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
792 mmc
->block_dev
.lba
= mmc
->capacity
/mmc
->read_bl_len
;
793 sprintf(mmc
->block_dev
.vendor
,"Man %02x%02x%02x Snr %02x%02x%02x%02x",
794 mmc
->cid
[0], mmc
->cid
[1], mmc
->cid
[2],
795 mmc
->cid
[9], mmc
->cid
[10], mmc
->cid
[11], mmc
->cid
[12]);
796 sprintf(mmc
->block_dev
.product
,"%c%c%c%c%c", mmc
->cid
[3],
797 mmc
->cid
[4], mmc
->cid
[5], mmc
->cid
[6], mmc
->cid
[7]);
798 sprintf(mmc
->block_dev
.revision
,"%d.%d", mmc
->cid
[8] >> 4,
800 init_part(&mmc
->block_dev
);
805 int mmc_send_if_cond(struct mmc
*mmc
)
810 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
811 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
812 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
813 cmd
.resp_type
= MMC_RSP_R7
;
816 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
821 if ((((uint
*)(cmd
.response
))[0] & 0xff) != 0xaa)
824 mmc
->version
= SD_VERSION_2
;
829 int mmc_register(struct mmc
*mmc
)
831 /* Setup the universal parts of the block interface just once */
832 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
833 mmc
->block_dev
.dev
= cur_dev_num
++;
834 mmc
->block_dev
.removable
= 1;
835 mmc
->block_dev
.block_read
= mmc_bread
;
836 mmc
->block_dev
.block_write
= mmc_bwrite
;
838 INIT_LIST_HEAD (&mmc
->link
);
840 list_add_tail (&mmc
->link
, &mmc_devices
);
845 block_dev_desc_t
*mmc_get_dev(int dev
)
847 struct mmc
*mmc
= find_mmc_device(dev
);
849 return mmc
? &mmc
->block_dev
: NULL
;
852 int mmc_init(struct mmc
*mmc
)
856 err
= mmc
->init(mmc
);
862 err
= mmc_go_idle(mmc
);
867 /* Test for SD version 2 */
868 err
= mmc_send_if_cond(mmc
);
870 /* If we got an error other than timeout, we bail */
871 if (err
&& err
!= TIMEOUT
)
874 /* Now try to get the SD card's operating condition */
875 err
= sd_send_op_cond(mmc
);
877 /* If the command timed out, we check for an MMC card */
878 if (err
== TIMEOUT
) {
879 err
= mmc_send_op_cond(mmc
);
882 printf("Card did not respond to voltage select!\n");
887 return mmc_startup(mmc
);
891 * CPU and board-specific MMC initializations. Aliased function
892 * signals caller to move on
894 static int __def_mmc_init(bd_t
*bis
)
899 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
900 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
902 void print_mmc_devices(char separator
)
905 struct list_head
*entry
;
907 list_for_each(entry
, &mmc_devices
) {
908 m
= list_entry(entry
, struct mmc
, link
);
910 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
912 if (entry
->next
!= &mmc_devices
)
913 printf("%c ", separator
);
919 int mmc_initialize(bd_t
*bis
)
921 INIT_LIST_HEAD (&mmc_devices
);
924 if (board_mmc_init(bis
) < 0)
927 print_mmc_devices(',');