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