blob: 283af35a81f889aae25c6d54589b6e3b7efa55af [file] [log] [blame]
Yangbo Lufa33d202019-06-21 11:42:27 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Yangbo Lub1d59862021-06-03 10:51:18 +08004 * Copyright 2019, 2021 NXP
Yangbo Lufa33d202019-06-21 11:42:27 +08005 * Andy Fleming
6 * Yangbo Lu <yangbo.lu@nxp.com>
7 *
8 * Based vaguely on the pxa mmc code:
9 * (C) Copyright 2003
10 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
11 */
12
13#include <config.h>
14#include <common.h>
15#include <command.h>
16#include <clk.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070017#include <cpu_func.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080018#include <errno.h>
19#include <hwconfig.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080021#include <mmc.h>
22#include <part.h>
Simon Glass90526e92020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glass61b29b82020-02-03 07:36:15 -070028#include <linux/err.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080029#include <power/regulator.h>
30#include <malloc.h>
31#include <fsl_esdhc_imx.h>
32#include <fdt_support.h>
33#include <asm/io.h>
34#include <dm.h>
35#include <asm-generic/gpio.h>
36#include <dm/pinctrl.h>
Walter Lozano23721772020-07-29 12:31:17 -030037#include <dt-structs.h>
38#include <mapmem.h>
39#include <dm/ofnode.h>
Haibo Chenf9c3a812020-09-01 15:34:06 +080040#include <linux/iopoll.h>
Sean Anderson01672672021-11-23 15:03:43 -050041#include <linux/dma-mapping.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080042
Haibo Chen0ba116a2021-02-19 11:25:32 -080043#ifndef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
44#ifdef CONFIG_FSL_USDHC
45#define ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 1
46#endif
47#endif
48
Yangbo Lufa33d202019-06-21 11:42:27 +080049DECLARE_GLOBAL_DATA_PTR;
50
51#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
52 IRQSTATEN_CINT | \
53 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
54 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
55 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
56 IRQSTATEN_DINT)
57#define MAX_TUNING_LOOP 40
Yangbo Lufa33d202019-06-21 11:42:27 +080058
59struct fsl_esdhc {
60 uint dsaddr; /* SDMA system address register */
61 uint blkattr; /* Block attributes register */
62 uint cmdarg; /* Command argument register */
63 uint xfertyp; /* Transfer type register */
64 uint cmdrsp0; /* Command response 0 register */
65 uint cmdrsp1; /* Command response 1 register */
66 uint cmdrsp2; /* Command response 2 register */
67 uint cmdrsp3; /* Command response 3 register */
68 uint datport; /* Buffer data port register */
69 uint prsstat; /* Present state register */
70 uint proctl; /* Protocol control register */
71 uint sysctl; /* System Control Register */
72 uint irqstat; /* Interrupt status register */
73 uint irqstaten; /* Interrupt status enable register */
74 uint irqsigen; /* Interrupt signal enable register */
75 uint autoc12err; /* Auto CMD error status register */
76 uint hostcapblt; /* Host controller capabilities register */
77 uint wml; /* Watermark level register */
78 uint mixctrl; /* For USDHC */
79 char reserved1[4]; /* reserved */
80 uint fevt; /* Force event register */
81 uint admaes; /* ADMA error status register */
82 uint adsaddr; /* ADMA system address register */
83 char reserved2[4];
84 uint dllctrl;
85 uint dllstat;
86 uint clktunectrlstatus;
87 char reserved3[4];
88 uint strobe_dllctrl;
89 uint strobe_dllstat;
90 char reserved4[72];
91 uint vendorspec;
92 uint mmcboot;
93 uint vendorspec2;
Giulio Benetti6a63a872020-01-10 15:51:46 +010094 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lufa33d202019-06-21 11:42:27 +080095 char reserved5[44];
96 uint hostver; /* Host controller version register */
97 char reserved6[4]; /* reserved */
98 uint dmaerraddr; /* DMA error address register */
99 char reserved7[4]; /* reserved */
100 uint dmaerrattr; /* DMA error attribute register */
101 char reserved8[4]; /* reserved */
102 uint hostcapblt2; /* Host controller capabilities register 2 */
103 char reserved9[8]; /* reserved */
104 uint tcr; /* Tuning control register */
105 char reserved10[28]; /* reserved */
106 uint sddirctl; /* SD direction control register */
107 char reserved11[712];/* reserved */
108 uint scr; /* eSDHC control register */
109};
110
111struct fsl_esdhc_plat {
Walter Lozano23721772020-07-29 12:31:17 -0300112#if CONFIG_IS_ENABLED(OF_PLATDATA)
113 /* Put this first since driver model will copy the data here */
114 struct dtd_fsl_esdhc dtplat;
115#endif
116
Yangbo Lufa33d202019-06-21 11:42:27 +0800117 struct mmc_config cfg;
118 struct mmc mmc;
119};
120
121struct esdhc_soc_data {
122 u32 flags;
Yangbo Lufa33d202019-06-21 11:42:27 +0800123};
124
125/**
126 * struct fsl_esdhc_priv
127 *
128 * @esdhc_regs: registers of the sdhc controller
129 * @sdhc_clk: Current clk of the sdhc controller
Yangbo Lufa33d202019-06-21 11:42:27 +0800130 * @cfg: mmc config
131 * @mmc: mmc
132 * Following is used when Driver Model is enabled for MMC
133 * @dev: pointer for the device
Fabio Estevam29230f32020-01-06 20:11:27 -0300134 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lufa33d202019-06-21 11:42:27 +0800135 * @wp_enable: 1: enable checking wp; 0: no check
136 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
137 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
138 * @caps: controller capabilities
139 * @tuning_step: tuning step setting in tuning_ctrl register
140 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
141 * @strobe_dll_delay_target: settings in strobe_dllctrl
142 * @signal_voltage: indicating the current voltage
Haibo Chen8974ff12021-03-22 18:55:38 +0800143 * @signal_voltage_switch_extra_delay_ms: extra delay for IO voltage switch
Yangbo Lufa33d202019-06-21 11:42:27 +0800144 * @cd_gpio: gpio for card detection
145 * @wp_gpio: gpio for write protection
146 */
147struct fsl_esdhc_priv {
148 struct fsl_esdhc *esdhc_regs;
149 unsigned int sdhc_clk;
150 struct clk per_clk;
151 unsigned int clock;
152 unsigned int mode;
Sean Anderson297d2de2022-01-12 08:18:52 +0900153#if !CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +0800154 struct mmc *mmc;
155#endif
156 struct udevice *dev;
Fabio Estevam29230f32020-01-06 20:11:27 -0300157 int broken_cd;
Yangbo Lufa33d202019-06-21 11:42:27 +0800158 int wp_enable;
159 int vs18_enable;
160 u32 flags;
161 u32 caps;
162 u32 tuning_step;
163 u32 tuning_start_tap;
164 u32 strobe_dll_delay_target;
165 u32 signal_voltage;
Haibo Chen8974ff12021-03-22 18:55:38 +0800166 u32 signal_voltage_switch_extra_delay_ms;
Ye Li82771712019-07-11 03:29:02 +0000167#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800168 struct udevice *vqmmc_dev;
169 struct udevice *vmmc_dev;
170#endif
Simon Glassbcee8d62019-12-06 21:41:35 -0700171#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +0800172 struct gpio_desc cd_gpio;
173 struct gpio_desc wp_gpio;
174#endif
Sean Anderson01672672021-11-23 15:03:43 -0500175 dma_addr_t dma_addr;
Yangbo Lufa33d202019-06-21 11:42:27 +0800176};
177
178/* Return the XFERTYP flags for a given command and data packet */
179static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
180{
181 uint xfertyp = 0;
182
183 if (data) {
184 xfertyp |= XFERTYP_DPSEL;
185#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
186 xfertyp |= XFERTYP_DMAEN;
187#endif
188 if (data->blocks > 1) {
189 xfertyp |= XFERTYP_MSBSEL;
190 xfertyp |= XFERTYP_BCEN;
191#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
192 xfertyp |= XFERTYP_AC12EN;
193#endif
194 }
195
196 if (data->flags & MMC_DATA_READ)
197 xfertyp |= XFERTYP_DTDSEL;
198 }
199
200 if (cmd->resp_type & MMC_RSP_CRC)
201 xfertyp |= XFERTYP_CCCEN;
202 if (cmd->resp_type & MMC_RSP_OPCODE)
203 xfertyp |= XFERTYP_CICEN;
204 if (cmd->resp_type & MMC_RSP_136)
205 xfertyp |= XFERTYP_RSPTYP_136;
206 else if (cmd->resp_type & MMC_RSP_BUSY)
207 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
208 else if (cmd->resp_type & MMC_RSP_PRESENT)
209 xfertyp |= XFERTYP_RSPTYP_48;
210
211 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
212 xfertyp |= XFERTYP_CMDTYP_ABORT;
213
214 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
215}
216
217#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
218/*
219 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
220 */
221static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
222 struct mmc_data *data)
223{
224 struct fsl_esdhc *regs = priv->esdhc_regs;
225 uint blocks;
226 char *buffer;
227 uint databuf;
228 uint size;
229 uint irqstat;
230 ulong start;
231
232 if (data->flags & MMC_DATA_READ) {
233 blocks = data->blocks;
234 buffer = data->dest;
235 while (blocks) {
236 start = get_timer(0);
237 size = data->blocksize;
238 irqstat = esdhc_read32(&regs->irqstat);
239 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
240 if (get_timer(start) > PIO_TIMEOUT) {
241 printf("\nData Read Failed in PIO Mode.");
242 return;
243 }
244 }
245 while (size && (!(irqstat & IRQSTAT_TC))) {
246 udelay(100); /* Wait before last byte transfer complete */
247 irqstat = esdhc_read32(&regs->irqstat);
248 databuf = in_le32(&regs->datport);
249 *((uint *)buffer) = databuf;
250 buffer += 4;
251 size -= 4;
252 }
253 blocks--;
254 }
255 } else {
256 blocks = data->blocks;
257 buffer = (char *)data->src;
258 while (blocks) {
259 start = get_timer(0);
260 size = data->blocksize;
261 irqstat = esdhc_read32(&regs->irqstat);
262 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
263 if (get_timer(start) > PIO_TIMEOUT) {
264 printf("\nData Write Failed in PIO Mode.");
265 return;
266 }
267 }
268 while (size && (!(irqstat & IRQSTAT_TC))) {
269 udelay(100); /* Wait before last byte transfer complete */
270 databuf = *((uint *)buffer);
271 buffer += 4;
272 size -= 4;
273 irqstat = esdhc_read32(&regs->irqstat);
274 out_le32(&regs->datport, databuf);
275 }
276 blocks--;
277 }
278 }
279}
280#endif
281
282static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
283 struct mmc_data *data)
284{
285 int timeout;
Sean Anderson01672672021-11-23 15:03:43 -0500286 uint trans_bytes = data->blocksize * data->blocks;
Yangbo Lufa33d202019-06-21 11:42:27 +0800287 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lufa33d202019-06-21 11:42:27 +0800288 uint wml_value;
289
290 wml_value = data->blocksize/4;
291
292 if (data->flags & MMC_DATA_READ) {
293 if (wml_value > WML_RD_WML_MAX)
294 wml_value = WML_RD_WML_MAX_VAL;
295
296 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
297#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Sean Anderson01672672021-11-23 15:03:43 -0500298 priv->dma_addr = dma_map_single(data->dest, trans_bytes,
299 mmc_get_dma_dir(data));
300 if (upper_32_bits(priv->dma_addr))
Sean Andersoned9e9b22021-11-23 15:03:42 -0500301 printf("Cannot use 64 bit addresses with SDMA\n");
Sean Anderson01672672021-11-23 15:03:43 -0500302 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
Yangbo Lufa33d202019-06-21 11:42:27 +0800303#endif
304 } else {
Yangbo Lufa33d202019-06-21 11:42:27 +0800305 if (wml_value > WML_WR_WML_MAX)
306 wml_value = WML_WR_WML_MAX_VAL;
307 if (priv->wp_enable) {
308 if ((esdhc_read32(&regs->prsstat) &
309 PRSSTAT_WPSPL) == 0) {
310 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
311 return -ETIMEDOUT;
312 }
313 } else {
Simon Glassbcee8d62019-12-06 21:41:35 -0700314#if CONFIG_IS_ENABLED(DM_GPIO)
315 if (dm_gpio_is_valid(&priv->wp_gpio) &&
316 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800317 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
318 return -ETIMEDOUT;
319 }
320#endif
321 }
322
323 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
324 wml_value << 16);
325#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Sean Anderson01672672021-11-23 15:03:43 -0500326 priv->dma_addr = dma_map_single((void *)data->src, trans_bytes,
327 mmc_get_dma_dir(data));
328 if (upper_32_bits(priv->dma_addr))
Sean Andersoned9e9b22021-11-23 15:03:42 -0500329 printf("Cannot use 64 bit addresses with SDMA\n");
Sean Anderson01672672021-11-23 15:03:43 -0500330 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
Yangbo Lufa33d202019-06-21 11:42:27 +0800331#endif
332 }
333
334 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
335
336 /* Calculate the timeout period for data transactions */
337 /*
338 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
339 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
340 * So, Number of SD Clock cycles for 0.25sec should be minimum
341 * (SD Clock/sec * 0.25 sec) SD Clock cycles
342 * = (mmc->clock * 1/4) SD Clock cycles
343 * As 1) >= 2)
344 * => (2^(timeout+13)) >= mmc->clock * 1/4
345 * Taking log2 both the sides
346 * => timeout + 13 >= log2(mmc->clock/4)
347 * Rounding up to next power of 2
348 * => timeout + 13 = log2(mmc->clock/4) + 1
349 * => timeout + 13 = fls(mmc->clock/4)
350 *
351 * However, the MMC spec "It is strongly recommended for hosts to
352 * implement more than 500ms timeout value even if the card
353 * indicates the 250ms maximum busy length." Even the previous
354 * value of 300ms is known to be insufficient for some cards.
355 * So, we use
356 * => timeout + 13 = fls(mmc->clock/2)
357 */
358 timeout = fls(mmc->clock/2);
359 timeout -= 13;
360
361 if (timeout > 14)
362 timeout = 14;
363
364 if (timeout < 0)
365 timeout = 0;
366
367#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
368 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
369 timeout++;
370#endif
371
372#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
373 timeout = 0xE;
374#endif
375 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
376
377 return 0;
378}
379
Yangbo Lufa33d202019-06-21 11:42:27 +0800380#ifdef CONFIG_MCF5441x
381/*
382 * Swaps 32-bit words to little-endian byte order.
383 */
384static inline void sd_swap_dma_buff(struct mmc_data *data)
385{
386 int i, size = data->blocksize >> 2;
387 u32 *buffer = (u32 *)data->dest;
388 u32 sw;
389
390 while (data->blocks--) {
391 for (i = 0; i < size; i++) {
392 sw = __sw32(*buffer);
393 *buffer++ = sw;
394 }
395 }
396}
397#endif
398
399/*
400 * Sends a command out on the bus. Takes the mmc pointer,
401 * a command pointer, and an optional data pointer.
402 */
403static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
404 struct mmc_cmd *cmd, struct mmc_data *data)
405{
406 int err = 0;
407 uint xfertyp;
408 uint irqstat;
409 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
410 struct fsl_esdhc *regs = priv->esdhc_regs;
411 unsigned long start;
412
413#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
414 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
415 return 0;
416#endif
417
418 esdhc_write32(&regs->irqstat, -1);
419
420 sync();
421
422 /* Wait for the bus to be idle */
423 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
424 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
425 ;
426
427 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
428 ;
429
Yangbo Lufa33d202019-06-21 11:42:27 +0800430 /* Set up for a data transfer if we have one */
431 if (data) {
432 err = esdhc_setup_data(priv, mmc, data);
433 if(err)
434 return err;
Yangbo Lufa33d202019-06-21 11:42:27 +0800435 }
436
437 /* Figure out the transfer arguments */
438 xfertyp = esdhc_xfertyp(cmd, data);
439
440 /* Mask all irqs */
441 esdhc_write32(&regs->irqsigen, 0);
442
443 /* Send the command */
444 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
445#if defined(CONFIG_FSL_USDHC)
446 esdhc_write32(&regs->mixctrl,
447 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
448 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
449 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
450#else
451 esdhc_write32(&regs->xfertyp, xfertyp);
452#endif
453
454 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
455 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
456 flags = IRQSTAT_BRR;
457
458 /* Wait for the command to complete */
459 start = get_timer(0);
460 while (!(esdhc_read32(&regs->irqstat) & flags)) {
461 if (get_timer(start) > 1000) {
462 err = -ETIMEDOUT;
463 goto out;
464 }
465 }
466
467 irqstat = esdhc_read32(&regs->irqstat);
468
469 if (irqstat & CMD_ERR) {
470 err = -ECOMM;
471 goto out;
472 }
473
474 if (irqstat & IRQSTAT_CTOE) {
475 err = -ETIMEDOUT;
476 goto out;
477 }
478
Yangbo Lufa33d202019-06-21 11:42:27 +0800479 /* Workaround for ESDHC errata ENGcm03648 */
480 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan356f7822019-07-10 09:35:30 +0000481 int timeout = 50000;
Yangbo Lufa33d202019-06-21 11:42:27 +0800482
Peng Fan356f7822019-07-10 09:35:30 +0000483 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lufa33d202019-06-21 11:42:27 +0800484 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
485 PRSSTAT_DAT0)) {
486 udelay(100);
487 timeout--;
488 }
489
490 if (timeout <= 0) {
491 printf("Timeout waiting for DAT0 to go high!\n");
492 err = -ETIMEDOUT;
493 goto out;
494 }
495 }
496
497 /* Copy the response to the response buffer */
498 if (cmd->resp_type & MMC_RSP_136) {
499 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
500
501 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
502 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
503 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
504 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
505 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
506 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
507 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
508 cmd->response[3] = (cmdrsp0 << 8);
509 } else
510 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
511
512 /* Wait until all of the blocks are transferred */
513 if (data) {
514#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
515 esdhc_pio_read_write(priv, data);
516#else
517 flags = DATA_COMPLETE;
518 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
519 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
520 flags = IRQSTAT_BRR;
521 }
522
523 do {
524 irqstat = esdhc_read32(&regs->irqstat);
525
526 if (irqstat & IRQSTAT_DTOE) {
527 err = -ETIMEDOUT;
528 goto out;
529 }
530
531 if (irqstat & DATA_ERR) {
532 err = -ECOMM;
533 goto out;
534 }
535 } while ((irqstat & flags) != flags);
536
537 /*
538 * Need invalidate the dcache here again to avoid any
539 * cache-fill during the DMA operations such as the
540 * speculative pre-fetching etc.
541 */
Sean Anderson01672672021-11-23 15:03:43 -0500542 dma_unmap_single(priv->dma_addr,
543 data->blocks * data->blocksize,
544 mmc_get_dma_dir(data));
Yangbo Lufa33d202019-06-21 11:42:27 +0800545#ifdef CONFIG_MCF5441x
Sean Anderson01672672021-11-23 15:03:43 -0500546 if (data->flags & MMC_DATA_READ)
Yangbo Lufa33d202019-06-21 11:42:27 +0800547 sd_swap_dma_buff(data);
548#endif
Yangbo Lufa33d202019-06-21 11:42:27 +0800549#endif
550 }
551
552out:
553 /* Reset CMD and DATA portions on error */
554 if (err) {
555 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
556 SYSCTL_RSTC);
557 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
558 ;
559
560 if (data) {
561 esdhc_write32(&regs->sysctl,
562 esdhc_read32(&regs->sysctl) |
563 SYSCTL_RSTD);
564 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
565 ;
566 }
567
568 /* If this was CMD11, then notify that power cycle is needed */
569 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
570 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
571 }
572
573 esdhc_write32(&regs->irqstat, -1);
574
575 return err;
576}
577
578static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
579{
580 struct fsl_esdhc *regs = priv->esdhc_regs;
581 int div = 1;
Haibo Chenf9c3a812020-09-01 15:34:06 +0800582 u32 tmp;
583 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +0800584#ifdef ARCH_MXC
585#ifdef CONFIG_MX53
586 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
587 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
588#else
589 int pre_div = 1;
590#endif
591#else
592 int pre_div = 2;
593#endif
594 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
595 int sdhc_clk = priv->sdhc_clk;
596 uint clk;
597
Yangbo Lufa33d202019-06-21 11:42:27 +0800598 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
599 pre_div *= 2;
600
601 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
602 div++;
603
604 pre_div >>= 1;
605 div -= 1;
606
607 clk = (pre_div << 8) | (div << 4);
608
609#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300610 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800611#else
612 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
613#endif
614
615 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
616
Haibo Chenf9c3a812020-09-01 15:34:06 +0800617 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
618 if (ret)
619 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lufa33d202019-06-21 11:42:27 +0800620
621#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300622 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800623#else
624 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
625#endif
626
Sean Anderson4ea11bf2021-11-23 15:03:41 -0500627 mmc->clock = sdhc_clk / pre_div / div;
Yangbo Lufa33d202019-06-21 11:42:27 +0800628 priv->clock = clock;
629}
630
Yangbo Lufa33d202019-06-21 11:42:27 +0800631#ifdef MMC_SUPPORTS_TUNING
632static int esdhc_change_pinstate(struct udevice *dev)
633{
634 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
635 int ret;
636
637 switch (priv->mode) {
638 case UHS_SDR50:
639 case UHS_DDR50:
640 ret = pinctrl_select_state(dev, "state_100mhz");
641 break;
642 case UHS_SDR104:
643 case MMC_HS_200:
644 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000645 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800646 ret = pinctrl_select_state(dev, "state_200mhz");
647 break;
648 default:
649 ret = pinctrl_select_state(dev, "default");
650 break;
651 }
652
653 if (ret)
654 printf("%s %d error\n", __func__, priv->mode);
655
656 return ret;
657}
658
659static void esdhc_reset_tuning(struct mmc *mmc)
660{
661 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
662 struct fsl_esdhc *regs = priv->esdhc_regs;
663
664 if (priv->flags & ESDHC_FLAG_USDHC) {
665 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
666 esdhc_clrbits32(&regs->autoc12err,
667 MIX_CTRL_SMPCLK_SEL |
668 MIX_CTRL_EXE_TUNE);
669 }
670 }
671}
672
673static void esdhc_set_strobe_dll(struct mmc *mmc)
674{
675 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
676 struct fsl_esdhc *regs = priv->esdhc_regs;
677 u32 val;
678
679 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800680 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300681 /* clear the reset bit on strobe dll before any setting */
682 esdhc_write32(&regs->strobe_dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +0800683
684 /*
685 * enable strobe dll ctrl and adjust the delay target
686 * for the uSDHC loopback read clock
687 */
688 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300689 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
Yangbo Lufa33d202019-06-21 11:42:27 +0800690 (priv->strobe_dll_delay_target <<
691 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chenc7f44182020-09-30 15:52:23 +0800692 esdhc_write32(&regs->strobe_dllctrl, val);
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +0300693 /* wait 5us to make sure strobe dll status register stable */
694 mdelay(5);
Haibo Chenc7f44182020-09-30 15:52:23 +0800695 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lufa33d202019-06-21 11:42:27 +0800696 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
697 pr_warn("HS400 strobe DLL status REF not lock!\n");
698 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
699 pr_warn("HS400 strobe DLL status SLV not lock!\n");
700 }
701}
702
703static int esdhc_set_timing(struct mmc *mmc)
704{
705 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
706 struct fsl_esdhc *regs = priv->esdhc_regs;
707 u32 mixctrl;
708
Haibo Chenc7f44182020-09-30 15:52:23 +0800709 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800710 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
711
712 switch (mmc->selected_mode) {
713 case MMC_LEGACY:
Yangbo Lufa33d202019-06-21 11:42:27 +0800714 esdhc_reset_tuning(mmc);
Haibo Chenc7f44182020-09-30 15:52:23 +0800715 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800716 break;
717 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000718 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800719 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800720 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800721 break;
722 case MMC_HS:
723 case MMC_HS_52:
724 case MMC_HS_200:
725 case SD_HS:
726 case UHS_SDR12:
727 case UHS_SDR25:
728 case UHS_SDR50:
729 case UHS_SDR104:
Haibo Chenc7f44182020-09-30 15:52:23 +0800730 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800731 break;
732 case UHS_DDR50:
733 case MMC_DDR_52:
734 mixctrl |= MIX_CTRL_DDREN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800735 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800736 break;
737 default:
738 printf("Not supported %d\n", mmc->selected_mode);
739 return -EINVAL;
740 }
741
742 priv->mode = mmc->selected_mode;
743
744 return esdhc_change_pinstate(mmc->dev);
745}
746
747static int esdhc_set_voltage(struct mmc *mmc)
748{
749 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
750 struct fsl_esdhc *regs = priv->esdhc_regs;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100751#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800752 int ret;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100753#endif
Yangbo Lufa33d202019-06-21 11:42:27 +0800754
755 priv->signal_voltage = mmc->signal_voltage;
756 switch (mmc->signal_voltage) {
757 case MMC_SIGNAL_VOLTAGE_330:
758 if (priv->vs18_enable)
Marek Vasut50a17a62020-05-22 18:28:33 +0200759 return -ENOTSUPP;
Yangbo Lufa33d202019-06-21 11:42:27 +0800760#if CONFIG_IS_ENABLED(DM_REGULATOR)
761 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
762 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
763 if (ret) {
764 printf("Setting to 3.3V error");
765 return -EIO;
766 }
767 /* Wait for 5ms */
768 mdelay(5);
769 }
770#endif
771
772 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
773 if (!(esdhc_read32(&regs->vendorspec) &
774 ESDHC_VENDORSPEC_VSELECT))
775 return 0;
776
777 return -EAGAIN;
778 case MMC_SIGNAL_VOLTAGE_180:
779#if CONFIG_IS_ENABLED(DM_REGULATOR)
780 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
781 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
782 if (ret) {
783 printf("Setting to 1.8V error");
784 return -EIO;
785 }
786 }
787#endif
788 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
Haibo Chen8974ff12021-03-22 18:55:38 +0800789 /*
790 * some board like imx8mm-evk need about 18ms to switch
791 * the IO voltage from 3.3v to 1.8v, common code only
792 * delay 10ms, so need to delay extra time to make sure
793 * the IO voltage change to 1.8v.
794 */
795 if (priv->signal_voltage_switch_extra_delay_ms)
796 mdelay(priv->signal_voltage_switch_extra_delay_ms);
Yangbo Lufa33d202019-06-21 11:42:27 +0800797 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
798 return 0;
799
800 return -EAGAIN;
801 case MMC_SIGNAL_VOLTAGE_120:
802 return -ENOTSUPP;
803 default:
804 return 0;
805 }
806}
807
808static void esdhc_stop_tuning(struct mmc *mmc)
809{
810 struct mmc_cmd cmd;
811
812 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
813 cmd.cmdarg = 0;
814 cmd.resp_type = MMC_RSP_R1b;
815
Jaehoon Chung2da23352021-05-31 08:31:49 +0900816 mmc_send_cmd(mmc, &cmd, NULL);
Yangbo Lufa33d202019-06-21 11:42:27 +0800817}
818
819static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
820{
Simon Glassc69cda22020-12-03 16:55:20 -0700821 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +0800822 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
823 struct fsl_esdhc *regs = priv->esdhc_regs;
824 struct mmc *mmc = &plat->mmc;
Haibo Chenc7f44182020-09-30 15:52:23 +0800825 u32 irqstaten = esdhc_read32(&regs->irqstaten);
826 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800827 int i, ret = -ETIMEDOUT;
828 u32 val, mixctrl;
829
830 /* clock tuning is not needed for upto 52MHz */
831 if (mmc->clock <= 52000000)
832 return 0;
833
834 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
835 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800836 val = esdhc_read32(&regs->autoc12err);
837 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800838 val &= ~MIX_CTRL_SMPCLK_SEL;
839 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
840
841 val |= MIX_CTRL_EXE_TUNE;
842 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
843
Haibo Chenc7f44182020-09-30 15:52:23 +0800844 esdhc_write32(&regs->autoc12err, val);
845 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800846 }
847
848 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chenc7f44182020-09-30 15:52:23 +0800849 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800850 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800851 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800852
Haibo Chenc7f44182020-09-30 15:52:23 +0800853 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
854 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lufa33d202019-06-21 11:42:27 +0800855
856 /*
857 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
858 * of loops reaches 40 times.
859 */
860 for (i = 0; i < MAX_TUNING_LOOP; i++) {
861 u32 ctrl;
862
863 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
864 if (mmc->bus_width == 8)
Haibo Chenc7f44182020-09-30 15:52:23 +0800865 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lufa33d202019-06-21 11:42:27 +0800866 else if (mmc->bus_width == 4)
Haibo Chenc7f44182020-09-30 15:52:23 +0800867 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800868 } else {
Haibo Chenc7f44182020-09-30 15:52:23 +0800869 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800870 }
871
872 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chenc7f44182020-09-30 15:52:23 +0800873 val = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800874 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800875 esdhc_write32(&regs->mixctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800876
877 /* We are using STD tuning, no need to check return value */
878 mmc_send_tuning(mmc, opcode, NULL);
879
Haibo Chenc7f44182020-09-30 15:52:23 +0800880 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lufa33d202019-06-21 11:42:27 +0800881 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
882 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800883 ret = 0;
884 break;
885 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800886 }
887
Haibo Chenc7f44182020-09-30 15:52:23 +0800888 esdhc_write32(&regs->irqstaten, irqstaten);
889 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800890
891 esdhc_stop_tuning(mmc);
892
893 return ret;
894}
895#endif
896
897static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
898{
899 struct fsl_esdhc *regs = priv->esdhc_regs;
900 int ret __maybe_unused;
Peng Fan1d01c982019-11-04 17:14:15 +0800901 u32 clock;
Yangbo Lufa33d202019-06-21 11:42:27 +0800902
Haibo Chen5d772192020-11-03 17:18:35 +0800903#ifdef MMC_SUPPORTS_TUNING
904 /*
905 * call esdhc_set_timing() before update the clock rate,
906 * This is because current we support DDR and SDR mode,
907 * Once the DDR_EN bit is set, the card clock will be
908 * divide by 2 automatically. So need to do this before
909 * setting clock rate.
910 */
911 if (priv->mode != mmc->selected_mode) {
912 ret = esdhc_set_timing(mmc);
913 if (ret) {
914 printf("esdhc_set_timing error %d\n", ret);
915 return ret;
916 }
917 }
918#endif
919
Yangbo Lufa33d202019-06-21 11:42:27 +0800920 /* Set the clock speed */
Peng Fan1d01c982019-11-04 17:14:15 +0800921 clock = mmc->clock;
922 if (clock < mmc->cfg->f_min)
923 clock = mmc->cfg->f_min;
924
925 if (priv->clock != clock)
926 set_sysctl(priv, mmc, clock);
Yangbo Lufa33d202019-06-21 11:42:27 +0800927
Yangbo Lufa33d202019-06-21 11:42:27 +0800928 if (mmc->clk_disable) {
929#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300930 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800931#else
932 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
933#endif
934 } else {
935#ifdef CONFIG_FSL_USDHC
Fabio Estevamf132aab2021-06-07 17:40:09 -0300936 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
937 VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +0800938#else
939 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
940#endif
941 }
942
Ye Li9b7c3492021-08-17 17:09:20 +0800943#ifdef MMC_SUPPORTS_TUNING
Haibo Chen5d772192020-11-03 17:18:35 +0800944 /*
945 * For HS400/HS400ES mode, make sure set the strobe dll in the
946 * target clock rate. So call esdhc_set_strobe_dll() after the
947 * clock updated.
948 */
949 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
950 esdhc_set_strobe_dll(mmc);
Yangbo Lufa33d202019-06-21 11:42:27 +0800951
952 if (priv->signal_voltage != mmc->signal_voltage) {
953 ret = esdhc_set_voltage(mmc);
954 if (ret) {
Marek Vasut50a17a62020-05-22 18:28:33 +0200955 if (ret != -ENOTSUPP)
956 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lufa33d202019-06-21 11:42:27 +0800957 return ret;
958 }
959 }
960#endif
961
962 /* Set the bus width */
963 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
964
965 if (mmc->bus_width == 4)
966 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
967 else if (mmc->bus_width == 8)
968 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
969
970 return 0;
971}
972
973static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
974{
975 struct fsl_esdhc *regs = priv->esdhc_regs;
976 ulong start;
977
978 /* Reset the entire host controller */
979 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
980
981 /* Wait until the controller is available */
982 start = get_timer(0);
983 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
984 if (get_timer(start) > 1000)
985 return -ETIMEDOUT;
986 }
987
988#if defined(CONFIG_FSL_USDHC)
989 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
990 esdhc_write32(&regs->mmcboot, 0x0);
991 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
992 esdhc_write32(&regs->mixctrl, 0x0);
993 esdhc_write32(&regs->clktunectrlstatus, 0x0);
994
995 /* Put VEND_SPEC to default value */
996 if (priv->vs18_enable)
997 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
998 ESDHC_VENDORSPEC_VSELECT));
999 else
1000 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1001
1002 /* Disable DLL_CTRL delay line */
1003 esdhc_write32(&regs->dllctrl, 0x0);
1004#endif
1005
1006#ifndef ARCH_MXC
1007 /* Enable cache snooping */
1008 esdhc_write32(&regs->scr, 0x00000040);
1009#endif
1010
1011#ifndef CONFIG_FSL_USDHC
1012 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1013#else
Fabio Estevamf132aab2021-06-07 17:40:09 -03001014 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Yangbo Lufa33d202019-06-21 11:42:27 +08001015#endif
1016
1017 /* Set the initial clock speed */
1018 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1019
1020 /* Disable the BRR and BWR bits in IRQSTAT */
1021 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1022
1023#ifdef CONFIG_MCF5441x
1024 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1025#else
1026 /* Put the PROCTL reg back to the default */
1027 esdhc_write32(&regs->proctl, PROCTL_INIT);
1028#endif
1029
1030 /* Set timout to the maximum value */
1031 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1032
1033 return 0;
1034}
1035
1036static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1037{
1038 struct fsl_esdhc *regs = priv->esdhc_regs;
1039 int timeout = 1000;
1040
1041#ifdef CONFIG_ESDHC_DETECT_QUIRK
1042 if (CONFIG_ESDHC_DETECT_QUIRK)
1043 return 1;
1044#endif
1045
1046#if CONFIG_IS_ENABLED(DM_MMC)
Fabio Estevam29230f32020-01-06 20:11:27 -03001047 if (priv->broken_cd)
1048 return 1;
Simon Glassbcee8d62019-12-06 21:41:35 -07001049#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001050 if (dm_gpio_is_valid(&priv->cd_gpio))
1051 return dm_gpio_get_value(&priv->cd_gpio);
1052#endif
1053#endif
1054
1055 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1056 udelay(1000);
1057
1058 return timeout > 0;
1059}
1060
1061static int esdhc_reset(struct fsl_esdhc *regs)
1062{
1063 ulong start;
1064
1065 /* reset the controller */
1066 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1067
1068 /* hardware clears the bit when it is done */
1069 start = get_timer(0);
1070 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1071 if (get_timer(start) > 100) {
1072 printf("MMC/SD: Reset never completed.\n");
1073 return -ETIMEDOUT;
1074 }
1075 }
1076
1077 return 0;
1078}
1079
1080#if !CONFIG_IS_ENABLED(DM_MMC)
1081static int esdhc_getcd(struct mmc *mmc)
1082{
1083 struct fsl_esdhc_priv *priv = mmc->priv;
1084
1085 return esdhc_getcd_common(priv);
1086}
1087
1088static int esdhc_init(struct mmc *mmc)
1089{
1090 struct fsl_esdhc_priv *priv = mmc->priv;
1091
1092 return esdhc_init_common(priv, mmc);
1093}
1094
1095static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1096 struct mmc_data *data)
1097{
1098 struct fsl_esdhc_priv *priv = mmc->priv;
1099
1100 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1101}
1102
1103static int esdhc_set_ios(struct mmc *mmc)
1104{
1105 struct fsl_esdhc_priv *priv = mmc->priv;
1106
1107 return esdhc_set_ios_common(priv, mmc);
1108}
1109
1110static const struct mmc_ops esdhc_ops = {
1111 .getcd = esdhc_getcd,
1112 .init = esdhc_init,
1113 .send_cmd = esdhc_send_cmd,
1114 .set_ios = esdhc_set_ios,
1115};
1116#endif
1117
1118static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1119 struct fsl_esdhc_plat *plat)
1120{
1121 struct mmc_config *cfg;
1122 struct fsl_esdhc *regs;
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001123 u32 caps;
Yangbo Lufa33d202019-06-21 11:42:27 +08001124 int ret;
1125
1126 if (!priv)
1127 return -EINVAL;
1128
1129 regs = priv->esdhc_regs;
1130
1131 /* First reset the eSDHC controller */
1132 ret = esdhc_reset(regs);
1133 if (ret)
1134 return ret;
1135
1136#ifdef CONFIG_MCF5441x
1137 /* ColdFire, using SDHC_DATA[3] for card detection */
1138 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1139#endif
1140
1141#ifndef CONFIG_FSL_USDHC
1142 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1143 | SYSCTL_IPGEN | SYSCTL_CKEN);
1144 /* Clearing tuning bits in case ROM has set it already */
1145 esdhc_write32(&regs->mixctrl, 0);
1146 esdhc_write32(&regs->autoc12err, 0);
1147 esdhc_write32(&regs->clktunectrlstatus, 0);
1148#else
Fabio Estevamf132aab2021-06-07 17:40:09 -03001149 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1150 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Yangbo Lufa33d202019-06-21 11:42:27 +08001151#endif
1152
1153 if (priv->vs18_enable)
1154 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1155
Haibo Chenc7f44182020-09-30 15:52:23 +08001156 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lufa33d202019-06-21 11:42:27 +08001157 cfg = &plat->cfg;
1158#ifndef CONFIG_DM_MMC
1159 memset(cfg, '\0', sizeof(*cfg));
1160#endif
1161
Yangbo Lufa33d202019-06-21 11:42:27 +08001162 caps = esdhc_read32(&regs->hostcapblt);
Yangbo Lufa33d202019-06-21 11:42:27 +08001163#ifdef CONFIG_MCF5441x
1164 /*
1165 * MCF5441x RM declares in more points that sdhc clock speed must
1166 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1167 * from host capabilities.
1168 */
1169 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1170#endif
1171
1172#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001173 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Yangbo Lufa33d202019-06-21 11:42:27 +08001174#endif
1175
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001176#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1177 caps |= HOSTCAPBLT_VS33;
1178#endif
1179
1180 if (caps & HOSTCAPBLT_VS18)
1181 cfg->voltages |= MMC_VDD_165_195;
1182 if (caps & HOSTCAPBLT_VS30)
1183 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
1184 if (caps & HOSTCAPBLT_VS33)
1185 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Yangbo Lufa33d202019-06-21 11:42:27 +08001186
1187 cfg->name = "FSL_SDHC";
1188#if !CONFIG_IS_ENABLED(DM_MMC)
1189 cfg->ops = &esdhc_ops;
1190#endif
Yangbo Lufa33d202019-06-21 11:42:27 +08001191#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1192 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1193#endif
1194
Sean Anderson2fd7d1f2021-11-23 15:03:38 -05001195 if (caps & HOSTCAPBLT_HSS)
Yangbo Lufa33d202019-06-21 11:42:27 +08001196 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1197
Yangbo Lufa33d202019-06-21 11:42:27 +08001198 cfg->host_caps |= priv->caps;
1199
1200 cfg->f_min = 400000;
1201 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1202
1203 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1204
Haibo Chenc7f44182020-09-30 15:52:23 +08001205 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +08001206 if (priv->flags & ESDHC_FLAG_USDHC) {
1207 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +08001208 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +08001209
1210 val |= ESDHC_STD_TUNING_EN;
1211 val &= ~ESDHC_TUNING_START_TAP_MASK;
1212 val |= priv->tuning_start_tap;
1213 val &= ~ESDHC_TUNING_STEP_MASK;
1214 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chenba616762020-06-22 19:38:04 +08001215
1216 /* Disable the CMD CRC check for tuning, if not, need to
1217 * add some delay after every tuning command, because
1218 * hardware standard tuning logic will directly go to next
1219 * step once it detect the CMD CRC error, will not wait for
1220 * the card side to finally send out the tuning data, trigger
1221 * the buffer read ready interrupt immediately. If usdhc send
1222 * the next tuning command some eMMC card will stuck, can't
1223 * response, block the tuning procedure or the first command
1224 * after the whole tuning procedure always can't get any response.
1225 */
1226 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chenc7f44182020-09-30 15:52:23 +08001227 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +08001228 }
1229 }
1230
1231 return 0;
1232}
1233
1234#if !CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001235int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lufa33d202019-06-21 11:42:27 +08001236{
1237 struct fsl_esdhc_plat *plat;
1238 struct fsl_esdhc_priv *priv;
Sean Anderson95d6b742021-11-23 15:03:39 -05001239 struct mmc_config *mmc_cfg;
Yangbo Lufa33d202019-06-21 11:42:27 +08001240 struct mmc *mmc;
1241 int ret;
1242
1243 if (!cfg)
1244 return -EINVAL;
1245
1246 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1247 if (!priv)
1248 return -ENOMEM;
1249 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1250 if (!plat) {
1251 free(priv);
1252 return -ENOMEM;
1253 }
1254
Sean Anderson95d6b742021-11-23 15:03:39 -05001255 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1256 priv->sdhc_clk = cfg->sdhc_clk;
1257 priv->wp_enable = cfg->wp_enable;
1258
1259 mmc_cfg = &plat->cfg;
1260
1261 switch (cfg->max_bus_width) {
1262 case 0: /* Not set in config; assume everything is supported */
1263 case 8:
1264 mmc_cfg->host_caps |= MMC_MODE_8BIT;
1265 fallthrough;
1266 case 4:
1267 mmc_cfg->host_caps |= MMC_MODE_4BIT;
1268 fallthrough;
1269 case 1:
1270 mmc_cfg->host_caps |= MMC_MODE_1BIT;
1271 break;
1272 default:
1273 printf("invalid max bus width %u\n", cfg->max_bus_width);
1274 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001275 }
1276
Sean Anderson95d6b742021-11-23 15:03:39 -05001277#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1278 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1279 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
1280#endif
1281
Yangbo Lufa33d202019-06-21 11:42:27 +08001282 ret = fsl_esdhc_init(priv, plat);
1283 if (ret) {
1284 debug("%s init failure\n", __func__);
1285 free(plat);
1286 free(priv);
1287 return ret;
1288 }
1289
1290 mmc = mmc_create(&plat->cfg, priv);
1291 if (!mmc)
1292 return -EIO;
1293
1294 priv->mmc = mmc;
1295
1296 return 0;
1297}
1298
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001299int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lufa33d202019-06-21 11:42:27 +08001300{
1301 struct fsl_esdhc_cfg *cfg;
1302
1303 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1304 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1305 cfg->sdhc_clk = gd->arch.sdhc_clk;
1306 return fsl_esdhc_initialize(bis, cfg);
1307}
1308#endif
1309
Yangbo Lufa33d202019-06-21 11:42:27 +08001310#ifdef CONFIG_OF_LIBFDT
1311__weak int esdhc_status_fixup(void *blob, const char *compat)
1312{
1313#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1314 if (!hwconfig("esdhc")) {
1315 do_fixup_by_compat(blob, compat, "status", "disabled",
1316 sizeof("disabled"), 1);
1317 return 1;
1318 }
1319#endif
1320 return 0;
1321}
1322
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001323void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lufa33d202019-06-21 11:42:27 +08001324{
1325 const char *compat = "fsl,esdhc";
1326
1327 if (esdhc_status_fixup(blob, compat))
1328 return;
1329
Yangbo Lufa33d202019-06-21 11:42:27 +08001330 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1331 gd->arch.sdhc_clk, 1);
Yangbo Lufa33d202019-06-21 11:42:27 +08001332}
1333#endif
1334
1335#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +08001336#include <asm/arch/clock.h>
Yangbo Lufa33d202019-06-21 11:42:27 +08001337__weak void init_clk_usdhc(u32 index)
1338{
1339}
1340
Simon Glassd1998a92020-12-03 16:55:21 -07001341static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lufa33d202019-06-21 11:42:27 +08001342{
Yangbo Lufa33d202019-06-21 11:42:27 +08001343 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001344#if CONFIG_IS_ENABLED(DM_REGULATOR)
1345 struct udevice *vqmmc_dev;
Walter Lozano23721772020-07-29 12:31:17 -03001346 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001347#endif
Walter Lozano23721772020-07-29 12:31:17 -03001348 const void *fdt = gd->fdt_blob;
1349 int node = dev_of_offset(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001350 fdt_addr_t addr;
1351 unsigned int val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001352
Simon Glassdcfc42b2021-08-07 07:24:06 -06001353 if (!CONFIG_IS_ENABLED(OF_REAL))
1354 return 0;
1355
Yangbo Lufa33d202019-06-21 11:42:27 +08001356 addr = dev_read_addr(dev);
1357 if (addr == FDT_ADDR_T_NONE)
1358 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001359 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lufa33d202019-06-21 11:42:27 +08001360 priv->dev = dev;
1361 priv->mode = -1;
Yangbo Lufa33d202019-06-21 11:42:27 +08001362
Yangbo Lufa33d202019-06-21 11:42:27 +08001363 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1364 priv->tuning_step = val;
1365 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1366 ESDHC_TUNING_START_TAP_DEFAULT);
1367 priv->tuning_start_tap = val;
1368 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1369 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1370 priv->strobe_dll_delay_target = val;
Haibo Chen8974ff12021-03-22 18:55:38 +08001371 val = fdtdec_get_int(fdt, node, "fsl,signal-voltage-switch-extra-delay-ms", 0);
1372 priv->signal_voltage_switch_extra_delay_ms = val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001373
Fabio Estevam29230f32020-01-06 20:11:27 -03001374 if (dev_read_bool(dev, "broken-cd"))
1375 priv->broken_cd = 1;
1376
Yangbo Lufa33d202019-06-21 11:42:27 +08001377 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1378 priv->wp_enable = 1;
1379 } else {
1380 priv->wp_enable = 0;
Yangbo Lufa33d202019-06-21 11:42:27 +08001381 }
1382
Sean Andersond39aa732021-11-23 15:03:40 -05001383#if CONFIG_IS_ENABLED(DM_GPIO)
1384 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1385 GPIOD_IS_IN);
1386 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1387 GPIOD_IS_IN);
1388#endif
1389
Yangbo Lufa33d202019-06-21 11:42:27 +08001390 priv->vs18_enable = 0;
1391
1392#if CONFIG_IS_ENABLED(DM_REGULATOR)
1393 /*
1394 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1395 * otherwise, emmc will work abnormally.
1396 */
1397 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1398 if (ret) {
1399 dev_dbg(dev, "no vqmmc-supply\n");
1400 } else {
Marek Vasut406df852020-05-22 18:19:08 +02001401 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001402 ret = regulator_set_enable(vqmmc_dev, true);
1403 if (ret) {
1404 dev_err(dev, "fail to enable vqmmc-supply\n");
1405 return ret;
1406 }
1407
1408 if (regulator_get_value(vqmmc_dev) == 1800000)
1409 priv->vs18_enable = 1;
1410 }
1411#endif
Simon Glassdcfc42b2021-08-07 07:24:06 -06001412
Walter Lozano23721772020-07-29 12:31:17 -03001413 return 0;
1414}
1415
1416static int fsl_esdhc_probe(struct udevice *dev)
1417{
1418 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001419 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano23721772020-07-29 12:31:17 -03001420 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1421 struct esdhc_soc_data *data =
1422 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1423 struct mmc *mmc;
Walter Lozano23721772020-07-29 12:31:17 -03001424 int ret;
1425
1426#if CONFIG_IS_ENABLED(OF_PLATDATA)
1427 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
Walter Lozano23721772020-07-29 12:31:17 -03001428
1429 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
Walter Lozano7142ff92020-07-29 12:31:19 -03001430
1431 if (dtplat->non_removable)
Sean Andersond39aa732021-11-23 15:03:40 -05001432 plat->cfg.host_caps |= MMC_CAP_NONREMOVABLE;
Walter Lozano7142ff92020-07-29 12:31:19 -03001433 else
Sean Andersond39aa732021-11-23 15:03:40 -05001434 plat->cfg.host_caps &= ~MMC_CAP_NONREMOVABLE;
Walter Lozano7142ff92020-07-29 12:31:19 -03001435
Sean Andersond39aa732021-11-23 15:03:40 -05001436 if (CONFIG_IS_ENABLED(DM_GPIO) && !dtplat->non_removable) {
Walter Lozano7142ff92020-07-29 12:31:19 -03001437 struct udevice *gpiodev;
Walter Lozano7142ff92020-07-29 12:31:19 -03001438
Simon Glasscc469b72021-03-15 17:25:28 +13001439 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
Walter Lozano7142ff92020-07-29 12:31:19 -03001440 if (ret)
1441 return ret;
1442
1443 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1444 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1445 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1446
1447 if (ret)
1448 return ret;
1449 }
Walter Lozano23721772020-07-29 12:31:17 -03001450#endif
1451
1452 if (data)
1453 priv->flags = data->flags;
Yangbo Lufa33d202019-06-21 11:42:27 +08001454
Yangbo Lufa33d202019-06-21 11:42:27 +08001455 /*
1456 * TODO:
1457 * Because lack of clk driver, if SDHC clk is not enabled,
1458 * need to enable it first before this driver is invoked.
1459 *
1460 * we use MXC_ESDHC_CLK to get clk freq.
1461 * If one would like to make this function work,
1462 * the aliases should be provided in dts as this:
1463 *
1464 * aliases {
1465 * mmc0 = &usdhc1;
1466 * mmc1 = &usdhc2;
1467 * mmc2 = &usdhc3;
1468 * mmc3 = &usdhc4;
1469 * };
1470 * Then if your board only supports mmc2 and mmc3, but we can
1471 * correctly get the seq as 2 and 3, then let mxc_get_clock
1472 * work as expected.
1473 */
1474
Simon Glass8b85dfc2020-12-16 21:20:07 -07001475 init_clk_usdhc(dev_seq(dev));
Yangbo Lufa33d202019-06-21 11:42:27 +08001476
Giulio Benettia820bed2020-01-10 15:51:45 +01001477#if CONFIG_IS_ENABLED(CLK)
1478 /* Assigned clock already set clock */
1479 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1480 if (ret) {
1481 printf("Failed to get per_clk\n");
1482 return ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001483 }
Giulio Benettia820bed2020-01-10 15:51:45 +01001484 ret = clk_enable(&priv->per_clk);
1485 if (ret) {
1486 printf("Failed to enable per_clk\n");
1487 return ret;
1488 }
1489
1490 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1491#else
Simon Glass8b85dfc2020-12-16 21:20:07 -07001492 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettia820bed2020-01-10 15:51:45 +01001493 if (priv->sdhc_clk <= 0) {
1494 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1495 return -EINVAL;
1496 }
1497#endif
Yangbo Lufa33d202019-06-21 11:42:27 +08001498
1499 ret = fsl_esdhc_init(priv, plat);
1500 if (ret) {
1501 dev_err(dev, "fsl_esdhc_init failure\n");
1502 return ret;
1503 }
1504
Simon Glassdcfc42b2021-08-07 07:24:06 -06001505 if (CONFIG_IS_ENABLED(OF_REAL)) {
1506 ret = mmc_of_parse(dev, &plat->cfg);
1507 if (ret)
1508 return ret;
1509 }
Peng Fanb0155ac2019-07-10 09:35:24 +00001510
Yangbo Lufa33d202019-06-21 11:42:27 +08001511 mmc = &plat->mmc;
1512 mmc->cfg = &plat->cfg;
1513 mmc->dev = dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001514
1515 upriv->mmc = mmc;
1516
1517 return esdhc_init_common(priv, mmc);
1518}
1519
Yangbo Lufa33d202019-06-21 11:42:27 +08001520static int fsl_esdhc_get_cd(struct udevice *dev)
1521{
Sean Andersond39aa732021-11-23 15:03:40 -05001522 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001523 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1524
Sean Andersond39aa732021-11-23 15:03:40 -05001525 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1526 return 1;
1527
Yangbo Lufa33d202019-06-21 11:42:27 +08001528 return esdhc_getcd_common(priv);
1529}
1530
1531static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1532 struct mmc_data *data)
1533{
Simon Glassc69cda22020-12-03 16:55:20 -07001534 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001535 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1536
1537 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1538}
1539
1540static int fsl_esdhc_set_ios(struct udevice *dev)
1541{
Simon Glassc69cda22020-12-03 16:55:20 -07001542 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001543 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1544
1545 return esdhc_set_ios_common(priv, &plat->mmc);
1546}
1547
Peng Fane9c22552019-07-10 09:35:26 +00001548#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1549static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1550{
1551 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1552 struct fsl_esdhc *regs = priv->esdhc_regs;
1553 u32 m;
1554
Haibo Chenc7f44182020-09-30 15:52:23 +08001555 m = esdhc_read32(&regs->mixctrl);
Peng Fane9c22552019-07-10 09:35:26 +00001556 m |= MIX_CTRL_HS400_ES;
Haibo Chenc7f44182020-09-30 15:52:23 +08001557 esdhc_write32(&regs->mixctrl, m);
Peng Fane9c22552019-07-10 09:35:26 +00001558
1559 return 0;
1560}
1561#endif
1562
Haibo Chenb5874b52020-11-05 14:57:13 +08001563static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1564 int timeout_us)
1565{
1566 int ret;
1567 u32 tmp;
1568 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1569 struct fsl_esdhc *regs = priv->esdhc_regs;
1570
1571 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1572 !!(tmp & PRSSTAT_DAT0) == !!state,
1573 timeout_us);
1574 return ret;
1575}
1576
Yangbo Lufa33d202019-06-21 11:42:27 +08001577static const struct dm_mmc_ops fsl_esdhc_ops = {
1578 .get_cd = fsl_esdhc_get_cd,
1579 .send_cmd = fsl_esdhc_send_cmd,
1580 .set_ios = fsl_esdhc_set_ios,
1581#ifdef MMC_SUPPORTS_TUNING
1582 .execute_tuning = fsl_esdhc_execute_tuning,
1583#endif
Peng Fane9c22552019-07-10 09:35:26 +00001584#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1585 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1586#endif
Haibo Chenb5874b52020-11-05 14:57:13 +08001587 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lufa33d202019-06-21 11:42:27 +08001588};
Yangbo Lufa33d202019-06-21 11:42:27 +08001589
1590static struct esdhc_soc_data usdhc_imx7d_data = {
1591 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1592 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1593 | ESDHC_FLAG_HS400,
Yangbo Lufa33d202019-06-21 11:42:27 +08001594};
1595
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001596static struct esdhc_soc_data usdhc_imx7ulp_data = {
1597 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
Oleksandr Suvorovfa0223a2021-09-08 21:56:43 +03001598 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1599 | ESDHC_FLAG_HS400,
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001600};
1601
Peng Fan609ba122019-07-10 09:35:28 +00001602static struct esdhc_soc_data usdhc_imx8qm_data = {
1603 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1604 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1605 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1606};
1607
Yangbo Lufa33d202019-06-21 11:42:27 +08001608static const struct udevice_id fsl_esdhc_ids[] = {
Fabio Estevamc3e6f992021-02-15 08:58:15 -03001609 { .compatible = "fsl,imx51-esdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001610 { .compatible = "fsl,imx53-esdhc", },
1611 { .compatible = "fsl,imx6ul-usdhc", },
1612 { .compatible = "fsl,imx6sx-usdhc", },
1613 { .compatible = "fsl,imx6sl-usdhc", },
1614 { .compatible = "fsl,imx6q-usdhc", },
1615 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Jorge Ramirez-Ortizc1412cb2021-09-08 21:56:42 +03001616 { .compatible = "fsl,imx7ulp-usdhc", .data = (ulong)&usdhc_imx7ulp_data,},
Peng Fan609ba122019-07-10 09:35:28 +00001617 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fanf65d0842019-11-04 17:31:17 +08001618 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1619 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1620 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti6a63a872020-01-10 15:51:46 +01001621 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001622 { .compatible = "fsl,esdhc", },
1623 { /* sentinel */ }
1624};
1625
Yangbo Lufa33d202019-06-21 11:42:27 +08001626static int fsl_esdhc_bind(struct udevice *dev)
1627{
Simon Glassc69cda22020-12-03 16:55:20 -07001628 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001629
1630 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1631}
Yangbo Lufa33d202019-06-21 11:42:27 +08001632
1633U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano45154f02020-07-29 12:31:16 -03001634 .name = "fsl_esdhc",
Yangbo Lufa33d202019-06-21 11:42:27 +08001635 .id = UCLASS_MMC,
1636 .of_match = fsl_esdhc_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07001637 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lufa33d202019-06-21 11:42:27 +08001638 .ops = &fsl_esdhc_ops,
Yangbo Lufa33d202019-06-21 11:42:27 +08001639 .bind = fsl_esdhc_bind,
Yangbo Lufa33d202019-06-21 11:42:27 +08001640 .probe = fsl_esdhc_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -07001641 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass41575d82020-12-03 16:55:17 -07001642 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lufa33d202019-06-21 11:42:27 +08001643};
Walter Lozano23721772020-07-29 12:31:17 -03001644
Simon Glassbdf8fd72020-12-28 20:34:57 -07001645DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lufa33d202019-06-21 11:42:27 +08001646#endif