1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
4 * Copyright 2019, 2021 NXP
8 * Based vaguely on the pxa mmc code:
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
25 #include <dm/device_compat.h>
26 #include <linux/bitops.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <power/regulator.h>
31 #include <fsl_esdhc_imx.h>
32 #include <fdt_support.h>
35 #include <asm-generic/gpio.h>
36 #include <dm/pinctrl.h>
37 #include <dt-structs.h>
39 #include <dm/ofnode.h>
40 #include <linux/iopoll.h>
42 #if !CONFIG_IS_ENABLED(BLK)
43 #include "mmc_private.h"
46 #ifndef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
47 #ifdef CONFIG_FSL_USDHC
48 #define ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 1
52 DECLARE_GLOBAL_DATA_PTR;
54 #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
56 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
57 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
58 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
60 #define MAX_TUNING_LOOP 40
61 #define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
64 uint dsaddr; /* SDMA system address register */
65 uint blkattr; /* Block attributes register */
66 uint cmdarg; /* Command argument register */
67 uint xfertyp; /* Transfer type register */
68 uint cmdrsp0; /* Command response 0 register */
69 uint cmdrsp1; /* Command response 1 register */
70 uint cmdrsp2; /* Command response 2 register */
71 uint cmdrsp3; /* Command response 3 register */
72 uint datport; /* Buffer data port register */
73 uint prsstat; /* Present state register */
74 uint proctl; /* Protocol control register */
75 uint sysctl; /* System Control Register */
76 uint irqstat; /* Interrupt status register */
77 uint irqstaten; /* Interrupt status enable register */
78 uint irqsigen; /* Interrupt signal enable register */
79 uint autoc12err; /* Auto CMD error status register */
80 uint hostcapblt; /* Host controller capabilities register */
81 uint wml; /* Watermark level register */
82 uint mixctrl; /* For USDHC */
83 char reserved1[4]; /* reserved */
84 uint fevt; /* Force event register */
85 uint admaes; /* ADMA error status register */
86 uint adsaddr; /* ADMA system address register */
90 uint clktunectrlstatus;
98 uint tuning_ctrl; /* on i.MX6/7/8/RT */
100 uint hostver; /* Host controller version register */
101 char reserved6[4]; /* reserved */
102 uint dmaerraddr; /* DMA error address register */
103 char reserved7[4]; /* reserved */
104 uint dmaerrattr; /* DMA error attribute register */
105 char reserved8[4]; /* reserved */
106 uint hostcapblt2; /* Host controller capabilities register 2 */
107 char reserved9[8]; /* reserved */
108 uint tcr; /* Tuning control register */
109 char reserved10[28]; /* reserved */
110 uint sddirctl; /* SD direction control register */
111 char reserved11[712];/* reserved */
112 uint scr; /* eSDHC control register */
115 struct fsl_esdhc_plat {
116 #if CONFIG_IS_ENABLED(OF_PLATDATA)
117 /* Put this first since driver model will copy the data here */
118 struct dtd_fsl_esdhc dtplat;
121 struct mmc_config cfg;
125 struct esdhc_soc_data {
130 * struct fsl_esdhc_priv
132 * @esdhc_regs: registers of the sdhc controller
133 * @sdhc_clk: Current clk of the sdhc controller
134 * @bus_width: bus width, 1bit, 4bit or 8bit
137 * Following is used when Driver Model is enabled for MMC
138 * @dev: pointer for the device
139 * @non_removable: 0: removable; 1: non-removable
140 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
141 * @wp_enable: 1: enable checking wp; 0: no check
142 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
143 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
144 * @caps: controller capabilities
145 * @tuning_step: tuning step setting in tuning_ctrl register
146 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
147 * @strobe_dll_delay_target: settings in strobe_dllctrl
148 * @signal_voltage: indicating the current voltage
149 * @signal_voltage_switch_extra_delay_ms: extra delay for IO voltage switch
150 * @cd_gpio: gpio for card detection
151 * @wp_gpio: gpio for write protection
153 struct fsl_esdhc_priv {
154 struct fsl_esdhc *esdhc_regs;
155 unsigned int sdhc_clk;
159 unsigned int bus_width;
160 #if !CONFIG_IS_ENABLED(BLK)
171 u32 tuning_start_tap;
172 u32 strobe_dll_delay_target;
174 u32 signal_voltage_switch_extra_delay_ms;
175 #if CONFIG_IS_ENABLED(DM_REGULATOR)
176 struct udevice *vqmmc_dev;
177 struct udevice *vmmc_dev;
179 #if CONFIG_IS_ENABLED(DM_GPIO)
180 struct gpio_desc cd_gpio;
181 struct gpio_desc wp_gpio;
185 /* Return the XFERTYP flags for a given command and data packet */
186 static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
191 xfertyp |= XFERTYP_DPSEL;
192 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
193 xfertyp |= XFERTYP_DMAEN;
195 if (data->blocks > 1) {
196 xfertyp |= XFERTYP_MSBSEL;
197 xfertyp |= XFERTYP_BCEN;
198 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
199 xfertyp |= XFERTYP_AC12EN;
203 if (data->flags & MMC_DATA_READ)
204 xfertyp |= XFERTYP_DTDSEL;
207 if (cmd->resp_type & MMC_RSP_CRC)
208 xfertyp |= XFERTYP_CCCEN;
209 if (cmd->resp_type & MMC_RSP_OPCODE)
210 xfertyp |= XFERTYP_CICEN;
211 if (cmd->resp_type & MMC_RSP_136)
212 xfertyp |= XFERTYP_RSPTYP_136;
213 else if (cmd->resp_type & MMC_RSP_BUSY)
214 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
215 else if (cmd->resp_type & MMC_RSP_PRESENT)
216 xfertyp |= XFERTYP_RSPTYP_48;
218 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
219 xfertyp |= XFERTYP_CMDTYP_ABORT;
221 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
224 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
226 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
228 static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
229 struct mmc_data *data)
231 struct fsl_esdhc *regs = priv->esdhc_regs;
239 if (data->flags & MMC_DATA_READ) {
240 blocks = data->blocks;
243 start = get_timer(0);
244 size = data->blocksize;
245 irqstat = esdhc_read32(®s->irqstat);
246 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) {
247 if (get_timer(start) > PIO_TIMEOUT) {
248 printf("\nData Read Failed in PIO Mode.");
252 while (size && (!(irqstat & IRQSTAT_TC))) {
253 udelay(100); /* Wait before last byte transfer complete */
254 irqstat = esdhc_read32(®s->irqstat);
255 databuf = in_le32(®s->datport);
256 *((uint *)buffer) = databuf;
263 blocks = data->blocks;
264 buffer = (char *)data->src;
266 start = get_timer(0);
267 size = data->blocksize;
268 irqstat = esdhc_read32(®s->irqstat);
269 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) {
270 if (get_timer(start) > PIO_TIMEOUT) {
271 printf("\nData Write Failed in PIO Mode.");
275 while (size && (!(irqstat & IRQSTAT_TC))) {
276 udelay(100); /* Wait before last byte transfer complete */
277 databuf = *((uint *)buffer);
280 irqstat = esdhc_read32(®s->irqstat);
281 out_le32(®s->datport, databuf);
289 static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
290 struct mmc_data *data)
293 struct fsl_esdhc *regs = priv->esdhc_regs;
294 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
299 wml_value = data->blocksize/4;
301 if (data->flags & MMC_DATA_READ) {
302 if (wml_value > WML_RD_WML_MAX)
303 wml_value = WML_RD_WML_MAX_VAL;
305 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
306 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
307 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
308 addr = virt_to_phys((void *)(data->dest));
309 if (upper_32_bits(addr))
310 printf("Error found for upper 32 bits\n");
312 esdhc_write32(®s->dsaddr, lower_32_bits(addr));
314 esdhc_write32(®s->dsaddr, (u32)data->dest);
318 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
319 flush_dcache_range((ulong)data->src,
320 (ulong)data->src+data->blocks
323 if (wml_value > WML_WR_WML_MAX)
324 wml_value = WML_WR_WML_MAX_VAL;
325 if (priv->wp_enable) {
326 if ((esdhc_read32(®s->prsstat) &
327 PRSSTAT_WPSPL) == 0) {
328 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
332 #if CONFIG_IS_ENABLED(DM_GPIO)
333 if (dm_gpio_is_valid(&priv->wp_gpio) &&
334 dm_gpio_get_value(&priv->wp_gpio)) {
335 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
341 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
343 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
344 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
345 addr = virt_to_phys((void *)(data->src));
346 if (upper_32_bits(addr))
347 printf("Error found for upper 32 bits\n");
349 esdhc_write32(®s->dsaddr, lower_32_bits(addr));
351 esdhc_write32(®s->dsaddr, (u32)data->src);
356 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
358 /* Calculate the timeout period for data transactions */
360 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
361 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
362 * So, Number of SD Clock cycles for 0.25sec should be minimum
363 * (SD Clock/sec * 0.25 sec) SD Clock cycles
364 * = (mmc->clock * 1/4) SD Clock cycles
366 * => (2^(timeout+13)) >= mmc->clock * 1/4
367 * Taking log2 both the sides
368 * => timeout + 13 >= log2(mmc->clock/4)
369 * Rounding up to next power of 2
370 * => timeout + 13 = log2(mmc->clock/4) + 1
371 * => timeout + 13 = fls(mmc->clock/4)
373 * However, the MMC spec "It is strongly recommended for hosts to
374 * implement more than 500ms timeout value even if the card
375 * indicates the 250ms maximum busy length." Even the previous
376 * value of 300ms is known to be insufficient for some cards.
378 * => timeout + 13 = fls(mmc->clock/2)
380 timeout = fls(mmc->clock/2);
389 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
390 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
394 #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
397 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
402 static void check_and_invalidate_dcache_range
403 (struct mmc_cmd *cmd,
404 struct mmc_data *data) {
407 unsigned size = roundup(ARCH_DMA_MINALIGN,
408 data->blocks*data->blocksize);
409 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
412 addr = virt_to_phys((void *)(data->dest));
413 if (upper_32_bits(addr))
414 printf("Error found for upper 32 bits\n");
416 start = lower_32_bits(addr);
418 start = (unsigned)data->dest;
421 invalidate_dcache_range(start, end);
424 #ifdef CONFIG_MCF5441x
426 * Swaps 32-bit words to little-endian byte order.
428 static inline void sd_swap_dma_buff(struct mmc_data *data)
430 int i, size = data->blocksize >> 2;
431 u32 *buffer = (u32 *)data->dest;
434 while (data->blocks--) {
435 for (i = 0; i < size; i++) {
436 sw = __sw32(*buffer);
444 * Sends a command out on the bus. Takes the mmc pointer,
445 * a command pointer, and an optional data pointer.
447 static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
448 struct mmc_cmd *cmd, struct mmc_data *data)
453 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
454 struct fsl_esdhc *regs = priv->esdhc_regs;
457 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
458 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
462 esdhc_write32(®s->irqstat, -1);
466 /* Wait for the bus to be idle */
467 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
468 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
471 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
474 /* Set up for a data transfer if we have one */
476 err = esdhc_setup_data(priv, mmc, data);
480 if (data->flags & MMC_DATA_READ)
481 check_and_invalidate_dcache_range(cmd, data);
484 /* Figure out the transfer arguments */
485 xfertyp = esdhc_xfertyp(cmd, data);
488 esdhc_write32(®s->irqsigen, 0);
490 /* Send the command */
491 esdhc_write32(®s->cmdarg, cmd->cmdarg);
492 #if defined(CONFIG_FSL_USDHC)
493 esdhc_write32(®s->mixctrl,
494 (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
495 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
496 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
498 esdhc_write32(®s->xfertyp, xfertyp);
501 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
502 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
505 /* Wait for the command to complete */
506 start = get_timer(0);
507 while (!(esdhc_read32(®s->irqstat) & flags)) {
508 if (get_timer(start) > 1000) {
514 irqstat = esdhc_read32(®s->irqstat);
516 if (irqstat & CMD_ERR) {
521 if (irqstat & IRQSTAT_CTOE) {
526 /* Workaround for ESDHC errata ENGcm03648 */
527 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
530 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
531 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
538 printf("Timeout waiting for DAT0 to go high!\n");
544 /* Copy the response to the response buffer */
545 if (cmd->resp_type & MMC_RSP_136) {
546 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
548 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
549 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
550 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
551 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
552 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
553 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
554 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
555 cmd->response[3] = (cmdrsp0 << 8);
557 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
559 /* Wait until all of the blocks are transferred */
561 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
562 esdhc_pio_read_write(priv, data);
564 flags = DATA_COMPLETE;
565 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
566 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
571 irqstat = esdhc_read32(®s->irqstat);
573 if (irqstat & IRQSTAT_DTOE) {
578 if (irqstat & DATA_ERR) {
582 } while ((irqstat & flags) != flags);
585 * Need invalidate the dcache here again to avoid any
586 * cache-fill during the DMA operations such as the
587 * speculative pre-fetching etc.
589 if (data->flags & MMC_DATA_READ) {
590 check_and_invalidate_dcache_range(cmd, data);
591 #ifdef CONFIG_MCF5441x
592 sd_swap_dma_buff(data);
599 /* Reset CMD and DATA portions on error */
601 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
603 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC)
607 esdhc_write32(®s->sysctl,
608 esdhc_read32(®s->sysctl) |
610 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD))
614 /* If this was CMD11, then notify that power cycle is needed */
615 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
616 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
619 esdhc_write32(®s->irqstat, -1);
624 static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
626 struct fsl_esdhc *regs = priv->esdhc_regs;
632 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
633 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
640 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
641 int sdhc_clk = priv->sdhc_clk;
644 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
647 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
653 clk = (pre_div << 8) | (div << 4);
655 #ifdef CONFIG_FSL_USDHC
656 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
657 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, tmp & PRSSTAT_SDOFF, 100);
659 pr_warn("fsl_esdhc_imx: Internal clock never gate off.\n");
661 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
664 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
666 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
668 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
670 #ifdef CONFIG_FSL_USDHC
671 esdhc_setbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
673 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
679 #ifdef MMC_SUPPORTS_TUNING
680 static int esdhc_change_pinstate(struct udevice *dev)
682 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
685 switch (priv->mode) {
688 ret = pinctrl_select_state(dev, "state_100mhz");
694 ret = pinctrl_select_state(dev, "state_200mhz");
697 ret = pinctrl_select_state(dev, "default");
702 printf("%s %d error\n", __func__, priv->mode);
707 static void esdhc_reset_tuning(struct mmc *mmc)
709 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
710 struct fsl_esdhc *regs = priv->esdhc_regs;
712 if (priv->flags & ESDHC_FLAG_USDHC) {
713 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
714 esdhc_clrbits32(®s->autoc12err,
715 MIX_CTRL_SMPCLK_SEL |
721 static void esdhc_set_strobe_dll(struct mmc *mmc)
723 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
724 struct fsl_esdhc *regs = priv->esdhc_regs;
729 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
730 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
731 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, tmp & PRSSTAT_SDOFF, 100);
733 pr_warn("fsl_esdhc_imx: Internal clock never gate off.\n");
734 esdhc_write32(®s->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
737 * enable strobe dll ctrl and adjust the delay target
738 * for the uSDHC loopback read clock
740 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
741 (priv->strobe_dll_delay_target <<
742 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
743 esdhc_write32(®s->strobe_dllctrl, val);
744 /* wait 1us to make sure strobe dll status register stable */
746 val = esdhc_read32(®s->strobe_dllstat);
747 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
748 pr_warn("HS400 strobe DLL status REF not lock!\n");
749 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
750 pr_warn("HS400 strobe DLL status SLV not lock!\n");
751 esdhc_setbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
755 static int esdhc_set_timing(struct mmc *mmc)
757 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
758 struct fsl_esdhc *regs = priv->esdhc_regs;
761 mixctrl = esdhc_read32(®s->mixctrl);
762 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
764 switch (mmc->selected_mode) {
766 esdhc_reset_tuning(mmc);
767 esdhc_write32(®s->mixctrl, mixctrl);
771 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
772 esdhc_write32(®s->mixctrl, mixctrl);
782 esdhc_write32(®s->mixctrl, mixctrl);
786 mixctrl |= MIX_CTRL_DDREN;
787 esdhc_write32(®s->mixctrl, mixctrl);
790 printf("Not supported %d\n", mmc->selected_mode);
794 priv->mode = mmc->selected_mode;
796 return esdhc_change_pinstate(mmc->dev);
799 static int esdhc_set_voltage(struct mmc *mmc)
801 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
802 struct fsl_esdhc *regs = priv->esdhc_regs;
803 #if CONFIG_IS_ENABLED(DM_REGULATOR)
807 priv->signal_voltage = mmc->signal_voltage;
808 switch (mmc->signal_voltage) {
809 case MMC_SIGNAL_VOLTAGE_330:
810 if (priv->vs18_enable)
812 #if CONFIG_IS_ENABLED(DM_REGULATOR)
813 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
814 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
816 printf("Setting to 3.3V error");
824 esdhc_clrbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
825 if (!(esdhc_read32(®s->vendorspec) &
826 ESDHC_VENDORSPEC_VSELECT))
830 case MMC_SIGNAL_VOLTAGE_180:
831 #if CONFIG_IS_ENABLED(DM_REGULATOR)
832 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
833 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
835 printf("Setting to 1.8V error");
840 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
842 * some board like imx8mm-evk need about 18ms to switch
843 * the IO voltage from 3.3v to 1.8v, common code only
844 * delay 10ms, so need to delay extra time to make sure
845 * the IO voltage change to 1.8v.
847 if (priv->signal_voltage_switch_extra_delay_ms)
848 mdelay(priv->signal_voltage_switch_extra_delay_ms);
849 if (esdhc_read32(®s->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
853 case MMC_SIGNAL_VOLTAGE_120:
860 static void esdhc_stop_tuning(struct mmc *mmc)
864 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
866 cmd.resp_type = MMC_RSP_R1b;
868 mmc_send_cmd(mmc, &cmd, NULL);
871 static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
873 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
874 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
875 struct fsl_esdhc *regs = priv->esdhc_regs;
876 struct mmc *mmc = &plat->mmc;
877 u32 irqstaten = esdhc_read32(®s->irqstaten);
878 u32 irqsigen = esdhc_read32(®s->irqsigen);
879 int i, ret = -ETIMEDOUT;
882 /* clock tuning is not needed for upto 52MHz */
883 if (mmc->clock <= 52000000)
886 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
887 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
888 val = esdhc_read32(®s->autoc12err);
889 mixctrl = esdhc_read32(®s->mixctrl);
890 val &= ~MIX_CTRL_SMPCLK_SEL;
891 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
893 val |= MIX_CTRL_EXE_TUNE;
894 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
896 esdhc_write32(®s->autoc12err, val);
897 esdhc_write32(®s->mixctrl, mixctrl);
900 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
901 mixctrl = esdhc_read32(®s->mixctrl);
902 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
903 esdhc_write32(®s->mixctrl, mixctrl);
905 esdhc_write32(®s->irqstaten, IRQSTATEN_BRR);
906 esdhc_write32(®s->irqsigen, IRQSTATEN_BRR);
909 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
910 * of loops reaches 40 times.
912 for (i = 0; i < MAX_TUNING_LOOP; i++) {
915 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
916 if (mmc->bus_width == 8)
917 esdhc_write32(®s->blkattr, 0x7080);
918 else if (mmc->bus_width == 4)
919 esdhc_write32(®s->blkattr, 0x7040);
921 esdhc_write32(®s->blkattr, 0x7040);
924 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
925 val = esdhc_read32(®s->mixctrl);
926 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
927 esdhc_write32(®s->mixctrl, val);
929 /* We are using STD tuning, no need to check return value */
930 mmc_send_tuning(mmc, opcode, NULL);
932 ctrl = esdhc_read32(®s->autoc12err);
933 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
934 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
940 esdhc_write32(®s->irqstaten, irqstaten);
941 esdhc_write32(®s->irqsigen, irqsigen);
943 esdhc_stop_tuning(mmc);
949 static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
951 struct fsl_esdhc *regs = priv->esdhc_regs;
952 int ret __maybe_unused;
955 #ifdef MMC_SUPPORTS_TUNING
957 * call esdhc_set_timing() before update the clock rate,
958 * This is because current we support DDR and SDR mode,
959 * Once the DDR_EN bit is set, the card clock will be
960 * divide by 2 automatically. So need to do this before
961 * setting clock rate.
963 if (priv->mode != mmc->selected_mode) {
964 ret = esdhc_set_timing(mmc);
966 printf("esdhc_set_timing error %d\n", ret);
972 /* Set the clock speed */
974 if (clock < mmc->cfg->f_min)
975 clock = mmc->cfg->f_min;
977 if (priv->clock != clock)
978 set_sysctl(priv, mmc, clock);
980 #ifdef MMC_SUPPORTS_TUNING
981 if (mmc->clk_disable) {
982 #ifdef CONFIG_FSL_USDHC
985 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
986 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, tmp & PRSSTAT_SDOFF, 100);
988 pr_warn("fsl_esdhc_imx: Internal clock never gate off.\n");
990 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
993 #ifdef CONFIG_FSL_USDHC
994 esdhc_setbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
996 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
1001 * For HS400/HS400ES mode, make sure set the strobe dll in the
1002 * target clock rate. So call esdhc_set_strobe_dll() after the
1005 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
1006 esdhc_set_strobe_dll(mmc);
1008 if (priv->signal_voltage != mmc->signal_voltage) {
1009 ret = esdhc_set_voltage(mmc);
1011 if (ret != -ENOTSUPP)
1012 printf("esdhc_set_voltage error %d\n", ret);
1018 /* Set the bus width */
1019 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
1021 if (mmc->bus_width == 4)
1022 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
1023 else if (mmc->bus_width == 8)
1024 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
1029 static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1031 struct fsl_esdhc *regs = priv->esdhc_regs;
1034 /* Reset the entire host controller */
1035 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
1037 /* Wait until the controller is available */
1038 start = get_timer(0);
1039 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
1040 if (get_timer(start) > 1000)
1044 #if defined(CONFIG_FSL_USDHC)
1045 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1046 esdhc_write32(®s->mmcboot, 0x0);
1047 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1048 esdhc_write32(®s->mixctrl, 0x0);
1049 esdhc_write32(®s->clktunectrlstatus, 0x0);
1051 /* Put VEND_SPEC to default value */
1052 if (priv->vs18_enable)
1053 esdhc_write32(®s->vendorspec, (VENDORSPEC_INIT |
1054 ESDHC_VENDORSPEC_VSELECT));
1056 esdhc_write32(®s->vendorspec, VENDORSPEC_INIT);
1058 /* Disable DLL_CTRL delay line */
1059 esdhc_write32(®s->dllctrl, 0x0);
1063 /* Enable cache snooping */
1064 esdhc_write32(®s->scr, 0x00000040);
1067 #ifndef CONFIG_FSL_USDHC
1068 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1070 esdhc_setbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
1073 /* Set the initial clock speed */
1074 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1076 /* Disable the BRR and BWR bits in IRQSTAT */
1077 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1079 #ifdef CONFIG_MCF5441x
1080 esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD);
1082 /* Put the PROCTL reg back to the default */
1083 esdhc_write32(®s->proctl, PROCTL_INIT);
1086 /* Set timout to the maximum value */
1087 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1092 static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1094 struct fsl_esdhc *regs = priv->esdhc_regs;
1097 #ifdef CONFIG_ESDHC_DETECT_QUIRK
1098 if (CONFIG_ESDHC_DETECT_QUIRK)
1102 #if CONFIG_IS_ENABLED(DM_MMC)
1103 if (priv->non_removable)
1106 if (priv->broken_cd)
1108 #if CONFIG_IS_ENABLED(DM_GPIO)
1109 if (dm_gpio_is_valid(&priv->cd_gpio))
1110 return dm_gpio_get_value(&priv->cd_gpio);
1114 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
1120 static int esdhc_reset(struct fsl_esdhc *regs)
1124 /* reset the controller */
1125 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
1127 /* hardware clears the bit when it is done */
1128 start = get_timer(0);
1129 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
1130 if (get_timer(start) > 100) {
1131 printf("MMC/SD: Reset never completed.\n");
1139 #if !CONFIG_IS_ENABLED(DM_MMC)
1140 static int esdhc_getcd(struct mmc *mmc)
1142 struct fsl_esdhc_priv *priv = mmc->priv;
1144 return esdhc_getcd_common(priv);
1147 static int esdhc_init(struct mmc *mmc)
1149 struct fsl_esdhc_priv *priv = mmc->priv;
1151 return esdhc_init_common(priv, mmc);
1154 static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1155 struct mmc_data *data)
1157 struct fsl_esdhc_priv *priv = mmc->priv;
1159 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1162 static int esdhc_set_ios(struct mmc *mmc)
1164 struct fsl_esdhc_priv *priv = mmc->priv;
1166 return esdhc_set_ios_common(priv, mmc);
1169 static const struct mmc_ops esdhc_ops = {
1170 .getcd = esdhc_getcd,
1172 .send_cmd = esdhc_send_cmd,
1173 .set_ios = esdhc_set_ios,
1177 static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1178 struct fsl_esdhc_plat *plat)
1180 struct mmc_config *cfg;
1181 struct fsl_esdhc *regs;
1182 u32 caps, voltage_caps;
1188 regs = priv->esdhc_regs;
1190 /* First reset the eSDHC controller */
1191 ret = esdhc_reset(regs);
1195 #ifdef CONFIG_MCF5441x
1196 /* ColdFire, using SDHC_DATA[3] for card detection */
1197 esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD);
1200 #ifndef CONFIG_FSL_USDHC
1201 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1202 | SYSCTL_IPGEN | SYSCTL_CKEN);
1203 /* Clearing tuning bits in case ROM has set it already */
1204 esdhc_write32(®s->mixctrl, 0);
1205 esdhc_write32(®s->autoc12err, 0);
1206 esdhc_write32(®s->clktunectrlstatus, 0);
1208 esdhc_setbits32(®s->vendorspec, VENDORSPEC_FRC_SDCLK_ON);
1211 if (priv->vs18_enable)
1212 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1214 esdhc_write32(®s->irqstaten, SDHCI_IRQ_EN_BITS);
1216 #ifndef CONFIG_DM_MMC
1217 memset(cfg, '\0', sizeof(*cfg));
1221 caps = esdhc_read32(®s->hostcapblt);
1223 #ifdef CONFIG_MCF5441x
1225 * MCF5441x RM declares in more points that sdhc clock speed must
1226 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1227 * from host capabilities.
1229 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1232 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1233 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1234 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1237 if (caps & ESDHC_HOSTCAPBLT_VS18)
1238 voltage_caps |= MMC_VDD_165_195;
1239 if (caps & ESDHC_HOSTCAPBLT_VS30)
1240 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1241 if (caps & ESDHC_HOSTCAPBLT_VS33)
1242 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1244 cfg->name = "FSL_SDHC";
1245 #if !CONFIG_IS_ENABLED(DM_MMC)
1246 cfg->ops = &esdhc_ops;
1248 #ifdef CONFIG_SYS_SD_VOLTAGE
1249 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1251 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1253 if ((cfg->voltages & voltage_caps) == 0) {
1254 printf("voltage not supported by controller\n");
1258 if (priv->bus_width == 8)
1259 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1260 else if (priv->bus_width == 4)
1261 cfg->host_caps = MMC_MODE_4BIT;
1263 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1264 #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1265 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1268 if (priv->bus_width > 0) {
1269 if (priv->bus_width < 8)
1270 cfg->host_caps &= ~MMC_MODE_8BIT;
1271 if (priv->bus_width < 4)
1272 cfg->host_caps &= ~MMC_MODE_4BIT;
1275 if (caps & ESDHC_HOSTCAPBLT_HSS)
1276 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1278 #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1279 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1280 cfg->host_caps &= ~MMC_MODE_8BIT;
1283 cfg->host_caps |= priv->caps;
1285 cfg->f_min = 400000;
1286 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1288 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1290 esdhc_write32(®s->dllctrl, 0);
1291 if (priv->flags & ESDHC_FLAG_USDHC) {
1292 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1293 u32 val = esdhc_read32(®s->tuning_ctrl);
1295 val |= ESDHC_STD_TUNING_EN;
1296 val &= ~ESDHC_TUNING_START_TAP_MASK;
1297 val |= priv->tuning_start_tap;
1298 val &= ~ESDHC_TUNING_STEP_MASK;
1299 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1301 /* Disable the CMD CRC check for tuning, if not, need to
1302 * add some delay after every tuning command, because
1303 * hardware standard tuning logic will directly go to next
1304 * step once it detect the CMD CRC error, will not wait for
1305 * the card side to finally send out the tuning data, trigger
1306 * the buffer read ready interrupt immediately. If usdhc send
1307 * the next tuning command some eMMC card will stuck, can't
1308 * response, block the tuning procedure or the first command
1309 * after the whole tuning procedure always can't get any response.
1311 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1312 esdhc_write32(®s->tuning_ctrl, val);
1319 #if !CONFIG_IS_ENABLED(DM_MMC)
1320 static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1321 struct fsl_esdhc_priv *priv)
1326 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1327 priv->bus_width = cfg->max_bus_width;
1328 priv->sdhc_clk = cfg->sdhc_clk;
1329 priv->wp_enable = cfg->wp_enable;
1330 priv->vs18_enable = cfg->vs18_enable;
1335 int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
1337 struct fsl_esdhc_plat *plat;
1338 struct fsl_esdhc_priv *priv;
1345 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1348 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1354 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1356 debug("%s xlate failure\n", __func__);
1362 ret = fsl_esdhc_init(priv, plat);
1364 debug("%s init failure\n", __func__);
1370 mmc = mmc_create(&plat->cfg, priv);
1379 int fsl_esdhc_mmc_init(struct bd_info *bis)
1381 struct fsl_esdhc_cfg *cfg;
1383 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1384 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1385 cfg->sdhc_clk = gd->arch.sdhc_clk;
1386 return fsl_esdhc_initialize(bis, cfg);
1390 #ifdef CONFIG_OF_LIBFDT
1391 __weak int esdhc_status_fixup(void *blob, const char *compat)
1393 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
1394 if (!hwconfig("esdhc")) {
1395 do_fixup_by_compat(blob, compat, "status", "disabled",
1396 sizeof("disabled"), 1);
1403 void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
1405 const char *compat = "fsl,esdhc";
1407 if (esdhc_status_fixup(blob, compat))
1410 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1411 gd->arch.sdhc_clk, 1);
1415 #if CONFIG_IS_ENABLED(DM_MMC)
1416 #include <asm/arch/clock.h>
1417 __weak void init_clk_usdhc(u32 index)
1421 static int fsl_esdhc_of_to_plat(struct udevice *dev)
1423 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1424 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1425 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1426 struct udevice *vqmmc_dev;
1429 const void *fdt = gd->fdt_blob;
1430 int node = dev_of_offset(dev);
1435 addr = dev_read_addr(dev);
1436 if (addr == FDT_ADDR_T_NONE)
1438 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1442 val = dev_read_u32_default(dev, "bus-width", -1);
1444 priv->bus_width = 8;
1446 priv->bus_width = 4;
1448 priv->bus_width = 1;
1450 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1451 priv->tuning_step = val;
1452 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1453 ESDHC_TUNING_START_TAP_DEFAULT);
1454 priv->tuning_start_tap = val;
1455 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1456 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1457 priv->strobe_dll_delay_target = val;
1458 val = fdtdec_get_int(fdt, node, "fsl,signal-voltage-switch-extra-delay-ms", 0);
1459 priv->signal_voltage_switch_extra_delay_ms = val;
1461 if (dev_read_bool(dev, "broken-cd"))
1462 priv->broken_cd = 1;
1464 if (dev_read_bool(dev, "non-removable")) {
1465 priv->non_removable = 1;
1467 priv->non_removable = 0;
1468 #if CONFIG_IS_ENABLED(DM_GPIO)
1469 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1474 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1475 priv->wp_enable = 1;
1477 priv->wp_enable = 0;
1478 #if CONFIG_IS_ENABLED(DM_GPIO)
1479 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1484 priv->vs18_enable = 0;
1486 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1488 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1489 * otherwise, emmc will work abnormally.
1491 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1493 dev_dbg(dev, "no vqmmc-supply\n");
1495 priv->vqmmc_dev = vqmmc_dev;
1496 ret = regulator_set_enable(vqmmc_dev, true);
1498 dev_err(dev, "fail to enable vqmmc-supply\n");
1502 if (regulator_get_value(vqmmc_dev) == 1800000)
1503 priv->vs18_enable = 1;
1510 static int fsl_esdhc_probe(struct udevice *dev)
1512 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1513 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1514 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1515 struct esdhc_soc_data *data =
1516 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1518 #if !CONFIG_IS_ENABLED(BLK)
1519 struct blk_desc *bdesc;
1523 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1524 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1527 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1528 val = plat->dtplat.bus_width;
1530 priv->bus_width = 8;
1532 priv->bus_width = 4;
1534 priv->bus_width = 1;
1536 if (dtplat->non_removable)
1537 priv->non_removable = 1;
1539 priv->non_removable = 0;
1541 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1542 struct udevice *gpiodev;
1544 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
1548 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1549 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1550 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1558 priv->flags = data->flags;
1562 * Because lack of clk driver, if SDHC clk is not enabled,
1563 * need to enable it first before this driver is invoked.
1565 * we use MXC_ESDHC_CLK to get clk freq.
1566 * If one would like to make this function work,
1567 * the aliases should be provided in dts as this:
1575 * Then if your board only supports mmc2 and mmc3, but we can
1576 * correctly get the seq as 2 and 3, then let mxc_get_clock
1580 init_clk_usdhc(dev_seq(dev));
1582 #if CONFIG_IS_ENABLED(CLK)
1583 /* Assigned clock already set clock */
1584 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1586 printf("Failed to get per_clk\n");
1589 ret = clk_enable(&priv->per_clk);
1591 printf("Failed to enable per_clk\n");
1595 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1597 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
1598 if (priv->sdhc_clk <= 0) {
1599 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1604 ret = fsl_esdhc_init(priv, plat);
1606 dev_err(dev, "fsl_esdhc_init failure\n");
1610 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1611 ret = mmc_of_parse(dev, &plat->cfg);
1617 mmc->cfg = &plat->cfg;
1619 #if !CONFIG_IS_ENABLED(BLK)
1622 /* Setup dsr related values */
1624 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1625 /* Setup the universal parts of the block interface just once */
1626 bdesc = mmc_get_blk_desc(mmc);
1627 bdesc->if_type = IF_TYPE_MMC;
1628 bdesc->removable = 1;
1629 bdesc->devnum = mmc_get_next_devnum();
1630 bdesc->block_read = mmc_bread;
1631 bdesc->block_write = mmc_bwrite;
1632 bdesc->block_erase = mmc_berase;
1634 /* setup initial part type */
1635 bdesc->part_type = mmc->cfg->part_type;
1641 return esdhc_init_common(priv, mmc);
1644 #if CONFIG_IS_ENABLED(DM_MMC)
1645 static int fsl_esdhc_get_cd(struct udevice *dev)
1647 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1649 return esdhc_getcd_common(priv);
1652 static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1653 struct mmc_data *data)
1655 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1656 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1658 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1661 static int fsl_esdhc_set_ios(struct udevice *dev)
1663 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1664 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1666 return esdhc_set_ios_common(priv, &plat->mmc);
1669 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1670 static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1672 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1673 struct fsl_esdhc *regs = priv->esdhc_regs;
1676 m = esdhc_read32(®s->mixctrl);
1677 m |= MIX_CTRL_HS400_ES;
1678 esdhc_write32(®s->mixctrl, m);
1684 static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1689 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1690 struct fsl_esdhc *regs = priv->esdhc_regs;
1692 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp,
1693 !!(tmp & PRSSTAT_DAT0) == !!state,
1698 static const struct dm_mmc_ops fsl_esdhc_ops = {
1699 .get_cd = fsl_esdhc_get_cd,
1700 .send_cmd = fsl_esdhc_send_cmd,
1701 .set_ios = fsl_esdhc_set_ios,
1702 #ifdef MMC_SUPPORTS_TUNING
1703 .execute_tuning = fsl_esdhc_execute_tuning,
1705 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1706 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1708 .wait_dat0 = fsl_esdhc_wait_dat0,
1712 static struct esdhc_soc_data usdhc_imx7d_data = {
1713 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1714 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1718 static struct esdhc_soc_data usdhc_imx8qm_data = {
1719 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1720 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1721 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1724 static const struct udevice_id fsl_esdhc_ids[] = {
1725 { .compatible = "fsl,imx51-esdhc", },
1726 { .compatible = "fsl,imx53-esdhc", },
1727 { .compatible = "fsl,imx6ul-usdhc", },
1728 { .compatible = "fsl,imx6sx-usdhc", },
1729 { .compatible = "fsl,imx6sl-usdhc", },
1730 { .compatible = "fsl,imx6q-usdhc", },
1731 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1732 { .compatible = "fsl,imx7ulp-usdhc", },
1733 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1734 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1735 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1736 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1737 { .compatible = "fsl,imxrt-usdhc", },
1738 { .compatible = "fsl,esdhc", },
1742 #if CONFIG_IS_ENABLED(BLK)
1743 static int fsl_esdhc_bind(struct udevice *dev)
1745 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1747 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1751 U_BOOT_DRIVER(fsl_esdhc) = {
1752 .name = "fsl_esdhc",
1754 .of_match = fsl_esdhc_ids,
1755 .of_to_plat = fsl_esdhc_of_to_plat,
1756 .ops = &fsl_esdhc_ops,
1757 #if CONFIG_IS_ENABLED(BLK)
1758 .bind = fsl_esdhc_bind,
1760 .probe = fsl_esdhc_probe,
1761 .plat_auto = sizeof(struct fsl_esdhc_plat),
1762 .priv_auto = sizeof(struct fsl_esdhc_priv),
1765 DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)