]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/mmc/fsl_esdhc.c
mmc: fsl_esdhc: make GPIO support optional
[people/ms/u-boot.git] / drivers / mmc / fsl_esdhc.c
CommitLineData
50586ef2 1/*
d621da00 2 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
50586ef2
AF
3 * Andy Fleming
4 *
5 * Based vaguely on the pxa mmc code:
6 * (C) Copyright 2003
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 *
1a459660 9 * SPDX-License-Identifier: GPL-2.0+
50586ef2
AF
10 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
915ffa52 15#include <errno.h>
b33433a6 16#include <hwconfig.h>
50586ef2
AF
17#include <mmc.h>
18#include <part.h>
19#include <malloc.h>
50586ef2 20#include <fsl_esdhc.h>
b33433a6 21#include <fdt_support.h>
50586ef2 22#include <asm/io.h>
96f0407b
PF
23#include <dm.h>
24#include <asm-generic/gpio.h>
50586ef2 25
50586ef2
AF
26DECLARE_GLOBAL_DATA_PTR;
27
a3d6e386
YL
28#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
29 IRQSTATEN_CINT | \
30 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
31 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
32 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
33 IRQSTATEN_DINT)
34
50586ef2 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 */
54 uint mixctrl; /* For USDHC */
55 char reserved1[4]; /* reserved */
56 uint fevt; /* Force event register */
57 uint admaes; /* ADMA error status register */
58 uint adsaddr; /* ADMA system address register */
f53225cc
PF
59 char reserved2[4];
60 uint dllctrl;
61 uint dllstat;
62 uint clktunectrlstatus;
63 char reserved3[84];
64 uint vendorspec;
65 uint mmcboot;
66 uint vendorspec2;
67 char reserved4[48];
511948b2 68 uint hostver; /* Host controller version register */
511948b2 69 char reserved5[4]; /* reserved */
f53225cc 70 uint dmaerraddr; /* DMA error address register */
f022d36e 71 char reserved6[4]; /* reserved */
f53225cc
PF
72 uint dmaerrattr; /* DMA error attribute register */
73 char reserved7[4]; /* reserved */
511948b2 74 uint hostcapblt2; /* Host controller capabilities register 2 */
f53225cc 75 char reserved8[8]; /* reserved */
511948b2 76 uint tcr; /* Tuning control register */
f53225cc 77 char reserved9[28]; /* reserved */
511948b2 78 uint sddirctl; /* SD direction control register */
f53225cc 79 char reserved10[712];/* reserved */
511948b2 80 uint scr; /* eSDHC control register */
50586ef2
AF
81};
82
96f0407b
PF
83/**
84 * struct fsl_esdhc_priv
85 *
86 * @esdhc_regs: registers of the sdhc controller
87 * @sdhc_clk: Current clk of the sdhc controller
88 * @bus_width: bus width, 1bit, 4bit or 8bit
89 * @cfg: mmc config
90 * @mmc: mmc
91 * Following is used when Driver Model is enabled for MMC
92 * @dev: pointer for the device
93 * @non_removable: 0: removable; 1: non-removable
1483151e 94 * @wp_enable: 1: enable checking wp; 0: no check
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;
101 unsigned int bus_width;
102 struct mmc_config cfg;
103 struct mmc *mmc;
104 struct udevice *dev;
105 int non_removable;
1483151e 106 int wp_enable;
fc8048a8 107#ifdef CONFIG_DM_GPIO
96f0407b 108 struct gpio_desc cd_gpio;
1483151e 109 struct gpio_desc wp_gpio;
fc8048a8 110#endif
96f0407b
PF
111};
112
50586ef2 113/* Return the XFERTYP flags for a given command and data packet */
eafa90a1 114static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
50586ef2
AF
115{
116 uint xfertyp = 0;
117
118 if (data) {
77c1458d
DD
119 xfertyp |= XFERTYP_DPSEL;
120#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
121 xfertyp |= XFERTYP_DMAEN;
122#endif
50586ef2
AF
123 if (data->blocks > 1) {
124 xfertyp |= XFERTYP_MSBSEL;
125 xfertyp |= XFERTYP_BCEN;
d621da00
JH
126#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
127 xfertyp |= XFERTYP_AC12EN;
128#endif
50586ef2
AF
129 }
130
131 if (data->flags & MMC_DATA_READ)
132 xfertyp |= XFERTYP_DTDSEL;
133 }
134
135 if (cmd->resp_type & MMC_RSP_CRC)
136 xfertyp |= XFERTYP_CCCEN;
137 if (cmd->resp_type & MMC_RSP_OPCODE)
138 xfertyp |= XFERTYP_CICEN;
139 if (cmd->resp_type & MMC_RSP_136)
140 xfertyp |= XFERTYP_RSPTYP_136;
141 else if (cmd->resp_type & MMC_RSP_BUSY)
142 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
143 else if (cmd->resp_type & MMC_RSP_PRESENT)
144 xfertyp |= XFERTYP_RSPTYP_48;
145
4571de33
JL
146 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
147 xfertyp |= XFERTYP_CMDTYP_ABORT;
25503443 148
50586ef2
AF
149 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
150}
151
77c1458d
DD
152#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
153/*
154 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
155 */
7b43db92 156static void
77c1458d
DD
157esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
158{
96f0407b
PF
159 struct fsl_esdhc_priv *priv = mmc->priv;
160 struct fsl_esdhc *regs = priv->esdhc_regs;
77c1458d
DD
161 uint blocks;
162 char *buffer;
163 uint databuf;
164 uint size;
165 uint irqstat;
166 uint timeout;
167
168 if (data->flags & MMC_DATA_READ) {
169 blocks = data->blocks;
170 buffer = data->dest;
171 while (blocks) {
172 timeout = PIO_TIMEOUT;
173 size = data->blocksize;
174 irqstat = esdhc_read32(&regs->irqstat);
175 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
176 && --timeout);
177 if (timeout <= 0) {
178 printf("\nData Read Failed in PIO Mode.");
7b43db92 179 return;
77c1458d
DD
180 }
181 while (size && (!(irqstat & IRQSTAT_TC))) {
182 udelay(100); /* Wait before last byte transfer complete */
183 irqstat = esdhc_read32(&regs->irqstat);
184 databuf = in_le32(&regs->datport);
185 *((uint *)buffer) = databuf;
186 buffer += 4;
187 size -= 4;
188 }
189 blocks--;
190 }
191 } else {
192 blocks = data->blocks;
7b43db92 193 buffer = (char *)data->src;
77c1458d
DD
194 while (blocks) {
195 timeout = PIO_TIMEOUT;
196 size = data->blocksize;
197 irqstat = esdhc_read32(&regs->irqstat);
198 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
199 && --timeout);
200 if (timeout <= 0) {
201 printf("\nData Write Failed in PIO Mode.");
7b43db92 202 return;
77c1458d
DD
203 }
204 while (size && (!(irqstat & IRQSTAT_TC))) {
205 udelay(100); /* Wait before last byte transfer complete */
206 databuf = *((uint *)buffer);
207 buffer += 4;
208 size -= 4;
209 irqstat = esdhc_read32(&regs->irqstat);
210 out_le32(&regs->datport, databuf);
211 }
212 blocks--;
213 }
214 }
215}
216#endif
217
50586ef2
AF
218static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
219{
50586ef2 220 int timeout;
96f0407b
PF
221 struct fsl_esdhc_priv *priv = mmc->priv;
222 struct fsl_esdhc *regs = priv->esdhc_regs;
9702ec00 223#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
8b06460e
YL
224 dma_addr_t addr;
225#endif
7b43db92 226 uint wml_value;
50586ef2
AF
227
228 wml_value = data->blocksize/4;
229
230 if (data->flags & MMC_DATA_READ) {
32c8cfb2
PJ
231 if (wml_value > WML_RD_WML_MAX)
232 wml_value = WML_RD_WML_MAX_VAL;
50586ef2 233
ab467c51 234 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
71689776 235#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
9702ec00 236#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
8b06460e
YL
237 addr = virt_to_phys((void *)(data->dest));
238 if (upper_32_bits(addr))
239 printf("Error found for upper 32 bits\n");
240 else
241 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
242#else
c67bee14 243 esdhc_write32(&regs->dsaddr, (u32)data->dest);
8b06460e 244#endif
71689776 245#endif
50586ef2 246 } else {
71689776 247#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
e576bd90
EN
248 flush_dcache_range((ulong)data->src,
249 (ulong)data->src+data->blocks
250 *data->blocksize);
71689776 251#endif
32c8cfb2
PJ
252 if (wml_value > WML_WR_WML_MAX)
253 wml_value = WML_WR_WML_MAX_VAL;
1483151e
PF
254 if (priv->wp_enable) {
255 if ((esdhc_read32(&regs->prsstat) &
256 PRSSTAT_WPSPL) == 0) {
257 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
915ffa52 258 return -ETIMEDOUT;
1483151e 259 }
50586ef2 260 }
ab467c51
RZ
261
262 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
263 wml_value << 16);
71689776 264#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
9702ec00 265#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
8b06460e
YL
266 addr = virt_to_phys((void *)(data->src));
267 if (upper_32_bits(addr))
268 printf("Error found for upper 32 bits\n");
269 else
270 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
271#else
c67bee14 272 esdhc_write32(&regs->dsaddr, (u32)data->src);
8b06460e 273#endif
71689776 274#endif
50586ef2
AF
275 }
276
c67bee14 277 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
50586ef2
AF
278
279 /* Calculate the timeout period for data transactions */
b71ea336
PJ
280 /*
281 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
282 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
283 * So, Number of SD Clock cycles for 0.25sec should be minimum
284 * (SD Clock/sec * 0.25 sec) SD Clock cycles
fb823981 285 * = (mmc->clock * 1/4) SD Clock cycles
b71ea336 286 * As 1) >= 2)
fb823981 287 * => (2^(timeout+13)) >= mmc->clock * 1/4
b71ea336 288 * Taking log2 both the sides
fb823981 289 * => timeout + 13 >= log2(mmc->clock/4)
b71ea336 290 * Rounding up to next power of 2
fb823981
AG
291 * => timeout + 13 = log2(mmc->clock/4) + 1
292 * => timeout + 13 = fls(mmc->clock/4)
e978a31b
YL
293 *
294 * However, the MMC spec "It is strongly recommended for hosts to
295 * implement more than 500ms timeout value even if the card
296 * indicates the 250ms maximum busy length." Even the previous
297 * value of 300ms is known to be insufficient for some cards.
298 * So, we use
299 * => timeout + 13 = fls(mmc->clock/2)
b71ea336 300 */
e978a31b 301 timeout = fls(mmc->clock/2);
50586ef2
AF
302 timeout -= 13;
303
304 if (timeout > 14)
305 timeout = 14;
306
307 if (timeout < 0)
308 timeout = 0;
309
5103a03a
KG
310#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
311 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
312 timeout++;
313#endif
314
1336e2d3
HZ
315#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
316 timeout = 0xE;
317#endif
c67bee14 318 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
50586ef2
AF
319
320 return 0;
321}
322
e576bd90
EN
323static void check_and_invalidate_dcache_range
324 (struct mmc_cmd *cmd,
325 struct mmc_data *data) {
8b06460e 326 unsigned start = 0;
cc634e28 327 unsigned end = 0;
e576bd90
EN
328 unsigned size = roundup(ARCH_DMA_MINALIGN,
329 data->blocks*data->blocksize);
9702ec00 330#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
8b06460e
YL
331 dma_addr_t addr;
332
333 addr = virt_to_phys((void *)(data->dest));
334 if (upper_32_bits(addr))
335 printf("Error found for upper 32 bits\n");
336 else
337 start = lower_32_bits(addr);
cc634e28
YL
338#else
339 start = (unsigned)data->dest;
8b06460e 340#endif
cc634e28 341 end = start + size;
e576bd90
EN
342 invalidate_dcache_range(start, end);
343}
10dc7771 344
50586ef2
AF
345/*
346 * Sends a command out on the bus. Takes the mmc pointer,
347 * a command pointer, and an optional data pointer.
348 */
349static int
350esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
351{
8a573022 352 int err = 0;
50586ef2
AF
353 uint xfertyp;
354 uint irqstat;
96f0407b
PF
355 struct fsl_esdhc_priv *priv = mmc->priv;
356 struct fsl_esdhc *regs = priv->esdhc_regs;
50586ef2 357
d621da00
JH
358#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
359 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
360 return 0;
361#endif
362
c67bee14 363 esdhc_write32(&regs->irqstat, -1);
50586ef2
AF
364
365 sync();
366
367 /* Wait for the bus to be idle */
c67bee14
SB
368 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
369 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
370 ;
50586ef2 371
c67bee14
SB
372 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
373 ;
50586ef2
AF
374
375 /* Wait at least 8 SD clock cycles before the next command */
376 /*
377 * Note: This is way more than 8 cycles, but 1ms seems to
378 * resolve timing issues with some cards
379 */
380 udelay(1000);
381
382 /* Set up for a data transfer if we have one */
383 if (data) {
50586ef2
AF
384 err = esdhc_setup_data(mmc, data);
385 if(err)
386 return err;
4683b220
PF
387
388 if (data->flags & MMC_DATA_READ)
389 check_and_invalidate_dcache_range(cmd, data);
50586ef2
AF
390 }
391
392 /* Figure out the transfer arguments */
393 xfertyp = esdhc_xfertyp(cmd, data);
394
01b77353
AG
395 /* Mask all irqs */
396 esdhc_write32(&regs->irqsigen, 0);
397
50586ef2 398 /* Send the command */
c67bee14 399 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
4692708d
JL
400#if defined(CONFIG_FSL_USDHC)
401 esdhc_write32(&regs->mixctrl,
0e1bf614
VR
402 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
403 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
4692708d
JL
404 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
405#else
c67bee14 406 esdhc_write32(&regs->xfertyp, xfertyp);
4692708d 407#endif
7a5b8029 408
50586ef2 409 /* Wait for the command to complete */
7a5b8029 410 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
c67bee14 411 ;
50586ef2 412
c67bee14 413 irqstat = esdhc_read32(&regs->irqstat);
50586ef2 414
8a573022 415 if (irqstat & CMD_ERR) {
915ffa52 416 err = -ECOMM;
8a573022 417 goto out;
7a5b8029
DB
418 }
419
8a573022 420 if (irqstat & IRQSTAT_CTOE) {
915ffa52 421 err = -ETIMEDOUT;
8a573022
AG
422 goto out;
423 }
50586ef2 424
f022d36e
OS
425 /* Switch voltage to 1.8V if CMD11 succeeded */
426 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
427 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
428
429 printf("Run CMD11 1.8V switch\n");
430 /* Sleep for 5 ms - max time for card to switch to 1.8V */
431 udelay(5000);
432 }
433
7a5b8029
DB
434 /* Workaround for ESDHC errata ENGcm03648 */
435 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
253d5bdd 436 int timeout = 6000;
7a5b8029 437
253d5bdd 438 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
7a5b8029
DB
439 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
440 PRSSTAT_DAT0)) {
441 udelay(100);
442 timeout--;
443 }
444
445 if (timeout <= 0) {
446 printf("Timeout waiting for DAT0 to go high!\n");
915ffa52 447 err = -ETIMEDOUT;
8a573022 448 goto out;
7a5b8029
DB
449 }
450 }
451
50586ef2
AF
452 /* Copy the response to the response buffer */
453 if (cmd->resp_type & MMC_RSP_136) {
454 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
455
c67bee14
SB
456 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
457 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
458 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
459 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
998be3dd
RV
460 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
461 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
462 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
463 cmd->response[3] = (cmdrsp0 << 8);
50586ef2 464 } else
c67bee14 465 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
50586ef2
AF
466
467 /* Wait until all of the blocks are transferred */
468 if (data) {
77c1458d
DD
469#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
470 esdhc_pio_read_write(mmc, data);
471#else
50586ef2 472 do {
c67bee14 473 irqstat = esdhc_read32(&regs->irqstat);
50586ef2 474
8a573022 475 if (irqstat & IRQSTAT_DTOE) {
915ffa52 476 err = -ETIMEDOUT;
8a573022
AG
477 goto out;
478 }
63fb5a7e 479
8a573022 480 if (irqstat & DATA_ERR) {
915ffa52 481 err = -ECOMM;
8a573022
AG
482 goto out;
483 }
9b74dc56 484 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
71689776 485
4683b220
PF
486 /*
487 * Need invalidate the dcache here again to avoid any
488 * cache-fill during the DMA operations such as the
489 * speculative pre-fetching etc.
490 */
54899fc8
EN
491 if (data->flags & MMC_DATA_READ)
492 check_and_invalidate_dcache_range(cmd, data);
71689776 493#endif
50586ef2
AF
494 }
495
8a573022
AG
496out:
497 /* Reset CMD and DATA portions on error */
498 if (err) {
499 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
500 SYSCTL_RSTC);
501 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
502 ;
503
504 if (data) {
505 esdhc_write32(&regs->sysctl,
506 esdhc_read32(&regs->sysctl) |
507 SYSCTL_RSTD);
508 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
509 ;
510 }
f022d36e
OS
511
512 /* If this was CMD11, then notify that power cycle is needed */
513 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
514 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
8a573022
AG
515 }
516
c67bee14 517 esdhc_write32(&regs->irqstat, -1);
50586ef2 518
8a573022 519 return err;
50586ef2
AF
520}
521
eafa90a1 522static void set_sysctl(struct mmc *mmc, uint clock)
50586ef2 523{
50586ef2 524 int div, pre_div;
96f0407b
PF
525 struct fsl_esdhc_priv *priv = mmc->priv;
526 struct fsl_esdhc *regs = priv->esdhc_regs;
527 int sdhc_clk = priv->sdhc_clk;
50586ef2
AF
528 uint clk;
529
93bfd616
PA
530 if (clock < mmc->cfg->f_min)
531 clock = mmc->cfg->f_min;
c67bee14 532
50586ef2
AF
533 if (sdhc_clk / 16 > clock) {
534 for (pre_div = 2; pre_div < 256; pre_div *= 2)
535 if ((sdhc_clk / pre_div) <= (clock * 16))
536 break;
537 } else
538 pre_div = 2;
539
540 for (div = 1; div <= 16; div++)
541 if ((sdhc_clk / (div * pre_div)) <= clock)
542 break;
543
0e1bf614 544 pre_div >>= mmc->ddr_mode ? 2 : 1;
50586ef2
AF
545 div -= 1;
546
547 clk = (pre_div << 8) | (div << 4);
548
f0b5f23f 549#ifdef CONFIG_FSL_USDHC
84ecdf6d 550 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
f0b5f23f 551#else
cc4d1226 552 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
f0b5f23f 553#endif
c67bee14
SB
554
555 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
50586ef2
AF
556
557 udelay(10000);
558
f0b5f23f 559#ifdef CONFIG_FSL_USDHC
84ecdf6d 560 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
f0b5f23f
EN
561#else
562 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
563#endif
c67bee14 564
50586ef2
AF
565}
566
2d9ca2c7
YL
567#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
568static void esdhc_clock_control(struct mmc *mmc, bool enable)
569{
96f0407b
PF
570 struct fsl_esdhc_priv *priv = mmc->priv;
571 struct fsl_esdhc *regs = priv->esdhc_regs;
2d9ca2c7
YL
572 u32 value;
573 u32 time_out;
574
575 value = esdhc_read32(&regs->sysctl);
576
577 if (enable)
578 value |= SYSCTL_CKEN;
579 else
580 value &= ~SYSCTL_CKEN;
581
582 esdhc_write32(&regs->sysctl, value);
583
584 time_out = 20;
585 value = PRSSTAT_SDSTB;
586 while (!(esdhc_read32(&regs->prsstat) & value)) {
587 if (time_out == 0) {
588 printf("fsl_esdhc: Internal clock never stabilised.\n");
589 break;
590 }
591 time_out--;
592 mdelay(1);
593 }
594}
595#endif
596
50586ef2
AF
597static void esdhc_set_ios(struct mmc *mmc)
598{
96f0407b
PF
599 struct fsl_esdhc_priv *priv = mmc->priv;
600 struct fsl_esdhc *regs = priv->esdhc_regs;
50586ef2 601
2d9ca2c7
YL
602#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
603 /* Select to use peripheral clock */
604 esdhc_clock_control(mmc, false);
605 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
606 esdhc_clock_control(mmc, true);
607#endif
50586ef2
AF
608 /* Set the clock speed */
609 set_sysctl(mmc, mmc->clock);
610
611 /* Set the bus width */
c67bee14 612 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
50586ef2
AF
613
614 if (mmc->bus_width == 4)
c67bee14 615 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
50586ef2 616 else if (mmc->bus_width == 8)
c67bee14
SB
617 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
618
50586ef2
AF
619}
620
621static int esdhc_init(struct mmc *mmc)
622{
96f0407b
PF
623 struct fsl_esdhc_priv *priv = mmc->priv;
624 struct fsl_esdhc *regs = priv->esdhc_regs;
50586ef2
AF
625 int timeout = 1000;
626
c67bee14 627 /* Reset the entire host controller */
a61da72b 628 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
c67bee14
SB
629
630 /* Wait until the controller is available */
631 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
632 udelay(1000);
50586ef2 633
f53225cc
PF
634#if defined(CONFIG_FSL_USDHC)
635 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
636 esdhc_write32(&regs->mmcboot, 0x0);
637 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
638 esdhc_write32(&regs->mixctrl, 0x0);
639 esdhc_write32(&regs->clktunectrlstatus, 0x0);
640
641 /* Put VEND_SPEC to default value */
642 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
643
644 /* Disable DLL_CTRL delay line */
645 esdhc_write32(&regs->dllctrl, 0x0);
646#endif
647
16e43f35 648#ifndef ARCH_MXC
2c1764ef 649 /* Enable cache snooping */
16e43f35
BT
650 esdhc_write32(&regs->scr, 0x00000040);
651#endif
2c1764ef 652
f0b5f23f 653#ifndef CONFIG_FSL_USDHC
a61da72b 654 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
84ecdf6d
YL
655#else
656 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
f0b5f23f 657#endif
50586ef2
AF
658
659 /* Set the initial clock speed */
4a6ee172 660 mmc_set_clock(mmc, 400000);
50586ef2
AF
661
662 /* Disable the BRR and BWR bits in IRQSTAT */
c67bee14 663 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
50586ef2
AF
664
665 /* Put the PROCTL reg back to the default */
c67bee14 666 esdhc_write32(&regs->proctl, PROCTL_INIT);
50586ef2 667
c67bee14
SB
668 /* Set timout to the maximum value */
669 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
50586ef2 670
ee0c5389
OS
671#ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
672 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
673#endif
674
d48d2e21
TR
675 return 0;
676}
50586ef2 677
d48d2e21
TR
678static int esdhc_getcd(struct mmc *mmc)
679{
96f0407b
PF
680 struct fsl_esdhc_priv *priv = mmc->priv;
681 struct fsl_esdhc *regs = priv->esdhc_regs;
d48d2e21
TR
682 int timeout = 1000;
683
f7e27cc5
HZ
684#ifdef CONFIG_ESDHC_DETECT_QUIRK
685 if (CONFIG_ESDHC_DETECT_QUIRK)
686 return 1;
687#endif
96f0407b
PF
688
689#ifdef CONFIG_DM_MMC
690 if (priv->non_removable)
691 return 1;
fc8048a8 692#ifdef CONFIG_DM_GPIO
96f0407b
PF
693 if (dm_gpio_is_valid(&priv->cd_gpio))
694 return dm_gpio_get_value(&priv->cd_gpio);
fc8048a8 695#endif
96f0407b
PF
696#endif
697
d48d2e21
TR
698 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
699 udelay(1000);
c67bee14 700
d48d2e21 701 return timeout > 0;
50586ef2
AF
702}
703
48bb3bb5
JH
704static void esdhc_reset(struct fsl_esdhc *regs)
705{
706 unsigned long timeout = 100; /* wait max 100 ms */
707
708 /* reset the controller */
a61da72b 709 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
48bb3bb5
JH
710
711 /* hardware clears the bit when it is done */
712 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
713 udelay(1000);
714 if (!timeout)
715 printf("MMC/SD: Reset never completed.\n");
716}
717
ab769f22
PA
718static const struct mmc_ops esdhc_ops = {
719 .send_cmd = esdhc_send_cmd,
720 .set_ios = esdhc_set_ios,
721 .init = esdhc_init,
722 .getcd = esdhc_getcd,
723};
724
96f0407b
PF
725static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
726 struct fsl_esdhc_priv *priv)
727{
728 if (!cfg || !priv)
729 return -EINVAL;
730
731 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
732 priv->bus_width = cfg->max_bus_width;
733 priv->sdhc_clk = cfg->sdhc_clk;
1483151e 734 priv->wp_enable = cfg->wp_enable;
96f0407b
PF
735
736 return 0;
737};
738
739static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
50586ef2 740{
c67bee14 741 struct fsl_esdhc *regs;
50586ef2 742 struct mmc *mmc;
030955c2 743 u32 caps, voltage_caps;
50586ef2 744
96f0407b
PF
745 if (!priv)
746 return -EINVAL;
c67bee14 747
96f0407b 748 regs = priv->esdhc_regs;
c67bee14 749
48bb3bb5
JH
750 /* First reset the eSDHC controller */
751 esdhc_reset(regs);
752
f0b5f23f 753#ifndef CONFIG_FSL_USDHC
975324a7
JH
754 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
755 | SYSCTL_IPGEN | SYSCTL_CKEN);
84ecdf6d
YL
756#else
757 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
758 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
f0b5f23f 759#endif
975324a7 760
a3d6e386 761 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
96f0407b 762 memset(&priv->cfg, 0, sizeof(priv->cfg));
93bfd616 763
030955c2 764 voltage_caps = 0;
19060bd8 765 caps = esdhc_read32(&regs->hostcapblt);
3b4456ec
RZ
766
767#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
768 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
769 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
770#endif
ef38f3ff
HZ
771
772/* T4240 host controller capabilities register should have VS33 bit */
773#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
774 caps = caps | ESDHC_HOSTCAPBLT_VS33;
775#endif
776
50586ef2 777 if (caps & ESDHC_HOSTCAPBLT_VS18)
030955c2 778 voltage_caps |= MMC_VDD_165_195;
50586ef2 779 if (caps & ESDHC_HOSTCAPBLT_VS30)
030955c2 780 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
50586ef2 781 if (caps & ESDHC_HOSTCAPBLT_VS33)
030955c2
LY
782 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
783
96f0407b
PF
784 priv->cfg.name = "FSL_SDHC";
785 priv->cfg.ops = &esdhc_ops;
030955c2 786#ifdef CONFIG_SYS_SD_VOLTAGE
96f0407b 787 priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
030955c2 788#else
96f0407b 789 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
030955c2 790#endif
96f0407b 791 if ((priv->cfg.voltages & voltage_caps) == 0) {
030955c2
LY
792 printf("voltage not supported by controller\n");
793 return -1;
794 }
50586ef2 795
96f0407b
PF
796 if (priv->bus_width == 8)
797 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
798 else if (priv->bus_width == 4)
799 priv->cfg.host_caps = MMC_MODE_4BIT;
800
801 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
0e1bf614 802#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
96f0407b 803 priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
0e1bf614 804#endif
50586ef2 805
96f0407b
PF
806 if (priv->bus_width > 0) {
807 if (priv->bus_width < 8)
808 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
809 if (priv->bus_width < 4)
810 priv->cfg.host_caps &= ~MMC_MODE_4BIT;
aad4659a
AR
811 }
812
50586ef2 813 if (caps & ESDHC_HOSTCAPBLT_HSS)
96f0407b 814 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
50586ef2 815
d47e3d27
HZ
816#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
817 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
96f0407b 818 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
d47e3d27
HZ
819#endif
820
96f0407b
PF
821 priv->cfg.f_min = 400000;
822 priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
50586ef2 823
96f0407b 824 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
93bfd616 825
96f0407b 826 mmc = mmc_create(&priv->cfg, priv);
93bfd616
PA
827 if (mmc == NULL)
828 return -1;
50586ef2 829
96f0407b
PF
830 priv->mmc = mmc;
831
832 return 0;
833}
834
835int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
836{
837 struct fsl_esdhc_priv *priv;
838 int ret;
839
840 if (!cfg)
841 return -EINVAL;
842
843 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
844 if (!priv)
845 return -ENOMEM;
846
847 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
848 if (ret) {
849 debug("%s xlate failure\n", __func__);
850 free(priv);
851 return ret;
852 }
853
854 ret = fsl_esdhc_init(priv);
855 if (ret) {
856 debug("%s init failure\n", __func__);
857 free(priv);
858 return ret;
859 }
860
50586ef2
AF
861 return 0;
862}
863
864int fsl_esdhc_mmc_init(bd_t *bis)
865{
c67bee14
SB
866 struct fsl_esdhc_cfg *cfg;
867
88227a1d 868 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
c67bee14 869 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
e9adeca3 870 cfg->sdhc_clk = gd->arch.sdhc_clk;
c67bee14 871 return fsl_esdhc_initialize(bis, cfg);
50586ef2 872}
b33433a6 873
5a8dbdc6
YL
874#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
875void mmc_adapter_card_type_ident(void)
876{
877 u8 card_id;
878 u8 value;
879
880 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
881 gd->arch.sdhc_adapter = card_id;
882
883 switch (card_id) {
884 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
cdc69550
YL
885 value = QIXIS_READ(brdcfg[5]);
886 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
887 QIXIS_WRITE(brdcfg[5], value);
5a8dbdc6
YL
888 break;
889 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
bf50be83
YL
890 value = QIXIS_READ(pwr_ctl[1]);
891 value |= QIXIS_EVDD_BY_SDHC_VS;
892 QIXIS_WRITE(pwr_ctl[1], value);
5a8dbdc6
YL
893 break;
894 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
895 value = QIXIS_READ(brdcfg[5]);
896 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
897 QIXIS_WRITE(brdcfg[5], value);
898 break;
899 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
900 break;
901 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
902 break;
903 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
904 break;
905 case QIXIS_ESDHC_NO_ADAPTER:
906 break;
907 default:
908 break;
909 }
910}
911#endif
912
c67bee14 913#ifdef CONFIG_OF_LIBFDT
b33433a6
AV
914void fdt_fixup_esdhc(void *blob, bd_t *bd)
915{
916 const char *compat = "fsl,esdhc";
b33433a6 917
a6da8b81 918#ifdef CONFIG_FSL_ESDHC_PIN_MUX
b33433a6 919 if (!hwconfig("esdhc")) {
a6da8b81
CZ
920 do_fixup_by_compat(blob, compat, "status", "disabled",
921 8 + 1, 1);
922 return;
b33433a6 923 }
a6da8b81 924#endif
b33433a6 925
2d9ca2c7
YL
926#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
927 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
928 gd->arch.sdhc_clk, 1);
929#else
b33433a6 930 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
e9adeca3 931 gd->arch.sdhc_clk, 1);
2d9ca2c7 932#endif
5a8dbdc6
YL
933#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
934 do_fixup_by_compat_u32(blob, compat, "adapter-type",
935 (u32)(gd->arch.sdhc_adapter), 1);
936#endif
a6da8b81
CZ
937 do_fixup_by_compat(blob, compat, "status", "okay",
938 4 + 1, 1);
b33433a6 939}
c67bee14 940#endif
96f0407b
PF
941
942#ifdef CONFIG_DM_MMC
943#include <asm/arch/clock.h>
944static int fsl_esdhc_probe(struct udevice *dev)
945{
946 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
947 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
948 const void *fdt = gd->fdt_blob;
949 int node = dev->of_offset;
950 fdt_addr_t addr;
951 unsigned int val;
952 int ret;
953
954 addr = dev_get_addr(dev);
955 if (addr == FDT_ADDR_T_NONE)
956 return -EINVAL;
957
958 priv->esdhc_regs = (struct fsl_esdhc *)addr;
959 priv->dev = dev;
960
961 val = fdtdec_get_int(fdt, node, "bus-width", -1);
962 if (val == 8)
963 priv->bus_width = 8;
964 else if (val == 4)
965 priv->bus_width = 4;
966 else
967 priv->bus_width = 1;
968
969 if (fdt_get_property(fdt, node, "non-removable", NULL)) {
970 priv->non_removable = 1;
971 } else {
972 priv->non_removable = 0;
fc8048a8 973#ifdef CONFIG_DM_GPIO
96f0407b
PF
974 gpio_request_by_name_nodev(fdt, node, "cd-gpios", 0,
975 &priv->cd_gpio, GPIOD_IS_IN);
fc8048a8 976#endif
96f0407b
PF
977 }
978
1483151e
PF
979 priv->wp_enable = 1;
980
fc8048a8 981#ifdef CONFIG_DM_GPIO
1483151e
PF
982 ret = gpio_request_by_name_nodev(fdt, node, "wp-gpios", 0,
983 &priv->wp_gpio, GPIOD_IS_IN);
984 if (ret)
985 priv->wp_enable = 0;
fc8048a8 986#endif
96f0407b
PF
987 /*
988 * TODO:
989 * Because lack of clk driver, if SDHC clk is not enabled,
990 * need to enable it first before this driver is invoked.
991 *
992 * we use MXC_ESDHC_CLK to get clk freq.
993 * If one would like to make this function work,
994 * the aliases should be provided in dts as this:
995 *
996 * aliases {
997 * mmc0 = &usdhc1;
998 * mmc1 = &usdhc2;
999 * mmc2 = &usdhc3;
1000 * mmc3 = &usdhc4;
1001 * };
1002 * Then if your board only supports mmc2 and mmc3, but we can
1003 * correctly get the seq as 2 and 3, then let mxc_get_clock
1004 * work as expected.
1005 */
1006 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1007 if (priv->sdhc_clk <= 0) {
1008 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1009 return -EINVAL;
1010 }
1011
1012 ret = fsl_esdhc_init(priv);
1013 if (ret) {
1014 dev_err(dev, "fsl_esdhc_init failure\n");
1015 return ret;
1016 }
1017
1018 upriv->mmc = priv->mmc;
35ae9946 1019 priv->mmc->dev = dev;
96f0407b
PF
1020
1021 return 0;
1022}
1023
1024static const struct udevice_id fsl_esdhc_ids[] = {
1025 { .compatible = "fsl,imx6ul-usdhc", },
1026 { .compatible = "fsl,imx6sx-usdhc", },
1027 { .compatible = "fsl,imx6sl-usdhc", },
1028 { .compatible = "fsl,imx6q-usdhc", },
1029 { .compatible = "fsl,imx7d-usdhc", },
1030 { /* sentinel */ }
1031};
1032
1033U_BOOT_DRIVER(fsl_esdhc) = {
1034 .name = "fsl-esdhc-mmc",
1035 .id = UCLASS_MMC,
1036 .of_match = fsl_esdhc_ids,
1037 .probe = fsl_esdhc_probe,
1038 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1039};
1040#endif