2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
14 #include <dm/device-internal.h>
18 #include <power/regulator.h>
21 #include <linux/list.h>
23 #include "mmc_private.h"
25 static const unsigned int sd_au_size
[] = {
26 0, SZ_16K
/ 512, SZ_32K
/ 512,
27 SZ_64K
/ 512, SZ_128K
/ 512, SZ_256K
/ 512,
28 SZ_512K
/ 512, SZ_1M
/ 512, SZ_2M
/ 512,
29 SZ_4M
/ 512, SZ_8M
/ 512, (SZ_8M
+ SZ_4M
) / 512,
30 SZ_16M
/ 512, (SZ_16M
+ SZ_8M
) / 512, SZ_32M
/ 512, SZ_64M
/ 512,
33 static int mmc_set_signal_voltage(struct mmc
*mmc
, uint signal_voltage
);
34 static int mmc_power_cycle(struct mmc
*mmc
);
36 #if CONFIG_IS_ENABLED(MMC_TINY)
37 static struct mmc mmc_static
;
38 struct mmc
*find_mmc_device(int dev_num
)
43 void mmc_do_preinit(void)
45 struct mmc
*m
= &mmc_static
;
46 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
47 mmc_set_preinit(m
, 1);
53 struct blk_desc
*mmc_get_blk_desc(struct mmc
*mmc
)
55 return &mmc
->block_dev
;
59 #if !CONFIG_IS_ENABLED(DM_MMC)
60 __weak
int board_mmc_getwp(struct mmc
*mmc
)
65 int mmc_getwp(struct mmc
*mmc
)
69 wp
= board_mmc_getwp(mmc
);
72 if (mmc
->cfg
->ops
->getwp
)
73 wp
= mmc
->cfg
->ops
->getwp(mmc
);
81 __weak
int board_mmc_getcd(struct mmc
*mmc
)
87 #ifdef CONFIG_MMC_TRACE
88 void mmmc_trace_before_send(struct mmc
*mmc
, struct mmc_cmd
*cmd
)
90 printf("CMD_SEND:%d\n", cmd
->cmdidx
);
91 printf("\t\tARG\t\t\t 0x%08X\n", cmd
->cmdarg
);
94 void mmmc_trace_after_send(struct mmc
*mmc
, struct mmc_cmd
*cmd
, int ret
)
100 printf("\t\tRET\t\t\t %d\n", ret
);
102 switch (cmd
->resp_type
) {
104 printf("\t\tMMC_RSP_NONE\n");
107 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
111 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
115 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
117 printf("\t\t \t\t 0x%08X \n",
119 printf("\t\t \t\t 0x%08X \n",
121 printf("\t\t \t\t 0x%08X \n",
124 printf("\t\t\t\t\tDUMPING DATA\n");
125 for (i
= 0; i
< 4; i
++) {
127 printf("\t\t\t\t\t%03d - ", i
*4);
128 ptr
= (u8
*)&cmd
->response
[i
];
130 for (j
= 0; j
< 4; j
++)
131 printf("%02X ", *ptr
--);
136 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
140 printf("\t\tERROR MMC rsp not supported\n");
146 void mmc_trace_state(struct mmc
*mmc
, struct mmc_cmd
*cmd
)
150 status
= (cmd
->response
[0] & MMC_STATUS_CURR_STATE
) >> 9;
151 printf("CURR STATE:%d\n", status
);
155 #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
156 const char *mmc_mode_name(enum bus_mode mode
)
158 static const char *const names
[] = {
159 [MMC_LEGACY
] = "MMC legacy",
160 [SD_LEGACY
] = "SD Legacy",
161 [MMC_HS
] = "MMC High Speed (26MHz)",
162 [SD_HS
] = "SD High Speed (50MHz)",
163 [UHS_SDR12
] = "UHS SDR12 (25MHz)",
164 [UHS_SDR25
] = "UHS SDR25 (50MHz)",
165 [UHS_SDR50
] = "UHS SDR50 (100MHz)",
166 [UHS_SDR104
] = "UHS SDR104 (208MHz)",
167 [UHS_DDR50
] = "UHS DDR50 (50MHz)",
168 [MMC_HS_52
] = "MMC High Speed (52MHz)",
169 [MMC_DDR_52
] = "MMC DDR52 (52MHz)",
170 [MMC_HS_200
] = "HS200 (200MHz)",
173 if (mode
>= MMC_MODES_END
)
174 return "Unknown mode";
180 static uint
mmc_mode2freq(struct mmc
*mmc
, enum bus_mode mode
)
182 static const int freqs
[] = {
183 [SD_LEGACY
] = 25000000,
186 [UHS_SDR12
] = 25000000,
187 [UHS_SDR25
] = 50000000,
188 [UHS_SDR50
] = 100000000,
189 [UHS_SDR104
] = 208000000,
190 [UHS_DDR50
] = 50000000,
191 [MMC_HS_52
] = 52000000,
192 [MMC_DDR_52
] = 52000000,
193 [MMC_HS_200
] = 200000000,
196 if (mode
== MMC_LEGACY
)
197 return mmc
->legacy_speed
;
198 else if (mode
>= MMC_MODES_END
)
204 static int mmc_select_mode(struct mmc
*mmc
, enum bus_mode mode
)
206 mmc
->selected_mode
= mode
;
207 mmc
->tran_speed
= mmc_mode2freq(mmc
, mode
);
208 mmc
->ddr_mode
= mmc_is_mode_ddr(mode
);
209 debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode
),
210 mmc
->tran_speed
/ 1000000);
214 #if !CONFIG_IS_ENABLED(DM_MMC)
215 int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
219 mmmc_trace_before_send(mmc
, cmd
);
220 ret
= mmc
->cfg
->ops
->send_cmd(mmc
, cmd
, data
);
221 mmmc_trace_after_send(mmc
, cmd
, ret
);
227 int mmc_send_status(struct mmc
*mmc
, int timeout
)
230 int err
, retries
= 5;
232 cmd
.cmdidx
= MMC_CMD_SEND_STATUS
;
233 cmd
.resp_type
= MMC_RSP_R1
;
234 if (!mmc_host_is_spi(mmc
))
235 cmd
.cmdarg
= mmc
->rca
<< 16;
238 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
240 if ((cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
) &&
241 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) !=
245 if (cmd
.response
[0] & MMC_STATUS_MASK
) {
246 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
247 printf("Status Error: 0x%08X\n",
252 } else if (--retries
< 0)
261 mmc_trace_state(mmc
, &cmd
);
263 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
264 printf("Timeout waiting card ready\n");
272 int mmc_set_blocklen(struct mmc
*mmc
, int len
)
279 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
280 cmd
.resp_type
= MMC_RSP_R1
;
283 return mmc_send_cmd(mmc
, &cmd
, NULL
);
286 static int mmc_read_blocks(struct mmc
*mmc
, void *dst
, lbaint_t start
,
290 struct mmc_data data
;
293 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
295 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
297 if (mmc
->high_capacity
)
300 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
302 cmd
.resp_type
= MMC_RSP_R1
;
305 data
.blocks
= blkcnt
;
306 data
.blocksize
= mmc
->read_bl_len
;
307 data
.flags
= MMC_DATA_READ
;
309 if (mmc_send_cmd(mmc
, &cmd
, &data
))
313 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
315 cmd
.resp_type
= MMC_RSP_R1b
;
316 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
317 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
318 printf("mmc fail to send stop cmd\n");
327 #if CONFIG_IS_ENABLED(BLK)
328 ulong
mmc_bread(struct udevice
*dev
, lbaint_t start
, lbaint_t blkcnt
, void *dst
)
330 ulong
mmc_bread(struct blk_desc
*block_dev
, lbaint_t start
, lbaint_t blkcnt
,
334 #if CONFIG_IS_ENABLED(BLK)
335 struct blk_desc
*block_dev
= dev_get_uclass_platdata(dev
);
337 int dev_num
= block_dev
->devnum
;
339 lbaint_t cur
, blocks_todo
= blkcnt
;
344 struct mmc
*mmc
= find_mmc_device(dev_num
);
348 if (CONFIG_IS_ENABLED(MMC_TINY
))
349 err
= mmc_switch_part(mmc
, block_dev
->hwpart
);
351 err
= blk_dselect_hwpart(block_dev
, block_dev
->hwpart
);
356 if ((start
+ blkcnt
) > block_dev
->lba
) {
357 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
358 printf("MMC: block number 0x" LBAF
" exceeds max(0x" LBAF
")\n",
359 start
+ blkcnt
, block_dev
->lba
);
364 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
)) {
365 debug("%s: Failed to set blocklen\n", __func__
);
370 cur
= (blocks_todo
> mmc
->cfg
->b_max
) ?
371 mmc
->cfg
->b_max
: blocks_todo
;
372 if (mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
) {
373 debug("%s: Failed to read blocks\n", __func__
);
378 dst
+= cur
* mmc
->read_bl_len
;
379 } while (blocks_todo
> 0);
384 static int mmc_go_idle(struct mmc
*mmc
)
391 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
393 cmd
.resp_type
= MMC_RSP_NONE
;
395 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
405 static int sd_send_op_cond(struct mmc
*mmc
)
412 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
413 cmd
.resp_type
= MMC_RSP_R1
;
416 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
421 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
422 cmd
.resp_type
= MMC_RSP_R3
;
425 * Most cards do not answer if some reserved bits
426 * in the ocr are set. However, Some controller
427 * can set bit 7 (reserved for low voltages), but
428 * how to manage low voltages SD card is not yet
431 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
432 (mmc
->cfg
->voltages
& 0xff8000);
434 if (mmc
->version
== SD_VERSION_2
)
435 cmd
.cmdarg
|= OCR_HCS
;
437 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
442 if (cmd
.response
[0] & OCR_BUSY
)
451 if (mmc
->version
!= SD_VERSION_2
)
452 mmc
->version
= SD_VERSION_1_0
;
454 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
455 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
456 cmd
.resp_type
= MMC_RSP_R3
;
459 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
465 mmc
->ocr
= cmd
.response
[0];
467 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
473 static int mmc_send_op_cond_iter(struct mmc
*mmc
, int use_arg
)
478 cmd
.cmdidx
= MMC_CMD_SEND_OP_COND
;
479 cmd
.resp_type
= MMC_RSP_R3
;
481 if (use_arg
&& !mmc_host_is_spi(mmc
))
482 cmd
.cmdarg
= OCR_HCS
|
483 (mmc
->cfg
->voltages
&
484 (mmc
->ocr
& OCR_VOLTAGE_MASK
)) |
485 (mmc
->ocr
& OCR_ACCESS_MODE
);
487 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
490 mmc
->ocr
= cmd
.response
[0];
494 static int mmc_send_op_cond(struct mmc
*mmc
)
498 /* Some cards seem to need this */
501 /* Asking to the card its capabilities */
502 for (i
= 0; i
< 2; i
++) {
503 err
= mmc_send_op_cond_iter(mmc
, i
!= 0);
507 /* exit if not busy (flag seems to be inverted) */
508 if (mmc
->ocr
& OCR_BUSY
)
511 mmc
->op_cond_pending
= 1;
515 static int mmc_complete_op_cond(struct mmc
*mmc
)
522 mmc
->op_cond_pending
= 0;
523 if (!(mmc
->ocr
& OCR_BUSY
)) {
524 /* Some cards seem to need this */
527 start
= get_timer(0);
529 err
= mmc_send_op_cond_iter(mmc
, 1);
532 if (mmc
->ocr
& OCR_BUSY
)
534 if (get_timer(start
) > timeout
)
540 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
541 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
542 cmd
.resp_type
= MMC_RSP_R3
;
545 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
550 mmc
->ocr
= cmd
.response
[0];
553 mmc
->version
= MMC_VERSION_UNKNOWN
;
555 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
562 static int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
565 struct mmc_data data
;
568 /* Get the Card Status Register */
569 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
570 cmd
.resp_type
= MMC_RSP_R1
;
573 data
.dest
= (char *)ext_csd
;
575 data
.blocksize
= MMC_MAX_BLOCK_LEN
;
576 data
.flags
= MMC_DATA_READ
;
578 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
583 int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
590 cmd
.cmdidx
= MMC_CMD_SWITCH
;
591 cmd
.resp_type
= MMC_RSP_R1b
;
592 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
596 while (retries
> 0) {
597 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
599 /* Waiting for the ready status */
601 ret
= mmc_send_status(mmc
, timeout
);
612 static int mmc_set_card_speed(struct mmc
*mmc
, enum bus_mode mode
)
617 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
623 speed_bits
= EXT_CSD_TIMING_HS
;
625 speed_bits
= EXT_CSD_TIMING_LEGACY
;
630 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
,
635 if ((mode
== MMC_HS
) || (mode
== MMC_HS_52
)) {
636 /* Now check to see that it worked */
637 err
= mmc_send_ext_csd(mmc
, test_csd
);
641 /* No high-speed support */
642 if (!test_csd
[EXT_CSD_HS_TIMING
])
649 static int mmc_get_capabilities(struct mmc
*mmc
)
651 u8
*ext_csd
= mmc
->ext_csd
;
654 mmc
->card_caps
= MMC_MODE_1BIT
;
656 if (mmc_host_is_spi(mmc
))
659 /* Only version 4 supports high-speed */
660 if (mmc
->version
< MMC_VERSION_4
)
664 printf("No ext_csd found!\n"); /* this should enver happen */
668 mmc
->card_caps
|= MMC_MODE_4BIT
| MMC_MODE_8BIT
;
670 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
672 /* High Speed is set, there are two types: 52MHz and 26MHz */
673 if (cardtype
& EXT_CSD_CARD_TYPE_52
) {
674 if (cardtype
& EXT_CSD_CARD_TYPE_DDR_52
)
675 mmc
->card_caps
|= MMC_MODE_DDR_52MHz
;
676 mmc
->card_caps
|= MMC_MODE_HS_52MHz
;
678 if (cardtype
& EXT_CSD_CARD_TYPE_26
)
679 mmc
->card_caps
|= MMC_MODE_HS
;
684 static int mmc_set_capacity(struct mmc
*mmc
, int part_num
)
688 mmc
->capacity
= mmc
->capacity_user
;
692 mmc
->capacity
= mmc
->capacity_boot
;
695 mmc
->capacity
= mmc
->capacity_rpmb
;
701 mmc
->capacity
= mmc
->capacity_gp
[part_num
- 4];
707 mmc_get_blk_desc(mmc
)->lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
712 int mmc_switch_part(struct mmc
*mmc
, unsigned int part_num
)
716 ret
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
717 (mmc
->part_config
& ~PART_ACCESS_MASK
)
718 | (part_num
& PART_ACCESS_MASK
));
721 * Set the capacity if the switch succeeded or was intended
722 * to return to representing the raw device.
724 if ((ret
== 0) || ((ret
== -ENODEV
) && (part_num
== 0))) {
725 ret
= mmc_set_capacity(mmc
, part_num
);
726 mmc_get_blk_desc(mmc
)->hwpart
= part_num
;
732 int mmc_hwpart_config(struct mmc
*mmc
,
733 const struct mmc_hwpart_conf
*conf
,
734 enum mmc_hwpart_conf_mode mode
)
740 u32 max_enh_size_mult
;
741 u32 tot_enh_size_mult
= 0;
744 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
746 if (mode
< MMC_HWPART_CONF_CHECK
|| mode
> MMC_HWPART_CONF_COMPLETE
)
749 if (IS_SD(mmc
) || (mmc
->version
< MMC_VERSION_4_41
)) {
750 printf("eMMC >= 4.4 required for enhanced user data area\n");
754 if (!(mmc
->part_support
& PART_SUPPORT
)) {
755 printf("Card does not support partitioning\n");
759 if (!mmc
->hc_wp_grp_size
) {
760 printf("Card does not define HC WP group size\n");
764 /* check partition alignment and total enhanced size */
765 if (conf
->user
.enh_size
) {
766 if (conf
->user
.enh_size
% mmc
->hc_wp_grp_size
||
767 conf
->user
.enh_start
% mmc
->hc_wp_grp_size
) {
768 printf("User data enhanced area not HC WP group "
772 part_attrs
|= EXT_CSD_ENH_USR
;
773 enh_size_mult
= conf
->user
.enh_size
/ mmc
->hc_wp_grp_size
;
774 if (mmc
->high_capacity
) {
775 enh_start_addr
= conf
->user
.enh_start
;
777 enh_start_addr
= (conf
->user
.enh_start
<< 9);
783 tot_enh_size_mult
+= enh_size_mult
;
785 for (pidx
= 0; pidx
< 4; pidx
++) {
786 if (conf
->gp_part
[pidx
].size
% mmc
->hc_wp_grp_size
) {
787 printf("GP%i partition not HC WP group size "
788 "aligned\n", pidx
+1);
791 gp_size_mult
[pidx
] = conf
->gp_part
[pidx
].size
/ mmc
->hc_wp_grp_size
;
792 if (conf
->gp_part
[pidx
].size
&& conf
->gp_part
[pidx
].enhanced
) {
793 part_attrs
|= EXT_CSD_ENH_GP(pidx
);
794 tot_enh_size_mult
+= gp_size_mult
[pidx
];
798 if (part_attrs
&& ! (mmc
->part_support
& ENHNCD_SUPPORT
)) {
799 printf("Card does not support enhanced attribute\n");
803 err
= mmc_send_ext_csd(mmc
, ext_csd
);
808 (ext_csd
[EXT_CSD_MAX_ENH_SIZE_MULT
+2] << 16) +
809 (ext_csd
[EXT_CSD_MAX_ENH_SIZE_MULT
+1] << 8) +
810 ext_csd
[EXT_CSD_MAX_ENH_SIZE_MULT
];
811 if (tot_enh_size_mult
> max_enh_size_mult
) {
812 printf("Total enhanced size exceeds maximum (%u > %u)\n",
813 tot_enh_size_mult
, max_enh_size_mult
);
817 /* The default value of EXT_CSD_WR_REL_SET is device
818 * dependent, the values can only be changed if the
819 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
820 * changed only once and before partitioning is completed. */
821 wr_rel_set
= ext_csd
[EXT_CSD_WR_REL_SET
];
822 if (conf
->user
.wr_rel_change
) {
823 if (conf
->user
.wr_rel_set
)
824 wr_rel_set
|= EXT_CSD_WR_DATA_REL_USR
;
826 wr_rel_set
&= ~EXT_CSD_WR_DATA_REL_USR
;
828 for (pidx
= 0; pidx
< 4; pidx
++) {
829 if (conf
->gp_part
[pidx
].wr_rel_change
) {
830 if (conf
->gp_part
[pidx
].wr_rel_set
)
831 wr_rel_set
|= EXT_CSD_WR_DATA_REL_GP(pidx
);
833 wr_rel_set
&= ~EXT_CSD_WR_DATA_REL_GP(pidx
);
837 if (wr_rel_set
!= ext_csd
[EXT_CSD_WR_REL_SET
] &&
838 !(ext_csd
[EXT_CSD_WR_REL_PARAM
] & EXT_CSD_HS_CTRL_REL
)) {
839 puts("Card does not support host controlled partition write "
840 "reliability settings\n");
844 if (ext_csd
[EXT_CSD_PARTITION_SETTING
] &
845 EXT_CSD_PARTITION_SETTING_COMPLETED
) {
846 printf("Card already partitioned\n");
850 if (mode
== MMC_HWPART_CONF_CHECK
)
853 /* Partitioning requires high-capacity size definitions */
854 if (!(ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] & 0x01)) {
855 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
856 EXT_CSD_ERASE_GROUP_DEF
, 1);
861 ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] = 1;
863 /* update erase group size to be high-capacity */
864 mmc
->erase_grp_size
=
865 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] * 1024;
869 /* all OK, write the configuration */
870 for (i
= 0; i
< 4; i
++) {
871 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
872 EXT_CSD_ENH_START_ADDR
+i
,
873 (enh_start_addr
>> (i
*8)) & 0xFF);
877 for (i
= 0; i
< 3; i
++) {
878 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
879 EXT_CSD_ENH_SIZE_MULT
+i
,
880 (enh_size_mult
>> (i
*8)) & 0xFF);
884 for (pidx
= 0; pidx
< 4; pidx
++) {
885 for (i
= 0; i
< 3; i
++) {
886 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
887 EXT_CSD_GP_SIZE_MULT
+pidx
*3+i
,
888 (gp_size_mult
[pidx
] >> (i
*8)) & 0xFF);
893 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
894 EXT_CSD_PARTITIONS_ATTRIBUTE
, part_attrs
);
898 if (mode
== MMC_HWPART_CONF_SET
)
901 /* The WR_REL_SET is a write-once register but shall be
902 * written before setting PART_SETTING_COMPLETED. As it is
903 * write-once we can only write it when completing the
905 if (wr_rel_set
!= ext_csd
[EXT_CSD_WR_REL_SET
]) {
906 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
907 EXT_CSD_WR_REL_SET
, wr_rel_set
);
912 /* Setting PART_SETTING_COMPLETED confirms the partition
913 * configuration but it only becomes effective after power
914 * cycle, so we do not adjust the partition related settings
915 * in the mmc struct. */
917 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
918 EXT_CSD_PARTITION_SETTING
,
919 EXT_CSD_PARTITION_SETTING_COMPLETED
);
926 #if !CONFIG_IS_ENABLED(DM_MMC)
927 int mmc_getcd(struct mmc
*mmc
)
931 cd
= board_mmc_getcd(mmc
);
934 if (mmc
->cfg
->ops
->getcd
)
935 cd
= mmc
->cfg
->ops
->getcd(mmc
);
944 static int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
947 struct mmc_data data
;
949 /* Switch the frequency */
950 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
951 cmd
.resp_type
= MMC_RSP_R1
;
952 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
953 cmd
.cmdarg
&= ~(0xf << (group
* 4));
954 cmd
.cmdarg
|= value
<< (group
* 4);
956 data
.dest
= (char *)resp
;
959 data
.flags
= MMC_DATA_READ
;
961 return mmc_send_cmd(mmc
, &cmd
, &data
);
965 static int sd_get_capabilities(struct mmc
*mmc
)
969 ALLOC_CACHE_ALIGN_BUFFER(__be32
, scr
, 2);
970 ALLOC_CACHE_ALIGN_BUFFER(__be32
, switch_status
, 16);
971 struct mmc_data data
;
974 mmc
->card_caps
= MMC_MODE_1BIT
;
976 if (mmc_host_is_spi(mmc
))
979 /* Read the SCR to find out if this card supports higher speeds */
980 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
981 cmd
.resp_type
= MMC_RSP_R1
;
982 cmd
.cmdarg
= mmc
->rca
<< 16;
984 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
989 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
990 cmd
.resp_type
= MMC_RSP_R1
;
996 data
.dest
= (char *)scr
;
999 data
.flags
= MMC_DATA_READ
;
1001 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
1010 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
1011 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
1013 switch ((mmc
->scr
[0] >> 24) & 0xf) {
1015 mmc
->version
= SD_VERSION_1_0
;
1018 mmc
->version
= SD_VERSION_1_10
;
1021 mmc
->version
= SD_VERSION_2
;
1022 if ((mmc
->scr
[0] >> 15) & 0x1)
1023 mmc
->version
= SD_VERSION_3
;
1026 mmc
->version
= SD_VERSION_1_0
;
1030 if (mmc
->scr
[0] & SD_DATA_4BIT
)
1031 mmc
->card_caps
|= MMC_MODE_4BIT
;
1033 /* Version 1.0 doesn't support switching */
1034 if (mmc
->version
== SD_VERSION_1_0
)
1039 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
1040 (u8
*)switch_status
);
1045 /* The high-speed function is busy. Try again */
1046 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
1050 /* If high-speed isn't supported, we return */
1051 if (__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
)
1052 mmc
->card_caps
|= MMC_CAP(SD_HS
);
1057 static int sd_set_card_speed(struct mmc
*mmc
, enum bus_mode mode
)
1061 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
1063 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
1067 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) != 0x01000000)
1073 int sd_select_bus_width(struct mmc
*mmc
, int w
)
1078 if ((w
!= 4) && (w
!= 1))
1081 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1082 cmd
.resp_type
= MMC_RSP_R1
;
1083 cmd
.cmdarg
= mmc
->rca
<< 16;
1085 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1089 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1090 cmd
.resp_type
= MMC_RSP_R1
;
1095 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1102 static int sd_read_ssr(struct mmc
*mmc
)
1106 ALLOC_CACHE_ALIGN_BUFFER(uint
, ssr
, 16);
1107 struct mmc_data data
;
1109 unsigned int au
, eo
, et
, es
;
1111 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1112 cmd
.resp_type
= MMC_RSP_R1
;
1113 cmd
.cmdarg
= mmc
->rca
<< 16;
1115 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1119 cmd
.cmdidx
= SD_CMD_APP_SD_STATUS
;
1120 cmd
.resp_type
= MMC_RSP_R1
;
1124 data
.dest
= (char *)ssr
;
1125 data
.blocksize
= 64;
1127 data
.flags
= MMC_DATA_READ
;
1129 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
1137 for (i
= 0; i
< 16; i
++)
1138 ssr
[i
] = be32_to_cpu(ssr
[i
]);
1140 au
= (ssr
[2] >> 12) & 0xF;
1141 if ((au
<= 9) || (mmc
->version
== SD_VERSION_3
)) {
1142 mmc
->ssr
.au
= sd_au_size
[au
];
1143 es
= (ssr
[3] >> 24) & 0xFF;
1144 es
|= (ssr
[2] & 0xFF) << 8;
1145 et
= (ssr
[3] >> 18) & 0x3F;
1147 eo
= (ssr
[3] >> 16) & 0x3;
1148 mmc
->ssr
.erase_timeout
= (et
* 1000) / es
;
1149 mmc
->ssr
.erase_offset
= eo
* 1000;
1152 debug("Invalid Allocation Unit Size.\n");
1158 /* frequency bases */
1159 /* divided by 10 to be nice to platforms without floating point */
1160 static const int fbase
[] = {
1167 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1168 * to platforms without floating point.
1170 static const u8 multipliers
[] = {
1189 static inline int bus_width(uint cap
)
1191 if (cap
== MMC_MODE_8BIT
)
1193 if (cap
== MMC_MODE_4BIT
)
1195 if (cap
== MMC_MODE_1BIT
)
1197 printf("invalid bus witdh capability 0x%x\n", cap
);
1201 #if !CONFIG_IS_ENABLED(DM_MMC)
1202 static void mmc_send_init_stream(struct mmc
*mmc
)
1206 static int mmc_set_ios(struct mmc
*mmc
)
1210 if (mmc
->cfg
->ops
->set_ios
)
1211 ret
= mmc
->cfg
->ops
->set_ios(mmc
);
1217 int mmc_set_clock(struct mmc
*mmc
, uint clock
, bool disable
)
1219 if (clock
> mmc
->cfg
->f_max
)
1220 clock
= mmc
->cfg
->f_max
;
1222 if (clock
< mmc
->cfg
->f_min
)
1223 clock
= mmc
->cfg
->f_min
;
1226 mmc
->clk_disable
= disable
;
1228 return mmc_set_ios(mmc
);
1231 static int mmc_set_bus_width(struct mmc
*mmc
, uint width
)
1233 mmc
->bus_width
= width
;
1235 return mmc_set_ios(mmc
);
1238 #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1240 * helper function to display the capabilities in a human
1241 * friendly manner. The capabilities include bus width and
1244 void mmc_dump_capabilities(const char *text
, uint caps
)
1248 printf("%s: widths [", text
);
1249 if (caps
& MMC_MODE_8BIT
)
1251 if (caps
& MMC_MODE_4BIT
)
1253 if (caps
& MMC_MODE_1BIT
)
1255 printf("\b\b] modes [");
1256 for (mode
= MMC_LEGACY
; mode
< MMC_MODES_END
; mode
++)
1257 if (MMC_CAP(mode
) & caps
)
1258 printf("%s, ", mmc_mode_name(mode
));
1263 struct mode_width_tuning
{
1268 static int mmc_set_signal_voltage(struct mmc
*mmc
, uint signal_voltage
)
1270 mmc
->signal_voltage
= signal_voltage
;
1271 return mmc_set_ios(mmc
);
1274 static const struct mode_width_tuning sd_modes_by_pref
[] = {
1277 .widths
= MMC_MODE_4BIT
| MMC_MODE_1BIT
,
1281 .widths
= MMC_MODE_4BIT
| MMC_MODE_1BIT
,
1285 #define for_each_sd_mode_by_pref(caps, mwt) \
1286 for (mwt = sd_modes_by_pref;\
1287 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1289 if (caps & MMC_CAP(mwt->mode))
1291 static int sd_select_mode_and_width(struct mmc
*mmc
)
1294 uint widths
[] = {MMC_MODE_4BIT
, MMC_MODE_1BIT
};
1295 const struct mode_width_tuning
*mwt
;
1297 err
= sd_get_capabilities(mmc
);
1300 /* Restrict card's capabilities by what the host can do */
1301 mmc
->card_caps
&= (mmc
->cfg
->host_caps
| MMC_MODE_1BIT
);
1303 for_each_sd_mode_by_pref(mmc
->card_caps
, mwt
) {
1306 for (w
= widths
; w
< widths
+ ARRAY_SIZE(widths
); w
++) {
1307 if (*w
& mmc
->card_caps
& mwt
->widths
) {
1308 debug("trying mode %s width %d (at %d MHz)\n",
1309 mmc_mode_name(mwt
->mode
),
1311 mmc_mode2freq(mmc
, mwt
->mode
) / 1000000);
1313 /* configure the bus width (card + host) */
1314 err
= sd_select_bus_width(mmc
, bus_width(*w
));
1317 mmc_set_bus_width(mmc
, bus_width(*w
));
1319 /* configure the bus mode (card) */
1320 err
= sd_set_card_speed(mmc
, mwt
->mode
);
1324 /* configure the bus mode (host) */
1325 mmc_select_mode(mmc
, mwt
->mode
);
1326 mmc_set_clock(mmc
, mmc
->tran_speed
, false);
1328 err
= sd_read_ssr(mmc
);
1332 printf("bad ssr\n");
1335 /* revert to a safer bus speed */
1336 mmc_select_mode(mmc
, SD_LEGACY
);
1337 mmc_set_clock(mmc
, mmc
->tran_speed
, false);
1342 printf("unable to select a mode\n");
1347 * read the compare the part of ext csd that is constant.
1348 * This can be used to check that the transfer is working
1351 static int mmc_read_and_compare_ext_csd(struct mmc
*mmc
)
1354 const u8
*ext_csd
= mmc
->ext_csd
;
1355 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
1357 err
= mmc_send_ext_csd(mmc
, test_csd
);
1361 /* Only compare read only fields */
1362 if (ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1363 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
] &&
1364 ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
]
1365 == test_csd
[EXT_CSD_HC_WP_GRP_SIZE
] &&
1366 ext_csd
[EXT_CSD_REV
]
1367 == test_csd
[EXT_CSD_REV
] &&
1368 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1369 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] &&
1370 memcmp(&ext_csd
[EXT_CSD_SEC_CNT
],
1371 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0)
1377 static const struct mode_width_tuning mmc_modes_by_pref
[] = {
1380 .widths
= MMC_MODE_8BIT
| MMC_MODE_4BIT
,
1384 .widths
= MMC_MODE_8BIT
| MMC_MODE_4BIT
,
1388 .widths
= MMC_MODE_8BIT
| MMC_MODE_4BIT
| MMC_MODE_1BIT
,
1392 .widths
= MMC_MODE_8BIT
| MMC_MODE_4BIT
| MMC_MODE_1BIT
,
1396 .widths
= MMC_MODE_8BIT
| MMC_MODE_4BIT
| MMC_MODE_1BIT
,
1400 #define for_each_mmc_mode_by_pref(caps, mwt) \
1401 for (mwt = mmc_modes_by_pref;\
1402 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1404 if (caps & MMC_CAP(mwt->mode))
1406 static const struct ext_csd_bus_width
{
1410 } ext_csd_bus_width
[] = {
1411 {MMC_MODE_8BIT
, true, EXT_CSD_DDR_BUS_WIDTH_8
},
1412 {MMC_MODE_4BIT
, true, EXT_CSD_DDR_BUS_WIDTH_4
},
1413 {MMC_MODE_8BIT
, false, EXT_CSD_BUS_WIDTH_8
},
1414 {MMC_MODE_4BIT
, false, EXT_CSD_BUS_WIDTH_4
},
1415 {MMC_MODE_1BIT
, false, EXT_CSD_BUS_WIDTH_1
},
1418 #define for_each_supported_width(caps, ddr, ecbv) \
1419 for (ecbv = ext_csd_bus_width;\
1420 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
1422 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
1424 static int mmc_select_mode_and_width(struct mmc
*mmc
)
1427 const struct mode_width_tuning
*mwt
;
1428 const struct ext_csd_bus_width
*ecbw
;
1430 err
= mmc_get_capabilities(mmc
);
1434 /* Restrict card's capabilities by what the host can do */
1435 mmc
->card_caps
&= (mmc
->cfg
->host_caps
| MMC_MODE_1BIT
);
1437 /* Only version 4 of MMC supports wider bus widths */
1438 if (mmc
->version
< MMC_VERSION_4
)
1441 if (!mmc
->ext_csd
) {
1442 debug("No ext_csd found!\n"); /* this should enver happen */
1446 for_each_mmc_mode_by_pref(mmc
->card_caps
, mwt
) {
1447 for_each_supported_width(mmc
->card_caps
& mwt
->widths
,
1448 mmc_is_mode_ddr(mwt
->mode
), ecbw
) {
1449 debug("trying mode %s width %d (at %d MHz)\n",
1450 mmc_mode_name(mwt
->mode
),
1451 bus_width(ecbw
->cap
),
1452 mmc_mode2freq(mmc
, mwt
->mode
) / 1000000);
1453 /* configure the bus width (card + host) */
1454 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1456 ecbw
->ext_csd_bits
& ~EXT_CSD_DDR_FLAG
);
1459 mmc_set_bus_width(mmc
, bus_width(ecbw
->cap
));
1461 /* configure the bus speed (card) */
1462 err
= mmc_set_card_speed(mmc
, mwt
->mode
);
1467 * configure the bus width AND the ddr mode (card)
1468 * The host side will be taken care of in the next step
1470 if (ecbw
->ext_csd_bits
& EXT_CSD_DDR_FLAG
) {
1471 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1473 ecbw
->ext_csd_bits
);
1478 /* configure the bus mode (host) */
1479 mmc_select_mode(mmc
, mwt
->mode
);
1480 mmc_set_clock(mmc
, mmc
->tran_speed
, false);
1482 /* do a transfer to check the configuration */
1483 err
= mmc_read_and_compare_ext_csd(mmc
);
1487 /* if an error occured, revert to a safer bus mode */
1488 mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1489 EXT_CSD_BUS_WIDTH
, EXT_CSD_BUS_WIDTH_1
);
1490 mmc_select_mode(mmc
, MMC_LEGACY
);
1491 mmc_set_bus_width(mmc
, 1);
1495 printf("unable to select a mode\n");
1500 static int mmc_startup_v4(struct mmc
*mmc
)
1504 bool has_parts
= false;
1505 bool part_completed
;
1508 if (IS_SD(mmc
) || (mmc
->version
< MMC_VERSION_4
))
1511 ext_csd
= malloc_cache_aligned(MMC_MAX_BLOCK_LEN
);
1515 mmc
->ext_csd
= ext_csd
;
1517 /* check ext_csd version and capacity */
1518 err
= mmc_send_ext_csd(mmc
, ext_csd
);
1521 if (ext_csd
[EXT_CSD_REV
] >= 2) {
1523 * According to the JEDEC Standard, the value of
1524 * ext_csd's capacity is valid if the value is more
1527 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
1528 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
1529 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
1530 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
1531 capacity
*= MMC_MAX_BLOCK_LEN
;
1532 if ((capacity
>> 20) > 2 * 1024)
1533 mmc
->capacity_user
= capacity
;
1536 switch (ext_csd
[EXT_CSD_REV
]) {
1538 mmc
->version
= MMC_VERSION_4_1
;
1541 mmc
->version
= MMC_VERSION_4_2
;
1544 mmc
->version
= MMC_VERSION_4_3
;
1547 mmc
->version
= MMC_VERSION_4_41
;
1550 mmc
->version
= MMC_VERSION_4_5
;
1553 mmc
->version
= MMC_VERSION_5_0
;
1556 mmc
->version
= MMC_VERSION_5_1
;
1560 /* The partition data may be non-zero but it is only
1561 * effective if PARTITION_SETTING_COMPLETED is set in
1562 * EXT_CSD, so ignore any data if this bit is not set,
1563 * except for enabling the high-capacity group size
1564 * definition (see below).
1566 part_completed
= !!(ext_csd
[EXT_CSD_PARTITION_SETTING
] &
1567 EXT_CSD_PARTITION_SETTING_COMPLETED
);
1569 /* store the partition info of emmc */
1570 mmc
->part_support
= ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
];
1571 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1572 ext_csd
[EXT_CSD_BOOT_MULT
])
1573 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1574 if (part_completed
&&
1575 (ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & ENHNCD_SUPPORT
))
1576 mmc
->part_attr
= ext_csd
[EXT_CSD_PARTITIONS_ATTRIBUTE
];
1578 mmc
->capacity_boot
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
1580 mmc
->capacity_rpmb
= ext_csd
[EXT_CSD_RPMB_MULT
] << 17;
1582 for (i
= 0; i
< 4; i
++) {
1583 int idx
= EXT_CSD_GP_SIZE_MULT
+ i
* 3;
1584 uint mult
= (ext_csd
[idx
+ 2] << 16) +
1585 (ext_csd
[idx
+ 1] << 8) + ext_csd
[idx
];
1588 if (!part_completed
)
1590 mmc
->capacity_gp
[i
] = mult
;
1591 mmc
->capacity_gp
[i
] *=
1592 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1593 mmc
->capacity_gp
[i
] *= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1594 mmc
->capacity_gp
[i
] <<= 19;
1597 if (part_completed
) {
1598 mmc
->enh_user_size
=
1599 (ext_csd
[EXT_CSD_ENH_SIZE_MULT
+ 2] << 16) +
1600 (ext_csd
[EXT_CSD_ENH_SIZE_MULT
+ 1] << 8) +
1601 ext_csd
[EXT_CSD_ENH_SIZE_MULT
];
1602 mmc
->enh_user_size
*= ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1603 mmc
->enh_user_size
*= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1604 mmc
->enh_user_size
<<= 19;
1605 mmc
->enh_user_start
=
1606 (ext_csd
[EXT_CSD_ENH_START_ADDR
+ 3] << 24) +
1607 (ext_csd
[EXT_CSD_ENH_START_ADDR
+ 2] << 16) +
1608 (ext_csd
[EXT_CSD_ENH_START_ADDR
+ 1] << 8) +
1609 ext_csd
[EXT_CSD_ENH_START_ADDR
];
1610 if (mmc
->high_capacity
)
1611 mmc
->enh_user_start
<<= 9;
1615 * Host needs to enable ERASE_GRP_DEF bit if device is
1616 * partitioned. This bit will be lost every time after a reset
1617 * or power off. This will affect erase size.
1621 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) &&
1622 (ext_csd
[EXT_CSD_PARTITIONS_ATTRIBUTE
] & PART_ENH_ATTRIB
))
1625 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1626 EXT_CSD_ERASE_GROUP_DEF
, 1);
1631 ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] = 1;
1634 if (ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] & 0x01) {
1635 /* Read out group size from ext_csd */
1636 mmc
->erase_grp_size
=
1637 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] * 1024;
1639 * if high capacity and partition setting completed
1640 * SEC_COUNT is valid even if it is smaller than 2 GiB
1641 * JEDEC Standard JESD84-B45, 6.2.4
1643 if (mmc
->high_capacity
&& part_completed
) {
1644 capacity
= (ext_csd
[EXT_CSD_SEC_CNT
]) |
1645 (ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8) |
1646 (ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16) |
1647 (ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24);
1648 capacity
*= MMC_MAX_BLOCK_LEN
;
1649 mmc
->capacity_user
= capacity
;
1652 /* Calculate the group size from the csd value. */
1653 int erase_gsz
, erase_gmul
;
1655 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1656 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1657 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1661 mmc
->hc_wp_grp_size
= 1024
1662 * ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1663 * ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1665 mmc
->wr_rel_set
= ext_csd
[EXT_CSD_WR_REL_SET
];
1670 static int mmc_startup(struct mmc
*mmc
)
1676 struct blk_desc
*bdesc
;
1678 #ifdef CONFIG_MMC_SPI_CRC_ON
1679 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
1680 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
1681 cmd
.resp_type
= MMC_RSP_R1
;
1683 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1690 /* Put the Card in Identify Mode */
1691 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
1692 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
1693 cmd
.resp_type
= MMC_RSP_R2
;
1696 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1701 memcpy(mmc
->cid
, cmd
.response
, 16);
1704 * For MMC cards, set the Relative Address.
1705 * For SD cards, get the Relatvie Address.
1706 * This also puts the cards into Standby State
1708 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
1709 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
1710 cmd
.cmdarg
= mmc
->rca
<< 16;
1711 cmd
.resp_type
= MMC_RSP_R6
;
1713 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1719 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
1722 /* Get the Card-Specific Data */
1723 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
1724 cmd
.resp_type
= MMC_RSP_R2
;
1725 cmd
.cmdarg
= mmc
->rca
<< 16;
1727 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1732 mmc
->csd
[0] = cmd
.response
[0];
1733 mmc
->csd
[1] = cmd
.response
[1];
1734 mmc
->csd
[2] = cmd
.response
[2];
1735 mmc
->csd
[3] = cmd
.response
[3];
1737 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
1738 int version
= (cmd
.response
[0] >> 26) & 0xf;
1742 mmc
->version
= MMC_VERSION_1_2
;
1745 mmc
->version
= MMC_VERSION_1_4
;
1748 mmc
->version
= MMC_VERSION_2_2
;
1751 mmc
->version
= MMC_VERSION_3
;
1754 mmc
->version
= MMC_VERSION_4
;
1757 mmc
->version
= MMC_VERSION_1_2
;
1762 /* divide frequency by 10, since the mults are 10x bigger */
1763 freq
= fbase
[(cmd
.response
[0] & 0x7)];
1764 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
1766 mmc
->legacy_speed
= freq
* mult
;
1767 mmc_select_mode(mmc
, MMC_LEGACY
);
1769 mmc
->dsr_imp
= ((cmd
.response
[1] >> 12) & 0x1);
1770 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
1773 mmc
->write_bl_len
= mmc
->read_bl_len
;
1775 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
1777 if (mmc
->high_capacity
) {
1778 csize
= (mmc
->csd
[1] & 0x3f) << 16
1779 | (mmc
->csd
[2] & 0xffff0000) >> 16;
1782 csize
= (mmc
->csd
[1] & 0x3ff) << 2
1783 | (mmc
->csd
[2] & 0xc0000000) >> 30;
1784 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
1787 mmc
->capacity_user
= (csize
+ 1) << (cmult
+ 2);
1788 mmc
->capacity_user
*= mmc
->read_bl_len
;
1789 mmc
->capacity_boot
= 0;
1790 mmc
->capacity_rpmb
= 0;
1791 for (i
= 0; i
< 4; i
++)
1792 mmc
->capacity_gp
[i
] = 0;
1794 if (mmc
->read_bl_len
> MMC_MAX_BLOCK_LEN
)
1795 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
1797 if (mmc
->write_bl_len
> MMC_MAX_BLOCK_LEN
)
1798 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
1800 if ((mmc
->dsr_imp
) && (0xffffffff != mmc
->dsr
)) {
1801 cmd
.cmdidx
= MMC_CMD_SET_DSR
;
1802 cmd
.cmdarg
= (mmc
->dsr
& 0xffff) << 16;
1803 cmd
.resp_type
= MMC_RSP_NONE
;
1804 if (mmc_send_cmd(mmc
, &cmd
, NULL
))
1805 printf("MMC: SET_DSR failed\n");
1808 /* Select the card, and put it into Transfer Mode */
1809 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
1810 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
1811 cmd
.resp_type
= MMC_RSP_R1
;
1812 cmd
.cmdarg
= mmc
->rca
<< 16;
1813 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1820 * For SD, its erase group is always one sector
1822 mmc
->erase_grp_size
= 1;
1823 mmc
->part_config
= MMCPART_NOAVAILABLE
;
1825 err
= mmc_startup_v4(mmc
);
1829 err
= mmc_set_capacity(mmc
, mmc_get_blk_desc(mmc
)->hwpart
);
1834 err
= sd_select_mode_and_width(mmc
);
1836 err
= mmc_select_mode_and_width(mmc
);
1842 /* Fix the block length for DDR mode */
1843 if (mmc
->ddr_mode
) {
1844 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
1845 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
1848 /* fill in device description */
1849 bdesc
= mmc_get_blk_desc(mmc
);
1853 bdesc
->blksz
= mmc
->read_bl_len
;
1854 bdesc
->log2blksz
= LOG2(bdesc
->blksz
);
1855 bdesc
->lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1856 #if !defined(CONFIG_SPL_BUILD) || \
1857 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1858 !defined(CONFIG_USE_TINY_PRINTF))
1859 sprintf(bdesc
->vendor
, "Man %06x Snr %04x%04x",
1860 mmc
->cid
[0] >> 24, (mmc
->cid
[2] & 0xffff),
1861 (mmc
->cid
[3] >> 16) & 0xffff);
1862 sprintf(bdesc
->product
, "%c%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1863 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1864 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff,
1865 (mmc
->cid
[2] >> 24) & 0xff);
1866 sprintf(bdesc
->revision
, "%d.%d", (mmc
->cid
[2] >> 20) & 0xf,
1867 (mmc
->cid
[2] >> 16) & 0xf);
1869 bdesc
->vendor
[0] = 0;
1870 bdesc
->product
[0] = 0;
1871 bdesc
->revision
[0] = 0;
1873 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1880 static int mmc_send_if_cond(struct mmc
*mmc
)
1885 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1886 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1887 cmd
.cmdarg
= ((mmc
->cfg
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1888 cmd
.resp_type
= MMC_RSP_R7
;
1890 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1895 if ((cmd
.response
[0] & 0xff) != 0xaa)
1898 mmc
->version
= SD_VERSION_2
;
1903 #if !CONFIG_IS_ENABLED(DM_MMC)
1904 /* board-specific MMC power initializations. */
1905 __weak
void board_mmc_power_init(void)
1910 static int mmc_power_init(struct mmc
*mmc
)
1912 #if CONFIG_IS_ENABLED(DM_MMC)
1913 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1916 ret
= device_get_supply_regulator(mmc
->dev
, "vmmc-supply",
1919 debug("%s: No vmmc supply\n", mmc
->dev
->name
);
1921 ret
= device_get_supply_regulator(mmc
->dev
, "vqmmc-supply",
1922 &mmc
->vqmmc_supply
);
1924 debug("%s: No vqmmc supply\n", mmc
->dev
->name
);
1926 #else /* !CONFIG_DM_MMC */
1928 * Driver model should use a regulator, as above, rather than calling
1929 * out to board code.
1931 board_mmc_power_init();
1937 * put the host in the initial state:
1938 * - turn on Vdd (card power supply)
1939 * - configure the bus width and clock to minimal values
1941 static void mmc_set_initial_state(struct mmc
*mmc
)
1945 /* First try to set 3.3V. If it fails set to 1.8V */
1946 err
= mmc_set_signal_voltage(mmc
, MMC_SIGNAL_VOLTAGE_330
);
1948 err
= mmc_set_signal_voltage(mmc
, MMC_SIGNAL_VOLTAGE_180
);
1950 printf("mmc: failed to set signal voltage\n");
1952 mmc_select_mode(mmc
, MMC_LEGACY
);
1953 mmc_set_bus_width(mmc
, 1);
1954 mmc_set_clock(mmc
, 0, false);
1957 static int mmc_power_on(struct mmc
*mmc
)
1959 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
1960 if (mmc
->vmmc_supply
) {
1961 int ret
= regulator_set_enable(mmc
->vmmc_supply
, true);
1964 puts("Error enabling VMMC supply\n");
1972 static int mmc_power_off(struct mmc
*mmc
)
1974 mmc_set_clock(mmc
, 1, true);
1975 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
1976 if (mmc
->vmmc_supply
) {
1977 int ret
= regulator_set_enable(mmc
->vmmc_supply
, false);
1980 puts("Error disabling VMMC supply\n");
1988 static int mmc_power_cycle(struct mmc
*mmc
)
1992 ret
= mmc_power_off(mmc
);
1996 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
1997 * to be on the safer side.
2000 return mmc_power_on(mmc
);
2003 int mmc_start_init(struct mmc
*mmc
)
2008 /* we pretend there's no card when init is NULL */
2009 no_card
= mmc_getcd(mmc
) == 0;
2010 #if !CONFIG_IS_ENABLED(DM_MMC)
2011 no_card
= no_card
|| (mmc
->cfg
->ops
->init
== NULL
);
2015 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2016 printf("MMC: no card present\n");
2024 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2025 mmc_adapter_card_type_ident();
2027 err
= mmc_power_init(mmc
);
2031 err
= mmc_power_on(mmc
);
2035 #if CONFIG_IS_ENABLED(DM_MMC)
2036 /* The device has already been probed ready for use */
2038 /* made sure it's not NULL earlier */
2039 err
= mmc
->cfg
->ops
->init(mmc
);
2045 mmc_set_initial_state(mmc
);
2046 mmc_send_init_stream(mmc
);
2048 /* Reset the Card */
2049 err
= mmc_go_idle(mmc
);
2054 /* The internal partition reset to user partition(0) at every CMD0*/
2055 mmc_get_blk_desc(mmc
)->hwpart
= 0;
2057 /* Test for SD version 2 */
2058 err
= mmc_send_if_cond(mmc
);
2060 /* Now try to get the SD card's operating condition */
2061 err
= sd_send_op_cond(mmc
);
2063 /* If the command timed out, we check for an MMC card */
2064 if (err
== -ETIMEDOUT
) {
2065 err
= mmc_send_op_cond(mmc
);
2068 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2069 printf("Card did not respond to voltage select!\n");
2076 mmc
->init_in_progress
= 1;
2081 static int mmc_complete_init(struct mmc
*mmc
)
2085 mmc
->init_in_progress
= 0;
2086 if (mmc
->op_cond_pending
)
2087 err
= mmc_complete_op_cond(mmc
);
2090 err
= mmc_startup(mmc
);
2098 int mmc_init(struct mmc
*mmc
)
2101 __maybe_unused
unsigned start
;
2102 #if CONFIG_IS_ENABLED(DM_MMC)
2103 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(mmc
->dev
);
2110 start
= get_timer(0);
2112 if (!mmc
->init_in_progress
)
2113 err
= mmc_start_init(mmc
);
2116 err
= mmc_complete_init(mmc
);
2118 printf("%s: %d, time %lu\n", __func__
, err
, get_timer(start
));
2123 int mmc_set_dsr(struct mmc
*mmc
, u16 val
)
2129 /* CPU-specific MMC initializations */
2130 __weak
int cpu_mmc_init(bd_t
*bis
)
2135 /* board-specific MMC initializations. */
2136 __weak
int board_mmc_init(bd_t
*bis
)
2141 void mmc_set_preinit(struct mmc
*mmc
, int preinit
)
2143 mmc
->preinit
= preinit
;
2146 #if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
2147 static int mmc_probe(bd_t
*bis
)
2151 #elif CONFIG_IS_ENABLED(DM_MMC)
2152 static int mmc_probe(bd_t
*bis
)
2156 struct udevice
*dev
;
2158 ret
= uclass_get(UCLASS_MMC
, &uc
);
2163 * Try to add them in sequence order. Really with driver model we
2164 * should allow holes, but the current MMC list does not allow that.
2165 * So if we request 0, 1, 3 we will get 0, 1, 2.
2167 for (i
= 0; ; i
++) {
2168 ret
= uclass_get_device_by_seq(UCLASS_MMC
, i
, &dev
);
2172 uclass_foreach_dev(dev
, uc
) {
2173 ret
= device_probe(dev
);
2175 printf("%s - probe failed: %d\n", dev
->name
, ret
);
2181 static int mmc_probe(bd_t
*bis
)
2183 if (board_mmc_init(bis
) < 0)
2190 int mmc_initialize(bd_t
*bis
)
2192 static int initialized
= 0;
2194 if (initialized
) /* Avoid initializing mmc multiple times */
2198 #if !CONFIG_IS_ENABLED(BLK)
2199 #if !CONFIG_IS_ENABLED(MMC_TINY)
2203 ret
= mmc_probe(bis
);
2207 #ifndef CONFIG_SPL_BUILD
2208 print_mmc_devices(',');
2215 #ifdef CONFIG_CMD_BKOPS_ENABLE
2216 int mmc_set_bkops_enable(struct mmc
*mmc
)
2219 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
2221 err
= mmc_send_ext_csd(mmc
, ext_csd
);
2223 puts("Could not get ext_csd register values\n");
2227 if (!(ext_csd
[EXT_CSD_BKOPS_SUPPORT
] & 0x1)) {
2228 puts("Background operations not supported on device\n");
2229 return -EMEDIUMTYPE
;
2232 if (ext_csd
[EXT_CSD_BKOPS_EN
] & 0x1) {
2233 puts("Background operations already enabled\n");
2237 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BKOPS_EN
, 1);
2239 puts("Failed to enable manual background operations\n");
2243 puts("Enabled manual background operations\n");