blob: 2da70663cbaa14ad6181631a4bf590f318b969e5 [file] [log] [blame]
Yangbo Lufa33d202019-06-21 11:42:27 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Yangbo Lub1d59862021-06-03 10:51:18 +08004 * Copyright 2019, 2021 NXP
Yangbo Lufa33d202019-06-21 11:42:27 +08005 * Andy Fleming
6 * Yangbo Lu <yangbo.lu@nxp.com>
7 *
8 * Based vaguely on the pxa mmc code:
9 * (C) Copyright 2003
10 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
11 */
12
13#include <config.h>
14#include <common.h>
15#include <command.h>
16#include <clk.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070017#include <cpu_func.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080018#include <errno.h>
19#include <hwconfig.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080021#include <mmc.h>
22#include <part.h>
Simon Glass90526e92020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glass61b29b82020-02-03 07:36:15 -070028#include <linux/err.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080029#include <power/regulator.h>
30#include <malloc.h>
31#include <fsl_esdhc_imx.h>
32#include <fdt_support.h>
33#include <asm/io.h>
34#include <dm.h>
35#include <asm-generic/gpio.h>
36#include <dm/pinctrl.h>
Walter Lozano23721772020-07-29 12:31:17 -030037#include <dt-structs.h>
38#include <mapmem.h>
39#include <dm/ofnode.h>
Haibo Chenf9c3a812020-09-01 15:34:06 +080040#include <linux/iopoll.h>
Sean Anderson01672672021-11-23 15:03:43 -050041#include <linux/dma-mapping.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080042
Haibo Chen0ba116a2021-02-19 11:25:32 -080043#ifndef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
44#ifdef CONFIG_FSL_USDHC
45#define ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 1
46#endif
47#endif
48
Yangbo Lufa33d202019-06-21 11:42:27 +080049DECLARE_GLOBAL_DATA_PTR;
50
51#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
52 IRQSTATEN_CINT | \
53 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
54 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
55 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
56 IRQSTATEN_DINT)
57#define MAX_TUNING_LOOP 40
Yangbo Lufa33d202019-06-21 11:42:27 +080058
59struct fsl_esdhc {
60 uint dsaddr; /* SDMA system address register */
61 uint blkattr; /* Block attributes register */
62 uint cmdarg; /* Command argument register */
63 uint xfertyp; /* Transfer type register */
64 uint cmdrsp0; /* Command response 0 register */
65 uint cmdrsp1; /* Command response 1 register */
66 uint cmdrsp2; /* Command response 2 register */
67 uint cmdrsp3; /* Command response 3 register */
68 uint datport; /* Buffer data port register */
69 uint prsstat; /* Present state register */
70 uint proctl; /* Protocol control register */
71 uint sysctl; /* System Control Register */
72 uint irqstat; /* Interrupt status register */
73 uint irqstaten; /* Interrupt status enable register */
74 uint irqsigen; /* Interrupt signal enable register */
75 uint autoc12err; /* Auto CMD error status register */
76 uint hostcapblt; /* Host controller capabilities register */
77 uint wml; /* Watermark level register */
78 uint mixctrl; /* For USDHC */
79 char reserved1[4]; /* reserved */
80 uint fevt; /* Force event register */
81 uint admaes; /* ADMA error status register */
82 uint adsaddr; /* ADMA system address register */
83 char reserved2[4];
84 uint dllctrl;
85 uint dllstat;
86 uint clktunectrlstatus;
87 char reserved3[4];
88 uint strobe_dllctrl;
89 uint strobe_dllstat;
90 char reserved4[72];
91 uint vendorspec;
92 uint mmcboot;
93 uint vendorspec2;
Giulio Benetti6a63a872020-01-10 15:51:46 +010094 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lufa33d202019-06-21 11:42:27 +080095 char reserved5[44];
96 uint hostver; /* Host controller version register */
97 char reserved6[4]; /* reserved */
98 uint dmaerraddr; /* DMA error address register */
99 char reserved7[4]; /* reserved */
100 uint dmaerrattr; /* DMA error attribute register */
101 char reserved8[4]; /* reserved */
102 uint hostcapblt2; /* Host controller capabilities register 2 */
103 char reserved9[8]; /* reserved */
104 uint tcr; /* Tuning control register */
105 char reserved10[28]; /* reserved */
106 uint sddirctl; /* SD direction control register */
107 char reserved11[712];/* reserved */
108 uint scr; /* eSDHC control register */
109};
110
111struct fsl_esdhc_plat {
Walter Lozano23721772020-07-29 12:31:17 -0300112#if CONFIG_IS_ENABLED(OF_PLATDATA)
113 /* Put this first since driver model will copy the data here */
114 struct dtd_fsl_esdhc dtplat;
115#endif
116
Yangbo Lufa33d202019-06-21 11:42:27 +0800117 struct mmc_config cfg;
118 struct mmc mmc;
119};
120
121struct esdhc_soc_data {
122 u32 flags;
Yangbo Lufa33d202019-06-21 11:42:27 +0800123};
124
125/**
126 * struct fsl_esdhc_priv
127 *
128 * @esdhc_regs: registers of the sdhc controller
129 * @sdhc_clk: Current clk of the sdhc controller
Yangbo Lufa33d202019-06-21 11:42:27 +0800130 * @cfg: mmc config
131 * @mmc: mmc
132 * Following is used when Driver Model is enabled for MMC
133 * @dev: pointer for the device
Fabio Estevam29230f32020-01-06 20:11:27 -0300134 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lufa33d202019-06-21 11:42:27 +0800135 * @wp_enable: 1: enable checking wp; 0: no check
136 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
137 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
138 * @caps: controller capabilities
139 * @tuning_step: tuning step setting in tuning_ctrl register
140 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
141 * @strobe_dll_delay_target: settings in strobe_dllctrl
142 * @signal_voltage: indicating the current voltage
Haibo Chen8974ff12021-03-22 18:55:38 +0800143 * @signal_voltage_switch_extra_delay_ms: extra delay for IO voltage switch
Yangbo Lufa33d202019-06-21 11:42:27 +0800144 * @cd_gpio: gpio for card detection
145 * @wp_gpio: gpio for write protection
146 */
147struct fsl_esdhc_priv {
148 struct fsl_esdhc *esdhc_regs;
149 unsigned int sdhc_clk;
150 struct clk per_clk;
151 unsigned int clock;
152 unsigned int mode;
Sean Anderson297d2de2022-01-12 08:18:52 +0900153#if !CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +0800154 struct mmc *mmc;
155#endif
156 struct udevice *dev;
Fabio Estevam29230f32020-01-06 20:11:27 -0300157 int broken_cd;
Yangbo Lufa33d202019-06-21 11:42:27 +0800158 int wp_enable;
159 int vs18_enable;
160 u32 flags;
161 u32 caps;
162 u32 tuning_step;
163 u32 tuning_start_tap;
164 u32 strobe_dll_delay_target;
165 u32 signal_voltage;
Haibo Chen8974ff12021-03-22 18:55:38 +0800166 u32 signal_voltage_switch_extra_delay_ms;
Ye Li82771712019-07-11 03:29:02 +0000167#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800168 struct udevice *vqmmc_dev;
169 struct udevice *vmmc_dev;
170#endif
Simon Glassbcee8d62019-12-06 21:41:35 -0700171#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +0800172 struct gpio_desc cd_gpio;
173 struct gpio_desc wp_gpio;
174#endif
Sean Anderson01672672021-11-23 15:03:43 -0500175 dma_addr_t dma_addr;
Yangbo Lufa33d202019-06-21 11:42:27 +0800176};
177
178/* Return the XFERTYP flags for a given command and data packet */
179static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
180{
181 uint xfertyp = 0;
182
183 if (data) {
184 xfertyp |= XFERTYP_DPSEL;
Sean Anderson4f01db82021-11-23 15:03:45 -0500185 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
186 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
187 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
188 xfertyp |= XFERTYP_DMAEN;
Yangbo Lufa33d202019-06-21 11:42:27 +0800189 if (data->blocks > 1) {
190 xfertyp |= XFERTYP_MSBSEL;
191 xfertyp |= XFERTYP_BCEN;
Sean Anderson4f01db82021-11-23 15:03:45 -0500192 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
193 xfertyp |= XFERTYP_AC12EN;
Yangbo Lufa33d202019-06-21 11:42:27 +0800194 }
195
196 if (data->flags & MMC_DATA_READ)
197 xfertyp |= XFERTYP_DTDSEL;
198 }
199
200 if (cmd->resp_type & MMC_RSP_CRC)
201 xfertyp |= XFERTYP_CCCEN;
202 if (cmd->resp_type & MMC_RSP_OPCODE)
203 xfertyp |= XFERTYP_CICEN;
204 if (cmd->resp_type & MMC_RSP_136)
205 xfertyp |= XFERTYP_RSPTYP_136;
206 else if (cmd->resp_type & MMC_RSP_BUSY)
207 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
208 else if (cmd->resp_type & MMC_RSP_PRESENT)
209 xfertyp |= XFERTYP_RSPTYP_48;
210
211 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
212 xfertyp |= XFERTYP_CMDTYP_ABORT;
213
214 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
215}
216
Yangbo Lufa33d202019-06-21 11:42:27 +0800217/*
218 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
219 */
220static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
221 struct mmc_data *data)
222{
223 struct fsl_esdhc *regs = priv->esdhc_regs;
224 uint blocks;
225 char *buffer;
226 uint databuf;
227 uint size;
228 uint irqstat;
229 ulong start;
230
231 if (data->flags & MMC_DATA_READ) {
232 blocks = data->blocks;
233 buffer = data->dest;
234 while (blocks) {
235 start = get_timer(0);
236 size = data->blocksize;
237 irqstat = esdhc_read32(&regs->irqstat);
238 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
239 if (get_timer(start) > PIO_TIMEOUT) {
240 printf("\nData Read Failed in PIO Mode.");
241 return;
242 }
243 }
244 while (size && (!(irqstat & IRQSTAT_TC))) {
245 udelay(100); /* Wait before last byte transfer complete */
246 irqstat = esdhc_read32(&regs->irqstat);
247 databuf = in_le32(&regs->datport);
248 *((uint *)buffer) = databuf;
249 buffer += 4;
250 size -= 4;
251 }
252 blocks--;
253 }
254 } else {
255 blocks = data->blocks;
256 buffer = (char *)data->src;
257 while (blocks) {
258 start = get_timer(0);
259 size = data->blocksize;
260 irqstat = esdhc_read32(&regs->irqstat);
261 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
262 if (get_timer(start) > PIO_TIMEOUT) {
263 printf("\nData Write Failed in PIO Mode.");
264 return;
265 }
266 }
267 while (size && (!(irqstat & IRQSTAT_TC))) {
268 udelay(100); /* Wait before last byte transfer complete */
269 databuf = *((uint *)buffer);
270 buffer += 4;
271 size -= 4;
272 irqstat = esdhc_read32(&regs->irqstat);
273 out_le32(&regs->datport, databuf);
274 }
275 blocks--;
276 }
277 }
278}
Yangbo Lufa33d202019-06-21 11:42:27 +0800279
Sean Anderson41c6a222021-11-23 15:03:44 -0500280static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
281 struct mmc_data *data)
Yangbo Lufa33d202019-06-21 11:42:27 +0800282{
Yangbo Lufa33d202019-06-21 11:42:27 +0800283 struct fsl_esdhc *regs = priv->esdhc_regs;
Sean Anderson41c6a222021-11-23 15:03:44 -0500284 uint wml_value = data->blocksize / 4;
Yangbo Lufa33d202019-06-21 11:42:27 +0800285
286 if (data->flags & MMC_DATA_READ) {
287 if (wml_value > WML_RD_WML_MAX)
288 wml_value = WML_RD_WML_MAX_VAL;
289
290 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Yangbo Lufa33d202019-06-21 11:42:27 +0800291 } else {
Yangbo Lufa33d202019-06-21 11:42:27 +0800292 if (wml_value > WML_WR_WML_MAX)
293 wml_value = WML_WR_WML_MAX_VAL;
Sean Anderson41c6a222021-11-23 15:03:44 -0500294
295 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
296 wml_value << 16);
297 }
298}
Sean Anderson41c6a222021-11-23 15:03:44 -0500299
300static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
301{
302 uint trans_bytes = data->blocksize * data->blocks;
303 struct fsl_esdhc *regs = priv->esdhc_regs;
304 void *buf;
305
306 if (data->flags & MMC_DATA_WRITE)
307 buf = (void *)data->src;
308 else
309 buf = data->dest;
310
311 priv->dma_addr = dma_map_single(buf, trans_bytes,
312 mmc_get_dma_dir(data));
313 if (upper_32_bits(priv->dma_addr))
314 printf("Cannot use 64 bit addresses with SDMA\n");
315 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
316 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
317}
318
319static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
320 struct mmc_data *data)
321{
322 int timeout;
323 bool is_write = data->flags & MMC_DATA_WRITE;
324 struct fsl_esdhc *regs = priv->esdhc_regs;
325
326 if (is_write) {
327 if (priv->wp_enable && !(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
328 printf("Cannot write to locked SD card.\n");
329 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +0800330 } else {
Simon Glassbcee8d62019-12-06 21:41:35 -0700331#if CONFIG_IS_ENABLED(DM_GPIO)
332 if (dm_gpio_is_valid(&priv->wp_gpio) &&
333 dm_gpio_get_value(&priv->wp_gpio)) {
Sean Anderson41c6a222021-11-23 15:03:44 -0500334 printf("Cannot write to locked SD card.\n");
335 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +0800336 }
337#endif
338 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800339 }
340
Sean Anderson4f01db82021-11-23 15:03:45 -0500341 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
342 esdhc_setup_watermark_level(priv, data);
343 else
344 esdhc_setup_dma(priv, data);
Yangbo Lufa33d202019-06-21 11:42:27 +0800345
346 /* Calculate the timeout period for data transactions */
347 /*
348 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
349 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
350 * So, Number of SD Clock cycles for 0.25sec should be minimum
351 * (SD Clock/sec * 0.25 sec) SD Clock cycles
352 * = (mmc->clock * 1/4) SD Clock cycles
353 * As 1) >= 2)
354 * => (2^(timeout+13)) >= mmc->clock * 1/4
355 * Taking log2 both the sides
356 * => timeout + 13 >= log2(mmc->clock/4)
357 * Rounding up to next power of 2
358 * => timeout + 13 = log2(mmc->clock/4) + 1
359 * => timeout + 13 = fls(mmc->clock/4)
360 *
361 * However, the MMC spec "It is strongly recommended for hosts to
362 * implement more than 500ms timeout value even if the card
363 * indicates the 250ms maximum busy length." Even the previous
364 * value of 300ms is known to be insufficient for some cards.
365 * So, we use
366 * => timeout + 13 = fls(mmc->clock/2)
367 */
368 timeout = fls(mmc->clock/2);
369 timeout -= 13;
370
371 if (timeout > 14)
372 timeout = 14;
373
374 if (timeout < 0)
375 timeout = 0;
376
Sean Anderson4f01db82021-11-23 15:03:45 -0500377 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
378 (timeout == 4 || timeout == 8 || timeout == 12))
Yangbo Lufa33d202019-06-21 11:42:27 +0800379 timeout++;
Yangbo Lufa33d202019-06-21 11:42:27 +0800380
Sean Anderson4f01db82021-11-23 15:03:45 -0500381 if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
382 timeout = 0xE;
383
Yangbo Lufa33d202019-06-21 11:42:27 +0800384 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
385
386 return 0;
387}
388
Yangbo Lufa33d202019-06-21 11:42:27 +0800389#ifdef CONFIG_MCF5441x
390/*
391 * Swaps 32-bit words to little-endian byte order.
392 */
393static inline void sd_swap_dma_buff(struct mmc_data *data)
394{
395 int i, size = data->blocksize >> 2;
396 u32 *buffer = (u32 *)data->dest;
397 u32 sw;
398
399 while (data->blocks--) {
400 for (i = 0; i < size; i++) {
401 sw = __sw32(*buffer);
402 *buffer++ = sw;
403 }
404 }
405}
Sean Anderson4f01db82021-11-23 15:03:45 -0500406#else
407static inline void sd_swap_dma_buff(struct mmc_data *data)
408{
409 return;
410}
Yangbo Lufa33d202019-06-21 11:42:27 +0800411#endif
412
413/*
414 * Sends a command out on the bus. Takes the mmc pointer,
415 * a command pointer, and an optional data pointer.
416 */
417static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
418 struct mmc_cmd *cmd, struct mmc_data *data)
419{
420 int err = 0;
421 uint xfertyp;
422 uint irqstat;
423 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
424 struct fsl_esdhc *regs = priv->esdhc_regs;
425 unsigned long start;
426
Sean Anderson4f01db82021-11-23 15:03:45 -0500427 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
428 cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
Yangbo Lufa33d202019-06-21 11:42:27 +0800429 return 0;
Yangbo Lufa33d202019-06-21 11:42:27 +0800430
431 esdhc_write32(&regs->irqstat, -1);
432
433 sync();
434
435 /* Wait for the bus to be idle */
436 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
437 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
438 ;
439
440 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
441 ;
442
Yangbo Lufa33d202019-06-21 11:42:27 +0800443 /* Set up for a data transfer if we have one */
444 if (data) {
445 err = esdhc_setup_data(priv, mmc, data);
446 if(err)
447 return err;
Yangbo Lufa33d202019-06-21 11:42:27 +0800448 }
449
450 /* Figure out the transfer arguments */
451 xfertyp = esdhc_xfertyp(cmd, data);
452
453 /* Mask all irqs */
454 esdhc_write32(&regs->irqsigen, 0);
455
456 /* Send the command */
457 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
458#if defined(CONFIG_FSL_USDHC)
459 esdhc_write32(&regs->mixctrl,
460 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
461 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
462 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
463#else
464 esdhc_write32(&regs->xfertyp, xfertyp);
465#endif
466
467 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
468 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
469 flags = IRQSTAT_BRR;
470
471 /* Wait for the command to complete */
472 start = get_timer(0);
473 while (!(esdhc_read32(&regs->irqstat) & flags)) {
474 if (get_timer(start) > 1000) {
475 err = -ETIMEDOUT;
476 goto out;
477 }
478 }
479
480 irqstat = esdhc_read32(&regs->irqstat);
481
482 if (irqstat & CMD_ERR) {
483 err = -ECOMM;
484 goto out;
485 }
486
487 if (irqstat & IRQSTAT_CTOE) {
488 err = -ETIMEDOUT;
489 goto out;
490 }
491
Yangbo Lufa33d202019-06-21 11:42:27 +0800492 /* Workaround for ESDHC errata ENGcm03648 */
493 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan356f7822019-07-10 09:35:30 +0000494 int timeout = 50000;
Yangbo Lufa33d202019-06-21 11:42:27 +0800495
Peng Fan356f7822019-07-10 09:35:30 +0000496 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lufa33d202019-06-21 11:42:27 +0800497 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
498 PRSSTAT_DAT0)) {
499 udelay(100);
500 timeout--;
501 }
502
503 if (timeout <= 0) {
504 printf("Timeout waiting for DAT0 to go high!\n");
505 err = -ETIMEDOUT;
506 goto out;
507 }
508 }
509
510 /* Copy the response to the response buffer */
511 if (cmd->resp_type & MMC_RSP_136) {
512 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
513
514 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
515 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
516 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
517 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
518 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
519 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
520 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
521 cmd->response[3] = (cmdrsp0 << 8);
522 } else
523 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
524
525 /* Wait until all of the blocks are transferred */
526 if (data) {
Sean Anderson4f01db82021-11-23 15:03:45 -0500527 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
528 esdhc_pio_read_write(priv, data);
529 } else {
530 flags = DATA_COMPLETE;
531 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
532 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
533 flags = IRQSTAT_BRR;
534
535 do {
536 irqstat = esdhc_read32(&regs->irqstat);
537
538 if (irqstat & IRQSTAT_DTOE) {
539 err = -ETIMEDOUT;
540 goto out;
541 }
542
543 if (irqstat & DATA_ERR) {
544 err = -ECOMM;
545 goto out;
546 }
547 } while ((irqstat & flags) != flags);
548
549 /*
550 * Need invalidate the dcache here again to avoid any
551 * cache-fill during the DMA operations such as the
552 * speculative pre-fetching etc.
553 */
554 dma_unmap_single(priv->dma_addr,
555 data->blocks * data->blocksize,
556 mmc_get_dma_dir(data));
557 if (IS_ENABLED(CONFIG_MCF5441x) &&
558 (data->flags & MMC_DATA_READ))
559 sd_swap_dma_buff(data);
Yangbo Lufa33d202019-06-21 11:42:27 +0800560 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800561 }
562
563out:
564 /* Reset CMD and DATA portions on error */
565 if (err) {
566 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
567 SYSCTL_RSTC);
568 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
569 ;
570
571 if (data) {
572 esdhc_write32(&regs->sysctl,
573 esdhc_read32(&regs->sysctl) |
574 SYSCTL_RSTD);
575 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
576 ;
577 }
578
579 /* If this was CMD11, then notify that power cycle is needed */
580 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
581 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
582 }
583
584 esdhc_write32(&regs->irqstat, -1);
585
586 return err;
587}
588
589static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
590{
591 struct fsl_esdhc *regs = priv->esdhc_regs;
592 int div = 1;
Haibo Chenf9c3a812020-09-01 15:34:06 +0800593 u32 tmp;
Sean Anderson4f01db82021-11-23 15:03:45 -0500594 int ret, pre_div;
Yangbo Lufa33d202019-06-21 11:42:27 +0800595 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
596 int sdhc_clk = priv->sdhc_clk;
597 uint clk;
598
Sean Anderson4f01db82021-11-23 15:03:45 -0500599 if (IS_ENABLED(ARCH_MXC)) {
600#ifdef CONFIG_MX53
601 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
602 pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
603#else
604 pre_div = 1;
605#endif
606 } else {
607 pre_div = 2;
608 }
609
Yangbo Lufa33d202019-06-21 11:42:27 +0800610 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
611 pre_div *= 2;
612
613 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
614 div++;
615
616 pre_div >>= 1;
617 div -= 1;
618
619 clk = (pre_div << 8) | (div << 4);
620
Sean Anderson4f01db82021-11-23 15:03:45 -0500621 if (IS_ENABLED(CONFIG_FSL_USDHC))
622 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
623 else
624 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800625
626 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
627
Haibo Chenf9c3a812020-09-01 15:34:06 +0800628 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
629 if (ret)
630 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lufa33d202019-06-21 11:42:27 +0800631
Sean Anderson4f01db82021-11-23 15:03:45 -0500632 if (IS_ENABLED(CONFIG_FSL_USDHC))
633 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
634 else
635 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800636
Sean Anderson4ea11bf2021-11-23 15:03:41 -0500637 mmc->clock = sdhc_clk / pre_div / div;
Yangbo Lufa33d202019-06-21 11:42:27 +0800638 priv->clock = clock;
639}
640
Yangbo Lufa33d202019-06-21 11:42:27 +0800641#ifdef MMC_SUPPORTS_TUNING
642static int esdhc_change_pinstate(struct udevice *dev)
643{
644 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
645 int ret;
646
647 switch (priv->mode) {
648 case UHS_SDR50:
649 case UHS_DDR50:
650 ret = pinctrl_select_state(dev, "state_100mhz");
651 break;
652 case UHS_SDR104:
653 case MMC_HS_200:
654 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000655 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800656 ret = pinctrl_select_state(dev, "state_200mhz");
657 break;
658 default:
659 ret = pinctrl_select_state(dev, "default");
660 break;
661 }
662
663 if (ret)
664 printf("%s %d error\n", __func__, priv->mode);
665
666 return ret;
667}
668
669static void esdhc_reset_tuning(struct mmc *mmc)
670{
671 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
672 struct fsl_esdhc *regs = priv->esdhc_regs;
673
674 if (priv->flags & ESDHC_FLAG_USDHC) {
675 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
676 esdhc_clrbits32(&regs->autoc12err,
677 MIX_CTRL_SMPCLK_SEL |
678 MIX_CTRL_EXE_TUNE);
679 }
680 }
681}
682
683static void esdhc_set_strobe_dll(struct mmc *mmc)
684{
685 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
686 struct fsl_esdhc *regs = priv->esdhc_regs;
687 u32 val;
688
689 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800690 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300691 /* clear the reset bit on strobe dll before any setting */
692 esdhc_write32(&regs->strobe_dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +0800693
694 /*
695 * enable strobe dll ctrl and adjust the delay target
696 * for the uSDHC loopback read clock
697 */
698 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300699 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
Yangbo Lufa33d202019-06-21 11:42:27 +0800700 (priv->strobe_dll_delay_target <<
701 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chenc7f44182020-09-30 15:52:23 +0800702 esdhc_write32(&regs->strobe_dllctrl, val);
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300703 /* wait 5us to make sure strobe dll status register stable */
704 mdelay(5);
Haibo Chenc7f44182020-09-30 15:52:23 +0800705 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lufa33d202019-06-21 11:42:27 +0800706 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
707 pr_warn("HS400 strobe DLL status REF not lock!\n");
708 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
709 pr_warn("HS400 strobe DLL status SLV not lock!\n");
710 }
711}
712
713static int esdhc_set_timing(struct mmc *mmc)
714{
715 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
716 struct fsl_esdhc *regs = priv->esdhc_regs;
717 u32 mixctrl;
718
Haibo Chenc7f44182020-09-30 15:52:23 +0800719 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800720 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
721
722 switch (mmc->selected_mode) {
723 case MMC_LEGACY:
Yangbo Lufa33d202019-06-21 11:42:27 +0800724 esdhc_reset_tuning(mmc);
Haibo Chenc7f44182020-09-30 15:52:23 +0800725 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800726 break;
727 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000728 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800729 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800730 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800731 break;
732 case MMC_HS:
733 case MMC_HS_52:
734 case MMC_HS_200:
735 case SD_HS:
736 case UHS_SDR12:
737 case UHS_SDR25:
738 case UHS_SDR50:
739 case UHS_SDR104:
Haibo Chenc7f44182020-09-30 15:52:23 +0800740 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800741 break;
742 case UHS_DDR50:
743 case MMC_DDR_52:
744 mixctrl |= MIX_CTRL_DDREN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800745 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800746 break;
747 default:
748 printf("Not supported %d\n", mmc->selected_mode);
749 return -EINVAL;
750 }
751
752 priv->mode = mmc->selected_mode;
753
754 return esdhc_change_pinstate(mmc->dev);
755}
756
757static int esdhc_set_voltage(struct mmc *mmc)
758{
759 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
760 struct fsl_esdhc *regs = priv->esdhc_regs;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100761#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800762 int ret;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100763#endif
Yangbo Lufa33d202019-06-21 11:42:27 +0800764
765 priv->signal_voltage = mmc->signal_voltage;
766 switch (mmc->signal_voltage) {
767 case MMC_SIGNAL_VOLTAGE_330:
768 if (priv->vs18_enable)
Marek Vasut50a17a62020-05-22 18:28:33 +0200769 return -ENOTSUPP;
Yangbo Lufa33d202019-06-21 11:42:27 +0800770#if CONFIG_IS_ENABLED(DM_REGULATOR)
771 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
772 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
773 if (ret) {
774 printf("Setting to 3.3V error");
775 return -EIO;
776 }
777 /* Wait for 5ms */
778 mdelay(5);
779 }
780#endif
781
782 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
783 if (!(esdhc_read32(&regs->vendorspec) &
784 ESDHC_VENDORSPEC_VSELECT))
785 return 0;
786
787 return -EAGAIN;
788 case MMC_SIGNAL_VOLTAGE_180:
789#if CONFIG_IS_ENABLED(DM_REGULATOR)
790 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
791 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
792 if (ret) {
793 printf("Setting to 1.8V error");
794 return -EIO;
795 }
796 }
797#endif
798 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
Haibo Chen8974ff12021-03-22 18:55:38 +0800799 /*
800 * some board like imx8mm-evk need about 18ms to switch
801 * the IO voltage from 3.3v to 1.8v, common code only
802 * delay 10ms, so need to delay extra time to make sure
803 * the IO voltage change to 1.8v.
804 */
805 if (priv->signal_voltage_switch_extra_delay_ms)
806 mdelay(priv->signal_voltage_switch_extra_delay_ms);
Yangbo Lufa33d202019-06-21 11:42:27 +0800807 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
808 return 0;
809
810 return -EAGAIN;
811 case MMC_SIGNAL_VOLTAGE_120:
812 return -ENOTSUPP;
813 default:
814 return 0;
815 }
816}
817
818static void esdhc_stop_tuning(struct mmc *mmc)
819{
820 struct mmc_cmd cmd;
821
822 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
823 cmd.cmdarg = 0;
824 cmd.resp_type = MMC_RSP_R1b;
825
Jaehoon Chung2da23352021-05-31 08:31:49 +0900826 mmc_send_cmd(mmc, &cmd, NULL);
Yangbo Lufa33d202019-06-21 11:42:27 +0800827}
828
829static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
830{
Simon Glassc69cda22020-12-03 16:55:20 -0700831 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +0800832 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
833 struct fsl_esdhc *regs = priv->esdhc_regs;
834 struct mmc *mmc = &plat->mmc;
Haibo Chenc7f44182020-09-30 15:52:23 +0800835 u32 irqstaten = esdhc_read32(&regs->irqstaten);
836 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800837 int i, ret = -ETIMEDOUT;
838 u32 val, mixctrl;
839
840 /* clock tuning is not needed for upto 52MHz */
841 if (mmc->clock <= 52000000)
842 return 0;
843
844 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
845 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800846 val = esdhc_read32(&regs->autoc12err);
847 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800848 val &= ~MIX_CTRL_SMPCLK_SEL;
849 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
850
851 val |= MIX_CTRL_EXE_TUNE;
852 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
853
Haibo Chenc7f44182020-09-30 15:52:23 +0800854 esdhc_write32(&regs->autoc12err, val);
855 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800856 }
857
858 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chenc7f44182020-09-30 15:52:23 +0800859 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800860 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800861 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800862
Haibo Chenc7f44182020-09-30 15:52:23 +0800863 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
864 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lufa33d202019-06-21 11:42:27 +0800865
866 /*
867 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
868 * of loops reaches 40 times.
869 */
870 for (i = 0; i < MAX_TUNING_LOOP; i++) {
871 u32 ctrl;
872
873 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
874 if (mmc->bus_width == 8)
Haibo Chenc7f44182020-09-30 15:52:23 +0800875 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lufa33d202019-06-21 11:42:27 +0800876 else if (mmc->bus_width == 4)
Haibo Chenc7f44182020-09-30 15:52:23 +0800877 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800878 } else {
Haibo Chenc7f44182020-09-30 15:52:23 +0800879 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800880 }
881
882 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chenc7f44182020-09-30 15:52:23 +0800883 val = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800884 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800885 esdhc_write32(&regs->mixctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800886
887 /* We are using STD tuning, no need to check return value */
888 mmc_send_tuning(mmc, opcode, NULL);
889
Haibo Chenc7f44182020-09-30 15:52:23 +0800890 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lufa33d202019-06-21 11:42:27 +0800891 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
892 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800893 ret = 0;
894 break;
895 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800896 }
897
Haibo Chenc7f44182020-09-30 15:52:23 +0800898 esdhc_write32(&regs->irqstaten, irqstaten);
899 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800900
901 esdhc_stop_tuning(mmc);
902
903 return ret;
904}
905#endif
906
907static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
908{
909 struct fsl_esdhc *regs = priv->esdhc_regs;
910 int ret __maybe_unused;
Peng Fan1d01c982019-11-04 17:14:15 +0800911 u32 clock;
Yangbo Lufa33d202019-06-21 11:42:27 +0800912
Haibo Chen5d772192020-11-03 17:18:35 +0800913#ifdef MMC_SUPPORTS_TUNING
914 /*
915 * call esdhc_set_timing() before update the clock rate,
916 * This is because current we support DDR and SDR mode,
917 * Once the DDR_EN bit is set, the card clock will be
918 * divide by 2 automatically. So need to do this before
919 * setting clock rate.
920 */
921 if (priv->mode != mmc->selected_mode) {
922 ret = esdhc_set_timing(mmc);
923 if (ret) {
924 printf("esdhc_set_timing error %d\n", ret);
925 return ret;
926 }
927 }
928#endif
929
Yangbo Lufa33d202019-06-21 11:42:27 +0800930 /* Set the clock speed */
Peng Fan1d01c982019-11-04 17:14:15 +0800931 clock = mmc->clock;
932 if (clock < mmc->cfg->f_min)
933 clock = mmc->cfg->f_min;
934
935 if (priv->clock != clock)
936 set_sysctl(priv, mmc, clock);
Yangbo Lufa33d202019-06-21 11:42:27 +0800937
Yangbo Lufa33d202019-06-21 11:42:27 +0800938 if (mmc->clk_disable) {
939#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300940 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800941#else
942 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
943#endif
944 } else {
945#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300946 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
947 VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800948#else
949 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
950#endif
951 }
952
Ye Li9b7c3492021-08-17 17:09:20 +0800953#ifdef MMC_SUPPORTS_TUNING
Haibo Chen5d772192020-11-03 17:18:35 +0800954 /*
955 * For HS400/HS400ES mode, make sure set the strobe dll in the
956 * target clock rate. So call esdhc_set_strobe_dll() after the
957 * clock updated.
958 */
959 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
960 esdhc_set_strobe_dll(mmc);
Yangbo Lufa33d202019-06-21 11:42:27 +0800961
962 if (priv->signal_voltage != mmc->signal_voltage) {
963 ret = esdhc_set_voltage(mmc);
964 if (ret) {
Marek Vasut50a17a62020-05-22 18:28:33 +0200965 if (ret != -ENOTSUPP)
966 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lufa33d202019-06-21 11:42:27 +0800967 return ret;
968 }
969 }
970#endif
971
972 /* Set the bus width */
973 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
974
975 if (mmc->bus_width == 4)
976 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
977 else if (mmc->bus_width == 8)
978 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
979
980 return 0;
981}
982
983static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
984{
985 struct fsl_esdhc *regs = priv->esdhc_regs;
986 ulong start;
987
988 /* Reset the entire host controller */
989 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
990
991 /* Wait until the controller is available */
992 start = get_timer(0);
993 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
994 if (get_timer(start) > 1000)
995 return -ETIMEDOUT;
996 }
997
998#if defined(CONFIG_FSL_USDHC)
999 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1000 esdhc_write32(&regs->mmcboot, 0x0);
1001 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1002 esdhc_write32(&regs->mixctrl, 0x0);
1003 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1004
1005 /* Put VEND_SPEC to default value */
1006 if (priv->vs18_enable)
1007 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1008 ESDHC_VENDORSPEC_VSELECT));
1009 else
1010 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1011
1012 /* Disable DLL_CTRL delay line */
1013 esdhc_write32(&regs->dllctrl, 0x0);
1014#endif
1015
1016#ifndef ARCH_MXC
1017 /* Enable cache snooping */
1018 esdhc_write32(&regs->scr, 0x00000040);
1019#endif
1020
1021#ifndef CONFIG_FSL_USDHC
1022 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1023#else
Fabio Estevamf132aab2021-06-07 17:40:09 -03001024 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Yangbo Lufa33d202019-06-21 11:42:27 +08001025#endif
1026
1027 /* Set the initial clock speed */
1028 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1029
1030 /* Disable the BRR and BWR bits in IRQSTAT */
1031 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1032
1033#ifdef CONFIG_MCF5441x
1034 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1035#else
1036 /* Put the PROCTL reg back to the default */
1037 esdhc_write32(&regs->proctl, PROCTL_INIT);
1038#endif
1039
1040 /* Set timout to the maximum value */
1041 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1042
1043 return 0;
1044}
1045
1046static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1047{
1048 struct fsl_esdhc *regs = priv->esdhc_regs;
1049 int timeout = 1000;
1050
1051#ifdef CONFIG_ESDHC_DETECT_QUIRK
1052 if (CONFIG_ESDHC_DETECT_QUIRK)
1053 return 1;
1054#endif
1055
1056#if CONFIG_IS_ENABLED(DM_MMC)
Fabio Estevam29230f32020-01-06 20:11:27 -03001057 if (priv->broken_cd)
1058 return 1;
Simon Glassbcee8d62019-12-06 21:41:35 -07001059#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001060 if (dm_gpio_is_valid(&priv->cd_gpio))
1061 return dm_gpio_get_value(&priv->cd_gpio);
1062#endif
1063#endif
1064
1065 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1066 udelay(1000);
1067
1068 return timeout > 0;
1069}
1070
1071static int esdhc_reset(struct fsl_esdhc *regs)
1072{
1073 ulong start;
1074
1075 /* reset the controller */
1076 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1077
1078 /* hardware clears the bit when it is done */
1079 start = get_timer(0);
1080 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1081 if (get_timer(start) > 100) {
1082 printf("MMC/SD: Reset never completed.\n");
1083 return -ETIMEDOUT;
1084 }
1085 }
1086
1087 return 0;
1088}
1089
1090#if !CONFIG_IS_ENABLED(DM_MMC)
1091static int esdhc_getcd(struct mmc *mmc)
1092{
1093 struct fsl_esdhc_priv *priv = mmc->priv;
1094
1095 return esdhc_getcd_common(priv);
1096}
1097
1098static int esdhc_init(struct mmc *mmc)
1099{
1100 struct fsl_esdhc_priv *priv = mmc->priv;
1101
1102 return esdhc_init_common(priv, mmc);
1103}
1104
1105static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1106 struct mmc_data *data)
1107{
1108 struct fsl_esdhc_priv *priv = mmc->priv;
1109
1110 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1111}
1112
1113static int esdhc_set_ios(struct mmc *mmc)
1114{
1115 struct fsl_esdhc_priv *priv = mmc->priv;
1116
1117 return esdhc_set_ios_common(priv, mmc);
1118}
1119
1120static const struct mmc_ops esdhc_ops = {
1121 .getcd = esdhc_getcd,
1122 .init = esdhc_init,
1123 .send_cmd = esdhc_send_cmd,
1124 .set_ios = esdhc_set_ios,
1125};
1126#endif
1127
1128static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1129 struct fsl_esdhc_plat *plat)
1130{
1131 struct mmc_config *cfg;
1132 struct fsl_esdhc *regs;
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001133 u32 caps;
Yangbo Lufa33d202019-06-21 11:42:27 +08001134 int ret;
1135
1136 if (!priv)
1137 return -EINVAL;
1138
1139 regs = priv->esdhc_regs;
1140
1141 /* First reset the eSDHC controller */
1142 ret = esdhc_reset(regs);
1143 if (ret)
1144 return ret;
1145
Yangbo Lufa33d202019-06-21 11:42:27 +08001146 /* ColdFire, using SDHC_DATA[3] for card detection */
Sean Anderson4f01db82021-11-23 15:03:45 -05001147 if (IS_ENABLED(CONFIG_MCF5441x))
1148 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
Yangbo Lufa33d202019-06-21 11:42:27 +08001149
Sean Anderson4f01db82021-11-23 15:03:45 -05001150 if (IS_ENABLED(CONFIG_FSL_USDHC)) {
1151 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1152 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1153 } else {
1154 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1155 | SYSCTL_IPGEN | SYSCTL_CKEN);
1156 /* Clearing tuning bits in case ROM has set it already */
1157 esdhc_write32(&regs->mixctrl, 0);
1158 esdhc_write32(&regs->autoc12err, 0);
1159 esdhc_write32(&regs->clktunectrlstatus, 0);
1160 }
Yangbo Lufa33d202019-06-21 11:42:27 +08001161
1162 if (priv->vs18_enable)
1163 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1164
Haibo Chenc7f44182020-09-30 15:52:23 +08001165 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lufa33d202019-06-21 11:42:27 +08001166 cfg = &plat->cfg;
1167#ifndef CONFIG_DM_MMC
1168 memset(cfg, '\0', sizeof(*cfg));
1169#endif
1170
Yangbo Lufa33d202019-06-21 11:42:27 +08001171 caps = esdhc_read32(&regs->hostcapblt);
Sean Anderson4f01db82021-11-23 15:03:45 -05001172
Yangbo Lufa33d202019-06-21 11:42:27 +08001173 /*
1174 * MCF5441x RM declares in more points that sdhc clock speed must
1175 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1176 * from host capabilities.
1177 */
Sean Anderson4f01db82021-11-23 15:03:45 -05001178 if (IS_ENABLED(CONFIG_MCF5441x))
1179 caps &= ~HOSTCAPBLT_HSS;
Yangbo Lufa33d202019-06-21 11:42:27 +08001180
Sean Anderson4f01db82021-11-23 15:03:45 -05001181 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
1182 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Yangbo Lufa33d202019-06-21 11:42:27 +08001183
Sean Anderson4f01db82021-11-23 15:03:45 -05001184 if (IS_ENABLED(CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33))
1185 caps |= HOSTCAPBLT_VS33;
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001186
1187 if (caps & HOSTCAPBLT_VS18)
1188 cfg->voltages |= MMC_VDD_165_195;
1189 if (caps & HOSTCAPBLT_VS30)
1190 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
1191 if (caps & HOSTCAPBLT_VS33)
1192 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Yangbo Lufa33d202019-06-21 11:42:27 +08001193
1194 cfg->name = "FSL_SDHC";
Sean Anderson4f01db82021-11-23 15:03:45 -05001195
Yangbo Lufa33d202019-06-21 11:42:27 +08001196#if !CONFIG_IS_ENABLED(DM_MMC)
1197 cfg->ops = &esdhc_ops;
1198#endif
Sean Anderson4f01db82021-11-23 15:03:45 -05001199
1200 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE))
1201 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Yangbo Lufa33d202019-06-21 11:42:27 +08001202
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001203 if (caps & HOSTCAPBLT_HSS)
Yangbo Lufa33d202019-06-21 11:42:27 +08001204 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1205
Yangbo Lufa33d202019-06-21 11:42:27 +08001206 cfg->host_caps |= priv->caps;
1207
1208 cfg->f_min = 400000;
1209 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1210
1211 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1212
Haibo Chenc7f44182020-09-30 15:52:23 +08001213 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +08001214 if (priv->flags & ESDHC_FLAG_USDHC) {
1215 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +08001216 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +08001217
1218 val |= ESDHC_STD_TUNING_EN;
1219 val &= ~ESDHC_TUNING_START_TAP_MASK;
1220 val |= priv->tuning_start_tap;
1221 val &= ~ESDHC_TUNING_STEP_MASK;
1222 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chenba616762020-06-22 19:38:04 +08001223
1224 /* Disable the CMD CRC check for tuning, if not, need to
1225 * add some delay after every tuning command, because
1226 * hardware standard tuning logic will directly go to next
1227 * step once it detect the CMD CRC error, will not wait for
1228 * the card side to finally send out the tuning data, trigger
1229 * the buffer read ready interrupt immediately. If usdhc send
1230 * the next tuning command some eMMC card will stuck, can't
1231 * response, block the tuning procedure or the first command
1232 * after the whole tuning procedure always can't get any response.
1233 */
1234 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chenc7f44182020-09-30 15:52:23 +08001235 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +08001236 }
1237 }
1238
1239 return 0;
1240}
1241
1242#if !CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001243int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lufa33d202019-06-21 11:42:27 +08001244{
1245 struct fsl_esdhc_plat *plat;
1246 struct fsl_esdhc_priv *priv;
Sean Anderson95d6b742021-11-23 15:03:39 -05001247 struct mmc_config *mmc_cfg;
Yangbo Lufa33d202019-06-21 11:42:27 +08001248 struct mmc *mmc;
1249 int ret;
1250
1251 if (!cfg)
1252 return -EINVAL;
1253
1254 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1255 if (!priv)
1256 return -ENOMEM;
1257 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1258 if (!plat) {
1259 free(priv);
1260 return -ENOMEM;
1261 }
1262
Sean Anderson95d6b742021-11-23 15:03:39 -05001263 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1264 priv->sdhc_clk = cfg->sdhc_clk;
1265 priv->wp_enable = cfg->wp_enable;
1266
1267 mmc_cfg = &plat->cfg;
1268
1269 switch (cfg->max_bus_width) {
1270 case 0: /* Not set in config; assume everything is supported */
1271 case 8:
1272 mmc_cfg->host_caps |= MMC_MODE_8BIT;
1273 fallthrough;
1274 case 4:
1275 mmc_cfg->host_caps |= MMC_MODE_4BIT;
1276 fallthrough;
1277 case 1:
1278 mmc_cfg->host_caps |= MMC_MODE_1BIT;
1279 break;
1280 default:
1281 printf("invalid max bus width %u\n", cfg->max_bus_width);
1282 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001283 }
1284
Sean Anderson4f01db82021-11-23 15:03:45 -05001285 if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
Sean Anderson95d6b742021-11-23 15:03:39 -05001286 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
Sean Anderson95d6b742021-11-23 15:03:39 -05001287
Yangbo Lufa33d202019-06-21 11:42:27 +08001288 ret = fsl_esdhc_init(priv, plat);
1289 if (ret) {
1290 debug("%s init failure\n", __func__);
1291 free(plat);
1292 free(priv);
1293 return ret;
1294 }
1295
1296 mmc = mmc_create(&plat->cfg, priv);
1297 if (!mmc)
1298 return -EIO;
1299
1300 priv->mmc = mmc;
1301
1302 return 0;
1303}
1304
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001305int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lufa33d202019-06-21 11:42:27 +08001306{
1307 struct fsl_esdhc_cfg *cfg;
1308
1309 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1310 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1311 cfg->sdhc_clk = gd->arch.sdhc_clk;
1312 return fsl_esdhc_initialize(bis, cfg);
1313}
1314#endif
1315
Yangbo Lufa33d202019-06-21 11:42:27 +08001316#ifdef CONFIG_OF_LIBFDT
1317__weak int esdhc_status_fixup(void *blob, const char *compat)
1318{
1319#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1320 if (!hwconfig("esdhc")) {
1321 do_fixup_by_compat(blob, compat, "status", "disabled",
1322 sizeof("disabled"), 1);
1323 return 1;
1324 }
1325#endif
1326 return 0;
1327}
1328
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001329void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lufa33d202019-06-21 11:42:27 +08001330{
1331 const char *compat = "fsl,esdhc";
1332
1333 if (esdhc_status_fixup(blob, compat))
1334 return;
1335
Yangbo Lufa33d202019-06-21 11:42:27 +08001336 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1337 gd->arch.sdhc_clk, 1);
Yangbo Lufa33d202019-06-21 11:42:27 +08001338}
1339#endif
1340
1341#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +08001342#include <asm/arch/clock.h>
Yangbo Lufa33d202019-06-21 11:42:27 +08001343__weak void init_clk_usdhc(u32 index)
1344{
1345}
1346
Simon Glassd1998a92020-12-03 16:55:21 -07001347static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lufa33d202019-06-21 11:42:27 +08001348{
Yangbo Lufa33d202019-06-21 11:42:27 +08001349 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001350#if CONFIG_IS_ENABLED(DM_REGULATOR)
1351 struct udevice *vqmmc_dev;
Walter Lozano23721772020-07-29 12:31:17 -03001352 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001353#endif
Walter Lozano23721772020-07-29 12:31:17 -03001354 const void *fdt = gd->fdt_blob;
1355 int node = dev_of_offset(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001356 fdt_addr_t addr;
1357 unsigned int val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001358
Simon Glassdcfc42b2021-08-07 07:24:06 -06001359 if (!CONFIG_IS_ENABLED(OF_REAL))
1360 return 0;
1361
Yangbo Lufa33d202019-06-21 11:42:27 +08001362 addr = dev_read_addr(dev);
1363 if (addr == FDT_ADDR_T_NONE)
1364 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001365 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lufa33d202019-06-21 11:42:27 +08001366 priv->dev = dev;
1367 priv->mode = -1;
Yangbo Lufa33d202019-06-21 11:42:27 +08001368
Yangbo Lufa33d202019-06-21 11:42:27 +08001369 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1370 priv->tuning_step = val;
1371 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1372 ESDHC_TUNING_START_TAP_DEFAULT);
1373 priv->tuning_start_tap = val;
1374 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1375 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1376 priv->strobe_dll_delay_target = val;
Haibo Chen8974ff12021-03-22 18:55:38 +08001377 val = fdtdec_get_int(fdt, node, "fsl,signal-voltage-switch-extra-delay-ms", 0);
1378 priv->signal_voltage_switch_extra_delay_ms = val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001379
Fabio Estevam29230f32020-01-06 20:11:27 -03001380 if (dev_read_bool(dev, "broken-cd"))
1381 priv->broken_cd = 1;
1382
Yangbo Lufa33d202019-06-21 11:42:27 +08001383 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1384 priv->wp_enable = 1;
1385 } else {
1386 priv->wp_enable = 0;
Yangbo Lufa33d202019-06-21 11:42:27 +08001387 }
1388
Sean Andersond39aa732021-11-23 15:03:40 -05001389#if CONFIG_IS_ENABLED(DM_GPIO)
1390 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1391 GPIOD_IS_IN);
1392 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1393 GPIOD_IS_IN);
1394#endif
1395
Yangbo Lufa33d202019-06-21 11:42:27 +08001396 priv->vs18_enable = 0;
1397
1398#if CONFIG_IS_ENABLED(DM_REGULATOR)
1399 /*
1400 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1401 * otherwise, emmc will work abnormally.
1402 */
1403 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1404 if (ret) {
1405 dev_dbg(dev, "no vqmmc-supply\n");
1406 } else {
Marek Vasut406df852020-05-22 18:19:08 +02001407 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001408 ret = regulator_set_enable(vqmmc_dev, true);
1409 if (ret) {
1410 dev_err(dev, "fail to enable vqmmc-supply\n");
1411 return ret;
1412 }
1413
1414 if (regulator_get_value(vqmmc_dev) == 1800000)
1415 priv->vs18_enable = 1;
1416 }
1417#endif
Simon Glassdcfc42b2021-08-07 07:24:06 -06001418
Walter Lozano23721772020-07-29 12:31:17 -03001419 return 0;
1420}
1421
1422static int fsl_esdhc_probe(struct udevice *dev)
1423{
1424 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001425 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano23721772020-07-29 12:31:17 -03001426 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1427 struct esdhc_soc_data *data =
1428 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1429 struct mmc *mmc;
Walter Lozano23721772020-07-29 12:31:17 -03001430 int ret;
1431
1432#if CONFIG_IS_ENABLED(OF_PLATDATA)
1433 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
Walter Lozano23721772020-07-29 12:31:17 -03001434
1435 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
Walter Lozano7142ff92020-07-29 12:31:19 -03001436
1437 if (dtplat->non_removable)
Sean Andersond39aa732021-11-23 15:03:40 -05001438 plat->cfg.host_caps |= MMC_CAP_NONREMOVABLE;
Walter Lozano7142ff92020-07-29 12:31:19 -03001439 else
Sean Andersond39aa732021-11-23 15:03:40 -05001440 plat->cfg.host_caps &= ~MMC_CAP_NONREMOVABLE;
Walter Lozano7142ff92020-07-29 12:31:19 -03001441
Sean Andersond39aa732021-11-23 15:03:40 -05001442 if (CONFIG_IS_ENABLED(DM_GPIO) && !dtplat->non_removable) {
Walter Lozano7142ff92020-07-29 12:31:19 -03001443 struct udevice *gpiodev;
Walter Lozano7142ff92020-07-29 12:31:19 -03001444
Simon Glasscc469b72021-03-15 17:25:28 +13001445 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
Walter Lozano7142ff92020-07-29 12:31:19 -03001446 if (ret)
1447 return ret;
1448
1449 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1450 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1451 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1452
1453 if (ret)
1454 return ret;
1455 }
Walter Lozano23721772020-07-29 12:31:17 -03001456#endif
1457
1458 if (data)
1459 priv->flags = data->flags;
Yangbo Lufa33d202019-06-21 11:42:27 +08001460
Yangbo Lufa33d202019-06-21 11:42:27 +08001461 /*
1462 * TODO:
1463 * Because lack of clk driver, if SDHC clk is not enabled,
1464 * need to enable it first before this driver is invoked.
1465 *
1466 * we use MXC_ESDHC_CLK to get clk freq.
1467 * If one would like to make this function work,
1468 * the aliases should be provided in dts as this:
1469 *
1470 * aliases {
1471 * mmc0 = &usdhc1;
1472 * mmc1 = &usdhc2;
1473 * mmc2 = &usdhc3;
1474 * mmc3 = &usdhc4;
1475 * };
1476 * Then if your board only supports mmc2 and mmc3, but we can
1477 * correctly get the seq as 2 and 3, then let mxc_get_clock
1478 * work as expected.
1479 */
1480
Simon Glass8b85dfc2020-12-16 21:20:07 -07001481 init_clk_usdhc(dev_seq(dev));
Yangbo Lufa33d202019-06-21 11:42:27 +08001482
Giulio Benettia820bed2020-01-10 15:51:45 +01001483#if CONFIG_IS_ENABLED(CLK)
1484 /* Assigned clock already set clock */
1485 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1486 if (ret) {
1487 printf("Failed to get per_clk\n");
1488 return ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001489 }
Giulio Benettia820bed2020-01-10 15:51:45 +01001490 ret = clk_enable(&priv->per_clk);
1491 if (ret) {
1492 printf("Failed to enable per_clk\n");
1493 return ret;
1494 }
1495
1496 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1497#else
Simon Glass8b85dfc2020-12-16 21:20:07 -07001498 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettia820bed2020-01-10 15:51:45 +01001499 if (priv->sdhc_clk <= 0) {
1500 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1501 return -EINVAL;
1502 }
1503#endif
Yangbo Lufa33d202019-06-21 11:42:27 +08001504
1505 ret = fsl_esdhc_init(priv, plat);
1506 if (ret) {
1507 dev_err(dev, "fsl_esdhc_init failure\n");
1508 return ret;
1509 }
1510
Simon Glassdcfc42b2021-08-07 07:24:06 -06001511 if (CONFIG_IS_ENABLED(OF_REAL)) {
1512 ret = mmc_of_parse(dev, &plat->cfg);
1513 if (ret)
1514 return ret;
1515 }
Peng Fanb0155ac2019-07-10 09:35:24 +00001516
Yangbo Lufa33d202019-06-21 11:42:27 +08001517 mmc = &plat->mmc;
1518 mmc->cfg = &plat->cfg;
1519 mmc->dev = dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001520
1521 upriv->mmc = mmc;
1522
1523 return esdhc_init_common(priv, mmc);
1524}
1525
Yangbo Lufa33d202019-06-21 11:42:27 +08001526static int fsl_esdhc_get_cd(struct udevice *dev)
1527{
Sean Andersond39aa732021-11-23 15:03:40 -05001528 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001529 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1530
Sean Andersond39aa732021-11-23 15:03:40 -05001531 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1532 return 1;
1533
Yangbo Lufa33d202019-06-21 11:42:27 +08001534 return esdhc_getcd_common(priv);
1535}
1536
1537static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1538 struct mmc_data *data)
1539{
Simon Glassc69cda22020-12-03 16:55:20 -07001540 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001541 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1542
1543 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1544}
1545
1546static int fsl_esdhc_set_ios(struct udevice *dev)
1547{
Simon Glassc69cda22020-12-03 16:55:20 -07001548 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001549 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1550
1551 return esdhc_set_ios_common(priv, &plat->mmc);
1552}
1553
Peng Fane9c22552019-07-10 09:35:26 +00001554#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1555static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1556{
1557 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1558 struct fsl_esdhc *regs = priv->esdhc_regs;
1559 u32 m;
1560
Haibo Chenc7f44182020-09-30 15:52:23 +08001561 m = esdhc_read32(&regs->mixctrl);
Peng Fane9c22552019-07-10 09:35:26 +00001562 m |= MIX_CTRL_HS400_ES;
Haibo Chenc7f44182020-09-30 15:52:23 +08001563 esdhc_write32(&regs->mixctrl, m);
Peng Fane9c22552019-07-10 09:35:26 +00001564
1565 return 0;
1566}
1567#endif
1568
Haibo Chenb5874b52020-11-05 14:57:13 +08001569static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1570 int timeout_us)
1571{
1572 int ret;
1573 u32 tmp;
1574 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1575 struct fsl_esdhc *regs = priv->esdhc_regs;
1576
1577 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1578 !!(tmp & PRSSTAT_DAT0) == !!state,
1579 timeout_us);
1580 return ret;
1581}
1582
Yangbo Lufa33d202019-06-21 11:42:27 +08001583static const struct dm_mmc_ops fsl_esdhc_ops = {
1584 .get_cd = fsl_esdhc_get_cd,
1585 .send_cmd = fsl_esdhc_send_cmd,
1586 .set_ios = fsl_esdhc_set_ios,
1587#ifdef MMC_SUPPORTS_TUNING
1588 .execute_tuning = fsl_esdhc_execute_tuning,
1589#endif
Peng Fane9c22552019-07-10 09:35:26 +00001590#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1591 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1592#endif
Haibo Chenb5874b52020-11-05 14:57:13 +08001593 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lufa33d202019-06-21 11:42:27 +08001594};
Yangbo Lufa33d202019-06-21 11:42:27 +08001595
1596static struct esdhc_soc_data usdhc_imx7d_data = {
1597 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1598 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1599 | ESDHC_FLAG_HS400,
Yangbo Lufa33d202019-06-21 11:42:27 +08001600};
1601
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001602static struct esdhc_soc_data usdhc_imx7ulp_data = {
1603 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +03001604 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1605 | ESDHC_FLAG_HS400,
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001606};
1607
Peng Fan609ba122019-07-10 09:35:28 +00001608static struct esdhc_soc_data usdhc_imx8qm_data = {
1609 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1610 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1611 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1612};
1613
Yangbo Lufa33d202019-06-21 11:42:27 +08001614static const struct udevice_id fsl_esdhc_ids[] = {
Fabio Estevamc3e6f992021-02-15 08:58:15 -03001615 { .compatible = "fsl,imx51-esdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001616 { .compatible = "fsl,imx53-esdhc", },
1617 { .compatible = "fsl,imx6ul-usdhc", },
1618 { .compatible = "fsl,imx6sx-usdhc", },
1619 { .compatible = "fsl,imx6sl-usdhc", },
1620 { .compatible = "fsl,imx6q-usdhc", },
1621 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001622 { .compatible = "fsl,imx7ulp-usdhc", .data = (ulong)&usdhc_imx7ulp_data,},
Peng Fan609ba122019-07-10 09:35:28 +00001623 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fanf65d0842019-11-04 17:31:17 +08001624 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1625 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1626 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti6a63a872020-01-10 15:51:46 +01001627 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001628 { .compatible = "fsl,esdhc", },
1629 { /* sentinel */ }
1630};
1631
Yangbo Lufa33d202019-06-21 11:42:27 +08001632static int fsl_esdhc_bind(struct udevice *dev)
1633{
Simon Glassc69cda22020-12-03 16:55:20 -07001634 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001635
1636 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1637}
Yangbo Lufa33d202019-06-21 11:42:27 +08001638
1639U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano45154f02020-07-29 12:31:16 -03001640 .name = "fsl_esdhc",
Yangbo Lufa33d202019-06-21 11:42:27 +08001641 .id = UCLASS_MMC,
1642 .of_match = fsl_esdhc_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07001643 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lufa33d202019-06-21 11:42:27 +08001644 .ops = &fsl_esdhc_ops,
Yangbo Lufa33d202019-06-21 11:42:27 +08001645 .bind = fsl_esdhc_bind,
Yangbo Lufa33d202019-06-21 11:42:27 +08001646 .probe = fsl_esdhc_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -07001647 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass41575d82020-12-03 16:55:17 -07001648 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lufa33d202019-06-21 11:42:27 +08001649};
Walter Lozano23721772020-07-29 12:31:17 -03001650
Simon Glassbdf8fd72020-12-28 20:34:57 -07001651DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lufa33d202019-06-21 11:42:27 +08001652#endif