blob: b49a7b425b9a01c64d37cd8747b5c68c05585727 [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>
13#include <common.h>
14#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070015#include <cpu_func.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090016#include <errno.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040017#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050018#include <mmc.h>
19#include <part.h>
20#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040022#include <fdt_support.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050025#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080026#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070027#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060028#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060029#include <linux/delay.h>
Stephen Carlson1822a972021-08-17 12:46:40 -070030#include <linux/iopoll.h>
Michael Wallec9bba2e2020-09-23 12:42:48 +020031#include <linux/dma-mapping.h>
Michael Walle081d4012020-10-12 10:07:14 +020032#include <sdhci.h>
Tom Rinibdd47f32022-06-16 14:04:38 -040033#include "../../board/freescale/common/qixis.h"
Andy Fleminge52ffb82008-10-30 16:47:16 -050034
Andy Fleminge52ffb82008-10-30 16:47:16 -050035DECLARE_GLOBAL_DATA_PTR;
36
37struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080038 uint dsaddr; /* SDMA system address register */
39 uint blkattr; /* Block attributes register */
40 uint cmdarg; /* Command argument register */
41 uint xfertyp; /* Transfer type register */
42 uint cmdrsp0; /* Command response 0 register */
43 uint cmdrsp1; /* Command response 1 register */
44 uint cmdrsp2; /* Command response 2 register */
45 uint cmdrsp3; /* Command response 3 register */
46 uint datport; /* Buffer data port register */
47 uint prsstat; /* Present state register */
48 uint proctl; /* Protocol control register */
49 uint sysctl; /* System Control Register */
50 uint irqstat; /* Interrupt status register */
51 uint irqstaten; /* Interrupt status enable register */
52 uint irqsigen; /* Interrupt signal enable register */
53 uint autoc12err; /* Auto CMD error status register */
54 uint hostcapblt; /* Host controller capabilities register */
55 uint wml; /* Watermark level register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080056 char reserved1[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080057 uint fevt; /* Force event register */
58 uint admaes; /* ADMA error status register */
Michael Walle081d4012020-10-12 10:07:14 +020059 uint adsaddrl; /* ADMA system address low register */
60 uint adsaddrh; /* ADMA system address high register */
61 char reserved2[156];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080062 uint hostver; /* Host controller version register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080063 char reserved3[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080064 uint dmaerraddr; /* DMA error address register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080065 char reserved4[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080066 uint dmaerrattr; /* DMA error attribute register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080067 char reserved5[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080068 uint hostcapblt2; /* Host controller capabilities register 2 */
Yangbo Lu73da9c82020-09-01 16:58:01 +080069 char reserved6[8]; /* reserved */
70 uint tbctl; /* Tuning block control register */
Yangbo Lu8f9ace12020-09-01 16:58:05 +080071 char reserved7[32]; /* reserved */
72 uint sdclkctl; /* SD clock control register */
73 uint sdtimingctl; /* SD timing control register */
74 char reserved8[20]; /* reserved */
75 uint dllcfg0; /* DLL config 0 register */
Michael Walle7259dc52021-03-17 15:01:37 +010076 uint dllcfg1; /* DLL config 1 register */
77 char reserved9[8]; /* reserved */
Yangbo Lu8fbe95b2020-10-20 11:04:52 +080078 uint dllstat0; /* DLL status 0 register */
79 char reserved10[664];/* reserved */
Yangbo Lu62b56b32019-06-21 11:42:29 +080080 uint esdhcctl; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050081};
82
Simon Glassfa02ca52017-07-29 11:35:21 -060083struct fsl_esdhc_plat {
84 struct mmc_config cfg;
85 struct mmc mmc;
86};
87
Peng Fana4d36f72016-03-25 14:16:56 +080088/**
89 * struct fsl_esdhc_priv
90 *
91 * @esdhc_regs: registers of the sdhc controller
92 * @sdhc_clk: Current clk of the sdhc controller
93 * @bus_width: bus width, 1bit, 4bit or 8bit
94 * @cfg: mmc config
95 * @mmc: mmc
96 * Following is used when Driver Model is enabled for MMC
97 * @dev: pointer for the device
Peng Fana4d36f72016-03-25 14:16:56 +080098 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080099 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +0800100 */
101struct fsl_esdhc_priv {
102 struct fsl_esdhc *esdhc_regs;
103 unsigned int sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800104 bool is_sdhc_per_clk;
Peng Fanc4142702018-01-21 19:00:24 +0800105 unsigned int clock;
Yangbo Lu77f26322019-10-21 18:09:07 +0800106#if !CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +0800107 struct mmc *mmc;
Simon Glass407025d2017-07-29 11:35:24 -0600108#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800109 struct udevice *dev;
Michael Walle081d4012020-10-12 10:07:14 +0200110 struct sdhci_adma_desc *adma_desc_table;
Michael Wallec9bba2e2020-09-23 12:42:48 +0200111 dma_addr_t dma_addr;
Peng Fana4d36f72016-03-25 14:16:56 +0800112};
113
Andy Fleminge52ffb82008-10-30 16:47:16 -0500114/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000115static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500116{
117 uint xfertyp = 0;
118
119 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530120 xfertyp |= XFERTYP_DPSEL;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200121 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
122 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
Yangbo Lu73da9c82020-09-01 16:58:01 +0800123 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
124 xfertyp |= XFERTYP_DMAEN;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500125 if (data->blocks > 1) {
126 xfertyp |= XFERTYP_MSBSEL;
127 xfertyp |= XFERTYP_BCEN;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200128 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
129 xfertyp |= XFERTYP_AC12EN;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500130 }
131
132 if (data->flags & MMC_DATA_READ)
133 xfertyp |= XFERTYP_DTDSEL;
134 }
135
136 if (cmd->resp_type & MMC_RSP_CRC)
137 xfertyp |= XFERTYP_CCCEN;
138 if (cmd->resp_type & MMC_RSP_OPCODE)
139 xfertyp |= XFERTYP_CICEN;
140 if (cmd->resp_type & MMC_RSP_136)
141 xfertyp |= XFERTYP_RSPTYP_136;
142 else if (cmd->resp_type & MMC_RSP_BUSY)
143 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
144 else if (cmd->resp_type & MMC_RSP_PRESENT)
145 xfertyp |= XFERTYP_RSPTYP_48;
146
Jason Liubef0ff02011-03-22 01:32:31 +0000147 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
148 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800149
Andy Fleminge52ffb82008-10-30 16:47:16 -0500150 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
151}
152
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530153/*
154 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
155 */
Simon Glass1d177d42017-07-29 11:35:17 -0600156static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
157 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530158{
Peng Fana4d36f72016-03-25 14:16:56 +0800159 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530160 uint blocks;
161 char *buffer;
162 uint databuf;
163 uint size;
164 uint irqstat;
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100165 ulong start;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530166
167 if (data->flags & MMC_DATA_READ) {
168 blocks = data->blocks;
169 buffer = data->dest;
170 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100171 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530172 size = data->blocksize;
173 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100174 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
175 if (get_timer(start) > PIO_TIMEOUT) {
176 printf("\nData Read Failed in PIO Mode.");
177 return;
178 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530179 }
180 while (size && (!(irqstat & IRQSTAT_TC))) {
181 udelay(100); /* Wait before last byte transfer complete */
182 irqstat = esdhc_read32(&regs->irqstat);
183 databuf = in_le32(&regs->datport);
184 *((uint *)buffer) = databuf;
185 buffer += 4;
186 size -= 4;
187 }
188 blocks--;
189 }
190 } else {
191 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200192 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530193 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100194 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530195 size = data->blocksize;
196 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100197 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
198 if (get_timer(start) > PIO_TIMEOUT) {
199 printf("\nData Write Failed in PIO Mode.");
200 return;
201 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530202 }
203 while (size && (!(irqstat & IRQSTAT_TC))) {
204 udelay(100); /* Wait before last byte transfer complete */
205 databuf = *((uint *)buffer);
206 buffer += 4;
207 size -= 4;
208 irqstat = esdhc_read32(&regs->irqstat);
209 out_le32(&regs->datport, databuf);
210 }
211 blocks--;
212 }
213 }
214}
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530215
Michael Wallebdd413f2020-09-23 12:42:49 +0200216static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
217 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500218{
Peng Fana4d36f72016-03-25 14:16:56 +0800219 struct fsl_esdhc *regs = priv->esdhc_regs;
Michael Wallebdd413f2020-09-23 12:42:49 +0200220 uint wml_value = data->blocksize / 4;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500221
222 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530223 if (wml_value > WML_RD_WML_MAX)
224 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500225
Roy Zange5853af2010-02-09 18:23:33 +0800226 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500227 } else {
Priyanka Jain02449632011-02-09 09:24:10 +0530228 if (wml_value > WML_WR_WML_MAX)
229 wml_value = WML_WR_WML_MAX_VAL;
Yangbo Luf3bcc832019-10-31 18:54:25 +0800230
Roy Zange5853af2010-02-09 18:23:33 +0800231 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
Michael Wallebdd413f2020-09-23 12:42:49 +0200232 wml_value << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500233 }
Michael Wallebdd413f2020-09-23 12:42:49 +0200234}
Michael Wallebdd413f2020-09-23 12:42:49 +0200235
236static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
237{
238 uint trans_bytes = data->blocksize * data->blocks;
239 struct fsl_esdhc *regs = priv->esdhc_regs;
Michael Walle081d4012020-10-12 10:07:14 +0200240 phys_addr_t adma_addr;
Michael Wallebdd413f2020-09-23 12:42:49 +0200241 void *buf;
242
243 if (data->flags & MMC_DATA_WRITE)
244 buf = (void *)data->src;
245 else
246 buf = data->dest;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500247
Michael Wallebdd413f2020-09-23 12:42:49 +0200248 priv->dma_addr = dma_map_single(buf, trans_bytes,
249 mmc_get_dma_dir(data));
Michael Walle081d4012020-10-12 10:07:14 +0200250
251 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) &&
252 priv->adma_desc_table) {
253 debug("Using ADMA2\n");
254 /* prefer ADMA2 if it is available */
255 sdhci_prepare_adma_table(priv->adma_desc_table, data,
256 priv->dma_addr);
257
258 adma_addr = virt_to_phys(priv->adma_desc_table);
259 esdhc_write32(&regs->adsaddrl, lower_32_bits(adma_addr));
260 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT))
261 esdhc_write32(&regs->adsaddrh, upper_32_bits(adma_addr));
262 esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
263 PROCTL_DMAS_ADMA2);
264 } else {
265 debug("Using SDMA\n");
266 if (upper_32_bits(priv->dma_addr))
267 printf("Cannot use 64 bit addresses with SDMA\n");
268 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
269 esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
270 PROCTL_DMAS_SDMA);
271 }
272
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100273 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Michael Wallebdd413f2020-09-23 12:42:49 +0200274}
275
276static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
277 struct mmc_data *data)
278{
279 int timeout;
280 bool is_write = data->flags & MMC_DATA_WRITE;
281 struct fsl_esdhc *regs = priv->esdhc_regs;
282
283 if (is_write && !(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
284 printf("Can not write to locked SD card.\n");
285 return -EINVAL;
286 }
287
Michael Wallebc9e13e2020-10-12 10:07:13 +0200288 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
289 esdhc_setup_watermark_level(priv, data);
290 else
291 esdhc_setup_dma(priv, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500292
293 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530294 /*
295 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
296 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
297 * So, Number of SD Clock cycles for 0.25sec should be minimum
298 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500299 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530300 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500301 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530302 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500303 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530304 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500305 * => timeout + 13 = log2(mmc->clock/4) + 1
306 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800307 *
308 * However, the MMC spec "It is strongly recommended for hosts to
309 * implement more than 500ms timeout value even if the card
310 * indicates the 250ms maximum busy length." Even the previous
311 * value of 300ms is known to be insufficient for some cards.
312 * So, we use
313 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530314 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800315 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500316 timeout -= 13;
317
318 if (timeout > 14)
319 timeout = 14;
320
321 if (timeout < 0)
322 timeout = 0;
323
Michael Wallebc9e13e2020-10-12 10:07:13 +0200324 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
325 (timeout == 4 || timeout == 8 || timeout == 12))
Kumar Gala9a878d52011-01-29 15:36:10 -0600326 timeout++;
Kumar Gala9a878d52011-01-29 15:36:10 -0600327
Michael Wallebc9e13e2020-10-12 10:07:13 +0200328 if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
329 timeout = 0xE;
330
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100331 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500332
333 return 0;
334}
335
Andy Fleminge52ffb82008-10-30 16:47:16 -0500336/*
337 * Sends a command out on the bus. Takes the mmc pointer,
338 * a command pointer, and an optional data pointer.
339 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600340static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
341 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500342{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500343 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500344 uint xfertyp;
345 uint irqstat;
Peng Fanc4142702018-01-21 19:00:24 +0800346 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fana4d36f72016-03-25 14:16:56 +0800347 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam7300ef52018-11-19 10:31:53 -0200348 unsigned long start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500349
Michael Wallebc9e13e2020-10-12 10:07:13 +0200350 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
351 cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
Jerry Huanged413672011-01-06 23:42:19 -0600352 return 0;
Jerry Huanged413672011-01-06 23:42:19 -0600353
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100354 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500355
356 sync();
357
358 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100359 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
360 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
361 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500362
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100363 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
364 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500365
Andy Fleminge52ffb82008-10-30 16:47:16 -0500366 /* Set up for a data transfer if we have one */
367 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600368 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500369 if(err)
370 return err;
371 }
372
373 /* Figure out the transfer arguments */
374 xfertyp = esdhc_xfertyp(cmd, data);
375
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500376 /* Mask all irqs */
377 esdhc_write32(&regs->irqsigen, 0);
378
Andy Fleminge52ffb82008-10-30 16:47:16 -0500379 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100380 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
381 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behmed8552d62012-03-26 03:13:05 +0000382
Yangbo Lu73da9c82020-09-01 16:58:01 +0800383 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
384 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
385 flags = IRQSTAT_BRR;
386
Andy Fleminge52ffb82008-10-30 16:47:16 -0500387 /* Wait for the command to complete */
Fabio Estevam7300ef52018-11-19 10:31:53 -0200388 start = get_timer(0);
389 while (!(esdhc_read32(&regs->irqstat) & flags)) {
390 if (get_timer(start) > 1000) {
391 err = -ETIMEDOUT;
392 goto out;
393 }
394 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500395
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100396 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500397
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500398 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900399 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500400 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000401 }
402
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500403 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900404 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500405 goto out;
406 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500407
Dirk Behmed8552d62012-03-26 03:13:05 +0000408 /* Workaround for ESDHC errata ENGcm03648 */
409 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800410 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000411
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800412 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000413 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
414 PRSSTAT_DAT0)) {
415 udelay(100);
416 timeout--;
417 }
418
419 if (timeout <= 0) {
420 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900421 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500422 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000423 }
424 }
425
Andy Fleminge52ffb82008-10-30 16:47:16 -0500426 /* Copy the response to the response buffer */
427 if (cmd->resp_type & MMC_RSP_136) {
428 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
429
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100430 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
431 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
432 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
433 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530434 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
435 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
436 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
437 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500438 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100439 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500440
441 /* Wait until all of the blocks are transferred */
442 if (data) {
Michael Wallebc9e13e2020-10-12 10:07:13 +0200443 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
444 esdhc_pio_read_write(priv, data);
445 } else {
446 flags = DATA_COMPLETE;
447 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
448 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
449 flags = IRQSTAT_BRR;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800450
Michael Wallebc9e13e2020-10-12 10:07:13 +0200451 do {
452 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500453
Michael Wallebc9e13e2020-10-12 10:07:13 +0200454 if (irqstat & IRQSTAT_DTOE) {
455 err = -ETIMEDOUT;
456 goto out;
457 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000458
Michael Wallebc9e13e2020-10-12 10:07:13 +0200459 if (irqstat & DATA_ERR) {
460 err = -ECOMM;
461 goto out;
462 }
463 } while ((irqstat & flags) != flags);
Ye.Li33a56b12014-02-20 18:00:57 +0800464
Michael Wallebc9e13e2020-10-12 10:07:13 +0200465 /*
466 * Need invalidate the dcache here again to avoid any
467 * cache-fill during the DMA operations such as the
468 * speculative pre-fetching etc.
469 */
470 dma_unmap_single(priv->dma_addr,
471 data->blocks * data->blocksize,
472 mmc_get_dma_dir(data));
473 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500474 }
475
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500476out:
477 /* Reset CMD and DATA portions on error */
478 if (err) {
479 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
480 SYSCTL_RSTC);
481 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
482 ;
483
484 if (data) {
485 esdhc_write32(&regs->sysctl,
486 esdhc_read32(&regs->sysctl) |
487 SYSCTL_RSTD);
488 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
489 ;
490 }
491 }
492
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100493 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500494
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500495 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500496}
497
Simon Glass1d177d42017-07-29 11:35:17 -0600498static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500499{
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100500 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200501 int div = 1;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200502 int pre_div = 2;
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800503 unsigned int sdhc_clk = priv->sdhc_clk;
504 u32 time_out;
505 u32 value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500506 uint clk;
507
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
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800547 time_out = 20;
548 value = PRSSTAT_SDSTB;
549 while (!(esdhc_read32(&regs->prsstat) & value)) {
550 if (time_out == 0) {
551 printf("fsl_esdhc: Internal clock never stabilised.\n");
552 break;
553 }
554 time_out--;
555 mdelay(1);
556 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500557
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700558 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500559}
560
Simon Glass1d177d42017-07-29 11:35:17 -0600561static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800562{
Peng Fana4d36f72016-03-25 14:16:56 +0800563 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800564 u32 value;
565 u32 time_out;
566
567 value = esdhc_read32(&regs->sysctl);
568
569 if (enable)
570 value |= SYSCTL_CKEN;
571 else
572 value &= ~SYSCTL_CKEN;
573
574 esdhc_write32(&regs->sysctl, value);
575
576 time_out = 20;
577 value = PRSSTAT_SDSTB;
578 while (!(esdhc_read32(&regs->prsstat) & value)) {
579 if (time_out == 0) {
580 printf("fsl_esdhc: Internal clock never stabilised.\n");
581 break;
582 }
583 time_out--;
584 mdelay(1);
585 }
Peng Fanc4142702018-01-21 19:00:24 +0800586}
Yangbo Lu163beec2015-04-22 13:57:40 +0800587
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800588static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv)
589{
590 struct fsl_esdhc *regs = priv->esdhc_regs;
591 u32 time_out;
592
593 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_FAF);
594
595 time_out = 20;
596 while (esdhc_read32(&regs->esdhcctl) & ESDHCCTL_FAF) {
597 if (time_out == 0) {
598 printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n");
599 break;
600 }
601 time_out--;
602 mdelay(1);
603 }
604}
605
606static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv,
607 bool en)
608{
609 struct fsl_esdhc *regs = priv->esdhc_regs;
610
611 esdhc_clock_control(priv, false);
612 esdhc_flush_async_fifo(priv);
613 if (en)
614 esdhc_setbits32(&regs->tbctl, TBCTL_TB_EN);
615 else
616 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
617 esdhc_clock_control(priv, true);
618}
619
620static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv)
621{
622 struct fsl_esdhc *regs = priv->esdhc_regs;
623
624 esdhc_clrbits32(&regs->sdtimingctl, FLW_CTL_BG);
625 esdhc_clrbits32(&regs->sdclkctl, CMD_CLK_CTL);
626
627 esdhc_clock_control(priv, false);
628 esdhc_clrbits32(&regs->tbctl, HS400_MODE);
629 esdhc_clock_control(priv, true);
630
631 esdhc_clrbits32(&regs->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE);
632 esdhc_clrbits32(&regs->tbctl, HS400_WNDW_ADJUST);
633
634 esdhc_tuning_block_enable(priv, false);
635}
636
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800637static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode)
Yangbo Lu73da9c82020-09-01 16:58:01 +0800638{
639 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800640 ulong start;
641 u32 val;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800642
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800643 /* Exit HS400 mode before setting any other mode */
644 if (esdhc_read32(&regs->tbctl) & HS400_MODE &&
645 mode != MMC_HS_400)
646 esdhc_exit_hs400(priv);
647
Yangbo Lu73da9c82020-09-01 16:58:01 +0800648 esdhc_clock_control(priv, false);
649
650 if (mode == MMC_HS_200)
651 esdhc_clrsetbits32(&regs->autoc12err, UHSM_MASK,
652 UHSM_SDR104_HS200);
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800653 if (mode == MMC_HS_400) {
654 esdhc_setbits32(&regs->tbctl, HS400_MODE);
655 esdhc_setbits32(&regs->sdclkctl, CMD_CLK_CTL);
656 esdhc_clock_control(priv, true);
Yangbo Lu73da9c82020-09-01 16:58:01 +0800657
Yangbo Lu9ac60a42020-09-01 16:58:07 +0800658 if (priv->clock == 200000000)
659 esdhc_setbits32(&regs->dllcfg0, DLL_FREQ_SEL);
660
661 esdhc_setbits32(&regs->dllcfg0, DLL_ENABLE);
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800662
663 esdhc_setbits32(&regs->dllcfg0, DLL_RESET);
664 udelay(1);
665 esdhc_clrbits32(&regs->dllcfg0, DLL_RESET);
666
667 start = get_timer(0);
668 val = DLL_STS_SLV_LOCK;
669 while (!(esdhc_read32(&regs->dllstat0) & val)) {
670 if (get_timer(start) > 1000) {
671 printf("fsl_esdhc: delay chain lock timeout\n");
672 return -ETIMEDOUT;
673 }
674 }
675
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800676 esdhc_setbits32(&regs->tbctl, HS400_WNDW_ADJUST);
677
678 esdhc_clock_control(priv, false);
679 esdhc_flush_async_fifo(priv);
680 }
Yangbo Lu73da9c82020-09-01 16:58:01 +0800681 esdhc_clock_control(priv, true);
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800682 return 0;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800683}
684
Simon Glass6aa55dc2017-07-29 11:35:18 -0600685static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500686{
Peng Fana4d36f72016-03-25 14:16:56 +0800687 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800688 int ret;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500689
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800690 if (priv->is_sdhc_per_clk) {
691 /* Select to use peripheral clock */
692 esdhc_clock_control(priv, false);
693 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
694 esdhc_clock_control(priv, true);
695 }
696
Yangbo Lu8f9ace12020-09-01 16:58:05 +0800697 if (mmc->selected_mode == MMC_HS_400)
698 esdhc_tuning_block_enable(priv, true);
699
Andy Fleminge52ffb82008-10-30 16:47:16 -0500700 /* Set the clock speed */
Peng Fanc4142702018-01-21 19:00:24 +0800701 if (priv->clock != mmc->clock)
702 set_sysctl(priv, mmc, mmc->clock);
703
Yangbo Lu73da9c82020-09-01 16:58:01 +0800704 /* Set timing */
Yangbo Lu8fbe95b2020-10-20 11:04:52 +0800705 ret = esdhc_set_timing(priv, mmc->selected_mode);
706 if (ret)
707 return ret;
Yangbo Lu73da9c82020-09-01 16:58:01 +0800708
Andy Fleminge52ffb82008-10-30 16:47:16 -0500709 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100710 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500711
712 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100713 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500714 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100715 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
716
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900717 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500718}
719
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000720static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
721{
722#ifdef CONFIG_ARCH_MPC830X
723 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
724 sysconf83xx_t *sysconf = &immr->sysconf;
725
726 setbits_be32(&sysconf->sdhccr, 0x02000000);
727#else
Pali Rohár6c6cec32022-04-04 18:32:13 +0200728 esdhc_write32(&regs->esdhcctl, ESDHCCTL_SNOOP);
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000729#endif
730}
731
Simon Glass6aa55dc2017-07-29 11:35:18 -0600732static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500733{
Peng Fana4d36f72016-03-25 14:16:56 +0800734 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass0c3ef222017-07-29 11:35:20 -0600735 ulong start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500736
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100737 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200738 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100739
740 /* Wait until the controller is available */
Simon Glass0c3ef222017-07-29 11:35:20 -0600741 start = get_timer(0);
742 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
743 if (get_timer(start) > 1000)
744 return -ETIMEDOUT;
745 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500746
Yangbo Lu573859c2020-09-01 16:58:02 +0800747 /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */
748 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
749
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000750 esdhc_enable_cache_snooping(regs);
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530751
Dirk Behmedbe67252013-07-15 15:44:29 +0200752 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500753
754 /* Set the initial clock speed */
Yangbo Luee2708b2020-10-20 11:04:51 +0800755 set_sysctl(priv, mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500756
757 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100758 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500759
760 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100761 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500762
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100763 /* Set timout to the maximum value */
764 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500765
Michael Walle7259dc52021-03-17 15:01:37 +0100766 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_UNRELIABLE_PULSE_DETECTION_WORKAROUND))
767 esdhc_clrbits32(&regs->dllcfg1, DLL_PD_PULSE_STRETCH_SEL);
768
Thierry Reding8cee4c982012-01-02 01:15:38 +0000769 return 0;
770}
771
Simon Glass6aa55dc2017-07-29 11:35:18 -0600772static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Reding8cee4c982012-01-02 01:15:38 +0000773{
Peng Fana4d36f72016-03-25 14:16:56 +0800774 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500775
Haijun.Zhang05f58542014-01-10 13:52:17 +0800776#ifdef CONFIG_ESDHC_DETECT_QUIRK
Tom Rinibdd47f32022-06-16 14:04:38 -0400777 if (qixis_esdhc_detect_quirk())
Haijun.Zhang05f58542014-01-10 13:52:17 +0800778 return 1;
779#endif
Yangbo Lu8abc0432020-05-19 11:06:43 +0800780 if (esdhc_read32(&regs->prsstat) & PRSSTAT_CINS)
781 return 1;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100782
Yangbo Lu8abc0432020-05-19 11:06:43 +0800783 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500784}
785
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800786static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
787 struct mmc_config *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500788{
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800789 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu63267b42019-10-31 18:54:21 +0800790 u32 caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500791
Wang Huanc9292132014-09-05 13:52:40 +0800792 caps = esdhc_read32(&regs->hostcapblt);
Yangbo Lue087cd62021-06-03 10:51:17 +0800793
794 /*
795 * For eSDHC, power supply is through peripheral circuit. Some eSDHC
796 * versions have value 0 of the bit but that does not reflect the
797 * truth. 3.3V is common for SD/MMC, and is supported for all boards
798 * with eSDHC in current u-boot. So, make 3.3V is supported in
799 * default in code. CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT can be enabled
800 * if future board does not support 3.3V.
801 */
802 caps |= HOSTCAPBLT_VS33;
803 if (IS_ENABLED(CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT))
804 caps &= ~HOSTCAPBLT_VS33;
805
Michael Wallebc9e13e2020-10-12 10:07:13 +0200806 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
807 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Yangbo Lu63267b42019-10-31 18:54:21 +0800808 if (caps & HOSTCAPBLT_VS18)
809 cfg->voltages |= MMC_VDD_165_195;
810 if (caps & HOSTCAPBLT_VS30)
811 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
812 if (caps & HOSTCAPBLT_VS33)
813 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000814
Simon Glassfa02ca52017-07-29 11:35:21 -0600815 cfg->name = "FSL_SDHC";
Abbas Razae6bf9772013-03-25 09:13:34 +0000816
Yangbo Lu63267b42019-10-31 18:54:21 +0800817 if (caps & HOSTCAPBLT_HSS)
Simon Glassfa02ca52017-07-29 11:35:21 -0600818 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500819
Simon Glassfa02ca52017-07-29 11:35:21 -0600820 cfg->f_min = 400000;
Peng Fanc4142702018-01-21 19:00:24 +0800821 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Simon Glassfa02ca52017-07-29 11:35:21 -0600822 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Peng Fana4d36f72016-03-25 14:16:56 +0800823}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400824
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100825#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800826__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400827{
Michael Wallebc9e13e2020-10-12 10:07:13 +0200828 if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800829 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800830 sizeof("disabled"), 1);
831 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400832 }
Michael Wallebc9e13e2020-10-12 10:07:13 +0200833
Yangbo Lud84139c2017-01-17 10:43:54 +0800834 return 0;
835}
836
Yangbo Luce884022020-05-19 11:06:44 +0800837
Michael Wallebc9e13e2020-10-12 10:07:13 +0200838#if CONFIG_IS_ENABLED(DM_MMC)
839static int fsl_esdhc_get_cd(struct udevice *dev);
Yangbo Luce884022020-05-19 11:06:44 +0800840static void esdhc_disable_for_no_card(void *blob)
841{
842 struct udevice *dev;
843
844 for (uclass_first_device(UCLASS_MMC, &dev);
845 dev;
846 uclass_next_device(&dev)) {
847 char esdhc_path[50];
848
849 if (fsl_esdhc_get_cd(dev))
850 continue;
851
852 snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
853 (unsigned long)dev_read_addr(dev));
854 do_fixup_by_path(blob, esdhc_path, "status", "disabled",
855 sizeof("disabled"), 1);
856 }
857}
Michael Wallebc9e13e2020-10-12 10:07:13 +0200858#else
859static void esdhc_disable_for_no_card(void *blob)
860{
861}
Yangbo Luce884022020-05-19 11:06:44 +0800862#endif
863
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900864void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lud84139c2017-01-17 10:43:54 +0800865{
866 const char *compat = "fsl,esdhc";
867
868 if (esdhc_status_fixup(blob, compat))
869 return;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200870
871 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND))
872 esdhc_disable_for_no_card(blob);
873
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400874 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000875 gd->arch.sdhc_clk, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400876}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100877#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800878
Yangbo Lu4fc93332019-10-31 18:54:26 +0800879#if !CONFIG_IS_ENABLED(DM_MMC)
880static int esdhc_getcd(struct mmc *mmc)
881{
882 struct fsl_esdhc_priv *priv = mmc->priv;
883
884 return esdhc_getcd_common(priv);
885}
886
887static int esdhc_init(struct mmc *mmc)
888{
889 struct fsl_esdhc_priv *priv = mmc->priv;
890
891 return esdhc_init_common(priv, mmc);
892}
893
894static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
895 struct mmc_data *data)
896{
897 struct fsl_esdhc_priv *priv = mmc->priv;
898
899 return esdhc_send_cmd_common(priv, mmc, cmd, data);
900}
901
902static int esdhc_set_ios(struct mmc *mmc)
903{
904 struct fsl_esdhc_priv *priv = mmc->priv;
905
906 return esdhc_set_ios_common(priv, mmc);
907}
908
909static const struct mmc_ops esdhc_ops = {
910 .getcd = esdhc_getcd,
911 .init = esdhc_init,
912 .send_cmd = esdhc_send_cmd,
913 .set_ios = esdhc_set_ios,
914};
915
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900916int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu4fc93332019-10-31 18:54:26 +0800917{
918 struct fsl_esdhc_plat *plat;
919 struct fsl_esdhc_priv *priv;
920 struct mmc_config *mmc_cfg;
921 struct mmc *mmc;
922
923 if (!cfg)
924 return -EINVAL;
925
926 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
927 if (!priv)
928 return -ENOMEM;
929 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
930 if (!plat) {
931 free(priv);
932 return -ENOMEM;
933 }
934
935 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
936 priv->sdhc_clk = cfg->sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800937 if (gd->arch.sdhc_per_clk)
938 priv->is_sdhc_per_clk = true;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800939
940 mmc_cfg = &plat->cfg;
941
942 if (cfg->max_bus_width == 8) {
943 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
944 MMC_MODE_8BIT;
945 } else if (cfg->max_bus_width == 4) {
946 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
947 } else if (cfg->max_bus_width == 1) {
948 mmc_cfg->host_caps |= MMC_MODE_1BIT;
949 } else {
Pali Rohár39a43b02022-05-11 20:27:12 +0200950 mmc_cfg->host_caps |= MMC_MODE_1BIT;
951 printf("No max bus width provided. Fallback to 1-bit mode.\n");
Yangbo Lu4fc93332019-10-31 18:54:26 +0800952 }
953
Michael Wallebc9e13e2020-10-12 10:07:13 +0200954 if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
Yangbo Lu4fc93332019-10-31 18:54:26 +0800955 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
Michael Wallebc9e13e2020-10-12 10:07:13 +0200956
Yangbo Lu4fc93332019-10-31 18:54:26 +0800957 mmc_cfg->ops = &esdhc_ops;
958
959 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
960
961 mmc = mmc_create(mmc_cfg, priv);
962 if (!mmc)
963 return -EIO;
964
965 priv->mmc = mmc;
966 return 0;
967}
968
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900969int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu4fc93332019-10-31 18:54:26 +0800970{
971 struct fsl_esdhc_cfg *cfg;
972
973 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
974 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Pali Rohár3a672d62022-05-11 20:27:13 +0200975 cfg->max_bus_width = CONFIG_SYS_FSL_ESDHC_DEFAULT_BUS_WIDTH;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800976 /* Prefer peripheral clock which provides higher frequency. */
977 if (gd->arch.sdhc_per_clk)
978 cfg->sdhc_clk = gd->arch.sdhc_per_clk;
979 else
980 cfg->sdhc_clk = gd->arch.sdhc_clk;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800981 return fsl_esdhc_initialize(bis, cfg);
982}
983#else /* DM_MMC */
Peng Fana4d36f72016-03-25 14:16:56 +0800984static int fsl_esdhc_probe(struct udevice *dev)
985{
986 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700987 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800988 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Michael Walle081d4012020-10-12 10:07:14 +0200989 u32 caps, hostver;
Peng Fana4d36f72016-03-25 14:16:56 +0800990 fdt_addr_t addr;
Simon Glass407025d2017-07-29 11:35:24 -0600991 struct mmc *mmc;
Yangbo Luce884022020-05-19 11:06:44 +0800992 int ret;
Peng Fana4d36f72016-03-25 14:16:56 +0800993
Simon Glass80e9df42017-07-29 11:35:23 -0600994 addr = dev_read_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800995 if (addr == FDT_ADDR_T_NONE)
996 return -EINVAL;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000997#ifdef CONFIG_PPC
998 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
999#else
Peng Fana4d36f72016-03-25 14:16:56 +08001000 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhu583d5e92019-04-11 11:01:50 +00001001#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001002 priv->dev = dev;
1003
Michael Walle081d4012020-10-12 10:07:14 +02001004 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) {
1005 /*
1006 * Only newer eSDHC controllers can do ADMA2 if the ADMA flag
1007 * is set in the host capabilities register.
1008 */
1009 caps = esdhc_read32(&priv->esdhc_regs->hostcapblt);
1010 hostver = esdhc_read32(&priv->esdhc_regs->hostver);
1011 if (caps & HOSTCAPBLT_DMAS &&
1012 HOSTVER_VENDOR(hostver) > VENDOR_V_22) {
1013 priv->adma_desc_table = sdhci_adma_init();
1014 if (!priv->adma_desc_table)
1015 debug("Could not allocate ADMA tables, falling back to SDMA\n");
1016 }
1017 }
1018
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +08001019 if (gd->arch.sdhc_per_clk) {
1020 priv->sdhc_clk = gd->arch.sdhc_per_clk;
1021 priv->is_sdhc_per_clk = true;
1022 } else {
1023 priv->sdhc_clk = gd->arch.sdhc_clk;
1024 }
1025
Yangbo Lub8626e42019-11-12 19:28:36 +08001026 if (priv->sdhc_clk <= 0) {
1027 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1028 return -EINVAL;
Peng Fana4d36f72016-03-25 14:16:56 +08001029 }
1030
Yangbo Lub64dc8d2019-10-31 18:54:23 +08001031 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
Peng Fana4d36f72016-03-25 14:16:56 +08001032
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001033 mmc_of_parse(dev, &plat->cfg);
1034
Simon Glass407025d2017-07-29 11:35:24 -06001035 mmc = &plat->mmc;
1036 mmc->cfg = &plat->cfg;
1037 mmc->dev = dev;
Yangbo Lu4cc119b2019-05-23 11:05:46 +08001038
Simon Glass407025d2017-07-29 11:35:24 -06001039 upriv->mmc = mmc;
Peng Fana4d36f72016-03-25 14:16:56 +08001040
Yangbo Luce884022020-05-19 11:06:44 +08001041 ret = esdhc_init_common(priv, mmc);
1042 if (ret)
1043 return ret;
1044
Michael Wallebc9e13e2020-10-12 10:07:13 +02001045 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) &&
1046 !fsl_esdhc_get_cd(dev))
Yangbo Luce884022020-05-19 11:06:44 +08001047 esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
Michael Wallebc9e13e2020-10-12 10:07:13 +02001048
Yangbo Luce884022020-05-19 11:06:44 +08001049 return 0;
Peng Fana4d36f72016-03-25 14:16:56 +08001050}
1051
Simon Glass407025d2017-07-29 11:35:24 -06001052static int fsl_esdhc_get_cd(struct udevice *dev)
1053{
Simon Glassfa20e932020-12-03 16:55:20 -07001054 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001055 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1056
Yangbo Lu9fed28d2019-10-31 18:54:24 +08001057 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1058 return 1;
1059
Simon Glass407025d2017-07-29 11:35:24 -06001060 return esdhc_getcd_common(priv);
1061}
1062
1063static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1064 struct mmc_data *data)
1065{
Simon Glassfa20e932020-12-03 16:55:20 -07001066 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001067 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1068
1069 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1070}
1071
1072static int fsl_esdhc_set_ios(struct udevice *dev)
1073{
Simon Glassfa20e932020-12-03 16:55:20 -07001074 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001075 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1076
1077 return esdhc_set_ios_common(priv, &plat->mmc);
1078}
1079
Yangbo Lu76c74692020-09-01 16:58:00 +08001080static int fsl_esdhc_reinit(struct udevice *dev)
1081{
Simon Glassfa20e932020-12-03 16:55:20 -07001082 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu76c74692020-09-01 16:58:00 +08001083 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1084
1085 return esdhc_init_common(priv, &plat->mmc);
1086}
1087
Yangbo Lu73da9c82020-09-01 16:58:01 +08001088#ifdef MMC_SUPPORTS_TUNING
Yangbo Lu73da9c82020-09-01 16:58:01 +08001089static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
1090{
Simon Glassfa20e932020-12-03 16:55:20 -07001091 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu73da9c82020-09-01 16:58:01 +08001092 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1093 struct fsl_esdhc *regs = priv->esdhc_regs;
Michael Walle148dc612021-03-17 15:01:36 +01001094 struct mmc *mmc = &plat->mmc;
Yangbo Lu73da9c82020-09-01 16:58:01 +08001095 u32 val, irqstaten;
1096 int i;
1097
Michael Walle148dc612021-03-17 15:01:36 +01001098 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
1099 plat->mmc.hs400_tuning)
1100 set_sysctl(priv, mmc, mmc->clock);
1101
Yangbo Lu73da9c82020-09-01 16:58:01 +08001102 esdhc_tuning_block_enable(priv, true);
1103 esdhc_setbits32(&regs->autoc12err, EXECUTE_TUNING);
1104
1105 irqstaten = esdhc_read32(&regs->irqstaten);
1106 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
1107
1108 for (i = 0; i < MAX_TUNING_LOOP; i++) {
Michael Walle148dc612021-03-17 15:01:36 +01001109 mmc_send_tuning(mmc, opcode, NULL);
Yangbo Lu73da9c82020-09-01 16:58:01 +08001110 mdelay(1);
1111
1112 val = esdhc_read32(&regs->autoc12err);
1113 if (!(val & EXECUTE_TUNING)) {
1114 if (val & SMPCLKSEL)
1115 break;
1116 }
1117 }
1118
1119 esdhc_write32(&regs->irqstaten, irqstaten);
1120
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001121 if (i != MAX_TUNING_LOOP) {
1122 if (plat->mmc.hs400_tuning)
1123 esdhc_setbits32(&regs->sdtimingctl, FLW_CTL_BG);
Yangbo Lu73da9c82020-09-01 16:58:01 +08001124 return 0;
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001125 }
Yangbo Lu73da9c82020-09-01 16:58:01 +08001126
1127 printf("fsl_esdhc: tuning failed!\n");
1128 esdhc_clrbits32(&regs->autoc12err, SMPCLKSEL);
1129 esdhc_clrbits32(&regs->autoc12err, EXECUTE_TUNING);
1130 esdhc_tuning_block_enable(priv, false);
1131 return -ETIMEDOUT;
1132}
1133#endif
1134
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001135int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev)
1136{
1137 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1138
1139 esdhc_tuning_block_enable(priv, false);
1140 return 0;
1141}
1142
Stephen Carlson1822a972021-08-17 12:46:40 -07001143static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1144 int timeout_us)
1145{
1146 int ret;
1147 u32 tmp;
1148 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1149 struct fsl_esdhc *regs = priv->esdhc_regs;
1150
1151 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1152 !!(tmp & PRSSTAT_DAT0) == !!state,
1153 timeout_us);
1154 return ret;
1155}
1156
Simon Glass407025d2017-07-29 11:35:24 -06001157static const struct dm_mmc_ops fsl_esdhc_ops = {
1158 .get_cd = fsl_esdhc_get_cd,
1159 .send_cmd = fsl_esdhc_send_cmd,
1160 .set_ios = fsl_esdhc_set_ios,
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001161#ifdef MMC_SUPPORTS_TUNING
1162 .execute_tuning = fsl_esdhc_execute_tuning,
1163#endif
Yangbo Lu76c74692020-09-01 16:58:00 +08001164 .reinit = fsl_esdhc_reinit,
Yangbo Lu8f9ace12020-09-01 16:58:05 +08001165 .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr,
Stephen Carlson1822a972021-08-17 12:46:40 -07001166 .wait_dat0 = fsl_esdhc_wait_dat0,
Simon Glass407025d2017-07-29 11:35:24 -06001167};
Simon Glass407025d2017-07-29 11:35:24 -06001168
Peng Fana4d36f72016-03-25 14:16:56 +08001169static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lu2a99b602016-12-07 11:54:31 +08001170 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001171 { /* sentinel */ }
1172};
1173
Simon Glass407025d2017-07-29 11:35:24 -06001174static int fsl_esdhc_bind(struct udevice *dev)
1175{
Simon Glassfa20e932020-12-03 16:55:20 -07001176 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Simon Glass407025d2017-07-29 11:35:24 -06001177
1178 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1179}
Simon Glass407025d2017-07-29 11:35:24 -06001180
Peng Fana4d36f72016-03-25 14:16:56 +08001181U_BOOT_DRIVER(fsl_esdhc) = {
1182 .name = "fsl-esdhc-mmc",
1183 .id = UCLASS_MMC,
1184 .of_match = fsl_esdhc_ids,
Simon Glass407025d2017-07-29 11:35:24 -06001185 .ops = &fsl_esdhc_ops,
Simon Glass407025d2017-07-29 11:35:24 -06001186 .bind = fsl_esdhc_bind,
Peng Fana4d36f72016-03-25 14:16:56 +08001187 .probe = fsl_esdhc_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -07001188 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001189 .priv_auto = sizeof(struct fsl_esdhc_priv),
Peng Fana4d36f72016-03-25 14:16:56 +08001190};
1191#endif