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