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