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