1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
4 * Copyright 2019-2021 NXP
7 * Based vaguely on the pxa mmc code:
9 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
21 #include <fsl_esdhc.h>
22 #include <fdt_support.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
27 #include <dm/device_compat.h>
28 #include <linux/bitops.h>
29 #include <linux/delay.h>
30 #include <linux/dma-mapping.h>
33 DECLARE_GLOBAL_DATA_PTR;
36 uint dsaddr; /* SDMA system address register */
37 uint blkattr; /* Block attributes register */
38 uint cmdarg; /* Command argument register */
39 uint xfertyp; /* Transfer type register */
40 uint cmdrsp0; /* Command response 0 register */
41 uint cmdrsp1; /* Command response 1 register */
42 uint cmdrsp2; /* Command response 2 register */
43 uint cmdrsp3; /* Command response 3 register */
44 uint datport; /* Buffer data port register */
45 uint prsstat; /* Present state register */
46 uint proctl; /* Protocol control register */
47 uint sysctl; /* System Control Register */
48 uint irqstat; /* Interrupt status register */
49 uint irqstaten; /* Interrupt status enable register */
50 uint irqsigen; /* Interrupt signal enable register */
51 uint autoc12err; /* Auto CMD error status register */
52 uint hostcapblt; /* Host controller capabilities register */
53 uint wml; /* Watermark level register */
54 char reserved1[8]; /* reserved */
55 uint fevt; /* Force event register */
56 uint admaes; /* ADMA error status register */
57 uint adsaddrl; /* ADMA system address low register */
58 uint adsaddrh; /* ADMA system address high register */
60 uint hostver; /* Host controller version register */
61 char reserved3[4]; /* reserved */
62 uint dmaerraddr; /* DMA error address register */
63 char reserved4[4]; /* reserved */
64 uint dmaerrattr; /* DMA error attribute register */
65 char reserved5[4]; /* reserved */
66 uint hostcapblt2; /* Host controller capabilities register 2 */
67 char reserved6[8]; /* reserved */
68 uint tbctl; /* Tuning block control register */
69 char reserved7[32]; /* reserved */
70 uint sdclkctl; /* SD clock control register */
71 uint sdtimingctl; /* SD timing control register */
72 char reserved8[20]; /* reserved */
73 uint dllcfg0; /* DLL config 0 register */
74 uint dllcfg1; /* DLL config 1 register */
75 char reserved9[8]; /* reserved */
76 uint dllstat0; /* DLL status 0 register */
77 char reserved10[664];/* reserved */
78 uint esdhcctl; /* eSDHC control register */
81 struct fsl_esdhc_plat {
82 struct mmc_config cfg;
87 * struct fsl_esdhc_priv
89 * @esdhc_regs: registers of the sdhc controller
90 * @sdhc_clk: Current clk of the sdhc controller
91 * @bus_width: bus width, 1bit, 4bit or 8bit
94 * Following is used when Driver Model is enabled for MMC
95 * @dev: pointer for the device
96 * @cd_gpio: gpio for card detection
97 * @wp_gpio: gpio for write protection
99 struct fsl_esdhc_priv {
100 struct fsl_esdhc *esdhc_regs;
101 unsigned int sdhc_clk;
102 bool is_sdhc_per_clk;
104 #if !CONFIG_IS_ENABLED(DM_MMC)
108 struct sdhci_adma_desc *adma_desc_table;
112 /* Return the XFERTYP flags for a given command and data packet */
113 static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
118 xfertyp |= XFERTYP_DPSEL;
119 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
120 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
121 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
122 xfertyp |= XFERTYP_DMAEN;
123 if (data->blocks > 1) {
124 xfertyp |= XFERTYP_MSBSEL;
125 xfertyp |= XFERTYP_BCEN;
126 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
127 xfertyp |= XFERTYP_AC12EN;
130 if (data->flags & MMC_DATA_READ)
131 xfertyp |= XFERTYP_DTDSEL;
134 if (cmd->resp_type & MMC_RSP_CRC)
135 xfertyp |= XFERTYP_CCCEN;
136 if (cmd->resp_type & MMC_RSP_OPCODE)
137 xfertyp |= XFERTYP_CICEN;
138 if (cmd->resp_type & MMC_RSP_136)
139 xfertyp |= XFERTYP_RSPTYP_136;
140 else if (cmd->resp_type & MMC_RSP_BUSY)
141 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
142 else if (cmd->resp_type & MMC_RSP_PRESENT)
143 xfertyp |= XFERTYP_RSPTYP_48;
145 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
146 xfertyp |= XFERTYP_CMDTYP_ABORT;
148 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
152 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
154 static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
155 struct mmc_data *data)
157 struct fsl_esdhc *regs = priv->esdhc_regs;
165 if (data->flags & MMC_DATA_READ) {
166 blocks = data->blocks;
169 start = get_timer(0);
170 size = data->blocksize;
171 irqstat = esdhc_read32(®s->irqstat);
172 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) {
173 if (get_timer(start) > PIO_TIMEOUT) {
174 printf("\nData Read Failed in PIO Mode.");
178 while (size && (!(irqstat & IRQSTAT_TC))) {
179 udelay(100); /* Wait before last byte transfer complete */
180 irqstat = esdhc_read32(®s->irqstat);
181 databuf = in_le32(®s->datport);
182 *((uint *)buffer) = databuf;
189 blocks = data->blocks;
190 buffer = (char *)data->src;
192 start = get_timer(0);
193 size = data->blocksize;
194 irqstat = esdhc_read32(®s->irqstat);
195 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) {
196 if (get_timer(start) > PIO_TIMEOUT) {
197 printf("\nData Write Failed in PIO Mode.");
201 while (size && (!(irqstat & IRQSTAT_TC))) {
202 udelay(100); /* Wait before last byte transfer complete */
203 databuf = *((uint *)buffer);
206 irqstat = esdhc_read32(®s->irqstat);
207 out_le32(®s->datport, databuf);
214 static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
215 struct mmc_data *data)
217 struct fsl_esdhc *regs = priv->esdhc_regs;
218 uint wml_value = data->blocksize / 4;
220 if (data->flags & MMC_DATA_READ) {
221 if (wml_value > WML_RD_WML_MAX)
222 wml_value = WML_RD_WML_MAX_VAL;
224 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
226 if (wml_value > WML_WR_WML_MAX)
227 wml_value = WML_WR_WML_MAX_VAL;
229 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
234 static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
236 uint trans_bytes = data->blocksize * data->blocks;
237 struct fsl_esdhc *regs = priv->esdhc_regs;
238 phys_addr_t adma_addr;
241 if (data->flags & MMC_DATA_WRITE)
242 buf = (void *)data->src;
246 priv->dma_addr = dma_map_single(buf, trans_bytes,
247 mmc_get_dma_dir(data));
249 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) &&
250 priv->adma_desc_table) {
251 debug("Using ADMA2\n");
252 /* prefer ADMA2 if it is available */
253 sdhci_prepare_adma_table(priv->adma_desc_table, data,
256 adma_addr = virt_to_phys(priv->adma_desc_table);
257 esdhc_write32(®s->adsaddrl, lower_32_bits(adma_addr));
258 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT))
259 esdhc_write32(®s->adsaddrh, upper_32_bits(adma_addr));
260 esdhc_clrsetbits32(®s->proctl, PROCTL_DMAS_MASK,
263 debug("Using SDMA\n");
264 if (upper_32_bits(priv->dma_addr))
265 printf("Cannot use 64 bit addresses with SDMA\n");
266 esdhc_write32(®s->dsaddr, lower_32_bits(priv->dma_addr));
267 esdhc_clrsetbits32(®s->proctl, PROCTL_DMAS_MASK,
271 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
274 static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
275 struct mmc_data *data)
278 bool is_write = data->flags & MMC_DATA_WRITE;
279 struct fsl_esdhc *regs = priv->esdhc_regs;
281 if (is_write && !(esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL)) {
282 printf("Can not write to locked SD card.\n");
286 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
287 esdhc_setup_watermark_level(priv, data);
289 esdhc_setup_dma(priv, data);
291 /* Calculate the timeout period for data transactions */
293 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
294 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
295 * So, Number of SD Clock cycles for 0.25sec should be minimum
296 * (SD Clock/sec * 0.25 sec) SD Clock cycles
297 * = (mmc->clock * 1/4) SD Clock cycles
299 * => (2^(timeout+13)) >= mmc->clock * 1/4
300 * Taking log2 both the sides
301 * => timeout + 13 >= log2(mmc->clock/4)
302 * Rounding up to next power of 2
303 * => timeout + 13 = log2(mmc->clock/4) + 1
304 * => timeout + 13 = fls(mmc->clock/4)
306 * However, the MMC spec "It is strongly recommended for hosts to
307 * implement more than 500ms timeout value even if the card
308 * indicates the 250ms maximum busy length." Even the previous
309 * value of 300ms is known to be insufficient for some cards.
311 * => timeout + 13 = fls(mmc->clock/2)
313 timeout = fls(mmc->clock/2);
322 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
323 (timeout == 4 || timeout == 8 || timeout == 12))
326 if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
329 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
335 * Sends a command out on the bus. Takes the mmc pointer,
336 * a command pointer, and an optional data pointer.
338 static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
339 struct mmc_cmd *cmd, struct mmc_data *data)
344 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
345 struct fsl_esdhc *regs = priv->esdhc_regs;
348 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
349 cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
352 esdhc_write32(®s->irqstat, -1);
356 /* Wait for the bus to be idle */
357 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
358 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
361 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
364 /* Set up for a data transfer if we have one */
366 err = esdhc_setup_data(priv, mmc, data);
371 /* Figure out the transfer arguments */
372 xfertyp = esdhc_xfertyp(cmd, data);
375 esdhc_write32(®s->irqsigen, 0);
377 /* Send the command */
378 esdhc_write32(®s->cmdarg, cmd->cmdarg);
379 esdhc_write32(®s->xfertyp, xfertyp);
381 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
382 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
385 /* Wait for the command to complete */
386 start = get_timer(0);
387 while (!(esdhc_read32(®s->irqstat) & flags)) {
388 if (get_timer(start) > 1000) {
394 irqstat = esdhc_read32(®s->irqstat);
396 if (irqstat & CMD_ERR) {
401 if (irqstat & IRQSTAT_CTOE) {
406 /* Workaround for ESDHC errata ENGcm03648 */
407 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
410 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
411 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
418 printf("Timeout waiting for DAT0 to go high!\n");
424 /* Copy the response to the response buffer */
425 if (cmd->resp_type & MMC_RSP_136) {
426 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
428 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
429 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
430 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
431 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
432 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
433 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
434 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
435 cmd->response[3] = (cmdrsp0 << 8);
437 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
439 /* Wait until all of the blocks are transferred */
441 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
442 esdhc_pio_read_write(priv, data);
444 flags = DATA_COMPLETE;
445 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
446 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
450 irqstat = esdhc_read32(®s->irqstat);
452 if (irqstat & IRQSTAT_DTOE) {
457 if (irqstat & DATA_ERR) {
461 } while ((irqstat & flags) != flags);
464 * Need invalidate the dcache here again to avoid any
465 * cache-fill during the DMA operations such as the
466 * speculative pre-fetching etc.
468 dma_unmap_single(priv->dma_addr,
469 data->blocks * data->blocksize,
470 mmc_get_dma_dir(data));
475 /* Reset CMD and DATA portions on error */
477 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
479 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC)
483 esdhc_write32(®s->sysctl,
484 esdhc_read32(®s->sysctl) |
486 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD))
491 esdhc_write32(®s->irqstat, -1);
496 static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
498 struct fsl_esdhc *regs = priv->esdhc_regs;
501 unsigned int sdhc_clk = priv->sdhc_clk;
506 if (clock < mmc->cfg->f_min)
507 clock = mmc->cfg->f_min;
509 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
512 while (sdhc_clk / (div * pre_div) > clock && div < 16)
515 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
516 clock == 200000000 && mmc->selected_mode == MMC_HS_400) {
517 u32 div_ratio = pre_div * div;
519 if (div_ratio <= 4) {
522 } else if (div_ratio <= 8) {
525 } else if (div_ratio <= 12) {
529 printf("unsupported clock division.\n");
533 mmc->clock = sdhc_clk / pre_div / div;
534 priv->clock = mmc->clock;
539 clk = (pre_div << 8) | (div << 4);
541 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
543 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
546 value = PRSSTAT_SDSTB;
547 while (!(esdhc_read32(®s->prsstat) & value)) {
549 printf("fsl_esdhc: Internal clock never stabilised.\n");
556 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
559 static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
561 struct fsl_esdhc *regs = priv->esdhc_regs;
565 value = esdhc_read32(®s->sysctl);
568 value |= SYSCTL_CKEN;
570 value &= ~SYSCTL_CKEN;
572 esdhc_write32(®s->sysctl, value);
575 value = PRSSTAT_SDSTB;
576 while (!(esdhc_read32(®s->prsstat) & value)) {
578 printf("fsl_esdhc: Internal clock never stabilised.\n");
586 static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv)
588 struct fsl_esdhc *regs = priv->esdhc_regs;
591 esdhc_setbits32(®s->esdhcctl, ESDHCCTL_FAF);
594 while (esdhc_read32(®s->esdhcctl) & ESDHCCTL_FAF) {
596 printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n");
604 static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv,
607 struct fsl_esdhc *regs = priv->esdhc_regs;
609 esdhc_clock_control(priv, false);
610 esdhc_flush_async_fifo(priv);
612 esdhc_setbits32(®s->tbctl, TBCTL_TB_EN);
614 esdhc_clrbits32(®s->tbctl, TBCTL_TB_EN);
615 esdhc_clock_control(priv, true);
618 static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv)
620 struct fsl_esdhc *regs = priv->esdhc_regs;
622 esdhc_clrbits32(®s->sdtimingctl, FLW_CTL_BG);
623 esdhc_clrbits32(®s->sdclkctl, CMD_CLK_CTL);
625 esdhc_clock_control(priv, false);
626 esdhc_clrbits32(®s->tbctl, HS400_MODE);
627 esdhc_clock_control(priv, true);
629 esdhc_clrbits32(®s->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE);
630 esdhc_clrbits32(®s->tbctl, HS400_WNDW_ADJUST);
632 esdhc_tuning_block_enable(priv, false);
635 static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode)
637 struct fsl_esdhc *regs = priv->esdhc_regs;
641 /* Exit HS400 mode before setting any other mode */
642 if (esdhc_read32(®s->tbctl) & HS400_MODE &&
644 esdhc_exit_hs400(priv);
646 esdhc_clock_control(priv, false);
648 if (mode == MMC_HS_200)
649 esdhc_clrsetbits32(®s->autoc12err, UHSM_MASK,
651 if (mode == MMC_HS_400) {
652 esdhc_setbits32(®s->tbctl, HS400_MODE);
653 esdhc_setbits32(®s->sdclkctl, CMD_CLK_CTL);
654 esdhc_clock_control(priv, true);
656 if (priv->clock == 200000000)
657 esdhc_setbits32(®s->dllcfg0, DLL_FREQ_SEL);
659 esdhc_setbits32(®s->dllcfg0, DLL_ENABLE);
661 esdhc_setbits32(®s->dllcfg0, DLL_RESET);
663 esdhc_clrbits32(®s->dllcfg0, DLL_RESET);
665 start = get_timer(0);
666 val = DLL_STS_SLV_LOCK;
667 while (!(esdhc_read32(®s->dllstat0) & val)) {
668 if (get_timer(start) > 1000) {
669 printf("fsl_esdhc: delay chain lock timeout\n");
674 esdhc_setbits32(®s->tbctl, HS400_WNDW_ADJUST);
676 esdhc_clock_control(priv, false);
677 esdhc_flush_async_fifo(priv);
679 esdhc_clock_control(priv, true);
683 static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
685 struct fsl_esdhc *regs = priv->esdhc_regs;
688 if (priv->is_sdhc_per_clk) {
689 /* Select to use peripheral clock */
690 esdhc_clock_control(priv, false);
691 esdhc_setbits32(®s->esdhcctl, ESDHCCTL_PCS);
692 esdhc_clock_control(priv, true);
695 if (mmc->selected_mode == MMC_HS_400)
696 esdhc_tuning_block_enable(priv, true);
698 /* Set the clock speed */
699 if (priv->clock != mmc->clock)
700 set_sysctl(priv, mmc, mmc->clock);
703 ret = esdhc_set_timing(priv, mmc->selected_mode);
707 /* Set the bus width */
708 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
710 if (mmc->bus_width == 4)
711 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
712 else if (mmc->bus_width == 8)
713 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
718 static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
720 #ifdef CONFIG_ARCH_MPC830X
721 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
722 sysconf83xx_t *sysconf = &immr->sysconf;
724 setbits_be32(&sysconf->sdhccr, 0x02000000);
726 esdhc_write32(®s->esdhcctl, 0x00000040);
730 static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
732 struct fsl_esdhc *regs = priv->esdhc_regs;
735 /* Reset the entire host controller */
736 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
738 /* Wait until the controller is available */
739 start = get_timer(0);
740 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
741 if (get_timer(start) > 1000)
745 /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */
746 esdhc_clrbits32(®s->tbctl, TBCTL_TB_EN);
748 esdhc_enable_cache_snooping(regs);
750 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
752 /* Set the initial clock speed */
753 set_sysctl(priv, mmc, 400000);
755 /* Disable the BRR and BWR bits in IRQSTAT */
756 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
758 /* Put the PROCTL reg back to the default */
759 esdhc_write32(®s->proctl, PROCTL_INIT);
761 /* Set timout to the maximum value */
762 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
764 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_UNRELIABLE_PULSE_DETECTION_WORKAROUND))
765 esdhc_clrbits32(®s->dllcfg1, DLL_PD_PULSE_STRETCH_SEL);
770 static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
772 struct fsl_esdhc *regs = priv->esdhc_regs;
774 #ifdef CONFIG_ESDHC_DETECT_QUIRK
775 if (CONFIG_ESDHC_DETECT_QUIRK)
778 if (esdhc_read32(®s->prsstat) & PRSSTAT_CINS)
784 static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
785 struct mmc_config *cfg)
787 struct fsl_esdhc *regs = priv->esdhc_regs;
790 caps = esdhc_read32(®s->hostcapblt);
793 * For eSDHC, power supply is through peripheral circuit. Some eSDHC
794 * versions have value 0 of the bit but that does not reflect the
795 * truth. 3.3V is common for SD/MMC, and is supported for all boards
796 * with eSDHC in current u-boot. So, make 3.3V is supported in
797 * default in code. CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT can be enabled
798 * if future board does not support 3.3V.
800 caps |= HOSTCAPBLT_VS33;
801 if (IS_ENABLED(CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT))
802 caps &= ~HOSTCAPBLT_VS33;
804 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
805 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
806 if (caps & HOSTCAPBLT_VS18)
807 cfg->voltages |= MMC_VDD_165_195;
808 if (caps & HOSTCAPBLT_VS30)
809 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
810 if (caps & HOSTCAPBLT_VS33)
811 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
813 cfg->name = "FSL_SDHC";
815 if (caps & HOSTCAPBLT_HSS)
816 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
819 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
820 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
823 #ifdef CONFIG_OF_LIBFDT
824 __weak int esdhc_status_fixup(void *blob, const char *compat)
826 if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) {
827 do_fixup_by_compat(blob, compat, "status", "disabled",
828 sizeof("disabled"), 1);
836 #if CONFIG_IS_ENABLED(DM_MMC)
837 static int fsl_esdhc_get_cd(struct udevice *dev);
838 static void esdhc_disable_for_no_card(void *blob)
842 for (uclass_first_device(UCLASS_MMC, &dev);
844 uclass_next_device(&dev)) {
847 if (fsl_esdhc_get_cd(dev))
850 snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
851 (unsigned long)dev_read_addr(dev));
852 do_fixup_by_path(blob, esdhc_path, "status", "disabled",
853 sizeof("disabled"), 1);
857 static void esdhc_disable_for_no_card(void *blob)
862 void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
864 const char *compat = "fsl,esdhc";
866 if (esdhc_status_fixup(blob, compat))
869 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND))
870 esdhc_disable_for_no_card(blob);
872 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
873 gd->arch.sdhc_clk, 1);
877 #if !CONFIG_IS_ENABLED(DM_MMC)
878 static int esdhc_getcd(struct mmc *mmc)
880 struct fsl_esdhc_priv *priv = mmc->priv;
882 return esdhc_getcd_common(priv);
885 static int esdhc_init(struct mmc *mmc)
887 struct fsl_esdhc_priv *priv = mmc->priv;
889 return esdhc_init_common(priv, mmc);
892 static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
893 struct mmc_data *data)
895 struct fsl_esdhc_priv *priv = mmc->priv;
897 return esdhc_send_cmd_common(priv, mmc, cmd, data);
900 static int esdhc_set_ios(struct mmc *mmc)
902 struct fsl_esdhc_priv *priv = mmc->priv;
904 return esdhc_set_ios_common(priv, mmc);
907 static const struct mmc_ops esdhc_ops = {
908 .getcd = esdhc_getcd,
910 .send_cmd = esdhc_send_cmd,
911 .set_ios = esdhc_set_ios,
914 int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
916 struct fsl_esdhc_plat *plat;
917 struct fsl_esdhc_priv *priv;
918 struct mmc_config *mmc_cfg;
924 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
927 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
933 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
934 priv->sdhc_clk = cfg->sdhc_clk;
935 if (gd->arch.sdhc_per_clk)
936 priv->is_sdhc_per_clk = true;
938 mmc_cfg = &plat->cfg;
940 if (cfg->max_bus_width == 8) {
941 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
943 } else if (cfg->max_bus_width == 4) {
944 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
945 } else if (cfg->max_bus_width == 1) {
946 mmc_cfg->host_caps |= MMC_MODE_1BIT;
948 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
950 printf("No max bus width provided. Assume 8-bit supported.\n");
953 if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
954 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
956 mmc_cfg->ops = &esdhc_ops;
958 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
960 mmc = mmc_create(mmc_cfg, priv);
968 int fsl_esdhc_mmc_init(struct bd_info *bis)
970 struct fsl_esdhc_cfg *cfg;
972 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
973 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
974 /* Prefer peripheral clock which provides higher frequency. */
975 if (gd->arch.sdhc_per_clk)
976 cfg->sdhc_clk = gd->arch.sdhc_per_clk;
978 cfg->sdhc_clk = gd->arch.sdhc_clk;
979 return fsl_esdhc_initialize(bis, cfg);
982 static int fsl_esdhc_probe(struct udevice *dev)
984 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
985 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
986 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
992 addr = dev_read_addr(dev);
993 if (addr == FDT_ADDR_T_NONE)
996 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
998 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1002 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) {
1004 * Only newer eSDHC controllers can do ADMA2 if the ADMA flag
1005 * is set in the host capabilities register.
1007 caps = esdhc_read32(&priv->esdhc_regs->hostcapblt);
1008 hostver = esdhc_read32(&priv->esdhc_regs->hostver);
1009 if (caps & HOSTCAPBLT_DMAS &&
1010 HOSTVER_VENDOR(hostver) > VENDOR_V_22) {
1011 priv->adma_desc_table = sdhci_adma_init();
1012 if (!priv->adma_desc_table)
1013 debug("Could not allocate ADMA tables, falling back to SDMA\n");
1017 if (gd->arch.sdhc_per_clk) {
1018 priv->sdhc_clk = gd->arch.sdhc_per_clk;
1019 priv->is_sdhc_per_clk = true;
1021 priv->sdhc_clk = gd->arch.sdhc_clk;
1024 if (priv->sdhc_clk <= 0) {
1025 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1029 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
1031 mmc_of_parse(dev, &plat->cfg);
1034 mmc->cfg = &plat->cfg;
1039 ret = esdhc_init_common(priv, mmc);
1043 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) &&
1044 !fsl_esdhc_get_cd(dev))
1045 esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
1050 static int fsl_esdhc_get_cd(struct udevice *dev)
1052 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1053 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1055 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1058 return esdhc_getcd_common(priv);
1061 static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1062 struct mmc_data *data)
1064 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1065 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1067 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1070 static int fsl_esdhc_set_ios(struct udevice *dev)
1072 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1073 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1075 return esdhc_set_ios_common(priv, &plat->mmc);
1078 static int fsl_esdhc_reinit(struct udevice *dev)
1080 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1081 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1083 return esdhc_init_common(priv, &plat->mmc);
1086 #ifdef MMC_SUPPORTS_TUNING
1087 static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
1089 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1090 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1091 struct fsl_esdhc *regs = priv->esdhc_regs;
1092 struct mmc *mmc = &plat->mmc;
1096 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
1097 plat->mmc.hs400_tuning)
1098 set_sysctl(priv, mmc, mmc->clock);
1100 esdhc_tuning_block_enable(priv, true);
1101 esdhc_setbits32(®s->autoc12err, EXECUTE_TUNING);
1103 irqstaten = esdhc_read32(®s->irqstaten);
1104 esdhc_write32(®s->irqstaten, IRQSTATEN_BRR);
1106 for (i = 0; i < MAX_TUNING_LOOP; i++) {
1107 mmc_send_tuning(mmc, opcode, NULL);
1110 val = esdhc_read32(®s->autoc12err);
1111 if (!(val & EXECUTE_TUNING)) {
1112 if (val & SMPCLKSEL)
1117 esdhc_write32(®s->irqstaten, irqstaten);
1119 if (i != MAX_TUNING_LOOP) {
1120 if (plat->mmc.hs400_tuning)
1121 esdhc_setbits32(®s->sdtimingctl, FLW_CTL_BG);
1125 printf("fsl_esdhc: tuning failed!\n");
1126 esdhc_clrbits32(®s->autoc12err, SMPCLKSEL);
1127 esdhc_clrbits32(®s->autoc12err, EXECUTE_TUNING);
1128 esdhc_tuning_block_enable(priv, false);
1133 int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev)
1135 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1137 esdhc_tuning_block_enable(priv, false);
1141 static const struct dm_mmc_ops fsl_esdhc_ops = {
1142 .get_cd = fsl_esdhc_get_cd,
1143 .send_cmd = fsl_esdhc_send_cmd,
1144 .set_ios = fsl_esdhc_set_ios,
1145 #ifdef MMC_SUPPORTS_TUNING
1146 .execute_tuning = fsl_esdhc_execute_tuning,
1148 .reinit = fsl_esdhc_reinit,
1149 .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr,
1152 static const struct udevice_id fsl_esdhc_ids[] = {
1153 { .compatible = "fsl,esdhc", },
1157 static int fsl_esdhc_bind(struct udevice *dev)
1159 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1161 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1164 U_BOOT_DRIVER(fsl_esdhc) = {
1165 .name = "fsl-esdhc-mmc",
1167 .of_match = fsl_esdhc_ids,
1168 .ops = &fsl_esdhc_ops,
1169 .bind = fsl_esdhc_bind,
1170 .probe = fsl_esdhc_probe,
1171 .plat_auto = sizeof(struct fsl_esdhc_plat),
1172 .priv_auto = sizeof(struct fsl_esdhc_priv),