blob: e0e132698e30a3ee436afb934e40a0f3a802212a [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
4 * Copyright 2019 NXP Semiconductors
5 * Andy Fleming
6 * Yangbo Lu <yangbo.lu@nxp.com>
7 *
8 * Based vaguely on the pxa mmc code:
9 * (C) Copyright 2003
10 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
11 */
12
13#include <config.h>
14#include <common.h>
15#include <command.h>
16#include <clk.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070017#include <cpu_func.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080018#include <errno.h>
19#include <hwconfig.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080021#include <mmc.h>
22#include <part.h>
Simon Glass90526e92020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glass61b29b82020-02-03 07:36:15 -070028#include <linux/err.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080029#include <power/regulator.h>
30#include <malloc.h>
31#include <fsl_esdhc_imx.h>
32#include <fdt_support.h>
33#include <asm/io.h>
34#include <dm.h>
35#include <asm-generic/gpio.h>
36#include <dm/pinctrl.h>
Walter Lozano23721772020-07-29 12:31:17 -030037#include <dt-structs.h>
38#include <mapmem.h>
39#include <dm/ofnode.h>
Haibo Chenf9c3a812020-09-01 15:34:06 +080040#include <linux/iopoll.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080041
42#if !CONFIG_IS_ENABLED(BLK)
43#include "mmc_private.h"
44#endif
45
46DECLARE_GLOBAL_DATA_PTR;
47
48#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
49 IRQSTATEN_CINT | \
50 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
51 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
52 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
53 IRQSTATEN_DINT)
54#define MAX_TUNING_LOOP 40
55#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
56
57struct fsl_esdhc {
58 uint dsaddr; /* SDMA system address register */
59 uint blkattr; /* Block attributes register */
60 uint cmdarg; /* Command argument register */
61 uint xfertyp; /* Transfer type register */
62 uint cmdrsp0; /* Command response 0 register */
63 uint cmdrsp1; /* Command response 1 register */
64 uint cmdrsp2; /* Command response 2 register */
65 uint cmdrsp3; /* Command response 3 register */
66 uint datport; /* Buffer data port register */
67 uint prsstat; /* Present state register */
68 uint proctl; /* Protocol control register */
69 uint sysctl; /* System Control Register */
70 uint irqstat; /* Interrupt status register */
71 uint irqstaten; /* Interrupt status enable register */
72 uint irqsigen; /* Interrupt signal enable register */
73 uint autoc12err; /* Auto CMD error status register */
74 uint hostcapblt; /* Host controller capabilities register */
75 uint wml; /* Watermark level register */
76 uint mixctrl; /* For USDHC */
77 char reserved1[4]; /* reserved */
78 uint fevt; /* Force event register */
79 uint admaes; /* ADMA error status register */
80 uint adsaddr; /* ADMA system address register */
81 char reserved2[4];
82 uint dllctrl;
83 uint dllstat;
84 uint clktunectrlstatus;
85 char reserved3[4];
86 uint strobe_dllctrl;
87 uint strobe_dllstat;
88 char reserved4[72];
89 uint vendorspec;
90 uint mmcboot;
91 uint vendorspec2;
Giulio Benetti6a63a872020-01-10 15:51:46 +010092 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lufa33d202019-06-21 11:42:27 +080093 char reserved5[44];
94 uint hostver; /* Host controller version register */
95 char reserved6[4]; /* reserved */
96 uint dmaerraddr; /* DMA error address register */
97 char reserved7[4]; /* reserved */
98 uint dmaerrattr; /* DMA error attribute register */
99 char reserved8[4]; /* reserved */
100 uint hostcapblt2; /* Host controller capabilities register 2 */
101 char reserved9[8]; /* reserved */
102 uint tcr; /* Tuning control register */
103 char reserved10[28]; /* reserved */
104 uint sddirctl; /* SD direction control register */
105 char reserved11[712];/* reserved */
106 uint scr; /* eSDHC control register */
107};
108
109struct fsl_esdhc_plat {
Walter Lozano23721772020-07-29 12:31:17 -0300110#if CONFIG_IS_ENABLED(OF_PLATDATA)
111 /* Put this first since driver model will copy the data here */
112 struct dtd_fsl_esdhc dtplat;
113#endif
114
Yangbo Lufa33d202019-06-21 11:42:27 +0800115 struct mmc_config cfg;
116 struct mmc mmc;
117};
118
119struct esdhc_soc_data {
120 u32 flags;
Yangbo Lufa33d202019-06-21 11:42:27 +0800121};
122
123/**
124 * struct fsl_esdhc_priv
125 *
126 * @esdhc_regs: registers of the sdhc controller
127 * @sdhc_clk: Current clk of the sdhc controller
128 * @bus_width: bus width, 1bit, 4bit or 8bit
129 * @cfg: mmc config
130 * @mmc: mmc
131 * Following is used when Driver Model is enabled for MMC
132 * @dev: pointer for the device
133 * @non_removable: 0: removable; 1: non-removable
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
143 * @cd_gpio: gpio for card detection
144 * @wp_gpio: gpio for write protection
145 */
146struct fsl_esdhc_priv {
147 struct fsl_esdhc *esdhc_regs;
148 unsigned int sdhc_clk;
149 struct clk per_clk;
150 unsigned int clock;
151 unsigned int mode;
152 unsigned int bus_width;
153#if !CONFIG_IS_ENABLED(BLK)
154 struct mmc *mmc;
155#endif
156 struct udevice *dev;
157 int non_removable;
Fabio Estevam29230f32020-01-06 20:11:27 -0300158 int broken_cd;
Yangbo Lufa33d202019-06-21 11:42:27 +0800159 int wp_enable;
160 int vs18_enable;
161 u32 flags;
162 u32 caps;
163 u32 tuning_step;
164 u32 tuning_start_tap;
165 u32 strobe_dll_delay_target;
166 u32 signal_voltage;
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
175};
176
177/* Return the XFERTYP flags for a given command and data packet */
178static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
179{
180 uint xfertyp = 0;
181
182 if (data) {
183 xfertyp |= XFERTYP_DPSEL;
184#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
185 xfertyp |= XFERTYP_DMAEN;
186#endif
187 if (data->blocks > 1) {
188 xfertyp |= XFERTYP_MSBSEL;
189 xfertyp |= XFERTYP_BCEN;
190#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
191 xfertyp |= XFERTYP_AC12EN;
192#endif
193 }
194
195 if (data->flags & MMC_DATA_READ)
196 xfertyp |= XFERTYP_DTDSEL;
197 }
198
199 if (cmd->resp_type & MMC_RSP_CRC)
200 xfertyp |= XFERTYP_CCCEN;
201 if (cmd->resp_type & MMC_RSP_OPCODE)
202 xfertyp |= XFERTYP_CICEN;
203 if (cmd->resp_type & MMC_RSP_136)
204 xfertyp |= XFERTYP_RSPTYP_136;
205 else if (cmd->resp_type & MMC_RSP_BUSY)
206 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
207 else if (cmd->resp_type & MMC_RSP_PRESENT)
208 xfertyp |= XFERTYP_RSPTYP_48;
209
210 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
211 xfertyp |= XFERTYP_CMDTYP_ABORT;
212
213 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
214}
215
216#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
217/*
218 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
219 */
220static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
221 struct mmc_data *data)
222{
223 struct fsl_esdhc *regs = priv->esdhc_regs;
224 uint blocks;
225 char *buffer;
226 uint databuf;
227 uint size;
228 uint irqstat;
229 ulong start;
230
231 if (data->flags & MMC_DATA_READ) {
232 blocks = data->blocks;
233 buffer = data->dest;
234 while (blocks) {
235 start = get_timer(0);
236 size = data->blocksize;
237 irqstat = esdhc_read32(&regs->irqstat);
238 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
239 if (get_timer(start) > PIO_TIMEOUT) {
240 printf("\nData Read Failed in PIO Mode.");
241 return;
242 }
243 }
244 while (size && (!(irqstat & IRQSTAT_TC))) {
245 udelay(100); /* Wait before last byte transfer complete */
246 irqstat = esdhc_read32(&regs->irqstat);
247 databuf = in_le32(&regs->datport);
248 *((uint *)buffer) = databuf;
249 buffer += 4;
250 size -= 4;
251 }
252 blocks--;
253 }
254 } else {
255 blocks = data->blocks;
256 buffer = (char *)data->src;
257 while (blocks) {
258 start = get_timer(0);
259 size = data->blocksize;
260 irqstat = esdhc_read32(&regs->irqstat);
261 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
262 if (get_timer(start) > PIO_TIMEOUT) {
263 printf("\nData Write Failed in PIO Mode.");
264 return;
265 }
266 }
267 while (size && (!(irqstat & IRQSTAT_TC))) {
268 udelay(100); /* Wait before last byte transfer complete */
269 databuf = *((uint *)buffer);
270 buffer += 4;
271 size -= 4;
272 irqstat = esdhc_read32(&regs->irqstat);
273 out_le32(&regs->datport, databuf);
274 }
275 blocks--;
276 }
277 }
278}
279#endif
280
281static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
282 struct mmc_data *data)
283{
284 int timeout;
285 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu5053da22019-06-21 11:42:30 +0800286#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800287 dma_addr_t addr;
288#endif
289 uint wml_value;
290
291 wml_value = data->blocksize/4;
292
293 if (data->flags & MMC_DATA_READ) {
294 if (wml_value > WML_RD_WML_MAX)
295 wml_value = WML_RD_WML_MAX_VAL;
296
297 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
298#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu5053da22019-06-21 11:42:30 +0800299#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800300 addr = virt_to_phys((void *)(data->dest));
301 if (upper_32_bits(addr))
302 printf("Error found for upper 32 bits\n");
303 else
304 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
305#else
306 esdhc_write32(&regs->dsaddr, (u32)data->dest);
307#endif
308#endif
309 } else {
310#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
311 flush_dcache_range((ulong)data->src,
312 (ulong)data->src+data->blocks
313 *data->blocksize);
314#endif
315 if (wml_value > WML_WR_WML_MAX)
316 wml_value = WML_WR_WML_MAX_VAL;
317 if (priv->wp_enable) {
318 if ((esdhc_read32(&regs->prsstat) &
319 PRSSTAT_WPSPL) == 0) {
320 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
321 return -ETIMEDOUT;
322 }
323 } else {
Simon Glassbcee8d62019-12-06 21:41:35 -0700324#if CONFIG_IS_ENABLED(DM_GPIO)
325 if (dm_gpio_is_valid(&priv->wp_gpio) &&
326 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800327 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
328 return -ETIMEDOUT;
329 }
330#endif
331 }
332
333 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
334 wml_value << 16);
335#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu5053da22019-06-21 11:42:30 +0800336#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800337 addr = virt_to_phys((void *)(data->src));
338 if (upper_32_bits(addr))
339 printf("Error found for upper 32 bits\n");
340 else
341 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
342#else
343 esdhc_write32(&regs->dsaddr, (u32)data->src);
344#endif
345#endif
346 }
347
348 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
349
350 /* Calculate the timeout period for data transactions */
351 /*
352 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
353 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
354 * So, Number of SD Clock cycles for 0.25sec should be minimum
355 * (SD Clock/sec * 0.25 sec) SD Clock cycles
356 * = (mmc->clock * 1/4) SD Clock cycles
357 * As 1) >= 2)
358 * => (2^(timeout+13)) >= mmc->clock * 1/4
359 * Taking log2 both the sides
360 * => timeout + 13 >= log2(mmc->clock/4)
361 * Rounding up to next power of 2
362 * => timeout + 13 = log2(mmc->clock/4) + 1
363 * => timeout + 13 = fls(mmc->clock/4)
364 *
365 * However, the MMC spec "It is strongly recommended for hosts to
366 * implement more than 500ms timeout value even if the card
367 * indicates the 250ms maximum busy length." Even the previous
368 * value of 300ms is known to be insufficient for some cards.
369 * So, we use
370 * => timeout + 13 = fls(mmc->clock/2)
371 */
372 timeout = fls(mmc->clock/2);
373 timeout -= 13;
374
375 if (timeout > 14)
376 timeout = 14;
377
378 if (timeout < 0)
379 timeout = 0;
380
381#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
382 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
383 timeout++;
384#endif
385
386#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
387 timeout = 0xE;
388#endif
389 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
390
391 return 0;
392}
393
394static void check_and_invalidate_dcache_range
395 (struct mmc_cmd *cmd,
396 struct mmc_data *data) {
397 unsigned start = 0;
398 unsigned end = 0;
399 unsigned size = roundup(ARCH_DMA_MINALIGN,
400 data->blocks*data->blocksize);
Yangbo Lu5053da22019-06-21 11:42:30 +0800401#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800402 dma_addr_t addr;
403
404 addr = virt_to_phys((void *)(data->dest));
405 if (upper_32_bits(addr))
406 printf("Error found for upper 32 bits\n");
407 else
408 start = lower_32_bits(addr);
409#else
410 start = (unsigned)data->dest;
411#endif
412 end = start + size;
413 invalidate_dcache_range(start, end);
414}
415
416#ifdef CONFIG_MCF5441x
417/*
418 * Swaps 32-bit words to little-endian byte order.
419 */
420static inline void sd_swap_dma_buff(struct mmc_data *data)
421{
422 int i, size = data->blocksize >> 2;
423 u32 *buffer = (u32 *)data->dest;
424 u32 sw;
425
426 while (data->blocks--) {
427 for (i = 0; i < size; i++) {
428 sw = __sw32(*buffer);
429 *buffer++ = sw;
430 }
431 }
432}
433#endif
434
435/*
436 * Sends a command out on the bus. Takes the mmc pointer,
437 * a command pointer, and an optional data pointer.
438 */
439static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
440 struct mmc_cmd *cmd, struct mmc_data *data)
441{
442 int err = 0;
443 uint xfertyp;
444 uint irqstat;
445 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
446 struct fsl_esdhc *regs = priv->esdhc_regs;
447 unsigned long start;
448
449#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
450 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
451 return 0;
452#endif
453
454 esdhc_write32(&regs->irqstat, -1);
455
456 sync();
457
458 /* Wait for the bus to be idle */
459 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
460 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
461 ;
462
463 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
464 ;
465
Yangbo Lufa33d202019-06-21 11:42:27 +0800466 /* Set up for a data transfer if we have one */
467 if (data) {
468 err = esdhc_setup_data(priv, mmc, data);
469 if(err)
470 return err;
471
472 if (data->flags & MMC_DATA_READ)
473 check_and_invalidate_dcache_range(cmd, data);
474 }
475
476 /* Figure out the transfer arguments */
477 xfertyp = esdhc_xfertyp(cmd, data);
478
479 /* Mask all irqs */
480 esdhc_write32(&regs->irqsigen, 0);
481
482 /* Send the command */
483 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
484#if defined(CONFIG_FSL_USDHC)
485 esdhc_write32(&regs->mixctrl,
486 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
487 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
488 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
489#else
490 esdhc_write32(&regs->xfertyp, xfertyp);
491#endif
492
493 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
494 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
495 flags = IRQSTAT_BRR;
496
497 /* Wait for the command to complete */
498 start = get_timer(0);
499 while (!(esdhc_read32(&regs->irqstat) & flags)) {
500 if (get_timer(start) > 1000) {
501 err = -ETIMEDOUT;
502 goto out;
503 }
504 }
505
506 irqstat = esdhc_read32(&regs->irqstat);
507
508 if (irqstat & CMD_ERR) {
509 err = -ECOMM;
510 goto out;
511 }
512
513 if (irqstat & IRQSTAT_CTOE) {
514 err = -ETIMEDOUT;
515 goto out;
516 }
517
518 /* Switch voltage to 1.8V if CMD11 succeeded */
519 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
520 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
521
522 printf("Run CMD11 1.8V switch\n");
523 /* Sleep for 5 ms - max time for card to switch to 1.8V */
524 udelay(5000);
525 }
526
527 /* Workaround for ESDHC errata ENGcm03648 */
528 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan356f7822019-07-10 09:35:30 +0000529 int timeout = 50000;
Yangbo Lufa33d202019-06-21 11:42:27 +0800530
Peng Fan356f7822019-07-10 09:35:30 +0000531 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lufa33d202019-06-21 11:42:27 +0800532 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
533 PRSSTAT_DAT0)) {
534 udelay(100);
535 timeout--;
536 }
537
538 if (timeout <= 0) {
539 printf("Timeout waiting for DAT0 to go high!\n");
540 err = -ETIMEDOUT;
541 goto out;
542 }
543 }
544
545 /* Copy the response to the response buffer */
546 if (cmd->resp_type & MMC_RSP_136) {
547 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
548
549 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
550 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
551 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
552 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
553 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
554 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
555 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
556 cmd->response[3] = (cmdrsp0 << 8);
557 } else
558 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
559
560 /* Wait until all of the blocks are transferred */
561 if (data) {
562#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
563 esdhc_pio_read_write(priv, data);
564#else
565 flags = DATA_COMPLETE;
566 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
567 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
568 flags = IRQSTAT_BRR;
569 }
570
571 do {
572 irqstat = esdhc_read32(&regs->irqstat);
573
574 if (irqstat & IRQSTAT_DTOE) {
575 err = -ETIMEDOUT;
576 goto out;
577 }
578
579 if (irqstat & DATA_ERR) {
580 err = -ECOMM;
581 goto out;
582 }
583 } while ((irqstat & flags) != flags);
584
585 /*
586 * Need invalidate the dcache here again to avoid any
587 * cache-fill during the DMA operations such as the
588 * speculative pre-fetching etc.
589 */
590 if (data->flags & MMC_DATA_READ) {
591 check_and_invalidate_dcache_range(cmd, data);
592#ifdef CONFIG_MCF5441x
593 sd_swap_dma_buff(data);
594#endif
595 }
596#endif
597 }
598
599out:
600 /* Reset CMD and DATA portions on error */
601 if (err) {
602 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
603 SYSCTL_RSTC);
604 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
605 ;
606
607 if (data) {
608 esdhc_write32(&regs->sysctl,
609 esdhc_read32(&regs->sysctl) |
610 SYSCTL_RSTD);
611 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
612 ;
613 }
614
615 /* If this was CMD11, then notify that power cycle is needed */
616 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
617 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
618 }
619
620 esdhc_write32(&regs->irqstat, -1);
621
622 return err;
623}
624
625static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
626{
627 struct fsl_esdhc *regs = priv->esdhc_regs;
628 int div = 1;
Haibo Chenf9c3a812020-09-01 15:34:06 +0800629 u32 tmp;
630 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +0800631#ifdef ARCH_MXC
632#ifdef CONFIG_MX53
633 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
634 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
635#else
636 int pre_div = 1;
637#endif
638#else
639 int pre_div = 2;
640#endif
641 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
642 int sdhc_clk = priv->sdhc_clk;
643 uint clk;
644
Yangbo Lufa33d202019-06-21 11:42:27 +0800645 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
646 pre_div *= 2;
647
648 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
649 div++;
650
651 pre_div >>= 1;
652 div -= 1;
653
654 clk = (pre_div << 8) | (div << 4);
655
656#ifdef CONFIG_FSL_USDHC
657 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
658#else
659 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
660#endif
661
662 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
663
Haibo Chenf9c3a812020-09-01 15:34:06 +0800664 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
665 if (ret)
666 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lufa33d202019-06-21 11:42:27 +0800667
668#ifdef CONFIG_FSL_USDHC
669 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
670#else
671 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
672#endif
673
674 priv->clock = clock;
675}
676
Yangbo Lufa33d202019-06-21 11:42:27 +0800677#ifdef MMC_SUPPORTS_TUNING
678static int esdhc_change_pinstate(struct udevice *dev)
679{
680 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
681 int ret;
682
683 switch (priv->mode) {
684 case UHS_SDR50:
685 case UHS_DDR50:
686 ret = pinctrl_select_state(dev, "state_100mhz");
687 break;
688 case UHS_SDR104:
689 case MMC_HS_200:
690 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000691 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800692 ret = pinctrl_select_state(dev, "state_200mhz");
693 break;
694 default:
695 ret = pinctrl_select_state(dev, "default");
696 break;
697 }
698
699 if (ret)
700 printf("%s %d error\n", __func__, priv->mode);
701
702 return ret;
703}
704
705static void esdhc_reset_tuning(struct mmc *mmc)
706{
707 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
708 struct fsl_esdhc *regs = priv->esdhc_regs;
709
710 if (priv->flags & ESDHC_FLAG_USDHC) {
711 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
712 esdhc_clrbits32(&regs->autoc12err,
713 MIX_CTRL_SMPCLK_SEL |
714 MIX_CTRL_EXE_TUNE);
715 }
716 }
717}
718
719static void esdhc_set_strobe_dll(struct mmc *mmc)
720{
721 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
722 struct fsl_esdhc *regs = priv->esdhc_regs;
723 u32 val;
724
725 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800726 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Yangbo Lufa33d202019-06-21 11:42:27 +0800727
728 /*
729 * enable strobe dll ctrl and adjust the delay target
730 * for the uSDHC loopback read clock
731 */
732 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
733 (priv->strobe_dll_delay_target <<
734 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chenc7f44182020-09-30 15:52:23 +0800735 esdhc_write32(&regs->strobe_dllctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800736 /* wait 1us to make sure strobe dll status register stable */
737 mdelay(1);
Haibo Chenc7f44182020-09-30 15:52:23 +0800738 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lufa33d202019-06-21 11:42:27 +0800739 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
740 pr_warn("HS400 strobe DLL status REF not lock!\n");
741 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
742 pr_warn("HS400 strobe DLL status SLV not lock!\n");
743 }
744}
745
746static int esdhc_set_timing(struct mmc *mmc)
747{
748 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
749 struct fsl_esdhc *regs = priv->esdhc_regs;
750 u32 mixctrl;
751
Haibo Chenc7f44182020-09-30 15:52:23 +0800752 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800753 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
754
755 switch (mmc->selected_mode) {
756 case MMC_LEGACY:
Yangbo Lufa33d202019-06-21 11:42:27 +0800757 esdhc_reset_tuning(mmc);
Haibo Chenc7f44182020-09-30 15:52:23 +0800758 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800759 break;
760 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000761 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800762 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800763 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800764 break;
765 case MMC_HS:
766 case MMC_HS_52:
767 case MMC_HS_200:
768 case SD_HS:
769 case UHS_SDR12:
770 case UHS_SDR25:
771 case UHS_SDR50:
772 case UHS_SDR104:
Haibo Chenc7f44182020-09-30 15:52:23 +0800773 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800774 break;
775 case UHS_DDR50:
776 case MMC_DDR_52:
777 mixctrl |= MIX_CTRL_DDREN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800778 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800779 break;
780 default:
781 printf("Not supported %d\n", mmc->selected_mode);
782 return -EINVAL;
783 }
784
785 priv->mode = mmc->selected_mode;
786
787 return esdhc_change_pinstate(mmc->dev);
788}
789
790static int esdhc_set_voltage(struct mmc *mmc)
791{
792 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
793 struct fsl_esdhc *regs = priv->esdhc_regs;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100794#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800795 int ret;
Heiko Schocher50125bd2021-01-15 10:37:09 +0100796#endif
Yangbo Lufa33d202019-06-21 11:42:27 +0800797
798 priv->signal_voltage = mmc->signal_voltage;
799 switch (mmc->signal_voltage) {
800 case MMC_SIGNAL_VOLTAGE_330:
801 if (priv->vs18_enable)
Marek Vasut50a17a62020-05-22 18:28:33 +0200802 return -ENOTSUPP;
Yangbo Lufa33d202019-06-21 11:42:27 +0800803#if CONFIG_IS_ENABLED(DM_REGULATOR)
804 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
805 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
806 if (ret) {
807 printf("Setting to 3.3V error");
808 return -EIO;
809 }
810 /* Wait for 5ms */
811 mdelay(5);
812 }
813#endif
814
815 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
816 if (!(esdhc_read32(&regs->vendorspec) &
817 ESDHC_VENDORSPEC_VSELECT))
818 return 0;
819
820 return -EAGAIN;
821 case MMC_SIGNAL_VOLTAGE_180:
822#if CONFIG_IS_ENABLED(DM_REGULATOR)
823 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
824 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
825 if (ret) {
826 printf("Setting to 1.8V error");
827 return -EIO;
828 }
829 }
830#endif
831 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
832 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
833 return 0;
834
835 return -EAGAIN;
836 case MMC_SIGNAL_VOLTAGE_120:
837 return -ENOTSUPP;
838 default:
839 return 0;
840 }
841}
842
843static void esdhc_stop_tuning(struct mmc *mmc)
844{
845 struct mmc_cmd cmd;
846
847 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
848 cmd.cmdarg = 0;
849 cmd.resp_type = MMC_RSP_R1b;
850
851 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
852}
853
854static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
855{
Simon Glassc69cda22020-12-03 16:55:20 -0700856 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +0800857 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
858 struct fsl_esdhc *regs = priv->esdhc_regs;
859 struct mmc *mmc = &plat->mmc;
Haibo Chenc7f44182020-09-30 15:52:23 +0800860 u32 irqstaten = esdhc_read32(&regs->irqstaten);
861 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800862 int i, ret = -ETIMEDOUT;
863 u32 val, mixctrl;
864
865 /* clock tuning is not needed for upto 52MHz */
866 if (mmc->clock <= 52000000)
867 return 0;
868
869 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
870 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800871 val = esdhc_read32(&regs->autoc12err);
872 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800873 val &= ~MIX_CTRL_SMPCLK_SEL;
874 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
875
876 val |= MIX_CTRL_EXE_TUNE;
877 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
878
Haibo Chenc7f44182020-09-30 15:52:23 +0800879 esdhc_write32(&regs->autoc12err, val);
880 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800881 }
882
883 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chenc7f44182020-09-30 15:52:23 +0800884 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800885 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800886 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800887
Haibo Chenc7f44182020-09-30 15:52:23 +0800888 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
889 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lufa33d202019-06-21 11:42:27 +0800890
891 /*
892 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
893 * of loops reaches 40 times.
894 */
895 for (i = 0; i < MAX_TUNING_LOOP; i++) {
896 u32 ctrl;
897
898 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
899 if (mmc->bus_width == 8)
Haibo Chenc7f44182020-09-30 15:52:23 +0800900 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lufa33d202019-06-21 11:42:27 +0800901 else if (mmc->bus_width == 4)
Haibo Chenc7f44182020-09-30 15:52:23 +0800902 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800903 } else {
Haibo Chenc7f44182020-09-30 15:52:23 +0800904 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800905 }
906
907 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chenc7f44182020-09-30 15:52:23 +0800908 val = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800909 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800910 esdhc_write32(&regs->mixctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800911
912 /* We are using STD tuning, no need to check return value */
913 mmc_send_tuning(mmc, opcode, NULL);
914
Haibo Chenc7f44182020-09-30 15:52:23 +0800915 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lufa33d202019-06-21 11:42:27 +0800916 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
917 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800918 ret = 0;
919 break;
920 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800921 }
922
Haibo Chenc7f44182020-09-30 15:52:23 +0800923 esdhc_write32(&regs->irqstaten, irqstaten);
924 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800925
926 esdhc_stop_tuning(mmc);
927
928 return ret;
929}
930#endif
931
932static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
933{
934 struct fsl_esdhc *regs = priv->esdhc_regs;
935 int ret __maybe_unused;
Peng Fan1d01c982019-11-04 17:14:15 +0800936 u32 clock;
Yangbo Lufa33d202019-06-21 11:42:27 +0800937
Haibo Chen5d772192020-11-03 17:18:35 +0800938#ifdef MMC_SUPPORTS_TUNING
939 /*
940 * call esdhc_set_timing() before update the clock rate,
941 * This is because current we support DDR and SDR mode,
942 * Once the DDR_EN bit is set, the card clock will be
943 * divide by 2 automatically. So need to do this before
944 * setting clock rate.
945 */
946 if (priv->mode != mmc->selected_mode) {
947 ret = esdhc_set_timing(mmc);
948 if (ret) {
949 printf("esdhc_set_timing error %d\n", ret);
950 return ret;
951 }
952 }
953#endif
954
Yangbo Lufa33d202019-06-21 11:42:27 +0800955 /* Set the clock speed */
Peng Fan1d01c982019-11-04 17:14:15 +0800956 clock = mmc->clock;
957 if (clock < mmc->cfg->f_min)
958 clock = mmc->cfg->f_min;
959
960 if (priv->clock != clock)
961 set_sysctl(priv, mmc, clock);
Yangbo Lufa33d202019-06-21 11:42:27 +0800962
963#ifdef MMC_SUPPORTS_TUNING
964 if (mmc->clk_disable) {
965#ifdef CONFIG_FSL_USDHC
966 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
967#else
968 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
969#endif
970 } else {
971#ifdef CONFIG_FSL_USDHC
972 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
973 VENDORSPEC_CKEN);
974#else
975 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
976#endif
977 }
978
Haibo Chen5d772192020-11-03 17:18:35 +0800979 /*
980 * For HS400/HS400ES mode, make sure set the strobe dll in the
981 * target clock rate. So call esdhc_set_strobe_dll() after the
982 * clock updated.
983 */
984 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
985 esdhc_set_strobe_dll(mmc);
Yangbo Lufa33d202019-06-21 11:42:27 +0800986
987 if (priv->signal_voltage != mmc->signal_voltage) {
988 ret = esdhc_set_voltage(mmc);
989 if (ret) {
Marek Vasut50a17a62020-05-22 18:28:33 +0200990 if (ret != -ENOTSUPP)
991 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lufa33d202019-06-21 11:42:27 +0800992 return ret;
993 }
994 }
995#endif
996
997 /* Set the bus width */
998 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
999
1000 if (mmc->bus_width == 4)
1001 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
1002 else if (mmc->bus_width == 8)
1003 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1004
1005 return 0;
1006}
1007
1008static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1009{
1010 struct fsl_esdhc *regs = priv->esdhc_regs;
1011 ulong start;
1012
1013 /* Reset the entire host controller */
1014 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1015
1016 /* Wait until the controller is available */
1017 start = get_timer(0);
1018 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1019 if (get_timer(start) > 1000)
1020 return -ETIMEDOUT;
1021 }
1022
1023#if defined(CONFIG_FSL_USDHC)
1024 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1025 esdhc_write32(&regs->mmcboot, 0x0);
1026 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1027 esdhc_write32(&regs->mixctrl, 0x0);
1028 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1029
1030 /* Put VEND_SPEC to default value */
1031 if (priv->vs18_enable)
1032 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1033 ESDHC_VENDORSPEC_VSELECT));
1034 else
1035 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1036
1037 /* Disable DLL_CTRL delay line */
1038 esdhc_write32(&regs->dllctrl, 0x0);
1039#endif
1040
1041#ifndef ARCH_MXC
1042 /* Enable cache snooping */
1043 esdhc_write32(&regs->scr, 0x00000040);
1044#endif
1045
1046#ifndef CONFIG_FSL_USDHC
1047 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1048#else
1049 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1050#endif
1051
1052 /* Set the initial clock speed */
1053 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1054
1055 /* Disable the BRR and BWR bits in IRQSTAT */
1056 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1057
1058#ifdef CONFIG_MCF5441x
1059 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1060#else
1061 /* Put the PROCTL reg back to the default */
1062 esdhc_write32(&regs->proctl, PROCTL_INIT);
1063#endif
1064
1065 /* Set timout to the maximum value */
1066 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1067
1068 return 0;
1069}
1070
1071static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1072{
1073 struct fsl_esdhc *regs = priv->esdhc_regs;
1074 int timeout = 1000;
1075
1076#ifdef CONFIG_ESDHC_DETECT_QUIRK
1077 if (CONFIG_ESDHC_DETECT_QUIRK)
1078 return 1;
1079#endif
1080
1081#if CONFIG_IS_ENABLED(DM_MMC)
1082 if (priv->non_removable)
1083 return 1;
Fabio Estevam29230f32020-01-06 20:11:27 -03001084
1085 if (priv->broken_cd)
1086 return 1;
Simon Glassbcee8d62019-12-06 21:41:35 -07001087#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001088 if (dm_gpio_is_valid(&priv->cd_gpio))
1089 return dm_gpio_get_value(&priv->cd_gpio);
1090#endif
1091#endif
1092
1093 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1094 udelay(1000);
1095
1096 return timeout > 0;
1097}
1098
1099static int esdhc_reset(struct fsl_esdhc *regs)
1100{
1101 ulong start;
1102
1103 /* reset the controller */
1104 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1105
1106 /* hardware clears the bit when it is done */
1107 start = get_timer(0);
1108 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1109 if (get_timer(start) > 100) {
1110 printf("MMC/SD: Reset never completed.\n");
1111 return -ETIMEDOUT;
1112 }
1113 }
1114
1115 return 0;
1116}
1117
1118#if !CONFIG_IS_ENABLED(DM_MMC)
1119static int esdhc_getcd(struct mmc *mmc)
1120{
1121 struct fsl_esdhc_priv *priv = mmc->priv;
1122
1123 return esdhc_getcd_common(priv);
1124}
1125
1126static int esdhc_init(struct mmc *mmc)
1127{
1128 struct fsl_esdhc_priv *priv = mmc->priv;
1129
1130 return esdhc_init_common(priv, mmc);
1131}
1132
1133static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1134 struct mmc_data *data)
1135{
1136 struct fsl_esdhc_priv *priv = mmc->priv;
1137
1138 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1139}
1140
1141static int esdhc_set_ios(struct mmc *mmc)
1142{
1143 struct fsl_esdhc_priv *priv = mmc->priv;
1144
1145 return esdhc_set_ios_common(priv, mmc);
1146}
1147
1148static const struct mmc_ops esdhc_ops = {
1149 .getcd = esdhc_getcd,
1150 .init = esdhc_init,
1151 .send_cmd = esdhc_send_cmd,
1152 .set_ios = esdhc_set_ios,
1153};
1154#endif
1155
1156static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1157 struct fsl_esdhc_plat *plat)
1158{
1159 struct mmc_config *cfg;
1160 struct fsl_esdhc *regs;
1161 u32 caps, voltage_caps;
1162 int ret;
1163
1164 if (!priv)
1165 return -EINVAL;
1166
1167 regs = priv->esdhc_regs;
1168
1169 /* First reset the eSDHC controller */
1170 ret = esdhc_reset(regs);
1171 if (ret)
1172 return ret;
1173
1174#ifdef CONFIG_MCF5441x
1175 /* ColdFire, using SDHC_DATA[3] for card detection */
1176 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1177#endif
1178
1179#ifndef CONFIG_FSL_USDHC
1180 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1181 | SYSCTL_IPGEN | SYSCTL_CKEN);
1182 /* Clearing tuning bits in case ROM has set it already */
1183 esdhc_write32(&regs->mixctrl, 0);
1184 esdhc_write32(&regs->autoc12err, 0);
1185 esdhc_write32(&regs->clktunectrlstatus, 0);
1186#else
1187 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1188 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1189#endif
1190
1191 if (priv->vs18_enable)
1192 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1193
Haibo Chenc7f44182020-09-30 15:52:23 +08001194 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lufa33d202019-06-21 11:42:27 +08001195 cfg = &plat->cfg;
1196#ifndef CONFIG_DM_MMC
1197 memset(cfg, '\0', sizeof(*cfg));
1198#endif
1199
1200 voltage_caps = 0;
1201 caps = esdhc_read32(&regs->hostcapblt);
1202
1203#ifdef CONFIG_MCF5441x
1204 /*
1205 * MCF5441x RM declares in more points that sdhc clock speed must
1206 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1207 * from host capabilities.
1208 */
1209 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1210#endif
1211
1212#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1213 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1214 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1215#endif
1216
1217/* T4240 host controller capabilities register should have VS33 bit */
1218#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1219 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1220#endif
1221
1222 if (caps & ESDHC_HOSTCAPBLT_VS18)
1223 voltage_caps |= MMC_VDD_165_195;
1224 if (caps & ESDHC_HOSTCAPBLT_VS30)
1225 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1226 if (caps & ESDHC_HOSTCAPBLT_VS33)
1227 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1228
1229 cfg->name = "FSL_SDHC";
1230#if !CONFIG_IS_ENABLED(DM_MMC)
1231 cfg->ops = &esdhc_ops;
1232#endif
1233#ifdef CONFIG_SYS_SD_VOLTAGE
1234 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1235#else
1236 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1237#endif
1238 if ((cfg->voltages & voltage_caps) == 0) {
1239 printf("voltage not supported by controller\n");
1240 return -1;
1241 }
1242
1243 if (priv->bus_width == 8)
1244 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1245 else if (priv->bus_width == 4)
1246 cfg->host_caps = MMC_MODE_4BIT;
1247
1248 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1249#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1250 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1251#endif
1252
1253 if (priv->bus_width > 0) {
1254 if (priv->bus_width < 8)
1255 cfg->host_caps &= ~MMC_MODE_8BIT;
1256 if (priv->bus_width < 4)
1257 cfg->host_caps &= ~MMC_MODE_4BIT;
1258 }
1259
1260 if (caps & ESDHC_HOSTCAPBLT_HSS)
1261 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1262
1263#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1264 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1265 cfg->host_caps &= ~MMC_MODE_8BIT;
1266#endif
1267
1268 cfg->host_caps |= priv->caps;
1269
1270 cfg->f_min = 400000;
1271 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1272
1273 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1274
Haibo Chenc7f44182020-09-30 15:52:23 +08001275 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +08001276 if (priv->flags & ESDHC_FLAG_USDHC) {
1277 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +08001278 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +08001279
1280 val |= ESDHC_STD_TUNING_EN;
1281 val &= ~ESDHC_TUNING_START_TAP_MASK;
1282 val |= priv->tuning_start_tap;
1283 val &= ~ESDHC_TUNING_STEP_MASK;
1284 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chenba616762020-06-22 19:38:04 +08001285
1286 /* Disable the CMD CRC check for tuning, if not, need to
1287 * add some delay after every tuning command, because
1288 * hardware standard tuning logic will directly go to next
1289 * step once it detect the CMD CRC error, will not wait for
1290 * the card side to finally send out the tuning data, trigger
1291 * the buffer read ready interrupt immediately. If usdhc send
1292 * the next tuning command some eMMC card will stuck, can't
1293 * response, block the tuning procedure or the first command
1294 * after the whole tuning procedure always can't get any response.
1295 */
1296 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chenc7f44182020-09-30 15:52:23 +08001297 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +08001298 }
1299 }
1300
1301 return 0;
1302}
1303
1304#if !CONFIG_IS_ENABLED(DM_MMC)
1305static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1306 struct fsl_esdhc_priv *priv)
1307{
1308 if (!cfg || !priv)
1309 return -EINVAL;
1310
1311 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1312 priv->bus_width = cfg->max_bus_width;
1313 priv->sdhc_clk = cfg->sdhc_clk;
1314 priv->wp_enable = cfg->wp_enable;
1315 priv->vs18_enable = cfg->vs18_enable;
1316
1317 return 0;
1318};
1319
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001320int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lufa33d202019-06-21 11:42:27 +08001321{
1322 struct fsl_esdhc_plat *plat;
1323 struct fsl_esdhc_priv *priv;
1324 struct mmc *mmc;
1325 int ret;
1326
1327 if (!cfg)
1328 return -EINVAL;
1329
1330 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1331 if (!priv)
1332 return -ENOMEM;
1333 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1334 if (!plat) {
1335 free(priv);
1336 return -ENOMEM;
1337 }
1338
1339 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1340 if (ret) {
1341 debug("%s xlate failure\n", __func__);
1342 free(plat);
1343 free(priv);
1344 return ret;
1345 }
1346
1347 ret = fsl_esdhc_init(priv, plat);
1348 if (ret) {
1349 debug("%s init failure\n", __func__);
1350 free(plat);
1351 free(priv);
1352 return ret;
1353 }
1354
1355 mmc = mmc_create(&plat->cfg, priv);
1356 if (!mmc)
1357 return -EIO;
1358
1359 priv->mmc = mmc;
1360
1361 return 0;
1362}
1363
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001364int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lufa33d202019-06-21 11:42:27 +08001365{
1366 struct fsl_esdhc_cfg *cfg;
1367
1368 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1369 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1370 cfg->sdhc_clk = gd->arch.sdhc_clk;
1371 return fsl_esdhc_initialize(bis, cfg);
1372}
1373#endif
1374
Yangbo Lufa33d202019-06-21 11:42:27 +08001375#ifdef CONFIG_OF_LIBFDT
1376__weak int esdhc_status_fixup(void *blob, const char *compat)
1377{
1378#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1379 if (!hwconfig("esdhc")) {
1380 do_fixup_by_compat(blob, compat, "status", "disabled",
1381 sizeof("disabled"), 1);
1382 return 1;
1383 }
1384#endif
1385 return 0;
1386}
1387
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001388void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lufa33d202019-06-21 11:42:27 +08001389{
1390 const char *compat = "fsl,esdhc";
1391
1392 if (esdhc_status_fixup(blob, compat))
1393 return;
1394
Yangbo Lufa33d202019-06-21 11:42:27 +08001395 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1396 gd->arch.sdhc_clk, 1);
Yangbo Lufa33d202019-06-21 11:42:27 +08001397}
1398#endif
1399
1400#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +08001401#include <asm/arch/clock.h>
Yangbo Lufa33d202019-06-21 11:42:27 +08001402__weak void init_clk_usdhc(u32 index)
1403{
1404}
1405
Simon Glassd1998a92020-12-03 16:55:21 -07001406static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lufa33d202019-06-21 11:42:27 +08001407{
Walter Lozano23721772020-07-29 12:31:17 -03001408#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Yangbo Lufa33d202019-06-21 11:42:27 +08001409 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001410#if CONFIG_IS_ENABLED(DM_REGULATOR)
1411 struct udevice *vqmmc_dev;
Walter Lozano23721772020-07-29 12:31:17 -03001412 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001413#endif
Walter Lozano23721772020-07-29 12:31:17 -03001414 const void *fdt = gd->fdt_blob;
1415 int node = dev_of_offset(dev);
1416
Yangbo Lufa33d202019-06-21 11:42:27 +08001417 fdt_addr_t addr;
1418 unsigned int val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001419
1420 addr = dev_read_addr(dev);
1421 if (addr == FDT_ADDR_T_NONE)
1422 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001423 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lufa33d202019-06-21 11:42:27 +08001424 priv->dev = dev;
1425 priv->mode = -1;
Yangbo Lufa33d202019-06-21 11:42:27 +08001426
1427 val = dev_read_u32_default(dev, "bus-width", -1);
1428 if (val == 8)
1429 priv->bus_width = 8;
1430 else if (val == 4)
1431 priv->bus_width = 4;
1432 else
1433 priv->bus_width = 1;
1434
1435 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1436 priv->tuning_step = val;
1437 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1438 ESDHC_TUNING_START_TAP_DEFAULT);
1439 priv->tuning_start_tap = val;
1440 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1441 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1442 priv->strobe_dll_delay_target = val;
1443
Fabio Estevam29230f32020-01-06 20:11:27 -03001444 if (dev_read_bool(dev, "broken-cd"))
1445 priv->broken_cd = 1;
1446
Yangbo Lufa33d202019-06-21 11:42:27 +08001447 if (dev_read_bool(dev, "non-removable")) {
1448 priv->non_removable = 1;
1449 } else {
1450 priv->non_removable = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -07001451#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001452 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1453 GPIOD_IS_IN);
1454#endif
1455 }
1456
1457 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1458 priv->wp_enable = 1;
1459 } else {
1460 priv->wp_enable = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -07001461#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001462 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1463 GPIOD_IS_IN);
1464#endif
1465 }
1466
1467 priv->vs18_enable = 0;
1468
1469#if CONFIG_IS_ENABLED(DM_REGULATOR)
1470 /*
1471 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1472 * otherwise, emmc will work abnormally.
1473 */
1474 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1475 if (ret) {
1476 dev_dbg(dev, "no vqmmc-supply\n");
1477 } else {
Marek Vasut406df852020-05-22 18:19:08 +02001478 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001479 ret = regulator_set_enable(vqmmc_dev, true);
1480 if (ret) {
1481 dev_err(dev, "fail to enable vqmmc-supply\n");
1482 return ret;
1483 }
1484
1485 if (regulator_get_value(vqmmc_dev) == 1800000)
1486 priv->vs18_enable = 1;
1487 }
1488#endif
Walter Lozano23721772020-07-29 12:31:17 -03001489#endif
1490 return 0;
1491}
1492
1493static int fsl_esdhc_probe(struct udevice *dev)
1494{
1495 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001496 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano23721772020-07-29 12:31:17 -03001497 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1498 struct esdhc_soc_data *data =
1499 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1500 struct mmc *mmc;
1501#if !CONFIG_IS_ENABLED(BLK)
1502 struct blk_desc *bdesc;
1503#endif
1504 int ret;
1505
1506#if CONFIG_IS_ENABLED(OF_PLATDATA)
1507 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1508 unsigned int val;
1509
1510 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1511 val = plat->dtplat.bus_width;
1512 if (val == 8)
1513 priv->bus_width = 8;
1514 else if (val == 4)
1515 priv->bus_width = 4;
1516 else
1517 priv->bus_width = 1;
Walter Lozano7142ff92020-07-29 12:31:19 -03001518
1519 if (dtplat->non_removable)
1520 priv->non_removable = 1;
1521 else
1522 priv->non_removable = 0;
1523
1524 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1525 struct udevice *gpiodev;
Walter Lozano7142ff92020-07-29 12:31:19 -03001526
Simon Glass8a38abf2020-10-03 11:31:40 -06001527 ret = device_get_by_driver_info_idx(dtplat->cd_gpios->idx,
1528 &gpiodev);
Walter Lozano7142ff92020-07-29 12:31:19 -03001529 if (ret)
1530 return ret;
1531
1532 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1533 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1534 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1535
1536 if (ret)
1537 return ret;
1538 }
Walter Lozano23721772020-07-29 12:31:17 -03001539#endif
1540
1541 if (data)
1542 priv->flags = data->flags;
Yangbo Lufa33d202019-06-21 11:42:27 +08001543
Yangbo Lufa33d202019-06-21 11:42:27 +08001544 /*
1545 * TODO:
1546 * Because lack of clk driver, if SDHC clk is not enabled,
1547 * need to enable it first before this driver is invoked.
1548 *
1549 * we use MXC_ESDHC_CLK to get clk freq.
1550 * If one would like to make this function work,
1551 * the aliases should be provided in dts as this:
1552 *
1553 * aliases {
1554 * mmc0 = &usdhc1;
1555 * mmc1 = &usdhc2;
1556 * mmc2 = &usdhc3;
1557 * mmc3 = &usdhc4;
1558 * };
1559 * Then if your board only supports mmc2 and mmc3, but we can
1560 * correctly get the seq as 2 and 3, then let mxc_get_clock
1561 * work as expected.
1562 */
1563
Simon Glass8b85dfc2020-12-16 21:20:07 -07001564 init_clk_usdhc(dev_seq(dev));
Yangbo Lufa33d202019-06-21 11:42:27 +08001565
Giulio Benettia820bed2020-01-10 15:51:45 +01001566#if CONFIG_IS_ENABLED(CLK)
1567 /* Assigned clock already set clock */
1568 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1569 if (ret) {
1570 printf("Failed to get per_clk\n");
1571 return ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001572 }
Giulio Benettia820bed2020-01-10 15:51:45 +01001573 ret = clk_enable(&priv->per_clk);
1574 if (ret) {
1575 printf("Failed to enable per_clk\n");
1576 return ret;
1577 }
1578
1579 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1580#else
Simon Glass8b85dfc2020-12-16 21:20:07 -07001581 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettia820bed2020-01-10 15:51:45 +01001582 if (priv->sdhc_clk <= 0) {
1583 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1584 return -EINVAL;
1585 }
1586#endif
Yangbo Lufa33d202019-06-21 11:42:27 +08001587
1588 ret = fsl_esdhc_init(priv, plat);
1589 if (ret) {
1590 dev_err(dev, "fsl_esdhc_init failure\n");
1591 return ret;
1592 }
1593
Walter Lozano23721772020-07-29 12:31:17 -03001594#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Peng Fanb0155ac2019-07-10 09:35:24 +00001595 ret = mmc_of_parse(dev, &plat->cfg);
1596 if (ret)
1597 return ret;
Walter Lozano23721772020-07-29 12:31:17 -03001598#endif
Peng Fanb0155ac2019-07-10 09:35:24 +00001599
Yangbo Lufa33d202019-06-21 11:42:27 +08001600 mmc = &plat->mmc;
1601 mmc->cfg = &plat->cfg;
1602 mmc->dev = dev;
1603#if !CONFIG_IS_ENABLED(BLK)
1604 mmc->priv = priv;
1605
1606 /* Setup dsr related values */
1607 mmc->dsr_imp = 0;
1608 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1609 /* Setup the universal parts of the block interface just once */
1610 bdesc = mmc_get_blk_desc(mmc);
1611 bdesc->if_type = IF_TYPE_MMC;
1612 bdesc->removable = 1;
1613 bdesc->devnum = mmc_get_next_devnum();
1614 bdesc->block_read = mmc_bread;
1615 bdesc->block_write = mmc_bwrite;
1616 bdesc->block_erase = mmc_berase;
1617
1618 /* setup initial part type */
1619 bdesc->part_type = mmc->cfg->part_type;
1620 mmc_list_add(mmc);
1621#endif
1622
1623 upriv->mmc = mmc;
1624
1625 return esdhc_init_common(priv, mmc);
1626}
1627
1628#if CONFIG_IS_ENABLED(DM_MMC)
1629static int fsl_esdhc_get_cd(struct udevice *dev)
1630{
1631 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1632
1633 return esdhc_getcd_common(priv);
1634}
1635
1636static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1637 struct mmc_data *data)
1638{
Simon Glassc69cda22020-12-03 16:55:20 -07001639 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001640 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1641
1642 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1643}
1644
1645static int fsl_esdhc_set_ios(struct udevice *dev)
1646{
Simon Glassc69cda22020-12-03 16:55:20 -07001647 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001648 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1649
1650 return esdhc_set_ios_common(priv, &plat->mmc);
1651}
1652
Peng Fane9c22552019-07-10 09:35:26 +00001653#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1654static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1655{
1656 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1657 struct fsl_esdhc *regs = priv->esdhc_regs;
1658 u32 m;
1659
Haibo Chenc7f44182020-09-30 15:52:23 +08001660 m = esdhc_read32(&regs->mixctrl);
Peng Fane9c22552019-07-10 09:35:26 +00001661 m |= MIX_CTRL_HS400_ES;
Haibo Chenc7f44182020-09-30 15:52:23 +08001662 esdhc_write32(&regs->mixctrl, m);
Peng Fane9c22552019-07-10 09:35:26 +00001663
1664 return 0;
1665}
1666#endif
1667
Haibo Chenb5874b52020-11-05 14:57:13 +08001668static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1669 int timeout_us)
1670{
1671 int ret;
1672 u32 tmp;
1673 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1674 struct fsl_esdhc *regs = priv->esdhc_regs;
1675
1676 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1677 !!(tmp & PRSSTAT_DAT0) == !!state,
1678 timeout_us);
1679 return ret;
1680}
1681
Yangbo Lufa33d202019-06-21 11:42:27 +08001682static const struct dm_mmc_ops fsl_esdhc_ops = {
1683 .get_cd = fsl_esdhc_get_cd,
1684 .send_cmd = fsl_esdhc_send_cmd,
1685 .set_ios = fsl_esdhc_set_ios,
1686#ifdef MMC_SUPPORTS_TUNING
1687 .execute_tuning = fsl_esdhc_execute_tuning,
1688#endif
Peng Fane9c22552019-07-10 09:35:26 +00001689#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1690 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1691#endif
Haibo Chenb5874b52020-11-05 14:57:13 +08001692 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lufa33d202019-06-21 11:42:27 +08001693};
1694#endif
1695
1696static struct esdhc_soc_data usdhc_imx7d_data = {
1697 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1698 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1699 | ESDHC_FLAG_HS400,
Yangbo Lufa33d202019-06-21 11:42:27 +08001700};
1701
Peng Fan609ba122019-07-10 09:35:28 +00001702static struct esdhc_soc_data usdhc_imx8qm_data = {
1703 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1704 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1705 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1706};
1707
Yangbo Lufa33d202019-06-21 11:42:27 +08001708static const struct udevice_id fsl_esdhc_ids[] = {
1709 { .compatible = "fsl,imx53-esdhc", },
1710 { .compatible = "fsl,imx6ul-usdhc", },
1711 { .compatible = "fsl,imx6sx-usdhc", },
1712 { .compatible = "fsl,imx6sl-usdhc", },
1713 { .compatible = "fsl,imx6q-usdhc", },
1714 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1715 { .compatible = "fsl,imx7ulp-usdhc", },
Peng Fan609ba122019-07-10 09:35:28 +00001716 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fanf65d0842019-11-04 17:31:17 +08001717 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1718 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1719 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti6a63a872020-01-10 15:51:46 +01001720 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001721 { .compatible = "fsl,esdhc", },
1722 { /* sentinel */ }
1723};
1724
1725#if CONFIG_IS_ENABLED(BLK)
1726static int fsl_esdhc_bind(struct udevice *dev)
1727{
Simon Glassc69cda22020-12-03 16:55:20 -07001728 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001729
1730 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1731}
1732#endif
1733
1734U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano45154f02020-07-29 12:31:16 -03001735 .name = "fsl_esdhc",
Yangbo Lufa33d202019-06-21 11:42:27 +08001736 .id = UCLASS_MMC,
1737 .of_match = fsl_esdhc_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07001738 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lufa33d202019-06-21 11:42:27 +08001739 .ops = &fsl_esdhc_ops,
1740#if CONFIG_IS_ENABLED(BLK)
1741 .bind = fsl_esdhc_bind,
1742#endif
1743 .probe = fsl_esdhc_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -07001744 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass41575d82020-12-03 16:55:17 -07001745 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lufa33d202019-06-21 11:42:27 +08001746};
Walter Lozano23721772020-07-29 12:31:17 -03001747
Simon Glassbdf8fd72020-12-28 20:34:57 -07001748DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lufa33d202019-06-21 11:42:27 +08001749#endif