blob: 6bca2a9c829ed26311bb17eba6ea6fa88e243a8e [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 Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070023#include <linux/err.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080024#include <power/regulator.h>
25#include <malloc.h>
26#include <fsl_esdhc_imx.h>
27#include <fdt_support.h>
28#include <asm/io.h>
29#include <dm.h>
30#include <asm-generic/gpio.h>
31#include <dm/pinctrl.h>
32
33#if !CONFIG_IS_ENABLED(BLK)
34#include "mmc_private.h"
35#endif
36
37DECLARE_GLOBAL_DATA_PTR;
38
39#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
40 IRQSTATEN_CINT | \
41 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
42 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
43 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
44 IRQSTATEN_DINT)
45#define MAX_TUNING_LOOP 40
46#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
47
48struct fsl_esdhc {
49 uint dsaddr; /* SDMA system address register */
50 uint blkattr; /* Block attributes register */
51 uint cmdarg; /* Command argument register */
52 uint xfertyp; /* Transfer type register */
53 uint cmdrsp0; /* Command response 0 register */
54 uint cmdrsp1; /* Command response 1 register */
55 uint cmdrsp2; /* Command response 2 register */
56 uint cmdrsp3; /* Command response 3 register */
57 uint datport; /* Buffer data port register */
58 uint prsstat; /* Present state register */
59 uint proctl; /* Protocol control register */
60 uint sysctl; /* System Control Register */
61 uint irqstat; /* Interrupt status register */
62 uint irqstaten; /* Interrupt status enable register */
63 uint irqsigen; /* Interrupt signal enable register */
64 uint autoc12err; /* Auto CMD error status register */
65 uint hostcapblt; /* Host controller capabilities register */
66 uint wml; /* Watermark level register */
67 uint mixctrl; /* For USDHC */
68 char reserved1[4]; /* reserved */
69 uint fevt; /* Force event register */
70 uint admaes; /* ADMA error status register */
71 uint adsaddr; /* ADMA system address register */
72 char reserved2[4];
73 uint dllctrl;
74 uint dllstat;
75 uint clktunectrlstatus;
76 char reserved3[4];
77 uint strobe_dllctrl;
78 uint strobe_dllstat;
79 char reserved4[72];
80 uint vendorspec;
81 uint mmcboot;
82 uint vendorspec2;
Giulio Benetti65b5ec12020-01-10 15:51:46 +010083 uint tuning_ctrl; /* on i.MX6/7/8/RT */
Yangbo Lu982f4252019-06-21 11:42:27 +080084 char reserved5[44];
85 uint hostver; /* Host controller version register */
86 char reserved6[4]; /* reserved */
87 uint dmaerraddr; /* DMA error address register */
88 char reserved7[4]; /* reserved */
89 uint dmaerrattr; /* DMA error attribute register */
90 char reserved8[4]; /* reserved */
91 uint hostcapblt2; /* Host controller capabilities register 2 */
92 char reserved9[8]; /* reserved */
93 uint tcr; /* Tuning control register */
94 char reserved10[28]; /* reserved */
95 uint sddirctl; /* SD direction control register */
96 char reserved11[712];/* reserved */
97 uint scr; /* eSDHC control register */
98};
99
100struct fsl_esdhc_plat {
101 struct mmc_config cfg;
102 struct mmc mmc;
103};
104
105struct esdhc_soc_data {
106 u32 flags;
Yangbo Lu982f4252019-06-21 11:42:27 +0800107};
108
109/**
110 * struct fsl_esdhc_priv
111 *
112 * @esdhc_regs: registers of the sdhc controller
113 * @sdhc_clk: Current clk of the sdhc controller
114 * @bus_width: bus width, 1bit, 4bit or 8bit
115 * @cfg: mmc config
116 * @mmc: mmc
117 * Following is used when Driver Model is enabled for MMC
118 * @dev: pointer for the device
119 * @non_removable: 0: removable; 1: non-removable
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300120 * @broken_cd: 0: use GPIO for card detect; 1: Do not use GPIO for card detect
Yangbo Lu982f4252019-06-21 11:42:27 +0800121 * @wp_enable: 1: enable checking wp; 0: no check
122 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
123 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h
124 * @caps: controller capabilities
125 * @tuning_step: tuning step setting in tuning_ctrl register
126 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
127 * @strobe_dll_delay_target: settings in strobe_dllctrl
128 * @signal_voltage: indicating the current voltage
129 * @cd_gpio: gpio for card detection
130 * @wp_gpio: gpio for write protection
131 */
132struct fsl_esdhc_priv {
133 struct fsl_esdhc *esdhc_regs;
134 unsigned int sdhc_clk;
135 struct clk per_clk;
136 unsigned int clock;
137 unsigned int mode;
138 unsigned int bus_width;
139#if !CONFIG_IS_ENABLED(BLK)
140 struct mmc *mmc;
141#endif
142 struct udevice *dev;
143 int non_removable;
Fabio Estevam7e3d8a92020-01-06 20:11:27 -0300144 int broken_cd;
Yangbo Lu982f4252019-06-21 11:42:27 +0800145 int wp_enable;
146 int vs18_enable;
147 u32 flags;
148 u32 caps;
149 u32 tuning_step;
150 u32 tuning_start_tap;
151 u32 strobe_dll_delay_target;
152 u32 signal_voltage;
Ye Li7aa20fd2019-07-11 03:29:02 +0000153#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800154 struct udevice *vqmmc_dev;
155 struct udevice *vmmc_dev;
156#endif
Simon Glassfa4689a2019-12-06 21:41:35 -0700157#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +0800158 struct gpio_desc cd_gpio;
159 struct gpio_desc wp_gpio;
160#endif
161};
162
163/* Return the XFERTYP flags for a given command and data packet */
164static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
165{
166 uint xfertyp = 0;
167
168 if (data) {
169 xfertyp |= XFERTYP_DPSEL;
170#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
171 xfertyp |= XFERTYP_DMAEN;
172#endif
173 if (data->blocks > 1) {
174 xfertyp |= XFERTYP_MSBSEL;
175 xfertyp |= XFERTYP_BCEN;
176#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
177 xfertyp |= XFERTYP_AC12EN;
178#endif
179 }
180
181 if (data->flags & MMC_DATA_READ)
182 xfertyp |= XFERTYP_DTDSEL;
183 }
184
185 if (cmd->resp_type & MMC_RSP_CRC)
186 xfertyp |= XFERTYP_CCCEN;
187 if (cmd->resp_type & MMC_RSP_OPCODE)
188 xfertyp |= XFERTYP_CICEN;
189 if (cmd->resp_type & MMC_RSP_136)
190 xfertyp |= XFERTYP_RSPTYP_136;
191 else if (cmd->resp_type & MMC_RSP_BUSY)
192 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
193 else if (cmd->resp_type & MMC_RSP_PRESENT)
194 xfertyp |= XFERTYP_RSPTYP_48;
195
196 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
197 xfertyp |= XFERTYP_CMDTYP_ABORT;
198
199 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
200}
201
202#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
203/*
204 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
205 */
206static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
207 struct mmc_data *data)
208{
209 struct fsl_esdhc *regs = priv->esdhc_regs;
210 uint blocks;
211 char *buffer;
212 uint databuf;
213 uint size;
214 uint irqstat;
215 ulong start;
216
217 if (data->flags & MMC_DATA_READ) {
218 blocks = data->blocks;
219 buffer = data->dest;
220 while (blocks) {
221 start = get_timer(0);
222 size = data->blocksize;
223 irqstat = esdhc_read32(&regs->irqstat);
224 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
225 if (get_timer(start) > PIO_TIMEOUT) {
226 printf("\nData Read Failed in PIO Mode.");
227 return;
228 }
229 }
230 while (size && (!(irqstat & IRQSTAT_TC))) {
231 udelay(100); /* Wait before last byte transfer complete */
232 irqstat = esdhc_read32(&regs->irqstat);
233 databuf = in_le32(&regs->datport);
234 *((uint *)buffer) = databuf;
235 buffer += 4;
236 size -= 4;
237 }
238 blocks--;
239 }
240 } else {
241 blocks = data->blocks;
242 buffer = (char *)data->src;
243 while (blocks) {
244 start = get_timer(0);
245 size = data->blocksize;
246 irqstat = esdhc_read32(&regs->irqstat);
247 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
248 if (get_timer(start) > PIO_TIMEOUT) {
249 printf("\nData Write Failed in PIO Mode.");
250 return;
251 }
252 }
253 while (size && (!(irqstat & IRQSTAT_TC))) {
254 udelay(100); /* Wait before last byte transfer complete */
255 databuf = *((uint *)buffer);
256 buffer += 4;
257 size -= 4;
258 irqstat = esdhc_read32(&regs->irqstat);
259 out_le32(&regs->datport, databuf);
260 }
261 blocks--;
262 }
263 }
264}
265#endif
266
267static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
268 struct mmc_data *data)
269{
270 int timeout;
271 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu16b38542019-06-21 11:42:30 +0800272#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800273 dma_addr_t addr;
274#endif
275 uint wml_value;
276
277 wml_value = data->blocksize/4;
278
279 if (data->flags & MMC_DATA_READ) {
280 if (wml_value > WML_RD_WML_MAX)
281 wml_value = WML_RD_WML_MAX_VAL;
282
283 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
284#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
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 addr = virt_to_phys((void *)(data->dest));
287 if (upper_32_bits(addr))
288 printf("Error found for upper 32 bits\n");
289 else
290 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
291#else
292 esdhc_write32(&regs->dsaddr, (u32)data->dest);
293#endif
294#endif
295 } else {
296#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
297 flush_dcache_range((ulong)data->src,
298 (ulong)data->src+data->blocks
299 *data->blocksize);
300#endif
301 if (wml_value > WML_WR_WML_MAX)
302 wml_value = WML_WR_WML_MAX_VAL;
303 if (priv->wp_enable) {
304 if ((esdhc_read32(&regs->prsstat) &
305 PRSSTAT_WPSPL) == 0) {
306 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
307 return -ETIMEDOUT;
308 }
309 } else {
Simon Glassfa4689a2019-12-06 21:41:35 -0700310#if CONFIG_IS_ENABLED(DM_GPIO)
311 if (dm_gpio_is_valid(&priv->wp_gpio) &&
312 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800313 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
314 return -ETIMEDOUT;
315 }
316#endif
317 }
318
319 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
320 wml_value << 16);
321#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu16b38542019-06-21 11:42:30 +0800322#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800323 addr = virt_to_phys((void *)(data->src));
324 if (upper_32_bits(addr))
325 printf("Error found for upper 32 bits\n");
326 else
327 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
328#else
329 esdhc_write32(&regs->dsaddr, (u32)data->src);
330#endif
331#endif
332 }
333
334 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
335
336 /* Calculate the timeout period for data transactions */
337 /*
338 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
339 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
340 * So, Number of SD Clock cycles for 0.25sec should be minimum
341 * (SD Clock/sec * 0.25 sec) SD Clock cycles
342 * = (mmc->clock * 1/4) SD Clock cycles
343 * As 1) >= 2)
344 * => (2^(timeout+13)) >= mmc->clock * 1/4
345 * Taking log2 both the sides
346 * => timeout + 13 >= log2(mmc->clock/4)
347 * Rounding up to next power of 2
348 * => timeout + 13 = log2(mmc->clock/4) + 1
349 * => timeout + 13 = fls(mmc->clock/4)
350 *
351 * However, the MMC spec "It is strongly recommended for hosts to
352 * implement more than 500ms timeout value even if the card
353 * indicates the 250ms maximum busy length." Even the previous
354 * value of 300ms is known to be insufficient for some cards.
355 * So, we use
356 * => timeout + 13 = fls(mmc->clock/2)
357 */
358 timeout = fls(mmc->clock/2);
359 timeout -= 13;
360
361 if (timeout > 14)
362 timeout = 14;
363
364 if (timeout < 0)
365 timeout = 0;
366
367#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
368 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
369 timeout++;
370#endif
371
372#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
373 timeout = 0xE;
374#endif
375 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
376
377 return 0;
378}
379
380static void check_and_invalidate_dcache_range
381 (struct mmc_cmd *cmd,
382 struct mmc_data *data) {
383 unsigned start = 0;
384 unsigned end = 0;
385 unsigned size = roundup(ARCH_DMA_MINALIGN,
386 data->blocks*data->blocksize);
Yangbo Lu16b38542019-06-21 11:42:30 +0800387#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800388 dma_addr_t addr;
389
390 addr = virt_to_phys((void *)(data->dest));
391 if (upper_32_bits(addr))
392 printf("Error found for upper 32 bits\n");
393 else
394 start = lower_32_bits(addr);
395#else
396 start = (unsigned)data->dest;
397#endif
398 end = start + size;
399 invalidate_dcache_range(start, end);
400}
401
402#ifdef CONFIG_MCF5441x
403/*
404 * Swaps 32-bit words to little-endian byte order.
405 */
406static inline void sd_swap_dma_buff(struct mmc_data *data)
407{
408 int i, size = data->blocksize >> 2;
409 u32 *buffer = (u32 *)data->dest;
410 u32 sw;
411
412 while (data->blocks--) {
413 for (i = 0; i < size; i++) {
414 sw = __sw32(*buffer);
415 *buffer++ = sw;
416 }
417 }
418}
419#endif
420
421/*
422 * Sends a command out on the bus. Takes the mmc pointer,
423 * a command pointer, and an optional data pointer.
424 */
425static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
426 struct mmc_cmd *cmd, struct mmc_data *data)
427{
428 int err = 0;
429 uint xfertyp;
430 uint irqstat;
431 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
432 struct fsl_esdhc *regs = priv->esdhc_regs;
433 unsigned long start;
434
435#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
436 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
437 return 0;
438#endif
439
440 esdhc_write32(&regs->irqstat, -1);
441
442 sync();
443
444 /* Wait for the bus to be idle */
445 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
446 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
447 ;
448
449 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
450 ;
451
452 /* Wait at least 8 SD clock cycles before the next command */
453 /*
454 * Note: This is way more than 8 cycles, but 1ms seems to
455 * resolve timing issues with some cards
456 */
457 udelay(1000);
458
459 /* Set up for a data transfer if we have one */
460 if (data) {
461 err = esdhc_setup_data(priv, mmc, data);
462 if(err)
463 return err;
464
465 if (data->flags & MMC_DATA_READ)
466 check_and_invalidate_dcache_range(cmd, data);
467 }
468
469 /* Figure out the transfer arguments */
470 xfertyp = esdhc_xfertyp(cmd, data);
471
472 /* Mask all irqs */
473 esdhc_write32(&regs->irqsigen, 0);
474
475 /* Send the command */
476 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
477#if defined(CONFIG_FSL_USDHC)
478 esdhc_write32(&regs->mixctrl,
479 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
480 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
481 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
482#else
483 esdhc_write32(&regs->xfertyp, xfertyp);
484#endif
485
486 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
487 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
488 flags = IRQSTAT_BRR;
489
490 /* Wait for the command to complete */
491 start = get_timer(0);
492 while (!(esdhc_read32(&regs->irqstat) & flags)) {
493 if (get_timer(start) > 1000) {
494 err = -ETIMEDOUT;
495 goto out;
496 }
497 }
498
499 irqstat = esdhc_read32(&regs->irqstat);
500
501 if (irqstat & CMD_ERR) {
502 err = -ECOMM;
503 goto out;
504 }
505
506 if (irqstat & IRQSTAT_CTOE) {
507 err = -ETIMEDOUT;
508 goto out;
509 }
510
511 /* Switch voltage to 1.8V if CMD11 succeeded */
512 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
513 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
514
515 printf("Run CMD11 1.8V switch\n");
516 /* Sleep for 5 ms - max time for card to switch to 1.8V */
517 udelay(5000);
518 }
519
520 /* Workaround for ESDHC errata ENGcm03648 */
521 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan3dbea592019-07-10 09:35:30 +0000522 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800523
Peng Fan3dbea592019-07-10 09:35:30 +0000524 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-06-21 11:42:27 +0800525 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
526 PRSSTAT_DAT0)) {
527 udelay(100);
528 timeout--;
529 }
530
531 if (timeout <= 0) {
532 printf("Timeout waiting for DAT0 to go high!\n");
533 err = -ETIMEDOUT;
534 goto out;
535 }
536 }
537
538 /* Copy the response to the response buffer */
539 if (cmd->resp_type & MMC_RSP_136) {
540 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
541
542 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
543 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
544 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
545 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
546 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
547 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
548 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
549 cmd->response[3] = (cmdrsp0 << 8);
550 } else
551 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
552
553 /* Wait until all of the blocks are transferred */
554 if (data) {
555#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
556 esdhc_pio_read_write(priv, data);
557#else
558 flags = DATA_COMPLETE;
559 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
560 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
561 flags = IRQSTAT_BRR;
562 }
563
564 do {
565 irqstat = esdhc_read32(&regs->irqstat);
566
567 if (irqstat & IRQSTAT_DTOE) {
568 err = -ETIMEDOUT;
569 goto out;
570 }
571
572 if (irqstat & DATA_ERR) {
573 err = -ECOMM;
574 goto out;
575 }
576 } while ((irqstat & flags) != flags);
577
578 /*
579 * Need invalidate the dcache here again to avoid any
580 * cache-fill during the DMA operations such as the
581 * speculative pre-fetching etc.
582 */
583 if (data->flags & MMC_DATA_READ) {
584 check_and_invalidate_dcache_range(cmd, data);
585#ifdef CONFIG_MCF5441x
586 sd_swap_dma_buff(data);
587#endif
588 }
589#endif
590 }
591
592out:
593 /* Reset CMD and DATA portions on error */
594 if (err) {
595 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
596 SYSCTL_RSTC);
597 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
598 ;
599
600 if (data) {
601 esdhc_write32(&regs->sysctl,
602 esdhc_read32(&regs->sysctl) |
603 SYSCTL_RSTD);
604 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
605 ;
606 }
607
608 /* If this was CMD11, then notify that power cycle is needed */
609 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
610 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
611 }
612
613 esdhc_write32(&regs->irqstat, -1);
614
615 return err;
616}
617
618static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
619{
620 struct fsl_esdhc *regs = priv->esdhc_regs;
621 int div = 1;
622#ifdef ARCH_MXC
623#ifdef CONFIG_MX53
624 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
625 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
626#else
627 int pre_div = 1;
628#endif
629#else
630 int pre_div = 2;
631#endif
632 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
633 int sdhc_clk = priv->sdhc_clk;
634 uint clk;
635
Yangbo Lu982f4252019-06-21 11:42:27 +0800636 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
637 pre_div *= 2;
638
639 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
640 div++;
641
642 pre_div >>= 1;
643 div -= 1;
644
645 clk = (pre_div << 8) | (div << 4);
646
647#ifdef CONFIG_FSL_USDHC
648 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
649#else
650 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
651#endif
652
653 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
654
655 udelay(10000);
656
657#ifdef CONFIG_FSL_USDHC
658 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
659#else
660 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
661#endif
662
663 priv->clock = clock;
664}
665
Yangbo Lu982f4252019-06-21 11:42:27 +0800666#ifdef MMC_SUPPORTS_TUNING
667static int esdhc_change_pinstate(struct udevice *dev)
668{
669 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
670 int ret;
671
672 switch (priv->mode) {
673 case UHS_SDR50:
674 case UHS_DDR50:
675 ret = pinctrl_select_state(dev, "state_100mhz");
676 break;
677 case UHS_SDR104:
678 case MMC_HS_200:
679 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000680 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800681 ret = pinctrl_select_state(dev, "state_200mhz");
682 break;
683 default:
684 ret = pinctrl_select_state(dev, "default");
685 break;
686 }
687
688 if (ret)
689 printf("%s %d error\n", __func__, priv->mode);
690
691 return ret;
692}
693
694static void esdhc_reset_tuning(struct mmc *mmc)
695{
696 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
697 struct fsl_esdhc *regs = priv->esdhc_regs;
698
699 if (priv->flags & ESDHC_FLAG_USDHC) {
700 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
701 esdhc_clrbits32(&regs->autoc12err,
702 MIX_CTRL_SMPCLK_SEL |
703 MIX_CTRL_EXE_TUNE);
704 }
705 }
706}
707
708static void esdhc_set_strobe_dll(struct mmc *mmc)
709{
710 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
711 struct fsl_esdhc *regs = priv->esdhc_regs;
712 u32 val;
713
714 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
715 writel(ESDHC_STROBE_DLL_CTRL_RESET, &regs->strobe_dllctrl);
716
717 /*
718 * enable strobe dll ctrl and adjust the delay target
719 * for the uSDHC loopback read clock
720 */
721 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
722 (priv->strobe_dll_delay_target <<
723 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
724 writel(val, &regs->strobe_dllctrl);
725 /* wait 1us to make sure strobe dll status register stable */
726 mdelay(1);
727 val = readl(&regs->strobe_dllstat);
728 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
729 pr_warn("HS400 strobe DLL status REF not lock!\n");
730 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
731 pr_warn("HS400 strobe DLL status SLV not lock!\n");
732 }
733}
734
735static int esdhc_set_timing(struct mmc *mmc)
736{
737 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
738 struct fsl_esdhc *regs = priv->esdhc_regs;
739 u32 mixctrl;
740
741 mixctrl = readl(&regs->mixctrl);
742 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
743
744 switch (mmc->selected_mode) {
745 case MMC_LEGACY:
Yangbo Lu982f4252019-06-21 11:42:27 +0800746 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