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 struct mmc_data backup
;
55 memset(&backup
, 0, sizeof(backup
));
57 #ifdef CONFIG_MMC_TRACE
61 printf("CMD_SEND:%d\n", cmd
->cmdidx
);
62 printf("\t\tARG\t\t\t 0x%08X\n", cmd
->cmdarg
);
63 ret
= mmc
->send_cmd(mmc
, cmd
, data
);
64 switch (cmd
->resp_type
) {
66 printf("\t\tMMC_RSP_NONE\n");
69 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
73 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
77 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
79 printf("\t\t \t\t 0x%08X \n",
81 printf("\t\t \t\t 0x%08X \n",
83 printf("\t\t \t\t 0x%08X \n",
86 printf("\t\t\t\t\tDUMPING DATA\n");
87 for (i
= 0; i
< 4; i
++) {
89 printf("\t\t\t\t\t%03d - ", i
*4);
90 ptr
= (u8
*)&cmd
->response
[i
];
92 for (j
= 0; j
< 4; j
++)
93 printf("%02X ", *ptr
--);
98 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
102 printf("\t\tERROR MMC rsp not supported\n");
106 ret
= mmc
->send_cmd(mmc
, cmd
, data
);
111 int mmc_send_status(struct mmc
*mmc
, int timeout
)
114 int err
, retries
= 5;
115 #ifdef CONFIG_MMC_TRACE
119 cmd
.cmdidx
= MMC_CMD_SEND_STATUS
;
120 cmd
.resp_type
= MMC_RSP_R1
;
121 if (!mmc_host_is_spi(mmc
))
122 cmd
.cmdarg
= mmc
->rca
<< 16;
125 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
127 if ((cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
) &&
128 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) !=
131 else if (cmd
.response
[0] & MMC_STATUS_MASK
) {
132 printf("Status Error: 0x%08X\n",
136 } else if (--retries
< 0)
143 #ifdef CONFIG_MMC_TRACE
144 status
= (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) >> 9;
145 printf("CURR STATE:%d\n", status
);
148 printf("Timeout waiting card ready\n");
155 int mmc_set_blocklen(struct mmc
*mmc
, int len
)
159 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
160 cmd
.resp_type
= MMC_RSP_R1
;
163 return mmc_send_cmd(mmc
, &cmd
, NULL
);
166 struct mmc
*find_mmc_device(int dev_num
)
169 struct list_head
*entry
;
171 list_for_each(entry
, &mmc_devices
) {
172 m
= list_entry(entry
, struct mmc
, link
);
174 if (m
->block_dev
.dev
== dev_num
)
178 printf("MMC Device %d not found\n", dev_num
);
183 static ulong
mmc_erase_t(struct mmc
*mmc
, ulong start
, lbaint_t blkcnt
)
187 int err
, start_cmd
, end_cmd
;
189 if (mmc
->high_capacity
)
190 end
= start
+ blkcnt
- 1;
192 end
= (start
+ blkcnt
- 1) * mmc
->write_bl_len
;
193 start
*= mmc
->write_bl_len
;
197 start_cmd
= SD_CMD_ERASE_WR_BLK_START
;
198 end_cmd
= SD_CMD_ERASE_WR_BLK_END
;
200 start_cmd
= MMC_CMD_ERASE_GROUP_START
;
201 end_cmd
= MMC_CMD_ERASE_GROUP_END
;
204 cmd
.cmdidx
= start_cmd
;
206 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;
245 if ((start
% mmc
->erase_grp_size
) || (blkcnt
% mmc
->erase_grp_size
))
246 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
247 "The erase range would be change to 0x%lx~0x%lx\n\n",
248 mmc
->erase_grp_size
, start
& ~(mmc
->erase_grp_size
- 1),
249 ((start
+ blkcnt
+ mmc
->erase_grp_size
)
250 & ~(mmc
->erase_grp_size
- 1)) - 1);
252 while (blk
< blkcnt
) {
253 blk_r
= ((blkcnt
- blk
) > mmc
->erase_grp_size
) ?
254 mmc
->erase_grp_size
: (blkcnt
- blk
);
255 err
= mmc_erase_t(mmc
, start
+ blk
, blk_r
);
261 /* Waiting for the ready status */
262 if (mmc_send_status(mmc
, timeout
))
270 mmc_write_blocks(struct mmc
*mmc
, ulong start
, lbaint_t blkcnt
, const void*src
)
273 struct mmc_data data
;
276 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
277 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
278 start
+ blkcnt
, mmc
->block_dev
.lba
);
283 cmd
.cmdidx
= MMC_CMD_WRITE_MULTIPLE_BLOCK
;
285 cmd
.cmdidx
= MMC_CMD_WRITE_SINGLE_BLOCK
;
287 if (mmc
->high_capacity
)
290 cmd
.cmdarg
= start
* mmc
->write_bl_len
;
292 cmd
.resp_type
= MMC_RSP_R1
;
295 data
.blocks
= blkcnt
;
296 data
.blocksize
= mmc
->write_bl_len
;
297 data
.flags
= MMC_DATA_WRITE
;
299 if (mmc_send_cmd(mmc
, &cmd
, &data
)) {
300 printf("mmc write failed\n");
304 /* SPI multiblock writes terminate using a special
305 * token, not a STOP_TRANSMISSION request.
307 if (!mmc_host_is_spi(mmc
) && blkcnt
> 1) {
308 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
310 cmd
.resp_type
= MMC_RSP_R1b
;
311 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
312 printf("mmc fail to send stop cmd\n");
317 /* Waiting for the ready status */
318 if (mmc_send_status(mmc
, timeout
))
325 mmc_bwrite(int dev_num
, ulong start
, lbaint_t blkcnt
, const void*src
)
327 lbaint_t cur
, blocks_todo
= blkcnt
;
329 struct mmc
*mmc
= find_mmc_device(dev_num
);
333 if (mmc_set_blocklen(mmc
, mmc
->write_bl_len
))
337 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
338 if(mmc_write_blocks(mmc
, start
, cur
, src
) != cur
)
342 src
+= cur
* mmc
->write_bl_len
;
343 } while (blocks_todo
> 0);
348 int mmc_read_blocks(struct mmc
*mmc
, void *dst
, ulong start
, lbaint_t blkcnt
)
351 struct mmc_data data
;
354 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
356 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
358 if (mmc
->high_capacity
)
361 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
363 cmd
.resp_type
= MMC_RSP_R1
;
366 data
.blocks
= blkcnt
;
367 data
.blocksize
= mmc
->read_bl_len
;
368 data
.flags
= MMC_DATA_READ
;
370 if (mmc_send_cmd(mmc
, &cmd
, &data
))
374 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
376 cmd
.resp_type
= MMC_RSP_R1b
;
377 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
378 printf("mmc fail to send stop cmd\n");
386 static ulong
mmc_bread(int dev_num
, ulong start
, lbaint_t blkcnt
, void *dst
)
388 lbaint_t cur
, blocks_todo
= blkcnt
;
393 struct mmc
*mmc
= find_mmc_device(dev_num
);
397 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
398 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
399 start
+ blkcnt
, mmc
->block_dev
.lba
);
403 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
407 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
408 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
412 dst
+= cur
* mmc
->read_bl_len
;
413 } while (blocks_todo
> 0);
418 int mmc_go_idle(struct mmc
* mmc
)
425 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
427 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
;
451 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
456 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
457 cmd
.resp_type
= MMC_RSP_R3
;
460 * Most cards do not answer if some reserved bits
461 * in the ocr are set. However, Some controller
462 * can set bit 7 (reserved for low voltages), but
463 * how to manage low voltages SD card is not yet
466 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
467 (mmc
->voltages
& 0xff8000);
469 if (mmc
->version
== SD_VERSION_2
)
470 cmd
.cmdarg
|= OCR_HCS
;
472 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
478 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
483 if (mmc
->version
!= SD_VERSION_2
)
484 mmc
->version
= SD_VERSION_1_0
;
486 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
487 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
488 cmd
.resp_type
= MMC_RSP_R3
;
491 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
497 mmc
->ocr
= cmd
.response
[0];
499 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
505 int mmc_send_op_cond(struct mmc
*mmc
)
511 /* Some cards seem to need this */
514 /* Asking to the card its capabilities */
515 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
516 cmd
.resp_type
= MMC_RSP_R3
;
519 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
527 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
528 cmd
.resp_type
= MMC_RSP_R3
;
529 cmd
.cmdarg
= (mmc_host_is_spi(mmc
) ? 0 :
531 (cmd
.response
[0] & OCR_VOLTAGE_MASK
)) |
532 (cmd
.response
[0] & OCR_ACCESS_MODE
));
534 if (mmc
->host_caps
& MMC_MODE_HC
)
535 cmd
.cmdarg
|= OCR_HCS
;
537 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
543 } while (!(cmd
.response
[0] & OCR_BUSY
) && timeout
--);
548 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
549 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
550 cmd
.resp_type
= MMC_RSP_R3
;
553 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
559 mmc
->version
= MMC_VERSION_UNKNOWN
;
560 mmc
->ocr
= cmd
.response
[0];
562 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
569 int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
572 struct mmc_data data
;
575 /* Get the Card Status Register */
576 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
577 cmd
.resp_type
= MMC_RSP_R1
;
580 data
.dest
= (char *)ext_csd
;
582 data
.blocksize
= 512;
583 data
.flags
= MMC_DATA_READ
;
585 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
591 int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
597 cmd
.cmdidx
= MMC_CMD_SWITCH
;
598 cmd
.resp_type
= MMC_RSP_R1b
;
599 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
603 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
605 /* Waiting for the ready status */
607 ret
= mmc_send_status(mmc
, timeout
);
613 int mmc_change_freq(struct mmc
*mmc
)
615 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, 512);
621 if (mmc_host_is_spi(mmc
))
624 /* Only version 4 supports high-speed */
625 if (mmc
->version
< MMC_VERSION_4
)
628 err
= mmc_send_ext_csd(mmc
, ext_csd
);
633 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
635 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
640 /* Now check to see that it worked */
641 err
= mmc_send_ext_csd(mmc
, ext_csd
);
646 /* No high-speed support */
647 if (!ext_csd
[EXT_CSD_HS_TIMING
])
650 /* High Speed is set, there are two types: 52MHz and 26MHz */
651 if (cardtype
& MMC_HS_52MHZ
)
652 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
654 mmc
->card_caps
|= MMC_MODE_HS
;
659 int mmc_switch_part(int dev_num
, unsigned int part_num
)
661 struct mmc
*mmc
= find_mmc_device(dev_num
);
666 return mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
667 (mmc
->part_config
& ~PART_ACCESS_MASK
)
668 | (part_num
& PART_ACCESS_MASK
));
671 int mmc_getcd(struct mmc
*mmc
)
675 cd
= board_mmc_getcd(mmc
);
677 if ((cd
< 0) && mmc
->getcd
)
678 cd
= mmc
->getcd(mmc
);
683 int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
686 struct mmc_data data
;
688 /* Switch the frequency */
689 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
690 cmd
.resp_type
= MMC_RSP_R1
;
691 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
692 cmd
.cmdarg
&= ~(0xf << (group
* 4));
693 cmd
.cmdarg
|= value
<< (group
* 4);
695 data
.dest
= (char *)resp
;
698 data
.flags
= MMC_DATA_READ
;
700 return mmc_send_cmd(mmc
, &cmd
, &data
);
704 int sd_change_freq(struct mmc
*mmc
)
708 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
709 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
710 struct mmc_data data
;
715 if (mmc_host_is_spi(mmc
))
718 /* Read the SCR to find out if this card supports higher speeds */
719 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
720 cmd
.resp_type
= MMC_RSP_R1
;
721 cmd
.cmdarg
= mmc
->rca
<< 16;
723 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
728 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
729 cmd
.resp_type
= MMC_RSP_R1
;
735 data
.dest
= (char *)scr
;
738 data
.flags
= MMC_DATA_READ
;
740 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
749 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
750 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
752 switch ((mmc
->scr
[0] >> 24) & 0xf) {
754 mmc
->version
= SD_VERSION_1_0
;
757 mmc
->version
= SD_VERSION_1_10
;
760 mmc
->version
= SD_VERSION_2
;
763 mmc
->version
= SD_VERSION_1_0
;
767 if (mmc
->scr
[0] & SD_DATA_4BIT
)
768 mmc
->card_caps
|= MMC_MODE_4BIT
;
770 /* Version 1.0 doesn't support switching */
771 if (mmc
->version
== SD_VERSION_1_0
)
776 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
777 (u8
*)switch_status
);
782 /* The high-speed function is busy. Try again */
783 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
787 /* If high-speed isn't supported, we return */
788 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
792 * If the host doesn't support SD_HIGHSPEED, do not switch card to
793 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
794 * This can avoid furthur problem when the card runs in different
795 * mode between the host.
797 if (!((mmc
->host_caps
& MMC_MODE_HS_52MHz
) &&
798 (mmc
->host_caps
& MMC_MODE_HS
)))
801 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
806 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
807 mmc
->card_caps
|= MMC_MODE_HS
;
812 /* frequency bases */
813 /* divided by 10 to be nice to platforms without floating point */
814 static const int fbase
[] = {
821 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
822 * to platforms without floating point.
824 static const int multipliers
[] = {
843 void mmc_set_ios(struct mmc
*mmc
)
848 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
850 if (clock
> mmc
->f_max
)
853 if (clock
< mmc
->f_min
)
861 void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
863 mmc
->bus_width
= width
;
868 int mmc_startup(struct mmc
*mmc
)
872 u64 cmult
, csize
, capacity
;
874 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, 512);
875 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, 512);
878 #ifdef CONFIG_MMC_SPI_CRC_ON
879 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
880 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
881 cmd
.resp_type
= MMC_RSP_R1
;
883 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
890 /* Put the Card in Identify Mode */
891 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
892 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
893 cmd
.resp_type
= MMC_RSP_R2
;
896 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
901 memcpy(mmc
->cid
, cmd
.response
, 16);
904 * For MMC cards, set the Relative Address.
905 * For SD cards, get the Relatvie Address.
906 * This also puts the cards into Standby State
908 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
909 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
910 cmd
.cmdarg
= mmc
->rca
<< 16;
911 cmd
.resp_type
= MMC_RSP_R6
;
913 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
919 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
922 /* Get the Card-Specific Data */
923 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
924 cmd
.resp_type
= MMC_RSP_R2
;
925 cmd
.cmdarg
= mmc
->rca
<< 16;
927 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
929 /* Waiting for the ready status */
930 mmc_send_status(mmc
, timeout
);
935 mmc
->csd
[0] = cmd
.response
[0];
936 mmc
->csd
[1] = cmd
.response
[1];
937 mmc
->csd
[2] = cmd
.response
[2];
938 mmc
->csd
[3] = cmd
.response
[3];
940 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
941 int version
= (cmd
.response
[0] >> 26) & 0xf;
945 mmc
->version
= MMC_VERSION_1_2
;
948 mmc
->version
= MMC_VERSION_1_4
;
951 mmc
->version
= MMC_VERSION_2_2
;
954 mmc
->version
= MMC_VERSION_3
;
957 mmc
->version
= MMC_VERSION_4
;
960 mmc
->version
= MMC_VERSION_1_2
;
965 /* divide frequency by 10, since the mults are 10x bigger */
966 freq
= fbase
[(cmd
.response
[0] & 0x7)];
967 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
969 mmc
->tran_speed
= freq
* mult
;
971 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
974 mmc
->write_bl_len
= mmc
->read_bl_len
;
976 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
978 if (mmc
->high_capacity
) {
979 csize
= (mmc
->csd
[1] & 0x3f) << 16
980 | (mmc
->csd
[2] & 0xffff0000) >> 16;
983 csize
= (mmc
->csd
[1] & 0x3ff) << 2
984 | (mmc
->csd
[2] & 0xc0000000) >> 30;
985 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
988 mmc
->capacity
= (csize
+ 1) << (cmult
+ 2);
989 mmc
->capacity
*= mmc
->read_bl_len
;
991 if (mmc
->read_bl_len
> 512)
992 mmc
->read_bl_len
= 512;
994 if (mmc
->write_bl_len
> 512)
995 mmc
->write_bl_len
= 512;
997 /* Select the card, and put it into Transfer Mode */
998 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
999 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
1000 cmd
.resp_type
= MMC_RSP_R1
;
1001 cmd
.cmdarg
= mmc
->rca
<< 16;
1002 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1009 * For SD, its erase group is always one sector
1011 mmc
->erase_grp_size
= 1;
1012 mmc
->part_config
= MMCPART_NOAVAILABLE
;
1013 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
1014 /* check ext_csd version and capacity */
1015 err
= mmc_send_ext_csd(mmc
, ext_csd
);
1016 if (!err
& (ext_csd
[EXT_CSD_REV
] >= 2)) {
1018 * According to the JEDEC Standard, the value of
1019 * ext_csd's capacity is valid if the value is more
1022 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
1023 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
1024 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
1025 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
1027 if ((capacity
>> 20) > 2 * 1024)
1028 mmc
->capacity
= capacity
;
1032 * Check whether GROUP_DEF is set, if yes, read out
1033 * group size from ext_csd directly, or calculate
1034 * the group size from the csd value.
1036 if (ext_csd
[EXT_CSD_ERASE_GROUP_DEF
])
1037 mmc
->erase_grp_size
=
1038 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] * 512 * 1024;
1040 int erase_gsz
, erase_gmul
;
1041 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1042 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1043 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1047 /* store the partition info of emmc */
1048 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1049 ext_csd
[EXT_CSD_BOOT_MULT
])
1050 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1054 err
= sd_change_freq(mmc
);
1056 err
= mmc_change_freq(mmc
);
1061 /* Restrict card's capabilities by what the host can do */
1062 mmc
->card_caps
&= mmc
->host_caps
;
1065 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1066 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1067 cmd
.resp_type
= MMC_RSP_R1
;
1068 cmd
.cmdarg
= mmc
->rca
<< 16;
1070 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1074 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1075 cmd
.resp_type
= MMC_RSP_R1
;
1077 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1081 mmc_set_bus_width(mmc
, 4);
1084 if (mmc
->card_caps
& MMC_MODE_HS
)
1085 mmc
->tran_speed
= 50000000;
1087 mmc
->tran_speed
= 25000000;
1089 width
= ((mmc
->host_caps
& MMC_MODE_MASK_WIDTH_BITS
) >>
1090 MMC_MODE_WIDTH_BITS_SHIFT
);
1091 for (; width
>= 0; width
--) {
1092 /* Set the card to use 4 bit*/
1093 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1094 EXT_CSD_BUS_WIDTH
, width
);
1100 mmc_set_bus_width(mmc
, 1);
1103 mmc_set_bus_width(mmc
, 4 * width
);
1105 err
= mmc_send_ext_csd(mmc
, test_csd
);
1106 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1107 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1108 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1109 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1110 && ext_csd
[EXT_CSD_REV
] \
1111 == test_csd
[EXT_CSD_REV
]
1112 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1113 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1114 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1115 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1117 mmc
->card_caps
|= width
;
1122 if (mmc
->card_caps
& MMC_MODE_HS
) {
1123 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1124 mmc
->tran_speed
= 52000000;
1126 mmc
->tran_speed
= 26000000;
1130 mmc_set_clock(mmc
, mmc
->tran_speed
);
1132 /* fill in device description */
1133 mmc
->block_dev
.lun
= 0;
1134 mmc
->block_dev
.type
= 0;
1135 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1136 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1137 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %08x", mmc
->cid
[0] >> 8,
1138 (mmc
->cid
[2] << 8) | (mmc
->cid
[3] >> 24));
1139 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1140 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1141 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff);
1142 sprintf(mmc
->block_dev
.revision
, "%d.%d", mmc
->cid
[2] >> 28,
1143 (mmc
->cid
[2] >> 24) & 0xf);
1144 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1145 init_part(&mmc
->block_dev
);
1151 int mmc_send_if_cond(struct mmc
*mmc
)
1156 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1157 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1158 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1159 cmd
.resp_type
= MMC_RSP_R7
;
1161 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1166 if ((cmd
.response
[0] & 0xff) != 0xaa)
1167 return UNUSABLE_ERR
;
1169 mmc
->version
= SD_VERSION_2
;
1174 int mmc_register(struct mmc
*mmc
)
1176 /* Setup the universal parts of the block interface just once */
1177 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1178 mmc
->block_dev
.dev
= cur_dev_num
++;
1179 mmc
->block_dev
.removable
= 1;
1180 mmc
->block_dev
.block_read
= mmc_bread
;
1181 mmc
->block_dev
.block_write
= mmc_bwrite
;
1182 mmc
->block_dev
.block_erase
= mmc_berase
;
1184 mmc
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
1186 INIT_LIST_HEAD (&mmc
->link
);
1188 list_add_tail (&mmc
->link
, &mmc_devices
);
1193 #ifdef CONFIG_PARTITIONS
1194 block_dev_desc_t
*mmc_get_dev(int dev
)
1196 struct mmc
*mmc
= find_mmc_device(dev
);
1197 if (!mmc
|| mmc_init(mmc
))
1200 return &mmc
->block_dev
;
1204 int mmc_init(struct mmc
*mmc
)
1208 if (mmc_getcd(mmc
) == 0) {
1210 printf("MMC: no card present\n");
1217 err
= mmc
->init(mmc
);
1222 mmc_set_bus_width(mmc
, 1);
1223 mmc_set_clock(mmc
, 1);
1225 /* Reset the Card */
1226 err
= mmc_go_idle(mmc
);
1231 /* The internal partition reset to user partition(0) at every CMD0*/
1234 /* Test for SD version 2 */
1235 err
= mmc_send_if_cond(mmc
);
1237 /* Now try to get the SD card's operating condition */
1238 err
= sd_send_op_cond(mmc
);
1240 /* If the command timed out, we check for an MMC card */
1241 if (err
== TIMEOUT
) {
1242 err
= mmc_send_op_cond(mmc
);
1245 printf("Card did not respond to voltage select!\n");
1246 return UNUSABLE_ERR
;
1250 err
= mmc_startup(mmc
);
1259 * CPU and board-specific MMC initializations. Aliased function
1260 * signals caller to move on
1262 static int __def_mmc_init(bd_t
*bis
)
1267 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1268 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1270 void print_mmc_devices(char separator
)
1273 struct list_head
*entry
;
1275 list_for_each(entry
, &mmc_devices
) {
1276 m
= list_entry(entry
, struct mmc
, link
);
1278 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
1280 if (entry
->next
!= &mmc_devices
)
1281 printf("%c ", separator
);
1287 int get_mmc_num(void)
1292 int mmc_initialize(bd_t
*bis
)
1294 INIT_LIST_HEAD (&mmc_devices
);
1297 if (board_mmc_init(bis
) < 0)
1300 print_mmc_devices(',');