blob: 09a5cd61e3bee6fb8ff904c03162a0c07227ea70 [file] [log] [blame]
Yangbo Lu982f4252019-06-21 11:42:27 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
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 Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080018#include <errno.h>
19#include <hwconfig.h>
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080021#include <mmc.h>
22#include <part.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070028#include <linux/err.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080029#include <power/regulator.h>
30#include <malloc.h>
31#include <fsl_esdhc_imx.h>
32#include <fdt_support.h>
33#include <asm/io.h>
34#include <dm.h>
35#include <asm-generic/gpio.h>
36#include <dm/pinctrl.h>
Walter Lozano8aff6732020-07-29 12:31:17 -030037#include <dt-structs.h>
38#include <mapmem.h>
39#include <dm/ofnode.h>
Haibo Chene6a999b2020-09-01 15:34:06 +080040#include <linux/iopoll.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080041
42#if !CONFIG_IS_ENABLED(BLK)
43#include "mmc_private.h"
44#endif
45
Haibo Chene8801ac2021-02-19 11:25:32 -080046#ifndef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
47#ifdef CONFIG_FSL_USDHC
48#define ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 1
49#endif
50#endif
51
Yangbo Lu982f4252019-06-21 11:42:27 +080052DECLARE_GLOBAL_DATA_PTR;
53
54#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
55 IRQSTATEN_CINT | \
56 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
57 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
58 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
59 IRQSTATEN_DINT)
60#define MAX_TUNING_LOOP 40
61#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
62
63struct fsl_esdhc {
64 uint dsaddr; /* SDMA system address register */
65 uint blkattr; /* Block attributes register */
66 uint cmdarg; /* Command argument register */
67 uint xfertyp; /* Transfer type register */
68 uint cmdrsp0; /* Command response 0 register */
69 uint cmdrsp1; /* Command response 1 register */
70 uint cmdrsp2; /* Command response 2 register */
71 uint cmdrsp3; /* Command response 3 register */
72 uint datport; /* Buffer data port register */
73 uint prsstat; /* Present state register */
74 uint proctl; /* Protocol control register */
75 uint sysctl; /* System Control Register */
76 uint irqstat; /* Interrupt status register */
77 uint irqstaten; /* Interrupt status enable register */
78 uint irqsigen; /* Interrupt signal enable register */
79 uint autoc12err; /* Auto CMD error status register */
80 uint hostcapblt; /* Host controller capabilities register */
81 uint wml; /* Watermark level register */
82 uint mixctrl; /* For USDHC */
83 char reserved1[4]; /* reserved */
84 uint fevt; /* Force event register */
85 uint admaes; /* ADMA error status register */
86 uint adsaddr; /* ADMA system address register */
87 char reserved2[4];
88 uint dllctrl;
89 uint dllstat;
90 uint clktunectrlstatus;
91 char reserved3[4];
92 uint strobe_dllctrl;
93 uint strobe_dllstat;
94 char reserved4[72];
95 uint vendorspec;
96 uint mmcboot;
97 uint vendorspec2;
Giulio Benetti65b5ec12020-01-10 15:51:46 +010098 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lu982f4252019-06-21 11:42:27 +080099 char reserved5[44];
100 uint hostver; /* Host controller version register */
101 char reserved6[4]; /* reserved */
102 uint dmaerraddr; /* DMA error address register */
103 char reserved7[4]; /* reserved */
104 uint dmaerrattr; /* DMA error attribute register */
105 char reserved8[4]; /* reserved */
106 uint hostcapblt2; /* Host controller capabilities register 2 */
107 char reserved9[8]; /* reserved */
108 uint tcr; /* Tuning control register */
109 char reserved10[28]; /* reserved */
110 uint sddirctl; /* SD direction control register */
111 char reserved11[712];/* reserved */
112 uint scr; /* eSDHC control register */
113};
114
115struct fsl_esdhc_plat {
Walter Lozano8aff6732020-07-29 12:31:17 -0300116#if CONFIG_IS_ENABLED(OF_PLATDATA)
117 /* Put this first since driver model will copy the data here */
118 struct dtd_fsl_esdhc dtplat;
119#endif
120
Yangbo Lu982f4252019-06-21 11:42:27 +0800121 struct mmc_config cfg;
122 struct mmc mmc;
123};
124
125struct esdhc_soc_data {
126 u32 flags;
Yangbo Lu982f4252019-06-21 11:42:27 +0800127};
128
129/**
130 * struct fsl_esdhc_priv
131 *
132 * @esdhc_regs: registers of the sdhc controller
133 * @sdhc_clk: Current clk of the sdhc controller
134 * @bus_width: bus width, 1bit, 4bit or 8bit
135 * @cfg: mmc config
136 * @mmc: mmc
137 * Following is used when Driver Model is enabled for MMC
138 * @dev: pointer for the device
139 * @non_removable: 0: removable; 1: non-removable
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300140 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lu982f4252019-06-21 11:42:27 +0800141 * @wp_enable: 1: enable checking wp; 0: no check
142 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
143 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
144 * @caps: controller capabilities
145 * @tuning_step: tuning step setting in tuning_ctrl register
146 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
147 * @strobe_dll_delay_target: settings in strobe_dllctrl
148 * @signal_voltage: indicating the current voltage
149 * @cd_gpio: gpio for card detection
150 * @wp_gpio: gpio for write protection
151 */
152struct fsl_esdhc_priv {
153 struct fsl_esdhc *esdhc_regs;
154 unsigned int sdhc_clk;
155 struct clk per_clk;
156 unsigned int clock;
157 unsigned int mode;
158 unsigned int bus_width;
159#if !CONFIG_IS_ENABLED(BLK)
160 struct mmc *mmc;
161#endif
162 struct udevice *dev;
163 int non_removable;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300164 int broken_cd;
Yangbo Lu982f4252019-06-21 11:42:27 +0800165 int wp_enable;
166 int vs18_enable;
167 u32 flags;
168 u32 caps;
169 u32 tuning_step;
170 u32 tuning_start_tap;
171 u32 strobe_dll_delay_target;
172 u32 signal_voltage;
Ye Li7aa20fd2019-07-11 03:29:02 +0000173#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800174 struct udevice *vqmmc_dev;
175 struct udevice *vmmc_dev;
176#endif
Simon Glassfa4689a2019-12-06 21:41:35 -0700177#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +0800178 struct gpio_desc cd_gpio;
179 struct gpio_desc wp_gpio;
180#endif
181};
182
183/* Return the XFERTYP flags for a given command and data packet */
184static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
185{
186 uint xfertyp = 0;
187
188 if (data) {
189 xfertyp |= XFERTYP_DPSEL;
190#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
191 xfertyp |= XFERTYP_DMAEN;
192#endif
193 if (data->blocks > 1) {
194 xfertyp |= XFERTYP_MSBSEL;
195 xfertyp |= XFERTYP_BCEN;
196#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
197 xfertyp |= XFERTYP_AC12EN;
198#endif
199 }
200
201 if (data->flags & MMC_DATA_READ)
202 xfertyp |= XFERTYP_DTDSEL;
203 }
204
205 if (cmd->resp_type & MMC_RSP_CRC)
206 xfertyp |= XFERTYP_CCCEN;
207 if (cmd->resp_type & MMC_RSP_OPCODE)
208 xfertyp |= XFERTYP_CICEN;
209 if (cmd->resp_type & MMC_RSP_136)
210 xfertyp |= XFERTYP_RSPTYP_136;
211 else if (cmd->resp_type & MMC_RSP_BUSY)
212 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
213 else if (cmd->resp_type & MMC_RSP_PRESENT)
214 xfertyp |= XFERTYP_RSPTYP_48;
215
216 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
217 xfertyp |= XFERTYP_CMDTYP_ABORT;
218
219 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
220}
221
222#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
223/*
224 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
225 */
226static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
227 struct mmc_data *data)
228{
229 struct fsl_esdhc *regs = priv->esdhc_regs;
230 uint blocks;
231 char *buffer;
232 uint databuf;
233 uint size;
234 uint irqstat;
235 ulong start;
236
237 if (data->flags & MMC_DATA_READ) {
238 blocks = data->blocks;
239 buffer = data->dest;
240 while (blocks) {
241 start = get_timer(0);
242 size = data->blocksize;
243 irqstat = esdhc_read32(&regs->irqstat);
244 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
245 if (get_timer(start) > PIO_TIMEOUT) {
246 printf("\nData Read Failed in PIO Mode.");
247 return;
248 }
249 }
250 while (size && (!(irqstat & IRQSTAT_TC))) {
251 udelay(100); /* Wait before last byte transfer complete */
252 irqstat = esdhc_read32(&regs->irqstat);
253 databuf = in_le32(&regs->datport);
254 *((uint *)buffer) = databuf;
255 buffer += 4;
256 size -= 4;
257 }
258 blocks--;
259 }
260 } else {
261 blocks = data->blocks;
262 buffer = (char *)data->src;
263 while (blocks) {
264 start = get_timer(0);
265 size = data->blocksize;
266 irqstat = esdhc_read32(&regs->irqstat);
267 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
268 if (get_timer(start) > PIO_TIMEOUT) {
269 printf("\nData Write Failed in PIO Mode.");
270 return;
271 }
272 }
273 while (size && (!(irqstat & IRQSTAT_TC))) {
274 udelay(100); /* Wait before last byte transfer complete */
275 databuf = *((uint *)buffer);
276 buffer += 4;
277 size -= 4;
278 irqstat = esdhc_read32(&regs->irqstat);
279 out_le32(&regs->datport, databuf);
280 }
281 blocks--;
282 }
283 }
284}
285#endif
286
287static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
288 struct mmc_data *data)
289{
290 int timeout;
291 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu16b38542019-06-21 11:42:30 +0800292#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800293 dma_addr_t addr;
294#endif
295 uint wml_value;
296
297 wml_value = data->blocksize/4;
298
299 if (data->flags & MMC_DATA_READ) {
300 if (wml_value > WML_RD_WML_MAX)
301 wml_value = WML_RD_WML_MAX_VAL;
302
303 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
304#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu16b38542019-06-21 11:42:30 +0800305#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800306 addr = virt_to_phys((void *)(data->dest));
307 if (upper_32_bits(addr))
308 printf("Error found for upper 32 bits\n");
309 else
310 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
311#else
312 esdhc_write32(&regs->dsaddr, (u32)data->dest);
313#endif
314#endif
315 } else {
316#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
317 flush_dcache_range((ulong)data->src,
318 (ulong)data->src+data->blocks
319 *data->blocksize);
320#endif
321 if (wml_value > WML_WR_WML_MAX)
322 wml_value = WML_WR_WML_MAX_VAL;
323 if (priv->wp_enable) {
324 if ((esdhc_read32(&regs->prsstat) &
325 PRSSTAT_WPSPL) == 0) {
326 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
327 return -ETIMEDOUT;
328 }
329 } else {
Simon Glassfa4689a2019-12-06 21:41:35 -0700330#if CONFIG_IS_ENABLED(DM_GPIO)
331 if (dm_gpio_is_valid(&priv->wp_gpio) &&
332 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800333 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
334 return -ETIMEDOUT;
335 }
336#endif
337 }
338
339 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
340 wml_value << 16);
341#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu16b38542019-06-21 11:42:30 +0800342#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800343 addr = virt_to_phys((void *)(data->src));
344 if (upper_32_bits(addr))
345 printf("Error found for upper 32 bits\n");
346 else
347 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
348#else
349 esdhc_write32(&regs->dsaddr, (u32)data->src);
350#endif
351#endif
352 }
353
354 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
355
356 /* Calculate the timeout period for data transactions */
357 /*
358 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
359 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
360 * So, Number of SD Clock cycles for 0.25sec should be minimum
361 * (SD Clock/sec * 0.25 sec) SD Clock cycles
362 * = (mmc->clock * 1/4) SD Clock cycles
363 * As 1) >= 2)
364 * => (2^(timeout+13)) >= mmc->clock * 1/4
365 * Taking log2 both the sides
366 * => timeout + 13 >= log2(mmc->clock/4)
367 * Rounding up to next power of 2
368 * => timeout + 13 = log2(mmc->clock/4) + 1
369 * => timeout + 13 = fls(mmc->clock/4)
370 *
371 * However, the MMC spec "It is strongly recommended for hosts to
372 * implement more than 500ms timeout value even if the card
373 * indicates the 250ms maximum busy length." Even the previous
374 * value of 300ms is known to be insufficient for some cards.
375 * So, we use
376 * => timeout + 13 = fls(mmc->clock/2)
377 */
378 timeout = fls(mmc->clock/2);
379 timeout -= 13;
380
381 if (timeout > 14)
382 timeout = 14;
383
384 if (timeout < 0)
385 timeout = 0;
386
387#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
388 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
389 timeout++;
390#endif
391
392#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
393 timeout = 0xE;
394#endif
395 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
396
397 return 0;
398}
399
400static void check_and_invalidate_dcache_range
401 (struct mmc_cmd *cmd,
402 struct mmc_data *data) {
403 unsigned start = 0;
404 unsigned end = 0;
405 unsigned size = roundup(ARCH_DMA_MINALIGN,
406 data->blocks*data->blocksize);
Yangbo Lu16b38542019-06-21 11:42:30 +0800407#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800408 dma_addr_t addr;
409
410 addr = virt_to_phys((void *)(data->dest));
411 if (upper_32_bits(addr))
412 printf("Error found for upper 32 bits\n");
413 else
414 start = lower_32_bits(addr);
415#else
416 start = (unsigned)data->dest;
417#endif
418 end = start + size;
419 invalidate_dcache_range(start, end);
420}
421
422#ifdef CONFIG_MCF5441x
423/*
424 * Swaps 32-bit words to little-endian byte order.
425 */
426static inline void sd_swap_dma_buff(struct mmc_data *data)
427{
428 int i, size = data->blocksize >> 2;
429 u32 *buffer = (u32 *)data->dest;
430 u32 sw;
431
432 while (data->blocks--) {
433 for (i = 0; i < size; i++) {
434 sw = __sw32(*buffer);
435 *buffer++ = sw;
436 }
437 }
438}
439#endif
440
441/*
442 * Sends a command out on the bus. Takes the mmc pointer,
443 * a command pointer, and an optional data pointer.
444 */
445static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
446 struct mmc_cmd *cmd, struct mmc_data *data)
447{
448 int err = 0;
449 uint xfertyp;
450 uint irqstat;
451 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
452 struct fsl_esdhc *regs = priv->esdhc_regs;
453 unsigned long start;
454
455#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
456 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
457 return 0;
458#endif
459
460 esdhc_write32(&regs->irqstat, -1);
461
462 sync();
463
464 /* Wait for the bus to be idle */
465 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
466 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
467 ;
468
469 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
470 ;
471
Yangbo Lu982f4252019-06-21 11:42:27 +0800472 /* Set up for a data transfer if we have one */
473 if (data) {
474 err = esdhc_setup_data(priv, mmc, data);
475 if(err)
476 return err;
477
478 if (data->flags & MMC_DATA_READ)
479 check_and_invalidate_dcache_range(cmd, data);
480 }
481
482 /* Figure out the transfer arguments */
483 xfertyp = esdhc_xfertyp(cmd, data);
484
485 /* Mask all irqs */
486 esdhc_write32(&regs->irqsigen, 0);
487
488 /* Send the command */
489 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
490#if defined(CONFIG_FSL_USDHC)
491 esdhc_write32(&regs->mixctrl,
492 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
493 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
494 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
495#else
496 esdhc_write32(&regs->xfertyp, xfertyp);
497#endif
498
499 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
500 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
501 flags = IRQSTAT_BRR;
502
503 /* Wait for the command to complete */
504 start = get_timer(0);
505 while (!(esdhc_read32(&regs->irqstat) & flags)) {
506 if (get_timer(start) > 1000) {
507 err = -ETIMEDOUT;
508 goto out;
509 }
510 }
511
512 irqstat = esdhc_read32(&regs->irqstat);
513
514 if (irqstat & CMD_ERR) {
515 err = -ECOMM;
516 goto out;
517 }
518
519 if (irqstat & IRQSTAT_CTOE) {
520 err = -ETIMEDOUT;
521 goto out;
522 }
523
524 /* Switch voltage to 1.8V if CMD11 succeeded */
525 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
526 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
527
528 printf("Run CMD11 1.8V switch\n");
529 /* Sleep for 5 ms - max time for card to switch to 1.8V */
530 udelay(5000);
531 }
532
533 /* Workaround for ESDHC errata ENGcm03648 */
534 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan3dbea592019-07-10 09:35:30 +0000535 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800536
Peng Fan3dbea592019-07-10 09:35:30 +0000537 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-06-21 11:42:27 +0800538 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
539 PRSSTAT_DAT0)) {
540 udelay(100);
541 timeout--;
542 }
543
544 if (timeout <= 0) {
545 printf("Timeout waiting for DAT0 to go high!\n");
546 err = -ETIMEDOUT;
547 goto out;
548 }
549 }
550
551 /* Copy the response to the response buffer */
552 if (cmd->resp_type & MMC_RSP_136) {
553 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
554
555 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
556 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
557 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
558 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
559 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
560 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
561 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
562 cmd->response[3] = (cmdrsp0 << 8);
563 } else
564 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
565
566 /* Wait until all of the blocks are transferred */
567 if (data) {
568#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
569 esdhc_pio_read_write(priv, data);
570#else
571 flags = DATA_COMPLETE;
572 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
573 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
574 flags = IRQSTAT_BRR;
575 }
576
577 do {
578 irqstat = esdhc_read32(&regs->irqstat);
579
580 if (irqstat & IRQSTAT_DTOE) {
581 err = -ETIMEDOUT;
582 goto out;
583 }
584
585 if (irqstat & DATA_ERR) {
586 err = -ECOMM;
587 goto out;
588 }
589 } while ((irqstat & flags) != flags);
590
591 /*
592 * Need invalidate the dcache here again to avoid any
593 * cache-fill during the DMA operations such as the
594 * speculative pre-fetching etc.
595 */
596 if (data->flags & MMC_DATA_READ) {
597 check_and_invalidate_dcache_range(cmd, data);
598#ifdef CONFIG_MCF5441x
599 sd_swap_dma_buff(data);
600#endif
601 }
602#endif
603 }
604
605out:
606 /* Reset CMD and DATA portions on error */
607 if (err) {
608 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
609 SYSCTL_RSTC);
610 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
611 ;
612
613 if (data) {
614 esdhc_write32(&regs->sysctl,
615 esdhc_read32(&regs->sysctl) |
616 SYSCTL_RSTD);
617 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
618 ;
619 }
620
621 /* If this was CMD11, then notify that power cycle is needed */
622 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
623 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
624 }
625
626 esdhc_write32(&regs->irqstat, -1);
627
628 return err;
629}
630
631static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
632{
633 struct fsl_esdhc *regs = priv->esdhc_regs;
634 int div = 1;
Haibo Chene6a999b2020-09-01 15:34:06 +0800635 u32 tmp;
636 int ret;
Yangbo Lu982f4252019-06-21 11:42:27 +0800637#ifdef ARCH_MXC
638#ifdef CONFIG_MX53
639 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
640 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
641#else
642 int pre_div = 1;
643#endif
644#else
645 int pre_div = 2;
646#endif
647 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
648 int sdhc_clk = priv->sdhc_clk;
649 uint clk;
650
Yangbo Lu982f4252019-06-21 11:42:27 +0800651 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
652 pre_div *= 2;
653
654 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
655 div++;
656
657 pre_div >>= 1;
658 div -= 1;
659
660 clk = (pre_div << 8) | (div << 4);
661
662#ifdef CONFIG_FSL_USDHC
663 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
664#else
665 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
666#endif
667
668 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
669
Haibo Chene6a999b2020-09-01 15:34:06 +0800670 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
671 if (ret)
672 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
Yangbo Lu982f4252019-06-21 11:42:27 +0800673
674#ifdef CONFIG_FSL_USDHC
675 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
676#else
677 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
678#endif
679
680 priv->clock = clock;
681}
682
Yangbo Lu982f4252019-06-21 11:42:27 +0800683#ifdef MMC_SUPPORTS_TUNING
684static int esdhc_change_pinstate(struct udevice *dev)
685{
686 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
687 int ret;
688
689 switch (priv->mode) {
690 case UHS_SDR50:
691 case UHS_DDR50:
692 ret = pinctrl_select_state(dev, "state_100mhz");
693 break;
694 case UHS_SDR104:
695 case MMC_HS_200:
696 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000697 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800698 ret = pinctrl_select_state(dev, "state_200mhz");
699 break;
700 default:
701 ret = pinctrl_select_state(dev, "default");
702 break;
703 }
704
705 if (ret)
706 printf("%s %d error\n", __func__, priv->mode);
707
708 return ret;
709}
710
711static void esdhc_reset_tuning(struct mmc *mmc)
712{
713 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
714 struct fsl_esdhc *regs = priv->esdhc_regs;
715
716 if (priv->flags & ESDHC_FLAG_USDHC) {
717 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
718 esdhc_clrbits32(&regs->autoc12err,
719 MIX_CTRL_SMPCLK_SEL |
720 MIX_CTRL_EXE_TUNE);
721 }
722 }
723}
724
725static void esdhc_set_strobe_dll(struct mmc *mmc)
726{
727 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
728 struct fsl_esdhc *regs = priv->esdhc_regs;
729 u32 val;
730
731 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
Haibo Chen920f5d02020-09-30 15:52:23 +0800732 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Yangbo Lu982f4252019-06-21 11:42:27 +0800733
734 /*
735 * enable strobe dll ctrl and adjust the delay target
736 * for the uSDHC loopback read clock
737 */
738 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
739 (priv->strobe_dll_delay_target <<
740 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
Haibo Chen920f5d02020-09-30 15:52:23 +0800741 esdhc_write32(&regs->strobe_dllctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +0800742 /* wait 1us to make sure strobe dll status register stable */
743 mdelay(1);
Haibo Chen920f5d02020-09-30 15:52:23 +0800744 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lu982f4252019-06-21 11:42:27 +0800745 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
746 pr_warn("HS400 strobe DLL status REF not lock!\n");
747 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
748 pr_warn("HS400 strobe DLL status SLV not lock!\n");
749 }
750}
751
752static int esdhc_set_timing(struct mmc *mmc)
753{
754 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
755 struct fsl_esdhc *regs = priv->esdhc_regs;
756 u32 mixctrl;
757
Haibo Chen920f5d02020-09-30 15:52:23 +0800758 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800759 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
760
761 switch (mmc->selected_mode) {
762 case MMC_LEGACY:
Yangbo Lu982f4252019-06-21 11:42:27 +0800763 esdhc_reset_tuning(mmc);
Haibo Chen920f5d02020-09-30 15:52:23 +0800764 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800765 break;
766 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000767 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800768 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800769 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800770 break;
771 case MMC_HS:
772 case MMC_HS_52:
773 case MMC_HS_200:
774 case SD_HS:
775 case UHS_SDR12:
776 case UHS_SDR25:
777 case UHS_SDR50:
778 case UHS_SDR104:
Haibo Chen920f5d02020-09-30 15:52:23 +0800779 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800780 break;
781 case UHS_DDR50:
782 case MMC_DDR_52:
783 mixctrl |= MIX_CTRL_DDREN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800784 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800785 break;
786 default:
787 printf("Not supported %d\n", mmc->selected_mode);
788 return -EINVAL;
789 }
790
791 priv->mode = mmc->selected_mode;
792
793 return esdhc_change_pinstate(mmc->dev);
794}
795
796static int esdhc_set_voltage(struct mmc *mmc)
797{
798 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
799 struct fsl_esdhc *regs = priv->esdhc_regs;
Heiko Schocher7989f602021-01-15 10:37:09 +0100800#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800801 int ret;
Heiko Schocher7989f602021-01-15 10:37:09 +0100802#endif
Yangbo Lu982f4252019-06-21 11:42:27 +0800803
804 priv->signal_voltage = mmc->signal_voltage;
805 switch (mmc->signal_voltage) {
806 case MMC_SIGNAL_VOLTAGE_330:
807 if (priv->vs18_enable)
Marek Vasutba79fed2020-05-22 18:28:33 +0200808 return -ENOTSUPP;
Yangbo Lu982f4252019-06-21 11:42:27 +0800809#if CONFIG_IS_ENABLED(DM_REGULATOR)
810 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
811 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
812 if (ret) {
813 printf("Setting to 3.3V error");
814 return -EIO;
815 }
816 /* Wait for 5ms */
817 mdelay(5);
818 }
819#endif
820
821 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
822 if (!(esdhc_read32(&regs->vendorspec) &
823 ESDHC_VENDORSPEC_VSELECT))
824 return 0;
825
826 return -EAGAIN;
827 case MMC_SIGNAL_VOLTAGE_180:
828#if CONFIG_IS_ENABLED(DM_REGULATOR)
829 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
830 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
831 if (ret) {
832 printf("Setting to 1.8V error");
833 return -EIO;
834 }
835 }
836#endif
837 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
838 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
839 return 0;
840
841 return -EAGAIN;
842 case MMC_SIGNAL_VOLTAGE_120:
843 return -ENOTSUPP;
844 default:
845 return 0;
846 }
847}
848
849static void esdhc_stop_tuning(struct mmc *mmc)
850{
851 struct mmc_cmd cmd;
852
853 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
854 cmd.cmdarg = 0;
855 cmd.resp_type = MMC_RSP_R1b;
856
857 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
858}
859
860static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
861{
Simon Glassfa20e932020-12-03 16:55:20 -0700862 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +0800863 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
864 struct fsl_esdhc *regs = priv->esdhc_regs;
865 struct mmc *mmc = &plat->mmc;
Haibo Chen920f5d02020-09-30 15:52:23 +0800866 u32 irqstaten = esdhc_read32(&regs->irqstaten);
867 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lu982f4252019-06-21 11:42:27 +0800868 int i, ret = -ETIMEDOUT;
869 u32 val, mixctrl;
870
871 /* clock tuning is not needed for upto 52MHz */
872 if (mmc->clock <= 52000000)
873 return 0;
874
875 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
876 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +0800877 val = esdhc_read32(&regs->autoc12err);
878 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800879 val &= ~MIX_CTRL_SMPCLK_SEL;
880 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
881
882 val |= MIX_CTRL_EXE_TUNE;
883 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
884
Haibo Chen920f5d02020-09-30 15:52:23 +0800885 esdhc_write32(&regs->autoc12err, val);
886 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800887 }
888
889 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chen920f5d02020-09-30 15:52:23 +0800890 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800891 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800892 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800893
Haibo Chen920f5d02020-09-30 15:52:23 +0800894 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
895 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lu982f4252019-06-21 11:42:27 +0800896
897 /*
898 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
899 * of loops reaches 40 times.
900 */
901 for (i = 0; i < MAX_TUNING_LOOP; i++) {
902 u32 ctrl;
903
904 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
905 if (mmc->bus_width == 8)
Haibo Chen920f5d02020-09-30 15:52:23 +0800906 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lu982f4252019-06-21 11:42:27 +0800907 else if (mmc->bus_width == 4)
Haibo Chen920f5d02020-09-30 15:52:23 +0800908 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800909 } else {
Haibo Chen920f5d02020-09-30 15:52:23 +0800910 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800911 }
912
913 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chen920f5d02020-09-30 15:52:23 +0800914 val = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800915 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800916 esdhc_write32(&regs->mixctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +0800917
918 /* We are using STD tuning, no need to check return value */
919 mmc_send_tuning(mmc, opcode, NULL);
920
Haibo Chen920f5d02020-09-30 15:52:23 +0800921 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lu982f4252019-06-21 11:42:27 +0800922 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
923 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800924 ret = 0;
925 break;
926 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800927 }
928
Haibo Chen920f5d02020-09-30 15:52:23 +0800929 esdhc_write32(&regs->irqstaten, irqstaten);
930 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lu982f4252019-06-21 11:42:27 +0800931
932 esdhc_stop_tuning(mmc);
933
934 return ret;
935}
936#endif
937
938static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
939{
940 struct fsl_esdhc *regs = priv->esdhc_regs;
941 int ret __maybe_unused;
Peng Fan2cdf52b2019-11-04 17:14:15 +0800942 u32 clock;
Yangbo Lu982f4252019-06-21 11:42:27 +0800943
Haibo Cheneaa2f102020-11-03 17:18:35 +0800944#ifdef MMC_SUPPORTS_TUNING
945 /*
946 * call esdhc_set_timing() before update the clock rate,
947 * This is because current we support DDR and SDR mode,
948 * Once the DDR_EN bit is set, the card clock will be
949 * divide by 2 automatically. So need to do this before
950 * setting clock rate.
951 */
952 if (priv->mode != mmc->selected_mode) {
953 ret = esdhc_set_timing(mmc);
954 if (ret) {
955 printf("esdhc_set_timing error %d\n", ret);
956 return ret;
957 }
958 }
959#endif
960
Yangbo Lu982f4252019-06-21 11:42:27 +0800961 /* Set the clock speed */
Peng Fan2cdf52b2019-11-04 17:14:15 +0800962 clock = mmc->clock;
963 if (clock < mmc->cfg->f_min)
964 clock = mmc->cfg->f_min;
965
966 if (priv->clock != clock)
967 set_sysctl(priv, mmc, clock);
Yangbo Lu982f4252019-06-21 11:42:27 +0800968
969#ifdef MMC_SUPPORTS_TUNING
970 if (mmc->clk_disable) {
971#ifdef CONFIG_FSL_USDHC
972 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
973#else
974 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
975#endif
976 } else {
977#ifdef CONFIG_FSL_USDHC
978 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
979 VENDORSPEC_CKEN);
980#else
981 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
982#endif
983 }
984
Haibo Cheneaa2f102020-11-03 17:18:35 +0800985 /*
986 * For HS400/HS400ES mode, make sure set the strobe dll in the
987 * target clock rate. So call esdhc_set_strobe_dll() after the
988 * clock updated.
989 */
990 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
991 esdhc_set_strobe_dll(mmc);
Yangbo Lu982f4252019-06-21 11:42:27 +0800992
993 if (priv->signal_voltage != mmc->signal_voltage) {
994 ret = esdhc_set_voltage(mmc);
995 if (ret) {
Marek Vasutba79fed2020-05-22 18:28:33 +0200996 if (ret != -ENOTSUPP)
997 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lu982f4252019-06-21 11:42:27 +0800998 return ret;
999 }
1000 }
1001#endif
1002
1003 /* Set the bus width */
1004 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
1005
1006 if (mmc->bus_width == 4)
1007 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
1008 else if (mmc->bus_width == 8)
1009 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1010
1011 return 0;
1012}
1013
1014static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1015{
1016 struct fsl_esdhc *regs = priv->esdhc_regs;
1017 ulong start;
1018
1019 /* Reset the entire host controller */
1020 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1021
1022 /* Wait until the controller is available */
1023 start = get_timer(0);
1024 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1025 if (get_timer(start) > 1000)
1026 return -ETIMEDOUT;
1027 }
1028
1029#if defined(CONFIG_FSL_USDHC)
1030 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1031 esdhc_write32(&regs->mmcboot, 0x0);
1032 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1033 esdhc_write32(&regs->mixctrl, 0x0);
1034 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1035
1036 /* Put VEND_SPEC to default value */
1037 if (priv->vs18_enable)
1038 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1039 ESDHC_VENDORSPEC_VSELECT));
1040 else
1041 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1042
1043 /* Disable DLL_CTRL delay line */
1044 esdhc_write32(&regs->dllctrl, 0x0);
1045#endif
1046
1047#ifndef ARCH_MXC
1048 /* Enable cache snooping */
1049 esdhc_write32(&regs->scr, 0x00000040);
1050#endif
1051
1052#ifndef CONFIG_FSL_USDHC
1053 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1054#else
1055 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1056#endif
1057
1058 /* Set the initial clock speed */
1059 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1060
1061 /* Disable the BRR and BWR bits in IRQSTAT */
1062 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1063
1064#ifdef CONFIG_MCF5441x
1065 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1066#else
1067 /* Put the PROCTL reg back to the default */
1068 esdhc_write32(&regs->proctl, PROCTL_INIT);
1069#endif
1070
1071 /* Set timout to the maximum value */
1072 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1073
1074 return 0;
1075}
1076
1077static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1078{
1079 struct fsl_esdhc *regs = priv->esdhc_regs;
1080 int timeout = 1000;
1081
1082#ifdef CONFIG_ESDHC_DETECT_QUIRK
1083 if (CONFIG_ESDHC_DETECT_QUIRK)
1084 return 1;
1085#endif
1086
1087#if CONFIG_IS_ENABLED(DM_MMC)
1088 if (priv->non_removable)
1089 return 1;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001090
1091 if (priv->broken_cd)
1092 return 1;
Simon Glassfa4689a2019-12-06 21:41:35 -07001093#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001094 if (dm_gpio_is_valid(&priv->cd_gpio))
1095 return dm_gpio_get_value(&priv->cd_gpio);
1096#endif
1097#endif
1098
1099 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1100 udelay(1000);
1101
1102 return timeout > 0;
1103}
1104
1105static int esdhc_reset(struct fsl_esdhc *regs)
1106{
1107 ulong start;
1108
1109 /* reset the controller */
1110 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1111
1112 /* hardware clears the bit when it is done */
1113 start = get_timer(0);
1114 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1115 if (get_timer(start) > 100) {
1116 printf("MMC/SD: Reset never completed.\n");
1117 return -ETIMEDOUT;
1118 }
1119 }
1120
1121 return 0;
1122}
1123
1124#if !CONFIG_IS_ENABLED(DM_MMC)
1125static int esdhc_getcd(struct mmc *mmc)
1126{
1127 struct fsl_esdhc_priv *priv = mmc->priv;
1128
1129 return esdhc_getcd_common(priv);
1130}
1131
1132static int esdhc_init(struct mmc *mmc)
1133{
1134 struct fsl_esdhc_priv *priv = mmc->priv;
1135
1136 return esdhc_init_common(priv, mmc);
1137}
1138
1139static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1140 struct mmc_data *data)
1141{
1142 struct fsl_esdhc_priv *priv = mmc->priv;
1143
1144 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1145}
1146
1147static int esdhc_set_ios(struct mmc *mmc)
1148{
1149 struct fsl_esdhc_priv *priv = mmc->priv;
1150
1151 return esdhc_set_ios_common(priv, mmc);
1152}
1153
1154static const struct mmc_ops esdhc_ops = {
1155 .getcd = esdhc_getcd,
1156 .init = esdhc_init,
1157 .send_cmd = esdhc_send_cmd,
1158 .set_ios = esdhc_set_ios,
1159};
1160#endif
1161
1162static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1163 struct fsl_esdhc_plat *plat)
1164{
1165 struct mmc_config *cfg;
1166 struct fsl_esdhc *regs;
1167 u32 caps, voltage_caps;
1168 int ret;
1169
1170 if (!priv)
1171 return -EINVAL;
1172
1173 regs = priv->esdhc_regs;
1174
1175 /* First reset the eSDHC controller */
1176 ret = esdhc_reset(regs);
1177 if (ret)
1178 return ret;
1179
1180#ifdef CONFIG_MCF5441x
1181 /* ColdFire, using SDHC_DATA[3] for card detection */
1182 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1183#endif
1184
1185#ifndef CONFIG_FSL_USDHC
1186 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1187 | SYSCTL_IPGEN | SYSCTL_CKEN);
1188 /* Clearing tuning bits in case ROM has set it already */
1189 esdhc_write32(&regs->mixctrl, 0);
1190 esdhc_write32(&regs->autoc12err, 0);
1191 esdhc_write32(&regs->clktunectrlstatus, 0);
1192#else
1193 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1194 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1195#endif
1196
1197 if (priv->vs18_enable)
1198 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1199
Haibo Chen920f5d02020-09-30 15:52:23 +08001200 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lu982f4252019-06-21 11:42:27 +08001201 cfg = &plat->cfg;
1202#ifndef CONFIG_DM_MMC
1203 memset(cfg, '\0', sizeof(*cfg));
1204#endif
1205
1206 voltage_caps = 0;
1207 caps = esdhc_read32(&regs->hostcapblt);
1208
1209#ifdef CONFIG_MCF5441x
1210 /*
1211 * MCF5441x RM declares in more points that sdhc clock speed must
1212 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1213 * from host capabilities.
1214 */
1215 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1216#endif
1217
1218#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1219 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1220 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1221#endif
1222
1223/* T4240 host controller capabilities register should have VS33 bit */
1224#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1225 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1226#endif
1227
1228 if (caps & ESDHC_HOSTCAPBLT_VS18)
1229 voltage_caps |= MMC_VDD_165_195;
1230 if (caps & ESDHC_HOSTCAPBLT_VS30)
1231 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1232 if (caps & ESDHC_HOSTCAPBLT_VS33)
1233 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1234
1235 cfg->name = "FSL_SDHC";
1236#if !CONFIG_IS_ENABLED(DM_MMC)
1237 cfg->ops = &esdhc_ops;
1238#endif
1239#ifdef CONFIG_SYS_SD_VOLTAGE
1240 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1241#else
1242 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1243#endif
1244 if ((cfg->voltages & voltage_caps) == 0) {
1245 printf("voltage not supported by controller\n");
1246 return -1;
1247 }
1248
1249 if (priv->bus_width == 8)
1250 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1251 else if (priv->bus_width == 4)
1252 cfg->host_caps = MMC_MODE_4BIT;
1253
1254 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1255#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1256 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1257#endif
1258
1259 if (priv->bus_width > 0) {
1260 if (priv->bus_width < 8)
1261 cfg->host_caps &= ~MMC_MODE_8BIT;
1262 if (priv->bus_width < 4)
1263 cfg->host_caps &= ~MMC_MODE_4BIT;
1264 }
1265
1266 if (caps & ESDHC_HOSTCAPBLT_HSS)
1267 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1268
1269#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1270 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1271 cfg->host_caps &= ~MMC_MODE_8BIT;
1272#endif
1273
1274 cfg->host_caps |= priv->caps;
1275
1276 cfg->f_min = 400000;
1277 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1278
1279 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1280
Haibo Chen920f5d02020-09-30 15:52:23 +08001281 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lu982f4252019-06-21 11:42:27 +08001282 if (priv->flags & ESDHC_FLAG_USDHC) {
1283 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +08001284 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +08001285
1286 val |= ESDHC_STD_TUNING_EN;
1287 val &= ~ESDHC_TUNING_START_TAP_MASK;
1288 val |= priv->tuning_start_tap;
1289 val &= ~ESDHC_TUNING_STEP_MASK;
1290 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chen43162c32020-06-22 19:38:04 +08001291
1292 /* Disable the CMD CRC check for tuning, if not, need to
1293 * add some delay after every tuning command, because
1294 * hardware standard tuning logic will directly go to next
1295 * step once it detect the CMD CRC error, will not wait for
1296 * the card side to finally send out the tuning data, trigger
1297 * the buffer read ready interrupt immediately. If usdhc send
1298 * the next tuning command some eMMC card will stuck, can't
1299 * response, block the tuning procedure or the first command
1300 * after the whole tuning procedure always can't get any response.
1301 */
1302 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chen920f5d02020-09-30 15:52:23 +08001303 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +08001304 }
1305 }
1306
1307 return 0;
1308}
1309
1310#if !CONFIG_IS_ENABLED(DM_MMC)
1311static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1312 struct fsl_esdhc_priv *priv)
1313{
1314 if (!cfg || !priv)
1315 return -EINVAL;
1316
1317 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1318 priv->bus_width = cfg->max_bus_width;
1319 priv->sdhc_clk = cfg->sdhc_clk;
1320 priv->wp_enable = cfg->wp_enable;
1321 priv->vs18_enable = cfg->vs18_enable;
1322
1323 return 0;
1324};
1325
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001326int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu982f4252019-06-21 11:42:27 +08001327{
1328 struct fsl_esdhc_plat *plat;
1329 struct fsl_esdhc_priv *priv;
1330 struct mmc *mmc;
1331 int ret;
1332
1333 if (!cfg)
1334 return -EINVAL;
1335
1336 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1337 if (!priv)
1338 return -ENOMEM;
1339 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1340 if (!plat) {
1341 free(priv);
1342 return -ENOMEM;
1343 }
1344
1345 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1346 if (ret) {
1347 debug("%s xlate failure\n", __func__);
1348 free(plat);
1349 free(priv);
1350 return ret;
1351 }
1352
1353 ret = fsl_esdhc_init(priv, plat);
1354 if (ret) {
1355 debug("%s init failure\n", __func__);
1356 free(plat);
1357 free(priv);
1358 return ret;
1359 }
1360
1361 mmc = mmc_create(&plat->cfg, priv);
1362 if (!mmc)
1363 return -EIO;
1364
1365 priv->mmc = mmc;
1366
1367 return 0;
1368}
1369
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001370int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu982f4252019-06-21 11:42:27 +08001371{
1372 struct fsl_esdhc_cfg *cfg;
1373
1374 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1375 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1376 cfg->sdhc_clk = gd->arch.sdhc_clk;
1377 return fsl_esdhc_initialize(bis, cfg);
1378}
1379#endif
1380
Yangbo Lu982f4252019-06-21 11:42:27 +08001381#ifdef CONFIG_OF_LIBFDT
1382__weak int esdhc_status_fixup(void *blob, const char *compat)
1383{
1384#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1385 if (!hwconfig("esdhc")) {
1386 do_fixup_by_compat(blob, compat, "status", "disabled",
1387 sizeof("disabled"), 1);
1388 return 1;
1389 }
1390#endif
1391 return 0;
1392}
1393
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001394void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lu982f4252019-06-21 11:42:27 +08001395{
1396 const char *compat = "fsl,esdhc";
1397
1398 if (esdhc_status_fixup(blob, compat))
1399 return;
1400
Yangbo Lu982f4252019-06-21 11:42:27 +08001401 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1402 gd->arch.sdhc_clk, 1);
Yangbo Lu982f4252019-06-21 11:42:27 +08001403}
1404#endif
1405
1406#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +08001407#include <asm/arch/clock.h>
Yangbo Lu982f4252019-06-21 11:42:27 +08001408__weak void init_clk_usdhc(u32 index)
1409{
1410}
1411
Simon Glassaad29ae2020-12-03 16:55:21 -07001412static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lu982f4252019-06-21 11:42:27 +08001413{
Walter Lozano8aff6732020-07-29 12:31:17 -03001414#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Yangbo Lu982f4252019-06-21 11:42:27 +08001415 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001416#if CONFIG_IS_ENABLED(DM_REGULATOR)
1417 struct udevice *vqmmc_dev;
Walter Lozano8aff6732020-07-29 12:31:17 -03001418 int ret;
Yangbo Lu982f4252019-06-21 11:42:27 +08001419#endif
Walter Lozano8aff6732020-07-29 12:31:17 -03001420 const void *fdt = gd->fdt_blob;
1421 int node = dev_of_offset(dev);
1422
Yangbo Lu982f4252019-06-21 11:42:27 +08001423 fdt_addr_t addr;
1424 unsigned int val;
Yangbo Lu982f4252019-06-21 11:42:27 +08001425
1426 addr = dev_read_addr(dev);
1427 if (addr == FDT_ADDR_T_NONE)
1428 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001429 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lu982f4252019-06-21 11:42:27 +08001430 priv->dev = dev;
1431 priv->mode = -1;
Yangbo Lu982f4252019-06-21 11:42:27 +08001432
1433 val = dev_read_u32_default(dev, "bus-width", -1);
1434 if (val == 8)
1435 priv->bus_width = 8;
1436 else if (val == 4)
1437 priv->bus_width = 4;
1438 else
1439 priv->bus_width = 1;
1440
1441 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1442 priv->tuning_step = val;
1443 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1444 ESDHC_TUNING_START_TAP_DEFAULT);
1445 priv->tuning_start_tap = val;
1446 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1447 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1448 priv->strobe_dll_delay_target = val;
1449
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001450 if (dev_read_bool(dev, "broken-cd"))
1451 priv->broken_cd = 1;
1452
Yangbo Lu982f4252019-06-21 11:42:27 +08001453 if (dev_read_bool(dev, "non-removable")) {
1454 priv->non_removable = 1;
1455 } else {
1456 priv->non_removable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001457#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001458 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1459 GPIOD_IS_IN);
1460#endif
1461 }
1462
1463 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1464 priv->wp_enable = 1;
1465 } else {
1466 priv->wp_enable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001467#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001468 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1469 GPIOD_IS_IN);
1470#endif
1471 }
1472
1473 priv->vs18_enable = 0;
1474
1475#if CONFIG_IS_ENABLED(DM_REGULATOR)
1476 /*
1477 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1478 * otherwise, emmc will work abnormally.
1479 */
1480 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1481 if (ret) {
1482 dev_dbg(dev, "no vqmmc-supply\n");
1483 } else {
Marek Vasut34e67f92020-05-22 18:19:08 +02001484 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lu982f4252019-06-21 11:42:27 +08001485 ret = regulator_set_enable(vqmmc_dev, true);
1486 if (ret) {
1487 dev_err(dev, "fail to enable vqmmc-supply\n");
1488 return ret;
1489 }
1490
1491 if (regulator_get_value(vqmmc_dev) == 1800000)
1492 priv->vs18_enable = 1;
1493 }
1494#endif
Walter Lozano8aff6732020-07-29 12:31:17 -03001495#endif
1496 return 0;
1497}
1498
1499static int fsl_esdhc_probe(struct udevice *dev)
1500{
1501 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001502 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano8aff6732020-07-29 12:31:17 -03001503 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1504 struct esdhc_soc_data *data =
1505 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1506 struct mmc *mmc;
1507#if !CONFIG_IS_ENABLED(BLK)
1508 struct blk_desc *bdesc;
1509#endif
1510 int ret;
1511
1512#if CONFIG_IS_ENABLED(OF_PLATDATA)
1513 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1514 unsigned int val;
1515
1516 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1517 val = plat->dtplat.bus_width;
1518 if (val == 8)
1519 priv->bus_width = 8;
1520 else if (val == 4)
1521 priv->bus_width = 4;
1522 else
1523 priv->bus_width = 1;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001524
1525 if (dtplat->non_removable)
1526 priv->non_removable = 1;
1527 else
1528 priv->non_removable = 0;
1529
1530 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1531 struct udevice *gpiodev;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001532
Simon Glass0000e0d2021-03-15 17:25:28 +13001533 ret = device_get_by_ofplat_idx(dtplat->cd_gpios->idx, &gpiodev);
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001534 if (ret)
1535 return ret;
1536
1537 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1538 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1539 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1540
1541 if (ret)
1542 return ret;
1543 }
Walter Lozano8aff6732020-07-29 12:31:17 -03001544#endif
1545
1546 if (data)
1547 priv->flags = data->flags;
Yangbo Lu982f4252019-06-21 11:42:27 +08001548
Yangbo Lu982f4252019-06-21 11:42:27 +08001549 /*
1550 * TODO:
1551 * Because lack of clk driver, if SDHC clk is not enabled,
1552 * need to enable it first before this driver is invoked.
1553 *
1554 * we use MXC_ESDHC_CLK to get clk freq.
1555 * If one would like to make this function work,
1556 * the aliases should be provided in dts as this:
1557 *
1558 * aliases {
1559 * mmc0 = &usdhc1;
1560 * mmc1 = &usdhc2;
1561 * mmc2 = &usdhc3;
1562 * mmc3 = &usdhc4;
1563 * };
1564 * Then if your board only supports mmc2 and mmc3, but we can
1565 * correctly get the seq as 2 and 3, then let mxc_get_clock
1566 * work as expected.
1567 */
1568
Simon Glass75e534b2020-12-16 21:20:07 -07001569 init_clk_usdhc(dev_seq(dev));
Yangbo Lu982f4252019-06-21 11:42:27 +08001570
Giulio Benettidbdbc632020-01-10 15:51:45 +01001571#if CONFIG_IS_ENABLED(CLK)
1572 /* Assigned clock already set clock */
1573 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1574 if (ret) {
1575 printf("Failed to get per_clk\n");
1576 return ret;
1577 }
1578 ret = clk_enable(&priv->per_clk);
1579 if (ret) {
1580 printf("Failed to enable per_clk\n");
1581 return ret;
1582 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001583
Giulio Benettidbdbc632020-01-10 15:51:45 +01001584 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1585#else
Simon Glass75e534b2020-12-16 21:20:07 -07001586 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettidbdbc632020-01-10 15:51:45 +01001587 if (priv->sdhc_clk <= 0) {
1588 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1589 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001590 }
Giulio Benettidbdbc632020-01-10 15:51:45 +01001591#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001592
1593 ret = fsl_esdhc_init(priv, plat);
1594 if (ret) {
1595 dev_err(dev, "fsl_esdhc_init failure\n");
1596 return ret;
1597 }
1598
Walter Lozano8aff6732020-07-29 12:31:17 -03001599#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Peng Fan3766a482019-07-10 09:35:24 +00001600 ret = mmc_of_parse(dev, &plat->cfg);
1601 if (ret)
1602 return ret;
Walter Lozano8aff6732020-07-29 12:31:17 -03001603#endif
Peng Fan3766a482019-07-10 09:35:24 +00001604
Yangbo Lu982f4252019-06-21 11:42:27 +08001605 mmc = &plat->mmc;
1606 mmc->cfg = &plat->cfg;
1607 mmc->dev = dev;
1608#if !CONFIG_IS_ENABLED(BLK)
1609 mmc->priv = priv;
1610
1611 /* Setup dsr related values */
1612 mmc->dsr_imp = 0;
1613 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1614 /* Setup the universal parts of the block interface just once */
1615 bdesc = mmc_get_blk_desc(mmc);
1616 bdesc->if_type = IF_TYPE_MMC;
1617 bdesc->removable = 1;
1618 bdesc->devnum = mmc_get_next_devnum();
1619 bdesc->block_read = mmc_bread;
1620 bdesc->block_write = mmc_bwrite;
1621 bdesc->block_erase = mmc_berase;
1622
1623 /* setup initial part type */
1624 bdesc->part_type = mmc->cfg->part_type;
1625 mmc_list_add(mmc);
1626#endif
1627
1628 upriv->mmc = mmc;
1629
1630 return esdhc_init_common(priv, mmc);
1631}
1632
1633#if CONFIG_IS_ENABLED(DM_MMC)
1634static int fsl_esdhc_get_cd(struct udevice *dev)
1635{
1636 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1637
1638 return esdhc_getcd_common(priv);
1639}
1640
1641static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1642 struct mmc_data *data)
1643{
Simon Glassfa20e932020-12-03 16:55:20 -07001644 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001645 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1646
1647 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1648}
1649
1650static int fsl_esdhc_set_ios(struct udevice *dev)
1651{
Simon Glassfa20e932020-12-03 16:55:20 -07001652 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001653 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1654
1655 return esdhc_set_ios_common(priv, &plat->mmc);
1656}
1657
Peng Fan69b9d3a2019-07-10 09:35:26 +00001658#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1659static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1660{
1661 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1662 struct fsl_esdhc *regs = priv->esdhc_regs;
1663 u32 m;
1664
Haibo Chen920f5d02020-09-30 15:52:23 +08001665 m = esdhc_read32(&regs->mixctrl);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001666 m |= MIX_CTRL_HS400_ES;
Haibo Chen920f5d02020-09-30 15:52:23 +08001667 esdhc_write32(&regs->mixctrl, m);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001668
1669 return 0;
1670}
1671#endif
1672
Haibo Chencb78f212020-11-05 14:57:13 +08001673static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1674 int timeout_us)
1675{
1676 int ret;
1677 u32 tmp;
1678 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1679 struct fsl_esdhc *regs = priv->esdhc_regs;
1680
1681 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1682 !!(tmp & PRSSTAT_DAT0) == !!state,
1683 timeout_us);
1684 return ret;
1685}
1686
Yangbo Lu982f4252019-06-21 11:42:27 +08001687static const struct dm_mmc_ops fsl_esdhc_ops = {
1688 .get_cd = fsl_esdhc_get_cd,
1689 .send_cmd = fsl_esdhc_send_cmd,
1690 .set_ios = fsl_esdhc_set_ios,
1691#ifdef MMC_SUPPORTS_TUNING
1692 .execute_tuning = fsl_esdhc_execute_tuning,
1693#endif
Peng Fan69b9d3a2019-07-10 09:35:26 +00001694#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1695 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1696#endif
Haibo Chencb78f212020-11-05 14:57:13 +08001697 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lu982f4252019-06-21 11:42:27 +08001698};
1699#endif
1700
1701static struct esdhc_soc_data usdhc_imx7d_data = {
1702 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1703 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1704 | ESDHC_FLAG_HS400,
Yangbo Lu982f4252019-06-21 11:42:27 +08001705};
1706
Peng Fan457fe962019-07-10 09:35:28 +00001707static struct esdhc_soc_data usdhc_imx8qm_data = {
1708 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1709 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1710 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1711};
1712
Yangbo Lu982f4252019-06-21 11:42:27 +08001713static const struct udevice_id fsl_esdhc_ids[] = {
Fabio Estevameaf8ccc2021-02-15 08:58:15 -03001714 { .compatible = "fsl,imx51-esdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001715 { .compatible = "fsl,imx53-esdhc", },
1716 { .compatible = "fsl,imx6ul-usdhc", },
1717 { .compatible = "fsl,imx6sx-usdhc", },
1718 { .compatible = "fsl,imx6sl-usdhc", },
1719 { .compatible = "fsl,imx6q-usdhc", },
1720 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1721 { .compatible = "fsl,imx7ulp-usdhc", },
Peng Fan457fe962019-07-10 09:35:28 +00001722 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fand7689fa2019-11-04 17:31:17 +08001723 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1724 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1725 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti65b5ec12020-01-10 15:51:46 +01001726 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001727 { .compatible = "fsl,esdhc", },
1728 { /* sentinel */ }
1729};
1730
1731#if CONFIG_IS_ENABLED(BLK)
1732static int fsl_esdhc_bind(struct udevice *dev)
1733{
Simon Glassfa20e932020-12-03 16:55:20 -07001734 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001735
1736 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1737}
1738#endif
1739
1740U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano904b0be2020-07-29 12:31:16 -03001741 .name = "fsl_esdhc",
Yangbo Lu982f4252019-06-21 11:42:27 +08001742 .id = UCLASS_MMC,
1743 .of_match = fsl_esdhc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001744 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lu982f4252019-06-21 11:42:27 +08001745 .ops = &fsl_esdhc_ops,
1746#if CONFIG_IS_ENABLED(BLK)
1747 .bind = fsl_esdhc_bind,
1748#endif
1749 .probe = fsl_esdhc_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -07001750 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001751 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lu982f4252019-06-21 11:42:27 +08001752};
Walter Lozano8aff6732020-07-29 12:31:17 -03001753
Simon Glassdf65db82020-12-28 20:34:57 -07001754DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lu982f4252019-06-21 11:42:27 +08001755#endif