blob: e5409ade1bc12c8fedda2ee59aeb7d171310804a [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 Glass336d4612020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060026#include <linux/delay.h>
Simon Glass61b29b82020-02-03 07:36:15 -070027#include <linux/err.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080028#include <power/regulator.h>
29#include <malloc.h>
30#include <fsl_esdhc_imx.h>
31#include <fdt_support.h>
32#include <asm/io.h>
33#include <dm.h>
34#include <asm-generic/gpio.h>
35#include <dm/pinctrl.h>
Walter Lozano23721772020-07-29 12:31:17 -030036#include <dt-structs.h>
37#include <mapmem.h>
38#include <dm/ofnode.h>
Haibo Chenf9c3a812020-09-01 15:34:06 +080039#include <linux/iopoll.h>
Yangbo Lufa33d202019-06-21 11:42:27 +080040
41#if !CONFIG_IS_ENABLED(BLK)
42#include "mmc_private.h"
43#endif
44
45DECLARE_GLOBAL_DATA_PTR;
46
47#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
48 IRQSTATEN_CINT | \
49 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
50 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
51 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
52 IRQSTATEN_DINT)
53#define MAX_TUNING_LOOP 40
54#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
55
56struct fsl_esdhc {
57 uint dsaddr; /* SDMA system address register */
58 uint blkattr; /* Block attributes register */
59 uint cmdarg; /* Command argument register */
60 uint xfertyp; /* Transfer type register */
61 uint cmdrsp0; /* Command response 0 register */
62 uint cmdrsp1; /* Command response 1 register */
63 uint cmdrsp2; /* Command response 2 register */
64 uint cmdrsp3; /* Command response 3 register */
65 uint datport; /* Buffer data port register */
66 uint prsstat; /* Present state register */
67 uint proctl; /* Protocol control register */
68 uint sysctl; /* System Control Register */
69 uint irqstat; /* Interrupt status register */
70 uint irqstaten; /* Interrupt status enable register */
71 uint irqsigen; /* Interrupt signal enable register */
72 uint autoc12err; /* Auto CMD error status register */
73 uint hostcapblt; /* Host controller capabilities register */
74 uint wml; /* Watermark level register */
75 uint mixctrl; /* For USDHC */
76 char reserved1[4]; /* reserved */
77 uint fevt; /* Force event register */
78 uint admaes; /* ADMA error status register */
79 uint adsaddr; /* ADMA system address register */
80 char reserved2[4];
81 uint dllctrl;
82 uint dllstat;
83 uint clktunectrlstatus;
84 char reserved3[4];
85 uint strobe_dllctrl;
86 uint strobe_dllstat;
87 char reserved4[72];
88 uint vendorspec;
89 uint mmcboot;
90 uint vendorspec2;
Giulio Benetti6a63a872020-01-10 15:51:46 +010091 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lufa33d202019-06-21 11:42:27 +080092 char reserved5[44];
93 uint hostver; /* Host controller version register */
94 char reserved6[4]; /* reserved */
95 uint dmaerraddr; /* DMA error address register */
96 char reserved7[4]; /* reserved */
97 uint dmaerrattr; /* DMA error attribute register */
98 char reserved8[4]; /* reserved */
99 uint hostcapblt2; /* Host controller capabilities register 2 */
100 char reserved9[8]; /* reserved */
101 uint tcr; /* Tuning control register */
102 char reserved10[28]; /* reserved */
103 uint sddirctl; /* SD direction control register */
104 char reserved11[712];/* reserved */
105 uint scr; /* eSDHC control register */
106};
107
108struct fsl_esdhc_plat {
Walter Lozano23721772020-07-29 12:31:17 -0300109#if CONFIG_IS_ENABLED(OF_PLATDATA)
110 /* Put this first since driver model will copy the data here */
111 struct dtd_fsl_esdhc dtplat;
112#endif
113
Yangbo Lufa33d202019-06-21 11:42:27 +0800114 struct mmc_config cfg;
115 struct mmc mmc;
116};
117
118struct esdhc_soc_data {
119 u32 flags;
Yangbo Lufa33d202019-06-21 11:42:27 +0800120};
121
122/**
123 * struct fsl_esdhc_priv
124 *
125 * @esdhc_regs: registers of the sdhc controller
126 * @sdhc_clk: Current clk of the sdhc controller
127 * @bus_width: bus width, 1bit, 4bit or 8bit
128 * @cfg: mmc config
129 * @mmc: mmc
130 * Following is used when Driver Model is enabled for MMC
131 * @dev: pointer for the device
132 * @non_removable: 0: removable; 1: non-removable
Fabio Estevam29230f32020-01-06 20:11:27 -0300133 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lufa33d202019-06-21 11:42:27 +0800134 * @wp_enable: 1: enable checking wp; 0: no check
135 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
136 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
137 * @caps: controller capabilities
138 * @tuning_step: tuning step setting in tuning_ctrl register
139 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
140 * @strobe_dll_delay_target: settings in strobe_dllctrl
141 * @signal_voltage: indicating the current voltage
142 * @cd_gpio: gpio for card detection
143 * @wp_gpio: gpio for write protection
144 */
145struct fsl_esdhc_priv {
146 struct fsl_esdhc *esdhc_regs;
147 unsigned int sdhc_clk;
148 struct clk per_clk;
149 unsigned int clock;
150 unsigned int mode;
151 unsigned int bus_width;
152#if !CONFIG_IS_ENABLED(BLK)
153 struct mmc *mmc;
154#endif
155 struct udevice *dev;
156 int non_removable;
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;
Ye Li82771712019-07-11 03:29:02 +0000166#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lufa33d202019-06-21 11:42:27 +0800167 struct udevice *vqmmc_dev;
168 struct udevice *vmmc_dev;
169#endif
Simon Glassbcee8d62019-12-06 21:41:35 -0700170#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +0800171 struct gpio_desc cd_gpio;
172 struct gpio_desc wp_gpio;
173#endif
174};
175
176/* Return the XFERTYP flags for a given command and data packet */
177static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
178{
179 uint xfertyp = 0;
180
181 if (data) {
182 xfertyp |= XFERTYP_DPSEL;
183#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
184 xfertyp |= XFERTYP_DMAEN;
185#endif
186 if (data->blocks > 1) {
187 xfertyp |= XFERTYP_MSBSEL;
188 xfertyp |= XFERTYP_BCEN;
189#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
190 xfertyp |= XFERTYP_AC12EN;
191#endif
192 }
193
194 if (data->flags & MMC_DATA_READ)
195 xfertyp |= XFERTYP_DTDSEL;
196 }
197
198 if (cmd->resp_type & MMC_RSP_CRC)
199 xfertyp |= XFERTYP_CCCEN;
200 if (cmd->resp_type & MMC_RSP_OPCODE)
201 xfertyp |= XFERTYP_CICEN;
202 if (cmd->resp_type & MMC_RSP_136)
203 xfertyp |= XFERTYP_RSPTYP_136;
204 else if (cmd->resp_type & MMC_RSP_BUSY)
205 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
206 else if (cmd->resp_type & MMC_RSP_PRESENT)
207 xfertyp |= XFERTYP_RSPTYP_48;
208
209 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
210 xfertyp |= XFERTYP_CMDTYP_ABORT;
211
212 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
213}
214
215#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
216/*
217 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
218 */
219static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
220 struct mmc_data *data)
221{
222 struct fsl_esdhc *regs = priv->esdhc_regs;
223 uint blocks;
224 char *buffer;
225 uint databuf;
226 uint size;
227 uint irqstat;
228 ulong start;
229
230 if (data->flags & MMC_DATA_READ) {
231 blocks = data->blocks;
232 buffer = data->dest;
233 while (blocks) {
234 start = get_timer(0);
235 size = data->blocksize;
236 irqstat = esdhc_read32(&regs->irqstat);
237 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
238 if (get_timer(start) > PIO_TIMEOUT) {
239 printf("\nData Read Failed in PIO Mode.");
240 return;
241 }
242 }
243 while (size && (!(irqstat & IRQSTAT_TC))) {
244 udelay(100); /* Wait before last byte transfer complete */
245 irqstat = esdhc_read32(&regs->irqstat);
246 databuf = in_le32(&regs->datport);
247 *((uint *)buffer) = databuf;
248 buffer += 4;
249 size -= 4;
250 }
251 blocks--;
252 }
253 } else {
254 blocks = data->blocks;
255 buffer = (char *)data->src;
256 while (blocks) {
257 start = get_timer(0);
258 size = data->blocksize;
259 irqstat = esdhc_read32(&regs->irqstat);
260 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
261 if (get_timer(start) > PIO_TIMEOUT) {
262 printf("\nData Write Failed in PIO Mode.");
263 return;
264 }
265 }
266 while (size && (!(irqstat & IRQSTAT_TC))) {
267 udelay(100); /* Wait before last byte transfer complete */
268 databuf = *((uint *)buffer);
269 buffer += 4;
270 size -= 4;
271 irqstat = esdhc_read32(&regs->irqstat);
272 out_le32(&regs->datport, databuf);
273 }
274 blocks--;
275 }
276 }
277}
278#endif
279
280static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
281 struct mmc_data *data)
282{
283 int timeout;
284 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu5053da22019-06-21 11:42:30 +0800285#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800286 dma_addr_t addr;
287#endif
288 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
Yangbo Lu5053da22019-06-21 11:42:30 +0800298#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800299 addr = virt_to_phys((void *)(data->dest));
300 if (upper_32_bits(addr))
301 printf("Error found for upper 32 bits\n");
302 else
303 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
304#else
305 esdhc_write32(&regs->dsaddr, (u32)data->dest);
306#endif
307#endif
308 } else {
309#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
310 flush_dcache_range((ulong)data->src,
311 (ulong)data->src+data->blocks
312 *data->blocksize);
313#endif
314 if (wml_value > WML_WR_WML_MAX)
315 wml_value = WML_WR_WML_MAX_VAL;
316 if (priv->wp_enable) {
317 if ((esdhc_read32(&regs->prsstat) &
318 PRSSTAT_WPSPL) == 0) {
319 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
320 return -ETIMEDOUT;
321 }
322 } else {
Simon Glassbcee8d62019-12-06 21:41:35 -0700323#if CONFIG_IS_ENABLED(DM_GPIO)
324 if (dm_gpio_is_valid(&priv->wp_gpio) &&
325 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800326 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
327 return -ETIMEDOUT;
328 }
329#endif
330 }
331
332 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
333 wml_value << 16);
334#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu5053da22019-06-21 11:42:30 +0800335#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800336 addr = virt_to_phys((void *)(data->src));
337 if (upper_32_bits(addr))
338 printf("Error found for upper 32 bits\n");
339 else
340 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
341#else
342 esdhc_write32(&regs->dsaddr, (u32)data->src);
343#endif
344#endif
345 }
346
347 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
348
349 /* Calculate the timeout period for data transactions */
350 /*
351 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
352 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
353 * So, Number of SD Clock cycles for 0.25sec should be minimum
354 * (SD Clock/sec * 0.25 sec) SD Clock cycles
355 * = (mmc->clock * 1/4) SD Clock cycles
356 * As 1) >= 2)
357 * => (2^(timeout+13)) >= mmc->clock * 1/4
358 * Taking log2 both the sides
359 * => timeout + 13 >= log2(mmc->clock/4)
360 * Rounding up to next power of 2
361 * => timeout + 13 = log2(mmc->clock/4) + 1
362 * => timeout + 13 = fls(mmc->clock/4)
363 *
364 * However, the MMC spec "It is strongly recommended for hosts to
365 * implement more than 500ms timeout value even if the card
366 * indicates the 250ms maximum busy length." Even the previous
367 * value of 300ms is known to be insufficient for some cards.
368 * So, we use
369 * => timeout + 13 = fls(mmc->clock/2)
370 */
371 timeout = fls(mmc->clock/2);
372 timeout -= 13;
373
374 if (timeout > 14)
375 timeout = 14;
376
377 if (timeout < 0)
378 timeout = 0;
379
380#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
381 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
382 timeout++;
383#endif
384
385#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
386 timeout = 0xE;
387#endif
388 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
389
390 return 0;
391}
392
393static void check_and_invalidate_dcache_range
394 (struct mmc_cmd *cmd,
395 struct mmc_data *data) {
396 unsigned start = 0;
397 unsigned end = 0;
398 unsigned size = roundup(ARCH_DMA_MINALIGN,
399 data->blocks*data->blocksize);
Yangbo Lu5053da22019-06-21 11:42:30 +0800400#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lufa33d202019-06-21 11:42:27 +0800401 dma_addr_t addr;
402
403 addr = virt_to_phys((void *)(data->dest));
404 if (upper_32_bits(addr))
405 printf("Error found for upper 32 bits\n");
406 else
407 start = lower_32_bits(addr);
408#else
409 start = (unsigned)data->dest;
410#endif
411 end = start + size;
412 invalidate_dcache_range(start, end);
413}
414
415#ifdef CONFIG_MCF5441x
416/*
417 * Swaps 32-bit words to little-endian byte order.
418 */
419static inline void sd_swap_dma_buff(struct mmc_data *data)
420{
421 int i, size = data->blocksize >> 2;
422 u32 *buffer = (u32 *)data->dest;
423 u32 sw;
424
425 while (data->blocks--) {
426 for (i = 0; i < size; i++) {
427 sw = __sw32(*buffer);
428 *buffer++ = sw;
429 }
430 }
431}
432#endif
433
434/*
435 * Sends a command out on the bus. Takes the mmc pointer,
436 * a command pointer, and an optional data pointer.
437 */
438static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
439 struct mmc_cmd *cmd, struct mmc_data *data)
440{
441 int err = 0;
442 uint xfertyp;
443 uint irqstat;
444 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
445 struct fsl_esdhc *regs = priv->esdhc_regs;
446 unsigned long start;
447
448#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
449 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
450 return 0;
451#endif
452
453 esdhc_write32(&regs->irqstat, -1);
454
455 sync();
456
457 /* Wait for the bus to be idle */
458 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
459 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
460 ;
461
462 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
463 ;
464
Yangbo Lufa33d202019-06-21 11:42:27 +0800465 /* Set up for a data transfer if we have one */
466 if (data) {
467 err = esdhc_setup_data(priv, mmc, data);
468 if(err)
469 return err;
470
471 if (data->flags & MMC_DATA_READ)
472 check_and_invalidate_dcache_range(cmd, data);
473 }
474
475 /* Figure out the transfer arguments */
476 xfertyp = esdhc_xfertyp(cmd, data);
477
478 /* Mask all irqs */
479 esdhc_write32(&regs->irqsigen, 0);
480
481 /* Send the command */
482 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
483#if defined(CONFIG_FSL_USDHC)
484 esdhc_write32(&regs->mixctrl,
485 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
486 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
487 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
488#else
489 esdhc_write32(&regs->xfertyp, xfertyp);
490#endif
491
492 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
493 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
494 flags = IRQSTAT_BRR;
495
496 /* Wait for the command to complete */
497 start = get_timer(0);
498 while (!(esdhc_read32(&regs->irqstat) & flags)) {
499 if (get_timer(start) > 1000) {
500 err = -ETIMEDOUT;
501 goto out;
502 }
503 }
504
505 irqstat = esdhc_read32(&regs->irqstat);
506
507 if (irqstat & CMD_ERR) {
508 err = -ECOMM;
509 goto out;
510 }
511
512 if (irqstat & IRQSTAT_CTOE) {
513 err = -ETIMEDOUT;
514 goto out;
515 }
516
517 /* Switch voltage to 1.8V if CMD11 succeeded */
518 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
519 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
520
521 printf("Run CMD11 1.8V switch\n");
522 /* Sleep for 5 ms - max time for card to switch to 1.8V */
523 udelay(5000);
524 }
525
526 /* Workaround for ESDHC errata ENGcm03648 */
527 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan356f7822019-07-10 09:35:30 +0000528 int timeout = 50000;
Yangbo Lufa33d202019-06-21 11:42:27 +0800529
Peng Fan356f7822019-07-10 09:35:30 +0000530 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lufa33d202019-06-21 11:42:27 +0800531 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
532 PRSSTAT_DAT0)) {
533 udelay(100);
534 timeout--;
535 }
536
537 if (timeout <= 0) {
538 printf("Timeout waiting for DAT0 to go high!\n");
539 err = -ETIMEDOUT;
540 goto out;
541 }
542 }
543
544 /* Copy the response to the response buffer */
545 if (cmd->resp_type & MMC_RSP_136) {
546 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
547
548 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
549 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
550 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
551 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
552 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
553 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
554 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
555 cmd->response[3] = (cmdrsp0 << 8);
556 } else
557 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
558
559 /* Wait until all of the blocks are transferred */
560 if (data) {
561#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
562 esdhc_pio_read_write(priv, data);
563#else
564 flags = DATA_COMPLETE;
565 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
566 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
567 flags = IRQSTAT_BRR;
568 }
569
570 do {
571 irqstat = esdhc_read32(&regs->irqstat);
572
573 if (irqstat & IRQSTAT_DTOE) {
574 err = -ETIMEDOUT;
575 goto out;
576 }
577
578 if (irqstat & DATA_ERR) {
579 err = -ECOMM;
580 goto out;
581 }
582 } while ((irqstat & flags) != flags);
583
584 /*
585 * Need invalidate the dcache here again to avoid any
586 * cache-fill during the DMA operations such as the
587 * speculative pre-fetching etc.
588 */
589 if (data->flags & MMC_DATA_READ) {
590 check_and_invalidate_dcache_range(cmd, data);
591#ifdef CONFIG_MCF5441x
592 sd_swap_dma_buff(data);
593#endif
594 }
595#endif
596 }
597
598out:
599 /* Reset CMD and DATA portions on error */
600 if (err) {
601 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
602 SYSCTL_RSTC);
603 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
604 ;
605
606 if (data) {
607 esdhc_write32(&regs->sysctl,
608 esdhc_read32(&regs->sysctl) |
609 SYSCTL_RSTD);
610 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
611 ;
612 }
613
614 /* If this was CMD11, then notify that power cycle is needed */
615 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
616 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
617 }
618
619 esdhc_write32(&regs->irqstat, -1);
620
621 return err;
622}
623
624static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
625{
626 struct fsl_esdhc *regs = priv->esdhc_regs;
627 int div = 1;
Haibo Chenf9c3a812020-09-01 15:34:06 +0800628 u32 tmp;
629 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +0800630#ifdef ARCH_MXC
631#ifdef CONFIG_MX53
632 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
633 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
634#else
635 int pre_div = 1;
636#endif
637#else
638 int pre_div = 2;
639#endif
640 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
641 int sdhc_clk = priv->sdhc_clk;
642 uint clk;
643
Yangbo Lufa33d202019-06-21 11:42:27 +0800644 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
645 pre_div *= 2;
646
647 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
648 div++;
649
650 pre_div >>= 1;
651 div -= 1;
652
653 clk = (pre_div << 8) | (div << 4);
654
655#ifdef CONFIG_FSL_USDHC
656 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
657#else
658 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
659#endif
660
661 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
662
Haibo Chenf9c3a812020-09-01 15:34:06 +0800663 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
664 if (ret)
665 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lufa33d202019-06-21 11:42:27 +0800666
667#ifdef CONFIG_FSL_USDHC
668 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
669#else
670 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
671#endif
672
673 priv->clock = clock;
674}
675
Yangbo Lufa33d202019-06-21 11:42:27 +0800676#ifdef MMC_SUPPORTS_TUNING
677static int esdhc_change_pinstate(struct udevice *dev)
678{
679 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
680 int ret;
681
682 switch (priv->mode) {
683 case UHS_SDR50:
684 case UHS_DDR50:
685 ret = pinctrl_select_state(dev, "state_100mhz");
686 break;
687 case UHS_SDR104:
688 case MMC_HS_200:
689 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000690 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800691 ret = pinctrl_select_state(dev, "state_200mhz");
692 break;
693 default:
694 ret = pinctrl_select_state(dev, "default");
695 break;
696 }
697
698 if (ret)
699 printf("%s %d error\n", __func__, priv->mode);
700
701 return ret;
702}
703
704static void esdhc_reset_tuning(struct mmc *mmc)
705{
706 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
707 struct fsl_esdhc *regs = priv->esdhc_regs;
708
709 if (priv->flags & ESDHC_FLAG_USDHC) {
710 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
711 esdhc_clrbits32(&regs->autoc12err,
712 MIX_CTRL_SMPCLK_SEL |
713 MIX_CTRL_EXE_TUNE);
714 }
715 }
716}
717
718static void esdhc_set_strobe_dll(struct mmc *mmc)
719{
720 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
721 struct fsl_esdhc *regs = priv->esdhc_regs;
722 u32 val;
723
724 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800725 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Yangbo Lufa33d202019-06-21 11:42:27 +0800726
727 /*
728 * enable strobe dll ctrl and adjust the delay target
729 * for the uSDHC loopback read clock
730 */
731 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
732 (priv->strobe_dll_delay_target <<
733 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chenc7f44182020-09-30 15:52:23 +0800734 esdhc_write32(&regs->strobe_dllctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800735 /* wait 1us to make sure strobe dll status register stable */
736 mdelay(1);
Haibo Chenc7f44182020-09-30 15:52:23 +0800737 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lufa33d202019-06-21 11:42:27 +0800738 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
739 pr_warn("HS400 strobe DLL status REF not lock!\n");
740 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
741 pr_warn("HS400 strobe DLL status SLV not lock!\n");
742 }
743}
744
745static int esdhc_set_timing(struct mmc *mmc)
746{
747 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
748 struct fsl_esdhc *regs = priv->esdhc_regs;
749 u32 mixctrl;
750
Haibo Chenc7f44182020-09-30 15:52:23 +0800751 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800752 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
753
754 switch (mmc->selected_mode) {
755 case MMC_LEGACY:
Yangbo Lufa33d202019-06-21 11:42:27 +0800756 esdhc_reset_tuning(mmc);
Haibo Chenc7f44182020-09-30 15:52:23 +0800757 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800758 break;
759 case MMC_HS_400:
Peng Fane9c22552019-07-10 09:35:26 +0000760 case MMC_HS_400_ES:
Yangbo Lufa33d202019-06-21 11:42:27 +0800761 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800762 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800763 break;
764 case MMC_HS:
765 case MMC_HS_52:
766 case MMC_HS_200:
767 case SD_HS:
768 case UHS_SDR12:
769 case UHS_SDR25:
770 case UHS_SDR50:
771 case UHS_SDR104:
Haibo Chenc7f44182020-09-30 15:52:23 +0800772 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800773 break;
774 case UHS_DDR50:
775 case MMC_DDR_52:
776 mixctrl |= MIX_CTRL_DDREN;
Haibo Chenc7f44182020-09-30 15:52:23 +0800777 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800778 break;
779 default:
780 printf("Not supported %d\n", mmc->selected_mode);
781 return -EINVAL;
782 }
783
784 priv->mode = mmc->selected_mode;
785
786 return esdhc_change_pinstate(mmc->dev);
787}
788
789static int esdhc_set_voltage(struct mmc *mmc)
790{
791 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
792 struct fsl_esdhc *regs = priv->esdhc_regs;
793 int ret;
794
795 priv->signal_voltage = mmc->signal_voltage;
796 switch (mmc->signal_voltage) {
797 case MMC_SIGNAL_VOLTAGE_330:
798 if (priv->vs18_enable)
Marek Vasut50a17a62020-05-22 18:28:33 +0200799 return -ENOTSUPP;
Yangbo Lufa33d202019-06-21 11:42:27 +0800800#if CONFIG_IS_ENABLED(DM_REGULATOR)
801 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
802 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
803 if (ret) {
804 printf("Setting to 3.3V error");
805 return -EIO;
806 }
807 /* Wait for 5ms */
808 mdelay(5);
809 }
810#endif
811
812 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
813 if (!(esdhc_read32(&regs->vendorspec) &
814 ESDHC_VENDORSPEC_VSELECT))
815 return 0;
816
817 return -EAGAIN;
818 case MMC_SIGNAL_VOLTAGE_180:
819#if CONFIG_IS_ENABLED(DM_REGULATOR)
820 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
821 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
822 if (ret) {
823 printf("Setting to 1.8V error");
824 return -EIO;
825 }
826 }
827#endif
828 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
829 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
830 return 0;
831
832 return -EAGAIN;
833 case MMC_SIGNAL_VOLTAGE_120:
834 return -ENOTSUPP;
835 default:
836 return 0;
837 }
838}
839
840static void esdhc_stop_tuning(struct mmc *mmc)
841{
842 struct mmc_cmd cmd;
843
844 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
845 cmd.cmdarg = 0;
846 cmd.resp_type = MMC_RSP_R1b;
847
848 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
849}
850
851static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
852{
853 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
854 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
855 struct fsl_esdhc *regs = priv->esdhc_regs;
856 struct mmc *mmc = &plat->mmc;
Haibo Chenc7f44182020-09-30 15:52:23 +0800857 u32 irqstaten = esdhc_read32(&regs->irqstaten);
858 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800859 int i, ret = -ETIMEDOUT;
860 u32 val, mixctrl;
861
862 /* clock tuning is not needed for upto 52MHz */
863 if (mmc->clock <= 52000000)
864 return 0;
865
866 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
867 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +0800868 val = esdhc_read32(&regs->autoc12err);
869 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800870 val &= ~MIX_CTRL_SMPCLK_SEL;
871 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
872
873 val |= MIX_CTRL_EXE_TUNE;
874 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
875
Haibo Chenc7f44182020-09-30 15:52:23 +0800876 esdhc_write32(&regs->autoc12err, val);
877 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800878 }
879
880 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chenc7f44182020-09-30 15:52:23 +0800881 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800882 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800883 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800884
Haibo Chenc7f44182020-09-30 15:52:23 +0800885 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
886 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lufa33d202019-06-21 11:42:27 +0800887
888 /*
889 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
890 * of loops reaches 40 times.
891 */
892 for (i = 0; i < MAX_TUNING_LOOP; i++) {
893 u32 ctrl;
894
895 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
896 if (mmc->bus_width == 8)
Haibo Chenc7f44182020-09-30 15:52:23 +0800897 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lufa33d202019-06-21 11:42:27 +0800898 else if (mmc->bus_width == 4)
Haibo Chenc7f44182020-09-30 15:52:23 +0800899 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800900 } else {
Haibo Chenc7f44182020-09-30 15:52:23 +0800901 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lufa33d202019-06-21 11:42:27 +0800902 }
903
904 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chenc7f44182020-09-30 15:52:23 +0800905 val = esdhc_read32(&regs->mixctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +0800906 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chenc7f44182020-09-30 15:52:23 +0800907 esdhc_write32(&regs->mixctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +0800908
909 /* We are using STD tuning, no need to check return value */
910 mmc_send_tuning(mmc, opcode, NULL);
911
Haibo Chenc7f44182020-09-30 15:52:23 +0800912 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lufa33d202019-06-21 11:42:27 +0800913 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
914 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lufa33d202019-06-21 11:42:27 +0800915 ret = 0;
916 break;
917 }
Yangbo Lufa33d202019-06-21 11:42:27 +0800918 }
919
Haibo Chenc7f44182020-09-30 15:52:23 +0800920 esdhc_write32(&regs->irqstaten, irqstaten);
921 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lufa33d202019-06-21 11:42:27 +0800922
923 esdhc_stop_tuning(mmc);
924
925 return ret;
926}
927#endif
928
929static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
930{
931 struct fsl_esdhc *regs = priv->esdhc_regs;
932 int ret __maybe_unused;
Peng Fan1d01c982019-11-04 17:14:15 +0800933 u32 clock;
Yangbo Lufa33d202019-06-21 11:42:27 +0800934
Haibo Chen5d772192020-11-03 17:18:35 +0800935#ifdef MMC_SUPPORTS_TUNING
936 /*
937 * call esdhc_set_timing() before update the clock rate,
938 * This is because current we support DDR and SDR mode,
939 * Once the DDR_EN bit is set, the card clock will be
940 * divide by 2 automatically. So need to do this before
941 * setting clock rate.
942 */
943 if (priv->mode != mmc->selected_mode) {
944 ret = esdhc_set_timing(mmc);
945 if (ret) {
946 printf("esdhc_set_timing error %d\n", ret);
947 return ret;
948 }
949 }
950#endif
951
Yangbo Lufa33d202019-06-21 11:42:27 +0800952 /* Set the clock speed */
Peng Fan1d01c982019-11-04 17:14:15 +0800953 clock = mmc->clock;
954 if (clock < mmc->cfg->f_min)
955 clock = mmc->cfg->f_min;
956
957 if (priv->clock != clock)
958 set_sysctl(priv, mmc, clock);
Yangbo Lufa33d202019-06-21 11:42:27 +0800959
960#ifdef MMC_SUPPORTS_TUNING
961 if (mmc->clk_disable) {
962#ifdef CONFIG_FSL_USDHC
963 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
964#else
965 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
966#endif
967 } else {
968#ifdef CONFIG_FSL_USDHC
969 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
970 VENDORSPEC_CKEN);
971#else
972 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
973#endif
974 }
975
Haibo Chen5d772192020-11-03 17:18:35 +0800976 /*
977 * For HS400/HS400ES mode, make sure set the strobe dll in the
978 * target clock rate. So call esdhc_set_strobe_dll() after the
979 * clock updated.
980 */
981 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
982 esdhc_set_strobe_dll(mmc);
Yangbo Lufa33d202019-06-21 11:42:27 +0800983
984 if (priv->signal_voltage != mmc->signal_voltage) {
985 ret = esdhc_set_voltage(mmc);
986 if (ret) {
Marek Vasut50a17a62020-05-22 18:28:33 +0200987 if (ret != -ENOTSUPP)
988 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lufa33d202019-06-21 11:42:27 +0800989 return ret;
990 }
991 }
992#endif
993
994 /* Set the bus width */
995 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
996
997 if (mmc->bus_width == 4)
998 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
999 else if (mmc->bus_width == 8)
1000 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1001
1002 return 0;
1003}
1004
1005static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1006{
1007 struct fsl_esdhc *regs = priv->esdhc_regs;
1008 ulong start;
1009
1010 /* Reset the entire host controller */
1011 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1012
1013 /* Wait until the controller is available */
1014 start = get_timer(0);
1015 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1016 if (get_timer(start) > 1000)
1017 return -ETIMEDOUT;
1018 }
1019
1020#if defined(CONFIG_FSL_USDHC)
1021 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1022 esdhc_write32(&regs->mmcboot, 0x0);
1023 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1024 esdhc_write32(&regs->mixctrl, 0x0);
1025 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1026
1027 /* Put VEND_SPEC to default value */
1028 if (priv->vs18_enable)
1029 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1030 ESDHC_VENDORSPEC_VSELECT));
1031 else
1032 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1033
1034 /* Disable DLL_CTRL delay line */
1035 esdhc_write32(&regs->dllctrl, 0x0);
1036#endif
1037
1038#ifndef ARCH_MXC
1039 /* Enable cache snooping */
1040 esdhc_write32(&regs->scr, 0x00000040);
1041#endif
1042
1043#ifndef CONFIG_FSL_USDHC
1044 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1045#else
1046 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1047#endif
1048
1049 /* Set the initial clock speed */
1050 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1051
1052 /* Disable the BRR and BWR bits in IRQSTAT */
1053 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1054
1055#ifdef CONFIG_MCF5441x
1056 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1057#else
1058 /* Put the PROCTL reg back to the default */
1059 esdhc_write32(&regs->proctl, PROCTL_INIT);
1060#endif
1061
1062 /* Set timout to the maximum value */
1063 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1064
1065 return 0;
1066}
1067
1068static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1069{
1070 struct fsl_esdhc *regs = priv->esdhc_regs;
1071 int timeout = 1000;
1072
1073#ifdef CONFIG_ESDHC_DETECT_QUIRK
1074 if (CONFIG_ESDHC_DETECT_QUIRK)
1075 return 1;
1076#endif
1077
1078#if CONFIG_IS_ENABLED(DM_MMC)
1079 if (priv->non_removable)
1080 return 1;
Fabio Estevam29230f32020-01-06 20:11:27 -03001081
1082 if (priv->broken_cd)
1083 return 1;
Simon Glassbcee8d62019-12-06 21:41:35 -07001084#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001085 if (dm_gpio_is_valid(&priv->cd_gpio))
1086 return dm_gpio_get_value(&priv->cd_gpio);
1087#endif
1088#endif
1089
1090 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1091 udelay(1000);
1092
1093 return timeout > 0;
1094}
1095
1096static int esdhc_reset(struct fsl_esdhc *regs)
1097{
1098 ulong start;
1099
1100 /* reset the controller */
1101 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1102
1103 /* hardware clears the bit when it is done */
1104 start = get_timer(0);
1105 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1106 if (get_timer(start) > 100) {
1107 printf("MMC/SD: Reset never completed.\n");
1108 return -ETIMEDOUT;
1109 }
1110 }
1111
1112 return 0;
1113}
1114
1115#if !CONFIG_IS_ENABLED(DM_MMC)
1116static int esdhc_getcd(struct mmc *mmc)
1117{
1118 struct fsl_esdhc_priv *priv = mmc->priv;
1119
1120 return esdhc_getcd_common(priv);
1121}
1122
1123static int esdhc_init(struct mmc *mmc)
1124{
1125 struct fsl_esdhc_priv *priv = mmc->priv;
1126
1127 return esdhc_init_common(priv, mmc);
1128}
1129
1130static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1131 struct mmc_data *data)
1132{
1133 struct fsl_esdhc_priv *priv = mmc->priv;
1134
1135 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1136}
1137
1138static int esdhc_set_ios(struct mmc *mmc)
1139{
1140 struct fsl_esdhc_priv *priv = mmc->priv;
1141
1142 return esdhc_set_ios_common(priv, mmc);
1143}
1144
1145static const struct mmc_ops esdhc_ops = {
1146 .getcd = esdhc_getcd,
1147 .init = esdhc_init,
1148 .send_cmd = esdhc_send_cmd,
1149 .set_ios = esdhc_set_ios,
1150};
1151#endif
1152
1153static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1154 struct fsl_esdhc_plat *plat)
1155{
1156 struct mmc_config *cfg;
1157 struct fsl_esdhc *regs;
1158 u32 caps, voltage_caps;
1159 int ret;
1160
1161 if (!priv)
1162 return -EINVAL;
1163
1164 regs = priv->esdhc_regs;
1165
1166 /* First reset the eSDHC controller */
1167 ret = esdhc_reset(regs);
1168 if (ret)
1169 return ret;
1170
1171#ifdef CONFIG_MCF5441x
1172 /* ColdFire, using SDHC_DATA[3] for card detection */
1173 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1174#endif
1175
1176#ifndef CONFIG_FSL_USDHC
1177 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1178 | SYSCTL_IPGEN | SYSCTL_CKEN);
1179 /* Clearing tuning bits in case ROM has set it already */
1180 esdhc_write32(&regs->mixctrl, 0);
1181 esdhc_write32(&regs->autoc12err, 0);
1182 esdhc_write32(&regs->clktunectrlstatus, 0);
1183#else
1184 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1185 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1186#endif
1187
1188 if (priv->vs18_enable)
1189 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1190
Haibo Chenc7f44182020-09-30 15:52:23 +08001191 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lufa33d202019-06-21 11:42:27 +08001192 cfg = &plat->cfg;
1193#ifndef CONFIG_DM_MMC
1194 memset(cfg, '\0', sizeof(*cfg));
1195#endif
1196
1197 voltage_caps = 0;
1198 caps = esdhc_read32(&regs->hostcapblt);
1199
1200#ifdef CONFIG_MCF5441x
1201 /*
1202 * MCF5441x RM declares in more points that sdhc clock speed must
1203 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1204 * from host capabilities.
1205 */
1206 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1207#endif
1208
1209#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1210 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1211 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1212#endif
1213
1214/* T4240 host controller capabilities register should have VS33 bit */
1215#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1216 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1217#endif
1218
1219 if (caps & ESDHC_HOSTCAPBLT_VS18)
1220 voltage_caps |= MMC_VDD_165_195;
1221 if (caps & ESDHC_HOSTCAPBLT_VS30)
1222 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1223 if (caps & ESDHC_HOSTCAPBLT_VS33)
1224 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1225
1226 cfg->name = "FSL_SDHC";
1227#if !CONFIG_IS_ENABLED(DM_MMC)
1228 cfg->ops = &esdhc_ops;
1229#endif
1230#ifdef CONFIG_SYS_SD_VOLTAGE
1231 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1232#else
1233 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1234#endif
1235 if ((cfg->voltages & voltage_caps) == 0) {
1236 printf("voltage not supported by controller\n");
1237 return -1;
1238 }
1239
1240 if (priv->bus_width == 8)
1241 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1242 else if (priv->bus_width == 4)
1243 cfg->host_caps = MMC_MODE_4BIT;
1244
1245 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1246#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1247 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1248#endif
1249
1250 if (priv->bus_width > 0) {
1251 if (priv->bus_width < 8)
1252 cfg->host_caps &= ~MMC_MODE_8BIT;
1253 if (priv->bus_width < 4)
1254 cfg->host_caps &= ~MMC_MODE_4BIT;
1255 }
1256
1257 if (caps & ESDHC_HOSTCAPBLT_HSS)
1258 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1259
1260#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1261 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1262 cfg->host_caps &= ~MMC_MODE_8BIT;
1263#endif
1264
1265 cfg->host_caps |= priv->caps;
1266
1267 cfg->f_min = 400000;
1268 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1269
1270 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1271
Haibo Chenc7f44182020-09-30 15:52:23 +08001272 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lufa33d202019-06-21 11:42:27 +08001273 if (priv->flags & ESDHC_FLAG_USDHC) {
1274 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chenc7f44182020-09-30 15:52:23 +08001275 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lufa33d202019-06-21 11:42:27 +08001276
1277 val |= ESDHC_STD_TUNING_EN;
1278 val &= ~ESDHC_TUNING_START_TAP_MASK;
1279 val |= priv->tuning_start_tap;
1280 val &= ~ESDHC_TUNING_STEP_MASK;
1281 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chenba616762020-06-22 19:38:04 +08001282
1283 /* Disable the CMD CRC check for tuning, if not, need to
1284 * add some delay after every tuning command, because
1285 * hardware standard tuning logic will directly go to next
1286 * step once it detect the CMD CRC error, will not wait for
1287 * the card side to finally send out the tuning data, trigger
1288 * the buffer read ready interrupt immediately. If usdhc send
1289 * the next tuning command some eMMC card will stuck, can't
1290 * response, block the tuning procedure or the first command
1291 * after the whole tuning procedure always can't get any response.
1292 */
1293 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chenc7f44182020-09-30 15:52:23 +08001294 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lufa33d202019-06-21 11:42:27 +08001295 }
1296 }
1297
1298 return 0;
1299}
1300
1301#if !CONFIG_IS_ENABLED(DM_MMC)
1302static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1303 struct fsl_esdhc_priv *priv)
1304{
1305 if (!cfg || !priv)
1306 return -EINVAL;
1307
1308 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1309 priv->bus_width = cfg->max_bus_width;
1310 priv->sdhc_clk = cfg->sdhc_clk;
1311 priv->wp_enable = cfg->wp_enable;
1312 priv->vs18_enable = cfg->vs18_enable;
1313
1314 return 0;
1315};
1316
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001317int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lufa33d202019-06-21 11:42:27 +08001318{
1319 struct fsl_esdhc_plat *plat;
1320 struct fsl_esdhc_priv *priv;
1321 struct mmc *mmc;
1322 int ret;
1323
1324 if (!cfg)
1325 return -EINVAL;
1326
1327 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1328 if (!priv)
1329 return -ENOMEM;
1330 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1331 if (!plat) {
1332 free(priv);
1333 return -ENOMEM;
1334 }
1335
1336 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1337 if (ret) {
1338 debug("%s xlate failure\n", __func__);
1339 free(plat);
1340 free(priv);
1341 return ret;
1342 }
1343
1344 ret = fsl_esdhc_init(priv, plat);
1345 if (ret) {
1346 debug("%s init failure\n", __func__);
1347 free(plat);
1348 free(priv);
1349 return ret;
1350 }
1351
1352 mmc = mmc_create(&plat->cfg, priv);
1353 if (!mmc)
1354 return -EIO;
1355
1356 priv->mmc = mmc;
1357
1358 return 0;
1359}
1360
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001361int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lufa33d202019-06-21 11:42:27 +08001362{
1363 struct fsl_esdhc_cfg *cfg;
1364
1365 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1366 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1367 cfg->sdhc_clk = gd->arch.sdhc_clk;
1368 return fsl_esdhc_initialize(bis, cfg);
1369}
1370#endif
1371
Yangbo Lufa33d202019-06-21 11:42:27 +08001372#ifdef CONFIG_OF_LIBFDT
1373__weak int esdhc_status_fixup(void *blob, const char *compat)
1374{
1375#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1376 if (!hwconfig("esdhc")) {
1377 do_fixup_by_compat(blob, compat, "status", "disabled",
1378 sizeof("disabled"), 1);
1379 return 1;
1380 }
1381#endif
1382 return 0;
1383}
1384
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001385void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lufa33d202019-06-21 11:42:27 +08001386{
1387 const char *compat = "fsl,esdhc";
1388
1389 if (esdhc_status_fixup(blob, compat))
1390 return;
1391
Yangbo Lufa33d202019-06-21 11:42:27 +08001392 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1393 gd->arch.sdhc_clk, 1);
Yangbo Lufa33d202019-06-21 11:42:27 +08001394}
1395#endif
1396
1397#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lufa33d202019-06-21 11:42:27 +08001398#include <asm/arch/clock.h>
Yangbo Lufa33d202019-06-21 11:42:27 +08001399__weak void init_clk_usdhc(u32 index)
1400{
1401}
1402
Walter Lozano23721772020-07-29 12:31:17 -03001403static int fsl_esdhc_ofdata_to_platdata(struct udevice *dev)
Yangbo Lufa33d202019-06-21 11:42:27 +08001404{
Walter Lozano23721772020-07-29 12:31:17 -03001405#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Yangbo Lufa33d202019-06-21 11:42:27 +08001406 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lufa33d202019-06-21 11:42:27 +08001407#if CONFIG_IS_ENABLED(DM_REGULATOR)
1408 struct udevice *vqmmc_dev;
Walter Lozano23721772020-07-29 12:31:17 -03001409 int ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001410#endif
Walter Lozano23721772020-07-29 12:31:17 -03001411 const void *fdt = gd->fdt_blob;
1412 int node = dev_of_offset(dev);
1413
Yangbo Lufa33d202019-06-21 11:42:27 +08001414 fdt_addr_t addr;
1415 unsigned int val;
Yangbo Lufa33d202019-06-21 11:42:27 +08001416
1417 addr = dev_read_addr(dev);
1418 if (addr == FDT_ADDR_T_NONE)
1419 return -EINVAL;
Yangbo Lufa33d202019-06-21 11:42:27 +08001420 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lufa33d202019-06-21 11:42:27 +08001421 priv->dev = dev;
1422 priv->mode = -1;
Yangbo Lufa33d202019-06-21 11:42:27 +08001423
1424 val = dev_read_u32_default(dev, "bus-width", -1);
1425 if (val == 8)
1426 priv->bus_width = 8;
1427 else if (val == 4)
1428 priv->bus_width = 4;
1429 else
1430 priv->bus_width = 1;
1431
1432 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1433 priv->tuning_step = val;
1434 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1435 ESDHC_TUNING_START_TAP_DEFAULT);
1436 priv->tuning_start_tap = val;
1437 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1438 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1439 priv->strobe_dll_delay_target = val;
1440
Fabio Estevam29230f32020-01-06 20:11:27 -03001441 if (dev_read_bool(dev, "broken-cd"))
1442 priv->broken_cd = 1;
1443
Yangbo Lufa33d202019-06-21 11:42:27 +08001444 if (dev_read_bool(dev, "non-removable")) {
1445 priv->non_removable = 1;
1446 } else {
1447 priv->non_removable = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -07001448#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001449 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1450 GPIOD_IS_IN);
1451#endif
1452 }
1453
1454 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1455 priv->wp_enable = 1;
1456 } else {
1457 priv->wp_enable = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -07001458#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lufa33d202019-06-21 11:42:27 +08001459 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1460 GPIOD_IS_IN);
1461#endif
1462 }
1463
1464 priv->vs18_enable = 0;
1465
1466#if CONFIG_IS_ENABLED(DM_REGULATOR)
1467 /*
1468 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1469 * otherwise, emmc will work abnormally.
1470 */
1471 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1472 if (ret) {
1473 dev_dbg(dev, "no vqmmc-supply\n");
1474 } else {
Marek Vasut406df852020-05-22 18:19:08 +02001475 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lufa33d202019-06-21 11:42:27 +08001476 ret = regulator_set_enable(vqmmc_dev, true);
1477 if (ret) {
1478 dev_err(dev, "fail to enable vqmmc-supply\n");
1479 return ret;
1480 }
1481
1482 if (regulator_get_value(vqmmc_dev) == 1800000)
1483 priv->vs18_enable = 1;
1484 }
1485#endif
Walter Lozano23721772020-07-29 12:31:17 -03001486#endif
1487 return 0;
1488}
1489
1490static int fsl_esdhc_probe(struct udevice *dev)
1491{
1492 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1493 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1494 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1495 struct esdhc_soc_data *data =
1496 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1497 struct mmc *mmc;
1498#if !CONFIG_IS_ENABLED(BLK)
1499 struct blk_desc *bdesc;
1500#endif
1501 int ret;
1502
1503#if CONFIG_IS_ENABLED(OF_PLATDATA)
1504 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1505 unsigned int val;
1506
1507 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1508 val = plat->dtplat.bus_width;
1509 if (val == 8)
1510 priv->bus_width = 8;
1511 else if (val == 4)
1512 priv->bus_width = 4;
1513 else
1514 priv->bus_width = 1;
Walter Lozano7142ff92020-07-29 12:31:19 -03001515
1516 if (dtplat->non_removable)
1517 priv->non_removable = 1;
1518 else
1519 priv->non_removable = 0;
1520
1521 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1522 struct udevice *gpiodev;
Walter Lozano7142ff92020-07-29 12:31:19 -03001523
Simon Glass8a38abf2020-10-03 11:31:40 -06001524 ret = device_get_by_driver_info_idx(dtplat->cd_gpios->idx,
1525 &gpiodev);
Walter Lozano7142ff92020-07-29 12:31:19 -03001526 if (ret)
1527 return ret;
1528
1529 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1530 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1531 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1532
1533 if (ret)
1534 return ret;
1535 }
Walter Lozano23721772020-07-29 12:31:17 -03001536#endif
1537
1538 if (data)
1539 priv->flags = data->flags;
Yangbo Lufa33d202019-06-21 11:42:27 +08001540
Yangbo Lufa33d202019-06-21 11:42:27 +08001541 /*
1542 * TODO:
1543 * Because lack of clk driver, if SDHC clk is not enabled,
1544 * need to enable it first before this driver is invoked.
1545 *
1546 * we use MXC_ESDHC_CLK to get clk freq.
1547 * If one would like to make this function work,
1548 * the aliases should be provided in dts as this:
1549 *
1550 * aliases {
1551 * mmc0 = &usdhc1;
1552 * mmc1 = &usdhc2;
1553 * mmc2 = &usdhc3;
1554 * mmc3 = &usdhc4;
1555 * };
1556 * Then if your board only supports mmc2 and mmc3, but we can
1557 * correctly get the seq as 2 and 3, then let mxc_get_clock
1558 * work as expected.
1559 */
1560
1561 init_clk_usdhc(dev->seq);
1562
Giulio Benettia820bed2020-01-10 15:51:45 +01001563#if CONFIG_IS_ENABLED(CLK)
1564 /* Assigned clock already set clock */
1565 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1566 if (ret) {
1567 printf("Failed to get per_clk\n");
1568 return ret;
Yangbo Lufa33d202019-06-21 11:42:27 +08001569 }
Giulio Benettia820bed2020-01-10 15:51:45 +01001570 ret = clk_enable(&priv->per_clk);
1571 if (ret) {
1572 printf("Failed to enable per_clk\n");
1573 return ret;
1574 }
1575
1576 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1577#else
1578 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1579 if (priv->sdhc_clk <= 0) {
1580 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1581 return -EINVAL;
1582 }
1583#endif
Yangbo Lufa33d202019-06-21 11:42:27 +08001584
1585 ret = fsl_esdhc_init(priv, plat);
1586 if (ret) {
1587 dev_err(dev, "fsl_esdhc_init failure\n");
1588 return ret;
1589 }
1590
Walter Lozano23721772020-07-29 12:31:17 -03001591#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Peng Fanb0155ac2019-07-10 09:35:24 +00001592 ret = mmc_of_parse(dev, &plat->cfg);
1593 if (ret)
1594 return ret;
Walter Lozano23721772020-07-29 12:31:17 -03001595#endif
Peng Fanb0155ac2019-07-10 09:35:24 +00001596
Yangbo Lufa33d202019-06-21 11:42:27 +08001597 mmc = &plat->mmc;
1598 mmc->cfg = &plat->cfg;
1599 mmc->dev = dev;
1600#if !CONFIG_IS_ENABLED(BLK)
1601 mmc->priv = priv;
1602
1603 /* Setup dsr related values */
1604 mmc->dsr_imp = 0;
1605 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1606 /* Setup the universal parts of the block interface just once */
1607 bdesc = mmc_get_blk_desc(mmc);
1608 bdesc->if_type = IF_TYPE_MMC;
1609 bdesc->removable = 1;
1610 bdesc->devnum = mmc_get_next_devnum();
1611 bdesc->block_read = mmc_bread;
1612 bdesc->block_write = mmc_bwrite;
1613 bdesc->block_erase = mmc_berase;
1614
1615 /* setup initial part type */
1616 bdesc->part_type = mmc->cfg->part_type;
1617 mmc_list_add(mmc);
1618#endif
1619
1620 upriv->mmc = mmc;
1621
1622 return esdhc_init_common(priv, mmc);
1623}
1624
1625#if CONFIG_IS_ENABLED(DM_MMC)
1626static int fsl_esdhc_get_cd(struct udevice *dev)
1627{
1628 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1629
1630 return esdhc_getcd_common(priv);
1631}
1632
1633static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1634 struct mmc_data *data)
1635{
1636 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1637 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1638
1639 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1640}
1641
1642static int fsl_esdhc_set_ios(struct udevice *dev)
1643{
1644 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1645 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1646
1647 return esdhc_set_ios_common(priv, &plat->mmc);
1648}
1649
Peng Fane9c22552019-07-10 09:35:26 +00001650#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1651static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1652{
1653 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1654 struct fsl_esdhc *regs = priv->esdhc_regs;
1655 u32 m;
1656
Haibo Chenc7f44182020-09-30 15:52:23 +08001657 m = esdhc_read32(&regs->mixctrl);
Peng Fane9c22552019-07-10 09:35:26 +00001658 m |= MIX_CTRL_HS400_ES;
Haibo Chenc7f44182020-09-30 15:52:23 +08001659 esdhc_write32(&regs->mixctrl, m);
Peng Fane9c22552019-07-10 09:35:26 +00001660
1661 return 0;
1662}
1663#endif
1664
Haibo Chenb5874b52020-11-05 14:57:13 +08001665static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1666 int timeout_us)
1667{
1668 int ret;
1669 u32 tmp;
1670 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1671 struct fsl_esdhc *regs = priv->esdhc_regs;
1672
1673 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1674 !!(tmp & PRSSTAT_DAT0) == !!state,
1675 timeout_us);
1676 return ret;
1677}
1678
Yangbo Lufa33d202019-06-21 11:42:27 +08001679static const struct dm_mmc_ops fsl_esdhc_ops = {
1680 .get_cd = fsl_esdhc_get_cd,
1681 .send_cmd = fsl_esdhc_send_cmd,
1682 .set_ios = fsl_esdhc_set_ios,
1683#ifdef MMC_SUPPORTS_TUNING
1684 .execute_tuning = fsl_esdhc_execute_tuning,
1685#endif
Peng Fane9c22552019-07-10 09:35:26 +00001686#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1687 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1688#endif
Haibo Chenb5874b52020-11-05 14:57:13 +08001689 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lufa33d202019-06-21 11:42:27 +08001690};
1691#endif
1692
1693static struct esdhc_soc_data usdhc_imx7d_data = {
1694 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1695 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1696 | ESDHC_FLAG_HS400,
Yangbo Lufa33d202019-06-21 11:42:27 +08001697};
1698
Peng Fan609ba122019-07-10 09:35:28 +00001699static struct esdhc_soc_data usdhc_imx8qm_data = {
1700 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1701 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1702 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1703};
1704
Yangbo Lufa33d202019-06-21 11:42:27 +08001705static const struct udevice_id fsl_esdhc_ids[] = {
1706 { .compatible = "fsl,imx53-esdhc", },
1707 { .compatible = "fsl,imx6ul-usdhc", },
1708 { .compatible = "fsl,imx6sx-usdhc", },
1709 { .compatible = "fsl,imx6sl-usdhc", },
1710 { .compatible = "fsl,imx6q-usdhc", },
1711 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1712 { .compatible = "fsl,imx7ulp-usdhc", },
Peng Fan609ba122019-07-10 09:35:28 +00001713 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fanf65d0842019-11-04 17:31:17 +08001714 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1715 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1716 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti6a63a872020-01-10 15:51:46 +01001717 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lufa33d202019-06-21 11:42:27 +08001718 { .compatible = "fsl,esdhc", },
1719 { /* sentinel */ }
1720};
1721
1722#if CONFIG_IS_ENABLED(BLK)
1723static int fsl_esdhc_bind(struct udevice *dev)
1724{
1725 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1726
1727 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1728}
1729#endif
1730
1731U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano45154f02020-07-29 12:31:16 -03001732 .name = "fsl_esdhc",
Yangbo Lufa33d202019-06-21 11:42:27 +08001733 .id = UCLASS_MMC,
1734 .of_match = fsl_esdhc_ids,
Walter Lozano23721772020-07-29 12:31:17 -03001735 .ofdata_to_platdata = fsl_esdhc_ofdata_to_platdata,
Yangbo Lufa33d202019-06-21 11:42:27 +08001736 .ops = &fsl_esdhc_ops,
1737#if CONFIG_IS_ENABLED(BLK)
1738 .bind = fsl_esdhc_bind,
1739#endif
1740 .probe = fsl_esdhc_probe,
1741 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
1742 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1743};
Walter Lozano23721772020-07-29 12:31:17 -03001744
1745U_BOOT_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lufa33d202019-06-21 11:42:27 +08001746#endif