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