2 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
5 * Based vaguely on the pxa mmc code:
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
9 * SPDX-License-Identifier: GPL-2.0+
20 #include <fsl_esdhc.h>
21 #include <fdt_support.h>
24 DECLARE_GLOBAL_DATA_PTR;
54 /* Return the XFERTYP flags for a given command and data packet */
55 static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
60 xfertyp |= XFERTYP_DPSEL;
61 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
62 xfertyp |= XFERTYP_DMAEN;
64 if (data->blocks > 1) {
65 xfertyp |= XFERTYP_MSBSEL;
66 xfertyp |= XFERTYP_BCEN;
67 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
68 xfertyp |= XFERTYP_AC12EN;
72 if (data->flags & MMC_DATA_READ)
73 xfertyp |= XFERTYP_DTDSEL;
76 if (cmd->resp_type & MMC_RSP_CRC)
77 xfertyp |= XFERTYP_CCCEN;
78 if (cmd->resp_type & MMC_RSP_OPCODE)
79 xfertyp |= XFERTYP_CICEN;
80 if (cmd->resp_type & MMC_RSP_136)
81 xfertyp |= XFERTYP_RSPTYP_136;
82 else if (cmd->resp_type & MMC_RSP_BUSY)
83 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
84 else if (cmd->resp_type & MMC_RSP_PRESENT)
85 xfertyp |= XFERTYP_RSPTYP_48;
87 #if defined(CONFIG_MX53) || defined(CONFIG_T4240QDS)
88 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
89 xfertyp |= XFERTYP_CMDTYP_ABORT;
91 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
94 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
96 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
99 esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
101 struct fsl_esdhc_cfg *cfg = mmc->priv;
102 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
110 if (data->flags & MMC_DATA_READ) {
111 blocks = data->blocks;
114 timeout = PIO_TIMEOUT;
115 size = data->blocksize;
116 irqstat = esdhc_read32(®s->irqstat);
117 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)
120 printf("\nData Read Failed in PIO Mode.");
123 while (size && (!(irqstat & IRQSTAT_TC))) {
124 udelay(100); /* Wait before last byte transfer complete */
125 irqstat = esdhc_read32(®s->irqstat);
126 databuf = in_le32(®s->datport);
127 *((uint *)buffer) = databuf;
134 blocks = data->blocks;
135 buffer = (char *)data->src;
137 timeout = PIO_TIMEOUT;
138 size = data->blocksize;
139 irqstat = esdhc_read32(®s->irqstat);
140 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)
143 printf("\nData Write Failed in PIO Mode.");
146 while (size && (!(irqstat & IRQSTAT_TC))) {
147 udelay(100); /* Wait before last byte transfer complete */
148 databuf = *((uint *)buffer);
151 irqstat = esdhc_read32(®s->irqstat);
152 out_le32(®s->datport, databuf);
160 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
163 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
164 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
165 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
168 wml_value = data->blocksize/4;
170 if (data->flags & MMC_DATA_READ) {
171 if (wml_value > WML_RD_WML_MAX)
172 wml_value = WML_RD_WML_MAX_VAL;
174 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
175 esdhc_write32(®s->dsaddr, (u32)data->dest);
177 flush_dcache_range((ulong)data->src,
178 (ulong)data->src+data->blocks
181 if (wml_value > WML_WR_WML_MAX)
182 wml_value = WML_WR_WML_MAX_VAL;
183 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
184 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
188 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
190 esdhc_write32(®s->dsaddr, (u32)data->src);
192 #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
193 if (!(data->flags & MMC_DATA_READ)) {
194 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
195 printf("\nThe SD card is locked. "
196 "Can not write to a locked card.\n\n");
199 esdhc_write32(®s->dsaddr, (u32)data->src);
201 esdhc_write32(®s->dsaddr, (u32)data->dest);
202 #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
204 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
206 /* Calculate the timeout period for data transactions */
208 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
209 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
210 * So, Number of SD Clock cycles for 0.25sec should be minimum
211 * (SD Clock/sec * 0.25 sec) SD Clock cycles
212 * = (mmc->tran_speed * 1/4) SD Clock cycles
214 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
215 * Taking log2 both the sides
216 * => timeout + 13 >= log2(mmc->tran_speed/4)
217 * Rounding up to next power of 2
218 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
219 * => timeout + 13 = fls(mmc->tran_speed/4)
221 timeout = fls(mmc->tran_speed/4);
230 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
231 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
235 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
240 static void check_and_invalidate_dcache_range
241 (struct mmc_cmd *cmd,
242 struct mmc_data *data) {
243 unsigned start = (unsigned)data->dest ;
244 unsigned size = roundup(ARCH_DMA_MINALIGN,
245 data->blocks*data->blocksize);
246 unsigned end = start+size ;
247 invalidate_dcache_range(start, end);
250 * Sends a command out on the bus. Takes the mmc pointer,
251 * a command pointer, and an optional data pointer.
254 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
258 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
259 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
261 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
262 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
266 esdhc_write32(®s->irqstat, -1);
270 /* Wait for the bus to be idle */
271 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
272 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
275 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
278 /* Wait at least 8 SD clock cycles before the next command */
280 * Note: This is way more than 8 cycles, but 1ms seems to
281 * resolve timing issues with some cards
285 /* Set up for a data transfer if we have one */
289 err = esdhc_setup_data(mmc, data);
294 /* Figure out the transfer arguments */
295 xfertyp = esdhc_xfertyp(cmd, data);
298 esdhc_write32(®s->irqsigen, 0);
300 /* Send the command */
301 esdhc_write32(®s->cmdarg, cmd->cmdarg);
302 #if defined(CONFIG_FSL_USDHC)
303 esdhc_write32(®s->mixctrl,
304 (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
305 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
307 esdhc_write32(®s->xfertyp, xfertyp);
310 /* Wait for the command to complete */
311 while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
314 irqstat = esdhc_read32(®s->irqstat);
316 /* Reset CMD and DATA portions on error */
317 if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
318 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
320 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC)
324 esdhc_write32(®s->sysctl,
325 esdhc_read32(®s->sysctl) |
327 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD))
332 if (irqstat & CMD_ERR)
335 if (irqstat & IRQSTAT_CTOE)
338 /* Workaround for ESDHC errata ENGcm03648 */
339 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
342 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
343 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
350 printf("Timeout waiting for DAT0 to go high!\n");
355 /* Copy the response to the response buffer */
356 if (cmd->resp_type & MMC_RSP_136) {
357 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
359 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
360 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
361 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
362 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
363 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
364 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
365 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
366 cmd->response[3] = (cmdrsp0 << 8);
368 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
370 /* Wait until all of the blocks are transferred */
372 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
373 esdhc_pio_read_write(mmc, data);
376 irqstat = esdhc_read32(®s->irqstat);
378 if (irqstat & IRQSTAT_DTOE)
381 if (irqstat & DATA_ERR)
383 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
385 if (data->flags & MMC_DATA_READ)
386 check_and_invalidate_dcache_range(cmd, data);
389 esdhc_write32(®s->irqstat, -1);
394 static void set_sysctl(struct mmc *mmc, uint clock)
397 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
398 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
399 int sdhc_clk = cfg->sdhc_clk;
402 if (clock < mmc->f_min)
405 if (sdhc_clk / 16 > clock) {
406 for (pre_div = 2; pre_div < 256; pre_div *= 2)
407 if ((sdhc_clk / pre_div) <= (clock * 16))
412 for (div = 1; div <= 16; div++)
413 if ((sdhc_clk / (div * pre_div)) <= clock)
419 clk = (pre_div << 8) | (div << 4);
421 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
423 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
427 clk = SYSCTL_PEREN | SYSCTL_CKEN;
429 esdhc_setbits32(®s->sysctl, clk);
432 static void esdhc_set_ios(struct mmc *mmc)
434 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
435 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
437 /* Set the clock speed */
438 set_sysctl(mmc, mmc->clock);
440 /* Set the bus width */
441 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
443 if (mmc->bus_width == 4)
444 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
445 else if (mmc->bus_width == 8)
446 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
450 static int esdhc_init(struct mmc *mmc)
452 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
453 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
456 /* Reset the entire host controller */
457 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
459 /* Wait until the controller is available */
460 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
464 /* Enable cache snooping */
465 esdhc_write32(®s->scr, 0x00000040);
468 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
470 /* Set the initial clock speed */
471 mmc_set_clock(mmc, 400000);
473 /* Disable the BRR and BWR bits in IRQSTAT */
474 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
476 /* Put the PROCTL reg back to the default */
477 esdhc_write32(®s->proctl, PROCTL_INIT);
479 /* Set timout to the maximum value */
480 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
485 static int esdhc_getcd(struct mmc *mmc)
487 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
488 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
491 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
497 static void esdhc_reset(struct fsl_esdhc *regs)
499 unsigned long timeout = 100; /* wait max 100 ms */
501 /* reset the controller */
502 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
504 /* hardware clears the bit when it is done */
505 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
508 printf("MMC/SD: Reset never completed.\n");
511 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
513 struct fsl_esdhc *regs;
515 u32 caps, voltage_caps;
520 mmc = malloc(sizeof(struct mmc));
522 sprintf(mmc->name, "FSL_SDHC");
523 regs = (struct fsl_esdhc *)cfg->esdhc_base;
525 /* First reset the eSDHC controller */
528 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
529 | SYSCTL_IPGEN | SYSCTL_CKEN);
532 mmc->send_cmd = esdhc_send_cmd;
533 mmc->set_ios = esdhc_set_ios;
534 mmc->init = esdhc_init;
535 mmc->getcd = esdhc_getcd;
539 caps = regs->hostcapblt;
541 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
542 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
543 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
545 if (caps & ESDHC_HOSTCAPBLT_VS18)
546 voltage_caps |= MMC_VDD_165_195;
547 if (caps & ESDHC_HOSTCAPBLT_VS30)
548 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
549 if (caps & ESDHC_HOSTCAPBLT_VS33)
550 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
552 #ifdef CONFIG_SYS_SD_VOLTAGE
553 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
555 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
557 if ((mmc->voltages & voltage_caps) == 0) {
558 printf("voltage not supported by controller\n");
562 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
564 if (cfg->max_bus_width > 0) {
565 if (cfg->max_bus_width < 8)
566 mmc->host_caps &= ~MMC_MODE_8BIT;
567 if (cfg->max_bus_width < 4)
568 mmc->host_caps &= ~MMC_MODE_4BIT;
571 if (caps & ESDHC_HOSTCAPBLT_HSS)
572 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
575 mmc->f_max = MIN(gd->arch.sdhc_clk, 52000000);
583 int fsl_esdhc_mmc_init(bd_t *bis)
585 struct fsl_esdhc_cfg *cfg;
587 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
588 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
589 cfg->sdhc_clk = gd->arch.sdhc_clk;
590 return fsl_esdhc_initialize(bis, cfg);
593 #ifdef CONFIG_OF_LIBFDT
594 void fdt_fixup_esdhc(void *blob, bd_t *bd)
596 const char *compat = "fsl,esdhc";
598 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
599 if (!hwconfig("esdhc")) {
600 do_fixup_by_compat(blob, compat, "status", "disabled",
606 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
607 gd->arch.sdhc_clk, 1);
609 do_fixup_by_compat(blob, compat, "status", "okay",