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);
35 static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps);
37 #if CONFIG_IS_ENABLED(MMC_TINY)
38 static struct mmc mmc_static;
39 struct mmc *find_mmc_device(int dev_num)
44 void mmc_do_preinit(void)
46 struct mmc *m = &mmc_static;
47 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
48 mmc_set_preinit(m, 1);
54 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
56 return &mmc->block_dev;
60 #if !CONFIG_IS_ENABLED(DM_MMC)
62 static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout)
67 __weak int board_mmc_getwp(struct mmc *mmc)
72 int mmc_getwp(struct mmc *mmc)
76 wp = board_mmc_getwp(mmc);
79 if (mmc->cfg->ops->getwp)
80 wp = mmc->cfg->ops->getwp(mmc);
88 __weak int board_mmc_getcd(struct mmc *mmc)
94 #ifdef CONFIG_MMC_TRACE
95 void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
97 printf("CMD_SEND:%d\n", cmd->cmdidx);
98 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
101 void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
107 printf("\t\tRET\t\t\t %d\n", ret);
109 switch (cmd->resp_type) {
111 printf("\t\tMMC_RSP_NONE\n");
114 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
118 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
122 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
124 printf("\t\t \t\t 0x%08X \n",
126 printf("\t\t \t\t 0x%08X \n",
128 printf("\t\t \t\t 0x%08X \n",
131 printf("\t\t\t\t\tDUMPING DATA\n");
132 for (i = 0; i < 4; i++) {
134 printf("\t\t\t\t\t%03d - ", i*4);
135 ptr = (u8 *)&cmd->response[i];
137 for (j = 0; j < 4; j++)
138 printf("%02X ", *ptr--);
143 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
147 printf("\t\tERROR MMC rsp not supported\n");
153 void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
157 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
158 printf("CURR STATE:%d\n", status);
162 #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
163 const char *mmc_mode_name(enum bus_mode mode)
165 static const char *const names[] = {
166 [MMC_LEGACY] = "MMC legacy",
167 [SD_LEGACY] = "SD Legacy",
168 [MMC_HS] = "MMC High Speed (26MHz)",
169 [SD_HS] = "SD High Speed (50MHz)",
170 [UHS_SDR12] = "UHS SDR12 (25MHz)",
171 [UHS_SDR25] = "UHS SDR25 (50MHz)",
172 [UHS_SDR50] = "UHS SDR50 (100MHz)",
173 [UHS_SDR104] = "UHS SDR104 (208MHz)",
174 [UHS_DDR50] = "UHS DDR50 (50MHz)",
175 [MMC_HS_52] = "MMC High Speed (52MHz)",
176 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
177 [MMC_HS_200] = "HS200 (200MHz)",
180 if (mode >= MMC_MODES_END)
181 return "Unknown mode";
187 static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
189 static const int freqs[] = {
190 [SD_LEGACY] = 25000000,
193 [UHS_SDR12] = 25000000,
194 [UHS_SDR25] = 50000000,
195 [UHS_SDR50] = 100000000,
196 [UHS_SDR104] = 208000000,
197 [UHS_DDR50] = 50000000,
198 [MMC_HS_52] = 52000000,
199 [MMC_DDR_52] = 52000000,
200 [MMC_HS_200] = 200000000,
203 if (mode == MMC_LEGACY)
204 return mmc->legacy_speed;
205 else if (mode >= MMC_MODES_END)
211 static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
213 mmc->selected_mode = mode;
214 mmc->tran_speed = mmc_mode2freq(mmc, mode);
215 mmc->ddr_mode = mmc_is_mode_ddr(mode);
216 debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
217 mmc->tran_speed / 1000000);
221 #if !CONFIG_IS_ENABLED(DM_MMC)
222 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
226 mmmc_trace_before_send(mmc, cmd);
227 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
228 mmmc_trace_after_send(mmc, cmd, ret);
234 int mmc_send_status(struct mmc *mmc, int timeout)
237 int err, retries = 5;
239 cmd.cmdidx = MMC_CMD_SEND_STATUS;
240 cmd.resp_type = MMC_RSP_R1;
241 if (!mmc_host_is_spi(mmc))
242 cmd.cmdarg = mmc->rca << 16;
245 err = mmc_send_cmd(mmc, &cmd, NULL);
247 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
248 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
252 if (cmd.response[0] & MMC_STATUS_MASK) {
253 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
254 printf("Status Error: 0x%08X\n",
259 } else if (--retries < 0)
268 mmc_trace_state(mmc, &cmd);
270 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
271 printf("Timeout waiting card ready\n");
279 int mmc_set_blocklen(struct mmc *mmc, int len)
287 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
288 cmd.resp_type = MMC_RSP_R1;
291 err = mmc_send_cmd(mmc, &cmd, NULL);
293 #ifdef CONFIG_MMC_QUIRKS
294 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
297 * It has been seen that SET_BLOCKLEN may fail on the first
298 * attempt, let's try a few more time
301 err = mmc_send_cmd(mmc, &cmd, NULL);
311 static const u8 tuning_blk_pattern_4bit[] = {
312 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
313 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
314 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
315 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
316 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
317 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
318 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
319 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
322 static const u8 tuning_blk_pattern_8bit[] = {
323 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
324 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
325 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
326 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
327 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
328 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
329 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
330 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
331 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
332 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
333 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
334 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
335 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
336 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
337 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
338 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
341 int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
344 struct mmc_data data;
345 const u8 *tuning_block_pattern;
348 if (mmc->bus_width == 8) {
349 tuning_block_pattern = tuning_blk_pattern_8bit;
350 size = sizeof(tuning_blk_pattern_8bit);
351 } else if (mmc->bus_width == 4) {
352 tuning_block_pattern = tuning_blk_pattern_4bit;
353 size = sizeof(tuning_blk_pattern_4bit);
358 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
362 cmd.resp_type = MMC_RSP_R1;
364 data.dest = (void *)data_buf;
366 data.blocksize = size;
367 data.flags = MMC_DATA_READ;
369 err = mmc_send_cmd(mmc, &cmd, &data);
373 if (memcmp(data_buf, tuning_block_pattern, size))
379 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
383 struct mmc_data data;
386 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
388 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
390 if (mmc->high_capacity)
393 cmd.cmdarg = start * mmc->read_bl_len;
395 cmd.resp_type = MMC_RSP_R1;
398 data.blocks = blkcnt;
399 data.blocksize = mmc->read_bl_len;
400 data.flags = MMC_DATA_READ;
402 if (mmc_send_cmd(mmc, &cmd, &data))
406 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
408 cmd.resp_type = MMC_RSP_R1b;
409 if (mmc_send_cmd(mmc, &cmd, NULL)) {
410 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
411 printf("mmc fail to send stop cmd\n");
420 #if CONFIG_IS_ENABLED(BLK)
421 ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
423 ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
427 #if CONFIG_IS_ENABLED(BLK)
428 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
430 int dev_num = block_dev->devnum;
432 lbaint_t cur, blocks_todo = blkcnt;
437 struct mmc *mmc = find_mmc_device(dev_num);
441 if (CONFIG_IS_ENABLED(MMC_TINY))
442 err = mmc_switch_part(mmc, block_dev->hwpart);
444 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
449 if ((start + blkcnt) > block_dev->lba) {
450 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
451 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
452 start + blkcnt, block_dev->lba);
457 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
458 debug("%s: Failed to set blocklen\n", __func__);
463 cur = (blocks_todo > mmc->cfg->b_max) ?
464 mmc->cfg->b_max : blocks_todo;
465 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
466 debug("%s: Failed to read blocks\n", __func__);
471 dst += cur * mmc->read_bl_len;
472 } while (blocks_todo > 0);
477 static int mmc_go_idle(struct mmc *mmc)
484 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
486 cmd.resp_type = MMC_RSP_NONE;
488 err = mmc_send_cmd(mmc, &cmd, NULL);
498 static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
504 * Send CMD11 only if the request is to switch the card to
507 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
508 return mmc_set_signal_voltage(mmc, signal_voltage);
510 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
512 cmd.resp_type = MMC_RSP_R1;
514 err = mmc_send_cmd(mmc, &cmd, NULL);
518 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
522 * The card should drive cmd and dat[0:3] low immediately
523 * after the response of cmd11, but wait 100 us to be sure
525 err = mmc_wait_dat0(mmc, 0, 100);
532 * During a signal voltage level switch, the clock must be gated
533 * for 5 ms according to the SD spec
535 mmc_set_clock(mmc, mmc->clock, true);
537 err = mmc_set_signal_voltage(mmc, signal_voltage);
541 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
543 mmc_set_clock(mmc, mmc->clock, false);
546 * Failure to switch is indicated by the card holding
547 * dat[0:3] low. Wait for at least 1 ms according to spec
549 err = mmc_wait_dat0(mmc, 1, 1000);
558 static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
565 cmd.cmdidx = MMC_CMD_APP_CMD;
566 cmd.resp_type = MMC_RSP_R1;
569 err = mmc_send_cmd(mmc, &cmd, NULL);
574 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
575 cmd.resp_type = MMC_RSP_R3;
578 * Most cards do not answer if some reserved bits
579 * in the ocr are set. However, Some controller
580 * can set bit 7 (reserved for low voltages), but
581 * how to manage low voltages SD card is not yet
584 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
585 (mmc->cfg->voltages & 0xff8000);
587 if (mmc->version == SD_VERSION_2)
588 cmd.cmdarg |= OCR_HCS;
591 cmd.cmdarg |= OCR_S18R;
593 err = mmc_send_cmd(mmc, &cmd, NULL);
598 if (cmd.response[0] & OCR_BUSY)
607 if (mmc->version != SD_VERSION_2)
608 mmc->version = SD_VERSION_1_0;
610 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
611 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
612 cmd.resp_type = MMC_RSP_R3;
615 err = mmc_send_cmd(mmc, &cmd, NULL);
621 mmc->ocr = cmd.response[0];
623 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
625 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
630 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
636 static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
641 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
642 cmd.resp_type = MMC_RSP_R3;
644 if (use_arg && !mmc_host_is_spi(mmc))
645 cmd.cmdarg = OCR_HCS |
646 (mmc->cfg->voltages &
647 (mmc->ocr & OCR_VOLTAGE_MASK)) |
648 (mmc->ocr & OCR_ACCESS_MODE);
650 err = mmc_send_cmd(mmc, &cmd, NULL);
653 mmc->ocr = cmd.response[0];
657 static int mmc_send_op_cond(struct mmc *mmc)
661 /* Some cards seem to need this */
664 /* Asking to the card its capabilities */
665 for (i = 0; i < 2; i++) {
666 err = mmc_send_op_cond_iter(mmc, i != 0);
670 /* exit if not busy (flag seems to be inverted) */
671 if (mmc->ocr & OCR_BUSY)
674 mmc->op_cond_pending = 1;
678 static int mmc_complete_op_cond(struct mmc *mmc)
685 mmc->op_cond_pending = 0;
686 if (!(mmc->ocr & OCR_BUSY)) {
687 /* Some cards seem to need this */
690 start = get_timer(0);
692 err = mmc_send_op_cond_iter(mmc, 1);
695 if (mmc->ocr & OCR_BUSY)
697 if (get_timer(start) > timeout)
703 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
704 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
705 cmd.resp_type = MMC_RSP_R3;
708 err = mmc_send_cmd(mmc, &cmd, NULL);
713 mmc->ocr = cmd.response[0];
716 mmc->version = MMC_VERSION_UNKNOWN;
718 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
725 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
728 struct mmc_data data;
731 /* Get the Card Status Register */
732 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
733 cmd.resp_type = MMC_RSP_R1;
736 data.dest = (char *)ext_csd;
738 data.blocksize = MMC_MAX_BLOCK_LEN;
739 data.flags = MMC_DATA_READ;
741 err = mmc_send_cmd(mmc, &cmd, &data);
746 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
753 cmd.cmdidx = MMC_CMD_SWITCH;
754 cmd.resp_type = MMC_RSP_R1b;
755 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
759 while (retries > 0) {
760 ret = mmc_send_cmd(mmc, &cmd, NULL);
762 /* Waiting for the ready status */
764 ret = mmc_send_status(mmc, timeout);
775 static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode)
780 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
786 speed_bits = EXT_CSD_TIMING_HS;
789 speed_bits = EXT_CSD_TIMING_HS200;
792 speed_bits = EXT_CSD_TIMING_LEGACY;
797 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
802 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
803 /* Now check to see that it worked */
804 err = mmc_send_ext_csd(mmc, test_csd);
808 /* No high-speed support */
809 if (!test_csd[EXT_CSD_HS_TIMING])
816 static int mmc_get_capabilities(struct mmc *mmc)
818 u8 *ext_csd = mmc->ext_csd;
821 mmc->card_caps = MMC_MODE_1BIT;
823 if (mmc_host_is_spi(mmc))
826 /* Only version 4 supports high-speed */
827 if (mmc->version < MMC_VERSION_4)
831 printf("No ext_csd found!\n"); /* this should enver happen */
835 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
837 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0x3f;
838 mmc->cardtype = cardtype;
840 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
841 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
842 mmc->card_caps |= MMC_MODE_HS200;
844 if (cardtype & EXT_CSD_CARD_TYPE_52) {
845 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
846 mmc->card_caps |= MMC_MODE_DDR_52MHz;
847 mmc->card_caps |= MMC_MODE_HS_52MHz;
849 if (cardtype & EXT_CSD_CARD_TYPE_26)
850 mmc->card_caps |= MMC_MODE_HS;
855 static int mmc_set_capacity(struct mmc *mmc, int part_num)
859 mmc->capacity = mmc->capacity_user;
863 mmc->capacity = mmc->capacity_boot;
866 mmc->capacity = mmc->capacity_rpmb;
872 mmc->capacity = mmc->capacity_gp[part_num - 4];
878 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
883 static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num)
888 if (part_num & PART_ACCESS_MASK)
889 forbidden = MMC_CAP(MMC_HS_200);
891 if (MMC_CAP(mmc->selected_mode) & forbidden) {
892 debug("selected mode (%s) is forbidden for part %d\n",
893 mmc_mode_name(mmc->selected_mode), part_num);
895 } else if (mmc->selected_mode != mmc->best_mode) {
896 debug("selected mode is not optimal\n");
901 return mmc_select_mode_and_width(mmc,
902 mmc->card_caps & ~forbidden);
907 int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
911 ret = mmc_boot_part_access_chk(mmc, part_num);
915 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
916 (mmc->part_config & ~PART_ACCESS_MASK)
917 | (part_num & PART_ACCESS_MASK));
920 * Set the capacity if the switch succeeded or was intended
921 * to return to representing the raw device.
923 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
924 ret = mmc_set_capacity(mmc, part_num);
925 mmc_get_blk_desc(mmc)->hwpart = part_num;
931 int mmc_hwpart_config(struct mmc *mmc,
932 const struct mmc_hwpart_conf *conf,
933 enum mmc_hwpart_conf_mode mode)
939 u32 max_enh_size_mult;
940 u32 tot_enh_size_mult = 0;
943 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
945 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
948 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
949 printf("eMMC >= 4.4 required for enhanced user data area\n");
953 if (!(mmc->part_support & PART_SUPPORT)) {
954 printf("Card does not support partitioning\n");
958 if (!mmc->hc_wp_grp_size) {
959 printf("Card does not define HC WP group size\n");
963 /* check partition alignment and total enhanced size */
964 if (conf->user.enh_size) {
965 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
966 conf->user.enh_start % mmc->hc_wp_grp_size) {
967 printf("User data enhanced area not HC WP group "
971 part_attrs |= EXT_CSD_ENH_USR;
972 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
973 if (mmc->high_capacity) {
974 enh_start_addr = conf->user.enh_start;
976 enh_start_addr = (conf->user.enh_start << 9);
982 tot_enh_size_mult += enh_size_mult;
984 for (pidx = 0; pidx < 4; pidx++) {
985 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
986 printf("GP%i partition not HC WP group size "
987 "aligned\n", pidx+1);
990 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
991 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
992 part_attrs |= EXT_CSD_ENH_GP(pidx);
993 tot_enh_size_mult += gp_size_mult[pidx];
997 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
998 printf("Card does not support enhanced attribute\n");
1002 err = mmc_send_ext_csd(mmc, ext_csd);
1007 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1008 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1009 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1010 if (tot_enh_size_mult > max_enh_size_mult) {
1011 printf("Total enhanced size exceeds maximum (%u > %u)\n",
1012 tot_enh_size_mult, max_enh_size_mult);
1013 return -EMEDIUMTYPE;
1016 /* The default value of EXT_CSD_WR_REL_SET is device
1017 * dependent, the values can only be changed if the
1018 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1019 * changed only once and before partitioning is completed. */
1020 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1021 if (conf->user.wr_rel_change) {
1022 if (conf->user.wr_rel_set)
1023 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1025 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1027 for (pidx = 0; pidx < 4; pidx++) {
1028 if (conf->gp_part[pidx].wr_rel_change) {
1029 if (conf->gp_part[pidx].wr_rel_set)
1030 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1032 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1036 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1037 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1038 puts("Card does not support host controlled partition write "
1039 "reliability settings\n");
1040 return -EMEDIUMTYPE;
1043 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1044 EXT_CSD_PARTITION_SETTING_COMPLETED) {
1045 printf("Card already partitioned\n");
1049 if (mode == MMC_HWPART_CONF_CHECK)
1052 /* Partitioning requires high-capacity size definitions */
1053 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1054 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1055 EXT_CSD_ERASE_GROUP_DEF, 1);
1060 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1062 /* update erase group size to be high-capacity */
1063 mmc->erase_grp_size =
1064 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1068 /* all OK, write the configuration */
1069 for (i = 0; i < 4; i++) {
1070 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1071 EXT_CSD_ENH_START_ADDR+i,
1072 (enh_start_addr >> (i*8)) & 0xFF);
1076 for (i = 0; i < 3; i++) {
1077 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1078 EXT_CSD_ENH_SIZE_MULT+i,
1079 (enh_size_mult >> (i*8)) & 0xFF);
1083 for (pidx = 0; pidx < 4; pidx++) {
1084 for (i = 0; i < 3; i++) {
1085 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1086 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1087 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1092 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1093 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1097 if (mode == MMC_HWPART_CONF_SET)
1100 /* The WR_REL_SET is a write-once register but shall be
1101 * written before setting PART_SETTING_COMPLETED. As it is
1102 * write-once we can only write it when completing the
1104 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1105 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1106 EXT_CSD_WR_REL_SET, wr_rel_set);
1111 /* Setting PART_SETTING_COMPLETED confirms the partition
1112 * configuration but it only becomes effective after power
1113 * cycle, so we do not adjust the partition related settings
1114 * in the mmc struct. */
1116 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1117 EXT_CSD_PARTITION_SETTING,
1118 EXT_CSD_PARTITION_SETTING_COMPLETED);
1125 #if !CONFIG_IS_ENABLED(DM_MMC)
1126 int mmc_getcd(struct mmc *mmc)
1130 cd = board_mmc_getcd(mmc);
1133 if (mmc->cfg->ops->getcd)
1134 cd = mmc->cfg->ops->getcd(mmc);
1143 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
1146 struct mmc_data data;
1148 /* Switch the frequency */
1149 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1150 cmd.resp_type = MMC_RSP_R1;
1151 cmd.cmdarg = (mode << 31) | 0xffffff;
1152 cmd.cmdarg &= ~(0xf << (group * 4));
1153 cmd.cmdarg |= value << (group * 4);
1155 data.dest = (char *)resp;
1156 data.blocksize = 64;
1158 data.flags = MMC_DATA_READ;
1160 return mmc_send_cmd(mmc, &cmd, &data);
1164 static int sd_get_capabilities(struct mmc *mmc)
1168 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1169 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
1170 struct mmc_data data;
1174 mmc->card_caps = MMC_MODE_1BIT;
1176 if (mmc_host_is_spi(mmc))
1179 /* Read the SCR to find out if this card supports higher speeds */
1180 cmd.cmdidx = MMC_CMD_APP_CMD;
1181 cmd.resp_type = MMC_RSP_R1;
1182 cmd.cmdarg = mmc->rca << 16;
1184 err = mmc_send_cmd(mmc, &cmd, NULL);
1189 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1190 cmd.resp_type = MMC_RSP_R1;
1196 data.dest = (char *)scr;
1199 data.flags = MMC_DATA_READ;
1201 err = mmc_send_cmd(mmc, &cmd, &data);
1210 mmc->scr[0] = __be32_to_cpu(scr[0]);
1211 mmc->scr[1] = __be32_to_cpu(scr[1]);
1213 switch ((mmc->scr[0] >> 24) & 0xf) {
1215 mmc->version = SD_VERSION_1_0;
1218 mmc->version = SD_VERSION_1_10;
1221 mmc->version = SD_VERSION_2;
1222 if ((mmc->scr[0] >> 15) & 0x1)
1223 mmc->version = SD_VERSION_3;
1226 mmc->version = SD_VERSION_1_0;
1230 if (mmc->scr[0] & SD_DATA_4BIT)
1231 mmc->card_caps |= MMC_MODE_4BIT;
1233 /* Version 1.0 doesn't support switching */
1234 if (mmc->version == SD_VERSION_1_0)
1239 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
1240 (u8 *)switch_status);
1245 /* The high-speed function is busy. Try again */
1246 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
1250 /* If high-speed isn't supported, we return */
1251 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1252 mmc->card_caps |= MMC_CAP(SD_HS);
1254 /* Version before 3.0 don't support UHS modes */
1255 if (mmc->version < SD_VERSION_3)
1258 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1259 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1260 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1261 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1262 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1263 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1264 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1265 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1266 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1267 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1268 mmc->card_caps |= MMC_CAP(UHS_DDR50);
1273 static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1277 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
1283 speed = UHS_SDR12_BUS_SPEED;
1287 speed = UHS_SDR25_BUS_SPEED;
1290 speed = UHS_SDR50_BUS_SPEED;
1293 speed = UHS_DDR50_BUS_SPEED;
1296 speed = UHS_SDR104_BUS_SPEED;
1302 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
1306 if ((__be32_to_cpu(switch_status[4]) >> 24) != speed)
1312 int sd_select_bus_width(struct mmc *mmc, int w)
1317 if ((w != 4) && (w != 1))
1320 cmd.cmdidx = MMC_CMD_APP_CMD;
1321 cmd.resp_type = MMC_RSP_R1;
1322 cmd.cmdarg = mmc->rca << 16;
1324 err = mmc_send_cmd(mmc, &cmd, NULL);
1328 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1329 cmd.resp_type = MMC_RSP_R1;
1334 err = mmc_send_cmd(mmc, &cmd, NULL);
1341 static int sd_read_ssr(struct mmc *mmc)
1345 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1346 struct mmc_data data;
1348 unsigned int au, eo, et, es;
1350 cmd.cmdidx = MMC_CMD_APP_CMD;
1351 cmd.resp_type = MMC_RSP_R1;
1352 cmd.cmdarg = mmc->rca << 16;
1354 err = mmc_send_cmd(mmc, &cmd, NULL);
1358 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1359 cmd.resp_type = MMC_RSP_R1;
1363 data.dest = (char *)ssr;
1364 data.blocksize = 64;
1366 data.flags = MMC_DATA_READ;
1368 err = mmc_send_cmd(mmc, &cmd, &data);
1376 for (i = 0; i < 16; i++)
1377 ssr[i] = be32_to_cpu(ssr[i]);
1379 au = (ssr[2] >> 12) & 0xF;
1380 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1381 mmc->ssr.au = sd_au_size[au];
1382 es = (ssr[3] >> 24) & 0xFF;
1383 es |= (ssr[2] & 0xFF) << 8;
1384 et = (ssr[3] >> 18) & 0x3F;
1386 eo = (ssr[3] >> 16) & 0x3;
1387 mmc->ssr.erase_timeout = (et * 1000) / es;
1388 mmc->ssr.erase_offset = eo * 1000;
1391 debug("Invalid Allocation Unit Size.\n");
1397 /* frequency bases */
1398 /* divided by 10 to be nice to platforms without floating point */
1399 static const int fbase[] = {
1406 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1407 * to platforms without floating point.
1409 static const u8 multipliers[] = {
1428 static inline int bus_width(uint cap)
1430 if (cap == MMC_MODE_8BIT)
1432 if (cap == MMC_MODE_4BIT)
1434 if (cap == MMC_MODE_1BIT)
1436 printf("invalid bus witdh capability 0x%x\n", cap);
1440 #if !CONFIG_IS_ENABLED(DM_MMC)
1441 static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1446 static void mmc_send_init_stream(struct mmc *mmc)
1450 static int mmc_set_ios(struct mmc *mmc)
1454 if (mmc->cfg->ops->set_ios)
1455 ret = mmc->cfg->ops->set_ios(mmc);
1461 int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
1463 if (clock > mmc->cfg->f_max)
1464 clock = mmc->cfg->f_max;
1466 if (clock < mmc->cfg->f_min)
1467 clock = mmc->cfg->f_min;
1470 mmc->clk_disable = disable;
1472 return mmc_set_ios(mmc);
1475 static int mmc_set_bus_width(struct mmc *mmc, uint width)
1477 mmc->bus_width = width;
1479 return mmc_set_ios(mmc);
1482 #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1484 * helper function to display the capabilities in a human
1485 * friendly manner. The capabilities include bus width and
1488 void mmc_dump_capabilities(const char *text, uint caps)
1492 printf("%s: widths [", text);
1493 if (caps & MMC_MODE_8BIT)
1495 if (caps & MMC_MODE_4BIT)
1497 if (caps & MMC_MODE_1BIT)
1499 printf("\b\b] modes [");
1500 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1501 if (MMC_CAP(mode) & caps)
1502 printf("%s, ", mmc_mode_name(mode));
1507 struct mode_width_tuning {
1513 int mmc_voltage_to_mv(enum mmc_voltage voltage)
1516 case MMC_SIGNAL_VOLTAGE_000: return 0;
1517 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1518 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1519 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1524 static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1528 if (mmc->signal_voltage == signal_voltage)
1531 mmc->signal_voltage = signal_voltage;
1532 err = mmc_set_ios(mmc);
1534 debug("unable to set voltage (err %d)\n", err);
1539 static const struct mode_width_tuning sd_modes_by_pref[] = {
1542 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1543 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1547 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1551 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1555 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1559 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1563 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1567 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1571 #define for_each_sd_mode_by_pref(caps, mwt) \
1572 for (mwt = sd_modes_by_pref;\
1573 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1575 if (caps & MMC_CAP(mwt->mode))
1577 static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
1580 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1581 const struct mode_width_tuning *mwt;
1582 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
1586 mmc_dump_capabilities("sd card", card_caps);
1587 mmc_dump_capabilities("host", mmc->host_caps | MMC_MODE_1BIT);
1590 /* Restrict card's capabilities by what the host can do */
1591 caps = card_caps & (mmc->host_caps | MMC_MODE_1BIT);
1596 for_each_sd_mode_by_pref(caps, mwt) {
1599 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
1600 if (*w & caps & mwt->widths) {
1601 debug("trying mode %s width %d (at %d MHz)\n",
1602 mmc_mode_name(mwt->mode),
1604 mmc_mode2freq(mmc, mwt->mode) / 1000000);
1606 /* configure the bus width (card + host) */
1607 err = sd_select_bus_width(mmc, bus_width(*w));
1610 mmc_set_bus_width(mmc, bus_width(*w));
1612 /* configure the bus mode (card) */
1613 err = sd_set_card_speed(mmc, mwt->mode);
1617 /* configure the bus mode (host) */
1618 mmc_select_mode(mmc, mwt->mode);
1619 mmc_set_clock(mmc, mmc->tran_speed, false);
1621 /* execute tuning if needed */
1622 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1623 err = mmc_execute_tuning(mmc,
1626 debug("tuning failed\n");
1631 err = sd_read_ssr(mmc);
1635 printf("bad ssr\n");
1638 /* revert to a safer bus speed */
1639 mmc_select_mode(mmc, SD_LEGACY);
1640 mmc_set_clock(mmc, mmc->tran_speed, false);
1645 printf("unable to select a mode\n");
1650 * read the compare the part of ext csd that is constant.
1651 * This can be used to check that the transfer is working
1654 static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1657 const u8 *ext_csd = mmc->ext_csd;
1658 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1660 err = mmc_send_ext_csd(mmc, test_csd);
1664 /* Only compare read only fields */
1665 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1666 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1667 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1668 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1669 ext_csd[EXT_CSD_REV]
1670 == test_csd[EXT_CSD_REV] &&
1671 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1672 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1673 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1674 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1680 static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1681 uint32_t allowed_mask)
1687 if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_8V)
1688 card_mask |= MMC_SIGNAL_VOLTAGE_180;
1689 if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_2V)
1690 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1693 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1694 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1695 MMC_SIGNAL_VOLTAGE_180;
1696 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1697 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1700 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1704 while (card_mask & allowed_mask) {
1705 enum mmc_voltage best_match;
1707 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1708 if (!mmc_set_signal_voltage(mmc, best_match))
1711 allowed_mask &= ~best_match;
1717 static const struct mode_width_tuning mmc_modes_by_pref[] = {
1720 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1721 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1725 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1729 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1733 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1737 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1741 #define for_each_mmc_mode_by_pref(caps, mwt) \
1742 for (mwt = mmc_modes_by_pref;\
1743 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1745 if (caps & MMC_CAP(mwt->mode))
1747 static const struct ext_csd_bus_width {
1751 } ext_csd_bus_width[] = {
1752 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1753 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1754 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1755 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1756 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1759 #define for_each_supported_width(caps, ddr, ecbv) \
1760 for (ecbv = ext_csd_bus_width;\
1761 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
1763 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
1765 static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
1768 const struct mode_width_tuning *mwt;
1769 const struct ext_csd_bus_width *ecbw;
1772 mmc_dump_capabilities("mmc", card_caps);
1773 mmc_dump_capabilities("host", mmc->host_caps | MMC_MODE_1BIT);
1776 /* Restrict card's capabilities by what the host can do */
1777 card_caps &= (mmc->host_caps | MMC_MODE_1BIT);
1779 /* Only version 4 of MMC supports wider bus widths */
1780 if (mmc->version < MMC_VERSION_4)
1783 if (!mmc->ext_csd) {
1784 debug("No ext_csd found!\n"); /* this should enver happen */
1788 mmc_set_clock(mmc, mmc->legacy_speed, false);
1790 for_each_mmc_mode_by_pref(card_caps, mwt) {
1791 for_each_supported_width(card_caps & mwt->widths,
1792 mmc_is_mode_ddr(mwt->mode), ecbw) {
1793 enum mmc_voltage old_voltage;
1794 debug("trying mode %s width %d (at %d MHz)\n",
1795 mmc_mode_name(mwt->mode),
1796 bus_width(ecbw->cap),
1797 mmc_mode2freq(mmc, mwt->mode) / 1000000);
1798 old_voltage = mmc->signal_voltage;
1799 err = mmc_set_lowest_voltage(mmc, mwt->mode,
1800 MMC_ALL_SIGNAL_VOLTAGE);
1804 /* configure the bus width (card + host) */
1805 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1807 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
1810 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
1812 /* configure the bus speed (card) */
1813 err = mmc_set_card_speed(mmc, mwt->mode);
1818 * configure the bus width AND the ddr mode (card)
1819 * The host side will be taken care of in the next step
1821 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
1822 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1824 ecbw->ext_csd_bits);
1829 /* configure the bus mode (host) */
1830 mmc_select_mode(mmc, mwt->mode);
1831 mmc_set_clock(mmc, mmc->tran_speed, false);
1833 /* execute tuning if needed */
1835 err = mmc_execute_tuning(mmc, mwt->tuning);
1837 debug("tuning failed\n");
1842 /* do a transfer to check the configuration */
1843 err = mmc_read_and_compare_ext_csd(mmc);
1847 mmc_set_signal_voltage(mmc, old_voltage);
1848 /* if an error occured, revert to a safer bus mode */
1849 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1850 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
1851 mmc_select_mode(mmc, MMC_LEGACY);
1852 mmc_set_bus_width(mmc, 1);
1856 printf("unable to select a mode\n");
1861 static int mmc_startup_v4(struct mmc *mmc)
1865 bool has_parts = false;
1866 bool part_completed;
1869 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
1872 ext_csd = malloc_cache_aligned(MMC_MAX_BLOCK_LEN);
1876 mmc->ext_csd = ext_csd;
1878 /* check ext_csd version and capacity */
1879 err = mmc_send_ext_csd(mmc, ext_csd);
1882 if (ext_csd[EXT_CSD_REV] >= 2) {
1884 * According to the JEDEC Standard, the value of
1885 * ext_csd's capacity is valid if the value is more
1888 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1889 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1890 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1891 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1892 capacity *= MMC_MAX_BLOCK_LEN;
1893 if ((capacity >> 20) > 2 * 1024)
1894 mmc->capacity_user = capacity;
1897 switch (ext_csd[EXT_CSD_REV]) {
1899 mmc->version = MMC_VERSION_4_1;
1902 mmc->version = MMC_VERSION_4_2;
1905 mmc->version = MMC_VERSION_4_3;
1908 mmc->version = MMC_VERSION_4_41;
1911 mmc->version = MMC_VERSION_4_5;
1914 mmc->version = MMC_VERSION_5_0;
1917 mmc->version = MMC_VERSION_5_1;
1921 /* The partition data may be non-zero but it is only
1922 * effective if PARTITION_SETTING_COMPLETED is set in
1923 * EXT_CSD, so ignore any data if this bit is not set,
1924 * except for enabling the high-capacity group size
1925 * definition (see below).
1927 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1928 EXT_CSD_PARTITION_SETTING_COMPLETED);
1930 /* store the partition info of emmc */
1931 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1932 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1933 ext_csd[EXT_CSD_BOOT_MULT])
1934 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1935 if (part_completed &&
1936 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1937 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1939 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1941 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1943 for (i = 0; i < 4; i++) {
1944 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1945 uint mult = (ext_csd[idx + 2] << 16) +
1946 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1949 if (!part_completed)
1951 mmc->capacity_gp[i] = mult;
1952 mmc->capacity_gp[i] *=
1953 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1954 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1955 mmc->capacity_gp[i] <<= 19;
1958 if (part_completed) {
1959 mmc->enh_user_size =
1960 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
1961 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1962 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1963 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1964 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1965 mmc->enh_user_size <<= 19;
1966 mmc->enh_user_start =
1967 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
1968 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1969 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1970 ext_csd[EXT_CSD_ENH_START_ADDR];
1971 if (mmc->high_capacity)
1972 mmc->enh_user_start <<= 9;
1976 * Host needs to enable ERASE_GRP_DEF bit if device is
1977 * partitioned. This bit will be lost every time after a reset
1978 * or power off. This will affect erase size.
1982 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1983 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1986 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1987 EXT_CSD_ERASE_GROUP_DEF, 1);
1992 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1995 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1996 /* Read out group size from ext_csd */
1997 mmc->erase_grp_size =
1998 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
2000 * if high capacity and partition setting completed
2001 * SEC_COUNT is valid even if it is smaller than 2 GiB
2002 * JEDEC Standard JESD84-B45, 6.2.4
2004 if (mmc->high_capacity && part_completed) {
2005 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2006 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2007 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2008 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2009 capacity *= MMC_MAX_BLOCK_LEN;
2010 mmc->capacity_user = capacity;
2013 /* Calculate the group size from the csd value. */
2014 int erase_gsz, erase_gmul;
2016 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2017 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2018 mmc->erase_grp_size = (erase_gsz + 1)
2022 mmc->hc_wp_grp_size = 1024
2023 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2024 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2026 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2031 static int mmc_startup(struct mmc *mmc)
2037 struct blk_desc *bdesc;
2039 #ifdef CONFIG_MMC_SPI_CRC_ON
2040 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2041 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2042 cmd.resp_type = MMC_RSP_R1;
2044 err = mmc_send_cmd(mmc, &cmd, NULL);
2050 /* Put the Card in Identify Mode */
2051 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2052 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
2053 cmd.resp_type = MMC_RSP_R2;
2056 err = mmc_send_cmd(mmc, &cmd, NULL);
2058 #ifdef CONFIG_MMC_QUIRKS
2059 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
2062 * It has been seen that SEND_CID may fail on the first
2063 * attempt, let's try a few more time
2066 err = mmc_send_cmd(mmc, &cmd, NULL);
2069 } while (retries--);
2076 memcpy(mmc->cid, cmd.response, 16);
2079 * For MMC cards, set the Relative Address.
2080 * For SD cards, get the Relatvie Address.
2081 * This also puts the cards into Standby State
2083 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2084 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2085 cmd.cmdarg = mmc->rca << 16;
2086 cmd.resp_type = MMC_RSP_R6;
2088 err = mmc_send_cmd(mmc, &cmd, NULL);
2094 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2097 /* Get the Card-Specific Data */
2098 cmd.cmdidx = MMC_CMD_SEND_CSD;
2099 cmd.resp_type = MMC_RSP_R2;
2100 cmd.cmdarg = mmc->rca << 16;
2102 err = mmc_send_cmd(mmc, &cmd, NULL);
2107 mmc->csd[0] = cmd.response[0];
2108 mmc->csd[1] = cmd.response[1];
2109 mmc->csd[2] = cmd.response[2];
2110 mmc->csd[3] = cmd.response[3];
2112 if (mmc->version == MMC_VERSION_UNKNOWN) {
2113 int version = (cmd.response[0] >> 26) & 0xf;
2117 mmc->version = MMC_VERSION_1_2;
2120 mmc->version = MMC_VERSION_1_4;
2123 mmc->version = MMC_VERSION_2_2;
2126 mmc->version = MMC_VERSION_3;
2129 mmc->version = MMC_VERSION_4;
2132 mmc->version = MMC_VERSION_1_2;
2137 /* divide frequency by 10, since the mults are 10x bigger */
2138 freq = fbase[(cmd.response[0] & 0x7)];
2139 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
2141 mmc->legacy_speed = freq * mult;
2142 mmc_select_mode(mmc, MMC_LEGACY);
2144 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
2145 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
2148 mmc->write_bl_len = mmc->read_bl_len;
2150 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
2152 if (mmc->high_capacity) {
2153 csize = (mmc->csd[1] & 0x3f) << 16
2154 | (mmc->csd[2] & 0xffff0000) >> 16;
2157 csize = (mmc->csd[1] & 0x3ff) << 2
2158 | (mmc->csd[2] & 0xc0000000) >> 30;
2159 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2162 mmc->capacity_user = (csize + 1) << (cmult + 2);
2163 mmc->capacity_user *= mmc->read_bl_len;
2164 mmc->capacity_boot = 0;
2165 mmc->capacity_rpmb = 0;
2166 for (i = 0; i < 4; i++)
2167 mmc->capacity_gp[i] = 0;
2169 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2170 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
2172 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2173 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
2175 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2176 cmd.cmdidx = MMC_CMD_SET_DSR;
2177 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2178 cmd.resp_type = MMC_RSP_NONE;
2179 if (mmc_send_cmd(mmc, &cmd, NULL))
2180 printf("MMC: SET_DSR failed\n");
2183 /* Select the card, and put it into Transfer Mode */
2184 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2185 cmd.cmdidx = MMC_CMD_SELECT_CARD;
2186 cmd.resp_type = MMC_RSP_R1;
2187 cmd.cmdarg = mmc->rca << 16;
2188 err = mmc_send_cmd(mmc, &cmd, NULL);
2195 * For SD, its erase group is always one sector
2197 mmc->erase_grp_size = 1;
2198 mmc->part_config = MMCPART_NOAVAILABLE;
2200 err = mmc_startup_v4(mmc);
2204 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
2209 err = sd_get_capabilities(mmc);
2212 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2214 err = mmc_get_capabilities(mmc);
2217 mmc_select_mode_and_width(mmc, mmc->card_caps);
2223 mmc->best_mode = mmc->selected_mode;
2225 /* Fix the block length for DDR mode */
2226 if (mmc->ddr_mode) {
2227 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
2228 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
2231 /* fill in device description */
2232 bdesc = mmc_get_blk_desc(mmc);
2236 bdesc->blksz = mmc->read_bl_len;
2237 bdesc->log2blksz = LOG2(bdesc->blksz);
2238 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
2239 #if !defined(CONFIG_SPL_BUILD) || \
2240 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
2241 !defined(CONFIG_USE_TINY_PRINTF))
2242 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
2243 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2244 (mmc->cid[3] >> 16) & 0xffff);
2245 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
2246 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2247 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2248 (mmc->cid[2] >> 24) & 0xff);
2249 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
2250 (mmc->cid[2] >> 16) & 0xf);
2252 bdesc->vendor[0] = 0;
2253 bdesc->product[0] = 0;
2254 bdesc->revision[0] = 0;
2256 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
2263 static int mmc_send_if_cond(struct mmc *mmc)
2268 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2269 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
2270 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
2271 cmd.resp_type = MMC_RSP_R7;
2273 err = mmc_send_cmd(mmc, &cmd, NULL);
2278 if ((cmd.response[0] & 0xff) != 0xaa)
2281 mmc->version = SD_VERSION_2;
2286 #if !CONFIG_IS_ENABLED(DM_MMC)
2287 /* board-specific MMC power initializations. */
2288 __weak void board_mmc_power_init(void)
2293 static int mmc_power_init(struct mmc *mmc)
2295 #if CONFIG_IS_ENABLED(DM_MMC)
2296 #if CONFIG_IS_ENABLED(DM_REGULATOR)
2299 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
2302 debug("%s: No vmmc supply\n", mmc->dev->name);
2304 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2305 &mmc->vqmmc_supply);
2307 debug("%s: No vqmmc supply\n", mmc->dev->name);
2309 #else /* !CONFIG_DM_MMC */
2311 * Driver model should use a regulator, as above, rather than calling
2312 * out to board code.
2314 board_mmc_power_init();
2320 * put the host in the initial state:
2321 * - turn on Vdd (card power supply)
2322 * - configure the bus width and clock to minimal values
2324 static void mmc_set_initial_state(struct mmc *mmc)
2328 /* First try to set 3.3V. If it fails set to 1.8V */
2329 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2331 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2333 printf("mmc: failed to set signal voltage\n");
2335 mmc_select_mode(mmc, MMC_LEGACY);
2336 mmc_set_bus_width(mmc, 1);
2337 mmc_set_clock(mmc, 0, false);
2340 static int mmc_power_on(struct mmc *mmc)
2342 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2343 if (mmc->vmmc_supply) {
2344 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2347 puts("Error enabling VMMC supply\n");
2355 static int mmc_power_off(struct mmc *mmc)
2357 mmc_set_clock(mmc, 1, true);
2358 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2359 if (mmc->vmmc_supply) {
2360 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2363 debug("Error disabling VMMC supply\n");
2371 static int mmc_power_cycle(struct mmc *mmc)
2375 ret = mmc_power_off(mmc);
2379 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2380 * to be on the safer side.
2383 return mmc_power_on(mmc);
2386 int mmc_start_init(struct mmc *mmc)
2389 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
2392 mmc->host_caps = mmc->cfg->host_caps;
2394 /* we pretend there's no card when init is NULL */
2395 no_card = mmc_getcd(mmc) == 0;
2396 #if !CONFIG_IS_ENABLED(DM_MMC)
2397 no_card = no_card || (mmc->cfg->ops->init == NULL);
2401 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2402 printf("MMC: no card present\n");
2410 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2411 mmc_adapter_card_type_ident();
2413 err = mmc_power_init(mmc);
2417 #ifdef CONFIG_MMC_QUIRKS
2418 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
2419 MMC_QUIRK_RETRY_SEND_CID;
2422 err = mmc_power_cycle(mmc);
2425 * if power cycling is not supported, we should not try
2426 * to use the UHS modes, because we wouldn't be able to
2427 * recover from an error during the UHS initialization.
2429 debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
2431 mmc->host_caps &= ~UHS_CAPS;
2432 err = mmc_power_on(mmc);
2437 #if CONFIG_IS_ENABLED(DM_MMC)
2438 /* The device has already been probed ready for use */
2440 /* made sure it's not NULL earlier */
2441 err = mmc->cfg->ops->init(mmc);
2448 mmc_set_initial_state(mmc);
2449 mmc_send_init_stream(mmc);
2451 /* Reset the Card */
2452 err = mmc_go_idle(mmc);
2457 /* The internal partition reset to user partition(0) at every CMD0*/
2458 mmc_get_blk_desc(mmc)->hwpart = 0;
2460 /* Test for SD version 2 */
2461 err = mmc_send_if_cond(mmc);
2463 /* Now try to get the SD card's operating condition */
2464 err = sd_send_op_cond(mmc, uhs_en);
2465 if (err && uhs_en) {
2467 mmc_power_cycle(mmc);
2471 /* If the command timed out, we check for an MMC card */
2472 if (err == -ETIMEDOUT) {
2473 err = mmc_send_op_cond(mmc);
2476 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2477 printf("Card did not respond to voltage select!\n");
2484 mmc->init_in_progress = 1;
2489 static int mmc_complete_init(struct mmc *mmc)
2493 mmc->init_in_progress = 0;
2494 if (mmc->op_cond_pending)
2495 err = mmc_complete_op_cond(mmc);
2498 err = mmc_startup(mmc);
2506 int mmc_init(struct mmc *mmc)
2509 __maybe_unused unsigned start;
2510 #if CONFIG_IS_ENABLED(DM_MMC)
2511 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
2518 start = get_timer(0);
2520 if (!mmc->init_in_progress)
2521 err = mmc_start_init(mmc);
2524 err = mmc_complete_init(mmc);
2526 printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
2531 int mmc_set_dsr(struct mmc *mmc, u16 val)
2537 /* CPU-specific MMC initializations */
2538 __weak int cpu_mmc_init(bd_t *bis)
2543 /* board-specific MMC initializations. */
2544 __weak int board_mmc_init(bd_t *bis)
2549 void mmc_set_preinit(struct mmc *mmc, int preinit)
2551 mmc->preinit = preinit;
2554 #if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
2555 static int mmc_probe(bd_t *bis)
2559 #elif CONFIG_IS_ENABLED(DM_MMC)
2560 static int mmc_probe(bd_t *bis)
2564 struct udevice *dev;
2566 ret = uclass_get(UCLASS_MMC, &uc);
2571 * Try to add them in sequence order. Really with driver model we
2572 * should allow holes, but the current MMC list does not allow that.
2573 * So if we request 0, 1, 3 we will get 0, 1, 2.
2575 for (i = 0; ; i++) {
2576 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
2580 uclass_foreach_dev(dev, uc) {
2581 ret = device_probe(dev);
2583 printf("%s - probe failed: %d\n", dev->name, ret);
2589 static int mmc_probe(bd_t *bis)
2591 if (board_mmc_init(bis) < 0)
2598 int mmc_initialize(bd_t *bis)
2600 static int initialized = 0;
2602 if (initialized) /* Avoid initializing mmc multiple times */
2606 #if !CONFIG_IS_ENABLED(BLK)
2607 #if !CONFIG_IS_ENABLED(MMC_TINY)
2611 ret = mmc_probe(bis);
2615 #ifndef CONFIG_SPL_BUILD
2616 print_mmc_devices(',');
2623 #ifdef CONFIG_CMD_BKOPS_ENABLE
2624 int mmc_set_bkops_enable(struct mmc *mmc)
2627 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
2629 err = mmc_send_ext_csd(mmc, ext_csd);
2631 puts("Could not get ext_csd register values\n");
2635 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
2636 puts("Background operations not supported on device\n");
2637 return -EMEDIUMTYPE;
2640 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
2641 puts("Background operations already enabled\n");
2645 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
2647 puts("Failed to enable manual background operations\n");
2651 puts("Enabled manual background operations\n");