blob: 8ac859797f043095f33ce728c8415c2c86e38a6b [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 Glass9bc15642020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <linux/err.h>
Yangbo Lu982f4252019-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 Lozano8aff6732020-07-29 12:31:17 -030036#include <dt-structs.h>
37#include <mapmem.h>
38#include <dm/ofnode.h>
Haibo Chene6a999b2020-09-01 15:34:06 +080039#include <linux/iopoll.h>
Yangbo Lu982f4252019-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 Benetti65b5ec12020-01-10 15:51:46 +010091 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lu982f4252019-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 Lozano8aff6732020-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 Lu982f4252019-06-21 11:42:27 +0800114 struct mmc_config cfg;
115 struct mmc mmc;
116};
117
118struct esdhc_soc_data {
119 u32 flags;
Yangbo Lu982f4252019-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 Estevam7e3d8a92020-01-06 20:11:27 -0300133 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lu982f4252019-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 Estevam7e3d8a92020-01-06 20:11:27 -0300157 int broken_cd;
Yangbo Lu982f4252019-06-21 11:42:27 +0800158 int wp_enable;
159 int vs18_enable;
160 u32 flags;
161 u32 caps;
162 u32 tuning_step;
163 u32 tuning_start_tap;
164 u32 strobe_dll_delay_target;
165 u32 signal_voltage;
Ye Li7aa20fd2019-07-11 03:29:02 +0000166#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800167 struct udevice *vqmmc_dev;
168 struct udevice *vmmc_dev;
169#endif
Simon Glassfa4689a2019-12-06 21:41:35 -0700170#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-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 Lu16b38542019-06-21 11:42:30 +0800285#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-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 Lu16b38542019-06-21 11:42:30 +0800298#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-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 Glassfa4689a2019-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 Lu982f4252019-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 Lu16b38542019-06-21 11:42:30 +0800335#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-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 Lu16b38542019-06-21 11:42:30 +0800400#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-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 Lu982f4252019-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 Fan3dbea592019-07-10 09:35:30 +0000528 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800529
Peng Fan3dbea592019-07-10 09:35:30 +0000530 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-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 Chene6a999b2020-09-01 15:34:06 +0800628 u32 tmp;
629 int ret;
Yangbo Lu982f4252019-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 Lu982f4252019-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 Chene6a999b2020-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 Lu982f4252019-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 Lu982f4252019-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 Fan69b9d3a2019-07-10 09:35:26 +0000690 case MMC_HS_400_ES:
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800725 esdhc_write32(&regs->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800734 esdhc_write32(&regs->strobe_dllctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +0800735 /* wait 1us to make sure strobe dll status register stable */
736 mdelay(1);
Haibo Chen920f5d02020-09-30 15:52:23 +0800737 val = esdhc_read32(&regs->strobe_dllstat);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800751 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-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 Lu982f4252019-06-21 11:42:27 +0800756 esdhc_reset_tuning(mmc);
Haibo Chen920f5d02020-09-30 15:52:23 +0800757 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800758 break;
759 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000760 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800761 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800762 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-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 Chen920f5d02020-09-30 15:52:23 +0800772 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800773 break;
774 case UHS_DDR50:
775 case MMC_DDR_52:
776 mixctrl |= MIX_CTRL_DDREN;
Haibo Chen920f5d02020-09-30 15:52:23 +0800777 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-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;
Heiko Schocher7989f602021-01-15 10:37:09 +0100793#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800794 int ret;
Heiko Schocher7989f602021-01-15 10:37:09 +0100795#endif
Yangbo Lu982f4252019-06-21 11:42:27 +0800796
797 priv->signal_voltage = mmc->signal_voltage;
798 switch (mmc->signal_voltage) {
799 case MMC_SIGNAL_VOLTAGE_330:
800 if (priv->vs18_enable)
Marek Vasutba79fed2020-05-22 18:28:33 +0200801 return -ENOTSUPP;
Yangbo Lu982f4252019-06-21 11:42:27 +0800802#if CONFIG_IS_ENABLED(DM_REGULATOR)
803 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
804 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
805 if (ret) {
806 printf("Setting to 3.3V error");
807 return -EIO;
808 }
809 /* Wait for 5ms */
810 mdelay(5);
811 }
812#endif
813
814 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
815 if (!(esdhc_read32(&regs->vendorspec) &
816 ESDHC_VENDORSPEC_VSELECT))
817 return 0;
818
819 return -EAGAIN;
820 case MMC_SIGNAL_VOLTAGE_180:
821#if CONFIG_IS_ENABLED(DM_REGULATOR)
822 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
823 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
824 if (ret) {
825 printf("Setting to 1.8V error");
826 return -EIO;
827 }
828 }
829#endif
830 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
831 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
832 return 0;
833
834 return -EAGAIN;
835 case MMC_SIGNAL_VOLTAGE_120:
836 return -ENOTSUPP;
837 default:
838 return 0;
839 }
840}
841
842static void esdhc_stop_tuning(struct mmc *mmc)
843{
844 struct mmc_cmd cmd;
845
846 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
847 cmd.cmdarg = 0;
848 cmd.resp_type = MMC_RSP_R1b;
849
850 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
851}
852
853static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
854{
Simon Glassfa20e932020-12-03 16:55:20 -0700855 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +0800856 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
857 struct fsl_esdhc *regs = priv->esdhc_regs;
858 struct mmc *mmc = &plat->mmc;
Haibo Chen920f5d02020-09-30 15:52:23 +0800859 u32 irqstaten = esdhc_read32(&regs->irqstaten);
860 u32 irqsigen = esdhc_read32(&regs->irqsigen);
Yangbo Lu982f4252019-06-21 11:42:27 +0800861 int i, ret = -ETIMEDOUT;
862 u32 val, mixctrl;
863
864 /* clock tuning is not needed for upto 52MHz */
865 if (mmc->clock <= 52000000)
866 return 0;
867
868 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
869 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +0800870 val = esdhc_read32(&regs->autoc12err);
871 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800872 val &= ~MIX_CTRL_SMPCLK_SEL;
873 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
874
875 val |= MIX_CTRL_EXE_TUNE;
876 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
877
Haibo Chen920f5d02020-09-30 15:52:23 +0800878 esdhc_write32(&regs->autoc12err, val);
879 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800880 }
881
882 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
Haibo Chen920f5d02020-09-30 15:52:23 +0800883 mixctrl = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800884 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800885 esdhc_write32(&regs->mixctrl, mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800886
Haibo Chen920f5d02020-09-30 15:52:23 +0800887 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
888 esdhc_write32(&regs->irqsigen, IRQSTATEN_BRR);
Yangbo Lu982f4252019-06-21 11:42:27 +0800889
890 /*
891 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
892 * of loops reaches 40 times.
893 */
894 for (i = 0; i < MAX_TUNING_LOOP; i++) {
895 u32 ctrl;
896
897 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
898 if (mmc->bus_width == 8)
Haibo Chen920f5d02020-09-30 15:52:23 +0800899 esdhc_write32(&regs->blkattr, 0x7080);
Yangbo Lu982f4252019-06-21 11:42:27 +0800900 else if (mmc->bus_width == 4)
Haibo Chen920f5d02020-09-30 15:52:23 +0800901 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800902 } else {
Haibo Chen920f5d02020-09-30 15:52:23 +0800903 esdhc_write32(&regs->blkattr, 0x7040);
Yangbo Lu982f4252019-06-21 11:42:27 +0800904 }
905
906 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
Haibo Chen920f5d02020-09-30 15:52:23 +0800907 val = esdhc_read32(&regs->mixctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +0800908 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
Haibo Chen920f5d02020-09-30 15:52:23 +0800909 esdhc_write32(&regs->mixctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +0800910
911 /* We are using STD tuning, no need to check return value */
912 mmc_send_tuning(mmc, opcode, NULL);
913
Haibo Chen920f5d02020-09-30 15:52:23 +0800914 ctrl = esdhc_read32(&regs->autoc12err);
Yangbo Lu982f4252019-06-21 11:42:27 +0800915 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
916 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800917 ret = 0;
918 break;
919 }
Yangbo Lu982f4252019-06-21 11:42:27 +0800920 }
921
Haibo Chen920f5d02020-09-30 15:52:23 +0800922 esdhc_write32(&regs->irqstaten, irqstaten);
923 esdhc_write32(&regs->irqsigen, irqsigen);
Yangbo Lu982f4252019-06-21 11:42:27 +0800924
925 esdhc_stop_tuning(mmc);
926
927 return ret;
928}
929#endif
930
931static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
932{
933 struct fsl_esdhc *regs = priv->esdhc_regs;
934 int ret __maybe_unused;
Peng Fan2cdf52b2019-11-04 17:14:15 +0800935 u32 clock;
Yangbo Lu982f4252019-06-21 11:42:27 +0800936
Haibo Cheneaa2f102020-11-03 17:18:35 +0800937#ifdef MMC_SUPPORTS_TUNING
938 /*
939 * call esdhc_set_timing() before update the clock rate,
940 * This is because current we support DDR and SDR mode,
941 * Once the DDR_EN bit is set, the card clock will be
942 * divide by 2 automatically. So need to do this before
943 * setting clock rate.
944 */
945 if (priv->mode != mmc->selected_mode) {
946 ret = esdhc_set_timing(mmc);
947 if (ret) {
948 printf("esdhc_set_timing error %d\n", ret);
949 return ret;
950 }
951 }
952#endif
953
Yangbo Lu982f4252019-06-21 11:42:27 +0800954 /* Set the clock speed */
Peng Fan2cdf52b2019-11-04 17:14:15 +0800955 clock = mmc->clock;
956 if (clock < mmc->cfg->f_min)
957 clock = mmc->cfg->f_min;
958
959 if (priv->clock != clock)
960 set_sysctl(priv, mmc, clock);
Yangbo Lu982f4252019-06-21 11:42:27 +0800961
962#ifdef MMC_SUPPORTS_TUNING
963 if (mmc->clk_disable) {
964#ifdef CONFIG_FSL_USDHC
965 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
966#else
967 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
968#endif
969 } else {
970#ifdef CONFIG_FSL_USDHC
971 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
972 VENDORSPEC_CKEN);
973#else
974 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
975#endif
976 }
977
Haibo Cheneaa2f102020-11-03 17:18:35 +0800978 /*
979 * For HS400/HS400ES mode, make sure set the strobe dll in the
980 * target clock rate. So call esdhc_set_strobe_dll() after the
981 * clock updated.
982 */
983 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
984 esdhc_set_strobe_dll(mmc);
Yangbo Lu982f4252019-06-21 11:42:27 +0800985
986 if (priv->signal_voltage != mmc->signal_voltage) {
987 ret = esdhc_set_voltage(mmc);
988 if (ret) {
Marek Vasutba79fed2020-05-22 18:28:33 +0200989 if (ret != -ENOTSUPP)
990 printf("esdhc_set_voltage error %d\n", ret);
Yangbo Lu982f4252019-06-21 11:42:27 +0800991 return ret;
992 }
993 }
994#endif
995
996 /* Set the bus width */
997 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
998
999 if (mmc->bus_width == 4)
1000 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
1001 else if (mmc->bus_width == 8)
1002 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1003
1004 return 0;
1005}
1006
1007static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1008{
1009 struct fsl_esdhc *regs = priv->esdhc_regs;
1010 ulong start;
1011
1012 /* Reset the entire host controller */
1013 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1014
1015 /* Wait until the controller is available */
1016 start = get_timer(0);
1017 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1018 if (get_timer(start) > 1000)
1019 return -ETIMEDOUT;
1020 }
1021
1022#if defined(CONFIG_FSL_USDHC)
1023 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1024 esdhc_write32(&regs->mmcboot, 0x0);
1025 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1026 esdhc_write32(&regs->mixctrl, 0x0);
1027 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1028
1029 /* Put VEND_SPEC to default value */
1030 if (priv->vs18_enable)
1031 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1032 ESDHC_VENDORSPEC_VSELECT));
1033 else
1034 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1035
1036 /* Disable DLL_CTRL delay line */
1037 esdhc_write32(&regs->dllctrl, 0x0);
1038#endif
1039
1040#ifndef ARCH_MXC
1041 /* Enable cache snooping */
1042 esdhc_write32(&regs->scr, 0x00000040);
1043#endif
1044
1045#ifndef CONFIG_FSL_USDHC
1046 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1047#else
1048 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1049#endif
1050
1051 /* Set the initial clock speed */
1052 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1053
1054 /* Disable the BRR and BWR bits in IRQSTAT */
1055 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1056
1057#ifdef CONFIG_MCF5441x
1058 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1059#else
1060 /* Put the PROCTL reg back to the default */
1061 esdhc_write32(&regs->proctl, PROCTL_INIT);
1062#endif
1063
1064 /* Set timout to the maximum value */
1065 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1066
1067 return 0;
1068}
1069
1070static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1071{
1072 struct fsl_esdhc *regs = priv->esdhc_regs;
1073 int timeout = 1000;
1074
1075#ifdef CONFIG_ESDHC_DETECT_QUIRK
1076 if (CONFIG_ESDHC_DETECT_QUIRK)
1077 return 1;
1078#endif
1079
1080#if CONFIG_IS_ENABLED(DM_MMC)
1081 if (priv->non_removable)
1082 return 1;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001083
1084 if (priv->broken_cd)
1085 return 1;
Simon Glassfa4689a2019-12-06 21:41:35 -07001086#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001087 if (dm_gpio_is_valid(&priv->cd_gpio))
1088 return dm_gpio_get_value(&priv->cd_gpio);
1089#endif
1090#endif
1091
1092 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1093 udelay(1000);
1094
1095 return timeout > 0;
1096}
1097
1098static int esdhc_reset(struct fsl_esdhc *regs)
1099{
1100 ulong start;
1101
1102 /* reset the controller */
1103 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1104
1105 /* hardware clears the bit when it is done */
1106 start = get_timer(0);
1107 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1108 if (get_timer(start) > 100) {
1109 printf("MMC/SD: Reset never completed.\n");
1110 return -ETIMEDOUT;
1111 }
1112 }
1113
1114 return 0;
1115}
1116
1117#if !CONFIG_IS_ENABLED(DM_MMC)
1118static int esdhc_getcd(struct mmc *mmc)
1119{
1120 struct fsl_esdhc_priv *priv = mmc->priv;
1121
1122 return esdhc_getcd_common(priv);
1123}
1124
1125static int esdhc_init(struct mmc *mmc)
1126{
1127 struct fsl_esdhc_priv *priv = mmc->priv;
1128
1129 return esdhc_init_common(priv, mmc);
1130}
1131
1132static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1133 struct mmc_data *data)
1134{
1135 struct fsl_esdhc_priv *priv = mmc->priv;
1136
1137 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1138}
1139
1140static int esdhc_set_ios(struct mmc *mmc)
1141{
1142 struct fsl_esdhc_priv *priv = mmc->priv;
1143
1144 return esdhc_set_ios_common(priv, mmc);
1145}
1146
1147static const struct mmc_ops esdhc_ops = {
1148 .getcd = esdhc_getcd,
1149 .init = esdhc_init,
1150 .send_cmd = esdhc_send_cmd,
1151 .set_ios = esdhc_set_ios,
1152};
1153#endif
1154
1155static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1156 struct fsl_esdhc_plat *plat)
1157{
1158 struct mmc_config *cfg;
1159 struct fsl_esdhc *regs;
1160 u32 caps, voltage_caps;
1161 int ret;
1162
1163 if (!priv)
1164 return -EINVAL;
1165
1166 regs = priv->esdhc_regs;
1167
1168 /* First reset the eSDHC controller */
1169 ret = esdhc_reset(regs);
1170 if (ret)
1171 return ret;
1172
1173#ifdef CONFIG_MCF5441x
1174 /* ColdFire, using SDHC_DATA[3] for card detection */
1175 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1176#endif
1177
1178#ifndef CONFIG_FSL_USDHC
1179 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1180 | SYSCTL_IPGEN | SYSCTL_CKEN);
1181 /* Clearing tuning bits in case ROM has set it already */
1182 esdhc_write32(&regs->mixctrl, 0);
1183 esdhc_write32(&regs->autoc12err, 0);
1184 esdhc_write32(&regs->clktunectrlstatus, 0);
1185#else
1186 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1187 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1188#endif
1189
1190 if (priv->vs18_enable)
1191 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1192
Haibo Chen920f5d02020-09-30 15:52:23 +08001193 esdhc_write32(&regs->irqstaten, SDHCI_IRQ_EN_BITS);
Yangbo Lu982f4252019-06-21 11:42:27 +08001194 cfg = &plat->cfg;
1195#ifndef CONFIG_DM_MMC
1196 memset(cfg, '\0', sizeof(*cfg));
1197#endif
1198
1199 voltage_caps = 0;
1200 caps = esdhc_read32(&regs->hostcapblt);
1201
1202#ifdef CONFIG_MCF5441x
1203 /*
1204 * MCF5441x RM declares in more points that sdhc clock speed must
1205 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1206 * from host capabilities.
1207 */
1208 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1209#endif
1210
1211#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1212 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1213 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1214#endif
1215
1216/* T4240 host controller capabilities register should have VS33 bit */
1217#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1218 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1219#endif
1220
1221 if (caps & ESDHC_HOSTCAPBLT_VS18)
1222 voltage_caps |= MMC_VDD_165_195;
1223 if (caps & ESDHC_HOSTCAPBLT_VS30)
1224 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1225 if (caps & ESDHC_HOSTCAPBLT_VS33)
1226 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1227
1228 cfg->name = "FSL_SDHC";
1229#if !CONFIG_IS_ENABLED(DM_MMC)
1230 cfg->ops = &esdhc_ops;
1231#endif
1232#ifdef CONFIG_SYS_SD_VOLTAGE
1233 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1234#else
1235 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1236#endif
1237 if ((cfg->voltages & voltage_caps) == 0) {
1238 printf("voltage not supported by controller\n");
1239 return -1;
1240 }
1241
1242 if (priv->bus_width == 8)
1243 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1244 else if (priv->bus_width == 4)
1245 cfg->host_caps = MMC_MODE_4BIT;
1246
1247 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1248#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1249 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1250#endif
1251
1252 if (priv->bus_width > 0) {
1253 if (priv->bus_width < 8)
1254 cfg->host_caps &= ~MMC_MODE_8BIT;
1255 if (priv->bus_width < 4)
1256 cfg->host_caps &= ~MMC_MODE_4BIT;
1257 }
1258
1259 if (caps & ESDHC_HOSTCAPBLT_HSS)
1260 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1261
1262#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1263 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1264 cfg->host_caps &= ~MMC_MODE_8BIT;
1265#endif
1266
1267 cfg->host_caps |= priv->caps;
1268
1269 cfg->f_min = 400000;
1270 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1271
1272 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1273
Haibo Chen920f5d02020-09-30 15:52:23 +08001274 esdhc_write32(&regs->dllctrl, 0);
Yangbo Lu982f4252019-06-21 11:42:27 +08001275 if (priv->flags & ESDHC_FLAG_USDHC) {
1276 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
Haibo Chen920f5d02020-09-30 15:52:23 +08001277 u32 val = esdhc_read32(&regs->tuning_ctrl);
Yangbo Lu982f4252019-06-21 11:42:27 +08001278
1279 val |= ESDHC_STD_TUNING_EN;
1280 val &= ~ESDHC_TUNING_START_TAP_MASK;
1281 val |= priv->tuning_start_tap;
1282 val &= ~ESDHC_TUNING_STEP_MASK;
1283 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
Haibo Chen43162c32020-06-22 19:38:04 +08001284
1285 /* Disable the CMD CRC check for tuning, if not, need to
1286 * add some delay after every tuning command, because
1287 * hardware standard tuning logic will directly go to next
1288 * step once it detect the CMD CRC error, will not wait for
1289 * the card side to finally send out the tuning data, trigger
1290 * the buffer read ready interrupt immediately. If usdhc send
1291 * the next tuning command some eMMC card will stuck, can't
1292 * response, block the tuning procedure or the first command
1293 * after the whole tuning procedure always can't get any response.
1294 */
1295 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
Haibo Chen920f5d02020-09-30 15:52:23 +08001296 esdhc_write32(&regs->tuning_ctrl, val);
Yangbo Lu982f4252019-06-21 11:42:27 +08001297 }
1298 }
1299
1300 return 0;
1301}
1302
1303#if !CONFIG_IS_ENABLED(DM_MMC)
1304static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1305 struct fsl_esdhc_priv *priv)
1306{
1307 if (!cfg || !priv)
1308 return -EINVAL;
1309
1310 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1311 priv->bus_width = cfg->max_bus_width;
1312 priv->sdhc_clk = cfg->sdhc_clk;
1313 priv->wp_enable = cfg->wp_enable;
1314 priv->vs18_enable = cfg->vs18_enable;
1315
1316 return 0;
1317};
1318
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001319int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu982f4252019-06-21 11:42:27 +08001320{
1321 struct fsl_esdhc_plat *plat;
1322 struct fsl_esdhc_priv *priv;
1323 struct mmc *mmc;
1324 int ret;
1325
1326 if (!cfg)
1327 return -EINVAL;
1328
1329 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1330 if (!priv)
1331 return -ENOMEM;
1332 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1333 if (!plat) {
1334 free(priv);
1335 return -ENOMEM;
1336 }
1337
1338 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1339 if (ret) {
1340 debug("%s xlate failure\n", __func__);
1341 free(plat);
1342 free(priv);
1343 return ret;
1344 }
1345
1346 ret = fsl_esdhc_init(priv, plat);
1347 if (ret) {
1348 debug("%s init failure\n", __func__);
1349 free(plat);
1350 free(priv);
1351 return ret;
1352 }
1353
1354 mmc = mmc_create(&plat->cfg, priv);
1355 if (!mmc)
1356 return -EIO;
1357
1358 priv->mmc = mmc;
1359
1360 return 0;
1361}
1362
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001363int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu982f4252019-06-21 11:42:27 +08001364{
1365 struct fsl_esdhc_cfg *cfg;
1366
1367 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1368 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1369 cfg->sdhc_clk = gd->arch.sdhc_clk;
1370 return fsl_esdhc_initialize(bis, cfg);
1371}
1372#endif
1373
Yangbo Lu982f4252019-06-21 11:42:27 +08001374#ifdef CONFIG_OF_LIBFDT
1375__weak int esdhc_status_fixup(void *blob, const char *compat)
1376{
1377#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1378 if (!hwconfig("esdhc")) {
1379 do_fixup_by_compat(blob, compat, "status", "disabled",
1380 sizeof("disabled"), 1);
1381 return 1;
1382 }
1383#endif
1384 return 0;
1385}
1386
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001387void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lu982f4252019-06-21 11:42:27 +08001388{
1389 const char *compat = "fsl,esdhc";
1390
1391 if (esdhc_status_fixup(blob, compat))
1392 return;
1393
Yangbo Lu982f4252019-06-21 11:42:27 +08001394 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1395 gd->arch.sdhc_clk, 1);
Yangbo Lu982f4252019-06-21 11:42:27 +08001396}
1397#endif
1398
1399#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +08001400#include <asm/arch/clock.h>
Yangbo Lu982f4252019-06-21 11:42:27 +08001401__weak void init_clk_usdhc(u32 index)
1402{
1403}
1404
Simon Glassaad29ae2020-12-03 16:55:21 -07001405static int fsl_esdhc_of_to_plat(struct udevice *dev)
Yangbo Lu982f4252019-06-21 11:42:27 +08001406{
Walter Lozano8aff6732020-07-29 12:31:17 -03001407#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Yangbo Lu982f4252019-06-21 11:42:27 +08001408 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001409#if CONFIG_IS_ENABLED(DM_REGULATOR)
1410 struct udevice *vqmmc_dev;
Walter Lozano8aff6732020-07-29 12:31:17 -03001411 int ret;
Yangbo Lu982f4252019-06-21 11:42:27 +08001412#endif
Walter Lozano8aff6732020-07-29 12:31:17 -03001413 const void *fdt = gd->fdt_blob;
1414 int node = dev_of_offset(dev);
1415
Yangbo Lu982f4252019-06-21 11:42:27 +08001416 fdt_addr_t addr;
1417 unsigned int val;
Yangbo Lu982f4252019-06-21 11:42:27 +08001418
1419 addr = dev_read_addr(dev);
1420 if (addr == FDT_ADDR_T_NONE)
1421 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001422 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lu982f4252019-06-21 11:42:27 +08001423 priv->dev = dev;
1424 priv->mode = -1;
Yangbo Lu982f4252019-06-21 11:42:27 +08001425
1426 val = dev_read_u32_default(dev, "bus-width", -1);
1427 if (val == 8)
1428 priv->bus_width = 8;
1429 else if (val == 4)
1430 priv->bus_width = 4;
1431 else
1432 priv->bus_width = 1;
1433
1434 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1435 priv->tuning_step = val;
1436 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1437 ESDHC_TUNING_START_TAP_DEFAULT);
1438 priv->tuning_start_tap = val;
1439 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1440 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1441 priv->strobe_dll_delay_target = val;
1442
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001443 if (dev_read_bool(dev, "broken-cd"))
1444 priv->broken_cd = 1;
1445
Yangbo Lu982f4252019-06-21 11:42:27 +08001446 if (dev_read_bool(dev, "non-removable")) {
1447 priv->non_removable = 1;
1448 } else {
1449 priv->non_removable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001450#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001451 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1452 GPIOD_IS_IN);
1453#endif
1454 }
1455
1456 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1457 priv->wp_enable = 1;
1458 } else {
1459 priv->wp_enable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001460#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001461 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1462 GPIOD_IS_IN);
1463#endif
1464 }
1465
1466 priv->vs18_enable = 0;
1467
1468#if CONFIG_IS_ENABLED(DM_REGULATOR)
1469 /*
1470 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1471 * otherwise, emmc will work abnormally.
1472 */
1473 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1474 if (ret) {
1475 dev_dbg(dev, "no vqmmc-supply\n");
1476 } else {
Marek Vasut34e67f92020-05-22 18:19:08 +02001477 priv->vqmmc_dev = vqmmc_dev;
Yangbo Lu982f4252019-06-21 11:42:27 +08001478 ret = regulator_set_enable(vqmmc_dev, true);
1479 if (ret) {
1480 dev_err(dev, "fail to enable vqmmc-supply\n");
1481 return ret;
1482 }
1483
1484 if (regulator_get_value(vqmmc_dev) == 1800000)
1485 priv->vs18_enable = 1;
1486 }
1487#endif
Walter Lozano8aff6732020-07-29 12:31:17 -03001488#endif
1489 return 0;
1490}
1491
1492static int fsl_esdhc_probe(struct udevice *dev)
1493{
1494 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001495 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Walter Lozano8aff6732020-07-29 12:31:17 -03001496 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1497 struct esdhc_soc_data *data =
1498 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1499 struct mmc *mmc;
1500#if !CONFIG_IS_ENABLED(BLK)
1501 struct blk_desc *bdesc;
1502#endif
1503 int ret;
1504
1505#if CONFIG_IS_ENABLED(OF_PLATDATA)
1506 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1507 unsigned int val;
1508
1509 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1510 val = plat->dtplat.bus_width;
1511 if (val == 8)
1512 priv->bus_width = 8;
1513 else if (val == 4)
1514 priv->bus_width = 4;
1515 else
1516 priv->bus_width = 1;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001517
1518 if (dtplat->non_removable)
1519 priv->non_removable = 1;
1520 else
1521 priv->non_removable = 0;
1522
1523 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1524 struct udevice *gpiodev;
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001525
Simon Glass5792f4b2020-10-03 11:31:40 -06001526 ret = device_get_by_driver_info_idx(dtplat->cd_gpios->idx,
1527 &gpiodev);
Walter Lozanocfc05fd2020-07-29 12:31:19 -03001528 if (ret)
1529 return ret;
1530
1531 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1532 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1533 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1534
1535 if (ret)
1536 return ret;
1537 }
Walter Lozano8aff6732020-07-29 12:31:17 -03001538#endif
1539
1540 if (data)
1541 priv->flags = data->flags;
Yangbo Lu982f4252019-06-21 11:42:27 +08001542
Yangbo Lu982f4252019-06-21 11:42:27 +08001543 /*
1544 * TODO:
1545 * Because lack of clk driver, if SDHC clk is not enabled,
1546 * need to enable it first before this driver is invoked.
1547 *
1548 * we use MXC_ESDHC_CLK to get clk freq.
1549 * If one would like to make this function work,
1550 * the aliases should be provided in dts as this:
1551 *
1552 * aliases {
1553 * mmc0 = &usdhc1;
1554 * mmc1 = &usdhc2;
1555 * mmc2 = &usdhc3;
1556 * mmc3 = &usdhc4;
1557 * };
1558 * Then if your board only supports mmc2 and mmc3, but we can
1559 * correctly get the seq as 2 and 3, then let mxc_get_clock
1560 * work as expected.
1561 */
1562
Simon Glass75e534b2020-12-16 21:20:07 -07001563 init_clk_usdhc(dev_seq(dev));
Yangbo Lu982f4252019-06-21 11:42:27 +08001564
Giulio Benettidbdbc632020-01-10 15:51:45 +01001565#if CONFIG_IS_ENABLED(CLK)
1566 /* Assigned clock already set clock */
1567 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1568 if (ret) {
1569 printf("Failed to get per_clk\n");
1570 return ret;
1571 }
1572 ret = clk_enable(&priv->per_clk);
1573 if (ret) {
1574 printf("Failed to enable per_clk\n");
1575 return ret;
1576 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001577
Giulio Benettidbdbc632020-01-10 15:51:45 +01001578 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1579#else
Simon Glass75e534b2020-12-16 21:20:07 -07001580 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev_seq(dev));
Giulio Benettidbdbc632020-01-10 15:51:45 +01001581 if (priv->sdhc_clk <= 0) {
1582 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1583 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001584 }
Giulio Benettidbdbc632020-01-10 15:51:45 +01001585#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001586
1587 ret = fsl_esdhc_init(priv, plat);
1588 if (ret) {
1589 dev_err(dev, "fsl_esdhc_init failure\n");
1590 return ret;
1591 }
1592
Walter Lozano8aff6732020-07-29 12:31:17 -03001593#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Peng Fan3766a482019-07-10 09:35:24 +00001594 ret = mmc_of_parse(dev, &plat->cfg);
1595 if (ret)
1596 return ret;
Walter Lozano8aff6732020-07-29 12:31:17 -03001597#endif
Peng Fan3766a482019-07-10 09:35:24 +00001598
Yangbo Lu982f4252019-06-21 11:42:27 +08001599 mmc = &plat->mmc;
1600 mmc->cfg = &plat->cfg;
1601 mmc->dev = dev;
1602#if !CONFIG_IS_ENABLED(BLK)
1603 mmc->priv = priv;
1604
1605 /* Setup dsr related values */
1606 mmc->dsr_imp = 0;
1607 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1608 /* Setup the universal parts of the block interface just once */
1609 bdesc = mmc_get_blk_desc(mmc);
1610 bdesc->if_type = IF_TYPE_MMC;
1611 bdesc->removable = 1;
1612 bdesc->devnum = mmc_get_next_devnum();
1613 bdesc->block_read = mmc_bread;
1614 bdesc->block_write = mmc_bwrite;
1615 bdesc->block_erase = mmc_berase;
1616
1617 /* setup initial part type */
1618 bdesc->part_type = mmc->cfg->part_type;
1619 mmc_list_add(mmc);
1620#endif
1621
1622 upriv->mmc = mmc;
1623
1624 return esdhc_init_common(priv, mmc);
1625}
1626
1627#if CONFIG_IS_ENABLED(DM_MMC)
1628static int fsl_esdhc_get_cd(struct udevice *dev)
1629{
1630 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1631
1632 return esdhc_getcd_common(priv);
1633}
1634
1635static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1636 struct mmc_data *data)
1637{
Simon Glassfa20e932020-12-03 16:55:20 -07001638 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001639 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1640
1641 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1642}
1643
1644static int fsl_esdhc_set_ios(struct udevice *dev)
1645{
Simon Glassfa20e932020-12-03 16:55:20 -07001646 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001647 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1648
1649 return esdhc_set_ios_common(priv, &plat->mmc);
1650}
1651
Peng Fan69b9d3a2019-07-10 09:35:26 +00001652#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1653static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1654{
1655 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1656 struct fsl_esdhc *regs = priv->esdhc_regs;
1657 u32 m;
1658
Haibo Chen920f5d02020-09-30 15:52:23 +08001659 m = esdhc_read32(&regs->mixctrl);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001660 m |= MIX_CTRL_HS400_ES;
Haibo Chen920f5d02020-09-30 15:52:23 +08001661 esdhc_write32(&regs->mixctrl, m);
Peng Fan69b9d3a2019-07-10 09:35:26 +00001662
1663 return 0;
1664}
1665#endif
1666
Haibo Chencb78f212020-11-05 14:57:13 +08001667static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1668 int timeout_us)
1669{
1670 int ret;
1671 u32 tmp;
1672 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1673 struct fsl_esdhc *regs = priv->esdhc_regs;
1674
1675 ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
1676 !!(tmp & PRSSTAT_DAT0) == !!state,
1677 timeout_us);
1678 return ret;
1679}
1680
Yangbo Lu982f4252019-06-21 11:42:27 +08001681static const struct dm_mmc_ops fsl_esdhc_ops = {
1682 .get_cd = fsl_esdhc_get_cd,
1683 .send_cmd = fsl_esdhc_send_cmd,
1684 .set_ios = fsl_esdhc_set_ios,
1685#ifdef MMC_SUPPORTS_TUNING
1686 .execute_tuning = fsl_esdhc_execute_tuning,
1687#endif
Peng Fan69b9d3a2019-07-10 09:35:26 +00001688#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1689 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1690#endif
Haibo Chencb78f212020-11-05 14:57:13 +08001691 .wait_dat0 = fsl_esdhc_wait_dat0,
Yangbo Lu982f4252019-06-21 11:42:27 +08001692};
1693#endif
1694
1695static struct esdhc_soc_data usdhc_imx7d_data = {
1696 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1697 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1698 | ESDHC_FLAG_HS400,
Yangbo Lu982f4252019-06-21 11:42:27 +08001699};
1700
Peng Fan457fe962019-07-10 09:35:28 +00001701static struct esdhc_soc_data usdhc_imx8qm_data = {
1702 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1703 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1704 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1705};
1706
Yangbo Lu982f4252019-06-21 11:42:27 +08001707static const struct udevice_id fsl_esdhc_ids[] = {
1708 { .compatible = "fsl,imx53-esdhc", },
1709 { .compatible = "fsl,imx6ul-usdhc", },
1710 { .compatible = "fsl,imx6sx-usdhc", },
1711 { .compatible = "fsl,imx6sl-usdhc", },
1712 { .compatible = "fsl,imx6q-usdhc", },
1713 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1714 { .compatible = "fsl,imx7ulp-usdhc", },
Peng Fan457fe962019-07-10 09:35:28 +00001715 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fand7689fa2019-11-04 17:31:17 +08001716 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1717 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1718 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti65b5ec12020-01-10 15:51:46 +01001719 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001720 { .compatible = "fsl,esdhc", },
1721 { /* sentinel */ }
1722};
1723
1724#if CONFIG_IS_ENABLED(BLK)
1725static int fsl_esdhc_bind(struct udevice *dev)
1726{
Simon Glassfa20e932020-12-03 16:55:20 -07001727 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
Yangbo Lu982f4252019-06-21 11:42:27 +08001728
1729 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1730}
1731#endif
1732
1733U_BOOT_DRIVER(fsl_esdhc) = {
Walter Lozano904b0be2020-07-29 12:31:16 -03001734 .name = "fsl_esdhc",
Yangbo Lu982f4252019-06-21 11:42:27 +08001735 .id = UCLASS_MMC,
1736 .of_match = fsl_esdhc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001737 .of_to_plat = fsl_esdhc_of_to_plat,
Yangbo Lu982f4252019-06-21 11:42:27 +08001738 .ops = &fsl_esdhc_ops,
1739#if CONFIG_IS_ENABLED(BLK)
1740 .bind = fsl_esdhc_bind,
1741#endif
1742 .probe = fsl_esdhc_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -07001743 .plat_auto = sizeof(struct fsl_esdhc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001744 .priv_auto = sizeof(struct fsl_esdhc_priv),
Yangbo Lu982f4252019-06-21 11:42:27 +08001745};
Walter Lozano8aff6732020-07-29 12:31:17 -03001746
Simon Glassdf65db82020-12-28 20:34:57 -07001747DM_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
Yangbo Lu982f4252019-06-21 11:42:27 +08001748#endif