1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
4 * Copyright 2019 NXP Semiconductors
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 DECLARE_GLOBAL_DATA_PTR;
48 #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
50 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
51 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
52 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
54 #define MAX_TUNING_LOOP 40
55 #define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
58 uint dsaddr; /* SDMA system address register */
59 uint blkattr; /* Block attributes register */
60 uint cmdarg; /* Command argument register */
61 uint xfertyp; /* Transfer type register */
62 uint cmdrsp0; /* Command response 0 register */
63 uint cmdrsp1; /* Command response 1 register */
64 uint cmdrsp2; /* Command response 2 register */
65 uint cmdrsp3; /* Command response 3 register */
66 uint datport; /* Buffer data port register */
67 uint prsstat; /* Present state register */
68 uint proctl; /* Protocol control register */
69 uint sysctl; /* System Control Register */
70 uint irqstat; /* Interrupt status register */
71 uint irqstaten; /* Interrupt status enable register */
72 uint irqsigen; /* Interrupt signal enable register */
73 uint autoc12err; /* Auto CMD error status register */
74 uint hostcapblt; /* Host controller capabilities register */
75 uint wml; /* Watermark level register */
76 uint mixctrl; /* For USDHC */
77 char reserved1[4]; /* reserved */
78 uint fevt; /* Force event register */
79 uint admaes; /* ADMA error status register */
80 uint adsaddr; /* ADMA system address register */
84 uint clktunectrlstatus;
92 uint tuning_ctrl; /* on i.MX6/7/8/RT */
94 uint hostver; /* Host controller version register */
95 char reserved6[4]; /* reserved */
96 uint dmaerraddr; /* DMA error address register */
97 char reserved7[4]; /* reserved */
98 uint dmaerrattr; /* DMA error attribute register */
99 char reserved8[4]; /* reserved */
100 uint hostcapblt2; /* Host controller capabilities register 2 */
101 char reserved9[8]; /* reserved */
102 uint tcr; /* Tuning control register */
103 char reserved10[28]; /* reserved */
104 uint sddirctl; /* SD direction control register */
105 char reserved11[712];/* reserved */
106 uint scr; /* eSDHC control register */
109 struct fsl_esdhc_plat {
110 #if CONFIG_IS_ENABLED(OF_PLATDATA)
111 /* Put this first since driver model will copy the data here */
112 struct dtd_fsl_esdhc dtplat;
115 struct mmc_config cfg;
119 struct esdhc_soc_data {
124 * struct fsl_esdhc_priv
126 * @esdhc_regs: registers of the sdhc controller
127 * @sdhc_clk: Current clk of the sdhc controller
128 * @bus_width: bus width, 1bit, 4bit or 8bit
131 * Following is used when Driver Model is enabled for MMC
132 * @dev: pointer for the device
133 * @non_removable: 0: removable; 1: non-removable
134 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
135 * @wp_enable: 1: enable checking wp; 0: no check
136 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
137 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
138 * @caps: controller capabilities
139 * @tuning_step: tuning step setting in tuning_ctrl register
140 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
141 * @strobe_dll_delay_target: settings in strobe_dllctrl
142 * @signal_voltage: indicating the current voltage
143 * @cd_gpio: gpio for card detection
144 * @wp_gpio: gpio for write protection
146 struct fsl_esdhc_priv {
147 struct fsl_esdhc *esdhc_regs;
148 unsigned int sdhc_clk;
152 unsigned int bus_width;
153 #if !CONFIG_IS_ENABLED(BLK)
164 u32 tuning_start_tap;
165 u32 strobe_dll_delay_target;
167 #if CONFIG_IS_ENABLED(DM_REGULATOR)
168 struct udevice *vqmmc_dev;
169 struct udevice *vmmc_dev;
171 #if CONFIG_IS_ENABLED(DM_GPIO)
172 struct gpio_desc cd_gpio;
173 struct gpio_desc wp_gpio;
177 /* Return the XFERTYP flags for a given command and data packet */
178 static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
183 xfertyp |= XFERTYP_DPSEL;
184 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
185 xfertyp |= XFERTYP_DMAEN;
187 if (data->blocks > 1) {
188 xfertyp |= XFERTYP_MSBSEL;
189 xfertyp |= XFERTYP_BCEN;
190 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
191 xfertyp |= XFERTYP_AC12EN;
195 if (data->flags & MMC_DATA_READ)
196 xfertyp |= XFERTYP_DTDSEL;
199 if (cmd->resp_type & MMC_RSP_CRC)
200 xfertyp |= XFERTYP_CCCEN;
201 if (cmd->resp_type & MMC_RSP_OPCODE)
202 xfertyp |= XFERTYP_CICEN;
203 if (cmd->resp_type & MMC_RSP_136)
204 xfertyp |= XFERTYP_RSPTYP_136;
205 else if (cmd->resp_type & MMC_RSP_BUSY)
206 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
207 else if (cmd->resp_type & MMC_RSP_PRESENT)
208 xfertyp |= XFERTYP_RSPTYP_48;
210 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
211 xfertyp |= XFERTYP_CMDTYP_ABORT;
213 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
216 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
218 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
220 static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
221 struct mmc_data *data)
223 struct fsl_esdhc *regs = priv->esdhc_regs;
231 if (data->flags & MMC_DATA_READ) {
232 blocks = data->blocks;
235 start = get_timer(0);
236 size = data->blocksize;
237 irqstat = esdhc_read32(®s->irqstat);
238 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) {
239 if (get_timer(start) > PIO_TIMEOUT) {
240 printf("\nData Read Failed in PIO Mode.");
244 while (size && (!(irqstat & IRQSTAT_TC))) {
245 udelay(100); /* Wait before last byte transfer complete */
246 irqstat = esdhc_read32(®s->irqstat);
247 databuf = in_le32(®s->datport);
248 *((uint *)buffer) = databuf;
255 blocks = data->blocks;
256 buffer = (char *)data->src;
258 start = get_timer(0);
259 size = data->blocksize;
260 irqstat = esdhc_read32(®s->irqstat);
261 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) {
262 if (get_timer(start) > PIO_TIMEOUT) {
263 printf("\nData Write Failed in PIO Mode.");
267 while (size && (!(irqstat & IRQSTAT_TC))) {
268 udelay(100); /* Wait before last byte transfer complete */
269 databuf = *((uint *)buffer);
272 irqstat = esdhc_read32(®s->irqstat);
273 out_le32(®s->datport, databuf);
281 static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
282 struct mmc_data *data)
285 struct fsl_esdhc *regs = priv->esdhc_regs;
286 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
291 wml_value = data->blocksize/4;
293 if (data->flags & MMC_DATA_READ) {
294 if (wml_value > WML_RD_WML_MAX)
295 wml_value = WML_RD_WML_MAX_VAL;
297 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
298 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
299 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
300 addr = virt_to_phys((void *)(data->dest));
301 if (upper_32_bits(addr))
302 printf("Error found for upper 32 bits\n");
304 esdhc_write32(®s->dsaddr, lower_32_bits(addr));
306 esdhc_write32(®s->dsaddr, (u32)data->dest);
310 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
311 flush_dcache_range((ulong)data->src,
312 (ulong)data->src+data->blocks
315 if (wml_value > WML_WR_WML_MAX)
316 wml_value = WML_WR_WML_MAX_VAL;
317 if (priv->wp_enable) {
318 if ((esdhc_read32(®s->prsstat) &
319 PRSSTAT_WPSPL) == 0) {
320 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
324 #if CONFIG_IS_ENABLED(DM_GPIO)
325 if (dm_gpio_is_valid(&priv->wp_gpio) &&
326 dm_gpio_get_value(&priv->wp_gpio)) {
327 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
333 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
335 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
336 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
337 addr = virt_to_phys((void *)(data->src));
338 if (upper_32_bits(addr))
339 printf("Error found for upper 32 bits\n");
341 esdhc_write32(®s->dsaddr, lower_32_bits(addr));
343 esdhc_write32(®s->dsaddr, (u32)data->src);
348 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
350 /* Calculate the timeout period for data transactions */
352 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
353 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
354 * So, Number of SD Clock cycles for 0.25sec should be minimum
355 * (SD Clock/sec * 0.25 sec) SD Clock cycles
356 * = (mmc->clock * 1/4) SD Clock cycles
358 * => (2^(timeout+13)) >= mmc->clock * 1/4
359 * Taking log2 both the sides
360 * => timeout + 13 >= log2(mmc->clock/4)
361 * Rounding up to next power of 2
362 * => timeout + 13 = log2(mmc->clock/4) + 1
363 * => timeout + 13 = fls(mmc->clock/4)
365 * However, the MMC spec "It is strongly recommended for hosts to
366 * implement more than 500ms timeout value even if the card
367 * indicates the 250ms maximum busy length." Even the previous
368 * value of 300ms is known to be insufficient for some cards.
370 * => timeout + 13 = fls(mmc->clock/2)
372 timeout = fls(mmc->clock/2);
381 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
382 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
386 #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
389 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
394 static void check_and_invalidate_dcache_range
395 (struct mmc_cmd *cmd,
396 struct mmc_data *data) {
399 unsigned size = roundup(ARCH_DMA_MINALIGN,
400 data->blocks*data->blocksize);
401 #if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
404 addr = virt_to_phys((void *)(data->dest));
405 if (upper_32_bits(addr))
406 printf("Error found for upper 32 bits\n");
408 start = lower_32_bits(addr);
410 start = (unsigned)data->dest;
413 invalidate_dcache_range(start, end);
416 #ifdef CONFIG_MCF5441x
418 * Swaps 32-bit words to little-endian byte order.
420 static inline void sd_swap_dma_buff(struct mmc_data *data)
422 int i, size = data->blocksize >> 2;
423 u32 *buffer = (u32 *)data->dest;
426 while (data->blocks--) {
427 for (i = 0; i < size; i++) {
428 sw = __sw32(*buffer);
436 * Sends a command out on the bus. Takes the mmc pointer,
437 * a command pointer, and an optional data pointer.
439 static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
440 struct mmc_cmd *cmd, struct mmc_data *data)
445 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
446 struct fsl_esdhc *regs = priv->esdhc_regs;
449 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
450 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
454 esdhc_write32(®s->irqstat, -1);
458 /* Wait for the bus to be idle */
459 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
460 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
463 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
466 /* Set up for a data transfer if we have one */
468 err = esdhc_setup_data(priv, mmc, data);
472 if (data->flags & MMC_DATA_READ)
473 check_and_invalidate_dcache_range(cmd, data);
476 /* Figure out the transfer arguments */
477 xfertyp = esdhc_xfertyp(cmd, data);
480 esdhc_write32(®s->irqsigen, 0);
482 /* Send the command */
483 esdhc_write32(®s->cmdarg, cmd->cmdarg);
484 #if defined(CONFIG_FSL_USDHC)
485 esdhc_write32(®s->mixctrl,
486 (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
487 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
488 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
490 esdhc_write32(®s->xfertyp, xfertyp);
493 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
494 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
497 /* Wait for the command to complete */
498 start = get_timer(0);
499 while (!(esdhc_read32(®s->irqstat) & flags)) {
500 if (get_timer(start) > 1000) {
506 irqstat = esdhc_read32(®s->irqstat);
508 if (irqstat & CMD_ERR) {
513 if (irqstat & IRQSTAT_CTOE) {
518 /* Switch voltage to 1.8V if CMD11 succeeded */
519 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
520 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
522 printf("Run CMD11 1.8V switch\n");
523 /* Sleep for 5 ms - max time for card to switch to 1.8V */
527 /* Workaround for ESDHC errata ENGcm03648 */
528 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
531 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
532 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
539 printf("Timeout waiting for DAT0 to go high!\n");
545 /* Copy the response to the response buffer */
546 if (cmd->resp_type & MMC_RSP_136) {
547 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
549 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
550 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
551 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
552 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
553 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
554 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
555 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
556 cmd->response[3] = (cmdrsp0 << 8);
558 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
560 /* Wait until all of the blocks are transferred */
562 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
563 esdhc_pio_read_write(priv, data);
565 flags = DATA_COMPLETE;
566 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
567 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
572 irqstat = esdhc_read32(®s->irqstat);
574 if (irqstat & IRQSTAT_DTOE) {
579 if (irqstat & DATA_ERR) {
583 } while ((irqstat & flags) != flags);
586 * Need invalidate the dcache here again to avoid any
587 * cache-fill during the DMA operations such as the
588 * speculative pre-fetching etc.
590 if (data->flags & MMC_DATA_READ) {
591 check_and_invalidate_dcache_range(cmd, data);
592 #ifdef CONFIG_MCF5441x
593 sd_swap_dma_buff(data);
600 /* Reset CMD and DATA portions on error */
602 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
604 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC)
608 esdhc_write32(®s->sysctl,
609 esdhc_read32(®s->sysctl) |
611 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD))
615 /* If this was CMD11, then notify that power cycle is needed */
616 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
617 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
620 esdhc_write32(®s->irqstat, -1);
625 static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
627 struct fsl_esdhc *regs = priv->esdhc_regs;
633 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
634 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
641 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
642 int sdhc_clk = priv->sdhc_clk;
645 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
648 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
654 clk = (pre_div << 8) | (div << 4);
656 #ifdef CONFIG_FSL_USDHC
657 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN);
659 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
662 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
664 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
666 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
668 #ifdef CONFIG_FSL_USDHC
669 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
671 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
677 #ifdef MMC_SUPPORTS_TUNING
678 static int esdhc_change_pinstate(struct udevice *dev)
680 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
683 switch (priv->mode) {
686 ret = pinctrl_select_state(dev, "state_100mhz");
692 ret = pinctrl_select_state(dev, "state_200mhz");
695 ret = pinctrl_select_state(dev, "default");
700 printf("%s %d error\n", __func__, priv->mode);
705 static void esdhc_reset_tuning(struct mmc *mmc)
707 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
708 struct fsl_esdhc *regs = priv->esdhc_regs;
710 if (priv->flags & ESDHC_FLAG_USDHC) {
711 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
712 esdhc_clrbits32(®s->autoc12err,
713 MIX_CTRL_SMPCLK_SEL |
719 static void esdhc_set_strobe_dll(struct mmc *mmc)
721 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
722 struct fsl_esdhc *regs = priv->esdhc_regs;
725 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
726 esdhc_write32(®s->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
729 * enable strobe dll ctrl and adjust the delay target
730 * for the uSDHC loopback read clock
732 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
733 (priv->strobe_dll_delay_target <<
734 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
735 esdhc_write32(®s->strobe_dllctrl, val);
736 /* wait 1us to make sure strobe dll status register stable */
738 val = esdhc_read32(®s->strobe_dllstat);
739 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
740 pr_warn("HS400 strobe DLL status REF not lock!\n");
741 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
742 pr_warn("HS400 strobe DLL status SLV not lock!\n");
746 static int esdhc_set_timing(struct mmc *mmc)
748 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
749 struct fsl_esdhc *regs = priv->esdhc_regs;
752 mixctrl = esdhc_read32(®s->mixctrl);
753 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
755 switch (mmc->selected_mode) {
757 esdhc_reset_tuning(mmc);
758 esdhc_write32(®s->mixctrl, mixctrl);
762 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
763 esdhc_write32(®s->mixctrl, mixctrl);
773 esdhc_write32(®s->mixctrl, mixctrl);
777 mixctrl |= MIX_CTRL_DDREN;
778 esdhc_write32(®s->mixctrl, mixctrl);
781 printf("Not supported %d\n", mmc->selected_mode);
785 priv->mode = mmc->selected_mode;
787 return esdhc_change_pinstate(mmc->dev);
790 static int esdhc_set_voltage(struct mmc *mmc)
792 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
793 struct fsl_esdhc *regs = priv->esdhc_regs;
794 #if CONFIG_IS_ENABLED(DM_REGULATOR)
798 priv->signal_voltage = mmc->signal_voltage;
799 switch (mmc->signal_voltage) {
800 case MMC_SIGNAL_VOLTAGE_330:
801 if (priv->vs18_enable)
803 #if CONFIG_IS_ENABLED(DM_REGULATOR)
804 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
805 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
807 printf("Setting to 3.3V error");
815 esdhc_clrbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
816 if (!(esdhc_read32(®s->vendorspec) &
817 ESDHC_VENDORSPEC_VSELECT))
821 case MMC_SIGNAL_VOLTAGE_180:
822 #if CONFIG_IS_ENABLED(DM_REGULATOR)
823 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
824 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
826 printf("Setting to 1.8V error");
831 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
832 if (esdhc_read32(®s->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
836 case MMC_SIGNAL_VOLTAGE_120:
843 static void esdhc_stop_tuning(struct mmc *mmc)
847 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
849 cmd.resp_type = MMC_RSP_R1b;
851 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
854 static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
856 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
857 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
858 struct fsl_esdhc *regs = priv->esdhc_regs;
859 struct mmc *mmc = &plat->mmc;
860 u32 irqstaten = esdhc_read32(®s->irqstaten);
861 u32 irqsigen = esdhc_read32(®s->irqsigen);
862 int i, ret = -ETIMEDOUT;
865 /* clock tuning is not needed for upto 52MHz */
866 if (mmc->clock <= 52000000)
869 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
870 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
871 val = esdhc_read32(®s->autoc12err);
872 mixctrl = esdhc_read32(®s->mixctrl);
873 val &= ~MIX_CTRL_SMPCLK_SEL;
874 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
876 val |= MIX_CTRL_EXE_TUNE;
877 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
879 esdhc_write32(®s->autoc12err, val);
880 esdhc_write32(®s->mixctrl, mixctrl);
883 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
884 mixctrl = esdhc_read32(®s->mixctrl);
885 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
886 esdhc_write32(®s->mixctrl, mixctrl);
888 esdhc_write32(®s->irqstaten, IRQSTATEN_BRR);
889 esdhc_write32(®s->irqsigen, IRQSTATEN_BRR);
892 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
893 * of loops reaches 40 times.
895 for (i = 0; i < MAX_TUNING_LOOP; i++) {
898 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
899 if (mmc->bus_width == 8)
900 esdhc_write32(®s->blkattr, 0x7080);
901 else if (mmc->bus_width == 4)
902 esdhc_write32(®s->blkattr, 0x7040);
904 esdhc_write32(®s->blkattr, 0x7040);
907 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
908 val = esdhc_read32(®s->mixctrl);
909 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
910 esdhc_write32(®s->mixctrl, val);
912 /* We are using STD tuning, no need to check return value */
913 mmc_send_tuning(mmc, opcode, NULL);
915 ctrl = esdhc_read32(®s->autoc12err);
916 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
917 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
923 esdhc_write32(®s->irqstaten, irqstaten);
924 esdhc_write32(®s->irqsigen, irqsigen);
926 esdhc_stop_tuning(mmc);
932 static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
934 struct fsl_esdhc *regs = priv->esdhc_regs;
935 int ret __maybe_unused;
938 #ifdef MMC_SUPPORTS_TUNING
940 * call esdhc_set_timing() before update the clock rate,
941 * This is because current we support DDR and SDR mode,
942 * Once the DDR_EN bit is set, the card clock will be
943 * divide by 2 automatically. So need to do this before
944 * setting clock rate.
946 if (priv->mode != mmc->selected_mode) {
947 ret = esdhc_set_timing(mmc);
949 printf("esdhc_set_timing error %d\n", ret);
955 /* Set the clock speed */
957 if (clock < mmc->cfg->f_min)
958 clock = mmc->cfg->f_min;
960 if (priv->clock != clock)
961 set_sysctl(priv, mmc, clock);
963 #ifdef MMC_SUPPORTS_TUNING
964 if (mmc->clk_disable) {
965 #ifdef CONFIG_FSL_USDHC
966 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN);
968 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
971 #ifdef CONFIG_FSL_USDHC
972 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN |
975 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
980 * For HS400/HS400ES mode, make sure set the strobe dll in the
981 * target clock rate. So call esdhc_set_strobe_dll() after the
984 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
985 esdhc_set_strobe_dll(mmc);
987 if (priv->signal_voltage != mmc->signal_voltage) {
988 ret = esdhc_set_voltage(mmc);
990 if (ret != -ENOTSUPP)
991 printf("esdhc_set_voltage error %d\n", ret);
997 /* Set the bus width */
998 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
1000 if (mmc->bus_width == 4)
1001 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
1002 else if (mmc->bus_width == 8)
1003 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
1008 static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1010 struct fsl_esdhc *regs = priv->esdhc_regs;
1013 /* Reset the entire host controller */
1014 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
1016 /* Wait until the controller is available */
1017 start = get_timer(0);
1018 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
1019 if (get_timer(start) > 1000)
1023 #if defined(CONFIG_FSL_USDHC)
1024 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1025 esdhc_write32(®s->mmcboot, 0x0);
1026 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1027 esdhc_write32(®s->mixctrl, 0x0);
1028 esdhc_write32(®s->clktunectrlstatus, 0x0);
1030 /* Put VEND_SPEC to default value */
1031 if (priv->vs18_enable)
1032 esdhc_write32(®s->vendorspec, (VENDORSPEC_INIT |
1033 ESDHC_VENDORSPEC_VSELECT));
1035 esdhc_write32(®s->vendorspec, VENDORSPEC_INIT);
1037 /* Disable DLL_CTRL delay line */
1038 esdhc_write32(®s->dllctrl, 0x0);
1042 /* Enable cache snooping */
1043 esdhc_write32(®s->scr, 0x00000040);
1046 #ifndef CONFIG_FSL_USDHC
1047 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1049 esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1052 /* Set the initial clock speed */
1053 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1055 /* Disable the BRR and BWR bits in IRQSTAT */
1056 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1058 #ifdef CONFIG_MCF5441x
1059 esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD);
1061 /* Put the PROCTL reg back to the default */
1062 esdhc_write32(®s->proctl, PROCTL_INIT);
1065 /* Set timout to the maximum value */
1066 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1071 static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1073 struct fsl_esdhc *regs = priv->esdhc_regs;
1076 #ifdef CONFIG_ESDHC_DETECT_QUIRK
1077 if (CONFIG_ESDHC_DETECT_QUIRK)
1081 #if CONFIG_IS_ENABLED(DM_MMC)
1082 if (priv->non_removable)
1085 if (priv->broken_cd)
1087 #if CONFIG_IS_ENABLED(DM_GPIO)
1088 if (dm_gpio_is_valid(&priv->cd_gpio))
1089 return dm_gpio_get_value(&priv->cd_gpio);
1093 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
1099 static int esdhc_reset(struct fsl_esdhc *regs)
1103 /* reset the controller */
1104 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
1106 /* hardware clears the bit when it is done */
1107 start = get_timer(0);
1108 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
1109 if (get_timer(start) > 100) {
1110 printf("MMC/SD: Reset never completed.\n");
1118 #if !CONFIG_IS_ENABLED(DM_MMC)
1119 static int esdhc_getcd(struct mmc *mmc)
1121 struct fsl_esdhc_priv *priv = mmc->priv;
1123 return esdhc_getcd_common(priv);
1126 static int esdhc_init(struct mmc *mmc)
1128 struct fsl_esdhc_priv *priv = mmc->priv;
1130 return esdhc_init_common(priv, mmc);
1133 static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1134 struct mmc_data *data)
1136 struct fsl_esdhc_priv *priv = mmc->priv;
1138 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1141 static int esdhc_set_ios(struct mmc *mmc)
1143 struct fsl_esdhc_priv *priv = mmc->priv;
1145 return esdhc_set_ios_common(priv, mmc);
1148 static const struct mmc_ops esdhc_ops = {
1149 .getcd = esdhc_getcd,
1151 .send_cmd = esdhc_send_cmd,
1152 .set_ios = esdhc_set_ios,
1156 static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1157 struct fsl_esdhc_plat *plat)
1159 struct mmc_config *cfg;
1160 struct fsl_esdhc *regs;
1161 u32 caps, voltage_caps;
1167 regs = priv->esdhc_regs;
1169 /* First reset the eSDHC controller */
1170 ret = esdhc_reset(regs);
1174 #ifdef CONFIG_MCF5441x
1175 /* ColdFire, using SDHC_DATA[3] for card detection */
1176 esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD);
1179 #ifndef CONFIG_FSL_USDHC
1180 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1181 | SYSCTL_IPGEN | SYSCTL_CKEN);
1182 /* Clearing tuning bits in case ROM has set it already */
1183 esdhc_write32(®s->mixctrl, 0);
1184 esdhc_write32(®s->autoc12err, 0);
1185 esdhc_write32(®s->clktunectrlstatus, 0);
1187 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN |
1188 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1191 if (priv->vs18_enable)
1192 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1194 esdhc_write32(®s->irqstaten, SDHCI_IRQ_EN_BITS);
1196 #ifndef CONFIG_DM_MMC
1197 memset(cfg, '\0', sizeof(*cfg));
1201 caps = esdhc_read32(®s->hostcapblt);
1203 #ifdef CONFIG_MCF5441x
1205 * MCF5441x RM declares in more points that sdhc clock speed must
1206 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1207 * from host capabilities.
1209 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1212 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1213 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1214 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1217 /* T4240 host controller capabilities register should have VS33 bit */
1218 #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1219 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1222 if (caps & ESDHC_HOSTCAPBLT_VS18)
1223 voltage_caps |= MMC_VDD_165_195;
1224 if (caps & ESDHC_HOSTCAPBLT_VS30)
1225 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1226 if (caps & ESDHC_HOSTCAPBLT_VS33)
1227 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1229 cfg->name = "FSL_SDHC";
1230 #if !CONFIG_IS_ENABLED(DM_MMC)
1231 cfg->ops = &esdhc_ops;
1233 #ifdef CONFIG_SYS_SD_VOLTAGE
1234 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1236 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1238 if ((cfg->voltages & voltage_caps) == 0) {
1239 printf("voltage not supported by controller\n");
1243 if (priv->bus_width == 8)
1244 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1245 else if (priv->bus_width == 4)
1246 cfg->host_caps = MMC_MODE_4BIT;
1248 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1249 #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1250 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1253 if (priv->bus_width > 0) {
1254 if (priv->bus_width < 8)
1255 cfg->host_caps &= ~MMC_MODE_8BIT;
1256 if (priv->bus_width < 4)
1257 cfg->host_caps &= ~MMC_MODE_4BIT;
1260 if (caps & ESDHC_HOSTCAPBLT_HSS)
1261 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1263 #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1264 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1265 cfg->host_caps &= ~MMC_MODE_8BIT;
1268 cfg->host_caps |= priv->caps;
1270 cfg->f_min = 400000;
1271 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1273 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1275 esdhc_write32(®s->dllctrl, 0);
1276 if (priv->flags & ESDHC_FLAG_USDHC) {
1277 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1278 u32 val = esdhc_read32(®s->tuning_ctrl);
1280 val |= ESDHC_STD_TUNING_EN;
1281 val &= ~ESDHC_TUNING_START_TAP_MASK;
1282 val |= priv->tuning_start_tap;
1283 val &= ~ESDHC_TUNING_STEP_MASK;
1284 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1286 /* Disable the CMD CRC check for tuning, if not, need to
1287 * add some delay after every tuning command, because
1288 * hardware standard tuning logic will directly go to next
1289 * step once it detect the CMD CRC error, will not wait for
1290 * the card side to finally send out the tuning data, trigger
1291 * the buffer read ready interrupt immediately. If usdhc send
1292 * the next tuning command some eMMC card will stuck, can't
1293 * response, block the tuning procedure or the first command
1294 * after the whole tuning procedure always can't get any response.
1296 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1297 esdhc_write32(®s->tuning_ctrl, val);
1304 #if !CONFIG_IS_ENABLED(DM_MMC)
1305 static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1306 struct fsl_esdhc_priv *priv)
1311 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1312 priv->bus_width = cfg->max_bus_width;
1313 priv->sdhc_clk = cfg->sdhc_clk;
1314 priv->wp_enable = cfg->wp_enable;
1315 priv->vs18_enable = cfg->vs18_enable;
1320 int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
1322 struct fsl_esdhc_plat *plat;
1323 struct fsl_esdhc_priv *priv;
1330 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1333 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1339 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1341 debug("%s xlate failure\n", __func__);
1347 ret = fsl_esdhc_init(priv, plat);
1349 debug("%s init failure\n", __func__);
1355 mmc = mmc_create(&plat->cfg, priv);
1364 int fsl_esdhc_mmc_init(struct bd_info *bis)
1366 struct fsl_esdhc_cfg *cfg;
1368 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1369 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1370 cfg->sdhc_clk = gd->arch.sdhc_clk;
1371 return fsl_esdhc_initialize(bis, cfg);
1375 #ifdef CONFIG_OF_LIBFDT
1376 __weak int esdhc_status_fixup(void *blob, const char *compat)
1378 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
1379 if (!hwconfig("esdhc")) {
1380 do_fixup_by_compat(blob, compat, "status", "disabled",
1381 sizeof("disabled"), 1);
1388 void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
1390 const char *compat = "fsl,esdhc";
1392 if (esdhc_status_fixup(blob, compat))
1395 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1396 gd->arch.sdhc_clk, 1);
1400 #if CONFIG_IS_ENABLED(DM_MMC)
1401 #include <asm/arch/clock.h>
1402 __weak void init_clk_usdhc(u32 index)
1406 static int fsl_esdhc_of_to_plat(struct udevice *dev)
1408 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1409 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1410 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1411 struct udevice *vqmmc_dev;
1414 const void *fdt = gd->fdt_blob;
1415 int node = dev_of_offset(dev);
1420 addr = dev_read_addr(dev);
1421 if (addr == FDT_ADDR_T_NONE)
1423 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1427 val = dev_read_u32_default(dev, "bus-width", -1);
1429 priv->bus_width = 8;
1431 priv->bus_width = 4;
1433 priv->bus_width = 1;
1435 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1436 priv->tuning_step = val;
1437 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1438 ESDHC_TUNING_START_TAP_DEFAULT);
1439 priv->tuning_start_tap = val;
1440 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1441 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1442 priv->strobe_dll_delay_target = val;
1444 if (dev_read_bool(dev, "broken-cd"))
1445 priv->broken_cd = 1;
1447 if (dev_read_bool(dev, "non-removable")) {
1448 priv->non_removable = 1;
1450 priv->non_removable = 0;
1451 #if CONFIG_IS_ENABLED(DM_GPIO)
1452 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1457 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1458 priv->wp_enable = 1;
1460 priv->wp_enable = 0;
1461 #if CONFIG_IS_ENABLED(DM_GPIO)
1462 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1467 priv->vs18_enable = 0;
1469 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1471 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1472 * otherwise, emmc will work abnormally.
1474 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1476 dev_dbg(dev, "no vqmmc-supply\n");
1478 priv->vqmmc_dev = vqmmc_dev;
1479 ret = regulator_set_enable(vqmmc_dev, true);
1481 dev_err(dev, "fail to enable vqmmc-supply\n");
1485 if (regulator_get_value(vqmmc_dev) == 1800000)
1486 priv->vs18_enable = 1;
1493 static int fsl_esdhc_probe(struct udevice *dev)
1495 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1496 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1497 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1498 struct esdhc_soc_data *data =
1499 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1501 #if !CONFIG_IS_ENABLED(BLK)
1502 struct blk_desc *bdesc;
1506 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1507 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1510 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1511 val = plat->dtplat.bus_width;
1513 priv->bus_width = 8;
1515 priv->bus_width = 4;
1517 priv->bus_width = 1;
1519 if (dtplat->non_removable)
1520 priv->non_removable = 1;
1522 priv->non_removable = 0;
1524 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1525 struct udevice *gpiodev;
1527 ret = device_get_by_driver_info_idx(dtplat->cd_gpios->idx,
1532 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1533 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1534 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1542 priv->flags = data->flags;
1546 * Because lack of clk driver, if SDHC clk is not enabled,
1547 * need to enable it first before this driver is invoked.
1549 * we use MXC_ESDHC_CLK to get clk freq.
1550 * If one would like to make this function work,
1551 * the aliases should be provided in dts as this:
1559 * Then if your board only supports mmc2 and mmc3, but we can
1560 * correctly get the seq as 2 and 3, then let mxc_get_clock
1564 init_clk_usdhc(dev_seq(dev));
1566 #if CONFIG_IS_ENABLED(CLK)
1567 /* Assigned clock already set clock */
1568 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1570 printf("Failed to get per_clk\n");
1573 ret = clk_enable(&priv->per_clk);
1575 printf("Failed to enable per_clk\n");
1579 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1581 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
1582 if (priv->sdhc_clk <= 0) {
1583 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1588 ret = fsl_esdhc_init(priv, plat);
1590 dev_err(dev, "fsl_esdhc_init failure\n");
1594 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1595 ret = mmc_of_parse(dev, &plat->cfg);
1601 mmc->cfg = &plat->cfg;
1603 #if !CONFIG_IS_ENABLED(BLK)
1606 /* Setup dsr related values */
1608 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1609 /* Setup the universal parts of the block interface just once */
1610 bdesc = mmc_get_blk_desc(mmc);
1611 bdesc->if_type = IF_TYPE_MMC;
1612 bdesc->removable = 1;
1613 bdesc->devnum = mmc_get_next_devnum();
1614 bdesc->block_read = mmc_bread;
1615 bdesc->block_write = mmc_bwrite;
1616 bdesc->block_erase = mmc_berase;
1618 /* setup initial part type */
1619 bdesc->part_type = mmc->cfg->part_type;
1625 return esdhc_init_common(priv, mmc);
1628 #if CONFIG_IS_ENABLED(DM_MMC)
1629 static int fsl_esdhc_get_cd(struct udevice *dev)
1631 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1633 return esdhc_getcd_common(priv);
1636 static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1637 struct mmc_data *data)
1639 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1640 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1642 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1645 static int fsl_esdhc_set_ios(struct udevice *dev)
1647 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1648 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1650 return esdhc_set_ios_common(priv, &plat->mmc);
1653 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1654 static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1656 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1657 struct fsl_esdhc *regs = priv->esdhc_regs;
1660 m = esdhc_read32(®s->mixctrl);
1661 m |= MIX_CTRL_HS400_ES;
1662 esdhc_write32(®s->mixctrl, m);
1668 static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1673 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1674 struct fsl_esdhc *regs = priv->esdhc_regs;
1676 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp,
1677 !!(tmp & PRSSTAT_DAT0) == !!state,
1682 static const struct dm_mmc_ops fsl_esdhc_ops = {
1683 .get_cd = fsl_esdhc_get_cd,
1684 .send_cmd = fsl_esdhc_send_cmd,
1685 .set_ios = fsl_esdhc_set_ios,
1686 #ifdef MMC_SUPPORTS_TUNING
1687 .execute_tuning = fsl_esdhc_execute_tuning,
1689 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1690 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1692 .wait_dat0 = fsl_esdhc_wait_dat0,
1696 static struct esdhc_soc_data usdhc_imx7d_data = {
1697 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1698 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1702 static struct esdhc_soc_data usdhc_imx8qm_data = {
1703 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1704 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1705 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1708 static const struct udevice_id fsl_esdhc_ids[] = {
1709 { .compatible = "fsl,imx53-esdhc", },
1710 { .compatible = "fsl,imx6ul-usdhc", },
1711 { .compatible = "fsl,imx6sx-usdhc", },
1712 { .compatible = "fsl,imx6sl-usdhc", },
1713 { .compatible = "fsl,imx6q-usdhc", },
1714 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1715 { .compatible = "fsl,imx7ulp-usdhc", },
1716 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1717 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1718 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1719 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1720 { .compatible = "fsl,imxrt-usdhc", },
1721 { .compatible = "fsl,esdhc", },
1725 #if CONFIG_IS_ENABLED(BLK)
1726 static int fsl_esdhc_bind(struct udevice *dev)
1728 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1730 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1734 U_BOOT_DRIVER(fsl_esdhc) = {
1735 .name = "fsl_esdhc",
1737 .of_match = fsl_esdhc_ids,
1738 .of_to_plat = fsl_esdhc_of_to_plat,
1739 .ops = &fsl_esdhc_ops,
1740 #if CONFIG_IS_ENABLED(BLK)
1741 .bind = fsl_esdhc_bind,
1743 .probe = fsl_esdhc_probe,
1744 .plat_auto = sizeof(struct fsl_esdhc_plat),
1745 .priv_auto = sizeof(struct fsl_esdhc_priv),
1748 DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)