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 = cfg->esdhc_base;
108 int wml = esdhc_read32(®s->wml);
110 if (data->flags & MMC_DATA_READ) {
111 wml &= WML_RD_WML_MASK;
112 blocks = data->blocks;
115 timeout = PIO_TIMEOUT;
116 size = data->blocksize;
118 !(esdhc_read32(®s->irqstat) & IRQSTAT_TC)) {
122 while (!((prsstat = esdhc_read32(®s->prsstat)) &
123 PRSSTAT_BREN) && --timeout)
125 if (!(prsstat & PRSSTAT_BREN)) {
126 printf("%s: Data Read Failed in PIO Mode\n",
130 for (i = 0; i < wml && size; i++) {
131 databuf = in_le32(®s->datport);
132 memcpy(buffer, &databuf, sizeof(databuf));
140 wml = (wml & WML_WR_WML_MASK) >> 16;
141 blocks = data->blocks;
142 buffer = (char *)data->src; /* cast away 'const' */
144 timeout = PIO_TIMEOUT;
145 size = data->blocksize;
147 !(esdhc_read32(®s->irqstat) & IRQSTAT_TC)) {
151 while (!((prsstat = esdhc_read32(®s->prsstat)) &
152 PRSSTAT_BWEN) && --timeout)
154 if (!(prsstat & PRSSTAT_BWEN)) {
155 printf("%s: Data Write Failed in PIO Mode\n",
159 for (i = 0; i < wml && size; i++) {
160 memcpy(&databuf, buffer, sizeof(databuf));
161 out_le32(®s->datport, databuf);
172 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
175 struct fsl_esdhc_cfg *cfg = mmc->priv;
176 struct fsl_esdhc *regs = cfg->esdhc_base;
177 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
180 wml_value = data->blocksize / 4;
182 if (data->flags & MMC_DATA_READ) {
183 if (wml_value > WML_RD_WML_MAX)
184 wml_value = WML_RD_WML_MAX_VAL;
186 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
187 esdhc_write32(®s->dsaddr, (u32)data->dest);
189 if (wml_value > WML_WR_WML_MAX)
190 wml_value = WML_WR_WML_MAX_VAL;
191 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
192 printf("The SD card is locked. Can not write to a locked card.\n");
196 flush_dcache_range((unsigned long)data->src,
197 (unsigned long)data->src + data->blocks * data->blocksize);
198 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
200 esdhc_write32(®s->dsaddr, (u32)data->src);
202 #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
203 if (!(data->flags & MMC_DATA_READ)) {
204 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
205 printf("The SD card is locked. Can not write to a locked card.\n");
208 esdhc_write32(®s->dsaddr, (u32)data->src);
210 esdhc_write32(®s->dsaddr, (u32)data->dest);
212 #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
214 esdhc_write32(®s->blkattr, (data->blocks << 16) | data->blocksize);
216 /* Calculate the timeout period for data transactions */
218 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
219 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
220 * So, Number of SD Clock cycles for 0.25sec should be minimum
221 * (SD Clock/sec * 0.25 sec) SD Clock cycles
222 * = (mmc->tran_speed * 1/4) SD Clock cycles
224 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
225 * Taking log2 both the sides
226 * => timeout + 13 >= log2(mmc->tran_speed/4)
227 * Rounding up to next power of 2
228 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
229 * => timeout + 13 = fls(mmc->tran_speed/4)
231 timeout = fls(mmc->tran_speed / 4);
236 else if (timeout < 0)
239 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
240 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
243 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
248 static inline void check_and_invalidate_dcache_range(struct mmc_cmd *cmd,
249 struct mmc_data *data)
251 unsigned long start = (unsigned long)data->dest;
252 size_t start_ofs = start & (ARCH_DMA_MINALIGN - 1);
253 unsigned long size = data->blocks * data->blocksize;
254 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
257 invalidate_dcache_range(start, end);
261 * Sends a command out on the bus. Takes the mmc pointer,
262 * a command pointer, and an optional data pointer.
265 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
269 struct fsl_esdhc_cfg *cfg = mmc->priv;
270 volatile struct fsl_esdhc *regs = cfg->esdhc_base;
273 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
274 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
277 esdhc_write32(®s->irqstat, -1);
281 start = get_timer_masked();
282 /* Wait for the bus to be idle */
283 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
284 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) {
285 if (get_timer(start) > CONFIG_SYS_HZ) {
286 printf("%s: Timeout waiting for bus idle\n", __func__);
291 start = get_timer_masked();
292 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) {
293 if (get_timer(start) > CONFIG_SYS_HZ)
297 /* Wait at least 8 SD clock cycles before the next command */
299 * Note: This is way more than 8 cycles, but 1ms seems to
300 * resolve timing issues with some cards
304 /* Set up for a data transfer if we have one */
308 err = esdhc_setup_data(mmc, data);
313 /* Figure out the transfer arguments */
314 xfertyp = esdhc_xfertyp(cmd, data);
317 esdhc_write32(®s->irqsigen, 0);
319 /* Send the command */
320 esdhc_write32(®s->cmdarg, cmd->cmdarg);
321 #if defined(CONFIG_FSL_USDHC)
322 esdhc_write32(®s->mixctrl,
323 (esdhc_read32(®s->mixctrl) & ~0x7f) | (xfertyp & 0x7F));
324 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
326 esdhc_write32(®s->xfertyp, xfertyp);
330 esdhc_write32(®s->irqsigen, 0);
332 start = get_timer_masked();
333 /* Wait for the command to complete */
334 while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) {
335 if (get_timer(start) > CONFIG_SYS_HZ) {
336 printf("%s: Timeout waiting for cmd completion\n", __func__);
341 if (data && (data->flags & MMC_DATA_READ))
342 check_and_invalidate_dcache_range(cmd, data);
344 irqstat = esdhc_read32(®s->irqstat);
346 /* Reset CMD and DATA portions on error */
347 if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
348 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
350 start = get_timer_masked();
351 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) {
352 if (get_timer(start) > CONFIG_SYS_HZ)
357 esdhc_write32(®s->sysctl,
358 esdhc_read32(®s->sysctl) |
360 start = get_timer_masked();
361 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) {
362 if (get_timer(start) > CONFIG_SYS_HZ)
368 if (irqstat & CMD_ERR)
371 if (irqstat & IRQSTAT_CTOE)
374 /* Workaround for ESDHC errata ENGcm03648 */
375 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
378 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
379 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
386 printf("Timeout waiting for DAT0 to go high!\n");
391 /* Copy the response to the response buffer */
392 if (cmd->resp_type & MMC_RSP_136) {
393 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
395 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
396 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
397 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
398 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
399 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
400 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
401 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
402 cmd->response[3] = (cmdrsp0 << 8);
404 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
406 /* Wait until all of the blocks are transferred */
408 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
409 esdhc_pio_read_write(mmc, data);
411 unsigned long start = get_timer_masked();
412 unsigned long data_timeout = data->blocks *
413 data->blocksize * 100 / mmc->bus_width /
414 (mmc->tran_speed / CONFIG_SYS_HZ) + CONFIG_SYS_HZ;
417 irqstat = esdhc_read32(®s->irqstat);
419 if (irqstat & IRQSTAT_DTOE) {
420 printf("MMC/SD data %s timeout\n",
421 data->flags & MMC_DATA_READ ?
426 if (irqstat & DATA_ERR) {
427 printf("MMC/SD data error\n");
431 if (get_timer(start) > data_timeout) {
432 printf("MMC/SD timeout waiting for %s xfer completion\n",
433 data->flags & MMC_DATA_READ ?
437 } while (!(irqstat & IRQSTAT_TC) &&
438 (esdhc_read32(®s->prsstat) & PRSSTAT_DLA));
440 check_and_invalidate_dcache_range(cmd, data);
444 esdhc_write32(®s->irqstat, irqstat);
449 static void set_sysctl(struct mmc *mmc, uint clock)
452 struct fsl_esdhc_cfg *cfg = mmc->priv;
453 volatile struct fsl_esdhc *regs = cfg->esdhc_base;
454 int sdhc_clk = cfg->sdhc_clk;
457 if (clock < mmc->f_min)
460 if (sdhc_clk / 16 > clock) {
461 for (pre_div = 2; pre_div < 256; pre_div *= 2)
462 if ((sdhc_clk / pre_div) <= (clock * 16))
467 for (div = 1; div <= 16; div++)
468 if ((sdhc_clk / (div * pre_div)) <= clock)
474 clk = (pre_div << 8) | (div << 4);
476 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
478 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
482 clk = SYSCTL_PEREN | SYSCTL_CKEN;
484 esdhc_setbits32(®s->sysctl, clk);
487 static void esdhc_set_ios(struct mmc *mmc)
489 struct fsl_esdhc_cfg *cfg = mmc->priv;
490 struct fsl_esdhc *regs = cfg->esdhc_base;
492 /* Set the clock speed */
493 set_sysctl(mmc, mmc->clock);
495 /* Set the bus width */
496 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
498 if (mmc->bus_width == 4)
499 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
500 else if (mmc->bus_width == 8)
501 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
505 static int esdhc_init(struct mmc *mmc)
507 struct fsl_esdhc_cfg *cfg = mmc->priv;
508 struct fsl_esdhc *regs = cfg->esdhc_base;
511 /* Reset the entire host controller */
512 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
514 /* Wait until the controller is available */
515 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
519 /* Enable cache snooping */
520 esdhc_write32(®s->scr, 0x00000040);
523 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
525 /* Set the initial clock speed */
526 mmc_set_clock(mmc, 400000);
528 /* Disable the BRR and BWR bits in IRQSTAT */
529 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
531 /* Put the PROCTL reg back to the default */
532 esdhc_write32(®s->proctl, PROCTL_INIT);
534 /* Set timout to the maximum value */
535 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
540 static int esdhc_getcd(struct mmc *mmc)
542 struct fsl_esdhc_cfg *cfg = mmc->priv;
543 struct fsl_esdhc *regs = cfg->esdhc_base;
546 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
552 static void esdhc_reset(struct fsl_esdhc *regs)
554 unsigned long timeout = 100; /* wait max 100 ms */
556 /* reset the controller */
557 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
559 /* hardware clears the bit when it is done */
560 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
563 printf("MMC/SD: Reset never completed.\n");
566 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
568 struct fsl_esdhc *regs;
570 u32 caps, voltage_caps;
575 mmc = kzalloc(sizeof(struct mmc), GFP_KERNEL);
579 sprintf(mmc->name, "FSL_SDHC");
580 regs = cfg->esdhc_base;
582 /* First reset the eSDHC controller */
585 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
586 | SYSCTL_IPGEN | SYSCTL_CKEN);
589 mmc->send_cmd = esdhc_send_cmd;
590 mmc->set_ios = esdhc_set_ios;
591 mmc->init = esdhc_init;
592 mmc->getcd = esdhc_getcd;
596 caps = regs->hostcapblt;
598 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
599 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
600 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
602 if (caps & ESDHC_HOSTCAPBLT_VS18)
603 voltage_caps |= MMC_VDD_165_195;
604 if (caps & ESDHC_HOSTCAPBLT_VS30)
605 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
606 if (caps & ESDHC_HOSTCAPBLT_VS33)
607 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
609 #ifdef CONFIG_SYS_SD_VOLTAGE
610 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
612 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
614 if ((mmc->voltages & voltage_caps) == 0) {
615 printf("voltage not supported by controller\n");
619 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
621 if (cfg->max_bus_width > 0) {
622 if (cfg->max_bus_width < 8)
623 mmc->host_caps &= ~MMC_MODE_8BIT;
624 if (cfg->max_bus_width < 4)
625 mmc->host_caps &= ~MMC_MODE_4BIT;
628 if (caps & ESDHC_HOSTCAPBLT_HSS)
629 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
632 mmc->f_max = MIN(cfg->sdhc_clk, 52000000);
640 int fsl_esdhc_mmc_init(bd_t *bis)
642 struct fsl_esdhc_cfg *cfg;
644 cfg = kzalloc(sizeof(struct fsl_esdhc_cfg), GFP_KERNEL);
647 cfg->esdhc_base = (void __iomem *)CONFIG_SYS_FSL_ESDHC_ADDR;
648 cfg->sdhc_clk = gd->arch.sdhc_clk;
649 return fsl_esdhc_initialize(bis, cfg);
652 #ifdef CONFIG_OF_LIBFDT
653 void fdt_fixup_esdhc(void *blob, bd_t *bd)
655 const char *compat = "fsl,esdhc";
657 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
658 if (!hwconfig("esdhc")) {
659 do_fixup_by_compat(blob, compat, "status", "disabled",
665 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
666 gd->arch.sdhc_clk, 1);
668 do_fixup_by_compat(blob, compat, "status", "okay",