blob: 7cbd6e458798c0a8598484973a867cd6265f34fe [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>
20#include <mmc.h>
21#include <part.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <linux/err.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080023#include <power/regulator.h>
24#include <malloc.h>
25#include <fsl_esdhc_imx.h>
26#include <fdt_support.h>
27#include <asm/io.h>
28#include <dm.h>
29#include <asm-generic/gpio.h>
30#include <dm/pinctrl.h>
31
32#if !CONFIG_IS_ENABLED(BLK)
33#include "mmc_private.h"
34#endif
35
36DECLARE_GLOBAL_DATA_PTR;
37
38#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
39 IRQSTATEN_CINT | \
40 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
41 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
42 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
43 IRQSTATEN_DINT)
44#define MAX_TUNING_LOOP 40
45#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
46
47struct fsl_esdhc {
48 uint dsaddr; /* SDMA system address register */
49 uint blkattr; /* Block attributes register */
50 uint cmdarg; /* Command argument register */
51 uint xfertyp; /* Transfer type register */
52 uint cmdrsp0; /* Command response 0 register */
53 uint cmdrsp1; /* Command response 1 register */
54 uint cmdrsp2; /* Command response 2 register */
55 uint cmdrsp3; /* Command response 3 register */
56 uint datport; /* Buffer data port register */
57 uint prsstat; /* Present state register */
58 uint proctl; /* Protocol control register */
59 uint sysctl; /* System Control Register */
60 uint irqstat; /* Interrupt status register */
61 uint irqstaten; /* Interrupt status enable register */
62 uint irqsigen; /* Interrupt signal enable register */
63 uint autoc12err; /* Auto CMD error status register */
64 uint hostcapblt; /* Host controller capabilities register */
65 uint wml; /* Watermark level register */
66 uint mixctrl; /* For USDHC */
67 char reserved1[4]; /* reserved */
68 uint fevt; /* Force event register */
69 uint admaes; /* ADMA error status register */
70 uint adsaddr; /* ADMA system address register */
71 char reserved2[4];
72 uint dllctrl;
73 uint dllstat;
74 uint clktunectrlstatus;
75 char reserved3[4];
76 uint strobe_dllctrl;
77 uint strobe_dllstat;
78 char reserved4[72];
79 uint vendorspec;
80 uint mmcboot;
81 uint vendorspec2;
Giulio Benetti65b5ec12020-01-10 15:51:46 +010082 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lu982f4252019-06-21 11:42:27 +080083 char reserved5[44];
84 uint hostver; /* Host controller version register */
85 char reserved6[4]; /* reserved */
86 uint dmaerraddr; /* DMA error address register */
87 char reserved7[4]; /* reserved */
88 uint dmaerrattr; /* DMA error attribute register */
89 char reserved8[4]; /* reserved */
90 uint hostcapblt2; /* Host controller capabilities register 2 */
91 char reserved9[8]; /* reserved */
92 uint tcr; /* Tuning control register */
93 char reserved10[28]; /* reserved */
94 uint sddirctl; /* SD direction control register */
95 char reserved11[712];/* reserved */
96 uint scr; /* eSDHC control register */
97};
98
99struct fsl_esdhc_plat {
100 struct mmc_config cfg;
101 struct mmc mmc;
102};
103
104struct esdhc_soc_data {
105 u32 flags;
Yangbo Lu982f4252019-06-21 11:42:27 +0800106};
107
108/**
109 * struct fsl_esdhc_priv
110 *
111 * @esdhc_regs: registers of the sdhc controller
112 * @sdhc_clk: Current clk of the sdhc controller
113 * @bus_width: bus width, 1bit, 4bit or 8bit
114 * @cfg: mmc config
115 * @mmc: mmc
116 * Following is used when Driver Model is enabled for MMC
117 * @dev: pointer for the device
118 * @non_removable: 0: removable; 1: non-removable
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300119 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lu982f4252019-06-21 11:42:27 +0800120 * @wp_enable: 1: enable checking wp; 0: no check
121 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
122 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
123 * @caps: controller capabilities
124 * @tuning_step: tuning step setting in tuning_ctrl register
125 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
126 * @strobe_dll_delay_target: settings in strobe_dllctrl
127 * @signal_voltage: indicating the current voltage
128 * @cd_gpio: gpio for card detection
129 * @wp_gpio: gpio for write protection
130 */
131struct fsl_esdhc_priv {
132 struct fsl_esdhc *esdhc_regs;
133 unsigned int sdhc_clk;
134 struct clk per_clk;
135 unsigned int clock;
136 unsigned int mode;
137 unsigned int bus_width;
138#if !CONFIG_IS_ENABLED(BLK)
139 struct mmc *mmc;
140#endif
141 struct udevice *dev;
142 int non_removable;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300143 int broken_cd;
Yangbo Lu982f4252019-06-21 11:42:27 +0800144 int wp_enable;
145 int vs18_enable;
146 u32 flags;
147 u32 caps;
148 u32 tuning_step;
149 u32 tuning_start_tap;
150 u32 strobe_dll_delay_target;
151 u32 signal_voltage;
Ye Li7aa20fd2019-07-11 03:29:02 +0000152#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800153 struct udevice *vqmmc_dev;
154 struct udevice *vmmc_dev;
155#endif
Simon Glassfa4689a2019-12-06 21:41:35 -0700156#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +0800157 struct gpio_desc cd_gpio;
158 struct gpio_desc wp_gpio;
159#endif
160};
161
162/* Return the XFERTYP flags for a given command and data packet */
163static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
164{
165 uint xfertyp = 0;
166
167 if (data) {
168 xfertyp |= XFERTYP_DPSEL;
169#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
170 xfertyp |= XFERTYP_DMAEN;
171#endif
172 if (data->blocks > 1) {
173 xfertyp |= XFERTYP_MSBSEL;
174 xfertyp |= XFERTYP_BCEN;
175#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
176 xfertyp |= XFERTYP_AC12EN;
177#endif
178 }
179
180 if (data->flags & MMC_DATA_READ)
181 xfertyp |= XFERTYP_DTDSEL;
182 }
183
184 if (cmd->resp_type & MMC_RSP_CRC)
185 xfertyp |= XFERTYP_CCCEN;
186 if (cmd->resp_type & MMC_RSP_OPCODE)
187 xfertyp |= XFERTYP_CICEN;
188 if (cmd->resp_type & MMC_RSP_136)
189 xfertyp |= XFERTYP_RSPTYP_136;
190 else if (cmd->resp_type & MMC_RSP_BUSY)
191 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
192 else if (cmd->resp_type & MMC_RSP_PRESENT)
193 xfertyp |= XFERTYP_RSPTYP_48;
194
195 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
196 xfertyp |= XFERTYP_CMDTYP_ABORT;
197
198 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
199}
200
201#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
202/*
203 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
204 */
205static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
206 struct mmc_data *data)
207{
208 struct fsl_esdhc *regs = priv->esdhc_regs;
209 uint blocks;
210 char *buffer;
211 uint databuf;
212 uint size;
213 uint irqstat;
214 ulong start;
215
216 if (data->flags & MMC_DATA_READ) {
217 blocks = data->blocks;
218 buffer = data->dest;
219 while (blocks) {
220 start = get_timer(0);
221 size = data->blocksize;
222 irqstat = esdhc_read32(&regs->irqstat);
223 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
224 if (get_timer(start) > PIO_TIMEOUT) {
225 printf("\nData Read Failed in PIO Mode.");
226 return;
227 }
228 }
229 while (size && (!(irqstat & IRQSTAT_TC))) {
230 udelay(100); /* Wait before last byte transfer complete */
231 irqstat = esdhc_read32(&regs->irqstat);
232 databuf = in_le32(&regs->datport);
233 *((uint *)buffer) = databuf;
234 buffer += 4;
235 size -= 4;
236 }
237 blocks--;
238 }
239 } else {
240 blocks = data->blocks;
241 buffer = (char *)data->src;
242 while (blocks) {
243 start = get_timer(0);
244 size = data->blocksize;
245 irqstat = esdhc_read32(&regs->irqstat);
246 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
247 if (get_timer(start) > PIO_TIMEOUT) {
248 printf("\nData Write Failed in PIO Mode.");
249 return;
250 }
251 }
252 while (size && (!(irqstat & IRQSTAT_TC))) {
253 udelay(100); /* Wait before last byte transfer complete */
254 databuf = *((uint *)buffer);
255 buffer += 4;
256 size -= 4;
257 irqstat = esdhc_read32(&regs->irqstat);
258 out_le32(&regs->datport, databuf);
259 }
260 blocks--;
261 }
262 }
263}
264#endif
265
266static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
267 struct mmc_data *data)
268{
269 int timeout;
270 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu16b38542019-06-21 11:42:30 +0800271#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800272 dma_addr_t addr;
273#endif
274 uint wml_value;
275
276 wml_value = data->blocksize/4;
277
278 if (data->flags & MMC_DATA_READ) {
279 if (wml_value > WML_RD_WML_MAX)
280 wml_value = WML_RD_WML_MAX_VAL;
281
282 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
283#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu16b38542019-06-21 11:42:30 +0800284#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800285 addr = virt_to_phys((void *)(data->dest));
286 if (upper_32_bits(addr))
287 printf("Error found for upper 32 bits\n");
288 else
289 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
290#else
291 esdhc_write32(&regs->dsaddr, (u32)data->dest);
292#endif
293#endif
294 } else {
295#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
296 flush_dcache_range((ulong)data->src,
297 (ulong)data->src+data->blocks
298 *data->blocksize);
299#endif
300 if (wml_value > WML_WR_WML_MAX)
301 wml_value = WML_WR_WML_MAX_VAL;
302 if (priv->wp_enable) {
303 if ((esdhc_read32(&regs->prsstat) &
304 PRSSTAT_WPSPL) == 0) {
305 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
306 return -ETIMEDOUT;
307 }
308 } else {
Simon Glassfa4689a2019-12-06 21:41:35 -0700309#if CONFIG_IS_ENABLED(DM_GPIO)
310 if (dm_gpio_is_valid(&priv->wp_gpio) &&
311 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800312 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
313 return -ETIMEDOUT;
314 }
315#endif
316 }
317
318 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
319 wml_value << 16);
320#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu16b38542019-06-21 11:42:30 +0800321#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800322 addr = virt_to_phys((void *)(data->src));
323 if (upper_32_bits(addr))
324 printf("Error found for upper 32 bits\n");
325 else
326 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
327#else
328 esdhc_write32(&regs->dsaddr, (u32)data->src);
329#endif
330#endif
331 }
332
333 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
334
335 /* Calculate the timeout period for data transactions */
336 /*
337 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
338 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
339 * So, Number of SD Clock cycles for 0.25sec should be minimum
340 * (SD Clock/sec * 0.25 sec) SD Clock cycles
341 * = (mmc->clock * 1/4) SD Clock cycles
342 * As 1) >= 2)
343 * => (2^(timeout+13)) >= mmc->clock * 1/4
344 * Taking log2 both the sides
345 * => timeout + 13 >= log2(mmc->clock/4)
346 * Rounding up to next power of 2
347 * => timeout + 13 = log2(mmc->clock/4) + 1
348 * => timeout + 13 = fls(mmc->clock/4)
349 *
350 * However, the MMC spec "It is strongly recommended for hosts to
351 * implement more than 500ms timeout value even if the card
352 * indicates the 250ms maximum busy length." Even the previous
353 * value of 300ms is known to be insufficient for some cards.
354 * So, we use
355 * => timeout + 13 = fls(mmc->clock/2)
356 */
357 timeout = fls(mmc->clock/2);
358 timeout -= 13;
359
360 if (timeout > 14)
361 timeout = 14;
362
363 if (timeout < 0)
364 timeout = 0;
365
366#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
367 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
368 timeout++;
369#endif
370
371#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
372 timeout = 0xE;
373#endif
374 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
375
376 return 0;
377}
378
379static void check_and_invalidate_dcache_range
380 (struct mmc_cmd *cmd,
381 struct mmc_data *data) {
382 unsigned start = 0;
383 unsigned end = 0;
384 unsigned size = roundup(ARCH_DMA_MINALIGN,
385 data->blocks*data->blocksize);
Yangbo Lu16b38542019-06-21 11:42:30 +0800386#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800387 dma_addr_t addr;
388
389 addr = virt_to_phys((void *)(data->dest));
390 if (upper_32_bits(addr))
391 printf("Error found for upper 32 bits\n");
392 else
393 start = lower_32_bits(addr);
394#else
395 start = (unsigned)data->dest;
396#endif
397 end = start + size;
398 invalidate_dcache_range(start, end);
399}
400
401#ifdef CONFIG_MCF5441x
402/*
403 * Swaps 32-bit words to little-endian byte order.
404 */
405static inline void sd_swap_dma_buff(struct mmc_data *data)
406{
407 int i, size = data->blocksize >> 2;
408 u32 *buffer = (u32 *)data->dest;
409 u32 sw;
410
411 while (data->blocks--) {
412 for (i = 0; i < size; i++) {
413 sw = __sw32(*buffer);
414 *buffer++ = sw;
415 }
416 }
417}
418#endif
419
420/*
421 * Sends a command out on the bus. Takes the mmc pointer,
422 * a command pointer, and an optional data pointer.
423 */
424static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
425 struct mmc_cmd *cmd, struct mmc_data *data)
426{
427 int err = 0;
428 uint xfertyp;
429 uint irqstat;
430 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
431 struct fsl_esdhc *regs = priv->esdhc_regs;
432 unsigned long start;
433
434#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
435 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
436 return 0;
437#endif
438
439 esdhc_write32(&regs->irqstat, -1);
440
441 sync();
442
443 /* Wait for the bus to be idle */
444 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
445 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
446 ;
447
448 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
449 ;
450
451 /* Wait at least 8 SD clock cycles before the next command */
452 /*
453 * Note: This is way more than 8 cycles, but 1ms seems to
454 * resolve timing issues with some cards
455 */
456 udelay(1000);
457
458 /* Set up for a data transfer if we have one */
459 if (data) {
460 err = esdhc_setup_data(priv, mmc, data);
461 if(err)
462 return err;
463
464 if (data->flags & MMC_DATA_READ)
465 check_and_invalidate_dcache_range(cmd, data);
466 }
467
468 /* Figure out the transfer arguments */
469 xfertyp = esdhc_xfertyp(cmd, data);
470
471 /* Mask all irqs */
472 esdhc_write32(&regs->irqsigen, 0);
473
474 /* Send the command */
475 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
476#if defined(CONFIG_FSL_USDHC)
477 esdhc_write32(&regs->mixctrl,
478 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
479 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
480 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
481#else
482 esdhc_write32(&regs->xfertyp, xfertyp);
483#endif
484
485 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
486 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
487 flags = IRQSTAT_BRR;
488
489 /* Wait for the command to complete */
490 start = get_timer(0);
491 while (!(esdhc_read32(&regs->irqstat) & flags)) {
492 if (get_timer(start) > 1000) {
493 err = -ETIMEDOUT;
494 goto out;
495 }
496 }
497
498 irqstat = esdhc_read32(&regs->irqstat);
499
500 if (irqstat & CMD_ERR) {
501 err = -ECOMM;
502 goto out;
503 }
504
505 if (irqstat & IRQSTAT_CTOE) {
506 err = -ETIMEDOUT;
507 goto out;
508 }
509
510 /* Switch voltage to 1.8V if CMD11 succeeded */
511 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
512 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
513
514 printf("Run CMD11 1.8V switch\n");
515 /* Sleep for 5 ms - max time for card to switch to 1.8V */
516 udelay(5000);
517 }
518
519 /* Workaround for ESDHC errata ENGcm03648 */
520 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan3dbea592019-07-10 09:35:30 +0000521 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800522
Peng Fan3dbea592019-07-10 09:35:30 +0000523 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-06-21 11:42:27 +0800524 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
525 PRSSTAT_DAT0)) {
526 udelay(100);
527 timeout--;
528 }
529
530 if (timeout <= 0) {
531 printf("Timeout waiting for DAT0 to go high!\n");
532 err = -ETIMEDOUT;
533 goto out;
534 }
535 }
536
537 /* Copy the response to the response buffer */
538 if (cmd->resp_type & MMC_RSP_136) {
539 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
540
541 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
542 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
543 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
544 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
545 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
546 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
547 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
548 cmd->response[3] = (cmdrsp0 << 8);
549 } else
550 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
551
552 /* Wait until all of the blocks are transferred */
553 if (data) {
554#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
555 esdhc_pio_read_write(priv, data);
556#else
557 flags = DATA_COMPLETE;
558 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
559 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
560 flags = IRQSTAT_BRR;
561 }
562
563 do {
564 irqstat = esdhc_read32(&regs->irqstat);
565
566 if (irqstat & IRQSTAT_DTOE) {
567 err = -ETIMEDOUT;
568 goto out;
569 }
570
571 if (irqstat & DATA_ERR) {
572 err = -ECOMM;
573 goto out;
574 }
575 } while ((irqstat & flags) != flags);
576
577 /*
578 * Need invalidate the dcache here again to avoid any
579 * cache-fill during the DMA operations such as the
580 * speculative pre-fetching etc.
581 */
582 if (data->flags & MMC_DATA_READ) {
583 check_and_invalidate_dcache_range(cmd, data);
584#ifdef CONFIG_MCF5441x
585 sd_swap_dma_buff(data);
586#endif
587 }
588#endif
589 }
590
591out:
592 /* Reset CMD and DATA portions on error */
593 if (err) {
594 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
595 SYSCTL_RSTC);
596 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
597 ;
598
599 if (data) {
600 esdhc_write32(&regs->sysctl,
601 esdhc_read32(&regs->sysctl) |
602 SYSCTL_RSTD);
603 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
604 ;
605 }
606
607 /* If this was CMD11, then notify that power cycle is needed */
608 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
609 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
610 }
611
612 esdhc_write32(&regs->irqstat, -1);
613
614 return err;
615}
616
617static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
618{
619 struct fsl_esdhc *regs = priv->esdhc_regs;
620 int div = 1;
621#ifdef ARCH_MXC
622#ifdef CONFIG_MX53
623 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
624 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
625#else
626 int pre_div = 1;
627#endif
628#else
629 int pre_div = 2;
630#endif
631 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
632 int sdhc_clk = priv->sdhc_clk;
633 uint clk;
634
Yangbo Lu982f4252019-06-21 11:42:27 +0800635 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
636 pre_div *= 2;
637
638 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
639 div++;
640
641 pre_div >>= 1;
642 div -= 1;
643
644 clk = (pre_div << 8) | (div << 4);
645
646#ifdef CONFIG_FSL_USDHC
647 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
648#else
649 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
650#endif
651
652 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
653
654 udelay(10000);
655
656#ifdef CONFIG_FSL_USDHC
657 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
658#else
659 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
660#endif
661
662 priv->clock = clock;
663}
664
Yangbo Lu982f4252019-06-21 11:42:27 +0800665#ifdef MMC_SUPPORTS_TUNING
666static int esdhc_change_pinstate(struct udevice *dev)
667{
668 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
669 int ret;
670
671 switch (priv->mode) {
672 case UHS_SDR50:
673 case UHS_DDR50:
674 ret = pinctrl_select_state(dev, "state_100mhz");
675 break;
676 case UHS_SDR104:
677 case MMC_HS_200:
678 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000679 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800680 ret = pinctrl_select_state(dev, "state_200mhz");
681 break;
682 default:
683 ret = pinctrl_select_state(dev, "default");
684 break;
685 }
686
687 if (ret)
688 printf("%s %d error\n", __func__, priv->mode);
689
690 return ret;
691}
692
693static void esdhc_reset_tuning(struct mmc *mmc)
694{
695 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
696 struct fsl_esdhc *regs = priv->esdhc_regs;
697
698 if (priv->flags & ESDHC_FLAG_USDHC) {
699 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
700 esdhc_clrbits32(&regs->autoc12err,
701 MIX_CTRL_SMPCLK_SEL |
702 MIX_CTRL_EXE_TUNE);
703 }
704 }
705}
706
707static void esdhc_set_strobe_dll(struct mmc *mmc)
708{
709 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
710 struct fsl_esdhc *regs = priv->esdhc_regs;
711 u32 val;
712
713 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
714 writel(ESDHC_STROBE_DLL_CTRL_RESET, &regs->strobe_dllctrl);
715
716 /*
717 * enable strobe dll ctrl and adjust the delay target
718 * for the uSDHC loopback read clock
719 */
720 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
721 (priv->strobe_dll_delay_target <<
722 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
723 writel(val, &regs->strobe_dllctrl);
724 /* wait 1us to make sure strobe dll status register stable */
725 mdelay(1);
726 val = readl(&regs->strobe_dllstat);
727 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
728 pr_warn("HS400 strobe DLL status REF not lock!\n");
729 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
730 pr_warn("HS400 strobe DLL status SLV not lock!\n");
731 }
732}
733
734static int esdhc_set_timing(struct mmc *mmc)
735{
736 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
737 struct fsl_esdhc *regs = priv->esdhc_regs;
738 u32 mixctrl;
739
740 mixctrl = readl(&regs->mixctrl);
741 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
742
743 switch (mmc->selected_mode) {
744 case MMC_LEGACY:
745 case SD_LEGACY:
746 esdhc_reset_tuning(mmc);
747 writel(mixctrl, &regs->mixctrl);
748 break;
749 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000750 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800751 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
752 writel(mixctrl, &regs->mixctrl);
753 esdhc_set_strobe_dll(mmc);
754 break;
755 case MMC_HS:
756 case MMC_HS_52:
757 case MMC_HS_200:
758 case SD_HS:
759 case UHS_SDR12:
760 case UHS_SDR25:
761 case UHS_SDR50:
762 case UHS_SDR104:
763 writel(mixctrl, &regs->mixctrl);
764 break;
765 case UHS_DDR50:
766 case MMC_DDR_52:
767 mixctrl |= MIX_CTRL_DDREN;
768 writel(mixctrl, &regs->mixctrl);
769 break;
770 default:
771 printf("Not supported %d\n", mmc->selected_mode);
772 return -EINVAL;
773 }
774
775 priv->mode = mmc->selected_mode;
776
777 return esdhc_change_pinstate(mmc->dev);
778}
779
780static int esdhc_set_voltage(struct mmc *mmc)
781{
782 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
783 struct fsl_esdhc *regs = priv->esdhc_regs;
784 int ret;
785
786 priv->signal_voltage = mmc->signal_voltage;
787 switch (mmc->signal_voltage) {
788 case MMC_SIGNAL_VOLTAGE_330:
789 if (priv->vs18_enable)
790 return -EIO;
791#if CONFIG_IS_ENABLED(DM_REGULATOR)
792 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
793 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
794 if (ret) {
795 printf("Setting to 3.3V error");
796 return -EIO;
797 }
798 /* Wait for 5ms */
799 mdelay(5);
800 }
801#endif
802
803 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
804 if (!(esdhc_read32(&regs->vendorspec) &
805 ESDHC_VENDORSPEC_VSELECT))
806 return 0;
807
808 return -EAGAIN;
809 case MMC_SIGNAL_VOLTAGE_180:
810#if CONFIG_IS_ENABLED(DM_REGULATOR)
811 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
812 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
813 if (ret) {
814 printf("Setting to 1.8V error");
815 return -EIO;
816 }
817 }
818#endif
819 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
820 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
821 return 0;
822
823 return -EAGAIN;
824 case MMC_SIGNAL_VOLTAGE_120:
825 return -ENOTSUPP;
826 default:
827 return 0;
828 }
829}
830
831static void esdhc_stop_tuning(struct mmc *mmc)
832{
833 struct mmc_cmd cmd;
834
835 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
836 cmd.cmdarg = 0;
837 cmd.resp_type = MMC_RSP_R1b;
838
839 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
840}
841
842static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
843{
844 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
845 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
846 struct fsl_esdhc *regs = priv->esdhc_regs;
847 struct mmc *mmc = &plat->mmc;
848 u32 irqstaten = readl(&regs->irqstaten);
849 u32 irqsigen = readl(&regs->irqsigen);
850 int i, ret = -ETIMEDOUT;
851 u32 val, mixctrl;
852
853 /* clock tuning is not needed for upto 52MHz */
854 if (mmc->clock <= 52000000)
855 return 0;
856
857 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
858 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
859 val = readl(&regs->autoc12err);
860 mixctrl = readl(&regs->mixctrl);
861 val &= ~MIX_CTRL_SMPCLK_SEL;
862 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
863
864 val |= MIX_CTRL_EXE_TUNE;
865 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
866
867 writel(val, &regs->autoc12err);
868 writel(mixctrl, &regs->mixctrl);
869 }
870
871 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
872 mixctrl = readl(&regs->mixctrl);
873 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
874 writel(mixctrl, &regs->mixctrl);
875
876 writel(IRQSTATEN_BRR, &regs->irqstaten);
877 writel(IRQSTATEN_BRR, &regs->irqsigen);
878
879 /*
880 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
881 * of loops reaches 40 times.
882 */
883 for (i = 0; i < MAX_TUNING_LOOP; i++) {
884 u32 ctrl;
885
886 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
887 if (mmc->bus_width == 8)
888 writel(0x7080, &regs->blkattr);
889 else if (mmc->bus_width == 4)
890 writel(0x7040, &regs->blkattr);
891 } else {
892 writel(0x7040, &regs->blkattr);
893 }
894
895 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
896 val = readl(&regs->mixctrl);
897 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
898 writel(val, &regs->mixctrl);
899
900 /* We are using STD tuning, no need to check return value */
901 mmc_send_tuning(mmc, opcode, NULL);
902
903 ctrl = readl(&regs->autoc12err);
904 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
905 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
906 /*
907 * need to wait some time, make sure sd/mmc fininsh
908 * send out tuning data, otherwise, the sd/mmc can't
909 * response to any command when the card still out
910 * put the tuning data.
911 */
912 mdelay(1);
913 ret = 0;
914 break;
915 }
916
917 /* Add 1ms delay for SD and eMMC */
918 mdelay(1);
919 }
920
921 writel(irqstaten, &regs->irqstaten);
922 writel(irqsigen, &regs->irqsigen);
923
924 esdhc_stop_tuning(mmc);
925
926 return ret;
927}
928#endif
929
930static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
931{
932 struct fsl_esdhc *regs = priv->esdhc_regs;
933 int ret __maybe_unused;
Peng Fan2cdf52b2019-11-04 17:14:15 +0800934 u32 clock;
Yangbo Lu982f4252019-06-21 11:42:27 +0800935
Yangbo Lu982f4252019-06-21 11:42:27 +0800936 /* Set the clock speed */
Peng Fan2cdf52b2019-11-04 17:14:15 +0800937 clock = mmc->clock;
938 if (clock < mmc->cfg->f_min)
939 clock = mmc->cfg->f_min;
940
941 if (priv->clock != clock)
942 set_sysctl(priv, mmc, clock);
Yangbo Lu982f4252019-06-21 11:42:27 +0800943
944#ifdef MMC_SUPPORTS_TUNING
945 if (mmc->clk_disable) {
946#ifdef CONFIG_FSL_USDHC
947 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
948#else
949 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
950#endif
951 } else {
952#ifdef CONFIG_FSL_USDHC
953 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
954 VENDORSPEC_CKEN);
955#else
956 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
957#endif
958 }
959
960 if (priv->mode != mmc->selected_mode) {
961 ret = esdhc_set_timing(mmc);
962 if (ret) {
963 printf("esdhc_set_timing error %d\n", ret);
964 return ret;
965 }
966 }
967
968 if (priv->signal_voltage != mmc->signal_voltage) {
969 ret = esdhc_set_voltage(mmc);
970 if (ret) {
971 printf("esdhc_set_voltage error %d\n", ret);
972 return ret;
973 }
974 }
975#endif
976
977 /* Set the bus width */
978 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
979
980 if (mmc->bus_width == 4)
981 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
982 else if (mmc->bus_width == 8)
983 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
984
985 return 0;
986}
987
988static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
989{
990 struct fsl_esdhc *regs = priv->esdhc_regs;
991 ulong start;
992
993 /* Reset the entire host controller */
994 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
995
996 /* Wait until the controller is available */
997 start = get_timer(0);
998 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
999 if (get_timer(start) > 1000)
1000 return -ETIMEDOUT;
1001 }
1002
1003#if defined(CONFIG_FSL_USDHC)
1004 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1005 esdhc_write32(&regs->mmcboot, 0x0);
1006 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1007 esdhc_write32(&regs->mixctrl, 0x0);
1008 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1009
1010 /* Put VEND_SPEC to default value */
1011 if (priv->vs18_enable)
1012 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1013 ESDHC_VENDORSPEC_VSELECT));
1014 else
1015 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1016
1017 /* Disable DLL_CTRL delay line */
1018 esdhc_write32(&regs->dllctrl, 0x0);
1019#endif
1020
1021#ifndef ARCH_MXC
1022 /* Enable cache snooping */
1023 esdhc_write32(&regs->scr, 0x00000040);
1024#endif
1025
1026#ifndef CONFIG_FSL_USDHC
1027 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1028#else
1029 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1030#endif
1031
1032 /* Set the initial clock speed */
1033 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1034
1035 /* Disable the BRR and BWR bits in IRQSTAT */
1036 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1037
1038#ifdef CONFIG_MCF5441x
1039 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1040#else
1041 /* Put the PROCTL reg back to the default */
1042 esdhc_write32(&regs->proctl, PROCTL_INIT);
1043#endif
1044
1045 /* Set timout to the maximum value */
1046 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1047
1048 return 0;
1049}
1050
1051static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1052{
1053 struct fsl_esdhc *regs = priv->esdhc_regs;
1054 int timeout = 1000;
1055
1056#ifdef CONFIG_ESDHC_DETECT_QUIRK
1057 if (CONFIG_ESDHC_DETECT_QUIRK)
1058 return 1;
1059#endif
1060
1061#if CONFIG_IS_ENABLED(DM_MMC)
1062 if (priv->non_removable)
1063 return 1;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001064
1065 if (priv->broken_cd)
1066 return 1;
Simon Glassfa4689a2019-12-06 21:41:35 -07001067#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001068 if (dm_gpio_is_valid(&priv->cd_gpio))
1069 return dm_gpio_get_value(&priv->cd_gpio);
1070#endif
1071#endif
1072
1073 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1074 udelay(1000);
1075
1076 return timeout > 0;
1077}
1078
1079static int esdhc_reset(struct fsl_esdhc *regs)
1080{
1081 ulong start;
1082
1083 /* reset the controller */
1084 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1085
1086 /* hardware clears the bit when it is done */
1087 start = get_timer(0);
1088 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1089 if (get_timer(start) > 100) {
1090 printf("MMC/SD: Reset never completed.\n");
1091 return -ETIMEDOUT;
1092 }
1093 }
1094
1095 return 0;
1096}
1097
1098#if !CONFIG_IS_ENABLED(DM_MMC)
1099static int esdhc_getcd(struct mmc *mmc)
1100{
1101 struct fsl_esdhc_priv *priv = mmc->priv;
1102
1103 return esdhc_getcd_common(priv);
1104}
1105
1106static int esdhc_init(struct mmc *mmc)
1107{
1108 struct fsl_esdhc_priv *priv = mmc->priv;
1109
1110 return esdhc_init_common(priv, mmc);
1111}
1112
1113static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1114 struct mmc_data *data)
1115{
1116 struct fsl_esdhc_priv *priv = mmc->priv;
1117
1118 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1119}
1120
1121static int esdhc_set_ios(struct mmc *mmc)
1122{
1123 struct fsl_esdhc_priv *priv = mmc->priv;
1124
1125 return esdhc_set_ios_common(priv, mmc);
1126}
1127
1128static const struct mmc_ops esdhc_ops = {
1129 .getcd = esdhc_getcd,
1130 .init = esdhc_init,
1131 .send_cmd = esdhc_send_cmd,
1132 .set_ios = esdhc_set_ios,
1133};
1134#endif
1135
1136static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1137 struct fsl_esdhc_plat *plat)
1138{
1139 struct mmc_config *cfg;
1140 struct fsl_esdhc *regs;
1141 u32 caps, voltage_caps;
1142 int ret;
1143
1144 if (!priv)
1145 return -EINVAL;
1146
1147 regs = priv->esdhc_regs;
1148
1149 /* First reset the eSDHC controller */
1150 ret = esdhc_reset(regs);
1151 if (ret)
1152 return ret;
1153
1154#ifdef CONFIG_MCF5441x
1155 /* ColdFire, using SDHC_DATA[3] for card detection */
1156 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1157#endif
1158
1159#ifndef CONFIG_FSL_USDHC
1160 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1161 | SYSCTL_IPGEN | SYSCTL_CKEN);
1162 /* Clearing tuning bits in case ROM has set it already */
1163 esdhc_write32(&regs->mixctrl, 0);
1164 esdhc_write32(&regs->autoc12err, 0);
1165 esdhc_write32(&regs->clktunectrlstatus, 0);
1166#else
1167 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1168 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1169#endif
1170
1171 if (priv->vs18_enable)
1172 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1173
1174 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
1175 cfg = &plat->cfg;
1176#ifndef CONFIG_DM_MMC
1177 memset(cfg, '\0', sizeof(*cfg));
1178#endif
1179
1180 voltage_caps = 0;
1181 caps = esdhc_read32(&regs->hostcapblt);
1182
1183#ifdef CONFIG_MCF5441x
1184 /*
1185 * MCF5441x RM declares in more points that sdhc clock speed must
1186 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1187 * from host capabilities.
1188 */
1189 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1190#endif
1191
1192#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1193 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1194 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1195#endif
1196
1197/* T4240 host controller capabilities register should have VS33 bit */
1198#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1199 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1200#endif
1201
1202 if (caps & ESDHC_HOSTCAPBLT_VS18)
1203 voltage_caps |= MMC_VDD_165_195;
1204 if (caps & ESDHC_HOSTCAPBLT_VS30)
1205 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1206 if (caps & ESDHC_HOSTCAPBLT_VS33)
1207 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1208
1209 cfg->name = "FSL_SDHC";
1210#if !CONFIG_IS_ENABLED(DM_MMC)
1211 cfg->ops = &esdhc_ops;
1212#endif
1213#ifdef CONFIG_SYS_SD_VOLTAGE
1214 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1215#else
1216 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1217#endif
1218 if ((cfg->voltages & voltage_caps) == 0) {
1219 printf("voltage not supported by controller\n");
1220 return -1;
1221 }
1222
1223 if (priv->bus_width == 8)
1224 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1225 else if (priv->bus_width == 4)
1226 cfg->host_caps = MMC_MODE_4BIT;
1227
1228 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1229#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1230 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1231#endif
1232
1233 if (priv->bus_width > 0) {
1234 if (priv->bus_width < 8)
1235 cfg->host_caps &= ~MMC_MODE_8BIT;
1236 if (priv->bus_width < 4)
1237 cfg->host_caps &= ~MMC_MODE_4BIT;
1238 }
1239
1240 if (caps & ESDHC_HOSTCAPBLT_HSS)
1241 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1242
1243#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1244 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1245 cfg->host_caps &= ~MMC_MODE_8BIT;
1246#endif
1247
1248 cfg->host_caps |= priv->caps;
1249
1250 cfg->f_min = 400000;
1251 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1252
1253 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1254
1255 writel(0, &regs->dllctrl);
1256 if (priv->flags & ESDHC_FLAG_USDHC) {
1257 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1258 u32 val = readl(&regs->tuning_ctrl);
1259
1260 val |= ESDHC_STD_TUNING_EN;
1261 val &= ~ESDHC_TUNING_START_TAP_MASK;
1262 val |= priv->tuning_start_tap;
1263 val &= ~ESDHC_TUNING_STEP_MASK;
1264 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1265 writel(val, &regs->tuning_ctrl);
1266 }
1267 }
1268
1269 return 0;
1270}
1271
1272#if !CONFIG_IS_ENABLED(DM_MMC)
1273static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1274 struct fsl_esdhc_priv *priv)
1275{
1276 if (!cfg || !priv)
1277 return -EINVAL;
1278
1279 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1280 priv->bus_width = cfg->max_bus_width;
1281 priv->sdhc_clk = cfg->sdhc_clk;
1282 priv->wp_enable = cfg->wp_enable;
1283 priv->vs18_enable = cfg->vs18_enable;
1284
1285 return 0;
1286};
1287
1288int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
1289{
1290 struct fsl_esdhc_plat *plat;
1291 struct fsl_esdhc_priv *priv;
1292 struct mmc *mmc;
1293 int ret;
1294
1295 if (!cfg)
1296 return -EINVAL;
1297
1298 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1299 if (!priv)
1300 return -ENOMEM;
1301 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1302 if (!plat) {
1303 free(priv);
1304 return -ENOMEM;
1305 }
1306
1307 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1308 if (ret) {
1309 debug("%s xlate failure\n", __func__);
1310 free(plat);
1311 free(priv);
1312 return ret;
1313 }
1314
1315 ret = fsl_esdhc_init(priv, plat);
1316 if (ret) {
1317 debug("%s init failure\n", __func__);
1318 free(plat);
1319 free(priv);
1320 return ret;
1321 }
1322
1323 mmc = mmc_create(&plat->cfg, priv);
1324 if (!mmc)
1325 return -EIO;
1326
1327 priv->mmc = mmc;
1328
1329 return 0;
1330}
1331
1332int fsl_esdhc_mmc_init(bd_t *bis)
1333{
1334 struct fsl_esdhc_cfg *cfg;
1335
1336 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1337 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1338 cfg->sdhc_clk = gd->arch.sdhc_clk;
1339 return fsl_esdhc_initialize(bis, cfg);
1340}
1341#endif
1342
Yangbo Lu982f4252019-06-21 11:42:27 +08001343#ifdef CONFIG_OF_LIBFDT
1344__weak int esdhc_status_fixup(void *blob, const char *compat)
1345{
1346#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1347 if (!hwconfig("esdhc")) {
1348 do_fixup_by_compat(blob, compat, "status", "disabled",
1349 sizeof("disabled"), 1);
1350 return 1;
1351 }
1352#endif
1353 return 0;
1354}
1355
1356void fdt_fixup_esdhc(void *blob, bd_t *bd)
1357{
1358 const char *compat = "fsl,esdhc";
1359
1360 if (esdhc_status_fixup(blob, compat))
1361 return;
1362
Yangbo Lu982f4252019-06-21 11:42:27 +08001363 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1364 gd->arch.sdhc_clk, 1);
Yangbo Lu982f4252019-06-21 11:42:27 +08001365}
1366#endif
1367
1368#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +08001369#include <asm/arch/clock.h>
Yangbo Lu982f4252019-06-21 11:42:27 +08001370__weak void init_clk_usdhc(u32 index)
1371{
1372}
1373
1374static int fsl_esdhc_probe(struct udevice *dev)
1375{
1376 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1377 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1378 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1379 const void *fdt = gd->fdt_blob;
1380 int node = dev_of_offset(dev);
1381 struct esdhc_soc_data *data =
1382 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1383#if CONFIG_IS_ENABLED(DM_REGULATOR)
1384 struct udevice *vqmmc_dev;
1385#endif
1386 fdt_addr_t addr;
1387 unsigned int val;
1388 struct mmc *mmc;
1389#if !CONFIG_IS_ENABLED(BLK)
1390 struct blk_desc *bdesc;
1391#endif
1392 int ret;
1393
1394 addr = dev_read_addr(dev);
1395 if (addr == FDT_ADDR_T_NONE)
1396 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001397 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lu982f4252019-06-21 11:42:27 +08001398 priv->dev = dev;
1399 priv->mode = -1;
Peng Fan3766a482019-07-10 09:35:24 +00001400 if (data)
Yangbo Lu982f4252019-06-21 11:42:27 +08001401 priv->flags = data->flags;
Yangbo Lu982f4252019-06-21 11:42:27 +08001402
1403 val = dev_read_u32_default(dev, "bus-width", -1);
1404 if (val == 8)
1405 priv->bus_width = 8;
1406 else if (val == 4)
1407 priv->bus_width = 4;
1408 else
1409 priv->bus_width = 1;
1410
1411 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1412 priv->tuning_step = val;
1413 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1414 ESDHC_TUNING_START_TAP_DEFAULT);
1415 priv->tuning_start_tap = val;
1416 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1417 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1418 priv->strobe_dll_delay_target = val;
1419
Fabio Estevam7e3d8a92020-01-06 20:11:27 -03001420 if (dev_read_bool(dev, "broken-cd"))
1421 priv->broken_cd = 1;
1422
Yangbo Lu982f4252019-06-21 11:42:27 +08001423 if (dev_read_bool(dev, "non-removable")) {
1424 priv->non_removable = 1;
1425 } else {
1426 priv->non_removable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001427#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001428 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1429 GPIOD_IS_IN);
1430#endif
1431 }
1432
1433 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1434 priv->wp_enable = 1;
1435 } else {
1436 priv->wp_enable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001437#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001438 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1439 GPIOD_IS_IN);
1440#endif
1441 }
1442
1443 priv->vs18_enable = 0;
1444
1445#if CONFIG_IS_ENABLED(DM_REGULATOR)
1446 /*
1447 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1448 * otherwise, emmc will work abnormally.
1449 */
1450 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1451 if (ret) {
1452 dev_dbg(dev, "no vqmmc-supply\n");
1453 } else {
1454 ret = regulator_set_enable(vqmmc_dev, true);
1455 if (ret) {
1456 dev_err(dev, "fail to enable vqmmc-supply\n");
1457 return ret;
1458 }
1459
1460 if (regulator_get_value(vqmmc_dev) == 1800000)
1461 priv->vs18_enable = 1;
1462 }
1463#endif
1464
Yangbo Lu982f4252019-06-21 11:42:27 +08001465 /*
1466 * TODO:
1467 * Because lack of clk driver, if SDHC clk is not enabled,
1468 * need to enable it first before this driver is invoked.
1469 *
1470 * we use MXC_ESDHC_CLK to get clk freq.
1471 * If one would like to make this function work,
1472 * the aliases should be provided in dts as this:
1473 *
1474 * aliases {
1475 * mmc0 = &usdhc1;
1476 * mmc1 = &usdhc2;
1477 * mmc2 = &usdhc3;
1478 * mmc3 = &usdhc4;
1479 * };
1480 * Then if your board only supports mmc2 and mmc3, but we can
1481 * correctly get the seq as 2 and 3, then let mxc_get_clock
1482 * work as expected.
1483 */
1484
1485 init_clk_usdhc(dev->seq);
1486
Giulio Benettidbdbc632020-01-10 15:51:45 +01001487#if CONFIG_IS_ENABLED(CLK)
1488 /* Assigned clock already set clock */
1489 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1490 if (ret) {
1491 printf("Failed to get per_clk\n");
1492 return ret;
1493 }
1494 ret = clk_enable(&priv->per_clk);
1495 if (ret) {
1496 printf("Failed to enable per_clk\n");
1497 return ret;
1498 }
Yangbo Lu982f4252019-06-21 11:42:27 +08001499
Giulio Benettidbdbc632020-01-10 15:51:45 +01001500 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1501#else
1502 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1503 if (priv->sdhc_clk <= 0) {
1504 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1505 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001506 }
Giulio Benettidbdbc632020-01-10 15:51:45 +01001507#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001508
1509 ret = fsl_esdhc_init(priv, plat);
1510 if (ret) {
1511 dev_err(dev, "fsl_esdhc_init failure\n");
1512 return ret;
1513 }
1514
Peng Fan3766a482019-07-10 09:35:24 +00001515 ret = mmc_of_parse(dev, &plat->cfg);
1516 if (ret)
1517 return ret;
1518
Yangbo Lu982f4252019-06-21 11:42:27 +08001519 mmc = &plat->mmc;
1520 mmc->cfg = &plat->cfg;
1521 mmc->dev = dev;
1522#if !CONFIG_IS_ENABLED(BLK)
1523 mmc->priv = priv;
1524
1525 /* Setup dsr related values */
1526 mmc->dsr_imp = 0;
1527 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1528 /* Setup the universal parts of the block interface just once */
1529 bdesc = mmc_get_blk_desc(mmc);
1530 bdesc->if_type = IF_TYPE_MMC;
1531 bdesc->removable = 1;
1532 bdesc->devnum = mmc_get_next_devnum();
1533 bdesc->block_read = mmc_bread;
1534 bdesc->block_write = mmc_bwrite;
1535 bdesc->block_erase = mmc_berase;
1536
1537 /* setup initial part type */
1538 bdesc->part_type = mmc->cfg->part_type;
1539 mmc_list_add(mmc);
1540#endif
1541
1542 upriv->mmc = mmc;
1543
1544 return esdhc_init_common(priv, mmc);
1545}
1546
1547#if CONFIG_IS_ENABLED(DM_MMC)
1548static int fsl_esdhc_get_cd(struct udevice *dev)
1549{
1550 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1551
1552 return esdhc_getcd_common(priv);
1553}
1554
1555static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1556 struct mmc_data *data)
1557{
1558 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1559 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1560
1561 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1562}
1563
1564static int fsl_esdhc_set_ios(struct udevice *dev)
1565{
1566 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1567 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1568
1569 return esdhc_set_ios_common(priv, &plat->mmc);
1570}
1571
Peng Fan69b9d3a2019-07-10 09:35:26 +00001572#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1573static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1574{
1575 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1576 struct fsl_esdhc *regs = priv->esdhc_regs;
1577 u32 m;
1578
1579 m = readl(&regs->mixctrl);
1580 m |= MIX_CTRL_HS400_ES;
1581 writel(m, &regs->mixctrl);
1582
1583 return 0;
1584}
1585#endif
1586
Yangbo Lu982f4252019-06-21 11:42:27 +08001587static const struct dm_mmc_ops fsl_esdhc_ops = {
1588 .get_cd = fsl_esdhc_get_cd,
1589 .send_cmd = fsl_esdhc_send_cmd,
1590 .set_ios = fsl_esdhc_set_ios,
1591#ifdef MMC_SUPPORTS_TUNING
1592 .execute_tuning = fsl_esdhc_execute_tuning,
1593#endif
Peng Fan69b9d3a2019-07-10 09:35:26 +00001594#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1595 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1596#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001597};
1598#endif
1599
1600static struct esdhc_soc_data usdhc_imx7d_data = {
1601 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1602 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1603 | ESDHC_FLAG_HS400,
Yangbo Lu982f4252019-06-21 11:42:27 +08001604};
1605
Peng Fan457fe962019-07-10 09:35:28 +00001606static struct esdhc_soc_data usdhc_imx8qm_data = {
1607 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1608 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1609 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1610};
1611
Yangbo Lu982f4252019-06-21 11:42:27 +08001612static const struct udevice_id fsl_esdhc_ids[] = {
1613 { .compatible = "fsl,imx53-esdhc", },
1614 { .compatible = "fsl,imx6ul-usdhc", },
1615 { .compatible = "fsl,imx6sx-usdhc", },
1616 { .compatible = "fsl,imx6sl-usdhc", },
1617 { .compatible = "fsl,imx6q-usdhc", },
1618 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1619 { .compatible = "fsl,imx7ulp-usdhc", },
Peng Fan457fe962019-07-10 09:35:28 +00001620 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fand7689fa2019-11-04 17:31:17 +08001621 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1622 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1623 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Giulio Benetti65b5ec12020-01-10 15:51:46 +01001624 { .compatible = "fsl,imxrt-usdhc", },
Yangbo Lu982f4252019-06-21 11:42:27 +08001625 { .compatible = "fsl,esdhc", },
1626 { /* sentinel */ }
1627};
1628
1629#if CONFIG_IS_ENABLED(BLK)
1630static int fsl_esdhc_bind(struct udevice *dev)
1631{
1632 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1633
1634 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1635}
1636#endif
1637
1638U_BOOT_DRIVER(fsl_esdhc) = {
1639 .name = "fsl-esdhc-mmc",
1640 .id = UCLASS_MMC,
1641 .of_match = fsl_esdhc_ids,
1642 .ops = &fsl_esdhc_ops,
1643#if CONFIG_IS_ENABLED(BLK)
1644 .bind = fsl_esdhc_bind,
1645#endif
1646 .probe = fsl_esdhc_probe,
1647 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
1648 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1649};
1650#endif