blob: f7b754bd9d37f520e92ecfcbb59c92a6d1eeb583 [file] [log] [blame]
Yangbo Lu982f4252019-06-21 11:42:27 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
4 * Copyright 2019 NXP Semiconductors
5 * Andy Fleming
6 * Yangbo Lu <yangbo.lu@nxp.com>
7 *
8 * Based vaguely on the pxa mmc code:
9 * (C) Copyright 2003
10 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
11 */
12
13#include <config.h>
14#include <common.h>
15#include <command.h>
16#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Yangbo Lu982f4252019-06-21 11:42:27 +080018#include <errno.h>
19#include <hwconfig.h>
20#include <mmc.h>
21#include <part.h>
22#include <power/regulator.h>
23#include <malloc.h>
24#include <fsl_esdhc_imx.h>
25#include <fdt_support.h>
26#include <asm/io.h>
27#include <dm.h>
28#include <asm-generic/gpio.h>
29#include <dm/pinctrl.h>
30
31#if !CONFIG_IS_ENABLED(BLK)
32#include "mmc_private.h"
33#endif
34
35DECLARE_GLOBAL_DATA_PTR;
36
37#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
38 IRQSTATEN_CINT | \
39 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
40 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
41 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
42 IRQSTATEN_DINT)
43#define MAX_TUNING_LOOP 40
44#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
45
46struct fsl_esdhc {
47 uint dsaddr; /* SDMA system address register */
48 uint blkattr; /* Block attributes register */
49 uint cmdarg; /* Command argument register */
50 uint xfertyp; /* Transfer type register */
51 uint cmdrsp0; /* Command response 0 register */
52 uint cmdrsp1; /* Command response 1 register */
53 uint cmdrsp2; /* Command response 2 register */
54 uint cmdrsp3; /* Command response 3 register */
55 uint datport; /* Buffer data port register */
56 uint prsstat; /* Present state register */
57 uint proctl; /* Protocol control register */
58 uint sysctl; /* System Control Register */
59 uint irqstat; /* Interrupt status register */
60 uint irqstaten; /* Interrupt status enable register */
61 uint irqsigen; /* Interrupt signal enable register */
62 uint autoc12err; /* Auto CMD error status register */
63 uint hostcapblt; /* Host controller capabilities register */
64 uint wml; /* Watermark level register */
65 uint mixctrl; /* For USDHC */
66 char reserved1[4]; /* reserved */
67 uint fevt; /* Force event register */
68 uint admaes; /* ADMA error status register */
69 uint adsaddr; /* ADMA system address register */
70 char reserved2[4];
71 uint dllctrl;
72 uint dllstat;
73 uint clktunectrlstatus;
74 char reserved3[4];
75 uint strobe_dllctrl;
76 uint strobe_dllstat;
77 char reserved4[72];
78 uint vendorspec;
79 uint mmcboot;
80 uint vendorspec2;
81 uint tuning_ctrl; /* on i.MX6/7/8 */
82 char reserved5[44];
83 uint hostver; /* Host controller version register */
84 char reserved6[4]; /* reserved */
85 uint dmaerraddr; /* DMA error address register */
86 char reserved7[4]; /* reserved */
87 uint dmaerrattr; /* DMA error attribute register */
88 char reserved8[4]; /* reserved */
89 uint hostcapblt2; /* Host controller capabilities register 2 */
90 char reserved9[8]; /* reserved */
91 uint tcr; /* Tuning control register */
92 char reserved10[28]; /* reserved */
93 uint sddirctl; /* SD direction control register */
94 char reserved11[712];/* reserved */
95 uint scr; /* eSDHC control register */
96};
97
98struct fsl_esdhc_plat {
99 struct mmc_config cfg;
100 struct mmc mmc;
101};
102
103struct esdhc_soc_data {
104 u32 flags;
Yangbo Lu982f4252019-06-21 11:42:27 +0800105};
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;
Ye Li7aa20fd2019-07-11 03:29:02 +0000149#if CONFIG_IS_ENABLED(DM_REGULATOR)
Yangbo Lu982f4252019-06-21 11:42:27 +0800150 struct udevice *vqmmc_dev;
151 struct udevice *vmmc_dev;
152#endif
Simon Glassfa4689a2019-12-06 21:41:35 -0700153#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +0800154 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 {
Simon Glassfa4689a2019-12-06 21:41:35 -0700306#if CONFIG_IS_ENABLED(DM_GPIO)
307 if (dm_gpio_is_valid(&priv->wp_gpio) &&
308 dm_gpio_get_value(&priv->wp_gpio)) {
Yangbo Lu982f4252019-06-21 11:42:27 +0800309 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
310 return -ETIMEDOUT;
311 }
312#endif
313 }
314
315 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
316 wml_value << 16);
317#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu16b38542019-06-21 11:42:30 +0800318#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800319 addr = virt_to_phys((void *)(data->src));
320 if (upper_32_bits(addr))
321 printf("Error found for upper 32 bits\n");
322 else
323 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
324#else
325 esdhc_write32(&regs->dsaddr, (u32)data->src);
326#endif
327#endif
328 }
329
330 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
331
332 /* Calculate the timeout period for data transactions */
333 /*
334 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
335 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
336 * So, Number of SD Clock cycles for 0.25sec should be minimum
337 * (SD Clock/sec * 0.25 sec) SD Clock cycles
338 * = (mmc->clock * 1/4) SD Clock cycles
339 * As 1) >= 2)
340 * => (2^(timeout+13)) >= mmc->clock * 1/4
341 * Taking log2 both the sides
342 * => timeout + 13 >= log2(mmc->clock/4)
343 * Rounding up to next power of 2
344 * => timeout + 13 = log2(mmc->clock/4) + 1
345 * => timeout + 13 = fls(mmc->clock/4)
346 *
347 * However, the MMC spec "It is strongly recommended for hosts to
348 * implement more than 500ms timeout value even if the card
349 * indicates the 250ms maximum busy length." Even the previous
350 * value of 300ms is known to be insufficient for some cards.
351 * So, we use
352 * => timeout + 13 = fls(mmc->clock/2)
353 */
354 timeout = fls(mmc->clock/2);
355 timeout -= 13;
356
357 if (timeout > 14)
358 timeout = 14;
359
360 if (timeout < 0)
361 timeout = 0;
362
363#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
364 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
365 timeout++;
366#endif
367
368#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
369 timeout = 0xE;
370#endif
371 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
372
373 return 0;
374}
375
376static void check_and_invalidate_dcache_range
377 (struct mmc_cmd *cmd,
378 struct mmc_data *data) {
379 unsigned start = 0;
380 unsigned end = 0;
381 unsigned size = roundup(ARCH_DMA_MINALIGN,
382 data->blocks*data->blocksize);
Yangbo Lu16b38542019-06-21 11:42:30 +0800383#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu982f4252019-06-21 11:42:27 +0800384 dma_addr_t addr;
385
386 addr = virt_to_phys((void *)(data->dest));
387 if (upper_32_bits(addr))
388 printf("Error found for upper 32 bits\n");
389 else
390 start = lower_32_bits(addr);
391#else
392 start = (unsigned)data->dest;
393#endif
394 end = start + size;
395 invalidate_dcache_range(start, end);
396}
397
398#ifdef CONFIG_MCF5441x
399/*
400 * Swaps 32-bit words to little-endian byte order.
401 */
402static inline void sd_swap_dma_buff(struct mmc_data *data)
403{
404 int i, size = data->blocksize >> 2;
405 u32 *buffer = (u32 *)data->dest;
406 u32 sw;
407
408 while (data->blocks--) {
409 for (i = 0; i < size; i++) {
410 sw = __sw32(*buffer);
411 *buffer++ = sw;
412 }
413 }
414}
415#endif
416
417/*
418 * Sends a command out on the bus. Takes the mmc pointer,
419 * a command pointer, and an optional data pointer.
420 */
421static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
422 struct mmc_cmd *cmd, struct mmc_data *data)
423{
424 int err = 0;
425 uint xfertyp;
426 uint irqstat;
427 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
428 struct fsl_esdhc *regs = priv->esdhc_regs;
429 unsigned long start;
430
431#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
432 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
433 return 0;
434#endif
435
436 esdhc_write32(&regs->irqstat, -1);
437
438 sync();
439
440 /* Wait for the bus to be idle */
441 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
442 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
443 ;
444
445 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
446 ;
447
448 /* Wait at least 8 SD clock cycles before the next command */
449 /*
450 * Note: This is way more than 8 cycles, but 1ms seems to
451 * resolve timing issues with some cards
452 */
453 udelay(1000);
454
455 /* Set up for a data transfer if we have one */
456 if (data) {
457 err = esdhc_setup_data(priv, mmc, data);
458 if(err)
459 return err;
460
461 if (data->flags & MMC_DATA_READ)
462 check_and_invalidate_dcache_range(cmd, data);
463 }
464
465 /* Figure out the transfer arguments */
466 xfertyp = esdhc_xfertyp(cmd, data);
467
468 /* Mask all irqs */
469 esdhc_write32(&regs->irqsigen, 0);
470
471 /* Send the command */
472 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
473#if defined(CONFIG_FSL_USDHC)
474 esdhc_write32(&regs->mixctrl,
475 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
476 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
477 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
478#else
479 esdhc_write32(&regs->xfertyp, xfertyp);
480#endif
481
482 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
483 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
484 flags = IRQSTAT_BRR;
485
486 /* Wait for the command to complete */
487 start = get_timer(0);
488 while (!(esdhc_read32(&regs->irqstat) & flags)) {
489 if (get_timer(start) > 1000) {
490 err = -ETIMEDOUT;
491 goto out;
492 }
493 }
494
495 irqstat = esdhc_read32(&regs->irqstat);
496
497 if (irqstat & CMD_ERR) {
498 err = -ECOMM;
499 goto out;
500 }
501
502 if (irqstat & IRQSTAT_CTOE) {
503 err = -ETIMEDOUT;
504 goto out;
505 }
506
507 /* Switch voltage to 1.8V if CMD11 succeeded */
508 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
509 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
510
511 printf("Run CMD11 1.8V switch\n");
512 /* Sleep for 5 ms - max time for card to switch to 1.8V */
513 udelay(5000);
514 }
515
516 /* Workaround for ESDHC errata ENGcm03648 */
517 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Peng Fan3dbea592019-07-10 09:35:30 +0000518 int timeout = 50000;
Yangbo Lu982f4252019-06-21 11:42:27 +0800519
Peng Fan3dbea592019-07-10 09:35:30 +0000520 /* Poll on DATA0 line for cmd with busy signal for 5000 ms */
Yangbo Lu982f4252019-06-21 11:42:27 +0800521 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
522 PRSSTAT_DAT0)) {
523 udelay(100);
524 timeout--;
525 }
526
527 if (timeout <= 0) {
528 printf("Timeout waiting for DAT0 to go high!\n");
529 err = -ETIMEDOUT;
530 goto out;
531 }
532 }
533
534 /* Copy the response to the response buffer */
535 if (cmd->resp_type & MMC_RSP_136) {
536 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
537
538 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
539 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
540 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
541 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
542 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
543 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
544 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
545 cmd->response[3] = (cmdrsp0 << 8);
546 } else
547 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
548
549 /* Wait until all of the blocks are transferred */
550 if (data) {
551#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
552 esdhc_pio_read_write(priv, data);
553#else
554 flags = DATA_COMPLETE;
555 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
556 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
557 flags = IRQSTAT_BRR;
558 }
559
560 do {
561 irqstat = esdhc_read32(&regs->irqstat);
562
563 if (irqstat & IRQSTAT_DTOE) {
564 err = -ETIMEDOUT;
565 goto out;
566 }
567
568 if (irqstat & DATA_ERR) {
569 err = -ECOMM;
570 goto out;
571 }
572 } while ((irqstat & flags) != flags);
573
574 /*
575 * Need invalidate the dcache here again to avoid any
576 * cache-fill during the DMA operations such as the
577 * speculative pre-fetching etc.
578 */
579 if (data->flags & MMC_DATA_READ) {
580 check_and_invalidate_dcache_range(cmd, data);
581#ifdef CONFIG_MCF5441x
582 sd_swap_dma_buff(data);
583#endif
584 }
585#endif
586 }
587
588out:
589 /* Reset CMD and DATA portions on error */
590 if (err) {
591 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
592 SYSCTL_RSTC);
593 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
594 ;
595
596 if (data) {
597 esdhc_write32(&regs->sysctl,
598 esdhc_read32(&regs->sysctl) |
599 SYSCTL_RSTD);
600 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
601 ;
602 }
603
604 /* If this was CMD11, then notify that power cycle is needed */
605 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
606 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
607 }
608
609 esdhc_write32(&regs->irqstat, -1);
610
611 return err;
612}
613
614static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
615{
616 struct fsl_esdhc *regs = priv->esdhc_regs;
617 int div = 1;
618#ifdef ARCH_MXC
619#ifdef CONFIG_MX53
620 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
621 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
622#else
623 int pre_div = 1;
624#endif
625#else
626 int pre_div = 2;
627#endif
628 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
629 int sdhc_clk = priv->sdhc_clk;
630 uint clk;
631
Yangbo Lu982f4252019-06-21 11:42:27 +0800632 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
633 pre_div *= 2;
634
635 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
636 div++;
637
638 pre_div >>= 1;
639 div -= 1;
640
641 clk = (pre_div << 8) | (div << 4);
642
643#ifdef CONFIG_FSL_USDHC
644 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
645#else
646 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
647#endif
648
649 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
650
651 udelay(10000);
652
653#ifdef CONFIG_FSL_USDHC
654 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
655#else
656 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
657#endif
658
659 priv->clock = clock;
660}
661
662#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
663static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
664{
665 struct fsl_esdhc *regs = priv->esdhc_regs;
666 u32 value;
667 u32 time_out;
668
669 value = esdhc_read32(&regs->sysctl);
670
671 if (enable)
672 value |= SYSCTL_CKEN;
673 else
674 value &= ~SYSCTL_CKEN;
675
676 esdhc_write32(&regs->sysctl, value);
677
678 time_out = 20;
679 value = PRSSTAT_SDSTB;
680 while (!(esdhc_read32(&regs->prsstat) & value)) {
681 if (time_out == 0) {
682 printf("fsl_esdhc: Internal clock never stabilised.\n");
683 break;
684 }
685 time_out--;
686 mdelay(1);
687 }
688}
689#endif
690
691#ifdef MMC_SUPPORTS_TUNING
692static int esdhc_change_pinstate(struct udevice *dev)
693{
694 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
695 int ret;
696
697 switch (priv->mode) {
698 case UHS_SDR50:
699 case UHS_DDR50:
700 ret = pinctrl_select_state(dev, "state_100mhz");
701 break;
702 case UHS_SDR104:
703 case MMC_HS_200:
704 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000705 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800706 ret = pinctrl_select_state(dev, "state_200mhz");
707 break;
708 default:
709 ret = pinctrl_select_state(dev, "default");
710 break;
711 }
712
713 if (ret)
714 printf("%s %d error\n", __func__, priv->mode);
715
716 return ret;
717}
718
719static void esdhc_reset_tuning(struct mmc *mmc)
720{
721 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
722 struct fsl_esdhc *regs = priv->esdhc_regs;
723
724 if (priv->flags & ESDHC_FLAG_USDHC) {
725 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
726 esdhc_clrbits32(&regs->autoc12err,
727 MIX_CTRL_SMPCLK_SEL |
728 MIX_CTRL_EXE_TUNE);
729 }
730 }
731}
732
733static void esdhc_set_strobe_dll(struct mmc *mmc)
734{
735 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
736 struct fsl_esdhc *regs = priv->esdhc_regs;
737 u32 val;
738
739 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
740 writel(ESDHC_STROBE_DLL_CTRL_RESET, &regs->strobe_dllctrl);
741
742 /*
743 * enable strobe dll ctrl and adjust the delay target
744 * for the uSDHC loopback read clock
745 */
746 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
747 (priv->strobe_dll_delay_target <<
748 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
749 writel(val, &regs->strobe_dllctrl);
750 /* wait 1us to make sure strobe dll status register stable */
751 mdelay(1);
752 val = readl(&regs->strobe_dllstat);
753 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
754 pr_warn("HS400 strobe DLL status REF not lock!\n");
755 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
756 pr_warn("HS400 strobe DLL status SLV not lock!\n");
757 }
758}
759
760static int esdhc_set_timing(struct mmc *mmc)
761{
762 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
763 struct fsl_esdhc *regs = priv->esdhc_regs;
764 u32 mixctrl;
765
766 mixctrl = readl(&regs->mixctrl);
767 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
768
769 switch (mmc->selected_mode) {
770 case MMC_LEGACY:
771 case SD_LEGACY:
772 esdhc_reset_tuning(mmc);
773 writel(mixctrl, &regs->mixctrl);
774 break;
775 case MMC_HS_400:
Peng Fan69b9d3a2019-07-10 09:35:26 +0000776 case MMC_HS_400_ES:
Yangbo Lu982f4252019-06-21 11:42:27 +0800777 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;
Peng Fan2cdf52b2019-11-04 17:14:15 +0800960 u32 clock;
Yangbo Lu982f4252019-06-21 11:42:27 +0800961
962#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
963 /* Select to use peripheral clock */
964 esdhc_clock_control(priv, false);
965 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
966 esdhc_clock_control(priv, true);
967#endif
968 /* Set the clock speed */
Peng Fan2cdf52b2019-11-04 17:14:15 +0800969 clock = mmc->clock;
970 if (clock < mmc->cfg->f_min)
971 clock = mmc->cfg->f_min;
972
973 if (priv->clock != clock)
974 set_sysctl(priv, mmc, clock);
Yangbo Lu982f4252019-06-21 11:42:27 +0800975
976#ifdef MMC_SUPPORTS_TUNING
977 if (mmc->clk_disable) {
978#ifdef CONFIG_FSL_USDHC
979 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
980#else
981 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
982#endif
983 } else {
984#ifdef CONFIG_FSL_USDHC
985 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
986 VENDORSPEC_CKEN);
987#else
988 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
989#endif
990 }
991
992 if (priv->mode != mmc->selected_mode) {
993 ret = esdhc_set_timing(mmc);
994 if (ret) {
995 printf("esdhc_set_timing error %d\n", ret);
996 return ret;
997 }
998 }
999
1000 if (priv->signal_voltage != mmc->signal_voltage) {
1001 ret = esdhc_set_voltage(mmc);
1002 if (ret) {
1003 printf("esdhc_set_voltage error %d\n", ret);
1004 return ret;
1005 }
1006 }
1007#endif
1008
1009 /* Set the bus width */
1010 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
1011
1012 if (mmc->bus_width == 4)
1013 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
1014 else if (mmc->bus_width == 8)
1015 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1016
1017 return 0;
1018}
1019
1020static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1021{
1022 struct fsl_esdhc *regs = priv->esdhc_regs;
1023 ulong start;
1024
1025 /* Reset the entire host controller */
1026 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1027
1028 /* Wait until the controller is available */
1029 start = get_timer(0);
1030 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1031 if (get_timer(start) > 1000)
1032 return -ETIMEDOUT;
1033 }
1034
1035#if defined(CONFIG_FSL_USDHC)
1036 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1037 esdhc_write32(&regs->mmcboot, 0x0);
1038 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1039 esdhc_write32(&regs->mixctrl, 0x0);
1040 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1041
1042 /* Put VEND_SPEC to default value */
1043 if (priv->vs18_enable)
1044 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1045 ESDHC_VENDORSPEC_VSELECT));
1046 else
1047 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
1048
1049 /* Disable DLL_CTRL delay line */
1050 esdhc_write32(&regs->dllctrl, 0x0);
1051#endif
1052
1053#ifndef ARCH_MXC
1054 /* Enable cache snooping */
1055 esdhc_write32(&regs->scr, 0x00000040);
1056#endif
1057
1058#ifndef CONFIG_FSL_USDHC
1059 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1060#else
1061 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1062#endif
1063
1064 /* Set the initial clock speed */
1065 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1066
1067 /* Disable the BRR and BWR bits in IRQSTAT */
1068 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1069
1070#ifdef CONFIG_MCF5441x
1071 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1072#else
1073 /* Put the PROCTL reg back to the default */
1074 esdhc_write32(&regs->proctl, PROCTL_INIT);
1075#endif
1076
1077 /* Set timout to the maximum value */
1078 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1079
1080 return 0;
1081}
1082
1083static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1084{
1085 struct fsl_esdhc *regs = priv->esdhc_regs;
1086 int timeout = 1000;
1087
1088#ifdef CONFIG_ESDHC_DETECT_QUIRK
1089 if (CONFIG_ESDHC_DETECT_QUIRK)
1090 return 1;
1091#endif
1092
1093#if CONFIG_IS_ENABLED(DM_MMC)
1094 if (priv->non_removable)
1095 return 1;
Simon Glassfa4689a2019-12-06 21:41:35 -07001096#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001097 if (dm_gpio_is_valid(&priv->cd_gpio))
1098 return dm_gpio_get_value(&priv->cd_gpio);
1099#endif
1100#endif
1101
1102 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1103 udelay(1000);
1104
1105 return timeout > 0;
1106}
1107
1108static int esdhc_reset(struct fsl_esdhc *regs)
1109{
1110 ulong start;
1111
1112 /* reset the controller */
1113 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
1114
1115 /* hardware clears the bit when it is done */
1116 start = get_timer(0);
1117 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1118 if (get_timer(start) > 100) {
1119 printf("MMC/SD: Reset never completed.\n");
1120 return -ETIMEDOUT;
1121 }
1122 }
1123
1124 return 0;
1125}
1126
1127#if !CONFIG_IS_ENABLED(DM_MMC)
1128static int esdhc_getcd(struct mmc *mmc)
1129{
1130 struct fsl_esdhc_priv *priv = mmc->priv;
1131
1132 return esdhc_getcd_common(priv);
1133}
1134
1135static int esdhc_init(struct mmc *mmc)
1136{
1137 struct fsl_esdhc_priv *priv = mmc->priv;
1138
1139 return esdhc_init_common(priv, mmc);
1140}
1141
1142static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1143 struct mmc_data *data)
1144{
1145 struct fsl_esdhc_priv *priv = mmc->priv;
1146
1147 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1148}
1149
1150static int esdhc_set_ios(struct mmc *mmc)
1151{
1152 struct fsl_esdhc_priv *priv = mmc->priv;
1153
1154 return esdhc_set_ios_common(priv, mmc);
1155}
1156
1157static const struct mmc_ops esdhc_ops = {
1158 .getcd = esdhc_getcd,
1159 .init = esdhc_init,
1160 .send_cmd = esdhc_send_cmd,
1161 .set_ios = esdhc_set_ios,
1162};
1163#endif
1164
1165static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1166 struct fsl_esdhc_plat *plat)
1167{
1168 struct mmc_config *cfg;
1169 struct fsl_esdhc *regs;
1170 u32 caps, voltage_caps;
1171 int ret;
1172
1173 if (!priv)
1174 return -EINVAL;
1175
1176 regs = priv->esdhc_regs;
1177
1178 /* First reset the eSDHC controller */
1179 ret = esdhc_reset(regs);
1180 if (ret)
1181 return ret;
1182
1183#ifdef CONFIG_MCF5441x
1184 /* ColdFire, using SDHC_DATA[3] for card detection */
1185 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1186#endif
1187
1188#ifndef CONFIG_FSL_USDHC
1189 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1190 | SYSCTL_IPGEN | SYSCTL_CKEN);
1191 /* Clearing tuning bits in case ROM has set it already */
1192 esdhc_write32(&regs->mixctrl, 0);
1193 esdhc_write32(&regs->autoc12err, 0);
1194 esdhc_write32(&regs->clktunectrlstatus, 0);
1195#else
1196 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1197 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1198#endif
1199
1200 if (priv->vs18_enable)
1201 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1202
1203 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
1204 cfg = &plat->cfg;
1205#ifndef CONFIG_DM_MMC
1206 memset(cfg, '\0', sizeof(*cfg));
1207#endif
1208
1209 voltage_caps = 0;
1210 caps = esdhc_read32(&regs->hostcapblt);
1211
1212#ifdef CONFIG_MCF5441x
1213 /*
1214 * MCF5441x RM declares in more points that sdhc clock speed must
1215 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1216 * from host capabilities.
1217 */
1218 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1219#endif
1220
1221#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1222 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1223 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1224#endif
1225
1226/* T4240 host controller capabilities register should have VS33 bit */
1227#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1228 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1229#endif
1230
1231 if (caps & ESDHC_HOSTCAPBLT_VS18)
1232 voltage_caps |= MMC_VDD_165_195;
1233 if (caps & ESDHC_HOSTCAPBLT_VS30)
1234 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1235 if (caps & ESDHC_HOSTCAPBLT_VS33)
1236 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1237
1238 cfg->name = "FSL_SDHC";
1239#if !CONFIG_IS_ENABLED(DM_MMC)
1240 cfg->ops = &esdhc_ops;
1241#endif
1242#ifdef CONFIG_SYS_SD_VOLTAGE
1243 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1244#else
1245 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1246#endif
1247 if ((cfg->voltages & voltage_caps) == 0) {
1248 printf("voltage not supported by controller\n");
1249 return -1;
1250 }
1251
1252 if (priv->bus_width == 8)
1253 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1254 else if (priv->bus_width == 4)
1255 cfg->host_caps = MMC_MODE_4BIT;
1256
1257 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1258#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1259 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1260#endif
1261
1262 if (priv->bus_width > 0) {
1263 if (priv->bus_width < 8)
1264 cfg->host_caps &= ~MMC_MODE_8BIT;
1265 if (priv->bus_width < 4)
1266 cfg->host_caps &= ~MMC_MODE_4BIT;
1267 }
1268
1269 if (caps & ESDHC_HOSTCAPBLT_HSS)
1270 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1271
1272#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1273 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1274 cfg->host_caps &= ~MMC_MODE_8BIT;
1275#endif
1276
1277 cfg->host_caps |= priv->caps;
1278
1279 cfg->f_min = 400000;
1280 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1281
1282 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1283
1284 writel(0, &regs->dllctrl);
1285 if (priv->flags & ESDHC_FLAG_USDHC) {
1286 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1287 u32 val = readl(&regs->tuning_ctrl);
1288
1289 val |= ESDHC_STD_TUNING_EN;
1290 val &= ~ESDHC_TUNING_START_TAP_MASK;
1291 val |= priv->tuning_start_tap;
1292 val &= ~ESDHC_TUNING_STEP_MASK;
1293 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1294 writel(val, &regs->tuning_ctrl);
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1301#if !CONFIG_IS_ENABLED(DM_MMC)
1302static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1303 struct fsl_esdhc_priv *priv)
1304{
1305 if (!cfg || !priv)
1306 return -EINVAL;
1307
1308 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1309 priv->bus_width = cfg->max_bus_width;
1310 priv->sdhc_clk = cfg->sdhc_clk;
1311 priv->wp_enable = cfg->wp_enable;
1312 priv->vs18_enable = cfg->vs18_enable;
1313
1314 return 0;
1315};
1316
1317int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
1318{
1319 struct fsl_esdhc_plat *plat;
1320 struct fsl_esdhc_priv *priv;
1321 struct mmc *mmc;
1322 int ret;
1323
1324 if (!cfg)
1325 return -EINVAL;
1326
1327 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1328 if (!priv)
1329 return -ENOMEM;
1330 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1331 if (!plat) {
1332 free(priv);
1333 return -ENOMEM;
1334 }
1335
1336 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1337 if (ret) {
1338 debug("%s xlate failure\n", __func__);
1339 free(plat);
1340 free(priv);
1341 return ret;
1342 }
1343
1344 ret = fsl_esdhc_init(priv, plat);
1345 if (ret) {
1346 debug("%s init failure\n", __func__);
1347 free(plat);
1348 free(priv);
1349 return ret;
1350 }
1351
1352 mmc = mmc_create(&plat->cfg, priv);
1353 if (!mmc)
1354 return -EIO;
1355
1356 priv->mmc = mmc;
1357
1358 return 0;
1359}
1360
1361int fsl_esdhc_mmc_init(bd_t *bis)
1362{
1363 struct fsl_esdhc_cfg *cfg;
1364
1365 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1366 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1367 cfg->sdhc_clk = gd->arch.sdhc_clk;
1368 return fsl_esdhc_initialize(bis, cfg);
1369}
1370#endif
1371
Yangbo Lu982f4252019-06-21 11:42:27 +08001372#ifdef CONFIG_OF_LIBFDT
1373__weak int esdhc_status_fixup(void *blob, const char *compat)
1374{
1375#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1376 if (!hwconfig("esdhc")) {
1377 do_fixup_by_compat(blob, compat, "status", "disabled",
1378 sizeof("disabled"), 1);
1379 return 1;
1380 }
1381#endif
1382 return 0;
1383}
1384
1385void fdt_fixup_esdhc(void *blob, bd_t *bd)
1386{
1387 const char *compat = "fsl,esdhc";
1388
1389 if (esdhc_status_fixup(blob, compat))
1390 return;
1391
1392#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
1393 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
1394 gd->arch.sdhc_clk, 1);
1395#else
1396 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1397 gd->arch.sdhc_clk, 1);
1398#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001399}
1400#endif
1401
1402#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu982f4252019-06-21 11:42:27 +08001403#include <asm/arch/clock.h>
Yangbo Lu982f4252019-06-21 11:42:27 +08001404__weak void init_clk_usdhc(u32 index)
1405{
1406}
1407
1408static int fsl_esdhc_probe(struct udevice *dev)
1409{
1410 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1411 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1412 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1413 const void *fdt = gd->fdt_blob;
1414 int node = dev_of_offset(dev);
1415 struct esdhc_soc_data *data =
1416 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1417#if CONFIG_IS_ENABLED(DM_REGULATOR)
1418 struct udevice *vqmmc_dev;
1419#endif
1420 fdt_addr_t addr;
1421 unsigned int val;
1422 struct mmc *mmc;
1423#if !CONFIG_IS_ENABLED(BLK)
1424 struct blk_desc *bdesc;
1425#endif
1426 int ret;
1427
1428 addr = dev_read_addr(dev);
1429 if (addr == FDT_ADDR_T_NONE)
1430 return -EINVAL;
Yangbo Lu982f4252019-06-21 11:42:27 +08001431 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yangbo Lu982f4252019-06-21 11:42:27 +08001432 priv->dev = dev;
1433 priv->mode = -1;
Peng Fan3766a482019-07-10 09:35:24 +00001434 if (data)
Yangbo Lu982f4252019-06-21 11:42:27 +08001435 priv->flags = data->flags;
Yangbo Lu982f4252019-06-21 11:42:27 +08001436
1437 val = dev_read_u32_default(dev, "bus-width", -1);
1438 if (val == 8)
1439 priv->bus_width = 8;
1440 else if (val == 4)
1441 priv->bus_width = 4;
1442 else
1443 priv->bus_width = 1;
1444
1445 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1446 priv->tuning_step = val;
1447 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1448 ESDHC_TUNING_START_TAP_DEFAULT);
1449 priv->tuning_start_tap = val;
1450 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1451 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1452 priv->strobe_dll_delay_target = val;
1453
1454 if (dev_read_bool(dev, "non-removable")) {
1455 priv->non_removable = 1;
1456 } else {
1457 priv->non_removable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001458#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001459 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1460 GPIOD_IS_IN);
1461#endif
1462 }
1463
1464 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1465 priv->wp_enable = 1;
1466 } else {
1467 priv->wp_enable = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -07001468#if CONFIG_IS_ENABLED(DM_GPIO)
Yangbo Lu982f4252019-06-21 11:42:27 +08001469 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1470 GPIOD_IS_IN);
1471#endif
1472 }
1473
1474 priv->vs18_enable = 0;
1475
1476#if CONFIG_IS_ENABLED(DM_REGULATOR)
1477 /*
1478 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1479 * otherwise, emmc will work abnormally.
1480 */
1481 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1482 if (ret) {
1483 dev_dbg(dev, "no vqmmc-supply\n");
1484 } else {
1485 ret = regulator_set_enable(vqmmc_dev, true);
1486 if (ret) {
1487 dev_err(dev, "fail to enable vqmmc-supply\n");
1488 return ret;
1489 }
1490
1491 if (regulator_get_value(vqmmc_dev) == 1800000)
1492 priv->vs18_enable = 1;
1493 }
1494#endif
1495
Yangbo Lu982f4252019-06-21 11:42:27 +08001496 /*
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
Ye Li7aa20fd2019-07-11 03:29:02 +00001518 if (CONFIG_IS_ENABLED(CLK)) {
Yangbo Lu982f4252019-06-21 11:42:27 +08001519 /* 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
Peng Fan3766a482019-07-10 09:35:24 +00001546 ret = mmc_of_parse(dev, &plat->cfg);
1547 if (ret)
1548 return ret;
1549
Yangbo Lu982f4252019-06-21 11:42:27 +08001550 mmc = &plat->mmc;
1551 mmc->cfg = &plat->cfg;
1552 mmc->dev = dev;
1553#if !CONFIG_IS_ENABLED(BLK)
1554 mmc->priv = priv;
1555
1556 /* Setup dsr related values */
1557 mmc->dsr_imp = 0;
1558 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1559 /* Setup the universal parts of the block interface just once */
1560 bdesc = mmc_get_blk_desc(mmc);
1561 bdesc->if_type = IF_TYPE_MMC;
1562 bdesc->removable = 1;
1563 bdesc->devnum = mmc_get_next_devnum();
1564 bdesc->block_read = mmc_bread;
1565 bdesc->block_write = mmc_bwrite;
1566 bdesc->block_erase = mmc_berase;
1567
1568 /* setup initial part type */
1569 bdesc->part_type = mmc->cfg->part_type;
1570 mmc_list_add(mmc);
1571#endif
1572
1573 upriv->mmc = mmc;
1574
1575 return esdhc_init_common(priv, mmc);
1576}
1577
1578#if CONFIG_IS_ENABLED(DM_MMC)
1579static int fsl_esdhc_get_cd(struct udevice *dev)
1580{
1581 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1582
1583 return esdhc_getcd_common(priv);
1584}
1585
1586static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1587 struct mmc_data *data)
1588{
1589 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1590 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1591
1592 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1593}
1594
1595static int fsl_esdhc_set_ios(struct udevice *dev)
1596{
1597 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1598 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1599
1600 return esdhc_set_ios_common(priv, &plat->mmc);
1601}
1602
Peng Fan69b9d3a2019-07-10 09:35:26 +00001603#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1604static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1605{
1606 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1607 struct fsl_esdhc *regs = priv->esdhc_regs;
1608 u32 m;
1609
1610 m = readl(&regs->mixctrl);
1611 m |= MIX_CTRL_HS400_ES;
1612 writel(m, &regs->mixctrl);
1613
1614 return 0;
1615}
1616#endif
1617
Yangbo Lu982f4252019-06-21 11:42:27 +08001618static const struct dm_mmc_ops fsl_esdhc_ops = {
1619 .get_cd = fsl_esdhc_get_cd,
1620 .send_cmd = fsl_esdhc_send_cmd,
1621 .set_ios = fsl_esdhc_set_ios,
1622#ifdef MMC_SUPPORTS_TUNING
1623 .execute_tuning = fsl_esdhc_execute_tuning,
1624#endif
Peng Fan69b9d3a2019-07-10 09:35:26 +00001625#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1626 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1627#endif
Yangbo Lu982f4252019-06-21 11:42:27 +08001628};
1629#endif
1630
1631static struct esdhc_soc_data usdhc_imx7d_data = {
1632 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1633 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1634 | ESDHC_FLAG_HS400,
Yangbo Lu982f4252019-06-21 11:42:27 +08001635};
1636
Peng Fan457fe962019-07-10 09:35:28 +00001637static struct esdhc_soc_data usdhc_imx8qm_data = {
1638 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1639 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1640 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1641};
1642
Yangbo Lu982f4252019-06-21 11:42:27 +08001643static const struct udevice_id fsl_esdhc_ids[] = {
1644 { .compatible = "fsl,imx53-esdhc", },
1645 { .compatible = "fsl,imx6ul-usdhc", },
1646 { .compatible = "fsl,imx6sx-usdhc", },
1647 { .compatible = "fsl,imx6sl-usdhc", },
1648 { .compatible = "fsl,imx6q-usdhc", },
1649 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1650 { .compatible = "fsl,imx7ulp-usdhc", },
Peng Fan457fe962019-07-10 09:35:28 +00001651 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Peng Fand7689fa2019-11-04 17:31:17 +08001652 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1653 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1654 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
Yangbo Lu982f4252019-06-21 11:42:27 +08001655 { .compatible = "fsl,esdhc", },
1656 { /* sentinel */ }
1657};
1658
1659#if CONFIG_IS_ENABLED(BLK)
1660static int fsl_esdhc_bind(struct udevice *dev)
1661{
1662 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1663
1664 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1665}
1666#endif
1667
1668U_BOOT_DRIVER(fsl_esdhc) = {
1669 .name = "fsl-esdhc-mmc",
1670 .id = UCLASS_MMC,
1671 .of_match = fsl_esdhc_ids,
1672 .ops = &fsl_esdhc_ops,
1673#if CONFIG_IS_ENABLED(BLK)
1674 .bind = fsl_esdhc_bind,
1675#endif
1676 .probe = fsl_esdhc_probe,
1677 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
1678 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1679};
1680#endif