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 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #include <fsl_esdhc.h>
37 #include <fdt_support.h>
40 DECLARE_GLOBAL_DATA_PTR;
70 /* Return the XFERTYP flags for a given command and data packet */
71 uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
76 xfertyp |= XFERTYP_DPSEL;
77 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
78 xfertyp |= XFERTYP_DMAEN;
80 if (data->blocks > 1) {
81 xfertyp |= XFERTYP_MSBSEL;
82 xfertyp |= XFERTYP_BCEN;
83 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
84 xfertyp |= XFERTYP_AC12EN;
88 if (data->flags & MMC_DATA_READ)
89 xfertyp |= XFERTYP_DTDSEL;
92 if (cmd->resp_type & MMC_RSP_CRC)
93 xfertyp |= XFERTYP_CCCEN;
94 if (cmd->resp_type & MMC_RSP_OPCODE)
95 xfertyp |= XFERTYP_CICEN;
96 if (cmd->resp_type & MMC_RSP_136)
97 xfertyp |= XFERTYP_RSPTYP_136;
98 else if (cmd->resp_type & MMC_RSP_BUSY)
99 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
100 else if (cmd->resp_type & MMC_RSP_PRESENT)
101 xfertyp |= XFERTYP_RSPTYP_48;
104 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
105 xfertyp |= XFERTYP_CMDTYP_ABORT;
107 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
110 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
112 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
115 esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
117 struct fsl_esdhc_cfg *cfg = mmc->priv;
118 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
126 if (data->flags & MMC_DATA_READ) {
127 blocks = data->blocks;
130 timeout = PIO_TIMEOUT;
131 size = data->blocksize;
132 irqstat = esdhc_read32(®s->irqstat);
133 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)
136 printf("\nData Read Failed in PIO Mode.");
139 while (size && (!(irqstat & IRQSTAT_TC))) {
140 udelay(100); /* Wait before last byte transfer complete */
141 irqstat = esdhc_read32(®s->irqstat);
142 databuf = in_le32(®s->datport);
143 *((uint *)buffer) = databuf;
150 blocks = data->blocks;
151 buffer = (char *)data->src;
153 timeout = PIO_TIMEOUT;
154 size = data->blocksize;
155 irqstat = esdhc_read32(®s->irqstat);
156 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)
159 printf("\nData Write Failed in PIO Mode.");
162 while (size && (!(irqstat & IRQSTAT_TC))) {
163 udelay(100); /* Wait before last byte transfer complete */
164 databuf = *((uint *)buffer);
167 irqstat = esdhc_read32(®s->irqstat);
168 out_le32(®s->datport, databuf);
176 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
179 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
180 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
181 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
184 wml_value = data->blocksize/4;
186 if (data->flags & MMC_DATA_READ) {
187 if (wml_value > WML_RD_WML_MAX)
188 wml_value = WML_RD_WML_MAX_VAL;
190 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
191 esdhc_write32(®s->dsaddr, (u32)data->dest);
193 if (wml_value > WML_WR_WML_MAX)
194 wml_value = WML_WR_WML_MAX_VAL;
195 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
196 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
200 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
202 esdhc_write32(®s->dsaddr, (u32)data->src);
204 #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
205 if (!(data->flags & MMC_DATA_READ)) {
206 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
207 printf("\nThe SD card is locked. "
208 "Can not write to a locked card.\n\n");
211 esdhc_write32(®s->dsaddr, (u32)data->src);
213 esdhc_write32(®s->dsaddr, (u32)data->dest);
214 #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
216 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
218 /* Calculate the timeout period for data transactions */
220 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
221 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
222 * So, Number of SD Clock cycles for 0.25sec should be minimum
223 * (SD Clock/sec * 0.25 sec) SD Clock cycles
224 * = (mmc->tran_speed * 1/4) SD Clock cycles
226 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
227 * Taking log2 both the sides
228 * => timeout + 13 >= log2(mmc->tran_speed/4)
229 * Rounding up to next power of 2
230 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
231 * => timeout + 13 = fls(mmc->tran_speed/4)
233 timeout = fls(mmc->tran_speed/4);
242 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
243 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
247 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
254 * Sends a command out on the bus. Takes the mmc pointer,
255 * a command pointer, and an optional data pointer.
258 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
262 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
263 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
265 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
266 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
270 esdhc_write32(®s->irqstat, -1);
274 /* Wait for the bus to be idle */
275 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
276 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
279 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
282 /* Wait at least 8 SD clock cycles before the next command */
284 * Note: This is way more than 8 cycles, but 1ms seems to
285 * resolve timing issues with some cards
289 /* Set up for a data transfer if we have one */
293 err = esdhc_setup_data(mmc, data);
298 /* Figure out the transfer arguments */
299 xfertyp = esdhc_xfertyp(cmd, data);
301 /* Send the command */
302 esdhc_write32(®s->cmdarg, cmd->cmdarg);
303 #if defined(CONFIG_FSL_USDHC)
304 esdhc_write32(®s->mixctrl,
305 (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
306 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
308 esdhc_write32(®s->xfertyp, xfertyp);
312 esdhc_write32(®s->irqsigen, 0);
314 /* Wait for the command to complete */
315 while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
318 irqstat = esdhc_read32(®s->irqstat);
319 esdhc_write32(®s->irqstat, irqstat);
321 /* Reset CMD and DATA portions on error */
322 if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
323 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
325 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC)
329 esdhc_write32(®s->sysctl,
330 esdhc_read32(®s->sysctl) |
332 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD))
337 if (irqstat & CMD_ERR)
340 if (irqstat & IRQSTAT_CTOE)
343 /* Workaround for ESDHC errata ENGcm03648 */
344 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
347 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
348 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
355 printf("Timeout waiting for DAT0 to go high!\n");
360 /* Copy the response to the response buffer */
361 if (cmd->resp_type & MMC_RSP_136) {
362 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
364 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
365 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
366 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
367 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
368 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
369 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
370 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
371 cmd->response[3] = (cmdrsp0 << 8);
373 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
375 /* Wait until all of the blocks are transferred */
377 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
378 esdhc_pio_read_write(mmc, data);
381 irqstat = esdhc_read32(®s->irqstat);
383 if (irqstat & IRQSTAT_DTOE)
386 if (irqstat & DATA_ERR)
388 } while (!(irqstat & IRQSTAT_TC) &&
389 (esdhc_read32(®s->prsstat) & PRSSTAT_DLA));
393 esdhc_write32(®s->irqstat, -1);
398 void set_sysctl(struct mmc *mmc, uint clock)
400 int sdhc_clk = gd->sdhc_clk;
402 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
403 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
406 if (clock < mmc->f_min)
409 if (sdhc_clk / 16 > clock) {
410 for (pre_div = 2; pre_div < 256; pre_div *= 2)
411 if ((sdhc_clk / pre_div) <= (clock * 16))
416 for (div = 1; div <= 16; div++)
417 if ((sdhc_clk / (div * pre_div)) <= clock)
423 clk = (pre_div << 8) | (div << 4);
425 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
427 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
431 clk = SYSCTL_PEREN | SYSCTL_CKEN;
433 esdhc_setbits32(®s->sysctl, clk);
436 static void esdhc_set_ios(struct mmc *mmc)
438 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
439 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
441 /* Set the clock speed */
442 set_sysctl(mmc, mmc->clock);
444 /* Set the bus width */
445 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
447 if (mmc->bus_width == 4)
448 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
449 else if (mmc->bus_width == 8)
450 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
454 static int esdhc_init(struct mmc *mmc)
456 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
457 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
460 /* Reset the entire host controller */
461 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
463 /* Wait until the controller is available */
464 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
467 /* Enable cache snooping */
468 if (cfg && !cfg->no_snoop)
469 esdhc_write32(®s->scr, 0x00000040);
471 esdhc_write32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
473 /* Set the initial clock speed */
474 mmc_set_clock(mmc, 400000);
476 /* Disable the BRR and BWR bits in IRQSTAT */
477 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
479 /* Put the PROCTL reg back to the default */
480 esdhc_write32(®s->proctl, PROCTL_INIT);
482 /* Set timout to the maximum value */
483 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
488 static int esdhc_getcd(struct mmc *mmc)
490 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
491 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
494 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
500 static void esdhc_reset(struct fsl_esdhc *regs)
502 unsigned long timeout = 100; /* wait max 100 ms */
504 /* reset the controller */
505 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
507 /* hardware clears the bit when it is done */
508 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
511 printf("MMC/SD: Reset never completed.\n");
514 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
516 struct fsl_esdhc *regs;
518 u32 caps, voltage_caps;
523 mmc = malloc(sizeof(struct mmc));
525 sprintf(mmc->name, "FSL_SDHC");
526 regs = (struct fsl_esdhc *)cfg->esdhc_base;
528 /* First reset the eSDHC controller */
532 mmc->send_cmd = esdhc_send_cmd;
533 mmc->set_ios = esdhc_set_ios;
534 mmc->init = esdhc_init;
535 mmc->getcd = esdhc_getcd;
538 caps = regs->hostcapblt;
540 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
541 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
542 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
544 if (caps & ESDHC_HOSTCAPBLT_VS18)
545 voltage_caps |= MMC_VDD_165_195;
546 if (caps & ESDHC_HOSTCAPBLT_VS30)
547 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
548 if (caps & ESDHC_HOSTCAPBLT_VS33)
549 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
551 #ifdef CONFIG_SYS_SD_VOLTAGE
552 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
554 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
556 if ((mmc->voltages & voltage_caps) == 0) {
557 printf("voltage not supported by controller\n");
561 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
563 if (caps & ESDHC_HOSTCAPBLT_HSS)
564 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
567 mmc->f_max = MIN(gd->sdhc_clk, 52000000);
575 int fsl_esdhc_mmc_init(bd_t *bis)
577 struct fsl_esdhc_cfg *cfg;
579 cfg = malloc(sizeof(struct fsl_esdhc_cfg));
580 memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
581 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
582 return fsl_esdhc_initialize(bis, cfg);
585 #ifdef CONFIG_OF_LIBFDT
586 void fdt_fixup_esdhc(void *blob, bd_t *bd)
588 const char *compat = "fsl,esdhc";
590 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
591 if (!hwconfig("esdhc")) {
592 do_fixup_by_compat(blob, compat, "status", "disabled",
598 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
601 do_fixup_by_compat(blob, compat, "status", "okay",