blob: f4ccb19d9fa9ff585494689c89bd94b99672ef19 [file] [log] [blame]
Yangbo Lu982f4252019-06-21 11:42:27 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Yangbo Lu023d4192021-06-03 10:51:18 +08004 * Copyright 2019, 2021 NXP
Yangbo Lu982f4252019-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 Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080018#include <errno.h>
19#include <hwconfig.h>
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080021#include <mmc.h>
22#include <part.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070028#include <linux/err.h>
Yangbo Lu982f4252019-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 Lozano8aff6732020-07-29 12:31:17 -030037#include <dt-structs.h>
38#include <mapmem.h>
39#include <dm/ofnode.h>
Haibo Chene6a999b2020-09-01 15:34:06 +080040#include <linux/iopoll.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080041
Haibo Chene8801ac2021-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 Lu982f4252019-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 Lu982f4252019-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 Benetti65b5ec12020-01-10 15:51:46 +010093 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lu982f4252019-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 Lozano8aff6732020-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 Lu982f4252019-06-21 11:42:27 +0800116 struct mmc_config cfg;
117 struct mmc mmc;
118};
119
120struct esdhc_soc_data {
121 u32 flags;
Yangbo Lu982f4252019-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 Estevam7e3d8a92020-01-06 20:11:27 -0300135 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lu982f4252019-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 Chen26154952021-03-22 18:55:38 +0800144 * @signal_voltage_switch_extra_delay_ms: extra delay for IO voltage switch
Yangbo Lu982f4252019-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 Anderson80b81612022-01-12 08:18:52 +0900155#if !CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +0800156 struct mmc *mmc;
157#endif
158 struct udevice *dev;
159 int non_removable;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300160 int broken_cd;
Yangbo Lu982f4252019-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 Chen26154952021-03-22 18:55:38 +0800169 u32 signal_voltage_switch_extra_delay_ms;
Ye Li7aa20fd2019-07-11 03:29:02 +0000170#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800171 struct udevice *vqmmc_dev;
172 struct udevice *vmmc_dev;
173#endif
Simon Glassfa4689a2019-12-06 21:41:35 -0700174#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-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 Fan82d85582021-08-07 16:00:46 +0800289#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
290 defined(CONFIG_IMX8ULP)
Yangbo Lu982f4252019-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 Fan82d85582021-08-07 16:00:46 +0800303#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
304 defined(CONFIG_IMX8ULP)
Yangbo Lu982f4252019-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 Glassfa4689a2019-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 Lu982f4252019-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 Fan82d85582021-08-07 16:00:46 +0800341#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
342 defined(CONFIG_IMX8ULP)
Yangbo Lu982f4252019-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 Fan82d85582021-08-07 16:00:46 +0800407#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) || \
408 defined(CONFIG_IMX8ULP)
Yangbo Lu982f4252019-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 Lu982f4252019-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 Lu982f4252019-06-21 11:42:27 +0800525 /* Workaround for ESDHC errata ENGcm03648 */
526 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan3dbea592019-07-10 09:35:30 +0000527 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800528
Peng Fan3dbea592019-07-10 09:35:30 +0000529 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-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 Chene6a999b2020-09-01 15:34:06 +0800627 u32 tmp;
628 int ret;
Yangbo Lu982f4252019-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 Lu982f4252019-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 Estevamd6837b42021-06-07 17:40:09 -0300655 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lu982f4252019-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 Chene6a999b2020-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 Lu982f4252019-06-21 11:42:27 +0800665
666#ifdef CONFIG_FSL_USDHC
Fabio Estevamd6837b42021-06-07 17:40:09 -0300667 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Yangbo Lu982f4252019-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 Lu982f4252019-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 Fan69b9d3a2019-07-10 09:35:26 +0000689 case MMC_HS_400_ES:
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800724 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +0300725 /* clear the reset bit on strobe dll before any setting */
726 esdhc_write32(&regs->strobe_dllctrl, 0);
Yangbo Lu982f4252019-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 Suvorovd4245c22021-09-08 21:56:43 +0300733 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
Yangbo Lu982f4252019-06-21 11:42:27 +0800734 (priv->strobe_dll_delay_target <<
735 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chen920f5d02020-09-30 15:52:23 +0800736 esdhc_write32(&regs->strobe_dllctrl, val);
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +0300737 /* wait 5us to make sure strobe dll status register stable */
738 mdelay(5);
Haibo Chen920f5d02020-09-30 15:52:23 +0800739 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800753 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-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 Lu982f4252019-06-21 11:42:27 +0800758 esdhc_reset_tuning(mmc);
Haibo Chen920f5d02020-09-30 15:52:23 +0800759 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800760 break;
761 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000762 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800763 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800764 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800774 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800775 break;
776 case UHS_DDR50:
777 case MMC_DDR_52:
778 mixctrl |= MIX_CTRL_DDREN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800779 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-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 Schocher7989f602021-01-15 10:37:09 +0100795#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800796 int ret;
Heiko Schocher7989f602021-01-15 10:37:09 +0100797#endif
Yangbo Lu982f4252019-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 Vasutba79fed2020-05-22 18:28:33 +0200803 return -ENOTSUPP;
Yangbo Lu982f4252019-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 Chen26154952021-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 Lu982f4252019-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 Chung099814b2021-05-31 08:31:49 +0900860 mmc_send_cmd(mmc, &cmd, NULL);
Yangbo Lu982f4252019-06-21 11:42:27 +0800861}
862
863static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
864{
Simon Glassfa20e932020-12-03 16:55:20 -0700865 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800869 u32 irqstaten = esdhc_read32(&regs->irqstaten);
870 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800880 val = esdhc_read32(&regs->autoc12err);
881 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800888 esdhc_write32(&regs->autoc12err, val);
889 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800890 }
891
892 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chen920f5d02020-09-30 15:52:23 +0800893 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800894 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800895 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800896
Haibo Chen920f5d02020-09-30 15:52:23 +0800897 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
898 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800909 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lu982f4252019-06-21 11:42:27 +0800910 else if (mmc->bus_width == 4)
Haibo Chen920f5d02020-09-30 15:52:23 +0800911 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800912 } else {
Haibo Chen920f5d02020-09-30 15:52:23 +0800913 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800914 }
915
916 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chen920f5d02020-09-30 15:52:23 +0800917 val = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800918 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800919 esdhc_write32(&regs->mixctrl, val);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800924 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lu982f4252019-06-21 11:42:27 +0800925 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
926 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800927 ret = 0;
928 break;
929 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800930 }
931
Haibo Chen920f5d02020-09-30 15:52:23 +0800932 esdhc_write32(&regs->irqstaten, irqstaten);
933 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lu982f4252019-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 Fan2cdf52b2019-11-04 17:14:15 +0800945 u32 clock;
Yangbo Lu982f4252019-06-21 11:42:27 +0800946
Haibo Cheneaa2f102020-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 Lu982f4252019-06-21 11:42:27 +0800964 /* Set the clock speed */
Peng Fan2cdf52b2019-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 Lu982f4252019-06-21 11:42:27 +0800971
Yangbo Lu982f4252019-06-21 11:42:27 +0800972 if (mmc->clk_disable) {
973#ifdef CONFIG_FSL_USDHC
Fabio Estevamd6837b42021-06-07 17:40:09 -0300974 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +0800975#else
976 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
977#endif
978 } else {
979#ifdef CONFIG_FSL_USDHC
Fabio Estevamd6837b42021-06-07 17:40:09 -0300980 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
981 VENDORSPEC_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +0800982#else
983 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
984#endif
985 }
986
Ye Li423483d2021-08-17 17:09:20 +0800987#ifdef MMC_SUPPORTS_TUNING
Haibo Cheneaa2f102020-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 Lu982f4252019-06-21 11:42:27 +0800995
996 if (priv->signal_voltage != mmc->signal_voltage) {
997 ret = esdhc_set_voltage(mmc);
998 if (ret) {
Marek Vasutba79fed2020-05-22 18:28:33 +0200999 if (ret != -ENOTSUPP)
1000 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lu982f4252019-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 Estevamd6837b42021-06-07 17:40:09 -03001058 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Yangbo Lu982f4252019-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 Estevam7e3d8a92020-01-06 20:11:27 -03001093
1094 if (priv->broken_cd)
1095 return 1;
Simon Glassfa4689a2019-12-06 21:41:35 -07001096#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-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;
Sean Anderson6dff7b72021-11-23 15:03:38 -05001170 u32 caps;
Yangbo Lu982f4252019-06-21 11:42:27 +08001171 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 Estevamd6837b42021-06-07 17:40:09 -03001196 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1197 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +08001198#endif
1199
1200 if (priv->vs18_enable)
1201 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1202
Haibo Chen920f5d02020-09-30 15:52:23 +08001203 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lu982f4252019-06-21 11:42:27 +08001204 cfg = &plat->cfg;
1205#ifndef CONFIG_DM_MMC
1206 memset(cfg, '\0', sizeof(*cfg));
1207#endif
1208
Yangbo Lu982f4252019-06-21 11:42:27 +08001209 caps = esdhc_read32(&regs->hostcapblt);
Yangbo Lu982f4252019-06-21 11:42:27 +08001210#ifdef CONFIG_MCF5441x
1211 /*
1212 * MCF5441x RM declares in more points that sdhc clock speed must
1213 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1214 * from host capabilities.
1215 */
1216 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1217#endif
1218
1219#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
Sean Anderson6dff7b72021-11-23 15:03:38 -05001220 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
1221#endif
1222
1223#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1224 caps |= HOSTCAPBLT_VS33;
Yangbo Lu982f4252019-06-21 11:42:27 +08001225#endif
1226
Sean Anderson6dff7b72021-11-23 15:03:38 -05001227 if (caps & HOSTCAPBLT_VS18)
1228 cfg->voltages |= MMC_VDD_165_195;
1229 if (caps & HOSTCAPBLT_VS30)
1230 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
1231 if (caps & HOSTCAPBLT_VS33)
1232 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Yangbo Lu982f4252019-06-21 11:42:27 +08001233
1234 cfg->name = "FSL_SDHC";
1235#if !CONFIG_IS_ENABLED(DM_MMC)
1236 cfg->ops = &esdhc_ops;
1237#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001238 if (priv->bus_width == 8)
1239 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1240 else if (priv->bus_width == 4)
1241 cfg->host_caps = MMC_MODE_4BIT;
1242
1243 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1244#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1245 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1246#endif
1247
1248 if (priv->bus_width > 0) {
1249 if (priv->bus_width < 8)
1250 cfg->host_caps &= ~MMC_MODE_8BIT;
1251 if (priv->bus_width < 4)
1252 cfg->host_caps &= ~MMC_MODE_4BIT;
1253 }
1254
Sean Anderson6dff7b72021-11-23 15:03:38 -05001255 if (caps & HOSTCAPBLT_HSS)
Yangbo Lu982f4252019-06-21 11:42:27 +08001256 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1257
1258#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1259 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1260 cfg->host_caps &= ~MMC_MODE_8BIT;
1261#endif
1262
1263 cfg->host_caps |= priv->caps;
1264
1265 cfg->f_min = 400000;
1266 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1267
1268 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1269
Haibo Chen920f5d02020-09-30 15:52:23 +08001270 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lu982f4252019-06-21 11:42:27 +08001271 if (priv->flags & ESDHC_FLAG_USDHC) {
1272 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +08001273 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +08001274
1275 val |= ESDHC_STD_TUNING_EN;
1276 val &= ~ESDHC_TUNING_START_TAP_MASK;
1277 val |= priv->tuning_start_tap;
1278 val &= ~ESDHC_TUNING_STEP_MASK;
1279 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chen43162c32020-06-22 19:38:04 +08001280
1281 /* Disable the CMD CRC check for tuning, if not, need to
1282 * add some delay after every tuning command, because
1283 * hardware standard tuning logic will directly go to next
1284 * step once it detect the CMD CRC error, will not wait for
1285 * the card side to finally send out the tuning data, trigger
1286 * the buffer read ready interrupt immediately. If usdhc send
1287 * the next tuning command some eMMC card will stuck, can't
1288 * response, block the tuning procedure or the first command
1289 * after the whole tuning procedure always can't get any response.
1290 */
1291 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chen920f5d02020-09-30 15:52:23 +08001292 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +08001293 }
1294 }
1295
1296 return 0;
1297}
1298
1299#if !CONFIG_IS_ENABLED(DM_MMC)
1300static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1301 struct fsl_esdhc_priv *priv)
1302{
1303 if (!cfg || !priv)
1304 return -EINVAL;
1305
1306 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1307 priv->bus_width = cfg->max_bus_width;
1308 priv->sdhc_clk = cfg->sdhc_clk;
1309 priv->wp_enable = cfg->wp_enable;
1310 priv->vs18_enable = cfg->vs18_enable;
1311
1312 return 0;
1313};
1314
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001315int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu982f4252019-06-21 11:42:27 +08001316{
1317 struct fsl_esdhc_plat *plat;
1318 struct fsl_esdhc_priv *priv;
1319 struct mmc *mmc;
1320 int ret;
1321
1322 if (!cfg)
1323 return -EINVAL;
1324
1325 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1326 if (!priv)
1327 return -ENOMEM;
1328 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1329 if (!plat) {
1330 free(priv);
1331 return -ENOMEM;
1332 }
1333
1334 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1335 if (ret) {
1336 debug("%s xlate failure\n", __func__);
1337 free(plat);
1338 free(priv);
1339 return ret;
1340 }
1341
1342 ret = fsl_esdhc_init(priv, plat);
1343 if (ret) {
1344 debug("%s init failure\n", __func__);
1345 free(plat);
1346 free(priv);
1347 return ret;
1348 }
1349
1350 mmc = mmc_create(&plat->cfg, priv);
1351 if (!mmc)
1352 return -EIO;
1353
1354 priv->mmc = mmc;
1355
1356 return 0;
1357}
1358
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001359int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu982f4252019-06-21 11:42:27 +08001360{
1361 struct fsl_esdhc_cfg *cfg;
1362
1363 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1364 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1365 cfg->sdhc_clk = gd->arch.sdhc_clk;
1366 return fsl_esdhc_initialize(bis, cfg);
1367}
1368#endif
1369
Yangbo Lu982f4252019-06-21 11:42:27 +08001370#ifdef CONFIG_OF_LIBFDT
1371__weak int esdhc_status_fixup(void *blob, const char *compat)
1372{
1373#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1374 if (!hwconfig("esdhc")) {
1375 do_fixup_by_compat(blob, compat, "status", "disabled",
1376 sizeof("disabled"), 1);
1377 return 1;
1378 }
1379#endif
1380 return 0;
1381}
1382
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001383void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lu982f4252019-06-21 11:42:27 +08001384{
1385 const char *compat = "fsl,esdhc";
1386
1387 if (esdhc_status_fixup(blob, compat))
1388 return;
1389
Yangbo Lu982f4252019-06-21 11:42:27 +08001390 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1391 gd->arch.sdhc_clk, 1);
Yangbo Lu982f4252019-06-21 11:42:27 +08001392}
1393#endif
1394
1395#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +08001396#include <asm/arch/clock.h>
Yangbo Lu982f4252019-06-21 11:42:27 +08001397__weak void init_clk_usdhc(u32 index)
1398{
1399}
1400
Simon Glassaad29ae2020-12-03 16:55:21 -07001401static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lu982f4252019-06-21 11:42:27 +08001402{
Yangbo Lu982f4252019-06-21 11:42:27 +08001403 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001404#if CONFIG_IS_ENABLED(DM_REGULATOR)
1405 struct udevice *vqmmc_dev;
Walter Lozano8aff6732020-07-29 12:31:17 -03001406 int ret;
Yangbo Lu982f4252019-06-21 11:42:27 +08001407#endif
Walter Lozano8aff6732020-07-29 12:31:17 -03001408 const void *fdt = gd->fdt_blob;
1409 int node = dev_of_offset(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001410 fdt_addr_t addr;
1411 unsigned int val;
Yangbo Lu982f4252019-06-21 11:42:27 +08001412
Simon Glass6d70ba02021-08-07 07:24:06 -06001413 if (!CONFIG_IS_ENABLED(OF_REAL))
1414 return 0;
1415
Yangbo Lu982f4252019-06-21 11:42:27 +08001416 addr = dev_read_addr(dev);
1417 if (addr == FDT_ADDR_T_NONE)
1418 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001419 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lu982f4252019-06-21 11:42:27 +08001420 priv->dev = dev;
1421 priv->mode = -1;
Yangbo Lu982f4252019-06-21 11:42:27 +08001422
1423 val = dev_read_u32_default(dev, "bus-width", -1);
1424 if (val == 8)
1425 priv->bus_width = 8;
1426 else if (val == 4)
1427 priv->bus_width = 4;
1428 else
1429 priv->bus_width = 1;
1430
1431 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1432 priv->tuning_step = val;
1433 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1434 ESDHC_TUNING_START_TAP_DEFAULT);
1435 priv->tuning_start_tap = val;
1436 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1437 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1438 priv->strobe_dll_delay_target = val;
Haibo Chen26154952021-03-22 18:55:38 +08001439 val = fdtdec_get_int(fdt, node, "fsl,signal-voltage-switch-extra-delay-ms", 0);
1440 priv->signal_voltage_switch_extra_delay_ms = val;
Yangbo Lu982f4252019-06-21 11:42:27 +08001441
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001442 if (dev_read_bool(dev, "broken-cd"))
1443 priv->broken_cd = 1;
1444
Yangbo Lu982f4252019-06-21 11:42:27 +08001445 if (dev_read_bool(dev, "non-removable")) {
1446 priv->non_removable = 1;
1447 } else {
1448 priv->non_removable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001449#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001450 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1451 GPIOD_IS_IN);
1452#endif
1453 }
1454
1455 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1456 priv->wp_enable = 1;
1457 } else {
1458 priv->wp_enable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001459#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001460 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1461 GPIOD_IS_IN);
1462#endif
1463 }
1464
1465 priv->vs18_enable = 0;
1466
1467#if CONFIG_IS_ENABLED(DM_REGULATOR)
1468 /*
1469 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1470 * otherwise, emmc will work abnormally.
1471 */
1472 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1473 if (ret) {
1474 dev_dbg(dev, "no vqmmc-supply\n");
1475 } else {
Marek Vasut34e67f92020-05-22 18:19:08 +02001476 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lu982f4252019-06-21 11:42:27 +08001477 ret = regulator_set_enable(vqmmc_dev, true);
1478 if (ret) {
1479 dev_err(dev, "fail to enable vqmmc-supply\n");
1480 return ret;
1481 }
1482
1483 if (regulator_get_value(vqmmc_dev) == 1800000)
1484 priv->vs18_enable = 1;
1485 }
1486#endif
Simon Glass6d70ba02021-08-07 07:24:06 -06001487
Walter Lozano8aff6732020-07-29 12:31:17 -03001488 return 0;
1489}
1490
1491static int fsl_esdhc_probe(struct udevice *dev)
1492{
1493 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001494 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano8aff6732020-07-29 12:31:17 -03001495 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1496 struct esdhc_soc_data *data =
1497 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1498 struct mmc *mmc;
Walter Lozano8aff6732020-07-29 12:31:17 -03001499 int ret;
1500
1501#if CONFIG_IS_ENABLED(OF_PLATDATA)
1502 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1503 unsigned int val;
1504
1505 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1506 val = plat->dtplat.bus_width;
1507 if (val == 8)
1508 priv->bus_width = 8;
1509 else if (val == 4)
1510 priv->bus_width = 4;
1511 else
1512 priv->bus_width = 1;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001513
1514 if (dtplat->non_removable)
1515 priv->non_removable = 1;
1516 else
1517 priv->non_removable = 0;
1518
1519 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1520 struct udevice *gpiodev;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001521
Simon Glass0000e0d2021-03-15 17:25:28 +13001522 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001523 if (ret)
1524 return ret;
1525
1526 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1527 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1528 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1529
1530 if (ret)
1531 return ret;
1532 }
Walter Lozano8aff6732020-07-29 12:31:17 -03001533#endif
1534
1535 if (data)
1536 priv->flags = data->flags;
Yangbo Lu982f4252019-06-21 11:42:27 +08001537
Yangbo Lu982f4252019-06-21 11:42:27 +08001538 /*
1539 * TODO:
1540 * Because lack of clk driver, if SDHC clk is not enabled,
1541 * need to enable it first before this driver is invoked.
1542 *
1543 * we use MXC_ESDHC_CLK to get clk freq.
1544 * If one would like to make this function work,
1545 * the aliases should be provided in dts as this:
1546 *
1547 * aliases {
1548 * mmc0 = &usdhc1;
1549 * mmc1 = &usdhc2;
1550 * mmc2 = &usdhc3;
1551 * mmc3 = &usdhc4;
1552 * };
1553 * Then if your board only supports mmc2 and mmc3, but we can
1554 * correctly get the seq as 2 and 3, then let mxc_get_clock
1555 * work as expected.
1556 */
1557
Simon Glass75e534b2020-12-16 21:20:07 -07001558 init_clk_usdhc(dev_seq(dev));
Yangbo Lu982f4252019-06-21 11:42:27 +08001559
Giulio Benettidbdbc632020-01-10 15:51:45 +01001560#if CONFIG_IS_ENABLED(CLK)
1561 /* Assigned clock already set clock */
1562 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1563 if (ret) {
1564 printf("Failed to get per_clk\n");
1565 return ret;
1566 }
1567 ret = clk_enable(&priv->per_clk);
1568 if (ret) {
1569 printf("Failed to enable per_clk\n");
1570 return ret;
1571 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001572
Giulio Benettidbdbc632020-01-10 15:51:45 +01001573 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1574#else
Simon Glass75e534b2020-12-16 21:20:07 -07001575 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettidbdbc632020-01-10 15:51:45 +01001576 if (priv->sdhc_clk <= 0) {
1577 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1578 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001579 }
Giulio Benettidbdbc632020-01-10 15:51:45 +01001580#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001581
1582 ret = fsl_esdhc_init(priv, plat);
1583 if (ret) {
1584 dev_err(dev, "fsl_esdhc_init failure\n");
1585 return ret;
1586 }
1587
Simon Glass6d70ba02021-08-07 07:24:06 -06001588 if (CONFIG_IS_ENABLED(OF_REAL)) {
1589 ret = mmc_of_parse(dev, &plat->cfg);
1590 if (ret)
1591 return ret;
1592 }
Peng Fan3766a482019-07-10 09:35:24 +00001593
Yangbo Lu982f4252019-06-21 11:42:27 +08001594 mmc = &plat->mmc;
1595 mmc->cfg = &plat->cfg;
1596 mmc->dev = dev;
Yangbo Lu982f4252019-06-21 11:42:27 +08001597
1598 upriv->mmc = mmc;
1599
1600 return esdhc_init_common(priv, mmc);
1601}
1602
Yangbo Lu982f4252019-06-21 11:42:27 +08001603static int fsl_esdhc_get_cd(struct udevice *dev)
1604{
1605 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1606
1607 return esdhc_getcd_common(priv);
1608}
1609
1610static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1611 struct mmc_data *data)
1612{
Simon Glassfa20e932020-12-03 16:55:20 -07001613 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001614 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1615
1616 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1617}
1618
1619static int fsl_esdhc_set_ios(struct udevice *dev)
1620{
Simon Glassfa20e932020-12-03 16:55:20 -07001621 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001622 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1623
1624 return esdhc_set_ios_common(priv, &plat->mmc);
1625}
1626
Peng Fan69b9d3a2019-07-10 09:35:26 +00001627#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1628static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1629{
1630 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1631 struct fsl_esdhc *regs = priv->esdhc_regs;
1632 u32 m;
1633
Haibo Chen920f5d02020-09-30 15:52:23 +08001634 m = esdhc_read32(&regs->mixctrl);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001635 m |= MIX_CTRL_HS400_ES;
Haibo Chen920f5d02020-09-30 15:52:23 +08001636 esdhc_write32(&regs->mixctrl, m);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001637
1638 return 0;
1639}
1640#endif
1641
Haibo Chencb78f212020-11-05 14:57:13 +08001642static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1643 int timeout_us)
1644{
1645 int ret;
1646 u32 tmp;
1647 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1648 struct fsl_esdhc *regs = priv->esdhc_regs;
1649
1650 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1651 !!(tmp & PRSSTAT_DAT0) == !!state,
1652 timeout_us);
1653 return ret;
1654}
1655
Yangbo Lu982f4252019-06-21 11:42:27 +08001656static const struct dm_mmc_ops fsl_esdhc_ops = {
1657 .get_cd = fsl_esdhc_get_cd,
1658 .send_cmd = fsl_esdhc_send_cmd,
1659 .set_ios = fsl_esdhc_set_ios,
1660#ifdef MMC_SUPPORTS_TUNING
1661 .execute_tuning = fsl_esdhc_execute_tuning,
1662#endif
Peng Fan69b9d3a2019-07-10 09:35:26 +00001663#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1664 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1665#endif
Haibo Chencb78f212020-11-05 14:57:13 +08001666 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lu982f4252019-06-21 11:42:27 +08001667};
Yangbo Lu982f4252019-06-21 11:42:27 +08001668
1669static struct esdhc_soc_data usdhc_imx7d_data = {
1670 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1671 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1672 | ESDHC_FLAG_HS400,
Yangbo Lu982f4252019-06-21 11:42:27 +08001673};
1674
Jorge Ramirez-Ortiz8239e822021-09-08 21:56:42 +03001675static struct esdhc_soc_data usdhc_imx7ulp_data = {
1676 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +03001677 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1678 | ESDHC_FLAG_HS400,
Jorge Ramirez-Ortiz8239e822021-09-08 21:56:42 +03001679};
1680
Peng Fan457fe962019-07-10 09:35:28 +00001681static struct esdhc_soc_data usdhc_imx8qm_data = {
1682 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1683 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1684 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1685};
1686
Yangbo Lu982f4252019-06-21 11:42:27 +08001687static const struct udevice_id fsl_esdhc_ids[] = {
Fabio Estevameaf8ccc2021-02-15 08:58:15 -03001688 { .compatible = "fsl,imx51-esdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001689 { .compatible = "fsl,imx53-esdhc", },
1690 { .compatible = "fsl,imx6ul-usdhc", },
1691 { .compatible = "fsl,imx6sx-usdhc", },
1692 { .compatible = "fsl,imx6sl-usdhc", },
1693 { .compatible = "fsl,imx6q-usdhc", },
1694 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Jorge Ramirez-Ortiz8239e822021-09-08 21:56:42 +03001695 { .compatible = "fsl,imx7ulp-usdhc", .data = (ulong)&usdhc_imx7ulp_data,},
Peng Fan457fe962019-07-10 09:35:28 +00001696 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fand7689fa2019-11-04 17:31:17 +08001697 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1698 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1699 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti65b5ec12020-01-10 15:51:46 +01001700 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001701 { .compatible = "fsl,esdhc", },
1702 { /* sentinel */ }
1703};
1704
Yangbo Lu982f4252019-06-21 11:42:27 +08001705static int fsl_esdhc_bind(struct udevice *dev)
1706{
Simon Glassfa20e932020-12-03 16:55:20 -07001707 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001708
1709 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1710}
Yangbo Lu982f4252019-06-21 11:42:27 +08001711
1712U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano904b0be2020-07-29 12:31:16 -03001713 .name = "fsl_esdhc",
Yangbo Lu982f4252019-06-21 11:42:27 +08001714 .id = UCLASS_MMC,
1715 .of_match = fsl_esdhc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001716 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lu982f4252019-06-21 11:42:27 +08001717 .ops = &fsl_esdhc_ops,
Yangbo Lu982f4252019-06-21 11:42:27 +08001718 .bind = fsl_esdhc_bind,
Yangbo Lu982f4252019-06-21 11:42:27 +08001719 .probe = fsl_esdhc_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -07001720 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001721 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lu982f4252019-06-21 11:42:27 +08001722};
Walter Lozano8aff6732020-07-29 12:31:17 -03001723
Simon Glassdf65db82020-12-28 20:34:57 -07001724DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lu982f4252019-06-21 11:42:27 +08001725#endif