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