]> Git Repo - J-u-boot.git/blame - drivers/mmc/fsl_esdhc.c
mmc: fsl_esdhc: add workaround for erratum A-011334
[J-u-boot.git] / drivers / mmc / fsl_esdhc.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
50586ef2 2/*
d621da00 3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
9abf6484 4 * Copyright 2019-2020 NXP
50586ef2
AF
5 * Andy Fleming
6 *
7 * Based vaguely on the pxa mmc code:
8 * (C) Copyright 2003
9 * Kyle Harris, Nexus Technologies, Inc. [email protected]
50586ef2
AF
10 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
1eb69ae4 15#include <cpu_func.h>
915ffa52 16#include <errno.h>
b33433a6 17#include <hwconfig.h>
50586ef2
AF
18#include <mmc.h>
19#include <part.h>
20#include <malloc.h>
50586ef2 21#include <fsl_esdhc.h>
b33433a6 22#include <fdt_support.h>
90526e9f 23#include <asm/cache.h>
401d1c4f 24#include <asm/global_data.h>
50586ef2 25#include <asm/io.h>
96f0407b 26#include <dm.h>
336d4615 27#include <dm/device_compat.h>
cd93d625 28#include <linux/bitops.h>
c05ed00a 29#include <linux/delay.h>
b1ba1460 30#include <linux/dma-mapping.h>
361a422b 31#include <sdhci.h>
50586ef2 32
50586ef2
AF
33DECLARE_GLOBAL_DATA_PTR;
34
35struct fsl_esdhc {
511948b2
HZ
36 uint dsaddr; /* SDMA system address register */
37 uint blkattr; /* Block attributes register */
38 uint cmdarg; /* Command argument register */
39 uint xfertyp; /* Transfer type register */
40 uint cmdrsp0; /* Command response 0 register */
41 uint cmdrsp1; /* Command response 1 register */
42 uint cmdrsp2; /* Command response 2 register */
43 uint cmdrsp3; /* Command response 3 register */
44 uint datport; /* Buffer data port register */
45 uint prsstat; /* Present state register */
46 uint proctl; /* Protocol control register */
47 uint sysctl; /* System Control Register */
48 uint irqstat; /* Interrupt status register */
49 uint irqstaten; /* Interrupt status enable register */
50 uint irqsigen; /* Interrupt signal enable register */
51 uint autoc12err; /* Auto CMD error status register */
52 uint hostcapblt; /* Host controller capabilities register */
53 uint wml; /* Watermark level register */
4d8ff42e 54 char reserved1[8]; /* reserved */
511948b2
HZ
55 uint fevt; /* Force event register */
56 uint admaes; /* ADMA error status register */
361a422b
MW
57 uint adsaddrl; /* ADMA system address low register */
58 uint adsaddrh; /* ADMA system address high register */
59 char reserved2[156];
511948b2 60 uint hostver; /* Host controller version register */
4d8ff42e 61 char reserved3[4]; /* reserved */
59d3782c 62 uint dmaerraddr; /* DMA error address register */
4d8ff42e 63 char reserved4[4]; /* reserved */
59d3782c 64 uint dmaerrattr; /* DMA error attribute register */
4d8ff42e 65 char reserved5[4]; /* reserved */
511948b2 66 uint hostcapblt2; /* Host controller capabilities register 2 */
b1a4247b
YL
67 char reserved6[8]; /* reserved */
68 uint tbctl; /* Tuning block control register */
db8f9367
YL
69 char reserved7[32]; /* reserved */
70 uint sdclkctl; /* SD clock control register */
71 uint sdtimingctl; /* SD timing control register */
72 char reserved8[20]; /* reserved */
73 uint dllcfg0; /* DLL config 0 register */
8ee802f8
YL
74 char reserved9[12]; /* reserved */
75 uint dllstat0; /* DLL status 0 register */
76 char reserved10[664];/* reserved */
4d8ff42e 77 uint esdhcctl; /* eSDHC control register */
50586ef2
AF
78};
79
e88e1d9c
SG
80struct fsl_esdhc_plat {
81 struct mmc_config cfg;
82 struct mmc mmc;
83};
84
96f0407b
PF
85/**
86 * struct fsl_esdhc_priv
87 *
88 * @esdhc_regs: registers of the sdhc controller
89 * @sdhc_clk: Current clk of the sdhc controller
90 * @bus_width: bus width, 1bit, 4bit or 8bit
91 * @cfg: mmc config
92 * @mmc: mmc
93 * Following is used when Driver Model is enabled for MMC
94 * @dev: pointer for the device
96f0407b 95 * @cd_gpio: gpio for card detection
1483151e 96 * @wp_gpio: gpio for write protection
96f0407b
PF
97 */
98struct fsl_esdhc_priv {
99 struct fsl_esdhc *esdhc_regs;
100 unsigned int sdhc_clk;
f1bce084 101 bool is_sdhc_per_clk;
51313b49 102 unsigned int clock;
41dec2fe 103#if !CONFIG_IS_ENABLED(DM_MMC)
96f0407b 104 struct mmc *mmc;
653282b5 105#endif
96f0407b 106 struct udevice *dev;
361a422b 107 struct sdhci_adma_desc *adma_desc_table;
b1ba1460 108 dma_addr_t dma_addr;
96f0407b
PF
109};
110
50586ef2 111/* Return the XFERTYP flags for a given command and data packet */
eafa90a1 112static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
50586ef2
AF
113{
114 uint xfertyp = 0;
115
116 if (data) {
77c1458d 117 xfertyp |= XFERTYP_DPSEL;
52faec31
MW
118 if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
119 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
b1a4247b
YL
120 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
121 xfertyp |= XFERTYP_DMAEN;
50586ef2
AF
122 if (data->blocks > 1) {
123 xfertyp |= XFERTYP_MSBSEL;
124 xfertyp |= XFERTYP_BCEN;
52faec31
MW
125 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
126 xfertyp |= XFERTYP_AC12EN;
50586ef2
AF
127 }
128
129 if (data->flags & MMC_DATA_READ)
130 xfertyp |= XFERTYP_DTDSEL;
131 }
132
133 if (cmd->resp_type & MMC_RSP_CRC)
134 xfertyp |= XFERTYP_CCCEN;
135 if (cmd->resp_type & MMC_RSP_OPCODE)
136 xfertyp |= XFERTYP_CICEN;
137 if (cmd->resp_type & MMC_RSP_136)
138 xfertyp |= XFERTYP_RSPTYP_136;
139 else if (cmd->resp_type & MMC_RSP_BUSY)
140 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
141 else if (cmd->resp_type & MMC_RSP_PRESENT)
142 xfertyp |= XFERTYP_RSPTYP_48;
143
4571de33
JL
144 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
145 xfertyp |= XFERTYP_CMDTYP_ABORT;
25503443 146
50586ef2
AF
147 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
148}
149
77c1458d
DD
150/*
151 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
152 */
09b465fd
SG
153static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
154 struct mmc_data *data)
77c1458d 155{
96f0407b 156 struct fsl_esdhc *regs = priv->esdhc_regs;
77c1458d
DD
157 uint blocks;
158 char *buffer;
159 uint databuf;
160 uint size;
161 uint irqstat;
bcfb3653 162 ulong start;
77c1458d
DD
163
164 if (data->flags & MMC_DATA_READ) {
165 blocks = data->blocks;
166 buffer = data->dest;
167 while (blocks) {
bcfb3653 168 start = get_timer(0);
77c1458d
DD
169 size = data->blocksize;
170 irqstat = esdhc_read32(&regs->irqstat);
bcfb3653
BT
171 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
172 if (get_timer(start) > PIO_TIMEOUT) {
173 printf("\nData Read Failed in PIO Mode.");
174 return;
175 }
77c1458d
DD
176 }
177 while (size && (!(irqstat & IRQSTAT_TC))) {
178 udelay(100); /* Wait before last byte transfer complete */
179 irqstat = esdhc_read32(&regs->irqstat);
180 databuf = in_le32(&regs->datport);
181 *((uint *)buffer) = databuf;
182 buffer += 4;
183 size -= 4;
184 }
185 blocks--;
186 }
187 } else {
188 blocks = data->blocks;
7b43db92 189 buffer = (char *)data->src;
77c1458d 190 while (blocks) {
bcfb3653 191 start = get_timer(0);
77c1458d
DD
192 size = data->blocksize;
193 irqstat = esdhc_read32(&regs->irqstat);
bcfb3653
BT
194 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
195 if (get_timer(start) > PIO_TIMEOUT) {
196 printf("\nData Write Failed in PIO Mode.");
197 return;
198 }
77c1458d
DD
199 }
200 while (size && (!(irqstat & IRQSTAT_TC))) {
201 udelay(100); /* Wait before last byte transfer complete */
202 databuf = *((uint *)buffer);
203 buffer += 4;
204 size -= 4;
205 irqstat = esdhc_read32(&regs->irqstat);
206 out_le32(&regs->datport, databuf);
207 }
208 blocks--;
209 }
210 }
211}
77c1458d 212
7e48a028
MW
213static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
214 struct mmc_data *data)
50586ef2 215{
96f0407b 216 struct fsl_esdhc *regs = priv->esdhc_regs;
7e48a028 217 uint wml_value = data->blocksize / 4;
50586ef2
AF
218
219 if (data->flags & MMC_DATA_READ) {
32c8cfb2
PJ
220 if (wml_value > WML_RD_WML_MAX)
221 wml_value = WML_RD_WML_MAX_VAL;
50586ef2 222
ab467c51 223 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
50586ef2 224 } else {
32c8cfb2
PJ
225 if (wml_value > WML_WR_WML_MAX)
226 wml_value = WML_WR_WML_MAX_VAL;
0cc127c4 227
ab467c51 228 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
7e48a028 229 wml_value << 16);
50586ef2 230 }
7e48a028 231}
50586ef2 232
7e48a028
MW
233static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
234{
235 uint trans_bytes = data->blocksize * data->blocks;
236 struct fsl_esdhc *regs = priv->esdhc_regs;
361a422b 237 phys_addr_t adma_addr;
7e48a028
MW
238 void *buf;
239
240 if (data->flags & MMC_DATA_WRITE)
241 buf = (void *)data->src;
242 else
243 buf = data->dest;
244
245 priv->dma_addr = dma_map_single(buf, trans_bytes,
246 mmc_get_dma_dir(data));
361a422b
MW
247
248 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) &&
249 priv->adma_desc_table) {
250 debug("Using ADMA2\n");
251 /* prefer ADMA2 if it is available */
252 sdhci_prepare_adma_table(priv->adma_desc_table, data,
253 priv->dma_addr);
254
255 adma_addr = virt_to_phys(priv->adma_desc_table);
256 esdhc_write32(&regs->adsaddrl, lower_32_bits(adma_addr));
257 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT))
258 esdhc_write32(&regs->adsaddrh, upper_32_bits(adma_addr));
259 esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
260 PROCTL_DMAS_ADMA2);
261 } else {
262 debug("Using SDMA\n");
263 if (upper_32_bits(priv->dma_addr))
264 printf("Cannot use 64 bit addresses with SDMA\n");
265 esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
266 esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
267 PROCTL_DMAS_SDMA);
268 }
269
c67bee14 270 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
7e48a028
MW
271}
272
273static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
274 struct mmc_data *data)
275{
276 int timeout;
277 bool is_write = data->flags & MMC_DATA_WRITE;
278 struct fsl_esdhc *regs = priv->esdhc_regs;
279
280 if (is_write && !(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
281 printf("Can not write to locked SD card.\n");
282 return -EINVAL;
283 }
284
52faec31
MW
285 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
286 esdhc_setup_watermark_level(priv, data);
287 else
288 esdhc_setup_dma(priv, data);
50586ef2
AF
289
290 /* Calculate the timeout period for data transactions */
b71ea336
PJ
291 /*
292 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
293 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
294 * So, Number of SD Clock cycles for 0.25sec should be minimum
295 * (SD Clock/sec * 0.25 sec) SD Clock cycles
fb823981 296 * = (mmc->clock * 1/4) SD Clock cycles
b71ea336 297 * As 1) >= 2)
fb823981 298 * => (2^(timeout+13)) >= mmc->clock * 1/4
b71ea336 299 * Taking log2 both the sides
fb823981 300 * => timeout + 13 >= log2(mmc->clock/4)
b71ea336 301 * Rounding up to next power of 2
fb823981
AG
302 * => timeout + 13 = log2(mmc->clock/4) + 1
303 * => timeout + 13 = fls(mmc->clock/4)
e978a31b
YL
304 *
305 * However, the MMC spec "It is strongly recommended for hosts to
306 * implement more than 500ms timeout value even if the card
307 * indicates the 250ms maximum busy length." Even the previous
308 * value of 300ms is known to be insufficient for some cards.
309 * So, we use
310 * => timeout + 13 = fls(mmc->clock/2)
b71ea336 311 */
e978a31b 312 timeout = fls(mmc->clock/2);
50586ef2
AF
313 timeout -= 13;
314
315 if (timeout > 14)
316 timeout = 14;
317
318 if (timeout < 0)
319 timeout = 0;
320
52faec31
MW
321 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
322 (timeout == 4 || timeout == 8 || timeout == 12))
5103a03a 323 timeout++;
5103a03a 324
52faec31
MW
325 if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
326 timeout = 0xE;
327
c67bee14 328 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
50586ef2
AF
329
330 return 0;
331}
332
50586ef2
AF
333/*
334 * Sends a command out on the bus. Takes the mmc pointer,
335 * a command pointer, and an optional data pointer.
336 */
9586aa6e
SG
337static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
338 struct mmc_cmd *cmd, struct mmc_data *data)
50586ef2 339{
8a573022 340 int err = 0;
50586ef2
AF
341 uint xfertyp;
342 uint irqstat;
51313b49 343 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
96f0407b 344 struct fsl_esdhc *regs = priv->esdhc_regs;
29c2edb4 345 unsigned long start;
50586ef2 346
52faec31
MW
347 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
348 cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
d621da00 349 return 0;
d621da00 350
c67bee14 351 esdhc_write32(&regs->irqstat, -1);
50586ef2
AF
352
353 sync();
354
355 /* Wait for the bus to be idle */
c67bee14
SB
356 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
357 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
358 ;
50586ef2 359
c67bee14
SB
360 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
361 ;
50586ef2
AF
362
363 /* Wait at least 8 SD clock cycles before the next command */
364 /*
365 * Note: This is way more than 8 cycles, but 1ms seems to
366 * resolve timing issues with some cards
367 */
368 udelay(1000);
369
370 /* Set up for a data transfer if we have one */
371 if (data) {
09b465fd 372 err = esdhc_setup_data(priv, mmc, data);
50586ef2
AF
373 if(err)
374 return err;
375 }
376
377 /* Figure out the transfer arguments */
378 xfertyp = esdhc_xfertyp(cmd, data);
379
01b77353
AG
380 /* Mask all irqs */
381 esdhc_write32(&regs->irqsigen, 0);
382
50586ef2 383 /* Send the command */
c67bee14
SB
384 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
385 esdhc_write32(&regs->xfertyp, xfertyp);
7a5b8029 386
b1a4247b
YL
387 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
388 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
389 flags = IRQSTAT_BRR;
390
50586ef2 391 /* Wait for the command to complete */
29c2edb4
FE
392 start = get_timer(0);
393 while (!(esdhc_read32(&regs->irqstat) & flags)) {
394 if (get_timer(start) > 1000) {
395 err = -ETIMEDOUT;
396 goto out;
397 }
398 }
50586ef2 399
c67bee14 400 irqstat = esdhc_read32(&regs->irqstat);
50586ef2 401
8a573022 402 if (irqstat & CMD_ERR) {
915ffa52 403 err = -ECOMM;
8a573022 404 goto out;
7a5b8029
DB
405 }
406
8a573022 407 if (irqstat & IRQSTAT_CTOE) {
915ffa52 408 err = -ETIMEDOUT;
8a573022
AG
409 goto out;
410 }
50586ef2 411
7a5b8029
DB
412 /* Workaround for ESDHC errata ENGcm03648 */
413 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
253d5bdd 414 int timeout = 6000;
7a5b8029 415
253d5bdd 416 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
7a5b8029
DB
417 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
418 PRSSTAT_DAT0)) {
419 udelay(100);
420 timeout--;
421 }
422
423 if (timeout <= 0) {
424 printf("Timeout waiting for DAT0 to go high!\n");
915ffa52 425 err = -ETIMEDOUT;
8a573022 426 goto out;
7a5b8029
DB
427 }
428 }
429
50586ef2
AF
430 /* Copy the response to the response buffer */
431 if (cmd->resp_type & MMC_RSP_136) {
432 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
433
c67bee14
SB
434 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
435 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
436 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
437 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
998be3dd
RV
438 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
439 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
440 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
441 cmd->response[3] = (cmdrsp0 << 8);
50586ef2 442 } else
c67bee14 443 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
50586ef2
AF
444
445 /* Wait until all of the blocks are transferred */
446 if (data) {
52faec31
MW
447 if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
448 esdhc_pio_read_write(priv, data);
449 } else {
450 flags = DATA_COMPLETE;
451 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
452 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
453 flags = IRQSTAT_BRR;
454
455 do {
456 irqstat = esdhc_read32(&regs->irqstat);
50586ef2 457
52faec31
MW
458 if (irqstat & IRQSTAT_DTOE) {
459 err = -ETIMEDOUT;
460 goto out;
461 }
63fb5a7e 462
52faec31
MW
463 if (irqstat & DATA_ERR) {
464 err = -ECOMM;
465 goto out;
466 }
467 } while ((irqstat & flags) != flags);
468
469 /*
470 * Need invalidate the dcache here again to avoid any
471 * cache-fill during the DMA operations such as the
472 * speculative pre-fetching etc.
473 */
474 dma_unmap_single(priv->dma_addr,
475 data->blocks * data->blocksize,
476 mmc_get_dma_dir(data));
477 }
50586ef2
AF
478 }
479
8a573022
AG
480out:
481 /* Reset CMD and DATA portions on error */
482 if (err) {
483 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
484 SYSCTL_RSTC);
485 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
486 ;
487
488 if (data) {
489 esdhc_write32(&regs->sysctl,
490 esdhc_read32(&regs->sysctl) |
491 SYSCTL_RSTD);
492 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
493 ;
494 }
495 }
496
c67bee14 497 esdhc_write32(&regs->irqstat, -1);
50586ef2 498
8a573022 499 return err;
50586ef2
AF
500}
501
09b465fd 502static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
50586ef2 503{
b9b4f146 504 struct fsl_esdhc *regs = priv->esdhc_regs;
4f425280 505 int div = 1;
4f425280 506 int pre_div = 2;
6f883e50
YZ
507 unsigned int sdhc_clk = priv->sdhc_clk;
508 u32 time_out;
509 u32 value;
50586ef2
AF
510 uint clk;
511
93bfd616
PA
512 if (clock < mmc->cfg->f_min)
513 clock = mmc->cfg->f_min;
c67bee14 514
5d336d17 515 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
b6a04275 516 pre_div *= 2;
50586ef2 517
5d336d17 518 while (sdhc_clk / (div * pre_div) > clock && div < 16)
b6a04275 519 div++;
50586ef2 520
bd7b8505
MW
521 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
522 clock == 200000000 && mmc->selected_mode == MMC_HS_400) {
523 u32 div_ratio = pre_div * div;
524
525 if (div_ratio <= 4) {
526 pre_div = 4;
527 div = 1;
528 } else if (div_ratio <= 8) {
529 pre_div = 4;
530 div = 2;
531 } else if (div_ratio <= 12) {
532 pre_div = 4;
533 div = 3;
534 } else {
535 printf("unsupported clock division.\n");
536 }
537 }
538
30f6444d
YL
539 mmc->clock = sdhc_clk / pre_div / div;
540 priv->clock = mmc->clock;
541
4f425280 542 pre_div >>= 1;
50586ef2
AF
543 div -= 1;
544
545 clk = (pre_div << 8) | (div << 4);
546
cc4d1226 547 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
c67bee14
SB
548
549 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
50586ef2 550
6f883e50
YZ
551 time_out = 20;
552 value = PRSSTAT_SDSTB;
553 while (!(esdhc_read32(&regs->prsstat) & value)) {
554 if (time_out == 0) {
555 printf("fsl_esdhc: Internal clock never stabilised.\n");
556 break;
557 }
558 time_out--;
559 mdelay(1);
560 }
50586ef2 561
f0b5f23f 562 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
50586ef2
AF
563}
564
09b465fd 565static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
2d9ca2c7 566{
96f0407b 567 struct fsl_esdhc *regs = priv->esdhc_regs;
2d9ca2c7
YL
568 u32 value;
569 u32 time_out;
570
571 value = esdhc_read32(&regs->sysctl);
572
573 if (enable)
574 value |= SYSCTL_CKEN;
575 else
576 value &= ~SYSCTL_CKEN;
577
578 esdhc_write32(&regs->sysctl, value);
579
580 time_out = 20;
581 value = PRSSTAT_SDSTB;
582 while (!(esdhc_read32(&regs->prsstat) & value)) {
583 if (time_out == 0) {
584 printf("fsl_esdhc: Internal clock never stabilised.\n");
585 break;
586 }
587 time_out--;
588 mdelay(1);
589 }
590}
2d9ca2c7 591
db8f9367
YL
592static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv)
593{
594 struct fsl_esdhc *regs = priv->esdhc_regs;
595 u32 time_out;
596
597 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_FAF);
598
599 time_out = 20;
600 while (esdhc_read32(&regs->esdhcctl) & ESDHCCTL_FAF) {
601 if (time_out == 0) {
602 printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n");
603 break;
604 }
605 time_out--;
606 mdelay(1);
607 }
608}
609
610static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv,
611 bool en)
612{
613 struct fsl_esdhc *regs = priv->esdhc_regs;
614
615 esdhc_clock_control(priv, false);
616 esdhc_flush_async_fifo(priv);
617 if (en)
618 esdhc_setbits32(&regs->tbctl, TBCTL_TB_EN);
619 else
620 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
621 esdhc_clock_control(priv, true);
622}
623
624static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv)
625{
626 struct fsl_esdhc *regs = priv->esdhc_regs;
627
628 esdhc_clrbits32(&regs->sdtimingctl, FLW_CTL_BG);
629 esdhc_clrbits32(&regs->sdclkctl, CMD_CLK_CTL);
630
631 esdhc_clock_control(priv, false);
632 esdhc_clrbits32(&regs->tbctl, HS400_MODE);
633 esdhc_clock_control(priv, true);
634
635 esdhc_clrbits32(&regs->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE);
636 esdhc_clrbits32(&regs->tbctl, HS400_WNDW_ADJUST);
637
638 esdhc_tuning_block_enable(priv, false);
639}
640
8ee802f8 641static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode)
b1a4247b
YL
642{
643 struct fsl_esdhc *regs = priv->esdhc_regs;
8ee802f8
YL
644 ulong start;
645 u32 val;
b1a4247b 646
db8f9367
YL
647 /* Exit HS400 mode before setting any other mode */
648 if (esdhc_read32(&regs->tbctl) & HS400_MODE &&
649 mode != MMC_HS_400)
650 esdhc_exit_hs400(priv);
651
b1a4247b
YL
652 esdhc_clock_control(priv, false);
653
654 if (mode == MMC_HS_200)
655 esdhc_clrsetbits32(&regs->autoc12err, UHSM_MASK,
656 UHSM_SDR104_HS200);
db8f9367
YL
657 if (mode == MMC_HS_400) {
658 esdhc_setbits32(&regs->tbctl, HS400_MODE);
659 esdhc_setbits32(&regs->sdclkctl, CMD_CLK_CTL);
660 esdhc_clock_control(priv, true);
b1a4247b 661
78804de4
YL
662 if (priv->clock == 200000000)
663 esdhc_setbits32(&regs->dllcfg0, DLL_FREQ_SEL);
664
665 esdhc_setbits32(&regs->dllcfg0, DLL_ENABLE);
8ee802f8
YL
666
667 esdhc_setbits32(&regs->dllcfg0, DLL_RESET);
668 udelay(1);
669 esdhc_clrbits32(&regs->dllcfg0, DLL_RESET);
670
671 start = get_timer(0);
672 val = DLL_STS_SLV_LOCK;
673 while (!(esdhc_read32(&regs->dllstat0) & val)) {
674 if (get_timer(start) > 1000) {
675 printf("fsl_esdhc: delay chain lock timeout\n");
676 return -ETIMEDOUT;
677 }
678 }
679
db8f9367
YL
680 esdhc_setbits32(&regs->tbctl, HS400_WNDW_ADJUST);
681
682 esdhc_clock_control(priv, false);
683 esdhc_flush_async_fifo(priv);
684 }
b1a4247b 685 esdhc_clock_control(priv, true);
8ee802f8 686 return 0;
b1a4247b
YL
687}
688
9586aa6e 689static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
50586ef2 690{
96f0407b 691 struct fsl_esdhc *regs = priv->esdhc_regs;
8ee802f8 692 int ret;
50586ef2 693
f1bce084
YL
694 if (priv->is_sdhc_per_clk) {
695 /* Select to use peripheral clock */
696 esdhc_clock_control(priv, false);
697 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
698 esdhc_clock_control(priv, true);
699 }
700
db8f9367
YL
701 if (mmc->selected_mode == MMC_HS_400)
702 esdhc_tuning_block_enable(priv, true);
703
50586ef2 704 /* Set the clock speed */
51313b49
PF
705 if (priv->clock != mmc->clock)
706 set_sysctl(priv, mmc, mmc->clock);
707
b1a4247b 708 /* Set timing */
8ee802f8
YL
709 ret = esdhc_set_timing(priv, mmc->selected_mode);
710 if (ret)
711 return ret;
b1a4247b 712
50586ef2 713 /* Set the bus width */
c67bee14 714 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
50586ef2
AF
715
716 if (mmc->bus_width == 4)
c67bee14 717 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
50586ef2 718 else if (mmc->bus_width == 8)
c67bee14
SB
719 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
720
07b0b9c0 721 return 0;
50586ef2
AF
722}
723
ede28228
RV
724static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
725{
726#ifdef CONFIG_ARCH_MPC830X
727 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
728 sysconf83xx_t *sysconf = &immr->sysconf;
729
730 setbits_be32(&sysconf->sdhccr, 0x02000000);
731#else
732 esdhc_write32(&regs->esdhcctl, 0x00000040);
733#endif
734}
735
9586aa6e 736static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
50586ef2 737{
96f0407b 738 struct fsl_esdhc *regs = priv->esdhc_regs;
201e828b 739 ulong start;
50586ef2 740
c67bee14 741 /* Reset the entire host controller */
a61da72b 742 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
c67bee14
SB
743
744 /* Wait until the controller is available */
201e828b
SG
745 start = get_timer(0);
746 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
747 if (get_timer(start) > 1000)
748 return -ETIMEDOUT;
749 }
50586ef2 750
1b5f0ba7
YL
751 /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */
752 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
753
ede28228 754 esdhc_enable_cache_snooping(regs);
2c1764ef 755
a61da72b 756 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
50586ef2
AF
757
758 /* Set the initial clock speed */
263ddfc3 759 set_sysctl(priv, mmc, 400000);
50586ef2
AF
760
761 /* Disable the BRR and BWR bits in IRQSTAT */
c67bee14 762 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
50586ef2
AF
763
764 /* Put the PROCTL reg back to the default */
c67bee14 765 esdhc_write32(&regs->proctl, PROCTL_INIT);
50586ef2 766
c67bee14
SB
767 /* Set timout to the maximum value */
768 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
50586ef2 769
d48d2e21
TR
770 return 0;
771}
50586ef2 772
9586aa6e 773static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
d48d2e21 774{
96f0407b 775 struct fsl_esdhc *regs = priv->esdhc_regs;
d48d2e21 776
f7e27cc5
HZ
777#ifdef CONFIG_ESDHC_DETECT_QUIRK
778 if (CONFIG_ESDHC_DETECT_QUIRK)
779 return 1;
780#endif
9abf6484
YL
781 if (esdhc_read32(&regs->prsstat) & PRSSTAT_CINS)
782 return 1;
c67bee14 783
9abf6484 784 return 0;
50586ef2
AF
785}
786
5705973b
YL
787static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
788 struct mmc_config *cfg)
50586ef2 789{
5705973b 790 struct fsl_esdhc *regs = priv->esdhc_regs;
5b05fc03 791 u32 caps;
50586ef2 792
19060bd8 793 caps = esdhc_read32(&regs->hostcapblt);
52faec31
MW
794 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
795 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
796 if (IS_ENABLED(CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33))
797 caps |= HOSTCAPBLT_VS33;
5b05fc03
YL
798 if (caps & HOSTCAPBLT_VS18)
799 cfg->voltages |= MMC_VDD_165_195;
800 if (caps & HOSTCAPBLT_VS30)
801 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
802 if (caps & HOSTCAPBLT_VS33)
803 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
030955c2 804
e88e1d9c 805 cfg->name = "FSL_SDHC";
aad4659a 806
5b05fc03 807 if (caps & HOSTCAPBLT_HSS)
e88e1d9c 808 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
50586ef2 809
e88e1d9c 810 cfg->f_min = 400000;
51313b49 811 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
e88e1d9c 812 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
96f0407b
PF
813}
814
c67bee14 815#ifdef CONFIG_OF_LIBFDT
fce1e16c 816__weak int esdhc_status_fixup(void *blob, const char *compat)
b33433a6 817{
52faec31 818 if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) {
a6da8b81 819 do_fixup_by_compat(blob, compat, "status", "disabled",
fce1e16c
YL
820 sizeof("disabled"), 1);
821 return 1;
b33433a6 822 }
52faec31 823
fce1e16c
YL
824 return 0;
825}
826
c927d658 827
52faec31
MW
828#if CONFIG_IS_ENABLED(DM_MMC)
829static int fsl_esdhc_get_cd(struct udevice *dev);
c927d658
YL
830static void esdhc_disable_for_no_card(void *blob)
831{
832 struct udevice *dev;
833
834 for (uclass_first_device(UCLASS_MMC, &dev);
835 dev;
836 uclass_next_device(&dev)) {
837 char esdhc_path[50];
838
839 if (fsl_esdhc_get_cd(dev))
840 continue;
841
842 snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
843 (unsigned long)dev_read_addr(dev));
844 do_fixup_by_path(blob, esdhc_path, "status", "disabled",
845 sizeof("disabled"), 1);
846 }
847}
52faec31
MW
848#else
849static void esdhc_disable_for_no_card(void *blob)
850{
851}
c927d658
YL
852#endif
853
b75d8dc5 854void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
fce1e16c
YL
855{
856 const char *compat = "fsl,esdhc";
857
858 if (esdhc_status_fixup(blob, compat))
859 return;
52faec31
MW
860
861 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND))
862 esdhc_disable_for_no_card(blob);
863
b33433a6 864 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
e9adeca3 865 gd->arch.sdhc_clk, 1);
b33433a6 866}
c67bee14 867#endif
96f0407b 868
61870475
YL
869#if !CONFIG_IS_ENABLED(DM_MMC)
870static int esdhc_getcd(struct mmc *mmc)
871{
872 struct fsl_esdhc_priv *priv = mmc->priv;
873
874 return esdhc_getcd_common(priv);
875}
876
877static int esdhc_init(struct mmc *mmc)
878{
879 struct fsl_esdhc_priv *priv = mmc->priv;
880
881 return esdhc_init_common(priv, mmc);
882}
883
884static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
885 struct mmc_data *data)
886{
887 struct fsl_esdhc_priv *priv = mmc->priv;
888
889 return esdhc_send_cmd_common(priv, mmc, cmd, data);
890}
891
892static int esdhc_set_ios(struct mmc *mmc)
893{
894 struct fsl_esdhc_priv *priv = mmc->priv;
895
896 return esdhc_set_ios_common(priv, mmc);
897}
898
899static const struct mmc_ops esdhc_ops = {
900 .getcd = esdhc_getcd,
901 .init = esdhc_init,
902 .send_cmd = esdhc_send_cmd,
903 .set_ios = esdhc_set_ios,
904};
905
b75d8dc5 906int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
61870475
YL
907{
908 struct fsl_esdhc_plat *plat;
909 struct fsl_esdhc_priv *priv;
910 struct mmc_config *mmc_cfg;
911 struct mmc *mmc;
912
913 if (!cfg)
914 return -EINVAL;
915
916 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
917 if (!priv)
918 return -ENOMEM;
919 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
920 if (!plat) {
921 free(priv);
922 return -ENOMEM;
923 }
924
925 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
926 priv->sdhc_clk = cfg->sdhc_clk;
f1bce084
YL
927 if (gd->arch.sdhc_per_clk)
928 priv->is_sdhc_per_clk = true;
61870475
YL
929
930 mmc_cfg = &plat->cfg;
931
932 if (cfg->max_bus_width == 8) {
933 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
934 MMC_MODE_8BIT;
935 } else if (cfg->max_bus_width == 4) {
936 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
937 } else if (cfg->max_bus_width == 1) {
938 mmc_cfg->host_caps |= MMC_MODE_1BIT;
939 } else {
940 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
941 MMC_MODE_8BIT;
942 printf("No max bus width provided. Assume 8-bit supported.\n");
943 }
944
52faec31 945 if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
61870475 946 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
52faec31 947
61870475
YL
948 mmc_cfg->ops = &esdhc_ops;
949
950 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
951
952 mmc = mmc_create(mmc_cfg, priv);
953 if (!mmc)
954 return -EIO;
955
956 priv->mmc = mmc;
957 return 0;
958}
959
b75d8dc5 960int fsl_esdhc_mmc_init(struct bd_info *bis)
61870475
YL
961{
962 struct fsl_esdhc_cfg *cfg;
963
964 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
965 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
f1bce084
YL
966 /* Prefer peripheral clock which provides higher frequency. */
967 if (gd->arch.sdhc_per_clk)
968 cfg->sdhc_clk = gd->arch.sdhc_per_clk;
969 else
970 cfg->sdhc_clk = gd->arch.sdhc_clk;
61870475
YL
971 return fsl_esdhc_initialize(bis, cfg);
972}
973#else /* DM_MMC */
96f0407b
PF
974static int fsl_esdhc_probe(struct udevice *dev)
975{
976 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
c69cda25 977 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
96f0407b 978 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
361a422b 979 u32 caps, hostver;
96f0407b 980 fdt_addr_t addr;
653282b5 981 struct mmc *mmc;
c927d658 982 int ret;
96f0407b 983
4aac33f5 984 addr = dev_read_addr(dev);
96f0407b
PF
985 if (addr == FDT_ADDR_T_NONE)
986 return -EINVAL;
b69e1d0b
YZ
987#ifdef CONFIG_PPC
988 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
989#else
96f0407b 990 priv->esdhc_regs = (struct fsl_esdhc *)addr;
b69e1d0b 991#endif
96f0407b
PF
992 priv->dev = dev;
993
361a422b
MW
994 if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) {
995 /*
996 * Only newer eSDHC controllers can do ADMA2 if the ADMA flag
997 * is set in the host capabilities register.
998 */
999 caps = esdhc_read32(&priv->esdhc_regs->hostcapblt);
1000 hostver = esdhc_read32(&priv->esdhc_regs->hostver);
1001 if (caps & HOSTCAPBLT_DMAS &&
1002 HOSTVER_VENDOR(hostver) > VENDOR_V_22) {
1003 priv->adma_desc_table = sdhci_adma_init();
1004 if (!priv->adma_desc_table)
1005 debug("Could not allocate ADMA tables, falling back to SDMA\n");
1006 }
1007 }
1008
f1bce084
YL
1009 if (gd->arch.sdhc_per_clk) {
1010 priv->sdhc_clk = gd->arch.sdhc_per_clk;
1011 priv->is_sdhc_per_clk = true;
1012 } else {
1013 priv->sdhc_clk = gd->arch.sdhc_clk;
1014 }
1015
5e81cbff
YL
1016 if (priv->sdhc_clk <= 0) {
1017 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1018 return -EINVAL;
96f0407b
PF
1019 }
1020
5705973b 1021 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
96f0407b 1022
6f883e50
YZ
1023 mmc_of_parse(dev, &plat->cfg);
1024
653282b5
SG
1025 mmc = &plat->mmc;
1026 mmc->cfg = &plat->cfg;
1027 mmc->dev = dev;
66fa035b 1028
653282b5 1029 upriv->mmc = mmc;
96f0407b 1030
c927d658
YL
1031 ret = esdhc_init_common(priv, mmc);
1032 if (ret)
1033 return ret;
1034
52faec31
MW
1035 if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) &&
1036 !fsl_esdhc_get_cd(dev))
c927d658 1037 esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
52faec31 1038
c927d658 1039 return 0;
96f0407b
PF
1040}
1041
653282b5
SG
1042static int fsl_esdhc_get_cd(struct udevice *dev)
1043{
c69cda25 1044 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
653282b5
SG
1045 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1046
08197cb8
YL
1047 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
1048 return 1;
1049
653282b5
SG
1050 return esdhc_getcd_common(priv);
1051}
1052
1053static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1054 struct mmc_data *data)
1055{
c69cda25 1056 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
653282b5
SG
1057 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1058
1059 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1060}
1061
1062static int fsl_esdhc_set_ios(struct udevice *dev)
1063{
c69cda25 1064 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
653282b5
SG
1065 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1066
1067 return esdhc_set_ios_common(priv, &plat->mmc);
1068}
1069
1fdefd1d
YL
1070static int fsl_esdhc_reinit(struct udevice *dev)
1071{
c69cda25 1072 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
1fdefd1d
YL
1073 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1074
1075 return esdhc_init_common(priv, &plat->mmc);
1076}
1077
b1a4247b 1078#ifdef MMC_SUPPORTS_TUNING
b1a4247b
YL
1079static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
1080{
c69cda25 1081 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
b1a4247b
YL
1082 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1083 struct fsl_esdhc *regs = priv->esdhc_regs;
bd7b8505 1084 struct mmc *mmc = &plat->mmc;
b1a4247b
YL
1085 u32 val, irqstaten;
1086 int i;
1087
bd7b8505
MW
1088 if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
1089 plat->mmc.hs400_tuning)
1090 set_sysctl(priv, mmc, mmc->clock);
1091
b1a4247b
YL
1092 esdhc_tuning_block_enable(priv, true);
1093 esdhc_setbits32(&regs->autoc12err, EXECUTE_TUNING);
1094
1095 irqstaten = esdhc_read32(&regs->irqstaten);
1096 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
1097
1098 for (i = 0; i < MAX_TUNING_LOOP; i++) {
bd7b8505 1099 mmc_send_tuning(mmc, opcode, NULL);
b1a4247b
YL
1100 mdelay(1);
1101
1102 val = esdhc_read32(&regs->autoc12err);
1103 if (!(val & EXECUTE_TUNING)) {
1104 if (val & SMPCLKSEL)
1105 break;
1106 }
1107 }
1108
1109 esdhc_write32(&regs->irqstaten, irqstaten);
1110
db8f9367
YL
1111 if (i != MAX_TUNING_LOOP) {
1112 if (plat->mmc.hs400_tuning)
1113 esdhc_setbits32(&regs->sdtimingctl, FLW_CTL_BG);
b1a4247b 1114 return 0;
db8f9367 1115 }
b1a4247b
YL
1116
1117 printf("fsl_esdhc: tuning failed!\n");
1118 esdhc_clrbits32(&regs->autoc12err, SMPCLKSEL);
1119 esdhc_clrbits32(&regs->autoc12err, EXECUTE_TUNING);
1120 esdhc_tuning_block_enable(priv, false);
1121 return -ETIMEDOUT;
1122}
1123#endif
1124
db8f9367
YL
1125int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev)
1126{
1127 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1128
1129 esdhc_tuning_block_enable(priv, false);
1130 return 0;
1131}
1132
653282b5
SG
1133static const struct dm_mmc_ops fsl_esdhc_ops = {
1134 .get_cd = fsl_esdhc_get_cd,
1135 .send_cmd = fsl_esdhc_send_cmd,
1136 .set_ios = fsl_esdhc_set_ios,
6f883e50
YZ
1137#ifdef MMC_SUPPORTS_TUNING
1138 .execute_tuning = fsl_esdhc_execute_tuning,
1139#endif
1fdefd1d 1140 .reinit = fsl_esdhc_reinit,
db8f9367 1141 .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr,
653282b5 1142};
653282b5 1143
96f0407b 1144static const struct udevice_id fsl_esdhc_ids[] = {
a6473f8e 1145 { .compatible = "fsl,esdhc", },
96f0407b
PF
1146 { /* sentinel */ }
1147};
1148
653282b5
SG
1149static int fsl_esdhc_bind(struct udevice *dev)
1150{
c69cda25 1151 struct fsl_esdhc_plat *plat = dev_get_plat(dev);
653282b5
SG
1152
1153 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1154}
653282b5 1155
96f0407b
PF
1156U_BOOT_DRIVER(fsl_esdhc) = {
1157 .name = "fsl-esdhc-mmc",
1158 .id = UCLASS_MMC,
1159 .of_match = fsl_esdhc_ids,
653282b5 1160 .ops = &fsl_esdhc_ops,
653282b5 1161 .bind = fsl_esdhc_bind,
96f0407b 1162 .probe = fsl_esdhc_probe,
caa4daa2 1163 .plat_auto = sizeof(struct fsl_esdhc_plat),
41575d8e 1164 .priv_auto = sizeof(struct fsl_esdhc_priv),
96f0407b
PF
1165};
1166#endif
This page took 0.707869 seconds and 4 git commands to generate.