blob: 0c66980b6218bc8d02df32989fb5abbaa9b0b205 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleminge52ffb82008-10-30 16:47:16 -05002/*
Jerry Huanged413672011-01-06 23:42:19 -06003 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Yangbo Lue087cd62021-06-03 10:51:17 +08004 * Copyright 2019-2021 NXP
Andy Fleminge52ffb82008-10-30 16:47:16 -05005 * Andy Fleming
6 *
7 * Based vaguely on the pxa mmc code:
8 * (C) Copyright 2003
9 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
Andy Fleminge52ffb82008-10-30 16:47:16 -050010 */
11
12#include <config.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050013#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070014#include <cpu_func.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090015#include <errno.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040016#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050017#include <mmc.h>
18#include <part.h>
19#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050020#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040021#include <fdt_support.h>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050024#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080025#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070026#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060027#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
Stephen Carlson1822a972021-08-17 12:46:40 -070029#include <linux/iopoll.h>
Michael Wallec9bba2e2020-09-23 12:42:48 +020030#include <linux/dma-mapping.h>
Michael Walle081d4012020-10-12 10:07:14 +020031#include <sdhci.h>
Tom Rinibdd47f32022-06-16 14:04:38 -040032#include "../../board/freescale/common/qixis.h"
Andy Fleminge52ffb82008-10-30 16:47:16 -050033
Andy Fleminge52ffb82008-10-30 16:47:16 -050034DECLARE_GLOBAL_DATA_PTR;
35
36struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080037 uint dsaddr; /* SDMA system address register */
38 uint blkattr; /* Block attributes register */
39 uint cmdarg; /* Command argument register */
40 uint xfertyp; /* Transfer type register */
41 uint cmdrsp0; /* Command response 0 register */
42 uint cmdrsp1; /* Command response 1 register */
43 uint cmdrsp2; /* Command response 2 register */
44 uint cmdrsp3; /* Command response 3 register */
45 uint datport; /* Buffer data port register */
46 uint prsstat; /* Present state register */
47 uint proctl; /* Protocol control register */
48 uint sysctl; /* System Control Register */
49 uint irqstat; /* Interrupt status register */
50 uint irqstaten; /* Interrupt status enable register */
51 uint irqsigen; /* Interrupt signal enable register */
52 uint autoc12err; /* Auto CMD error status register */
53 uint hostcapblt; /* Host controller capabilities register */
54 uint wml; /* Watermark level register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080055 char reserved1[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080056 uint fevt; /* Force event register */
57 uint admaes; /* ADMA error status register */
Michael Walle081d4012020-10-12 10:07:14 +020058 uint adsaddrl; /* ADMA system address low register */
59 uint adsaddrh; /* ADMA system address high register */
60 char reserved2[156];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080061 uint hostver; /* Host controller version register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080062 char reserved3[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080063 uint dmaerraddr; /* DMA error address register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080064 char reserved4[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080065 uint dmaerrattr; /* DMA error attribute register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080066 char reserved5[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080067 uint hostcapblt2; /* Host controller capabilities register 2 */
Yangbo Lu73da9c82020-09-01 16:58:01 +080068 char reserved6[8]; /* reserved */
69 uint tbctl; /* Tuning block control register */
Yangbo Lu8f9ace12020-09-01 16:58:05 +080070 char reserved7[32]; /* reserved */
71 uint sdclkctl; /* SD clock control register */
72 uint sdtimingctl; /* SD timing control register */
73 char reserved8[20]; /* reserved */
74 uint dllcfg0; /* DLL config 0 register */
Michael Walle7259dc52021-03-17 15:01:37 +010075 uint dllcfg1; /* DLL config 1 register */
76 char reserved9[8]; /* reserved */
Yangbo Lu8fbe95b2020-10-20 11:04:52 +080077 uint dllstat0; /* DLL status 0 register */
78 char reserved10[664];/* reserved */
Yangbo Lu62b56b32019-06-21 11:42:29 +080079 uint esdhcctl; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050080};
81
Simon Glassfa02ca52017-07-29 11:35:21 -060082struct fsl_esdhc_plat {
83 struct mmc_config cfg;
84 struct mmc mmc;
85};
86
Peng Fana4d36f72016-03-25 14:16:56 +080087/**
88 * struct fsl_esdhc_priv
89 *
90 * @esdhc_regs: registers of the sdhc controller
91 * @sdhc_clk: Current clk of the sdhc controller
92 * @bus_width: bus width, 1bit, 4bit or 8bit
93 * @cfg: mmc config
94 * @mmc: mmc
95 * Following is used when Driver Model is enabled for MMC
96 * @dev: pointer for the device
Peng Fana4d36f72016-03-25 14:16:56 +080097 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080098 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080099 */
100struct fsl_esdhc_priv {
101 struct fsl_esdhc *esdhc_regs;
102 unsigned int sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800103 bool is_sdhc_per_clk;
Peng Fanc4142702018-01-21 19:00:24 +0800104 unsigned int clock;
Yangbo Lu77f26322019-10-21 18:09:07 +0800105#if !CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +0800106 struct mmc *mmc;
Simon Glass407025d2017-07-29 11:35:24 -0600107#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800108 struct udevice *dev;
Michael Walle081d4012020-10-12 10:07:14 +0200109 struct sdhci_adma_desc *adma_desc_table;
Michael Wallec9bba2e2020-09-23 12:42:48 +0200110 dma_addr_t dma_addr;
Peng Fana4d36f72016-03-25 14:16:56 +0800111};
112
Andy Fleminge52ffb82008-10-30 16:47:16 -0500113/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000114static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500115{
116 uint xfertyp = 0;
117
118 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530119 xfertyp |= XFERTYP_DPSEL;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200120 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
121 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
Yangbo Lu73da9c82020-09-01 16:58:01 +0800122 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
123 xfertyp |= XFERTYP_DMAEN;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500124 if (data->blocks > 1) {
125 xfertyp |= XFERTYP_MSBSEL;
126 xfertyp |= XFERTYP_BCEN;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200127 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
128 xfertyp |= XFERTYP_AC12EN;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500129 }
130
131 if (data->flags & MMC_DATA_READ)
132 xfertyp |= XFERTYP_DTDSEL;
133 }
134
135 if (cmd->resp_type & MMC_RSP_CRC)
136 xfertyp |= XFERTYP_CCCEN;
137 if (cmd->resp_type & MMC_RSP_OPCODE)
138 xfertyp |= XFERTYP_CICEN;
139 if (cmd->resp_type & MMC_RSP_136)
140 xfertyp |= XFERTYP_RSPTYP_136;
141 else if (cmd->resp_type & MMC_RSP_BUSY)
142 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
143 else if (cmd->resp_type & MMC_RSP_PRESENT)
144 xfertyp |= XFERTYP_RSPTYP_48;
145
Jason Liubef0ff02011-03-22 01:32:31 +0000146 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
147 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800148
Andy Fleminge52ffb82008-10-30 16:47:16 -0500149 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
150}
151
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530152/*
153 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
154 */
Simon Glass1d177d42017-07-29 11:35:17 -0600155static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
156 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530157{
Peng Fana4d36f72016-03-25 14:16:56 +0800158 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530159 uint blocks;
160 char *buffer;
161 uint databuf;
162 uint size;
163 uint irqstat;
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100164 ulong start;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530165
166 if (data->flags & MMC_DATA_READ) {
167 blocks = data->blocks;
168 buffer = data->dest;
169 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100170 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530171 size = data->blocksize;
172 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100173 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
174 if (get_timer(start) > PIO_TIMEOUT) {
175 printf("\nData Read Failed in PIO Mode.");
176 return;
177 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530178 }
179 while (size && (!(irqstat & IRQSTAT_TC))) {
180 udelay(100); /* Wait before last byte transfer complete */
181 irqstat = esdhc_read32(&regs->irqstat);
182 databuf = in_le32(&regs->datport);
183 *((uint *)buffer) = databuf;
184 buffer += 4;
185 size -= 4;
186 }
187 blocks--;
188 }
189 } else {
190 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200191 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530192 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100193 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530194 size = data->blocksize;
195 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100196 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
197 if (get_timer(start) > PIO_TIMEOUT) {
198 printf("\nData Write Failed in PIO Mode.");
199 return;
200 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530201 }
202 while (size && (!(irqstat & IRQSTAT_TC))) {
203 udelay(100); /* Wait before last byte transfer complete */
204 databuf = *((uint *)buffer);
205 buffer += 4;
206 size -= 4;
207 irqstat = esdhc_read32(&regs->irqstat);
208 out_le32(&regs->datport, databuf);
209 }
210 blocks--;
211 }
212 }
213}
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530214
Michael Wallebdd413f2020-09-23 12:42:49 +0200215static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
216 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500217{
Peng Fana4d36f72016-03-25 14:16:56 +0800218 struct fsl_esdhc *regs = priv->esdhc_regs;
Michael Wallebdd413f2020-09-23 12:42:49 +0200219 uint wml_value = data->blocksize / 4;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500220
221 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530222 if (wml_value > WML_RD_WML_MAX)
223 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500224
Roy Zange5853af2010-02-09 18:23:33 +0800225 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500226 } else {
Priyanka Jain02449632011-02-09 09:24:10 +0530227 if (wml_value > WML_WR_WML_MAX)
228 wml_value = WML_WR_WML_MAX_VAL;
Yangbo Luf3bcc832019-10-31 18:54:25 +0800229
Roy Zange5853af2010-02-09 18:23:33 +0800230 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
Michael Wallebdd413f2020-09-23 12:42:49 +0200231 wml_value << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500232 }
Michael Wallebdd413f2020-09-23 12:42:49 +0200233}
Michael Wallebdd413f2020-09-23 12:42:49 +0200234
235static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
236{
237 uint trans_bytes = data->blocksize * data->blocks;
238 struct fsl_esdhc *regs = priv->esdhc_regs;
Michael Walle081d4012020-10-12 10:07:14 +0200239 phys_addr_t adma_addr;
Michael Wallebdd413f2020-09-23 12:42:49 +0200240 void *buf;
241
242 if (data->flags & MMC_DATA_WRITE)
243 buf = (void *)data->src;
244 else
245 buf = data->dest;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500246
Michael Wallebdd413f2020-09-23 12:42:49 +0200247 priv->dma_addr = dma_map_single(buf, trans_bytes,
248 mmc_get_dma_dir(data));
Michael Walle081d4012020-10-12 10:07:14 +0200249
250 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) &&
251 priv->adma_desc_table) {
252 debug("Using ADMA2\n");
253 /* prefer ADMA2 if it is available */
Ian Roberts6853d892024-04-22 15:00:02 -0400254 sdhci_prepare_adma_table(NULL, priv->adma_desc_table, data,
Michael Walle081d4012020-10-12 10:07:14 +0200255 priv->dma_addr);
256
257 adma_addr = virt_to_phys(priv->adma_desc_table);
258 esdhc_write32(&regs->adsaddrl, lower_32_bits(adma_addr));
259 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT))
260 esdhc_write32(&regs->adsaddrh, upper_32_bits(adma_addr));
261 esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
262 PROCTL_DMAS_ADMA2);
263 } else {
264 debug("Using SDMA\n");
265 if (upper_32_bits(priv->dma_addr))
266 printf("Cannot use 64 bit addresses with SDMA\n");
267 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
268 esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
269 PROCTL_DMAS_SDMA);
270 }
271
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100272 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Michael Wallebdd413f2020-09-23 12:42:49 +0200273}
274
275static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
276 struct mmc_data *data)
277{
278 int timeout;
279 bool is_write = data->flags & MMC_DATA_WRITE;
280 struct fsl_esdhc *regs = priv->esdhc_regs;
281
282 if (is_write && !(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
283 printf("Can not write to locked SD card.\n");
284 return -EINVAL;
285 }
286
Michael Wallebc9e13e2020-10-12 10:07:13 +0200287 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
288 esdhc_setup_watermark_level(priv, data);
289 else
290 esdhc_setup_dma(priv, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500291
292 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530293 /*
294 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
295 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
296 * So, Number of SD Clock cycles for 0.25sec should be minimum
297 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500298 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530299 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500300 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530301 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500302 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530303 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500304 * => timeout + 13 = log2(mmc->clock/4) + 1
305 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800306 *
307 * However, the MMC spec "It is strongly recommended for hosts to
308 * implement more than 500ms timeout value even if the card
309 * indicates the 250ms maximum busy length." Even the previous
310 * value of 300ms is known to be insufficient for some cards.
311 * So, we use
312 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530313 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800314 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500315 timeout -= 13;
316
317 if (timeout > 14)
318 timeout = 14;
319
320 if (timeout < 0)
321 timeout = 0;
322
Michael Wallebc9e13e2020-10-12 10:07:13 +0200323 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
324 (timeout == 4 || timeout == 8 || timeout == 12))
Kumar Gala9a878d52011-01-29 15:36:10 -0600325 timeout++;
Kumar Gala9a878d52011-01-29 15:36:10 -0600326
Michael Wallebc9e13e2020-10-12 10:07:13 +0200327 if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
328 timeout = 0xE;
329
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100330 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500331
332 return 0;
333}
334
Andy Fleminge52ffb82008-10-30 16:47:16 -0500335/*
336 * Sends a command out on the bus. Takes the mmc pointer,
337 * a command pointer, and an optional data pointer.
338 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600339static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
340 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500341{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500342 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500343 uint xfertyp;
344 uint irqstat;
Peng Fanc4142702018-01-21 19:00:24 +0800345 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fana4d36f72016-03-25 14:16:56 +0800346 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam7300ef52018-11-19 10:31:53 -0200347 unsigned long start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500348
Michael Wallebc9e13e2020-10-12 10:07:13 +0200349 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
350 cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
Jerry Huanged413672011-01-06 23:42:19 -0600351 return 0;
Jerry Huanged413672011-01-06 23:42:19 -0600352
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100353 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500354
355 sync();
356
357 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100358 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
359 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
360 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500361
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100362 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
363 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500364
Andy Fleminge52ffb82008-10-30 16:47:16 -0500365 /* Set up for a data transfer if we have one */
366 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600367 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500368 if(err)
369 return err;
370 }
371
372 /* Figure out the transfer arguments */
373 xfertyp = esdhc_xfertyp(cmd, data);
374
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500375 /* Mask all irqs */
376 esdhc_write32(&regs->irqsigen, 0);
377
Andy Fleminge52ffb82008-10-30 16:47:16 -0500378 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100379 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
380 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behmed8552d62012-03-26 03:13:05 +0000381
Yangbo Lu73da9c82020-09-01 16:58:01 +0800382 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
383 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
384 flags = IRQSTAT_BRR;
385
Andy Fleminge52ffb82008-10-30 16:47:16 -0500386 /* Wait for the command to complete */
Fabio Estevam7300ef52018-11-19 10:31:53 -0200387 start = get_timer(0);
388 while (!(esdhc_read32(&regs->irqstat) & flags)) {
389 if (get_timer(start) > 1000) {
390 err = -ETIMEDOUT;
391 goto out;
392 }
393 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500394
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100395 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500396
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500397 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900398 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500399 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000400 }
401
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500402 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900403 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500404 goto out;
405 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500406
Dirk Behmed8552d62012-03-26 03:13:05 +0000407 /* Workaround for ESDHC errata ENGcm03648 */
408 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800409 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000410
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800411 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000412 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
413 PRSSTAT_DAT0)) {
414 udelay(100);
415 timeout--;
416 }
417
418 if (timeout <= 0) {
419 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900420 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500421 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000422 }
423 }
424
Andy Fleminge52ffb82008-10-30 16:47:16 -0500425 /* Copy the response to the response buffer */
426 if (cmd->resp_type & MMC_RSP_136) {
427 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
428
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100429 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
430 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
431 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
432 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530433 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
434 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
435 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
436 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500437 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100438 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500439
440 /* Wait until all of the blocks are transferred */
441 if (data) {
Michael Wallebc9e13e2020-10-12 10:07:13 +0200442 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
443 esdhc_pio_read_write(priv, data);
444 } else {
445 flags = DATA_COMPLETE;
446 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
447 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
448 flags = IRQSTAT_BRR;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800449
Michael Wallebc9e13e2020-10-12 10:07:13 +0200450 do {
451 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500452
Michael Wallebc9e13e2020-10-12 10:07:13 +0200453 if (irqstat & IRQSTAT_DTOE) {
454 err = -ETIMEDOUT;
455 goto out;
456 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000457
Michael Wallebc9e13e2020-10-12 10:07:13 +0200458 if (irqstat & DATA_ERR) {
459 err = -ECOMM;
460 goto out;
461 }
462 } while ((irqstat & flags) != flags);
Ye.Li33a56b12014-02-20 18:00:57 +0800463
Michael Wallebc9e13e2020-10-12 10:07:13 +0200464 /*
465 * Need invalidate the dcache here again to avoid any
466 * cache-fill during the DMA operations such as the
467 * speculative pre-fetching etc.
468 */
469 dma_unmap_single(priv->dma_addr,
470 data->blocks * data->blocksize,
471 mmc_get_dma_dir(data));
472 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500473 }
474
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500475out:
476 /* Reset CMD and DATA portions on error */
477 if (err) {
478 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
479 SYSCTL_RSTC);
480 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
481 ;
482
483 if (data) {
484 esdhc_write32(&regs->sysctl,
485 esdhc_read32(&regs->sysctl) |
486 SYSCTL_RSTD);
487 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
488 ;
489 }
490 }
491
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100492 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500493
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500494 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500495}
496
Simon Glass1d177d42017-07-29 11:35:17 -0600497static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500498{
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100499 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200500 int div = 1;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200501 int pre_div = 2;
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800502 unsigned int sdhc_clk = priv->sdhc_clk;
503 u32 time_out;
504 u32 value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500505 uint clk;
Pali Rohár7ab65002022-04-29 20:27:34 +0200506 u32 hostver;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500507
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200508 if (clock < mmc->cfg->f_min)
509 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100510
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800511 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200512 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500513
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800514 while (sdhc_clk / (div * pre_div) > clock && div < 16)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200515 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500516
Michael Walle148dc612021-03-17 15:01:36 +0100517 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
518 clock == 200000000 && mmc->selected_mode == MMC_HS_400) {
519 u32 div_ratio = pre_div * div;
520
521 if (div_ratio <= 4) {
522 pre_div = 4;
523 div = 1;
524 } else if (div_ratio <= 8) {
525 pre_div = 4;
526 div = 2;
527 } else if (div_ratio <= 12) {
528 pre_div = 4;
529 div = 3;
530 } else {
531 printf("unsupported clock division.\n");
532 }
533 }
534
Yangbo Ludd08eea2020-09-01 16:58:06 +0800535 mmc->clock = sdhc_clk / pre_div / div;
536 priv->clock = mmc->clock;
537
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200538 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500539 div -= 1;
540
541 clk = (pre_div << 8) | (div << 4);
542
Kumar Gala09876a32010-03-18 15:51:05 -0500543 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100544
545 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500546
Pali Rohár7ab65002022-04-29 20:27:34 +0200547 /* Only newer eSDHC controllers set PRSSTAT_SDSTB flag */
548 hostver = esdhc_read32(&priv->esdhc_regs->hostver);
549 if (HOSTVER_VENDOR(hostver) <= VENDOR_V_22) {
550 udelay(10000);
551 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
552 return;
553 }
554
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800555 time_out = 20;
556 value = PRSSTAT_SDSTB;
557 while (!(esdhc_read32(&regs->prsstat) & value)) {
558 if (time_out == 0) {
559 printf("fsl_esdhc: Internal clock never stabilised.\n");
560 break;
561 }
562 time_out--;
563 mdelay(1);
564 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500565
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700566 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500567}
568
Simon Glass1d177d42017-07-29 11:35:17 -0600569static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800570{
Peng Fana4d36f72016-03-25 14:16:56 +0800571 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800572 u32 value;
573 u32 time_out;
Pali Rohár7ab65002022-04-29 20:27:34 +0200574 u32 hostver;
Yangbo Lu163beec2015-04-22 13:57:40 +0800575
576 value = esdhc_read32(&regs->sysctl);
577
578 if (enable)
579 value |= SYSCTL_CKEN;
580 else
581 value &= ~SYSCTL_CKEN;
582
583 esdhc_write32(&regs->sysctl, value);
584
Pali Rohár7ab65002022-04-29 20:27:34 +0200585 /* Only newer eSDHC controllers set PRSSTAT_SDSTB flag */
586 hostver = esdhc_read32(&priv->esdhc_regs->hostver);
587 if (HOSTVER_VENDOR(hostver) <= VENDOR_V_22) {
588 udelay(10000);
589 return;
590 }
591
Yangbo Lu163beec2015-04-22 13:57:40 +0800592 time_out = 20;
593 value = PRSSTAT_SDSTB;
594 while (!(esdhc_read32(&regs->prsstat) & value)) {
595 if (time_out == 0) {
596 printf("fsl_esdhc: Internal clock never stabilised.\n");
597 break;
598 }
599 time_out--;
600 mdelay(1);
601 }
Peng Fanc4142702018-01-21 19:00:24 +0800602}
Yangbo Lu163beec2015-04-22 13:57:40 +0800603
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800604static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv)
605{
606 struct fsl_esdhc *regs = priv->esdhc_regs;
607 u32 time_out;
608
609 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_FAF);
610
611 time_out = 20;
612 while (esdhc_read32(&regs->esdhcctl) & ESDHCCTL_FAF) {
613 if (time_out == 0) {
614 printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n");
615 break;
616 }
617 time_out--;
618 mdelay(1);
619 }
620}
621
622static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv,
623 bool en)
624{
625 struct fsl_esdhc *regs = priv->esdhc_regs;
626
627 esdhc_clock_control(priv, false);
628 esdhc_flush_async_fifo(priv);
629 if (en)
630 esdhc_setbits32(&regs->tbctl, TBCTL_TB_EN);
631 else
632 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
633 esdhc_clock_control(priv, true);
634}
635
636static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv)
637{
638 struct fsl_esdhc *regs = priv->esdhc_regs;
639
640 esdhc_clrbits32(&regs->sdtimingctl, FLW_CTL_BG);
641 esdhc_clrbits32(&regs->sdclkctl, CMD_CLK_CTL);
642
643 esdhc_clock_control(priv, false);
644 esdhc_clrbits32(&regs->tbctl, HS400_MODE);
645 esdhc_clock_control(priv, true);
646
647 esdhc_clrbits32(&regs->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE);
648 esdhc_clrbits32(&regs->tbctl, HS400_WNDW_ADJUST);
649
650 esdhc_tuning_block_enable(priv, false);
651}
652
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800653static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode)
Yangbo Lu73da9c82020-09-01 16:58:01 +0800654{
655 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800656 ulong start;
657 u32 val;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800658
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800659 /* Exit HS400 mode before setting any other mode */
660 if (esdhc_read32(&regs->tbctl) & HS400_MODE &&
661 mode != MMC_HS_400)
662 esdhc_exit_hs400(priv);
663
Yangbo Lu73da9c82020-09-01 16:58:01 +0800664 esdhc_clock_control(priv, false);
665
666 if (mode == MMC_HS_200)
667 esdhc_clrsetbits32(&regs->autoc12err, UHSM_MASK,
668 UHSM_SDR104_HS200);
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800669 if (mode == MMC_HS_400) {
670 esdhc_setbits32(&regs->tbctl, HS400_MODE);
671 esdhc_setbits32(&regs->sdclkctl, CMD_CLK_CTL);
672 esdhc_clock_control(priv, true);
Yangbo Lu73da9c82020-09-01 16:58:01 +0800673
Yangbo Lu9ac60a42020-09-01 16:58:07 +0800674 if (priv->clock == 200000000)
675 esdhc_setbits32(&regs->dllcfg0, DLL_FREQ_SEL);
676
677 esdhc_setbits32(&regs->dllcfg0, DLL_ENABLE);
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800678
679 esdhc_setbits32(&regs->dllcfg0, DLL_RESET);
680 udelay(1);
681 esdhc_clrbits32(&regs->dllcfg0, DLL_RESET);
682
683 start = get_timer(0);
684 val = DLL_STS_SLV_LOCK;
685 while (!(esdhc_read32(&regs->dllstat0) & val)) {
686 if (get_timer(start) > 1000) {
687 printf("fsl_esdhc: delay chain lock timeout\n");
688 return -ETIMEDOUT;
689 }
690 }
691
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800692 esdhc_setbits32(&regs->tbctl, HS400_WNDW_ADJUST);
693
694 esdhc_clock_control(priv, false);
695 esdhc_flush_async_fifo(priv);
696 }
Yangbo Lu73da9c82020-09-01 16:58:01 +0800697 esdhc_clock_control(priv, true);
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800698 return 0;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800699}
700
Simon Glass6aa55dc2017-07-29 11:35:18 -0600701static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500702{
Peng Fana4d36f72016-03-25 14:16:56 +0800703 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800704 int ret;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500705
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800706 if (priv->is_sdhc_per_clk) {
707 /* Select to use peripheral clock */
708 esdhc_clock_control(priv, false);
709 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
710 esdhc_clock_control(priv, true);
711 }
712
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800713 if (mmc->selected_mode == MMC_HS_400)
714 esdhc_tuning_block_enable(priv, true);
715
Andy Fleminge52ffb82008-10-30 16:47:16 -0500716 /* Set the clock speed */
Peng Fanc4142702018-01-21 19:00:24 +0800717 if (priv->clock != mmc->clock)
718 set_sysctl(priv, mmc, mmc->clock);
719
Yangbo Lu73da9c82020-09-01 16:58:01 +0800720 /* Set timing */
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800721 ret = esdhc_set_timing(priv, mmc->selected_mode);
722 if (ret)
723 return ret;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800724
Andy Fleminge52ffb82008-10-30 16:47:16 -0500725 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100726 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500727
728 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100729 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500730 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100731 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
732
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900733 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500734}
735
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000736static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
737{
738#ifdef CONFIG_ARCH_MPC830X
739 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
740 sysconf83xx_t *sysconf = &immr->sysconf;
741
742 setbits_be32(&sysconf->sdhccr, 0x02000000);
743#else
Pali Rohár6c6cec32022-04-04 18:32:13 +0200744 esdhc_write32(&regs->esdhcctl, ESDHCCTL_SNOOP);
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000745#endif
746}
747
Simon Glass6aa55dc2017-07-29 11:35:18 -0600748static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500749{
Peng Fana4d36f72016-03-25 14:16:56 +0800750 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass0c3ef222017-07-29 11:35:20 -0600751 ulong start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500752
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100753 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200754 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100755
756 /* Wait until the controller is available */
Simon Glass0c3ef222017-07-29 11:35:20 -0600757 start = get_timer(0);
758 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
759 if (get_timer(start) > 1000)
760 return -ETIMEDOUT;
761 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500762
Yangbo Lu573859c2020-09-01 16:58:02 +0800763 /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */
764 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
765
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000766 esdhc_enable_cache_snooping(regs);
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530767
Dirk Behmedbe67252013-07-15 15:44:29 +0200768 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500769
770 /* Set the initial clock speed */
Yangbo Luee2708b2020-10-20 11:04:51 +0800771 set_sysctl(priv, mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500772
773 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100774 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500775
776 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100777 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500778
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100779 /* Set timout to the maximum value */
780 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500781
Michael Walle7259dc52021-03-17 15:01:37 +0100782 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_UNRELIABLE_PULSE_DETECTION_WORKAROUND))
783 esdhc_clrbits32(&regs->dllcfg1, DLL_PD_PULSE_STRETCH_SEL);
784
Thierry Reding8cee4c982012-01-02 01:15:38 +0000785 return 0;
786}
787
Simon Glass6aa55dc2017-07-29 11:35:18 -0600788static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Reding8cee4c982012-01-02 01:15:38 +0000789{
Peng Fana4d36f72016-03-25 14:16:56 +0800790 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500791
Haijun.Zhang05f58542014-01-10 13:52:17 +0800792#ifdef CONFIG_ESDHC_DETECT_QUIRK
Tom Rinibdd47f32022-06-16 14:04:38 -0400793 if (qixis_esdhc_detect_quirk())
Haijun.Zhang05f58542014-01-10 13:52:17 +0800794 return 1;
795#endif
Yangbo Lu8abc0432020-05-19 11:06:43 +0800796 if (esdhc_read32(&regs->prsstat) & PRSSTAT_CINS)
797 return 1;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100798
Yangbo Lu8abc0432020-05-19 11:06:43 +0800799 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500800}
801
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800802static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
803 struct mmc_config *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500804{
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800805 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu63267b42019-10-31 18:54:21 +0800806 u32 caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500807
Wang Huanc9292132014-09-05 13:52:40 +0800808 caps = esdhc_read32(&regs->hostcapblt);
Yangbo Lue087cd62021-06-03 10:51:17 +0800809
810 /*
811 * For eSDHC, power supply is through peripheral circuit. Some eSDHC
812 * versions have value 0 of the bit but that does not reflect the
813 * truth. 3.3V is common for SD/MMC, and is supported for all boards
814 * with eSDHC in current u-boot. So, make 3.3V is supported in
815 * default in code. CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT can be enabled
816 * if future board does not support 3.3V.
817 */
818 caps |= HOSTCAPBLT_VS33;
819 if (IS_ENABLED(CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT))
820 caps &= ~HOSTCAPBLT_VS33;
821
Michael Wallebc9e13e2020-10-12 10:07:13 +0200822 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
823 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Yangbo Lu63267b42019-10-31 18:54:21 +0800824 if (caps & HOSTCAPBLT_VS18)
825 cfg->voltages |= MMC_VDD_165_195;
826 if (caps & HOSTCAPBLT_VS30)
827 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
828 if (caps & HOSTCAPBLT_VS33)
829 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000830
Simon Glassfa02ca52017-07-29 11:35:21 -0600831 cfg->name = "FSL_SDHC";
Abbas Razae6bf9772013-03-25 09:13:34 +0000832
Yangbo Lu63267b42019-10-31 18:54:21 +0800833 if (caps & HOSTCAPBLT_HSS)
Simon Glassfa02ca52017-07-29 11:35:21 -0600834 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500835
Simon Glassfa02ca52017-07-29 11:35:21 -0600836 cfg->f_min = 400000;
Peng Fanc4142702018-01-21 19:00:24 +0800837 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Simon Glassfa02ca52017-07-29 11:35:21 -0600838 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Peng Fana4d36f72016-03-25 14:16:56 +0800839}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400840
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100841#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800842__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400843{
Michael Wallebc9e13e2020-10-12 10:07:13 +0200844 if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800845 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800846 sizeof("disabled"), 1);
847 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400848 }
Michael Wallebc9e13e2020-10-12 10:07:13 +0200849
Yangbo Lud84139c2017-01-17 10:43:54 +0800850 return 0;
851}
852
Yangbo Luce884022020-05-19 11:06:44 +0800853
Michael Wallebc9e13e2020-10-12 10:07:13 +0200854#if CONFIG_IS_ENABLED(DM_MMC)
855static int fsl_esdhc_get_cd(struct udevice *dev);
Yangbo Luce884022020-05-19 11:06:44 +0800856static void esdhc_disable_for_no_card(void *blob)
857{
858 struct udevice *dev;
859
860 for (uclass_first_device(UCLASS_MMC, &dev);
861 dev;
862 uclass_next_device(&dev)) {
863 char esdhc_path[50];
864
865 if (fsl_esdhc_get_cd(dev))
866 continue;
867
868 snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
869 (unsigned long)dev_read_addr(dev));
870 do_fixup_by_path(blob, esdhc_path, "status", "disabled",
871 sizeof("disabled"), 1);
872 }
873}
Michael Wallebc9e13e2020-10-12 10:07:13 +0200874#else
875static void esdhc_disable_for_no_card(void *blob)
876{
877}
Yangbo Luce884022020-05-19 11:06:44 +0800878#endif
879
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900880void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lud84139c2017-01-17 10:43:54 +0800881{
882 const char *compat = "fsl,esdhc";
883
884 if (esdhc_status_fixup(blob, compat))
885 return;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200886
887 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND))
888 esdhc_disable_for_no_card(blob);
889
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400890 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000891 gd->arch.sdhc_clk, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400892}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100893#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800894
Yangbo Lu4fc93332019-10-31 18:54:26 +0800895#if !CONFIG_IS_ENABLED(DM_MMC)
896static int esdhc_getcd(struct mmc *mmc)
897{
898 struct fsl_esdhc_priv *priv = mmc->priv;
899
900 return esdhc_getcd_common(priv);
901}
902
903static int esdhc_init(struct mmc *mmc)
904{
905 struct fsl_esdhc_priv *priv = mmc->priv;
906
907 return esdhc_init_common(priv, mmc);
908}
909
910static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
911 struct mmc_data *data)
912{
913 struct fsl_esdhc_priv *priv = mmc->priv;
914
915 return esdhc_send_cmd_common(priv, mmc, cmd, data);
916}
917
918static int esdhc_set_ios(struct mmc *mmc)
919{
920 struct fsl_esdhc_priv *priv = mmc->priv;
921
922 return esdhc_set_ios_common(priv, mmc);
923}
924
925static const struct mmc_ops esdhc_ops = {
926 .getcd = esdhc_getcd,
927 .init = esdhc_init,
928 .send_cmd = esdhc_send_cmd,
929 .set_ios = esdhc_set_ios,
930};
931
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900932int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu4fc93332019-10-31 18:54:26 +0800933{
934 struct fsl_esdhc_plat *plat;
935 struct fsl_esdhc_priv *priv;
936 struct mmc_config *mmc_cfg;
937 struct mmc *mmc;
938
939 if (!cfg)
940 return -EINVAL;
941
942 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
943 if (!priv)
944 return -ENOMEM;
945 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
946 if (!plat) {
947 free(priv);
948 return -ENOMEM;
949 }
950
951 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
952 priv->sdhc_clk = cfg->sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800953 if (gd->arch.sdhc_per_clk)
954 priv->is_sdhc_per_clk = true;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800955
956 mmc_cfg = &plat->cfg;
957
958 if (cfg->max_bus_width == 8) {
959 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
960 MMC_MODE_8BIT;
961 } else if (cfg->max_bus_width == 4) {
962 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
963 } else if (cfg->max_bus_width == 1) {
964 mmc_cfg->host_caps |= MMC_MODE_1BIT;
965 } else {
Pali Rohár39a43b02022-05-11 20:27:12 +0200966 mmc_cfg->host_caps |= MMC_MODE_1BIT;
967 printf("No max bus width provided. Fallback to 1-bit mode.\n");
Yangbo Lu4fc93332019-10-31 18:54:26 +0800968 }
969
Michael Wallebc9e13e2020-10-12 10:07:13 +0200970 if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
Yangbo Lu4fc93332019-10-31 18:54:26 +0800971 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200972
Yangbo Lu4fc93332019-10-31 18:54:26 +0800973 mmc_cfg->ops = &esdhc_ops;
974
975 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
976
977 mmc = mmc_create(mmc_cfg, priv);
978 if (!mmc)
979 return -EIO;
980
981 priv->mmc = mmc;
982 return 0;
983}
984
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900985int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu4fc93332019-10-31 18:54:26 +0800986{
987 struct fsl_esdhc_cfg *cfg;
988
989 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Tom Rini376b88a2022-10-28 20:27:13 -0400990 cfg->esdhc_base = CFG_SYS_FSL_ESDHC_ADDR;
Pali Rohár3a672d62022-05-11 20:27:13 +0200991 cfg->max_bus_width = CONFIG_SYS_FSL_ESDHC_DEFAULT_BUS_WIDTH;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800992 /* Prefer peripheral clock which provides higher frequency. */
993 if (gd->arch.sdhc_per_clk)
994 cfg->sdhc_clk = gd->arch.sdhc_per_clk;
995 else
996 cfg->sdhc_clk = gd->arch.sdhc_clk;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800997 return fsl_esdhc_initialize(bis, cfg);
998}
999#else /* DM_MMC */
Peng Fana4d36f72016-03-25 14:16:56 +08001000static int fsl_esdhc_probe(struct udevice *dev)
1001{
1002 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001003 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Peng Fana4d36f72016-03-25 14:16:56 +08001004 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Michael Walle081d4012020-10-12 10:07:14 +02001005 u32 caps, hostver;
Peng Fana4d36f72016-03-25 14:16:56 +08001006 fdt_addr_t addr;
Simon Glass407025d2017-07-29 11:35:24 -06001007 struct mmc *mmc;
Yangbo Luce884022020-05-19 11:06:44 +08001008 int ret;
Peng Fana4d36f72016-03-25 14:16:56 +08001009
Simon Glass80e9df42017-07-29 11:35:23 -06001010 addr = dev_read_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +08001011 if (addr == FDT_ADDR_T_NONE)
1012 return -EINVAL;
Yinbo Zhu583d5e92019-04-11 11:01:50 +00001013#ifdef CONFIG_PPC
1014 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
1015#else
Peng Fana4d36f72016-03-25 14:16:56 +08001016 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhu583d5e92019-04-11 11:01:50 +00001017#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001018 priv->dev = dev;
1019
Michael Walle081d4012020-10-12 10:07:14 +02001020 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) {
1021 /*
1022 * Only newer eSDHC controllers can do ADMA2 if the ADMA flag
1023 * is set in the host capabilities register.
1024 */
1025 caps = esdhc_read32(&priv->esdhc_regs->hostcapblt);
1026 hostver = esdhc_read32(&priv->esdhc_regs->hostver);
1027 if (caps & HOSTCAPBLT_DMAS &&
1028 HOSTVER_VENDOR(hostver) > VENDOR_V_22) {
1029 priv->adma_desc_table = sdhci_adma_init();
1030 if (!priv->adma_desc_table)
1031 debug("Could not allocate ADMA tables, falling back to SDMA\n");
1032 }
1033 }
1034
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +08001035 if (gd->arch.sdhc_per_clk) {
1036 priv->sdhc_clk = gd->arch.sdhc_per_clk;
1037 priv->is_sdhc_per_clk = true;
1038 } else {
1039 priv->sdhc_clk = gd->arch.sdhc_clk;
1040 }
1041
Yangbo Lub8626e42019-11-12 19:28:36 +08001042 if (priv->sdhc_clk <= 0) {
1043 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1044 return -EINVAL;
Peng Fana4d36f72016-03-25 14:16:56 +08001045 }
1046
Yangbo Lub64dc8d2019-10-31 18:54:23 +08001047 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
Peng Fana4d36f72016-03-25 14:16:56 +08001048
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001049 mmc_of_parse(dev, &plat->cfg);
1050
Simon Glass407025d2017-07-29 11:35:24 -06001051 mmc = &plat->mmc;
1052 mmc->cfg = &plat->cfg;
1053 mmc->dev = dev;
Yangbo Lu4cc119b2019-05-23 11:05:46 +08001054
Simon Glass407025d2017-07-29 11:35:24 -06001055 upriv->mmc = mmc;
Peng Fana4d36f72016-03-25 14:16:56 +08001056
Yangbo Luce884022020-05-19 11:06:44 +08001057 ret = esdhc_init_common(priv, mmc);
1058 if (ret)
1059 return ret;
1060
Michael Wallebc9e13e2020-10-12 10:07:13 +02001061 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) &&
1062 !fsl_esdhc_get_cd(dev))
Yangbo Luce884022020-05-19 11:06:44 +08001063 esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
Michael Wallebc9e13e2020-10-12 10:07:13 +02001064
Yangbo Luce884022020-05-19 11:06:44 +08001065 return 0;
Peng Fana4d36f72016-03-25 14:16:56 +08001066}
1067
Simon Glass407025d2017-07-29 11:35:24 -06001068static int fsl_esdhc_get_cd(struct udevice *dev)
1069{
Simon Glassfa20e932020-12-03 16:55:20 -07001070 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001071 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1072
Yangbo Lu9fed28d2019-10-31 18:54:24 +08001073 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1074 return 1;
1075
Simon Glass407025d2017-07-29 11:35:24 -06001076 return esdhc_getcd_common(priv);
1077}
1078
1079static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1080 struct mmc_data *data)
1081{
Simon Glassfa20e932020-12-03 16:55:20 -07001082 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001083 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1084
1085 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1086}
1087
1088static int fsl_esdhc_set_ios(struct udevice *dev)
1089{
Simon Glassfa20e932020-12-03 16:55:20 -07001090 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001091 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1092
1093 return esdhc_set_ios_common(priv, &plat->mmc);
1094}
1095
Yangbo Lu76c74692020-09-01 16:58:00 +08001096static int fsl_esdhc_reinit(struct udevice *dev)
1097{
Simon Glassfa20e932020-12-03 16:55:20 -07001098 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu76c74692020-09-01 16:58:00 +08001099 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1100
1101 return esdhc_init_common(priv, &plat->mmc);
1102}
1103
Tom Rinidec7ea02024-05-20 13:35:03 -06001104#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Yangbo Lu73da9c82020-09-01 16:58:01 +08001105static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
1106{
Simon Glassfa20e932020-12-03 16:55:20 -07001107 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu73da9c82020-09-01 16:58:01 +08001108 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1109 struct fsl_esdhc *regs = priv->esdhc_regs;
Michael Walle148dc612021-03-17 15:01:36 +01001110 struct mmc *mmc = &plat->mmc;
Yangbo Lu73da9c82020-09-01 16:58:01 +08001111 u32 val, irqstaten;
1112 int i;
1113
Michael Walle148dc612021-03-17 15:01:36 +01001114 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
1115 plat->mmc.hs400_tuning)
1116 set_sysctl(priv, mmc, mmc->clock);
1117
Yangbo Lu73da9c82020-09-01 16:58:01 +08001118 esdhc_tuning_block_enable(priv, true);
1119 esdhc_setbits32(&regs->autoc12err, EXECUTE_TUNING);
1120
1121 irqstaten = esdhc_read32(&regs->irqstaten);
1122 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
1123
1124 for (i = 0; i < MAX_TUNING_LOOP; i++) {
Marek Vasutdad81fb2024-02-20 09:36:23 +01001125 mmc_send_tuning(mmc, opcode);
Yangbo Lu73da9c82020-09-01 16:58:01 +08001126 mdelay(1);
1127
1128 val = esdhc_read32(&regs->autoc12err);
1129 if (!(val & EXECUTE_TUNING)) {
1130 if (val & SMPCLKSEL)
1131 break;
1132 }
1133 }
1134
1135 esdhc_write32(&regs->irqstaten, irqstaten);
1136
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001137 if (i != MAX_TUNING_LOOP) {
1138 if (plat->mmc.hs400_tuning)
1139 esdhc_setbits32(&regs->sdtimingctl, FLW_CTL_BG);
Yangbo Lu73da9c82020-09-01 16:58:01 +08001140 return 0;
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001141 }
Yangbo Lu73da9c82020-09-01 16:58:01 +08001142
1143 printf("fsl_esdhc: tuning failed!\n");
1144 esdhc_clrbits32(&regs->autoc12err, SMPCLKSEL);
1145 esdhc_clrbits32(&regs->autoc12err, EXECUTE_TUNING);
1146 esdhc_tuning_block_enable(priv, false);
1147 return -ETIMEDOUT;
1148}
1149#endif
1150
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001151int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev)
1152{
1153 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1154
1155 esdhc_tuning_block_enable(priv, false);
1156 return 0;
1157}
1158
Stephen Carlson1822a972021-08-17 12:46:40 -07001159static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1160 int timeout_us)
1161{
1162 int ret;
1163 u32 tmp;
1164 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1165 struct fsl_esdhc *regs = priv->esdhc_regs;
1166
1167 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1168 !!(tmp & PRSSTAT_DAT0) == !!state,
1169 timeout_us);
1170 return ret;
1171}
1172
Simon Glass407025d2017-07-29 11:35:24 -06001173static const struct dm_mmc_ops fsl_esdhc_ops = {
1174 .get_cd = fsl_esdhc_get_cd,
1175 .send_cmd = fsl_esdhc_send_cmd,
1176 .set_ios = fsl_esdhc_set_ios,
Tom Rinidec7ea02024-05-20 13:35:03 -06001177#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001178 .execute_tuning = fsl_esdhc_execute_tuning,
1179#endif
Yangbo Lu76c74692020-09-01 16:58:00 +08001180 .reinit = fsl_esdhc_reinit,
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001181 .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr,
Stephen Carlson1822a972021-08-17 12:46:40 -07001182 .wait_dat0 = fsl_esdhc_wait_dat0,
Simon Glass407025d2017-07-29 11:35:24 -06001183};
Simon Glass407025d2017-07-29 11:35:24 -06001184
Peng Fana4d36f72016-03-25 14:16:56 +08001185static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lu2a99b602016-12-07 11:54:31 +08001186 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001187 { /* sentinel */ }
1188};
1189
Simon Glass407025d2017-07-29 11:35:24 -06001190static int fsl_esdhc_bind(struct udevice *dev)
1191{
Simon Glassfa20e932020-12-03 16:55:20 -07001192 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001193
1194 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1195}
Simon Glass407025d2017-07-29 11:35:24 -06001196
Peng Fana4d36f72016-03-25 14:16:56 +08001197U_BOOT_DRIVER(fsl_esdhc) = {
1198 .name = "fsl-esdhc-mmc",
1199 .id = UCLASS_MMC,
1200 .of_match = fsl_esdhc_ids,
Simon Glass407025d2017-07-29 11:35:24 -06001201 .ops = &fsl_esdhc_ops,
Simon Glass407025d2017-07-29 11:35:24 -06001202 .bind = fsl_esdhc_bind,
Peng Fana4d36f72016-03-25 14:16:56 +08001203 .probe = fsl_esdhc_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -07001204 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001205 .priv_auto = sizeof(struct fsl_esdhc_priv),
Peng Fana4d36f72016-03-25 14:16:56 +08001206};
1207#endif