blob: 85cd72a796986c611929f82033f0547079529ca0 [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>
Yangbo Lufa33d202019-06-21 11:42:27 +080041
Haibo Chen0ba116a2021-02-19 11:25:32 -080042#ifndef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
43#ifdef CONFIG_FSL_USDHC
44#define ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 1
45#endif
46#endif
47
Yangbo Lufa33d202019-06-21 11:42:27 +080048DECLARE_GLOBAL_DATA_PTR;
49
50#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
51 IRQSTATEN_CINT | \
52 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
53 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
54 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
55 IRQSTATEN_DINT)
56#define MAX_TUNING_LOOP 40
Yangbo Lufa33d202019-06-21 11:42:27 +080057
58struct fsl_esdhc {
59 uint dsaddr; /* SDMA system address register */
60 uint blkattr; /* Block attributes register */
61 uint cmdarg; /* Command argument register */
62 uint xfertyp; /* Transfer type register */
63 uint cmdrsp0; /* Command response 0 register */
64 uint cmdrsp1; /* Command response 1 register */
65 uint cmdrsp2; /* Command response 2 register */
66 uint cmdrsp3; /* Command response 3 register */
67 uint datport; /* Buffer data port register */
68 uint prsstat; /* Present state register */
69 uint proctl; /* Protocol control register */
70 uint sysctl; /* System Control Register */
71 uint irqstat; /* Interrupt status register */
72 uint irqstaten; /* Interrupt status enable register */
73 uint irqsigen; /* Interrupt signal enable register */
74 uint autoc12err; /* Auto CMD error status register */
75 uint hostcapblt; /* Host controller capabilities register */
76 uint wml; /* Watermark level register */
77 uint mixctrl; /* For USDHC */
78 char reserved1[4]; /* reserved */
79 uint fevt; /* Force event register */
80 uint admaes; /* ADMA error status register */
81 uint adsaddr; /* ADMA system address register */
82 char reserved2[4];
83 uint dllctrl;
84 uint dllstat;
85 uint clktunectrlstatus;
86 char reserved3[4];
87 uint strobe_dllctrl;
88 uint strobe_dllstat;
89 char reserved4[72];
90 uint vendorspec;
91 uint mmcboot;
92 uint vendorspec2;
Giulio Benetti6a63a872020-01-10 15:51:46 +010093 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lufa33d202019-06-21 11:42:27 +080094 char reserved5[44];
95 uint hostver; /* Host controller version register */
96 char reserved6[4]; /* reserved */
97 uint dmaerraddr; /* DMA error address register */
98 char reserved7[4]; /* reserved */
99 uint dmaerrattr; /* DMA error attribute register */
100 char reserved8[4]; /* reserved */
101 uint hostcapblt2; /* Host controller capabilities register 2 */
102 char reserved9[8]; /* reserved */
103 uint tcr; /* Tuning control register */
104 char reserved10[28]; /* reserved */
105 uint sddirctl; /* SD direction control register */
106 char reserved11[712];/* reserved */
107 uint scr; /* eSDHC control register */
108};
109
110struct fsl_esdhc_plat {
Walter Lozano23721772020-07-29 12:31:17 -0300111#if CONFIG_IS_ENABLED(OF_PLATDATA)
112 /* Put this first since driver model will copy the data here */
113 struct dtd_fsl_esdhc dtplat;
114#endif
115
Yangbo Lufa33d202019-06-21 11:42:27 +0800116 struct mmc_config cfg;
117 struct mmc mmc;
118};
119
120struct esdhc_soc_data {
121 u32 flags;
Yangbo Lufa33d202019-06-21 11:42:27 +0800122};
123
124/**
125 * struct fsl_esdhc_priv
126 *
127 * @esdhc_regs: registers of the sdhc controller
128 * @sdhc_clk: Current clk of the sdhc controller
129 * @bus_width: bus width, 1bit, 4bit or 8bit
130 * @cfg: mmc config
131 * @mmc: mmc
132 * Following is used when Driver Model is enabled for MMC
133 * @dev: pointer for the device
134 * @non_removable: 0: removable; 1: non-removable
Fabio Estevam29230f32020-01-06 20:11:27 -0300135 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lufa33d202019-06-21 11:42:27 +0800136 * @wp_enable: 1: enable checking wp; 0: no check
137 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
138 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
139 * @caps: controller capabilities
140 * @tuning_step: tuning step setting in tuning_ctrl register
141 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
142 * @strobe_dll_delay_target: settings in strobe_dllctrl
143 * @signal_voltage: indicating the current voltage
Haibo Chen8974ff12021-03-22 18:55:38 +0800144 * @signal_voltage_switch_extra_delay_ms: extra delay for IO voltage switch
Yangbo Lufa33d202019-06-21 11:42:27 +0800145 * @cd_gpio: gpio for card detection
146 * @wp_gpio: gpio for write protection
147 */
148struct fsl_esdhc_priv {
149 struct fsl_esdhc *esdhc_regs;
150 unsigned int sdhc_clk;
151 struct clk per_clk;
152 unsigned int clock;
153 unsigned int mode;
154 unsigned int bus_width;
Sean Anderson297d2de2022-01-12 08:18:52 +0900155#if !CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +0800156 struct mmc *mmc;
157#endif
158 struct udevice *dev;
159 int non_removable;
Fabio Estevam29230f32020-01-06 20:11:27 -0300160 int broken_cd;
Yangbo Lufa33d202019-06-21 11:42:27 +0800161 int wp_enable;
162 int vs18_enable;
163 u32 flags;
164 u32 caps;
165 u32 tuning_step;
166 u32 tuning_start_tap;
167 u32 strobe_dll_delay_target;
168 u32 signal_voltage;
Haibo Chen8974ff12021-03-22 18:55:38 +0800169 u32 signal_voltage_switch_extra_delay_ms;
Ye Li82771712019-07-11 03:29:02 +0000170#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800171 struct udevice *vqmmc_dev;
172 struct udevice *vmmc_dev;
173#endif
Simon Glassbcee8d62019-12-06 21:41:35 -0700174#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +0800175 struct gpio_desc cd_gpio;
176 struct gpio_desc wp_gpio;
177#endif
178};
179
180/* Return the XFERTYP flags for a given command and data packet */
181static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
182{
183 uint xfertyp = 0;
184
185 if (data) {
186 xfertyp |= XFERTYP_DPSEL;
187#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
188 xfertyp |= XFERTYP_DMAEN;
189#endif
190 if (data->blocks > 1) {
191 xfertyp |= XFERTYP_MSBSEL;
192 xfertyp |= XFERTYP_BCEN;
193#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
194 xfertyp |= XFERTYP_AC12EN;
195#endif
196 }
197
198 if (data->flags & MMC_DATA_READ)
199 xfertyp |= XFERTYP_DTDSEL;
200 }
201
202 if (cmd->resp_type & MMC_RSP_CRC)
203 xfertyp |= XFERTYP_CCCEN;
204 if (cmd->resp_type & MMC_RSP_OPCODE)
205 xfertyp |= XFERTYP_CICEN;
206 if (cmd->resp_type & MMC_RSP_136)
207 xfertyp |= XFERTYP_RSPTYP_136;
208 else if (cmd->resp_type & MMC_RSP_BUSY)
209 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
210 else if (cmd->resp_type & MMC_RSP_PRESENT)
211 xfertyp |= XFERTYP_RSPTYP_48;
212
213 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
214 xfertyp |= XFERTYP_CMDTYP_ABORT;
215
216 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
217}
218
219#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
220/*
221 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
222 */
223static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
224 struct mmc_data *data)
225{
226 struct fsl_esdhc *regs = priv->esdhc_regs;
227 uint blocks;
228 char *buffer;
229 uint databuf;
230 uint size;
231 uint irqstat;
232 ulong start;
233
234 if (data->flags & MMC_DATA_READ) {
235 blocks = data->blocks;
236 buffer = data->dest;
237 while (blocks) {
238 start = get_timer(0);
239 size = data->blocksize;
240 irqstat = esdhc_read32(&regs->irqstat);
241 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
242 if (get_timer(start) > PIO_TIMEOUT) {
243 printf("\nData Read Failed in PIO Mode.");
244 return;
245 }
246 }
247 while (size && (!(irqstat & IRQSTAT_TC))) {
248 udelay(100); /* Wait before last byte transfer complete */
249 irqstat = esdhc_read32(&regs->irqstat);
250 databuf = in_le32(&regs->datport);
251 *((uint *)buffer) = databuf;
252 buffer += 4;
253 size -= 4;
254 }
255 blocks--;
256 }
257 } else {
258 blocks = data->blocks;
259 buffer = (char *)data->src;
260 while (blocks) {
261 start = get_timer(0);
262 size = data->blocksize;
263 irqstat = esdhc_read32(&regs->irqstat);
264 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
265 if (get_timer(start) > PIO_TIMEOUT) {
266 printf("\nData Write Failed in PIO Mode.");
267 return;
268 }
269 }
270 while (size && (!(irqstat & IRQSTAT_TC))) {
271 udelay(100); /* Wait before last byte transfer complete */
272 databuf = *((uint *)buffer);
273 buffer += 4;
274 size -= 4;
275 irqstat = esdhc_read32(&regs->irqstat);
276 out_le32(&regs->datport, databuf);
277 }
278 blocks--;
279 }
280 }
281}
282#endif
283
284static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
285 struct mmc_data *data)
286{
287 int timeout;
288 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Fan744c5fd2021-08-07 16:00:46 +0800289#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
290 defined(CONFIG_IMX8ULP)
Yangbo Lufa33d202019-06-21 11:42:27 +0800291 dma_addr_t addr;
292#endif
293 uint wml_value;
294
295 wml_value = data->blocksize/4;
296
297 if (data->flags & MMC_DATA_READ) {
298 if (wml_value > WML_RD_WML_MAX)
299 wml_value = WML_RD_WML_MAX_VAL;
300
301 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
302#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Fan744c5fd2021-08-07 16:00:46 +0800303#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
304 defined(CONFIG_IMX8ULP)
Yangbo Lufa33d202019-06-21 11:42:27 +0800305 addr = virt_to_phys((void *)(data->dest));
306 if (upper_32_bits(addr))
307 printf("Error found for upper 32 bits\n");
308 else
309 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
310#else
311 esdhc_write32(&regs->dsaddr, (u32)data->dest);
312#endif
313#endif
314 } else {
315#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
316 flush_dcache_range((ulong)data->src,
317 (ulong)data->src+data->blocks
318 *data->blocksize);
319#endif
320 if (wml_value > WML_WR_WML_MAX)
321 wml_value = WML_WR_WML_MAX_VAL;
322 if (priv->wp_enable) {
323 if ((esdhc_read32(&regs->prsstat) &
324 PRSSTAT_WPSPL) == 0) {
325 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
326 return -ETIMEDOUT;
327 }
328 } else {
Simon Glassbcee8d62019-12-06 21:41:35 -0700329#if CONFIG_IS_ENABLED(DM_GPIO)
330 if (dm_gpio_is_valid(&priv->wp_gpio) &&
331 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800332 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
333 return -ETIMEDOUT;
334 }
335#endif
336 }
337
338 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
339 wml_value << 16);
340#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Fan744c5fd2021-08-07 16:00:46 +0800341#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
342 defined(CONFIG_IMX8ULP)
Yangbo Lufa33d202019-06-21 11:42:27 +0800343 addr = virt_to_phys((void *)(data->src));
344 if (upper_32_bits(addr))
345 printf("Error found for upper 32 bits\n");
346 else
347 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
348#else
349 esdhc_write32(&regs->dsaddr, (u32)data->src);
350#endif
351#endif
352 }
353
354 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
355
356 /* Calculate the timeout period for data transactions */
357 /*
358 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
359 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
360 * So, Number of SD Clock cycles for 0.25sec should be minimum
361 * (SD Clock/sec * 0.25 sec) SD Clock cycles
362 * = (mmc->clock * 1/4) SD Clock cycles
363 * As 1) >= 2)
364 * => (2^(timeout+13)) >= mmc->clock * 1/4
365 * Taking log2 both the sides
366 * => timeout + 13 >= log2(mmc->clock/4)
367 * Rounding up to next power of 2
368 * => timeout + 13 = log2(mmc->clock/4) + 1
369 * => timeout + 13 = fls(mmc->clock/4)
370 *
371 * However, the MMC spec "It is strongly recommended for hosts to
372 * implement more than 500ms timeout value even if the card
373 * indicates the 250ms maximum busy length." Even the previous
374 * value of 300ms is known to be insufficient for some cards.
375 * So, we use
376 * => timeout + 13 = fls(mmc->clock/2)
377 */
378 timeout = fls(mmc->clock/2);
379 timeout -= 13;
380
381 if (timeout > 14)
382 timeout = 14;
383
384 if (timeout < 0)
385 timeout = 0;
386
387#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
388 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
389 timeout++;
390#endif
391
392#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
393 timeout = 0xE;
394#endif
395 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
396
397 return 0;
398}
399
400static void check_and_invalidate_dcache_range
401 (struct mmc_cmd *cmd,
402 struct mmc_data *data) {
403 unsigned start = 0;
404 unsigned end = 0;
405 unsigned size = roundup(ARCH_DMA_MINALIGN,
406 data->blocks*data->blocksize);
Peng Fan744c5fd2021-08-07 16:00:46 +0800407#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
408 defined(CONFIG_IMX8ULP)
Yangbo Lufa33d202019-06-21 11:42:27 +0800409 dma_addr_t addr;
410
411 addr = virt_to_phys((void *)(data->dest));
412 if (upper_32_bits(addr))
413 printf("Error found for upper 32 bits\n");
414 else
415 start = lower_32_bits(addr);
416#else
417 start = (unsigned)data->dest;
418#endif
419 end = start + size;
420 invalidate_dcache_range(start, end);
421}
422
423#ifdef CONFIG_MCF5441x
424/*
425 * Swaps 32-bit words to little-endian byte order.
426 */
427static inline void sd_swap_dma_buff(struct mmc_data *data)
428{
429 int i, size = data->blocksize >> 2;
430 u32 *buffer = (u32 *)data->dest;
431 u32 sw;
432
433 while (data->blocks--) {
434 for (i = 0; i < size; i++) {
435 sw = __sw32(*buffer);
436 *buffer++ = sw;
437 }
438 }
439}
440#endif
441
442/*
443 * Sends a command out on the bus. Takes the mmc pointer,
444 * a command pointer, and an optional data pointer.
445 */
446static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
447 struct mmc_cmd *cmd, struct mmc_data *data)
448{
449 int err = 0;
450 uint xfertyp;
451 uint irqstat;
452 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
453 struct fsl_esdhc *regs = priv->esdhc_regs;
454 unsigned long start;
455
456#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
457 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
458 return 0;
459#endif
460
461 esdhc_write32(&regs->irqstat, -1);
462
463 sync();
464
465 /* Wait for the bus to be idle */
466 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
467 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
468 ;
469
470 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
471 ;
472
Yangbo Lufa33d202019-06-21 11:42:27 +0800473 /* Set up for a data transfer if we have one */
474 if (data) {
475 err = esdhc_setup_data(priv, mmc, data);
476 if(err)
477 return err;
478
479 if (data->flags & MMC_DATA_READ)
480 check_and_invalidate_dcache_range(cmd, data);
481 }
482
483 /* Figure out the transfer arguments */
484 xfertyp = esdhc_xfertyp(cmd, data);
485
486 /* Mask all irqs */
487 esdhc_write32(&regs->irqsigen, 0);
488
489 /* Send the command */
490 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
491#if defined(CONFIG_FSL_USDHC)
492 esdhc_write32(&regs->mixctrl,
493 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
494 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
495 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
496#else
497 esdhc_write32(&regs->xfertyp, xfertyp);
498#endif
499
500 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
501 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
502 flags = IRQSTAT_BRR;
503
504 /* Wait for the command to complete */
505 start = get_timer(0);
506 while (!(esdhc_read32(&regs->irqstat) & flags)) {
507 if (get_timer(start) > 1000) {
508 err = -ETIMEDOUT;
509 goto out;
510 }
511 }
512
513 irqstat = esdhc_read32(&regs->irqstat);
514
515 if (irqstat & CMD_ERR) {
516 err = -ECOMM;
517 goto out;
518 }
519
520 if (irqstat & IRQSTAT_CTOE) {
521 err = -ETIMEDOUT;
522 goto out;
523 }
524
Yangbo Lufa33d202019-06-21 11:42:27 +0800525 /* Workaround for ESDHC errata ENGcm03648 */
526 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan356f7822019-07-10 09:35:30 +0000527 int timeout = 50000;
Yangbo Lufa33d202019-06-21 11:42:27 +0800528
Peng Fan356f7822019-07-10 09:35:30 +0000529 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lufa33d202019-06-21 11:42:27 +0800530 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
531 PRSSTAT_DAT0)) {
532 udelay(100);
533 timeout--;
534 }
535
536 if (timeout <= 0) {
537 printf("Timeout waiting for DAT0 to go high!\n");
538 err = -ETIMEDOUT;
539 goto out;
540 }
541 }
542
543 /* Copy the response to the response buffer */
544 if (cmd->resp_type & MMC_RSP_136) {
545 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
546
547 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
548 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
549 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
550 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
551 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
552 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
553 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
554 cmd->response[3] = (cmdrsp0 << 8);
555 } else
556 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
557
558 /* Wait until all of the blocks are transferred */
559 if (data) {
560#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
561 esdhc_pio_read_write(priv, data);
562#else
563 flags = DATA_COMPLETE;
564 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
565 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
566 flags = IRQSTAT_BRR;
567 }
568
569 do {
570 irqstat = esdhc_read32(&regs->irqstat);
571
572 if (irqstat & IRQSTAT_DTOE) {
573 err = -ETIMEDOUT;
574 goto out;
575 }
576
577 if (irqstat & DATA_ERR) {
578 err = -ECOMM;
579 goto out;
580 }
581 } while ((irqstat & flags) != flags);
582
583 /*
584 * Need invalidate the dcache here again to avoid any
585 * cache-fill during the DMA operations such as the
586 * speculative pre-fetching etc.
587 */
588 if (data->flags & MMC_DATA_READ) {
589 check_and_invalidate_dcache_range(cmd, data);
590#ifdef CONFIG_MCF5441x
591 sd_swap_dma_buff(data);
592#endif
593 }
594#endif
595 }
596
597out:
598 /* Reset CMD and DATA portions on error */
599 if (err) {
600 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
601 SYSCTL_RSTC);
602 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
603 ;
604
605 if (data) {
606 esdhc_write32(&regs->sysctl,
607 esdhc_read32(&regs->sysctl) |
608 SYSCTL_RSTD);
609 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
610 ;
611 }
612
613 /* If this was CMD11, then notify that power cycle is needed */
614 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
615 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
616 }
617
618 esdhc_write32(&regs->irqstat, -1);
619
620 return err;
621}
622
623static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
624{
625 struct fsl_esdhc *regs = priv->esdhc_regs;
626 int div = 1;
Haibo Chenf9c3a812020-09-01 15:34:06 +0800627 u32 tmp;
628 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +0800629#ifdef ARCH_MXC
630#ifdef CONFIG_MX53
631 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
632 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
633#else
634 int pre_div = 1;
635#endif
636#else
637 int pre_div = 2;
638#endif
639 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
640 int sdhc_clk = priv->sdhc_clk;
641 uint clk;
642
Yangbo Lufa33d202019-06-21 11:42:27 +0800643 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
644 pre_div *= 2;
645
646 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
647 div++;
648
649 pre_div >>= 1;
650 div -= 1;
651
652 clk = (pre_div << 8) | (div << 4);
653
654#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300655 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800656#else
657 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
658#endif
659
660 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
661
Haibo Chenf9c3a812020-09-01 15:34:06 +0800662 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
663 if (ret)
664 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lufa33d202019-06-21 11:42:27 +0800665
666#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300667 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800668#else
669 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
670#endif
671
672 priv->clock = clock;
673}
674
Yangbo Lufa33d202019-06-21 11:42:27 +0800675#ifdef MMC_SUPPORTS_TUNING
676static int esdhc_change_pinstate(struct udevice *dev)
677{
678 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
679 int ret;
680
681 switch (priv->mode) {
682 case UHS_SDR50:
683 case UHS_DDR50:
684 ret = pinctrl_select_state(dev, "state_100mhz");
685 break;
686 case UHS_SDR104:
687 case MMC_HS_200:
688 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000689 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800690 ret = pinctrl_select_state(dev, "state_200mhz");
691 break;
692 default:
693 ret = pinctrl_select_state(dev, "default");
694 break;
695 }
696
697 if (ret)
698 printf("%s %d error\n", __func__, priv->mode);
699
700 return ret;
701}
702
703static void esdhc_reset_tuning(struct mmc *mmc)
704{
705 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
706 struct fsl_esdhc *regs = priv->esdhc_regs;
707
708 if (priv->flags & ESDHC_FLAG_USDHC) {
709 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
710 esdhc_clrbits32(&regs->autoc12err,
711 MIX_CTRL_SMPCLK_SEL |
712 MIX_CTRL_EXE_TUNE);
713 }
714 }
715}
716
717static void esdhc_set_strobe_dll(struct mmc *mmc)
718{
719 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
720 struct fsl_esdhc *regs = priv->esdhc_regs;
721 u32 val;
722
723 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800724 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300725 /* clear the reset bit on strobe dll before any setting */
726 esdhc_write32(&regs->strobe_dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +0800727
728 /*
729 * enable strobe dll ctrl and adjust the delay target
730 * for the uSDHC loopback read clock
731 */
732 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300733 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
Yangbo Lufa33d202019-06-21 11:42:27 +0800734 (priv->strobe_dll_delay_target <<
735 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chenc7f44182020-09-30 15:52:23 +0800736 esdhc_write32(&regs->strobe_dllctrl, val);
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300737 /* wait 5us to make sure strobe dll status register stable */
738 mdelay(5);
Haibo Chenc7f44182020-09-30 15:52:23 +0800739 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lufa33d202019-06-21 11:42:27 +0800740 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
741 pr_warn("HS400 strobe DLL status REF not lock!\n");
742 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
743 pr_warn("HS400 strobe DLL status SLV not lock!\n");
744 }
745}
746
747static int esdhc_set_timing(struct mmc *mmc)
748{
749 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
750 struct fsl_esdhc *regs = priv->esdhc_regs;
751 u32 mixctrl;
752
Haibo Chenc7f44182020-09-30 15:52:23 +0800753 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800754 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
755
756 switch (mmc->selected_mode) {
757 case MMC_LEGACY:
Yangbo Lufa33d202019-06-21 11:42:27 +0800758 esdhc_reset_tuning(mmc);
Haibo Chenc7f44182020-09-30 15:52:23 +0800759 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800760 break;
761 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000762 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800763 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800764 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800765 break;
766 case MMC_HS:
767 case MMC_HS_52:
768 case MMC_HS_200:
769 case SD_HS:
770 case UHS_SDR12:
771 case UHS_SDR25:
772 case UHS_SDR50:
773 case UHS_SDR104:
Haibo Chenc7f44182020-09-30 15:52:23 +0800774 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800775 break;
776 case UHS_DDR50:
777 case MMC_DDR_52:
778 mixctrl |= MIX_CTRL_DDREN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800779 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800780 break;
781 default:
782 printf("Not supported %d\n", mmc->selected_mode);
783 return -EINVAL;
784 }
785
786 priv->mode = mmc->selected_mode;
787
788 return esdhc_change_pinstate(mmc->dev);
789}
790
791static int esdhc_set_voltage(struct mmc *mmc)
792{
793 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
794 struct fsl_esdhc *regs = priv->esdhc_regs;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100795#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800796 int ret;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100797#endif
Yangbo Lufa33d202019-06-21 11:42:27 +0800798
799 priv->signal_voltage = mmc->signal_voltage;
800 switch (mmc->signal_voltage) {
801 case MMC_SIGNAL_VOLTAGE_330:
802 if (priv->vs18_enable)
Marek Vasut50a17a62020-05-22 18:28:33 +0200803 return -ENOTSUPP;
Yangbo Lufa33d202019-06-21 11:42:27 +0800804#if CONFIG_IS_ENABLED(DM_REGULATOR)
805 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
806 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
807 if (ret) {
808 printf("Setting to 3.3V error");
809 return -EIO;
810 }
811 /* Wait for 5ms */
812 mdelay(5);
813 }
814#endif
815
816 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
817 if (!(esdhc_read32(&regs->vendorspec) &
818 ESDHC_VENDORSPEC_VSELECT))
819 return 0;
820
821 return -EAGAIN;
822 case MMC_SIGNAL_VOLTAGE_180:
823#if CONFIG_IS_ENABLED(DM_REGULATOR)
824 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
825 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
826 if (ret) {
827 printf("Setting to 1.8V error");
828 return -EIO;
829 }
830 }
831#endif
832 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
Haibo Chen8974ff12021-03-22 18:55:38 +0800833 /*
834 * some board like imx8mm-evk need about 18ms to switch
835 * the IO voltage from 3.3v to 1.8v, common code only
836 * delay 10ms, so need to delay extra time to make sure
837 * the IO voltage change to 1.8v.
838 */
839 if (priv->signal_voltage_switch_extra_delay_ms)
840 mdelay(priv->signal_voltage_switch_extra_delay_ms);
Yangbo Lufa33d202019-06-21 11:42:27 +0800841 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
842 return 0;
843
844 return -EAGAIN;
845 case MMC_SIGNAL_VOLTAGE_120:
846 return -ENOTSUPP;
847 default:
848 return 0;
849 }
850}
851
852static void esdhc_stop_tuning(struct mmc *mmc)
853{
854 struct mmc_cmd cmd;
855
856 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
857 cmd.cmdarg = 0;
858 cmd.resp_type = MMC_RSP_R1b;
859
Jaehoon Chung2da23352021-05-31 08:31:49 +0900860 mmc_send_cmd(mmc, &cmd, NULL);
Yangbo Lufa33d202019-06-21 11:42:27 +0800861}
862
863static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
864{
Simon Glassc69cda22020-12-03 16:55:20 -0700865 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +0800866 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
867 struct fsl_esdhc *regs = priv->esdhc_regs;
868 struct mmc *mmc = &plat->mmc;
Haibo Chenc7f44182020-09-30 15:52:23 +0800869 u32 irqstaten = esdhc_read32(&regs->irqstaten);
870 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800871 int i, ret = -ETIMEDOUT;
872 u32 val, mixctrl;
873
874 /* clock tuning is not needed for upto 52MHz */
875 if (mmc->clock <= 52000000)
876 return 0;
877
878 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
879 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800880 val = esdhc_read32(&regs->autoc12err);
881 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800882 val &= ~MIX_CTRL_SMPCLK_SEL;
883 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
884
885 val |= MIX_CTRL_EXE_TUNE;
886 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
887
Haibo Chenc7f44182020-09-30 15:52:23 +0800888 esdhc_write32(&regs->autoc12err, val);
889 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800890 }
891
892 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chenc7f44182020-09-30 15:52:23 +0800893 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800894 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800895 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800896
Haibo Chenc7f44182020-09-30 15:52:23 +0800897 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
898 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lufa33d202019-06-21 11:42:27 +0800899
900 /*
901 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
902 * of loops reaches 40 times.
903 */
904 for (i = 0; i < MAX_TUNING_LOOP; i++) {
905 u32 ctrl;
906
907 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
908 if (mmc->bus_width == 8)
Haibo Chenc7f44182020-09-30 15:52:23 +0800909 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lufa33d202019-06-21 11:42:27 +0800910 else if (mmc->bus_width == 4)
Haibo Chenc7f44182020-09-30 15:52:23 +0800911 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800912 } else {
Haibo Chenc7f44182020-09-30 15:52:23 +0800913 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800914 }
915
916 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chenc7f44182020-09-30 15:52:23 +0800917 val = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800918 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800919 esdhc_write32(&regs->mixctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800920
921 /* We are using STD tuning, no need to check return value */
922 mmc_send_tuning(mmc, opcode, NULL);
923
Haibo Chenc7f44182020-09-30 15:52:23 +0800924 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lufa33d202019-06-21 11:42:27 +0800925 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
926 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800927 ret = 0;
928 break;
929 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800930 }
931
Haibo Chenc7f44182020-09-30 15:52:23 +0800932 esdhc_write32(&regs->irqstaten, irqstaten);
933 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800934
935 esdhc_stop_tuning(mmc);
936
937 return ret;
938}
939#endif
940
941static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
942{
943 struct fsl_esdhc *regs = priv->esdhc_regs;
944 int ret __maybe_unused;
Peng Fan1d01c982019-11-04 17:14:15 +0800945 u32 clock;
Yangbo Lufa33d202019-06-21 11:42:27 +0800946
Haibo Chen5d772192020-11-03 17:18:35 +0800947#ifdef MMC_SUPPORTS_TUNING
948 /*
949 * call esdhc_set_timing() before update the clock rate,
950 * This is because current we support DDR and SDR mode,
951 * Once the DDR_EN bit is set, the card clock will be
952 * divide by 2 automatically. So need to do this before
953 * setting clock rate.
954 */
955 if (priv->mode != mmc->selected_mode) {
956 ret = esdhc_set_timing(mmc);
957 if (ret) {
958 printf("esdhc_set_timing error %d\n", ret);
959 return ret;
960 }
961 }
962#endif
963
Yangbo Lufa33d202019-06-21 11:42:27 +0800964 /* Set the clock speed */
Peng Fan1d01c982019-11-04 17:14:15 +0800965 clock = mmc->clock;
966 if (clock < mmc->cfg->f_min)
967 clock = mmc->cfg->f_min;
968
969 if (priv->clock != clock)
970 set_sysctl(priv, mmc, clock);
Yangbo Lufa33d202019-06-21 11:42:27 +0800971
Yangbo Lufa33d202019-06-21 11:42:27 +0800972 if (mmc->clk_disable) {
973#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300974 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800975#else
976 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
977#endif
978 } else {
979#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300980 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
981 VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800982#else
983 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
984#endif
985 }
986
Ye Li9b7c3492021-08-17 17:09:20 +0800987#ifdef MMC_SUPPORTS_TUNING
Haibo Chen5d772192020-11-03 17:18:35 +0800988 /*
989 * For HS400/HS400ES mode, make sure set the strobe dll in the
990 * target clock rate. So call esdhc_set_strobe_dll() after the
991 * clock updated.
992 */
993 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
994 esdhc_set_strobe_dll(mmc);
Yangbo Lufa33d202019-06-21 11:42:27 +0800995
996 if (priv->signal_voltage != mmc->signal_voltage) {
997 ret = esdhc_set_voltage(mmc);
998 if (ret) {
Marek Vasut50a17a62020-05-22 18:28:33 +0200999 if (ret != -ENOTSUPP)
1000 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lufa33d202019-06-21 11:42:27 +08001001 return ret;
1002 }
1003 }
1004#endif
1005
1006 /* Set the bus width */
1007 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
1008
1009 if (mmc->bus_width == 4)
1010 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
1011 else if (mmc->bus_width == 8)
1012 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1013
1014 return 0;
1015}
1016
1017static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1018{
1019 struct fsl_esdhc *regs = priv->esdhc_regs;
1020 ulong start;
1021
1022 /* Reset the entire host controller */
1023 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1024
1025 /* Wait until the controller is available */
1026 start = get_timer(0);
1027 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1028 if (get_timer(start) > 1000)
1029 return -ETIMEDOUT;
1030 }
1031
1032#if defined(CONFIG_FSL_USDHC)
1033 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1034 esdhc_write32(&regs->mmcboot, 0x0);
1035 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1036 esdhc_write32(&regs->mixctrl, 0x0);
1037 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1038
1039 /* Put VEND_SPEC to default value */
1040 if (priv->vs18_enable)
1041 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1042 ESDHC_VENDORSPEC_VSELECT));
1043 else
1044 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1045
1046 /* Disable DLL_CTRL delay line */
1047 esdhc_write32(&regs->dllctrl, 0x0);
1048#endif
1049
1050#ifndef ARCH_MXC
1051 /* Enable cache snooping */
1052 esdhc_write32(&regs->scr, 0x00000040);
1053#endif
1054
1055#ifndef CONFIG_FSL_USDHC
1056 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1057#else
Fabio Estevamf132aab2021-06-07 17:40:09 -03001058 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Yangbo Lufa33d202019-06-21 11:42:27 +08001059#endif
1060
1061 /* Set the initial clock speed */
1062 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1063
1064 /* Disable the BRR and BWR bits in IRQSTAT */
1065 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1066
1067#ifdef CONFIG_MCF5441x
1068 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1069#else
1070 /* Put the PROCTL reg back to the default */
1071 esdhc_write32(&regs->proctl, PROCTL_INIT);
1072#endif
1073
1074 /* Set timout to the maximum value */
1075 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1076
1077 return 0;
1078}
1079
1080static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1081{
1082 struct fsl_esdhc *regs = priv->esdhc_regs;
1083 int timeout = 1000;
1084
1085#ifdef CONFIG_ESDHC_DETECT_QUIRK
1086 if (CONFIG_ESDHC_DETECT_QUIRK)
1087 return 1;
1088#endif
1089
1090#if CONFIG_IS_ENABLED(DM_MMC)
1091 if (priv->non_removable)
1092 return 1;
Fabio Estevam29230f32020-01-06 20:11:27 -03001093
1094 if (priv->broken_cd)
1095 return 1;
Simon Glassbcee8d62019-12-06 21:41:35 -07001096#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001097 if (dm_gpio_is_valid(&priv->cd_gpio))
1098 return dm_gpio_get_value(&priv->cd_gpio);
1099#endif
1100#endif
1101
1102 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1103 udelay(1000);
1104
1105 return timeout > 0;
1106}
1107
1108static int esdhc_reset(struct fsl_esdhc *regs)
1109{
1110 ulong start;
1111
1112 /* reset the controller */
1113 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1114
1115 /* hardware clears the bit when it is done */
1116 start = get_timer(0);
1117 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1118 if (get_timer(start) > 100) {
1119 printf("MMC/SD: Reset never completed.\n");
1120 return -ETIMEDOUT;
1121 }
1122 }
1123
1124 return 0;
1125}
1126
1127#if !CONFIG_IS_ENABLED(DM_MMC)
1128static int esdhc_getcd(struct mmc *mmc)
1129{
1130 struct fsl_esdhc_priv *priv = mmc->priv;
1131
1132 return esdhc_getcd_common(priv);
1133}
1134
1135static int esdhc_init(struct mmc *mmc)
1136{
1137 struct fsl_esdhc_priv *priv = mmc->priv;
1138
1139 return esdhc_init_common(priv, mmc);
1140}
1141
1142static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1143 struct mmc_data *data)
1144{
1145 struct fsl_esdhc_priv *priv = mmc->priv;
1146
1147 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1148}
1149
1150static int esdhc_set_ios(struct mmc *mmc)
1151{
1152 struct fsl_esdhc_priv *priv = mmc->priv;
1153
1154 return esdhc_set_ios_common(priv, mmc);
1155}
1156
1157static const struct mmc_ops esdhc_ops = {
1158 .getcd = esdhc_getcd,
1159 .init = esdhc_init,
1160 .send_cmd = esdhc_send_cmd,
1161 .set_ios = esdhc_set_ios,
1162};
1163#endif
1164
1165static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1166 struct fsl_esdhc_plat *plat)
1167{
1168 struct mmc_config *cfg;
1169 struct fsl_esdhc *regs;
1170 u32 caps, voltage_caps;
1171 int ret;
1172
1173 if (!priv)
1174 return -EINVAL;
1175
1176 regs = priv->esdhc_regs;
1177
1178 /* First reset the eSDHC controller */
1179 ret = esdhc_reset(regs);
1180 if (ret)
1181 return ret;
1182
1183#ifdef CONFIG_MCF5441x
1184 /* ColdFire, using SDHC_DATA[3] for card detection */
1185 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1186#endif
1187
1188#ifndef CONFIG_FSL_USDHC
1189 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1190 | SYSCTL_IPGEN | SYSCTL_CKEN);
1191 /* Clearing tuning bits in case ROM has set it already */
1192 esdhc_write32(&regs->mixctrl, 0);
1193 esdhc_write32(&regs->autoc12err, 0);
1194 esdhc_write32(&regs->clktunectrlstatus, 0);
1195#else
Fabio Estevamf132aab2021-06-07 17:40:09 -03001196 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1197 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +08001198#endif
1199
1200 if (priv->vs18_enable)
1201 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1202
Haibo Chenc7f44182020-09-30 15:52:23 +08001203 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lufa33d202019-06-21 11:42:27 +08001204 cfg = &plat->cfg;
1205#ifndef CONFIG_DM_MMC
1206 memset(cfg, '\0', sizeof(*cfg));
1207#endif
1208
1209 voltage_caps = 0;
1210 caps = esdhc_read32(&regs->hostcapblt);
1211
1212#ifdef CONFIG_MCF5441x
1213 /*
1214 * MCF5441x RM declares in more points that sdhc clock speed must
1215 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1216 * from host capabilities.
1217 */
1218 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1219#endif
1220
1221#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1222 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1223 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1224#endif
1225
Yangbo Lufa33d202019-06-21 11:42:27 +08001226 if (caps & ESDHC_HOSTCAPBLT_VS18)
1227 voltage_caps |= MMC_VDD_165_195;
1228 if (caps & ESDHC_HOSTCAPBLT_VS30)
1229 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1230 if (caps & ESDHC_HOSTCAPBLT_VS33)
1231 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1232
1233 cfg->name = "FSL_SDHC";
1234#if !CONFIG_IS_ENABLED(DM_MMC)
1235 cfg->ops = &esdhc_ops;
1236#endif
1237#ifdef CONFIG_SYS_SD_VOLTAGE
1238 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1239#else
1240 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1241#endif
1242 if ((cfg->voltages & voltage_caps) == 0) {
1243 printf("voltage not supported by controller\n");
1244 return -1;
1245 }
1246
1247 if (priv->bus_width == 8)
1248 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1249 else if (priv->bus_width == 4)
1250 cfg->host_caps = MMC_MODE_4BIT;
1251
1252 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1253#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1254 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1255#endif
1256
1257 if (priv->bus_width > 0) {
1258 if (priv->bus_width < 8)
1259 cfg->host_caps &= ~MMC_MODE_8BIT;
1260 if (priv->bus_width < 4)
1261 cfg->host_caps &= ~MMC_MODE_4BIT;
1262 }
1263
1264 if (caps & ESDHC_HOSTCAPBLT_HSS)
1265 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1266
1267#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1268 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1269 cfg->host_caps &= ~MMC_MODE_8BIT;
1270#endif
1271
1272 cfg->host_caps |= priv->caps;
1273
1274 cfg->f_min = 400000;
1275 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1276
1277 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1278
Haibo Chenc7f44182020-09-30 15:52:23 +08001279 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +08001280 if (priv->flags & ESDHC_FLAG_USDHC) {
1281 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +08001282 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +08001283
1284 val |= ESDHC_STD_TUNING_EN;
1285 val &= ~ESDHC_TUNING_START_TAP_MASK;
1286 val |= priv->tuning_start_tap;
1287 val &= ~ESDHC_TUNING_STEP_MASK;
1288 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chenba616762020-06-22 19:38:04 +08001289
1290 /* Disable the CMD CRC check for tuning, if not, need to
1291 * add some delay after every tuning command, because
1292 * hardware standard tuning logic will directly go to next
1293 * step once it detect the CMD CRC error, will not wait for
1294 * the card side to finally send out the tuning data, trigger
1295 * the buffer read ready interrupt immediately. If usdhc send
1296 * the next tuning command some eMMC card will stuck, can't
1297 * response, block the tuning procedure or the first command
1298 * after the whole tuning procedure always can't get any response.
1299 */
1300 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chenc7f44182020-09-30 15:52:23 +08001301 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +08001302 }
1303 }
1304
1305 return 0;
1306}
1307
1308#if !CONFIG_IS_ENABLED(DM_MMC)
1309static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1310 struct fsl_esdhc_priv *priv)
1311{
1312 if (!cfg || !priv)
1313 return -EINVAL;
1314
1315 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1316 priv->bus_width = cfg->max_bus_width;
1317 priv->sdhc_clk = cfg->sdhc_clk;
1318 priv->wp_enable = cfg->wp_enable;
1319 priv->vs18_enable = cfg->vs18_enable;
1320
1321 return 0;
1322};
1323
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001324int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lufa33d202019-06-21 11:42:27 +08001325{
1326 struct fsl_esdhc_plat *plat;
1327 struct fsl_esdhc_priv *priv;
1328 struct mmc *mmc;
1329 int ret;
1330
1331 if (!cfg)
1332 return -EINVAL;
1333
1334 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1335 if (!priv)
1336 return -ENOMEM;
1337 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1338 if (!plat) {
1339 free(priv);
1340 return -ENOMEM;
1341 }
1342
1343 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1344 if (ret) {
1345 debug("%s xlate failure\n", __func__);
1346 free(plat);
1347 free(priv);
1348 return ret;
1349 }
1350
1351 ret = fsl_esdhc_init(priv, plat);
1352 if (ret) {
1353 debug("%s init failure\n", __func__);
1354 free(plat);
1355 free(priv);
1356 return ret;
1357 }
1358
1359 mmc = mmc_create(&plat->cfg, priv);
1360 if (!mmc)
1361 return -EIO;
1362
1363 priv->mmc = mmc;
1364
1365 return 0;
1366}
1367
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001368int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lufa33d202019-06-21 11:42:27 +08001369{
1370 struct fsl_esdhc_cfg *cfg;
1371
1372 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1373 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1374 cfg->sdhc_clk = gd->arch.sdhc_clk;
1375 return fsl_esdhc_initialize(bis, cfg);
1376}
1377#endif
1378
Yangbo Lufa33d202019-06-21 11:42:27 +08001379#ifdef CONFIG_OF_LIBFDT
1380__weak int esdhc_status_fixup(void *blob, const char *compat)
1381{
1382#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1383 if (!hwconfig("esdhc")) {
1384 do_fixup_by_compat(blob, compat, "status", "disabled",
1385 sizeof("disabled"), 1);
1386 return 1;
1387 }
1388#endif
1389 return 0;
1390}
1391
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001392void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lufa33d202019-06-21 11:42:27 +08001393{
1394 const char *compat = "fsl,esdhc";
1395
1396 if (esdhc_status_fixup(blob, compat))
1397 return;
1398
Yangbo Lufa33d202019-06-21 11:42:27 +08001399 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1400 gd->arch.sdhc_clk, 1);
Yangbo Lufa33d202019-06-21 11:42:27 +08001401}
1402#endif
1403
1404#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +08001405#include <asm/arch/clock.h>
Yangbo Lufa33d202019-06-21 11:42:27 +08001406__weak void init_clk_usdhc(u32 index)
1407{
1408}
1409
Simon Glassd1998a92020-12-03 16:55:21 -07001410static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lufa33d202019-06-21 11:42:27 +08001411{
Yangbo Lufa33d202019-06-21 11:42:27 +08001412 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001413#if CONFIG_IS_ENABLED(DM_REGULATOR)
1414 struct udevice *vqmmc_dev;
Walter Lozano23721772020-07-29 12:31:17 -03001415 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001416#endif
Walter Lozano23721772020-07-29 12:31:17 -03001417 const void *fdt = gd->fdt_blob;
1418 int node = dev_of_offset(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001419 fdt_addr_t addr;
1420 unsigned int val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001421
Simon Glassdcfc42b2021-08-07 07:24:06 -06001422 if (!CONFIG_IS_ENABLED(OF_REAL))
1423 return 0;
1424
Yangbo Lufa33d202019-06-21 11:42:27 +08001425 addr = dev_read_addr(dev);
1426 if (addr == FDT_ADDR_T_NONE)
1427 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001428 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lufa33d202019-06-21 11:42:27 +08001429 priv->dev = dev;
1430 priv->mode = -1;
Yangbo Lufa33d202019-06-21 11:42:27 +08001431
1432 val = dev_read_u32_default(dev, "bus-width", -1);
1433 if (val == 8)
1434 priv->bus_width = 8;
1435 else if (val == 4)
1436 priv->bus_width = 4;
1437 else
1438 priv->bus_width = 1;
1439
1440 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1441 priv->tuning_step = val;
1442 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1443 ESDHC_TUNING_START_TAP_DEFAULT);
1444 priv->tuning_start_tap = val;
1445 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1446 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1447 priv->strobe_dll_delay_target = val;
Haibo Chen8974ff12021-03-22 18:55:38 +08001448 val = fdtdec_get_int(fdt, node, "fsl,signal-voltage-switch-extra-delay-ms", 0);
1449 priv->signal_voltage_switch_extra_delay_ms = val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001450
Fabio Estevam29230f32020-01-06 20:11:27 -03001451 if (dev_read_bool(dev, "broken-cd"))
1452 priv->broken_cd = 1;
1453
Yangbo Lufa33d202019-06-21 11:42:27 +08001454 if (dev_read_bool(dev, "non-removable")) {
1455 priv->non_removable = 1;
1456 } else {
1457 priv->non_removable = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -07001458#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001459 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1460 GPIOD_IS_IN);
1461#endif
1462 }
1463
1464 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1465 priv->wp_enable = 1;
1466 } else {
1467 priv->wp_enable = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -07001468#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001469 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1470 GPIOD_IS_IN);
1471#endif
1472 }
1473
1474 priv->vs18_enable = 0;
1475
1476#if CONFIG_IS_ENABLED(DM_REGULATOR)
1477 /*
1478 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1479 * otherwise, emmc will work abnormally.
1480 */
1481 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1482 if (ret) {
1483 dev_dbg(dev, "no vqmmc-supply\n");
1484 } else {
Marek Vasut406df852020-05-22 18:19:08 +02001485 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001486 ret = regulator_set_enable(vqmmc_dev, true);
1487 if (ret) {
1488 dev_err(dev, "fail to enable vqmmc-supply\n");
1489 return ret;
1490 }
1491
1492 if (regulator_get_value(vqmmc_dev) == 1800000)
1493 priv->vs18_enable = 1;
1494 }
1495#endif
Simon Glassdcfc42b2021-08-07 07:24:06 -06001496
Walter Lozano23721772020-07-29 12:31:17 -03001497 return 0;
1498}
1499
1500static int fsl_esdhc_probe(struct udevice *dev)
1501{
1502 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001503 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano23721772020-07-29 12:31:17 -03001504 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1505 struct esdhc_soc_data *data =
1506 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1507 struct mmc *mmc;
Walter Lozano23721772020-07-29 12:31:17 -03001508 int ret;
1509
1510#if CONFIG_IS_ENABLED(OF_PLATDATA)
1511 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1512 unsigned int val;
1513
1514 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1515 val = plat->dtplat.bus_width;
1516 if (val == 8)
1517 priv->bus_width = 8;
1518 else if (val == 4)
1519 priv->bus_width = 4;
1520 else
1521 priv->bus_width = 1;
Walter Lozano7142ff92020-07-29 12:31:19 -03001522
1523 if (dtplat->non_removable)
1524 priv->non_removable = 1;
1525 else
1526 priv->non_removable = 0;
1527
1528 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1529 struct udevice *gpiodev;
Walter Lozano7142ff92020-07-29 12:31:19 -03001530
Simon Glasscc469b72021-03-15 17:25:28 +13001531 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
Walter Lozano7142ff92020-07-29 12:31:19 -03001532 if (ret)
1533 return ret;
1534
1535 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1536 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1537 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1538
1539 if (ret)
1540 return ret;
1541 }
Walter Lozano23721772020-07-29 12:31:17 -03001542#endif
1543
1544 if (data)
1545 priv->flags = data->flags;
Yangbo Lufa33d202019-06-21 11:42:27 +08001546
Yangbo Lufa33d202019-06-21 11:42:27 +08001547 /*
1548 * TODO:
1549 * Because lack of clk driver, if SDHC clk is not enabled,
1550 * need to enable it first before this driver is invoked.
1551 *
1552 * we use MXC_ESDHC_CLK to get clk freq.
1553 * If one would like to make this function work,
1554 * the aliases should be provided in dts as this:
1555 *
1556 * aliases {
1557 * mmc0 = &usdhc1;
1558 * mmc1 = &usdhc2;
1559 * mmc2 = &usdhc3;
1560 * mmc3 = &usdhc4;
1561 * };
1562 * Then if your board only supports mmc2 and mmc3, but we can
1563 * correctly get the seq as 2 and 3, then let mxc_get_clock
1564 * work as expected.
1565 */
1566
Simon Glass8b85dfc2020-12-16 21:20:07 -07001567 init_clk_usdhc(dev_seq(dev));
Yangbo Lufa33d202019-06-21 11:42:27 +08001568
Giulio Benettia820bed2020-01-10 15:51:45 +01001569#if CONFIG_IS_ENABLED(CLK)
1570 /* Assigned clock already set clock */
1571 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1572 if (ret) {
1573 printf("Failed to get per_clk\n");
1574 return ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001575 }
Giulio Benettia820bed2020-01-10 15:51:45 +01001576 ret = clk_enable(&priv->per_clk);
1577 if (ret) {
1578 printf("Failed to enable per_clk\n");
1579 return ret;
1580 }
1581
1582 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1583#else
Simon Glass8b85dfc2020-12-16 21:20:07 -07001584 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettia820bed2020-01-10 15:51:45 +01001585 if (priv->sdhc_clk <= 0) {
1586 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1587 return -EINVAL;
1588 }
1589#endif
Yangbo Lufa33d202019-06-21 11:42:27 +08001590
1591 ret = fsl_esdhc_init(priv, plat);
1592 if (ret) {
1593 dev_err(dev, "fsl_esdhc_init failure\n");
1594 return ret;
1595 }
1596
Simon Glassdcfc42b2021-08-07 07:24:06 -06001597 if (CONFIG_IS_ENABLED(OF_REAL)) {
1598 ret = mmc_of_parse(dev, &plat->cfg);
1599 if (ret)
1600 return ret;
1601 }
Peng Fanb0155ac2019-07-10 09:35:24 +00001602
Yangbo Lufa33d202019-06-21 11:42:27 +08001603 mmc = &plat->mmc;
1604 mmc->cfg = &plat->cfg;
1605 mmc->dev = dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001606
1607 upriv->mmc = mmc;
1608
1609 return esdhc_init_common(priv, mmc);
1610}
1611
1612#if CONFIG_IS_ENABLED(DM_MMC)
1613static int fsl_esdhc_get_cd(struct udevice *dev)
1614{
1615 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1616
1617 return esdhc_getcd_common(priv);
1618}
1619
1620static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1621 struct mmc_data *data)
1622{
Simon Glassc69cda22020-12-03 16:55:20 -07001623 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001624 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1625
1626 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1627}
1628
1629static int fsl_esdhc_set_ios(struct udevice *dev)
1630{
Simon Glassc69cda22020-12-03 16:55:20 -07001631 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001632 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1633
1634 return esdhc_set_ios_common(priv, &plat->mmc);
1635}
1636
Peng Fane9c22552019-07-10 09:35:26 +00001637#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1638static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1639{
1640 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1641 struct fsl_esdhc *regs = priv->esdhc_regs;
1642 u32 m;
1643
Haibo Chenc7f44182020-09-30 15:52:23 +08001644 m = esdhc_read32(&regs->mixctrl);
Peng Fane9c22552019-07-10 09:35:26 +00001645 m |= MIX_CTRL_HS400_ES;
Haibo Chenc7f44182020-09-30 15:52:23 +08001646 esdhc_write32(&regs->mixctrl, m);
Peng Fane9c22552019-07-10 09:35:26 +00001647
1648 return 0;
1649}
1650#endif
1651
Haibo Chenb5874b52020-11-05 14:57:13 +08001652static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1653 int timeout_us)
1654{
1655 int ret;
1656 u32 tmp;
1657 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1658 struct fsl_esdhc *regs = priv->esdhc_regs;
1659
1660 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1661 !!(tmp & PRSSTAT_DAT0) == !!state,
1662 timeout_us);
1663 return ret;
1664}
1665
Yangbo Lufa33d202019-06-21 11:42:27 +08001666static const struct dm_mmc_ops fsl_esdhc_ops = {
1667 .get_cd = fsl_esdhc_get_cd,
1668 .send_cmd = fsl_esdhc_send_cmd,
1669 .set_ios = fsl_esdhc_set_ios,
1670#ifdef MMC_SUPPORTS_TUNING
1671 .execute_tuning = fsl_esdhc_execute_tuning,
1672#endif
Peng Fane9c22552019-07-10 09:35:26 +00001673#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1674 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1675#endif
Haibo Chenb5874b52020-11-05 14:57:13 +08001676 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lufa33d202019-06-21 11:42:27 +08001677};
1678#endif
1679
1680static struct esdhc_soc_data usdhc_imx7d_data = {
1681 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1682 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1683 | ESDHC_FLAG_HS400,
Yangbo Lufa33d202019-06-21 11:42:27 +08001684};
1685
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001686static struct esdhc_soc_data usdhc_imx7ulp_data = {
1687 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +03001688 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1689 | ESDHC_FLAG_HS400,
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001690};
1691
Peng Fan609ba122019-07-10 09:35:28 +00001692static struct esdhc_soc_data usdhc_imx8qm_data = {
1693 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1694 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1695 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1696};
1697
Yangbo Lufa33d202019-06-21 11:42:27 +08001698static const struct udevice_id fsl_esdhc_ids[] = {
Fabio Estevamc3e6f992021-02-15 08:58:15 -03001699 { .compatible = "fsl,imx51-esdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001700 { .compatible = "fsl,imx53-esdhc", },
1701 { .compatible = "fsl,imx6ul-usdhc", },
1702 { .compatible = "fsl,imx6sx-usdhc", },
1703 { .compatible = "fsl,imx6sl-usdhc", },
1704 { .compatible = "fsl,imx6q-usdhc", },
1705 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001706 { .compatible = "fsl,imx7ulp-usdhc", .data = (ulong)&usdhc_imx7ulp_data,},
Peng Fan609ba122019-07-10 09:35:28 +00001707 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fanf65d0842019-11-04 17:31:17 +08001708 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1709 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1710 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti6a63a872020-01-10 15:51:46 +01001711 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001712 { .compatible = "fsl,esdhc", },
1713 { /* sentinel */ }
1714};
1715
Yangbo Lufa33d202019-06-21 11:42:27 +08001716static int fsl_esdhc_bind(struct udevice *dev)
1717{
Simon Glassc69cda22020-12-03 16:55:20 -07001718 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001719
1720 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1721}
Yangbo Lufa33d202019-06-21 11:42:27 +08001722
1723U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano45154f02020-07-29 12:31:16 -03001724 .name = "fsl_esdhc",
Yangbo Lufa33d202019-06-21 11:42:27 +08001725 .id = UCLASS_MMC,
1726 .of_match = fsl_esdhc_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07001727 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lufa33d202019-06-21 11:42:27 +08001728 .ops = &fsl_esdhc_ops,
Yangbo Lufa33d202019-06-21 11:42:27 +08001729 .bind = fsl_esdhc_bind,
Yangbo Lufa33d202019-06-21 11:42:27 +08001730 .probe = fsl_esdhc_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -07001731 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass41575d82020-12-03 16:55:17 -07001732 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lufa33d202019-06-21 11:42:27 +08001733};
Walter Lozano23721772020-07-29 12:31:17 -03001734
Simon Glassbdf8fd72020-12-28 20:34:57 -07001735DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lufa33d202019-06-21 11:42:27 +08001736#endif