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 /* Set block count limit because of 16 bit register limit on some hardware*/
36 #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37 #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
40 static struct list_head mmc_devices
;
41 static int cur_dev_num
= -1;
43 int __board_mmc_getcd(struct mmc
*mmc
) {
47 int board_mmc_getcd(struct mmc
*mmc
)__attribute__((weak
,
48 alias("__board_mmc_getcd")));
50 int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
52 #ifdef CONFIG_MMC_TRACE
57 printf("CMD_SEND:%d\n", cmd
->cmdidx
);
58 printf("\t\tARG\t\t\t 0x%08X\n", cmd
->cmdarg
);
59 printf("\t\tFLAG\t\t\t %d\n", cmd
->flags
);
60 ret
= mmc
->send_cmd(mmc
, cmd
, data
);
61 switch (cmd
->resp_type
) {
63 printf("\t\tMMC_RSP_NONE\n");
66 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
70 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
74 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
76 printf("\t\t \t\t 0x%08X \n",
78 printf("\t\t \t\t 0x%08X \n",
80 printf("\t\t \t\t 0x%08X \n",
83 printf("\t\t\t\t\tDUMPING DATA\n");
84 for (i
= 0; i
< 4; i
++) {
86 printf("\t\t\t\t\t%03d - ", i
*4);
87 ptr
= &cmd
->response
[i
];
89 for (j
= 0; j
< 4; j
++)
90 printf("%02X ", *ptr
--);
95 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
99 printf("\t\tERROR MMC rsp not supported\n");
104 return mmc
->send_cmd(mmc
, cmd
, data
);
108 int mmc_send_status(struct mmc
*mmc
, int timeout
)
111 int err
, retries
= 5;
112 #ifdef CONFIG_MMC_TRACE
116 cmd
.cmdidx
= MMC_CMD_SEND_STATUS
;
117 cmd
.resp_type
= MMC_RSP_R1
;
118 if (!mmc_host_is_spi(mmc
))
119 cmd
.cmdarg
= mmc
->rca
<< 16;
123 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
125 if ((cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
) &&
126 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) !=
129 else if (cmd
.response
[0] & MMC_STATUS_MASK
) {
130 printf("Status Error: 0x%08X\n",
134 } else if (--retries
< 0)
141 #ifdef CONFIG_MMC_TRACE
142 status
= (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) >> 9;
143 printf("CURR STATE:%d\n", status
);
146 printf("Timeout waiting card ready\n");
153 int mmc_set_blocklen(struct mmc
*mmc
, int len
)
157 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
158 cmd
.resp_type
= MMC_RSP_R1
;
162 return mmc_send_cmd(mmc
, &cmd
, NULL
);
165 struct mmc
*find_mmc_device(int dev_num
)
168 struct list_head
*entry
;
170 list_for_each(entry
, &mmc_devices
) {
171 m
= list_entry(entry
, struct mmc
, link
);
173 if (m
->block_dev
.dev
== dev_num
)
177 printf("MMC Device %d not found\n", dev_num
);
182 static ulong
mmc_erase_t(struct mmc
*mmc
, ulong start
, lbaint_t blkcnt
)
186 int err
, start_cmd
, end_cmd
;
188 if (mmc
->high_capacity
)
189 end
= start
+ blkcnt
- 1;
191 end
= (start
+ blkcnt
- 1) * mmc
->write_bl_len
;
192 start
*= mmc
->write_bl_len
;
196 start_cmd
= SD_CMD_ERASE_WR_BLK_START
;
197 end_cmd
= SD_CMD_ERASE_WR_BLK_END
;
199 start_cmd
= MMC_CMD_ERASE_GROUP_START
;
200 end_cmd
= MMC_CMD_ERASE_GROUP_END
;
203 cmd
.cmdidx
= start_cmd
;
205 cmd
.resp_type
= MMC_RSP_R1
;
208 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
212 cmd
.cmdidx
= end_cmd
;
215 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
219 cmd
.cmdidx
= MMC_CMD_ERASE
;
220 cmd
.cmdarg
= SECURE_ERASE
;
221 cmd
.resp_type
= MMC_RSP_R1b
;
223 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
230 puts("mmc erase failed\n");
235 mmc_berase(int dev_num
, unsigned long start
, lbaint_t blkcnt
)
238 struct mmc
*mmc
= find_mmc_device(dev_num
);
239 lbaint_t blk
= 0, blk_r
= 0;
244 if ((start
% mmc
->erase_grp_size
) || (blkcnt
% mmc
->erase_grp_size
))
245 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
246 "The erase range would be change to 0x%lx~0x%lx\n\n",
247 mmc
->erase_grp_size
, start
& ~(mmc
->erase_grp_size
- 1),
248 ((start
+ blkcnt
+ mmc
->erase_grp_size
)
249 & ~(mmc
->erase_grp_size
- 1)) - 1);
251 while (blk
< blkcnt
) {
252 blk_r
= ((blkcnt
- blk
) > mmc
->erase_grp_size
) ?
253 mmc
->erase_grp_size
: (blkcnt
- blk
);
254 err
= mmc_erase_t(mmc
, start
+ blk
, blk_r
);
265 mmc_write_blocks(struct mmc
*mmc
, ulong start
, lbaint_t blkcnt
, const void*src
)
268 struct mmc_data data
;
271 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
272 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
273 start
+ blkcnt
, mmc
->block_dev
.lba
);
278 cmd
.cmdidx
= MMC_CMD_WRITE_MULTIPLE_BLOCK
;
280 cmd
.cmdidx
= MMC_CMD_WRITE_SINGLE_BLOCK
;
282 if (mmc
->high_capacity
)
285 cmd
.cmdarg
= start
* mmc
->write_bl_len
;
287 cmd
.resp_type
= MMC_RSP_R1
;
291 data
.blocks
= blkcnt
;
292 data
.blocksize
= mmc
->write_bl_len
;
293 data
.flags
= MMC_DATA_WRITE
;
295 if (mmc_send_cmd(mmc
, &cmd
, &data
)) {
296 printf("mmc write failed\n");
300 /* SPI multiblock writes terminate using a special
301 * token, not a STOP_TRANSMISSION request.
303 if (!mmc_host_is_spi(mmc
) && blkcnt
> 1) {
304 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
306 cmd
.resp_type
= MMC_RSP_R1b
;
308 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
309 printf("mmc fail to send stop cmd\n");
314 /* Waiting for the ready status */
315 if (mmc_send_status(mmc
, timeout
))
322 mmc_bwrite(int dev_num
, ulong start
, lbaint_t blkcnt
, const void*src
)
324 lbaint_t cur
, blocks_todo
= blkcnt
;
326 struct mmc
*mmc
= find_mmc_device(dev_num
);
330 if (mmc_set_blocklen(mmc
, mmc
->write_bl_len
))
334 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
335 if(mmc_write_blocks(mmc
, start
, cur
, src
) != cur
)
339 src
+= cur
* mmc
->write_bl_len
;
340 } while (blocks_todo
> 0);
345 int mmc_read_blocks(struct mmc
*mmc
, void *dst
, ulong start
, lbaint_t blkcnt
)
348 struct mmc_data data
;
351 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
353 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
355 if (mmc
->high_capacity
)
358 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
360 cmd
.resp_type
= MMC_RSP_R1
;
364 data
.blocks
= blkcnt
;
365 data
.blocksize
= mmc
->read_bl_len
;
366 data
.flags
= MMC_DATA_READ
;
368 if (mmc_send_cmd(mmc
, &cmd
, &data
))
372 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
374 cmd
.resp_type
= MMC_RSP_R1b
;
376 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
377 printf("mmc fail to send stop cmd\n");
385 static ulong
mmc_bread(int dev_num
, ulong start
, lbaint_t blkcnt
, void *dst
)
387 lbaint_t cur
, blocks_todo
= blkcnt
;
392 struct mmc
*mmc
= find_mmc_device(dev_num
);
396 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
397 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
398 start
+ blkcnt
, mmc
->block_dev
.lba
);
402 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
406 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
407 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
411 dst
+= cur
* mmc
->read_bl_len
;
412 } while (blocks_todo
> 0);
417 int mmc_go_idle(struct mmc
* mmc
)
424 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
426 cmd
.resp_type
= MMC_RSP_NONE
;
429 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
440 sd_send_op_cond(struct mmc
*mmc
)
447 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
448 cmd
.resp_type
= MMC_RSP_R1
;
452 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
457 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
458 cmd
.resp_type
= MMC_RSP_R3
;
461 * Most cards do not answer if some reserved bits
462 * in the ocr are set. However, Some controller
463 * can set bit 7 (reserved for low voltages), but
464 * how to manage low voltages SD card is not yet
467 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
468 (mmc
->voltages
& 0xff8000);
470 if (mmc
->version
== SD_VERSION_2
)
471 cmd
.cmdarg
|= OCR_HCS
;
473 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
479 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
484 if (mmc
->version
!= SD_VERSION_2
)
485 mmc
->version
= SD_VERSION_1_0
;
487 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
488 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
489 cmd
.resp_type
= MMC_RSP_R3
;
493 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
499 mmc
->ocr
= cmd
.response
[0];
501 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
507 int mmc_send_op_cond(struct mmc
*mmc
)
513 /* Some cards seem to need this */
516 /* Asking to the card its capabilities */
517 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
518 cmd
.resp_type
= MMC_RSP_R3
;
522 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
530 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
531 cmd
.resp_type
= MMC_RSP_R3
;
532 cmd
.cmdarg
= (mmc_host_is_spi(mmc
) ? 0 :
534 (cmd
.response
[0] & OCR_VOLTAGE_MASK
)) |
535 (cmd
.response
[0] & OCR_ACCESS_MODE
));
537 if (mmc
->host_caps
& MMC_MODE_HC
)
538 cmd
.cmdarg
|= OCR_HCS
;
542 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
548 } while (!(cmd
.response
[0] & OCR_BUSY
) && timeout
--);
553 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
554 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
555 cmd
.resp_type
= MMC_RSP_R3
;
559 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
565 mmc
->version
= MMC_VERSION_UNKNOWN
;
566 mmc
->ocr
= cmd
.response
[0];
568 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
575 int mmc_send_ext_csd(struct mmc
*mmc
, char *ext_csd
)
578 struct mmc_data data
;
581 /* Get the Card Status Register */
582 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
583 cmd
.resp_type
= MMC_RSP_R1
;
589 data
.blocksize
= 512;
590 data
.flags
= MMC_DATA_READ
;
592 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
598 int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
604 cmd
.cmdidx
= MMC_CMD_SWITCH
;
605 cmd
.resp_type
= MMC_RSP_R1b
;
606 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
611 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
613 /* Waiting for the ready status */
615 ret
= mmc_send_status(mmc
, timeout
);
621 int mmc_change_freq(struct mmc
*mmc
)
623 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd
, 512);
629 if (mmc_host_is_spi(mmc
))
632 /* Only version 4 supports high-speed */
633 if (mmc
->version
< MMC_VERSION_4
)
636 err
= mmc_send_ext_csd(mmc
, ext_csd
);
641 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
643 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
648 /* Now check to see that it worked */
649 err
= mmc_send_ext_csd(mmc
, ext_csd
);
654 /* No high-speed support */
655 if (!ext_csd
[EXT_CSD_HS_TIMING
])
658 /* High Speed is set, there are two types: 52MHz and 26MHz */
659 if (cardtype
& MMC_HS_52MHZ
)
660 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
662 mmc
->card_caps
|= MMC_MODE_HS
;
667 int mmc_switch_part(int dev_num
, unsigned int part_num
)
669 struct mmc
*mmc
= find_mmc_device(dev_num
);
674 return mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
675 (mmc
->part_config
& ~PART_ACCESS_MASK
)
676 | (part_num
& PART_ACCESS_MASK
));
679 int mmc_getcd(struct mmc
*mmc
)
683 cd
= board_mmc_getcd(mmc
);
685 if ((cd
< 0) && mmc
->getcd
)
686 cd
= mmc
->getcd(mmc
);
691 int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
694 struct mmc_data data
;
696 /* Switch the frequency */
697 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
698 cmd
.resp_type
= MMC_RSP_R1
;
699 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
700 cmd
.cmdarg
&= ~(0xf << (group
* 4));
701 cmd
.cmdarg
|= value
<< (group
* 4);
704 data
.dest
= (char *)resp
;
707 data
.flags
= MMC_DATA_READ
;
709 return mmc_send_cmd(mmc
, &cmd
, &data
);
713 int sd_change_freq(struct mmc
*mmc
)
717 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
718 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
719 struct mmc_data data
;
724 if (mmc_host_is_spi(mmc
))
727 /* Read the SCR to find out if this card supports higher speeds */
728 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
729 cmd
.resp_type
= MMC_RSP_R1
;
730 cmd
.cmdarg
= mmc
->rca
<< 16;
733 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
738 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
739 cmd
.resp_type
= MMC_RSP_R1
;
746 data
.dest
= (char *)scr
;
749 data
.flags
= MMC_DATA_READ
;
751 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
760 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
761 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
763 switch ((mmc
->scr
[0] >> 24) & 0xf) {
765 mmc
->version
= SD_VERSION_1_0
;
768 mmc
->version
= SD_VERSION_1_10
;
771 mmc
->version
= SD_VERSION_2
;
774 mmc
->version
= SD_VERSION_1_0
;
778 if (mmc
->scr
[0] & SD_DATA_4BIT
)
779 mmc
->card_caps
|= MMC_MODE_4BIT
;
781 /* Version 1.0 doesn't support switching */
782 if (mmc
->version
== SD_VERSION_1_0
)
787 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
788 (u8
*)switch_status
);
793 /* The high-speed function is busy. Try again */
794 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
798 /* If high-speed isn't supported, we return */
799 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
803 * If the host doesn't support SD_HIGHSPEED, do not switch card to
804 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
805 * This can avoid furthur problem when the card runs in different
806 * mode between the host.
808 if (!((mmc
->host_caps
& MMC_MODE_HS_52MHz
) &&
809 (mmc
->host_caps
& MMC_MODE_HS
)))
812 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
817 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
818 mmc
->card_caps
|= MMC_MODE_HS
;
823 /* frequency bases */
824 /* divided by 10 to be nice to platforms without floating point */
825 static const int fbase
[] = {
832 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
833 * to platforms without floating point.
835 static const int multipliers
[] = {
854 void mmc_set_ios(struct mmc
*mmc
)
859 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
861 if (clock
> mmc
->f_max
)
864 if (clock
< mmc
->f_min
)
872 void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
874 mmc
->bus_width
= width
;
879 int mmc_startup(struct mmc
*mmc
)
883 u64 cmult
, csize
, capacity
;
885 ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd
, 512);
886 ALLOC_CACHE_ALIGN_BUFFER(char, test_csd
, 512);
889 #ifdef CONFIG_MMC_SPI_CRC_ON
890 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
891 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
892 cmd
.resp_type
= MMC_RSP_R1
;
895 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
902 /* Put the Card in Identify Mode */
903 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
904 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
905 cmd
.resp_type
= MMC_RSP_R2
;
909 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
914 memcpy(mmc
->cid
, cmd
.response
, 16);
917 * For MMC cards, set the Relative Address.
918 * For SD cards, get the Relatvie Address.
919 * This also puts the cards into Standby State
921 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
922 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
923 cmd
.cmdarg
= mmc
->rca
<< 16;
924 cmd
.resp_type
= MMC_RSP_R6
;
927 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
933 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
936 /* Get the Card-Specific Data */
937 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
938 cmd
.resp_type
= MMC_RSP_R2
;
939 cmd
.cmdarg
= mmc
->rca
<< 16;
942 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
944 /* Waiting for the ready status */
945 mmc_send_status(mmc
, timeout
);
950 mmc
->csd
[0] = cmd
.response
[0];
951 mmc
->csd
[1] = cmd
.response
[1];
952 mmc
->csd
[2] = cmd
.response
[2];
953 mmc
->csd
[3] = cmd
.response
[3];
955 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
956 int version
= (cmd
.response
[0] >> 26) & 0xf;
960 mmc
->version
= MMC_VERSION_1_2
;
963 mmc
->version
= MMC_VERSION_1_4
;
966 mmc
->version
= MMC_VERSION_2_2
;
969 mmc
->version
= MMC_VERSION_3
;
972 mmc
->version
= MMC_VERSION_4
;
975 mmc
->version
= MMC_VERSION_1_2
;
980 /* divide frequency by 10, since the mults are 10x bigger */
981 freq
= fbase
[(cmd
.response
[0] & 0x7)];
982 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
984 mmc
->tran_speed
= freq
* mult
;
986 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
989 mmc
->write_bl_len
= mmc
->read_bl_len
;
991 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
993 if (mmc
->high_capacity
) {
994 csize
= (mmc
->csd
[1] & 0x3f) << 16
995 | (mmc
->csd
[2] & 0xffff0000) >> 16;
998 csize
= (mmc
->csd
[1] & 0x3ff) << 2
999 | (mmc
->csd
[2] & 0xc0000000) >> 30;
1000 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
1003 mmc
->capacity
= (csize
+ 1) << (cmult
+ 2);
1004 mmc
->capacity
*= mmc
->read_bl_len
;
1006 if (mmc
->read_bl_len
> 512)
1007 mmc
->read_bl_len
= 512;
1009 if (mmc
->write_bl_len
> 512)
1010 mmc
->write_bl_len
= 512;
1012 /* Select the card, and put it into Transfer Mode */
1013 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
1014 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
1015 cmd
.resp_type
= MMC_RSP_R1
;
1016 cmd
.cmdarg
= mmc
->rca
<< 16;
1018 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1025 * For SD, its erase group is always one sector
1027 mmc
->erase_grp_size
= 1;
1028 mmc
->part_config
= MMCPART_NOAVAILABLE
;
1029 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
1030 /* check ext_csd version and capacity */
1031 err
= mmc_send_ext_csd(mmc
, ext_csd
);
1032 if (!err
& (ext_csd
[EXT_CSD_REV
] >= 2)) {
1034 * According to the JEDEC Standard, the value of
1035 * ext_csd's capacity is valid if the value is more
1038 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
1039 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
1040 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
1041 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
1043 if ((capacity
>> 20) > 2 * 1024)
1044 mmc
->capacity
= capacity
;
1048 * Check whether GROUP_DEF is set, if yes, read out
1049 * group size from ext_csd directly, or calculate
1050 * the group size from the csd value.
1052 if (ext_csd
[EXT_CSD_ERASE_GROUP_DEF
])
1053 mmc
->erase_grp_size
=
1054 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] * 512 * 1024;
1056 int erase_gsz
, erase_gmul
;
1057 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1058 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1059 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1063 /* store the partition info of emmc */
1064 if (ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
)
1065 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1069 err
= sd_change_freq(mmc
);
1071 err
= mmc_change_freq(mmc
);
1076 /* Restrict card's capabilities by what the host can do */
1077 mmc
->card_caps
&= mmc
->host_caps
;
1080 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1081 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1082 cmd
.resp_type
= MMC_RSP_R1
;
1083 cmd
.cmdarg
= mmc
->rca
<< 16;
1086 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1090 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1091 cmd
.resp_type
= MMC_RSP_R1
;
1094 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1098 mmc_set_bus_width(mmc
, 4);
1101 if (mmc
->card_caps
& MMC_MODE_HS
)
1102 mmc_set_clock(mmc
, 50000000);
1104 mmc_set_clock(mmc
, 25000000);
1106 for (width
= EXT_CSD_BUS_WIDTH_8
; width
>= 0; width
--) {
1107 /* Set the card to use 4 bit*/
1108 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1109 EXT_CSD_BUS_WIDTH
, width
);
1115 mmc_set_bus_width(mmc
, 1);
1118 mmc_set_bus_width(mmc
, 4 * width
);
1120 err
= mmc_send_ext_csd(mmc
, test_csd
);
1121 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1122 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1123 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1124 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1125 && ext_csd
[EXT_CSD_REV
] \
1126 == test_csd
[EXT_CSD_REV
]
1127 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1128 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1129 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1130 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1132 mmc
->card_caps
|= width
;
1137 if (mmc
->card_caps
& MMC_MODE_HS
) {
1138 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1139 mmc_set_clock(mmc
, 52000000);
1141 mmc_set_clock(mmc
, 26000000);
1143 mmc_set_clock(mmc
, 20000000);
1146 /* fill in device description */
1147 mmc
->block_dev
.lun
= 0;
1148 mmc
->block_dev
.type
= 0;
1149 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1150 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1151 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %08x", mmc
->cid
[0] >> 8,
1152 (mmc
->cid
[2] << 8) | (mmc
->cid
[3] >> 24));
1153 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1154 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1155 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff);
1156 sprintf(mmc
->block_dev
.revision
, "%d.%d", mmc
->cid
[2] >> 28,
1157 (mmc
->cid
[2] >> 24) & 0xf);
1158 init_part(&mmc
->block_dev
);
1163 int mmc_send_if_cond(struct mmc
*mmc
)
1168 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1169 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1170 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1171 cmd
.resp_type
= MMC_RSP_R7
;
1174 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1179 if ((cmd
.response
[0] & 0xff) != 0xaa)
1180 return UNUSABLE_ERR
;
1182 mmc
->version
= SD_VERSION_2
;
1187 int mmc_register(struct mmc
*mmc
)
1189 /* Setup the universal parts of the block interface just once */
1190 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1191 mmc
->block_dev
.dev
= cur_dev_num
++;
1192 mmc
->block_dev
.removable
= 1;
1193 mmc
->block_dev
.block_read
= mmc_bread
;
1194 mmc
->block_dev
.block_write
= mmc_bwrite
;
1195 mmc
->block_dev
.block_erase
= mmc_berase
;
1197 mmc
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
1199 INIT_LIST_HEAD (&mmc
->link
);
1201 list_add_tail (&mmc
->link
, &mmc_devices
);
1206 #ifdef CONFIG_PARTITIONS
1207 block_dev_desc_t
*mmc_get_dev(int dev
)
1209 struct mmc
*mmc
= find_mmc_device(dev
);
1211 return mmc
? &mmc
->block_dev
: NULL
;
1215 int mmc_init(struct mmc
*mmc
)
1219 if (mmc_getcd(mmc
) == 0) {
1221 printf("MMC: no card present\n");
1228 err
= mmc
->init(mmc
);
1233 mmc_set_bus_width(mmc
, 1);
1234 mmc_set_clock(mmc
, 1);
1236 /* Reset the Card */
1237 err
= mmc_go_idle(mmc
);
1242 /* The internal partition reset to user partition(0) at every CMD0*/
1245 /* Test for SD version 2 */
1246 err
= mmc_send_if_cond(mmc
);
1248 /* Now try to get the SD card's operating condition */
1249 err
= sd_send_op_cond(mmc
);
1251 /* If the command timed out, we check for an MMC card */
1252 if (err
== TIMEOUT
) {
1253 err
= mmc_send_op_cond(mmc
);
1256 printf("Card did not respond to voltage select!\n");
1257 return UNUSABLE_ERR
;
1261 err
= mmc_startup(mmc
);
1270 * CPU and board-specific MMC initializations. Aliased function
1271 * signals caller to move on
1273 static int __def_mmc_init(bd_t
*bis
)
1278 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1279 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1281 void print_mmc_devices(char separator
)
1284 struct list_head
*entry
;
1286 list_for_each(entry
, &mmc_devices
) {
1287 m
= list_entry(entry
, struct mmc
, link
);
1289 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
1291 if (entry
->next
!= &mmc_devices
)
1292 printf("%c ", separator
);
1298 int get_mmc_num(void)
1303 int mmc_initialize(bd_t
*bis
)
1305 INIT_LIST_HEAD (&mmc_devices
);
1308 if (board_mmc_init(bis
) < 0)
1311 print_mmc_devices(',');