blob: 697e3c641d0e6e7624cfb22d0dc891a3afc49603 [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>
Sean Anderson120fc012021-11-23 15:03:43 -050041#include <linux/dma-mapping.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080042
Haibo Chene8801ac2021-02-19 11:25:32 -080043#ifndef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
44#ifdef CONFIG_FSL_USDHC
45#define ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 1
46#endif
47#endif
48
Yangbo Lu982f4252019-06-21 11:42:27 +080049DECLARE_GLOBAL_DATA_PTR;
50
51#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
52 IRQSTATEN_CINT | \
53 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
54 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
55 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
56 IRQSTATEN_DINT)
57#define MAX_TUNING_LOOP 40
Yangbo Lu982f4252019-06-21 11:42:27 +080058
59struct fsl_esdhc {
60 uint dsaddr; /* SDMA system address register */
61 uint blkattr; /* Block attributes register */
62 uint cmdarg; /* Command argument register */
63 uint xfertyp; /* Transfer type register */
64 uint cmdrsp0; /* Command response 0 register */
65 uint cmdrsp1; /* Command response 1 register */
66 uint cmdrsp2; /* Command response 2 register */
67 uint cmdrsp3; /* Command response 3 register */
68 uint datport; /* Buffer data port register */
69 uint prsstat; /* Present state register */
70 uint proctl; /* Protocol control register */
71 uint sysctl; /* System Control Register */
72 uint irqstat; /* Interrupt status register */
73 uint irqstaten; /* Interrupt status enable register */
74 uint irqsigen; /* Interrupt signal enable register */
75 uint autoc12err; /* Auto CMD error status register */
76 uint hostcapblt; /* Host controller capabilities register */
77 uint wml; /* Watermark level register */
78 uint mixctrl; /* For USDHC */
79 char reserved1[4]; /* reserved */
80 uint fevt; /* Force event register */
81 uint admaes; /* ADMA error status register */
82 uint adsaddr; /* ADMA system address register */
83 char reserved2[4];
84 uint dllctrl;
85 uint dllstat;
86 uint clktunectrlstatus;
87 char reserved3[4];
88 uint strobe_dllctrl;
89 uint strobe_dllstat;
90 char reserved4[72];
91 uint vendorspec;
92 uint mmcboot;
93 uint vendorspec2;
Giulio Benetti65b5ec12020-01-10 15:51:46 +010094 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lu982f4252019-06-21 11:42:27 +080095 char reserved5[44];
96 uint hostver; /* Host controller version register */
97 char reserved6[4]; /* reserved */
98 uint dmaerraddr; /* DMA error address register */
99 char reserved7[4]; /* reserved */
100 uint dmaerrattr; /* DMA error attribute register */
101 char reserved8[4]; /* reserved */
102 uint hostcapblt2; /* Host controller capabilities register 2 */
103 char reserved9[8]; /* reserved */
104 uint tcr; /* Tuning control register */
105 char reserved10[28]; /* reserved */
106 uint sddirctl; /* SD direction control register */
107 char reserved11[712];/* reserved */
108 uint scr; /* eSDHC control register */
109};
110
111struct fsl_esdhc_plat {
Walter Lozano8aff6732020-07-29 12:31:17 -0300112#if CONFIG_IS_ENABLED(OF_PLATDATA)
113 /* Put this first since driver model will copy the data here */
114 struct dtd_fsl_esdhc dtplat;
115#endif
116
Yangbo Lu982f4252019-06-21 11:42:27 +0800117 struct mmc_config cfg;
118 struct mmc mmc;
119};
120
121struct esdhc_soc_data {
122 u32 flags;
Yangbo Lu982f4252019-06-21 11:42:27 +0800123};
124
125/**
126 * struct fsl_esdhc_priv
127 *
128 * @esdhc_regs: registers of the sdhc controller
129 * @sdhc_clk: Current clk of the sdhc controller
Yangbo Lu982f4252019-06-21 11:42:27 +0800130 * @cfg: mmc config
131 * @mmc: mmc
132 * Following is used when Driver Model is enabled for MMC
133 * @dev: pointer for the device
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300134 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lu982f4252019-06-21 11:42:27 +0800135 * @wp_enable: 1: enable checking wp; 0: no check
136 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
137 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
138 * @caps: controller capabilities
139 * @tuning_step: tuning step setting in tuning_ctrl register
140 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
141 * @strobe_dll_delay_target: settings in strobe_dllctrl
142 * @signal_voltage: indicating the current voltage
Haibo Chen26154952021-03-22 18:55:38 +0800143 * @signal_voltage_switch_extra_delay_ms: extra delay for IO voltage switch
Yangbo Lu982f4252019-06-21 11:42:27 +0800144 * @cd_gpio: gpio for card detection
145 * @wp_gpio: gpio for write protection
146 */
147struct fsl_esdhc_priv {
148 struct fsl_esdhc *esdhc_regs;
149 unsigned int sdhc_clk;
150 struct clk per_clk;
151 unsigned int clock;
152 unsigned int mode;
Sean Anderson80b81612022-01-12 08:18:52 +0900153#if !CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +0800154 struct mmc *mmc;
155#endif
156 struct udevice *dev;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300157 int broken_cd;
Yangbo Lu982f4252019-06-21 11:42:27 +0800158 int wp_enable;
159 int vs18_enable;
160 u32 flags;
161 u32 caps;
162 u32 tuning_step;
163 u32 tuning_start_tap;
164 u32 strobe_dll_delay_target;
165 u32 signal_voltage;
Haibo Chen26154952021-03-22 18:55:38 +0800166 u32 signal_voltage_switch_extra_delay_ms;
Yangbo Lu982f4252019-06-21 11:42:27 +0800167 struct udevice *vqmmc_dev;
168 struct udevice *vmmc_dev;
Simon Glassfa4689a2019-12-06 21:41:35 -0700169#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +0800170 struct gpio_desc cd_gpio;
171 struct gpio_desc wp_gpio;
172#endif
Sean Anderson120fc012021-11-23 15:03:43 -0500173 dma_addr_t dma_addr;
Yangbo Lu982f4252019-06-21 11:42:27 +0800174};
175
176/* Return the XFERTYP flags for a given command and data packet */
177static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
178{
179 uint xfertyp = 0;
180
181 if (data) {
182 xfertyp |= XFERTYP_DPSEL;
Sean Anderson218dfa12021-11-23 15:03:45 -0500183 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
184 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
185 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
186 xfertyp |= XFERTYP_DMAEN;
Yangbo Lu982f4252019-06-21 11:42:27 +0800187 if (data->blocks > 1) {
188 xfertyp |= XFERTYP_MSBSEL;
189 xfertyp |= XFERTYP_BCEN;
Sean Anderson218dfa12021-11-23 15:03:45 -0500190 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
191 xfertyp |= XFERTYP_AC12EN;
Yangbo Lu982f4252019-06-21 11:42:27 +0800192 }
193
194 if (data->flags & MMC_DATA_READ)
195 xfertyp |= XFERTYP_DTDSEL;
196 }
197
198 if (cmd->resp_type & MMC_RSP_CRC)
199 xfertyp |= XFERTYP_CCCEN;
200 if (cmd->resp_type & MMC_RSP_OPCODE)
201 xfertyp |= XFERTYP_CICEN;
202 if (cmd->resp_type & MMC_RSP_136)
203 xfertyp |= XFERTYP_RSPTYP_136;
204 else if (cmd->resp_type & MMC_RSP_BUSY)
205 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
206 else if (cmd->resp_type & MMC_RSP_PRESENT)
207 xfertyp |= XFERTYP_RSPTYP_48;
208
209 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
210 xfertyp |= XFERTYP_CMDTYP_ABORT;
211
212 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
213}
214
Yangbo Lu982f4252019-06-21 11:42:27 +0800215/*
216 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
217 */
218static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
219 struct mmc_data *data)
220{
221 struct fsl_esdhc *regs = priv->esdhc_regs;
222 uint blocks;
223 char *buffer;
224 uint databuf;
225 uint size;
226 uint irqstat;
227 ulong start;
228
229 if (data->flags & MMC_DATA_READ) {
230 blocks = data->blocks;
231 buffer = data->dest;
232 while (blocks) {
233 start = get_timer(0);
234 size = data->blocksize;
235 irqstat = esdhc_read32(&regs->irqstat);
236 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
237 if (get_timer(start) > PIO_TIMEOUT) {
238 printf("\nData Read Failed in PIO Mode.");
239 return;
240 }
241 }
242 while (size && (!(irqstat & IRQSTAT_TC))) {
243 udelay(100); /* Wait before last byte transfer complete */
244 irqstat = esdhc_read32(&regs->irqstat);
245 databuf = in_le32(&regs->datport);
246 *((uint *)buffer) = databuf;
247 buffer += 4;
248 size -= 4;
249 }
250 blocks--;
251 }
252 } else {
253 blocks = data->blocks;
254 buffer = (char *)data->src;
255 while (blocks) {
256 start = get_timer(0);
257 size = data->blocksize;
258 irqstat = esdhc_read32(&regs->irqstat);
259 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
260 if (get_timer(start) > PIO_TIMEOUT) {
261 printf("\nData Write Failed in PIO Mode.");
262 return;
263 }
264 }
265 while (size && (!(irqstat & IRQSTAT_TC))) {
266 udelay(100); /* Wait before last byte transfer complete */
267 databuf = *((uint *)buffer);
268 buffer += 4;
269 size -= 4;
270 irqstat = esdhc_read32(&regs->irqstat);
271 out_le32(&regs->datport, databuf);
272 }
273 blocks--;
274 }
275 }
276}
Yangbo Lu982f4252019-06-21 11:42:27 +0800277
Sean Andersonaa6d91d2021-11-23 15:03:44 -0500278static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
279 struct mmc_data *data)
Yangbo Lu982f4252019-06-21 11:42:27 +0800280{
Yangbo Lu982f4252019-06-21 11:42:27 +0800281 struct fsl_esdhc *regs = priv->esdhc_regs;
Sean Andersonaa6d91d2021-11-23 15:03:44 -0500282 uint wml_value = data->blocksize / 4;
Yangbo Lu982f4252019-06-21 11:42:27 +0800283
284 if (data->flags & MMC_DATA_READ) {
285 if (wml_value > WML_RD_WML_MAX)
286 wml_value = WML_RD_WML_MAX_VAL;
287
288 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Yangbo Lu982f4252019-06-21 11:42:27 +0800289 } else {
Yangbo Lu982f4252019-06-21 11:42:27 +0800290 if (wml_value > WML_WR_WML_MAX)
291 wml_value = WML_WR_WML_MAX_VAL;
Sean Andersonaa6d91d2021-11-23 15:03:44 -0500292
293 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
294 wml_value << 16);
295 }
296}
Sean Andersonaa6d91d2021-11-23 15:03:44 -0500297
298static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
299{
300 uint trans_bytes = data->blocksize * data->blocks;
301 struct fsl_esdhc *regs = priv->esdhc_regs;
302 void *buf;
303
304 if (data->flags & MMC_DATA_WRITE)
305 buf = (void *)data->src;
306 else
307 buf = data->dest;
308
309 priv->dma_addr = dma_map_single(buf, trans_bytes,
310 mmc_get_dma_dir(data));
311 if (upper_32_bits(priv->dma_addr))
312 printf("Cannot use 64 bit addresses with SDMA\n");
313 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
314 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
315}
316
317static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
318 struct mmc_data *data)
319{
320 int timeout;
321 bool is_write = data->flags & MMC_DATA_WRITE;
322 struct fsl_esdhc *regs = priv->esdhc_regs;
323
324 if (is_write) {
325 if (priv->wp_enable && !(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
326 printf("Cannot write to locked SD card.\n");
327 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +0800328 } 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)) {
Sean Andersonaa6d91d2021-11-23 15:03:44 -0500332 printf("Cannot write to locked SD card.\n");
333 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +0800334 }
335#endif
336 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800337 }
338
Marcel Ziswiler86376fd2022-01-31 23:08:31 +0100339 esdhc_setup_watermark_level(priv, data);
340 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
Sean Anderson218dfa12021-11-23 15:03:45 -0500341 esdhc_setup_dma(priv, data);
Yangbo Lu982f4252019-06-21 11:42:27 +0800342
343 /* Calculate the timeout period for data transactions */
344 /*
345 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
346 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
347 * So, Number of SD Clock cycles for 0.25sec should be minimum
348 * (SD Clock/sec * 0.25 sec) SD Clock cycles
349 * = (mmc->clock * 1/4) SD Clock cycles
350 * As 1) >= 2)
351 * => (2^(timeout+13)) >= mmc->clock * 1/4
352 * Taking log2 both the sides
353 * => timeout + 13 >= log2(mmc->clock/4)
354 * Rounding up to next power of 2
355 * => timeout + 13 = log2(mmc->clock/4) + 1
356 * => timeout + 13 = fls(mmc->clock/4)
357 *
358 * However, the MMC spec "It is strongly recommended for hosts to
359 * implement more than 500ms timeout value even if the card
360 * indicates the 250ms maximum busy length." Even the previous
361 * value of 300ms is known to be insufficient for some cards.
362 * So, we use
363 * => timeout + 13 = fls(mmc->clock/2)
364 */
365 timeout = fls(mmc->clock/2);
366 timeout -= 13;
367
368 if (timeout > 14)
369 timeout = 14;
370
371 if (timeout < 0)
372 timeout = 0;
373
Sean Anderson218dfa12021-11-23 15:03:45 -0500374 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
375 (timeout == 4 || timeout == 8 || timeout == 12))
Yangbo Lu982f4252019-06-21 11:42:27 +0800376 timeout++;
Yangbo Lu982f4252019-06-21 11:42:27 +0800377
Sean Anderson218dfa12021-11-23 15:03:45 -0500378 if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
379 timeout = 0xE;
380
Yangbo Lu982f4252019-06-21 11:42:27 +0800381 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
382
383 return 0;
384}
385
Sean Anderson3e330792021-11-23 15:03:46 -0500386#if IS_ENABLED(CONFIG_MCF5441x)
Yangbo Lu982f4252019-06-21 11:42:27 +0800387/*
388 * Swaps 32-bit words to little-endian byte order.
389 */
390static inline void sd_swap_dma_buff(struct mmc_data *data)
391{
392 int i, size = data->blocksize >> 2;
393 u32 *buffer = (u32 *)data->dest;
394 u32 sw;
395
396 while (data->blocks--) {
397 for (i = 0; i < size; i++) {
398 sw = __sw32(*buffer);
399 *buffer++ = sw;
400 }
401 }
402}
Sean Anderson218dfa12021-11-23 15:03:45 -0500403#else
404static inline void sd_swap_dma_buff(struct mmc_data *data)
405{
406 return;
407}
Yangbo Lu982f4252019-06-21 11:42:27 +0800408#endif
409
410/*
411 * Sends a command out on the bus. Takes the mmc pointer,
412 * a command pointer, and an optional data pointer.
413 */
414static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
415 struct mmc_cmd *cmd, struct mmc_data *data)
416{
417 int err = 0;
418 uint xfertyp;
419 uint irqstat;
420 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
421 struct fsl_esdhc *regs = priv->esdhc_regs;
422 unsigned long start;
423
Sean Anderson218dfa12021-11-23 15:03:45 -0500424 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
425 cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
Yangbo Lu982f4252019-06-21 11:42:27 +0800426 return 0;
Yangbo Lu982f4252019-06-21 11:42:27 +0800427
428 esdhc_write32(&regs->irqstat, -1);
429
430 sync();
431
432 /* Wait for the bus to be idle */
433 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
434 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
435 ;
436
437 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
438 ;
439
Yangbo Lu982f4252019-06-21 11:42:27 +0800440 /* Set up for a data transfer if we have one */
441 if (data) {
442 err = esdhc_setup_data(priv, mmc, data);
443 if(err)
444 return err;
Yangbo Lu982f4252019-06-21 11:42:27 +0800445 }
446
447 /* Figure out the transfer arguments */
448 xfertyp = esdhc_xfertyp(cmd, data);
449
450 /* Mask all irqs */
451 esdhc_write32(&regs->irqsigen, 0);
452
453 /* Send the command */
454 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Simon Glass87f79c52022-01-22 05:07:24 -0700455 if (IS_ENABLED(CONFIG_FSL_USDHC)) {
Sean Anderson3e330792021-11-23 15:03:46 -0500456 u32 mixctrl = esdhc_read32(&regs->mixctrl);
457
458 esdhc_write32(&regs->mixctrl,
459 (mixctrl & 0xFFFFFF80) | (xfertyp & 0x7F)
460 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
461 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
462 } else {
463 esdhc_write32(&regs->xfertyp, xfertyp);
464 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800465
466 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
467 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
468 flags = IRQSTAT_BRR;
469
470 /* Wait for the command to complete */
471 start = get_timer(0);
472 while (!(esdhc_read32(&regs->irqstat) & flags)) {
473 if (get_timer(start) > 1000) {
474 err = -ETIMEDOUT;
475 goto out;
476 }
477 }
478
479 irqstat = esdhc_read32(&regs->irqstat);
480
481 if (irqstat & CMD_ERR) {
482 err = -ECOMM;
483 goto out;
484 }
485
486 if (irqstat & IRQSTAT_CTOE) {
487 err = -ETIMEDOUT;
488 goto out;
489 }
490
Yangbo Lu982f4252019-06-21 11:42:27 +0800491 /* Workaround for ESDHC errata ENGcm03648 */
492 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan3dbea592019-07-10 09:35:30 +0000493 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800494
Peng Fan3dbea592019-07-10 09:35:30 +0000495 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-06-21 11:42:27 +0800496 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
497 PRSSTAT_DAT0)) {
498 udelay(100);
499 timeout--;
500 }
501
502 if (timeout <= 0) {
503 printf("Timeout waiting for DAT0 to go high!\n");
504 err = -ETIMEDOUT;
505 goto out;
506 }
507 }
508
509 /* Copy the response to the response buffer */
510 if (cmd->resp_type & MMC_RSP_136) {
511 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
512
513 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
514 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
515 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
516 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
517 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
518 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
519 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
520 cmd->response[3] = (cmdrsp0 << 8);
521 } else
522 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
523
524 /* Wait until all of the blocks are transferred */
525 if (data) {
Sean Anderson218dfa12021-11-23 15:03:45 -0500526 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
527 esdhc_pio_read_write(priv, data);
528 } else {
529 flags = DATA_COMPLETE;
530 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
531 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
532 flags = IRQSTAT_BRR;
Yangbo Lu982f4252019-06-21 11:42:27 +0800533
Sean Anderson218dfa12021-11-23 15:03:45 -0500534 do {
535 irqstat = esdhc_read32(&regs->irqstat);
Yangbo Lu982f4252019-06-21 11:42:27 +0800536
Sean Anderson218dfa12021-11-23 15:03:45 -0500537 if (irqstat & IRQSTAT_DTOE) {
538 err = -ETIMEDOUT;
539 goto out;
540 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800541
Sean Anderson218dfa12021-11-23 15:03:45 -0500542 if (irqstat & DATA_ERR) {
543 err = -ECOMM;
544 goto out;
545 }
546 } while ((irqstat & flags) != flags);
Yangbo Lu982f4252019-06-21 11:42:27 +0800547
Sean Anderson218dfa12021-11-23 15:03:45 -0500548 /*
549 * Need invalidate the dcache here again to avoid any
550 * cache-fill during the DMA operations such as the
551 * speculative pre-fetching etc.
552 */
553 dma_unmap_single(priv->dma_addr,
554 data->blocks * data->blocksize,
555 mmc_get_dma_dir(data));
556 if (IS_ENABLED(CONFIG_MCF5441x) &&
557 (data->flags & MMC_DATA_READ))
558 sd_swap_dma_buff(data);
559 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800560 }
561
562out:
563 /* Reset CMD and DATA portions on error */
564 if (err) {
565 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
566 SYSCTL_RSTC);
567 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
568 ;
569
570 if (data) {
571 esdhc_write32(&regs->sysctl,
572 esdhc_read32(&regs->sysctl) |
573 SYSCTL_RSTD);
574 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
575 ;
576 }
577
578 /* If this was CMD11, then notify that power cycle is needed */
579 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
580 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
581 }
582
583 esdhc_write32(&regs->irqstat, -1);
584
585 return err;
586}
587
588static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
589{
590 struct fsl_esdhc *regs = priv->esdhc_regs;
591 int div = 1;
Haibo Chene6a999b2020-09-01 15:34:06 +0800592 u32 tmp;
Sean Anderson218dfa12021-11-23 15:03:45 -0500593 int ret, pre_div;
Yangbo Lu982f4252019-06-21 11:42:27 +0800594 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
595 int sdhc_clk = priv->sdhc_clk;
596 uint clk;
597
Sean Anderson3e330792021-11-23 15:03:46 -0500598#if IS_ENABLED(CONFIG_MX53)
Haibo Chen84516a72022-02-11 19:16:56 +0800599 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
600 pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
Sean Anderson218dfa12021-11-23 15:03:45 -0500601#else
Haibo Chen84516a72022-02-11 19:16:56 +0800602 pre_div = 1;
Sean Anderson218dfa12021-11-23 15:03:45 -0500603#endif
Sean Anderson218dfa12021-11-23 15:03:45 -0500604
Yangbo Lu982f4252019-06-21 11:42:27 +0800605 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
606 pre_div *= 2;
607
608 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
609 div++;
610
Haibo Chen8be0ba32022-02-11 19:16:57 +0800611 mmc->clock = sdhc_clk / pre_div / div / ddr_pre_div;
612
Yangbo Lu982f4252019-06-21 11:42:27 +0800613 pre_div >>= 1;
614 div -= 1;
615
616 clk = (pre_div << 8) | (div << 4);
617
Sean Anderson218dfa12021-11-23 15:03:45 -0500618 if (IS_ENABLED(CONFIG_FSL_USDHC))
619 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
620 else
621 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +0800622
623 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
624
Haibo Chene6a999b2020-09-01 15:34:06 +0800625 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
626 if (ret)
627 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lu982f4252019-06-21 11:42:27 +0800628
Sean Anderson218dfa12021-11-23 15:03:45 -0500629 if (IS_ENABLED(CONFIG_FSL_USDHC))
630 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
631 else
632 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +0800633
634 priv->clock = clock;
635}
636
Yangbo Lu982f4252019-06-21 11:42:27 +0800637#ifdef MMC_SUPPORTS_TUNING
638static int esdhc_change_pinstate(struct udevice *dev)
639{
640 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
641 int ret;
642
643 switch (priv->mode) {
644 case UHS_SDR50:
645 case UHS_DDR50:
646 ret = pinctrl_select_state(dev, "state_100mhz");
647 break;
648 case UHS_SDR104:
649 case MMC_HS_200:
650 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000651 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800652 ret = pinctrl_select_state(dev, "state_200mhz");
653 break;
654 default:
655 ret = pinctrl_select_state(dev, "default");
656 break;
657 }
658
659 if (ret)
660 printf("%s %d error\n", __func__, priv->mode);
661
662 return ret;
663}
664
665static void esdhc_reset_tuning(struct mmc *mmc)
666{
667 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
668 struct fsl_esdhc *regs = priv->esdhc_regs;
669
670 if (priv->flags & ESDHC_FLAG_USDHC) {
671 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
672 esdhc_clrbits32(&regs->autoc12err,
673 MIX_CTRL_SMPCLK_SEL |
674 MIX_CTRL_EXE_TUNE);
675 }
676 }
677}
678
679static void esdhc_set_strobe_dll(struct mmc *mmc)
680{
681 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
682 struct fsl_esdhc *regs = priv->esdhc_regs;
683 u32 val;
684
685 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chen920f5d02020-09-30 15:52:23 +0800686 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +0300687 /* clear the reset bit on strobe dll before any setting */
688 esdhc_write32(&regs->strobe_dllctrl, 0);
Yangbo Lu982f4252019-06-21 11:42:27 +0800689
690 /*
691 * enable strobe dll ctrl and adjust the delay target
692 * for the uSDHC loopback read clock
693 */
694 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +0300695 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
Yangbo Lu982f4252019-06-21 11:42:27 +0800696 (priv->strobe_dll_delay_target <<
697 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chen920f5d02020-09-30 15:52:23 +0800698 esdhc_write32(&regs->strobe_dllctrl, val);
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +0300699 /* wait 5us to make sure strobe dll status register stable */
700 mdelay(5);
Haibo Chen920f5d02020-09-30 15:52:23 +0800701 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lu982f4252019-06-21 11:42:27 +0800702 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
703 pr_warn("HS400 strobe DLL status REF not lock!\n");
704 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
705 pr_warn("HS400 strobe DLL status SLV not lock!\n");
706 }
707}
708
709static int esdhc_set_timing(struct mmc *mmc)
710{
711 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
712 struct fsl_esdhc *regs = priv->esdhc_regs;
713 u32 mixctrl;
714
Haibo Chen920f5d02020-09-30 15:52:23 +0800715 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800716 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
717
718 switch (mmc->selected_mode) {
719 case MMC_LEGACY:
Yangbo Lu982f4252019-06-21 11:42:27 +0800720 esdhc_reset_tuning(mmc);
Haibo Chen920f5d02020-09-30 15:52:23 +0800721 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800722 break;
723 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000724 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800725 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800726 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800727 break;
728 case MMC_HS:
729 case MMC_HS_52:
730 case MMC_HS_200:
731 case SD_HS:
732 case UHS_SDR12:
733 case UHS_SDR25:
734 case UHS_SDR50:
735 case UHS_SDR104:
Haibo Chen920f5d02020-09-30 15:52:23 +0800736 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800737 break;
738 case UHS_DDR50:
739 case MMC_DDR_52:
740 mixctrl |= MIX_CTRL_DDREN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800741 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800742 break;
743 default:
744 printf("Not supported %d\n", mmc->selected_mode);
745 return -EINVAL;
746 }
747
748 priv->mode = mmc->selected_mode;
749
750 return esdhc_change_pinstate(mmc->dev);
751}
752
753static int esdhc_set_voltage(struct mmc *mmc)
754{
755 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
756 struct fsl_esdhc *regs = priv->esdhc_regs;
757 int ret;
758
759 priv->signal_voltage = mmc->signal_voltage;
760 switch (mmc->signal_voltage) {
761 case MMC_SIGNAL_VOLTAGE_330:
762 if (priv->vs18_enable)
Marek Vasutba79fed2020-05-22 18:28:33 +0200763 return -ENOTSUPP;
Sean Anderson3e330792021-11-23 15:03:46 -0500764 if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
765 !IS_ERR_OR_NULL(priv->vqmmc_dev)) {
766 ret = regulator_set_value(priv->vqmmc_dev,
767 3300000);
Yangbo Lu982f4252019-06-21 11:42:27 +0800768 if (ret) {
769 printf("Setting to 3.3V error");
770 return -EIO;
771 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800772 mdelay(5);
773 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800774
775 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
776 if (!(esdhc_read32(&regs->vendorspec) &
777 ESDHC_VENDORSPEC_VSELECT))
778 return 0;
779
780 return -EAGAIN;
781 case MMC_SIGNAL_VOLTAGE_180:
Sean Anderson3e330792021-11-23 15:03:46 -0500782 if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
783 !IS_ERR_OR_NULL(priv->vqmmc_dev)) {
784 ret = regulator_set_value(priv->vqmmc_dev,
785 1800000);
Yangbo Lu982f4252019-06-21 11:42:27 +0800786 if (ret) {
787 printf("Setting to 1.8V error");
788 return -EIO;
789 }
790 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800791 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
Haibo Chen26154952021-03-22 18:55:38 +0800792 /*
793 * some board like imx8mm-evk need about 18ms to switch
794 * the IO voltage from 3.3v to 1.8v, common code only
795 * delay 10ms, so need to delay extra time to make sure
796 * the IO voltage change to 1.8v.
797 */
798 if (priv->signal_voltage_switch_extra_delay_ms)
799 mdelay(priv->signal_voltage_switch_extra_delay_ms);
Yangbo Lu982f4252019-06-21 11:42:27 +0800800 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
801 return 0;
802
803 return -EAGAIN;
804 case MMC_SIGNAL_VOLTAGE_120:
805 return -ENOTSUPP;
806 default:
807 return 0;
808 }
809}
810
811static void esdhc_stop_tuning(struct mmc *mmc)
812{
813 struct mmc_cmd cmd;
814
815 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
816 cmd.cmdarg = 0;
817 cmd.resp_type = MMC_RSP_R1b;
818
Jaehoon Chung099814b2021-05-31 08:31:49 +0900819 mmc_send_cmd(mmc, &cmd, NULL);
Yangbo Lu982f4252019-06-21 11:42:27 +0800820}
821
822static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
823{
Simon Glassfa20e932020-12-03 16:55:20 -0700824 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +0800825 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
826 struct fsl_esdhc *regs = priv->esdhc_regs;
827 struct mmc *mmc = &plat->mmc;
Haibo Chen920f5d02020-09-30 15:52:23 +0800828 u32 irqstaten = esdhc_read32(&regs->irqstaten);
829 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lu982f4252019-06-21 11:42:27 +0800830 int i, ret = -ETIMEDOUT;
831 u32 val, mixctrl;
832
833 /* clock tuning is not needed for upto 52MHz */
834 if (mmc->clock <= 52000000)
835 return 0;
836
837 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
838 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +0800839 val = esdhc_read32(&regs->autoc12err);
840 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800841 val &= ~MIX_CTRL_SMPCLK_SEL;
842 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
843
844 val |= MIX_CTRL_EXE_TUNE;
845 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
846
Haibo Chen920f5d02020-09-30 15:52:23 +0800847 esdhc_write32(&regs->autoc12err, val);
848 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800849 }
850
851 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chen920f5d02020-09-30 15:52:23 +0800852 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800853 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800854 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800855
Haibo Chen920f5d02020-09-30 15:52:23 +0800856 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
857 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lu982f4252019-06-21 11:42:27 +0800858
859 /*
860 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
861 * of loops reaches 40 times.
862 */
863 for (i = 0; i < MAX_TUNING_LOOP; i++) {
864 u32 ctrl;
865
866 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
867 if (mmc->bus_width == 8)
Haibo Chen920f5d02020-09-30 15:52:23 +0800868 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lu982f4252019-06-21 11:42:27 +0800869 else if (mmc->bus_width == 4)
Haibo Chen920f5d02020-09-30 15:52:23 +0800870 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800871 } else {
Haibo Chen920f5d02020-09-30 15:52:23 +0800872 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800873 }
874
875 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chen920f5d02020-09-30 15:52:23 +0800876 val = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800877 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800878 esdhc_write32(&regs->mixctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +0800879
880 /* We are using STD tuning, no need to check return value */
881 mmc_send_tuning(mmc, opcode, NULL);
882
Haibo Chen920f5d02020-09-30 15:52:23 +0800883 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lu982f4252019-06-21 11:42:27 +0800884 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
885 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800886 ret = 0;
887 break;
888 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800889 }
890
Haibo Chen920f5d02020-09-30 15:52:23 +0800891 esdhc_write32(&regs->irqstaten, irqstaten);
892 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lu982f4252019-06-21 11:42:27 +0800893
894 esdhc_stop_tuning(mmc);
895
896 return ret;
897}
898#endif
899
900static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
901{
902 struct fsl_esdhc *regs = priv->esdhc_regs;
903 int ret __maybe_unused;
Peng Fan2cdf52b2019-11-04 17:14:15 +0800904 u32 clock;
Yangbo Lu982f4252019-06-21 11:42:27 +0800905
Haibo Cheneaa2f102020-11-03 17:18:35 +0800906#ifdef MMC_SUPPORTS_TUNING
907 /*
908 * call esdhc_set_timing() before update the clock rate,
909 * This is because current we support DDR and SDR mode,
910 * Once the DDR_EN bit is set, the card clock will be
911 * divide by 2 automatically. So need to do this before
912 * setting clock rate.
913 */
914 if (priv->mode != mmc->selected_mode) {
915 ret = esdhc_set_timing(mmc);
916 if (ret) {
917 printf("esdhc_set_timing error %d\n", ret);
918 return ret;
919 }
920 }
921#endif
922
Yangbo Lu982f4252019-06-21 11:42:27 +0800923 /* Set the clock speed */
Peng Fan2cdf52b2019-11-04 17:14:15 +0800924 clock = mmc->clock;
925 if (clock < mmc->cfg->f_min)
926 clock = mmc->cfg->f_min;
927
928 if (priv->clock != clock)
929 set_sysctl(priv, mmc, clock);
Yangbo Lu982f4252019-06-21 11:42:27 +0800930
Yangbo Lu982f4252019-06-21 11:42:27 +0800931 if (mmc->clk_disable) {
Sean Anderson3e330792021-11-23 15:03:46 -0500932 if (IS_ENABLED(CONFIG_FSL_USDHC))
933 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
934 else
935 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +0800936 } else {
Sean Anderson3e330792021-11-23 15:03:46 -0500937 if (IS_ENABLED(CONFIG_FSL_USDHC))
938 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
939 VENDORSPEC_CKEN);
940 else
941 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Yangbo Lu982f4252019-06-21 11:42:27 +0800942 }
943
Ye Li423483d2021-08-17 17:09:20 +0800944#ifdef MMC_SUPPORTS_TUNING
Haibo Cheneaa2f102020-11-03 17:18:35 +0800945 /*
946 * For HS400/HS400ES mode, make sure set the strobe dll in the
947 * target clock rate. So call esdhc_set_strobe_dll() after the
948 * clock updated.
949 */
950 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
951 esdhc_set_strobe_dll(mmc);
Yangbo Lu982f4252019-06-21 11:42:27 +0800952
953 if (priv->signal_voltage != mmc->signal_voltage) {
954 ret = esdhc_set_voltage(mmc);
955 if (ret) {
Marek Vasutba79fed2020-05-22 18:28:33 +0200956 if (ret != -ENOTSUPP)
957 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lu982f4252019-06-21 11:42:27 +0800958 return ret;
959 }
960 }
961#endif
962
963 /* Set the bus width */
964 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
965
966 if (mmc->bus_width == 4)
967 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
968 else if (mmc->bus_width == 8)
969 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
970
971 return 0;
972}
973
974static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
975{
976 struct fsl_esdhc *regs = priv->esdhc_regs;
977 ulong start;
978
979 /* Reset the entire host controller */
980 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
981
982 /* Wait until the controller is available */
983 start = get_timer(0);
984 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
985 if (get_timer(start) > 1000)
986 return -ETIMEDOUT;
987 }
988
Sean Anderson3e330792021-11-23 15:03:46 -0500989 if (IS_ENABLED(CONFIG_FSL_USDHC)) {
990 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
991 esdhc_write32(&regs->mmcboot, 0x0);
992 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
993 esdhc_write32(&regs->mixctrl, 0x0);
994 esdhc_write32(&regs->clktunectrlstatus, 0x0);
Yangbo Lu982f4252019-06-21 11:42:27 +0800995
Sean Anderson3e330792021-11-23 15:03:46 -0500996 /* Put VEND_SPEC to default value */
997 if (priv->vs18_enable)
998 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT |
999 ESDHC_VENDORSPEC_VSELECT);
1000 else
1001 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
Yangbo Lu982f4252019-06-21 11:42:27 +08001002
Sean Anderson3e330792021-11-23 15:03:46 -05001003 /* Disable DLL_CTRL delay line */
1004 esdhc_write32(&regs->dllctrl, 0x0);
1005 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001006
Sean Anderson3e330792021-11-23 15:03:46 -05001007 if (IS_ENABLED(CONFIG_FSL_USDHC))
1008 esdhc_setbits32(&regs->vendorspec,
1009 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1010 else
1011 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Yangbo Lu982f4252019-06-21 11:42:27 +08001012
1013 /* Set the initial clock speed */
Sean Andersonab3d1542021-11-23 15:03:47 -05001014 set_sysctl(priv, mmc, 400000);
Yangbo Lu982f4252019-06-21 11:42:27 +08001015
1016 /* Disable the BRR and BWR bits in IRQSTAT */
1017 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1018
Yangbo Lu982f4252019-06-21 11:42:27 +08001019 /* Put the PROCTL reg back to the default */
Sean Anderson3e330792021-11-23 15:03:46 -05001020 if (IS_ENABLED(CONFIG_MCF5441x))
1021 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1022 else
1023 esdhc_write32(&regs->proctl, PROCTL_INIT);
Yangbo Lu982f4252019-06-21 11:42:27 +08001024
1025 /* Set timout to the maximum value */
1026 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1027
1028 return 0;
1029}
1030
1031static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1032{
1033 struct fsl_esdhc *regs = priv->esdhc_regs;
1034 int timeout = 1000;
1035
Sean Anderson3e330792021-11-23 15:03:46 -05001036 if (IS_ENABLED(CONFIG_ESDHC_DETECT_QUIRK))
Yangbo Lu982f4252019-06-21 11:42:27 +08001037 return 1;
Yangbo Lu982f4252019-06-21 11:42:27 +08001038
Sean Anderson3e330792021-11-23 15:03:46 -05001039 if (CONFIG_IS_ENABLED(DM_MMC)) {
1040 if (priv->broken_cd)
1041 return 1;
Simon Glassfa4689a2019-12-06 21:41:35 -07001042#if CONFIG_IS_ENABLED(DM_GPIO)
Sean Anderson3e330792021-11-23 15:03:46 -05001043 if (dm_gpio_is_valid(&priv->cd_gpio))
1044 return dm_gpio_get_value(&priv->cd_gpio);
Yangbo Lu982f4252019-06-21 11:42:27 +08001045#endif
Sean Anderson3e330792021-11-23 15:03:46 -05001046 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001047
1048 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1049 udelay(1000);
1050
1051 return timeout > 0;
1052}
1053
1054static int esdhc_reset(struct fsl_esdhc *regs)
1055{
1056 ulong start;
1057
1058 /* reset the controller */
1059 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1060
1061 /* hardware clears the bit when it is done */
1062 start = get_timer(0);
1063 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1064 if (get_timer(start) > 100) {
1065 printf("MMC/SD: Reset never completed.\n");
1066 return -ETIMEDOUT;
1067 }
1068 }
1069
1070 return 0;
1071}
1072
1073#if !CONFIG_IS_ENABLED(DM_MMC)
1074static int esdhc_getcd(struct mmc *mmc)
1075{
1076 struct fsl_esdhc_priv *priv = mmc->priv;
1077
1078 return esdhc_getcd_common(priv);
1079}
1080
1081static int esdhc_init(struct mmc *mmc)
1082{
1083 struct fsl_esdhc_priv *priv = mmc->priv;
1084
1085 return esdhc_init_common(priv, mmc);
1086}
1087
1088static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1089 struct mmc_data *data)
1090{
1091 struct fsl_esdhc_priv *priv = mmc->priv;
1092
1093 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1094}
1095
1096static int esdhc_set_ios(struct mmc *mmc)
1097{
1098 struct fsl_esdhc_priv *priv = mmc->priv;
1099
1100 return esdhc_set_ios_common(priv, mmc);
1101}
1102
1103static const struct mmc_ops esdhc_ops = {
1104 .getcd = esdhc_getcd,
1105 .init = esdhc_init,
1106 .send_cmd = esdhc_send_cmd,
1107 .set_ios = esdhc_set_ios,
1108};
1109#endif
1110
1111static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1112 struct fsl_esdhc_plat *plat)
1113{
1114 struct mmc_config *cfg;
1115 struct fsl_esdhc *regs;
Sean Anderson6dff7b72021-11-23 15:03:38 -05001116 u32 caps;
Yangbo Lu982f4252019-06-21 11:42:27 +08001117 int ret;
1118
1119 if (!priv)
1120 return -EINVAL;
1121
1122 regs = priv->esdhc_regs;
1123
1124 /* First reset the eSDHC controller */
1125 ret = esdhc_reset(regs);
1126 if (ret)
1127 return ret;
1128
Yangbo Lu982f4252019-06-21 11:42:27 +08001129 /* ColdFire, using SDHC_DATA[3] for card detection */
Sean Anderson218dfa12021-11-23 15:03:45 -05001130 if (IS_ENABLED(CONFIG_MCF5441x))
1131 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
Yangbo Lu982f4252019-06-21 11:42:27 +08001132
Sean Anderson218dfa12021-11-23 15:03:45 -05001133 if (IS_ENABLED(CONFIG_FSL_USDHC)) {
1134 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1135 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1136 } else {
1137 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1138 | SYSCTL_IPGEN | SYSCTL_CKEN);
1139 /* Clearing tuning bits in case ROM has set it already */
1140 esdhc_write32(&regs->mixctrl, 0);
1141 esdhc_write32(&regs->autoc12err, 0);
1142 esdhc_write32(&regs->clktunectrlstatus, 0);
1143 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001144
1145 if (priv->vs18_enable)
1146 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1147
Haibo Chen920f5d02020-09-30 15:52:23 +08001148 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lu982f4252019-06-21 11:42:27 +08001149 cfg = &plat->cfg;
Sean Anderson3e330792021-11-23 15:03:46 -05001150 if (!CONFIG_IS_ENABLED(DM_MMC))
1151 memset(cfg, '\0', sizeof(*cfg));
Yangbo Lu982f4252019-06-21 11:42:27 +08001152
Yangbo Lu982f4252019-06-21 11:42:27 +08001153 caps = esdhc_read32(&regs->hostcapblt);
Sean Anderson218dfa12021-11-23 15:03:45 -05001154
Yangbo Lu982f4252019-06-21 11:42:27 +08001155 /*
1156 * MCF5441x RM declares in more points that sdhc clock speed must
1157 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1158 * from host capabilities.
1159 */
Sean Anderson218dfa12021-11-23 15:03:45 -05001160 if (IS_ENABLED(CONFIG_MCF5441x))
1161 caps &= ~HOSTCAPBLT_HSS;
Yangbo Lu982f4252019-06-21 11:42:27 +08001162
Sean Anderson218dfa12021-11-23 15:03:45 -05001163 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
1164 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Sean Anderson6dff7b72021-11-23 15:03:38 -05001165
Sean Anderson218dfa12021-11-23 15:03:45 -05001166 if (IS_ENABLED(CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33))
1167 caps |= HOSTCAPBLT_VS33;
Yangbo Lu982f4252019-06-21 11:42:27 +08001168
Sean Anderson6dff7b72021-11-23 15:03:38 -05001169 if (caps & HOSTCAPBLT_VS18)
1170 cfg->voltages |= MMC_VDD_165_195;
1171 if (caps & HOSTCAPBLT_VS30)
1172 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
1173 if (caps & HOSTCAPBLT_VS33)
1174 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Yangbo Lu982f4252019-06-21 11:42:27 +08001175
1176 cfg->name = "FSL_SDHC";
Sean Anderson218dfa12021-11-23 15:03:45 -05001177
Yangbo Lu982f4252019-06-21 11:42:27 +08001178#if !CONFIG_IS_ENABLED(DM_MMC)
1179 cfg->ops = &esdhc_ops;
1180#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001181
Sean Anderson218dfa12021-11-23 15:03:45 -05001182 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE))
1183 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1184
Sean Anderson6dff7b72021-11-23 15:03:38 -05001185 if (caps & HOSTCAPBLT_HSS)
Yangbo Lu982f4252019-06-21 11:42:27 +08001186 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1187
Yangbo Lu982f4252019-06-21 11:42:27 +08001188 cfg->host_caps |= priv->caps;
1189
1190 cfg->f_min = 400000;
1191 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1192
1193 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1194
Haibo Chen920f5d02020-09-30 15:52:23 +08001195 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lu982f4252019-06-21 11:42:27 +08001196 if (priv->flags & ESDHC_FLAG_USDHC) {
1197 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +08001198 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +08001199
1200 val |= ESDHC_STD_TUNING_EN;
1201 val &= ~ESDHC_TUNING_START_TAP_MASK;
1202 val |= priv->tuning_start_tap;
1203 val &= ~ESDHC_TUNING_STEP_MASK;
1204 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chen43162c32020-06-22 19:38:04 +08001205
1206 /* Disable the CMD CRC check for tuning, if not, need to
1207 * add some delay after every tuning command, because
1208 * hardware standard tuning logic will directly go to next
1209 * step once it detect the CMD CRC error, will not wait for
1210 * the card side to finally send out the tuning data, trigger
1211 * the buffer read ready interrupt immediately. If usdhc send
1212 * the next tuning command some eMMC card will stuck, can't
1213 * response, block the tuning procedure or the first command
1214 * after the whole tuning procedure always can't get any response.
1215 */
1216 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chen920f5d02020-09-30 15:52:23 +08001217 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +08001218 }
Adam Ford97497fd2022-01-12 07:53:56 -06001219
1220 /*
1221 * UHS doesn't have explicit ESDHC flags, so if it's
1222 * not supported, disable it in config.
1223 */
1224 if (CONFIG_IS_ENABLED(MMC_UHS_SUPPORT))
1225 cfg->host_caps |= UHS_CAPS;
Yangbo Lu982f4252019-06-21 11:42:27 +08001226
Adam Ford97497fd2022-01-12 07:53:56 -06001227 if (CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)) {
1228 if (priv->flags & ESDHC_FLAG_HS200)
1229 cfg->host_caps |= MMC_CAP(MMC_HS_200);
1230 }
1231
1232 if (CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)) {
1233 if (priv->flags & ESDHC_FLAG_HS400)
1234 cfg->host_caps |= MMC_CAP(MMC_HS_400);
1235 }
1236
1237 if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)) {
1238 if (priv->flags & ESDHC_FLAG_HS400_ES)
1239 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
1240 }
1241 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001242 return 0;
1243}
1244
1245#if !CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001246int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu982f4252019-06-21 11:42:27 +08001247{
1248 struct fsl_esdhc_plat *plat;
1249 struct fsl_esdhc_priv *priv;
Sean Anderson1f0d6c62021-11-23 15:03:39 -05001250 struct mmc_config *mmc_cfg;
Yangbo Lu982f4252019-06-21 11:42:27 +08001251 struct mmc *mmc;
1252 int ret;
1253
1254 if (!cfg)
1255 return -EINVAL;
1256
1257 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1258 if (!priv)
1259 return -ENOMEM;
1260 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1261 if (!plat) {
1262 free(priv);
1263 return -ENOMEM;
1264 }
1265
Sean Anderson1f0d6c62021-11-23 15:03:39 -05001266 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1267 priv->sdhc_clk = cfg->sdhc_clk;
1268 priv->wp_enable = cfg->wp_enable;
1269
1270 mmc_cfg = &plat->cfg;
1271
1272 switch (cfg->max_bus_width) {
1273 case 0: /* Not set in config; assume everything is supported */
1274 case 8:
1275 mmc_cfg->host_caps |= MMC_MODE_8BIT;
1276 fallthrough;
1277 case 4:
1278 mmc_cfg->host_caps |= MMC_MODE_4BIT;
1279 fallthrough;
1280 case 1:
1281 mmc_cfg->host_caps |= MMC_MODE_1BIT;
1282 break;
1283 default:
1284 printf("invalid max bus width %u\n", cfg->max_bus_width);
1285 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001286 }
1287
Sean Anderson218dfa12021-11-23 15:03:45 -05001288 if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
Sean Anderson1f0d6c62021-11-23 15:03:39 -05001289 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
Sean Anderson1f0d6c62021-11-23 15:03:39 -05001290
Yangbo Lu982f4252019-06-21 11:42:27 +08001291 ret = fsl_esdhc_init(priv, plat);
1292 if (ret) {
1293 debug("%s init failure\n", __func__);
1294 free(plat);
1295 free(priv);
1296 return ret;
1297 }
1298
1299 mmc = mmc_create(&plat->cfg, priv);
1300 if (!mmc)
1301 return -EIO;
1302
1303 priv->mmc = mmc;
1304
1305 return 0;
1306}
1307
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001308int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu982f4252019-06-21 11:42:27 +08001309{
1310 struct fsl_esdhc_cfg *cfg;
1311
1312 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1313 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1314 cfg->sdhc_clk = gd->arch.sdhc_clk;
1315 return fsl_esdhc_initialize(bis, cfg);
1316}
1317#endif
1318
Sean Anderson3e330792021-11-23 15:03:46 -05001319#if CONFIG_IS_ENABLED(OF_LIBFDT)
Yangbo Lu982f4252019-06-21 11:42:27 +08001320__weak int esdhc_status_fixup(void *blob, const char *compat)
1321{
Sean Anderson3e330792021-11-23 15:03:46 -05001322 if (IS_ENABLED(FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) {
Yangbo Lu982f4252019-06-21 11:42:27 +08001323 do_fixup_by_compat(blob, compat, "status", "disabled",
1324 sizeof("disabled"), 1);
1325 return 1;
1326 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001327 return 0;
1328}
1329
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001330void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lu982f4252019-06-21 11:42:27 +08001331{
1332 const char *compat = "fsl,esdhc";
1333
1334 if (esdhc_status_fixup(blob, compat))
1335 return;
1336
Yangbo Lu982f4252019-06-21 11:42:27 +08001337 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1338 gd->arch.sdhc_clk, 1);
Yangbo Lu982f4252019-06-21 11:42:27 +08001339}
1340#endif
1341
1342#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +08001343#include <asm/arch/clock.h>
Yangbo Lu982f4252019-06-21 11:42:27 +08001344__weak void init_clk_usdhc(u32 index)
1345{
1346}
1347
Simon Glassaad29ae2020-12-03 16:55:21 -07001348static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lu982f4252019-06-21 11:42:27 +08001349{
Yangbo Lu982f4252019-06-21 11:42:27 +08001350 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001351 struct udevice *vqmmc_dev;
Walter Lozano8aff6732020-07-29 12:31:17 -03001352 int ret;
Sean Anderson3e330792021-11-23 15:03:46 -05001353
Walter Lozano8aff6732020-07-29 12:31:17 -03001354 const void *fdt = gd->fdt_blob;
1355 int node = dev_of_offset(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001356 fdt_addr_t addr;
1357 unsigned int val;
Yangbo Lu982f4252019-06-21 11:42:27 +08001358
Simon Glass6d70ba02021-08-07 07:24:06 -06001359 if (!CONFIG_IS_ENABLED(OF_REAL))
1360 return 0;
1361
Yangbo Lu982f4252019-06-21 11:42:27 +08001362 addr = dev_read_addr(dev);
1363 if (addr == FDT_ADDR_T_NONE)
1364 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001365 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lu982f4252019-06-21 11:42:27 +08001366 priv->dev = dev;
1367 priv->mode = -1;
Yangbo Lu982f4252019-06-21 11:42:27 +08001368
Yangbo Lu982f4252019-06-21 11:42:27 +08001369 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1370 priv->tuning_step = val;
1371 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1372 ESDHC_TUNING_START_TAP_DEFAULT);
1373 priv->tuning_start_tap = val;
1374 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1375 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1376 priv->strobe_dll_delay_target = val;
Haibo Chen26154952021-03-22 18:55:38 +08001377 val = fdtdec_get_int(fdt, node, "fsl,signal-voltage-switch-extra-delay-ms", 0);
1378 priv->signal_voltage_switch_extra_delay_ms = val;
Yangbo Lu982f4252019-06-21 11:42:27 +08001379
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001380 if (dev_read_bool(dev, "broken-cd"))
1381 priv->broken_cd = 1;
1382
Yangbo Lu982f4252019-06-21 11:42:27 +08001383 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1384 priv->wp_enable = 1;
1385 } else {
1386 priv->wp_enable = 0;
Sean Anderson5b8040b2021-11-23 15:03:40 -05001387 }
1388
Simon Glassfa4689a2019-12-06 21:41:35 -07001389#if CONFIG_IS_ENABLED(DM_GPIO)
Sean Anderson5b8040b2021-11-23 15:03:40 -05001390 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1391 GPIOD_IS_IN);
1392 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1393 GPIOD_IS_IN);
Yangbo Lu982f4252019-06-21 11:42:27 +08001394#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001395
1396 priv->vs18_enable = 0;
1397
Sean Anderson3e330792021-11-23 15:03:46 -05001398 if (!CONFIG_IS_ENABLED(DM_REGULATOR))
1399 return 0;
1400
Yangbo Lu982f4252019-06-21 11:42:27 +08001401 /*
1402 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1403 * otherwise, emmc will work abnormally.
1404 */
1405 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1406 if (ret) {
1407 dev_dbg(dev, "no vqmmc-supply\n");
1408 } else {
Marek Vasut34e67f92020-05-22 18:19:08 +02001409 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lu982f4252019-06-21 11:42:27 +08001410 ret = regulator_set_enable(vqmmc_dev, true);
1411 if (ret) {
1412 dev_err(dev, "fail to enable vqmmc-supply\n");
1413 return ret;
1414 }
1415
1416 if (regulator_get_value(vqmmc_dev) == 1800000)
1417 priv->vs18_enable = 1;
1418 }
Walter Lozano8aff6732020-07-29 12:31:17 -03001419 return 0;
1420}
1421
1422static int fsl_esdhc_probe(struct udevice *dev)
1423{
1424 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001425 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano8aff6732020-07-29 12:31:17 -03001426 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1427 struct esdhc_soc_data *data =
1428 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1429 struct mmc *mmc;
Walter Lozano8aff6732020-07-29 12:31:17 -03001430 int ret;
1431
1432#if CONFIG_IS_ENABLED(OF_PLATDATA)
1433 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
Walter Lozano8aff6732020-07-29 12:31:17 -03001434
1435 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001436
1437 if (dtplat->non_removable)
Sean Anderson5b8040b2021-11-23 15:03:40 -05001438 plat->cfg.host_caps |= MMC_CAP_NONREMOVABLE;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001439 else
Sean Anderson5b8040b2021-11-23 15:03:40 -05001440 plat->cfg.host_caps &= ~MMC_CAP_NONREMOVABLE;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001441
Sean Anderson5b8040b2021-11-23 15:03:40 -05001442 if (CONFIG_IS_ENABLED(DM_GPIO) && !dtplat->non_removable) {
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001443 struct udevice *gpiodev;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001444
Simon Glass0000e0d2021-03-15 17:25:28 +13001445 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001446 if (ret)
1447 return ret;
1448
1449 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1450 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1451 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1452
1453 if (ret)
1454 return ret;
1455 }
Walter Lozano8aff6732020-07-29 12:31:17 -03001456#endif
1457
1458 if (data)
1459 priv->flags = data->flags;
Yangbo Lu982f4252019-06-21 11:42:27 +08001460
Yangbo Lu982f4252019-06-21 11:42:27 +08001461 /*
1462 * TODO:
1463 * Because lack of clk driver, if SDHC clk is not enabled,
1464 * need to enable it first before this driver is invoked.
1465 *
1466 * we use MXC_ESDHC_CLK to get clk freq.
1467 * If one would like to make this function work,
1468 * the aliases should be provided in dts as this:
1469 *
1470 * aliases {
1471 * mmc0 = &usdhc1;
1472 * mmc1 = &usdhc2;
1473 * mmc2 = &usdhc3;
1474 * mmc3 = &usdhc4;
1475 * };
1476 * Then if your board only supports mmc2 and mmc3, but we can
1477 * correctly get the seq as 2 and 3, then let mxc_get_clock
1478 * work as expected.
1479 */
1480
Simon Glass75e534b2020-12-16 21:20:07 -07001481 init_clk_usdhc(dev_seq(dev));
Yangbo Lu982f4252019-06-21 11:42:27 +08001482
Giulio Benettidbdbc632020-01-10 15:51:45 +01001483#if CONFIG_IS_ENABLED(CLK)
1484 /* Assigned clock already set clock */
1485 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1486 if (ret) {
1487 printf("Failed to get per_clk\n");
1488 return ret;
1489 }
1490 ret = clk_enable(&priv->per_clk);
1491 if (ret) {
1492 printf("Failed to enable per_clk\n");
1493 return ret;
1494 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001495
Giulio Benettidbdbc632020-01-10 15:51:45 +01001496 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1497#else
Simon Glass75e534b2020-12-16 21:20:07 -07001498 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettidbdbc632020-01-10 15:51:45 +01001499 if (priv->sdhc_clk <= 0) {
1500 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1501 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001502 }
Giulio Benettidbdbc632020-01-10 15:51:45 +01001503#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001504
1505 ret = fsl_esdhc_init(priv, plat);
1506 if (ret) {
1507 dev_err(dev, "fsl_esdhc_init failure\n");
1508 return ret;
1509 }
1510
Simon Glass6d70ba02021-08-07 07:24:06 -06001511 if (CONFIG_IS_ENABLED(OF_REAL)) {
1512 ret = mmc_of_parse(dev, &plat->cfg);
1513 if (ret)
1514 return ret;
1515 }
Peng Fan3766a482019-07-10 09:35:24 +00001516
Yangbo Lu982f4252019-06-21 11:42:27 +08001517 mmc = &plat->mmc;
1518 mmc->cfg = &plat->cfg;
1519 mmc->dev = dev;
Yangbo Lu982f4252019-06-21 11:42:27 +08001520
1521 upriv->mmc = mmc;
1522
1523 return esdhc_init_common(priv, mmc);
1524}
1525
Yangbo Lu982f4252019-06-21 11:42:27 +08001526static int fsl_esdhc_get_cd(struct udevice *dev)
1527{
Sean Anderson5b8040b2021-11-23 15:03:40 -05001528 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001529 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1530
Sean Anderson5b8040b2021-11-23 15:03:40 -05001531 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1532 return 1;
1533
Yangbo Lu982f4252019-06-21 11:42:27 +08001534 return esdhc_getcd_common(priv);
1535}
1536
1537static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1538 struct mmc_data *data)
1539{
Simon Glassfa20e932020-12-03 16:55:20 -07001540 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001541 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1542
1543 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1544}
1545
1546static int fsl_esdhc_set_ios(struct udevice *dev)
1547{
Simon Glassfa20e932020-12-03 16:55:20 -07001548 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001549 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1550
1551 return esdhc_set_ios_common(priv, &plat->mmc);
1552}
1553
Sean Anderson3e330792021-11-23 15:03:46 -05001554static int __maybe_unused fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
Peng Fan69b9d3a2019-07-10 09:35:26 +00001555{
1556 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1557 struct fsl_esdhc *regs = priv->esdhc_regs;
1558 u32 m;
1559
Haibo Chen920f5d02020-09-30 15:52:23 +08001560 m = esdhc_read32(&regs->mixctrl);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001561 m |= MIX_CTRL_HS400_ES;
Haibo Chen920f5d02020-09-30 15:52:23 +08001562 esdhc_write32(&regs->mixctrl, m);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001563
1564 return 0;
1565}
Peng Fan69b9d3a2019-07-10 09:35:26 +00001566
Haibo Chencb78f212020-11-05 14:57:13 +08001567static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1568 int timeout_us)
1569{
1570 int ret;
1571 u32 tmp;
1572 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1573 struct fsl_esdhc *regs = priv->esdhc_regs;
1574
1575 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1576 !!(tmp & PRSSTAT_DAT0) == !!state,
1577 timeout_us);
1578 return ret;
1579}
1580
Yangbo Lu982f4252019-06-21 11:42:27 +08001581static const struct dm_mmc_ops fsl_esdhc_ops = {
1582 .get_cd = fsl_esdhc_get_cd,
1583 .send_cmd = fsl_esdhc_send_cmd,
1584 .set_ios = fsl_esdhc_set_ios,
1585#ifdef MMC_SUPPORTS_TUNING
1586 .execute_tuning = fsl_esdhc_execute_tuning,
1587#endif
Peng Fan69b9d3a2019-07-10 09:35:26 +00001588#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1589 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1590#endif
Haibo Chencb78f212020-11-05 14:57:13 +08001591 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lu982f4252019-06-21 11:42:27 +08001592};
Yangbo Lu982f4252019-06-21 11:42:27 +08001593
1594static struct esdhc_soc_data usdhc_imx7d_data = {
1595 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1596 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1597 | ESDHC_FLAG_HS400,
Yangbo Lu982f4252019-06-21 11:42:27 +08001598};
1599
Jorge Ramirez-Ortiz8239e822021-09-08 21:56:42 +03001600static struct esdhc_soc_data usdhc_imx7ulp_data = {
1601 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
Oleksandr Suvorovd4245c22021-09-08 21:56:43 +03001602 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1603 | ESDHC_FLAG_HS400,
Jorge Ramirez-Ortiz8239e822021-09-08 21:56:42 +03001604};
1605
Peng Fan457fe962019-07-10 09:35:28 +00001606static struct esdhc_soc_data usdhc_imx8qm_data = {
1607 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1608 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1609 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1610};
1611
Yangbo Lu982f4252019-06-21 11:42:27 +08001612static const struct udevice_id fsl_esdhc_ids[] = {
Fabio Estevameaf8ccc2021-02-15 08:58:15 -03001613 { .compatible = "fsl,imx51-esdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001614 { .compatible = "fsl,imx53-esdhc", },
1615 { .compatible = "fsl,imx6ul-usdhc", },
1616 { .compatible = "fsl,imx6sx-usdhc", },
1617 { .compatible = "fsl,imx6sl-usdhc", },
1618 { .compatible = "fsl,imx6q-usdhc", },
1619 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Jorge Ramirez-Ortiz8239e822021-09-08 21:56:42 +03001620 { .compatible = "fsl,imx7ulp-usdhc", .data = (ulong)&usdhc_imx7ulp_data,},
Peng Fan457fe962019-07-10 09:35:28 +00001621 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fand7689fa2019-11-04 17:31:17 +08001622 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1623 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1624 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti65b5ec12020-01-10 15:51:46 +01001625 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001626 { .compatible = "fsl,esdhc", },
1627 { /* sentinel */ }
1628};
1629
Yangbo Lu982f4252019-06-21 11:42:27 +08001630static int fsl_esdhc_bind(struct udevice *dev)
1631{
Simon Glassfa20e932020-12-03 16:55:20 -07001632 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001633
1634 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1635}
Yangbo Lu982f4252019-06-21 11:42:27 +08001636
1637U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano904b0be2020-07-29 12:31:16 -03001638 .name = "fsl_esdhc",
Yangbo Lu982f4252019-06-21 11:42:27 +08001639 .id = UCLASS_MMC,
1640 .of_match = fsl_esdhc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001641 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lu982f4252019-06-21 11:42:27 +08001642 .ops = &fsl_esdhc_ops,
Yangbo Lu982f4252019-06-21 11:42:27 +08001643 .bind = fsl_esdhc_bind,
Yangbo Lu982f4252019-06-21 11:42:27 +08001644 .probe = fsl_esdhc_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -07001645 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001646 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lu982f4252019-06-21 11:42:27 +08001647};
Walter Lozano8aff6732020-07-29 12:31:17 -03001648
Simon Glassdf65db82020-12-28 20:34:57 -07001649DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lu982f4252019-06-21 11:42:27 +08001650#endif