2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
17 #include <linux/list.h>
19 #include "mmc_private.h"
21 static struct list_head mmc_devices
;
22 static int cur_dev_num
= -1;
24 int __weak
board_mmc_getwp(struct mmc
*mmc
)
29 int mmc_getwp(struct mmc
*mmc
)
33 wp
= board_mmc_getwp(mmc
);
36 if (mmc
->cfg
->ops
->getwp
)
37 wp
= mmc
->cfg
->ops
->getwp(mmc
);
45 int __board_mmc_getcd(struct mmc
*mmc
) {
49 int board_mmc_getcd(struct mmc
*mmc
)__attribute__((weak
,
50 alias("__board_mmc_getcd")));
52 int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
56 #ifdef CONFIG_MMC_TRACE
60 printf("CMD_SEND:%d\n", cmd
->cmdidx
);
61 printf("\t\tARG\t\t\t 0x%08X\n", cmd
->cmdarg
);
62 ret
= mmc
->cfg
->ops
->send_cmd(mmc
, cmd
, data
);
63 switch (cmd
->resp_type
) {
65 printf("\t\tMMC_RSP_NONE\n");
68 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
72 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
76 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
78 printf("\t\t \t\t 0x%08X \n",
80 printf("\t\t \t\t 0x%08X \n",
82 printf("\t\t \t\t 0x%08X \n",
85 printf("\t\t\t\t\tDUMPING DATA\n");
86 for (i
= 0; i
< 4; i
++) {
88 printf("\t\t\t\t\t%03d - ", i
*4);
89 ptr
= (u8
*)&cmd
->response
[i
];
91 for (j
= 0; j
< 4; j
++)
92 printf("%02X ", *ptr
--);
97 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
101 printf("\t\tERROR MMC rsp not supported\n");
105 ret
= mmc
->cfg
->ops
->send_cmd(mmc
, cmd
, data
);
110 int mmc_send_status(struct mmc
*mmc
, int timeout
)
113 int err
, retries
= 5;
114 #ifdef CONFIG_MMC_TRACE
118 cmd
.cmdidx
= MMC_CMD_SEND_STATUS
;
119 cmd
.resp_type
= MMC_RSP_R1
;
120 if (!mmc_host_is_spi(mmc
))
121 cmd
.cmdarg
= mmc
->rca
<< 16;
124 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
126 if ((cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
) &&
127 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) !=
130 else if (cmd
.response
[0] & MMC_STATUS_MASK
) {
131 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
132 printf("Status Error: 0x%08X\n",
137 } else if (--retries
< 0)
144 #ifdef CONFIG_MMC_TRACE
145 status
= (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) >> 9;
146 printf("CURR STATE:%d\n", status
);
149 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
150 printf("Timeout waiting card ready\n");
154 if (cmd
.response
[0] & MMC_STATUS_SWITCH_ERROR
)
160 int mmc_set_blocklen(struct mmc
*mmc
, int len
)
164 if (mmc
->card_caps
& MMC_MODE_DDR_52MHz
)
167 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
168 cmd
.resp_type
= MMC_RSP_R1
;
171 return mmc_send_cmd(mmc
, &cmd
, NULL
);
174 struct mmc
*find_mmc_device(int dev_num
)
177 struct list_head
*entry
;
179 list_for_each(entry
, &mmc_devices
) {
180 m
= list_entry(entry
, struct mmc
, link
);
182 if (m
->block_dev
.dev
== dev_num
)
186 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
187 printf("MMC Device %d not found\n", dev_num
);
193 static int mmc_read_blocks(struct mmc
*mmc
, void *dst
, lbaint_t start
,
197 struct mmc_data data
;
200 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
202 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
204 if (mmc
->high_capacity
)
207 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
209 cmd
.resp_type
= MMC_RSP_R1
;
212 data
.blocks
= blkcnt
;
213 data
.blocksize
= mmc
->read_bl_len
;
214 data
.flags
= MMC_DATA_READ
;
216 if (mmc_send_cmd(mmc
, &cmd
, &data
))
220 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
222 cmd
.resp_type
= MMC_RSP_R1b
;
223 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
224 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
225 printf("mmc fail to send stop cmd\n");
234 static ulong
mmc_bread(int dev_num
, lbaint_t start
, lbaint_t blkcnt
, void *dst
)
236 lbaint_t cur
, blocks_todo
= blkcnt
;
241 struct mmc
*mmc
= find_mmc_device(dev_num
);
245 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
246 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
247 printf("MMC: block number 0x" LBAF
" exceeds max(0x" LBAF
")\n",
248 start
+ blkcnt
, mmc
->block_dev
.lba
);
253 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
257 cur
= (blocks_todo
> mmc
->cfg
->b_max
) ?
258 mmc
->cfg
->b_max
: blocks_todo
;
259 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
263 dst
+= cur
* mmc
->read_bl_len
;
264 } while (blocks_todo
> 0);
269 static int mmc_go_idle(struct mmc
*mmc
)
276 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
278 cmd
.resp_type
= MMC_RSP_NONE
;
280 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
290 static int sd_send_op_cond(struct mmc
*mmc
)
297 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
298 cmd
.resp_type
= MMC_RSP_R1
;
301 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
306 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
307 cmd
.resp_type
= MMC_RSP_R3
;
310 * Most cards do not answer if some reserved bits
311 * in the ocr are set. However, Some controller
312 * can set bit 7 (reserved for low voltages), but
313 * how to manage low voltages SD card is not yet
316 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
317 (mmc
->cfg
->voltages
& 0xff8000);
319 if (mmc
->version
== SD_VERSION_2
)
320 cmd
.cmdarg
|= OCR_HCS
;
322 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
328 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
333 if (mmc
->version
!= SD_VERSION_2
)
334 mmc
->version
= SD_VERSION_1_0
;
336 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
337 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
338 cmd
.resp_type
= MMC_RSP_R3
;
341 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
347 mmc
->ocr
= cmd
.response
[0];
349 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
355 /* We pass in the cmd since otherwise the init seems to fail */
356 static int mmc_send_op_cond_iter(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
361 cmd
->cmdidx
= MMC_CMD_SEND_OP_COND
;
362 cmd
->resp_type
= MMC_RSP_R3
;
364 if (use_arg
&& !mmc_host_is_spi(mmc
)) {
366 (mmc
->cfg
->voltages
&
367 (mmc
->op_cond_response
& OCR_VOLTAGE_MASK
)) |
368 (mmc
->op_cond_response
& OCR_ACCESS_MODE
);
370 if (mmc
->cfg
->host_caps
& MMC_MODE_HC
)
371 cmd
->cmdarg
|= OCR_HCS
;
373 err
= mmc_send_cmd(mmc
, cmd
, NULL
);
376 mmc
->op_cond_response
= cmd
->response
[0];
380 int mmc_send_op_cond(struct mmc
*mmc
)
385 /* Some cards seem to need this */
388 /* Asking to the card its capabilities */
389 mmc
->op_cond_pending
= 1;
390 for (i
= 0; i
< 2; i
++) {
391 err
= mmc_send_op_cond_iter(mmc
, &cmd
, i
!= 0);
395 /* exit if not busy (flag seems to be inverted) */
396 if (mmc
->op_cond_response
& OCR_BUSY
)
402 int mmc_complete_op_cond(struct mmc
*mmc
)
409 mmc
->op_cond_pending
= 0;
410 start
= get_timer(0);
412 err
= mmc_send_op_cond_iter(mmc
, &cmd
, 1);
415 if (get_timer(start
) > timeout
)
418 } while (!(mmc
->op_cond_response
& OCR_BUSY
));
420 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
421 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
422 cmd
.resp_type
= MMC_RSP_R3
;
425 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
431 mmc
->version
= MMC_VERSION_UNKNOWN
;
432 mmc
->ocr
= cmd
.response
[0];
434 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
441 static int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
444 struct mmc_data data
;
447 /* Get the Card Status Register */
448 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
449 cmd
.resp_type
= MMC_RSP_R1
;
452 data
.dest
= (char *)ext_csd
;
454 data
.blocksize
= MMC_MAX_BLOCK_LEN
;
455 data
.flags
= MMC_DATA_READ
;
457 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
463 static int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
469 cmd
.cmdidx
= MMC_CMD_SWITCH
;
470 cmd
.resp_type
= MMC_RSP_R1b
;
471 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
475 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
477 /* Waiting for the ready status */
479 ret
= mmc_send_status(mmc
, timeout
);
485 static int mmc_change_freq(struct mmc
*mmc
)
487 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
493 if (mmc_host_is_spi(mmc
))
496 /* Only version 4 supports high-speed */
497 if (mmc
->version
< MMC_VERSION_4
)
500 err
= mmc_send_ext_csd(mmc
, ext_csd
);
505 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
507 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
510 return err
== SWITCH_ERR
? 0 : err
;
512 /* Now check to see that it worked */
513 err
= mmc_send_ext_csd(mmc
, ext_csd
);
518 /* No high-speed support */
519 if (!ext_csd
[EXT_CSD_HS_TIMING
])
522 /* High Speed is set, there are two types: 52MHz and 26MHz */
523 if (cardtype
& EXT_CSD_CARD_TYPE_52
) {
524 if (cardtype
& EXT_CSD_CARD_TYPE_DDR_52
)
525 mmc
->card_caps
|= MMC_MODE_DDR_52MHz
;
526 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
528 mmc
->card_caps
|= MMC_MODE_HS
;
534 static int mmc_set_capacity(struct mmc
*mmc
, int part_num
)
538 mmc
->capacity
= mmc
->capacity_user
;
542 mmc
->capacity
= mmc
->capacity_boot
;
545 mmc
->capacity
= mmc
->capacity_rpmb
;
551 mmc
->capacity
= mmc
->capacity_gp
[part_num
- 4];
557 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
562 int mmc_select_hwpart(int dev_num
, int hwpart
)
564 struct mmc
*mmc
= find_mmc_device(dev_num
);
570 if (mmc
->part_num
== hwpart
)
573 if (mmc
->part_config
== MMCPART_NOAVAILABLE
) {
574 printf("Card doesn't support part_switch\n");
578 ret
= mmc_switch_part(dev_num
, hwpart
);
582 mmc
->part_num
= hwpart
;
588 int mmc_switch_part(int dev_num
, unsigned int part_num
)
590 struct mmc
*mmc
= find_mmc_device(dev_num
);
596 ret
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
597 (mmc
->part_config
& ~PART_ACCESS_MASK
)
598 | (part_num
& PART_ACCESS_MASK
));
602 return mmc_set_capacity(mmc
, part_num
);
605 int mmc_getcd(struct mmc
*mmc
)
609 cd
= board_mmc_getcd(mmc
);
612 if (mmc
->cfg
->ops
->getcd
)
613 cd
= mmc
->cfg
->ops
->getcd(mmc
);
621 static int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
624 struct mmc_data data
;
626 /* Switch the frequency */
627 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
628 cmd
.resp_type
= MMC_RSP_R1
;
629 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
630 cmd
.cmdarg
&= ~(0xf << (group
* 4));
631 cmd
.cmdarg
|= value
<< (group
* 4);
633 data
.dest
= (char *)resp
;
636 data
.flags
= MMC_DATA_READ
;
638 return mmc_send_cmd(mmc
, &cmd
, &data
);
642 static int sd_change_freq(struct mmc
*mmc
)
646 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
647 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
648 struct mmc_data data
;
653 if (mmc_host_is_spi(mmc
))
656 /* Read the SCR to find out if this card supports higher speeds */
657 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
658 cmd
.resp_type
= MMC_RSP_R1
;
659 cmd
.cmdarg
= mmc
->rca
<< 16;
661 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
666 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
667 cmd
.resp_type
= MMC_RSP_R1
;
673 data
.dest
= (char *)scr
;
676 data
.flags
= MMC_DATA_READ
;
678 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
687 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
688 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
690 switch ((mmc
->scr
[0] >> 24) & 0xf) {
692 mmc
->version
= SD_VERSION_1_0
;
695 mmc
->version
= SD_VERSION_1_10
;
698 mmc
->version
= SD_VERSION_2
;
699 if ((mmc
->scr
[0] >> 15) & 0x1)
700 mmc
->version
= SD_VERSION_3
;
703 mmc
->version
= SD_VERSION_1_0
;
707 if (mmc
->scr
[0] & SD_DATA_4BIT
)
708 mmc
->card_caps
|= MMC_MODE_4BIT
;
710 /* Version 1.0 doesn't support switching */
711 if (mmc
->version
== SD_VERSION_1_0
)
716 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
717 (u8
*)switch_status
);
722 /* The high-speed function is busy. Try again */
723 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
727 /* If high-speed isn't supported, we return */
728 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
732 * If the host doesn't support SD_HIGHSPEED, do not switch card to
733 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
734 * This can avoid furthur problem when the card runs in different
735 * mode between the host.
737 if (!((mmc
->cfg
->host_caps
& MMC_MODE_HS_52MHz
) &&
738 (mmc
->cfg
->host_caps
& MMC_MODE_HS
)))
741 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
746 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
747 mmc
->card_caps
|= MMC_MODE_HS
;
752 /* frequency bases */
753 /* divided by 10 to be nice to platforms without floating point */
754 static const int fbase
[] = {
761 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
762 * to platforms without floating point.
764 static const int multipliers
[] = {
783 static void mmc_set_ios(struct mmc
*mmc
)
785 if (mmc
->cfg
->ops
->set_ios
)
786 mmc
->cfg
->ops
->set_ios(mmc
);
789 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
791 if (clock
> mmc
->cfg
->f_max
)
792 clock
= mmc
->cfg
->f_max
;
794 if (clock
< mmc
->cfg
->f_min
)
795 clock
= mmc
->cfg
->f_min
;
802 static void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
804 mmc
->bus_width
= width
;
809 static int mmc_startup(struct mmc
*mmc
)
813 u64 cmult
, csize
, capacity
;
815 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
816 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
819 #ifdef CONFIG_MMC_SPI_CRC_ON
820 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
821 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
822 cmd
.resp_type
= MMC_RSP_R1
;
824 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
831 /* Put the Card in Identify Mode */
832 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
833 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
834 cmd
.resp_type
= MMC_RSP_R2
;
837 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
842 memcpy(mmc
->cid
, cmd
.response
, 16);
845 * For MMC cards, set the Relative Address.
846 * For SD cards, get the Relatvie Address.
847 * This also puts the cards into Standby State
849 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
850 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
851 cmd
.cmdarg
= mmc
->rca
<< 16;
852 cmd
.resp_type
= MMC_RSP_R6
;
854 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
860 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
863 /* Get the Card-Specific Data */
864 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
865 cmd
.resp_type
= MMC_RSP_R2
;
866 cmd
.cmdarg
= mmc
->rca
<< 16;
868 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
870 /* Waiting for the ready status */
871 mmc_send_status(mmc
, timeout
);
876 mmc
->csd
[0] = cmd
.response
[0];
877 mmc
->csd
[1] = cmd
.response
[1];
878 mmc
->csd
[2] = cmd
.response
[2];
879 mmc
->csd
[3] = cmd
.response
[3];
881 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
882 int version
= (cmd
.response
[0] >> 26) & 0xf;
886 mmc
->version
= MMC_VERSION_1_2
;
889 mmc
->version
= MMC_VERSION_1_4
;
892 mmc
->version
= MMC_VERSION_2_2
;
895 mmc
->version
= MMC_VERSION_3
;
898 mmc
->version
= MMC_VERSION_4
;
901 mmc
->version
= MMC_VERSION_1_2
;
906 /* divide frequency by 10, since the mults are 10x bigger */
907 freq
= fbase
[(cmd
.response
[0] & 0x7)];
908 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
910 mmc
->tran_speed
= freq
* mult
;
912 mmc
->dsr_imp
= ((cmd
.response
[1] >> 12) & 0x1);
913 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
916 mmc
->write_bl_len
= mmc
->read_bl_len
;
918 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
920 if (mmc
->high_capacity
) {
921 csize
= (mmc
->csd
[1] & 0x3f) << 16
922 | (mmc
->csd
[2] & 0xffff0000) >> 16;
925 csize
= (mmc
->csd
[1] & 0x3ff) << 2
926 | (mmc
->csd
[2] & 0xc0000000) >> 30;
927 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
930 mmc
->capacity_user
= (csize
+ 1) << (cmult
+ 2);
931 mmc
->capacity_user
*= mmc
->read_bl_len
;
932 mmc
->capacity_boot
= 0;
933 mmc
->capacity_rpmb
= 0;
934 for (i
= 0; i
< 4; i
++)
935 mmc
->capacity_gp
[i
] = 0;
937 if (mmc
->read_bl_len
> MMC_MAX_BLOCK_LEN
)
938 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
940 if (mmc
->write_bl_len
> MMC_MAX_BLOCK_LEN
)
941 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
943 if ((mmc
->dsr_imp
) && (0xffffffff != mmc
->dsr
)) {
944 cmd
.cmdidx
= MMC_CMD_SET_DSR
;
945 cmd
.cmdarg
= (mmc
->dsr
& 0xffff) << 16;
946 cmd
.resp_type
= MMC_RSP_NONE
;
947 if (mmc_send_cmd(mmc
, &cmd
, NULL
))
948 printf("MMC: SET_DSR failed\n");
951 /* Select the card, and put it into Transfer Mode */
952 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
953 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
954 cmd
.resp_type
= MMC_RSP_R1
;
955 cmd
.cmdarg
= mmc
->rca
<< 16;
956 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
963 * For SD, its erase group is always one sector
965 mmc
->erase_grp_size
= 1;
966 mmc
->part_config
= MMCPART_NOAVAILABLE
;
967 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
968 /* check ext_csd version and capacity */
969 err
= mmc_send_ext_csd(mmc
, ext_csd
);
970 if (!err
&& (ext_csd
[EXT_CSD_REV
] >= 2)) {
972 * According to the JEDEC Standard, the value of
973 * ext_csd's capacity is valid if the value is more
976 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
977 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
978 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
979 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
980 capacity
*= MMC_MAX_BLOCK_LEN
;
981 if ((capacity
>> 20) > 2 * 1024)
982 mmc
->capacity_user
= capacity
;
985 switch (ext_csd
[EXT_CSD_REV
]) {
987 mmc
->version
= MMC_VERSION_4_1
;
990 mmc
->version
= MMC_VERSION_4_2
;
993 mmc
->version
= MMC_VERSION_4_3
;
996 mmc
->version
= MMC_VERSION_4_41
;
999 mmc
->version
= MMC_VERSION_4_5
;
1004 * Host needs to enable ERASE_GRP_DEF bit if device is
1005 * partitioned. This bit will be lost every time after a reset
1006 * or power off. This will affect erase size.
1008 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) &&
1009 (ext_csd
[EXT_CSD_PARTITIONS_ATTRIBUTE
] & PART_ENH_ATTRIB
)) {
1010 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1011 EXT_CSD_ERASE_GROUP_DEF
, 1);
1016 /* Read out group size from ext_csd */
1017 mmc
->erase_grp_size
=
1018 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] *
1019 MMC_MAX_BLOCK_LEN
* 1024;
1021 /* Calculate the group size from the csd value. */
1022 int erase_gsz
, erase_gmul
;
1023 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1024 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1025 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1029 /* store the partition info of emmc */
1030 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1031 ext_csd
[EXT_CSD_BOOT_MULT
])
1032 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1034 mmc
->capacity_boot
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
1036 mmc
->capacity_rpmb
= ext_csd
[EXT_CSD_RPMB_MULT
] << 17;
1038 for (i
= 0; i
< 4; i
++) {
1039 int idx
= EXT_CSD_GP_SIZE_MULT
+ i
* 3;
1040 mmc
->capacity_gp
[i
] = (ext_csd
[idx
+ 2] << 16) +
1041 (ext_csd
[idx
+ 1] << 8) + ext_csd
[idx
];
1042 mmc
->capacity_gp
[i
] *=
1043 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1044 mmc
->capacity_gp
[i
] *= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1048 err
= mmc_set_capacity(mmc
, mmc
->part_num
);
1053 err
= sd_change_freq(mmc
);
1055 err
= mmc_change_freq(mmc
);
1060 /* Restrict card's capabilities by what the host can do */
1061 mmc
->card_caps
&= mmc
->cfg
->host_caps
;
1064 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1065 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1066 cmd
.resp_type
= MMC_RSP_R1
;
1067 cmd
.cmdarg
= mmc
->rca
<< 16;
1069 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1073 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1074 cmd
.resp_type
= MMC_RSP_R1
;
1076 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1080 mmc_set_bus_width(mmc
, 4);
1083 if (mmc
->card_caps
& MMC_MODE_HS
)
1084 mmc
->tran_speed
= 50000000;
1086 mmc
->tran_speed
= 25000000;
1090 /* An array of possible bus widths in order of preference */
1091 static unsigned ext_csd_bits
[] = {
1092 EXT_CSD_DDR_BUS_WIDTH_8
,
1093 EXT_CSD_DDR_BUS_WIDTH_4
,
1094 EXT_CSD_BUS_WIDTH_8
,
1095 EXT_CSD_BUS_WIDTH_4
,
1096 EXT_CSD_BUS_WIDTH_1
,
1099 /* An array to map CSD bus widths to host cap bits */
1100 static unsigned ext_to_hostcaps
[] = {
1101 [EXT_CSD_DDR_BUS_WIDTH_4
] = MMC_MODE_DDR_52MHz
,
1102 [EXT_CSD_DDR_BUS_WIDTH_8
] = MMC_MODE_DDR_52MHz
,
1103 [EXT_CSD_BUS_WIDTH_4
] = MMC_MODE_4BIT
,
1104 [EXT_CSD_BUS_WIDTH_8
] = MMC_MODE_8BIT
,
1107 /* An array to map chosen bus width to an integer */
1108 static unsigned widths
[] = {
1112 for (idx
=0; idx
< ARRAY_SIZE(ext_csd_bits
); idx
++) {
1113 unsigned int extw
= ext_csd_bits
[idx
];
1116 * Check to make sure the controller supports
1117 * this bus width, if it's more than 1
1119 if (extw
!= EXT_CSD_BUS_WIDTH_1
&&
1120 !(mmc
->cfg
->host_caps
& ext_to_hostcaps
[extw
]))
1123 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1124 EXT_CSD_BUS_WIDTH
, extw
);
1129 mmc_set_bus_width(mmc
, widths
[idx
]);
1131 err
= mmc_send_ext_csd(mmc
, test_csd
);
1132 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1133 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1134 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1135 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1136 && ext_csd
[EXT_CSD_REV
] \
1137 == test_csd
[EXT_CSD_REV
]
1138 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1139 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1140 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1141 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1143 mmc
->card_caps
|= ext_to_hostcaps
[extw
];
1148 if (mmc
->card_caps
& MMC_MODE_HS
) {
1149 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1150 mmc
->tran_speed
= 52000000;
1152 mmc
->tran_speed
= 26000000;
1156 mmc_set_clock(mmc
, mmc
->tran_speed
);
1158 /* fill in device description */
1159 mmc
->block_dev
.lun
= 0;
1160 mmc
->block_dev
.type
= 0;
1161 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1162 mmc
->block_dev
.log2blksz
= LOG2(mmc
->block_dev
.blksz
);
1163 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1164 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1165 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %04x%04x",
1166 mmc
->cid
[0] >> 24, (mmc
->cid
[2] & 0xffff),
1167 (mmc
->cid
[3] >> 16) & 0xffff);
1168 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1169 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1170 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff,
1171 (mmc
->cid
[2] >> 24) & 0xff);
1172 sprintf(mmc
->block_dev
.revision
, "%d.%d", (mmc
->cid
[2] >> 20) & 0xf,
1173 (mmc
->cid
[2] >> 16) & 0xf);
1175 mmc
->block_dev
.vendor
[0] = 0;
1176 mmc
->block_dev
.product
[0] = 0;
1177 mmc
->block_dev
.revision
[0] = 0;
1179 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1180 init_part(&mmc
->block_dev
);
1186 static int mmc_send_if_cond(struct mmc
*mmc
)
1191 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1192 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1193 cmd
.cmdarg
= ((mmc
->cfg
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1194 cmd
.resp_type
= MMC_RSP_R7
;
1196 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1201 if ((cmd
.response
[0] & 0xff) != 0xaa)
1202 return UNUSABLE_ERR
;
1204 mmc
->version
= SD_VERSION_2
;
1209 /* not used any more */
1210 int __deprecated
mmc_register(struct mmc
*mmc
)
1212 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1213 printf("%s is deprecated! use mmc_create() instead.\n", __func__
);
1218 struct mmc
*mmc_create(const struct mmc_config
*cfg
, void *priv
)
1222 /* quick validation */
1223 if (cfg
== NULL
|| cfg
->ops
== NULL
|| cfg
->ops
->send_cmd
== NULL
||
1224 cfg
->f_min
== 0 || cfg
->f_max
== 0 || cfg
->b_max
== 0)
1227 mmc
= calloc(1, sizeof(*mmc
));
1234 /* the following chunk was mmc_register() */
1236 /* Setup dsr related values */
1238 mmc
->dsr
= 0xffffffff;
1239 /* Setup the universal parts of the block interface just once */
1240 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1241 mmc
->block_dev
.dev
= cur_dev_num
++;
1242 mmc
->block_dev
.removable
= 1;
1243 mmc
->block_dev
.block_read
= mmc_bread
;
1244 mmc
->block_dev
.block_write
= mmc_bwrite
;
1245 mmc
->block_dev
.block_erase
= mmc_berase
;
1247 /* setup initial part type */
1248 mmc
->block_dev
.part_type
= mmc
->cfg
->part_type
;
1250 INIT_LIST_HEAD(&mmc
->link
);
1252 list_add_tail(&mmc
->link
, &mmc_devices
);
1257 void mmc_destroy(struct mmc
*mmc
)
1259 /* only freeing memory for now */
1263 #ifdef CONFIG_PARTITIONS
1264 block_dev_desc_t
*mmc_get_dev(int dev
)
1266 struct mmc
*mmc
= find_mmc_device(dev
);
1267 if (!mmc
|| mmc_init(mmc
))
1270 return &mmc
->block_dev
;
1274 int mmc_start_init(struct mmc
*mmc
)
1278 /* we pretend there's no card when init is NULL */
1279 if (mmc_getcd(mmc
) == 0 || mmc
->cfg
->ops
->init
== NULL
) {
1281 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1282 printf("MMC: no card present\n");
1290 /* made sure it's not NULL earlier */
1291 err
= mmc
->cfg
->ops
->init(mmc
);
1296 mmc_set_bus_width(mmc
, 1);
1297 mmc_set_clock(mmc
, 1);
1299 /* Reset the Card */
1300 err
= mmc_go_idle(mmc
);
1305 /* The internal partition reset to user partition(0) at every CMD0*/
1308 /* Test for SD version 2 */
1309 err
= mmc_send_if_cond(mmc
);
1311 /* Now try to get the SD card's operating condition */
1312 err
= sd_send_op_cond(mmc
);
1314 /* If the command timed out, we check for an MMC card */
1315 if (err
== TIMEOUT
) {
1316 err
= mmc_send_op_cond(mmc
);
1318 if (err
&& err
!= IN_PROGRESS
) {
1319 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1320 printf("Card did not respond to voltage select!\n");
1322 return UNUSABLE_ERR
;
1326 if (err
== IN_PROGRESS
)
1327 mmc
->init_in_progress
= 1;
1332 static int mmc_complete_init(struct mmc
*mmc
)
1336 if (mmc
->op_cond_pending
)
1337 err
= mmc_complete_op_cond(mmc
);
1340 err
= mmc_startup(mmc
);
1345 mmc
->init_in_progress
= 0;
1349 int mmc_init(struct mmc
*mmc
)
1351 int err
= IN_PROGRESS
;
1357 start
= get_timer(0);
1359 if (!mmc
->init_in_progress
)
1360 err
= mmc_start_init(mmc
);
1362 if (!err
|| err
== IN_PROGRESS
)
1363 err
= mmc_complete_init(mmc
);
1364 debug("%s: %d, time %lu\n", __func__
, err
, get_timer(start
));
1368 int mmc_set_dsr(struct mmc
*mmc
, u16 val
)
1375 * CPU and board-specific MMC initializations. Aliased function
1376 * signals caller to move on
1378 static int __def_mmc_init(bd_t
*bis
)
1383 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1384 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1386 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1388 void print_mmc_devices(char separator
)
1391 struct list_head
*entry
;
1393 list_for_each(entry
, &mmc_devices
) {
1394 m
= list_entry(entry
, struct mmc
, link
);
1396 printf("%s: %d", m
->cfg
->name
, m
->block_dev
.dev
);
1398 if (entry
->next
!= &mmc_devices
)
1399 printf("%c ", separator
);
1406 void print_mmc_devices(char separator
) { }
1409 int get_mmc_num(void)
1414 void mmc_set_preinit(struct mmc
*mmc
, int preinit
)
1416 mmc
->preinit
= preinit
;
1419 static void do_preinit(void)
1422 struct list_head
*entry
;
1424 list_for_each(entry
, &mmc_devices
) {
1425 m
= list_entry(entry
, struct mmc
, link
);
1433 int mmc_initialize(bd_t
*bis
)
1435 INIT_LIST_HEAD (&mmc_devices
);
1438 if (board_mmc_init(bis
) < 0)
1441 #ifndef CONFIG_SPL_BUILD
1442 print_mmc_devices(',');
1449 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1451 * This function changes the size of boot partition and the size of rpmb
1452 * partition present on EMMC devices.
1455 * struct *mmc: pointer for the mmc device strcuture
1456 * bootsize: size of boot partition
1457 * rpmbsize: size of rpmb partition
1459 * Returns 0 on success.
1462 int mmc_boot_partition_size_change(struct mmc
*mmc
, unsigned long bootsize
,
1463 unsigned long rpmbsize
)
1468 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1469 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1470 cmd
.resp_type
= MMC_RSP_R1b
;
1471 cmd
.cmdarg
= MMC_CMD62_ARG1
;
1473 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1475 debug("mmc_boot_partition_size_change: Error1 = %d\n", err
);
1479 /* Boot partition changing mode */
1480 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1481 cmd
.resp_type
= MMC_RSP_R1b
;
1482 cmd
.cmdarg
= MMC_CMD62_ARG2
;
1484 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1486 debug("mmc_boot_partition_size_change: Error2 = %d\n", err
);
1489 /* boot partition size is multiple of 128KB */
1490 bootsize
= (bootsize
* 1024) / 128;
1492 /* Arg: boot partition size */
1493 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1494 cmd
.resp_type
= MMC_RSP_R1b
;
1495 cmd
.cmdarg
= bootsize
;
1497 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1499 debug("mmc_boot_partition_size_change: Error3 = %d\n", err
);
1502 /* RPMB partition size is multiple of 128KB */
1503 rpmbsize
= (rpmbsize
* 1024) / 128;
1504 /* Arg: RPMB partition size */
1505 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1506 cmd
.resp_type
= MMC_RSP_R1b
;
1507 cmd
.cmdarg
= rpmbsize
;
1509 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1511 debug("mmc_boot_partition_size_change: Error4 = %d\n", err
);
1518 * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1519 * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1522 * Returns 0 on success.
1524 int mmc_set_boot_bus_width(struct mmc
*mmc
, u8 width
, u8 reset
, u8 mode
)
1528 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_BUS_WIDTH
,
1529 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode
) |
1530 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset
) |
1531 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width
));
1539 * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1540 * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1543 * Returns 0 on success.
1545 int mmc_set_part_conf(struct mmc
*mmc
, u8 ack
, u8 part_num
, u8 access
)
1549 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
1550 EXT_CSD_BOOT_ACK(ack
) |
1551 EXT_CSD_BOOT_PART_NUM(part_num
) |
1552 EXT_CSD_PARTITION_ACCESS(access
));
1560 * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1561 * for enable. Note that this is a write-once field for non-zero values.
1563 * Returns 0 on success.
1565 int mmc_set_rst_n_function(struct mmc
*mmc
, u8 enable
)
1567 return mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_RST_N_FUNCTION
,