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 static 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 = cfg->esdhc_base;
124 int wml = esdhc_read32(®s->wml);
126 if (data->flags & MMC_DATA_READ) {
127 wml &= WML_RD_WML_MASK;
128 blocks = data->blocks;
131 timeout = PIO_TIMEOUT;
132 size = data->blocksize;
134 !(esdhc_read32(®s->irqstat) & IRQSTAT_TC)) {
138 while (!((prsstat = esdhc_read32(®s->prsstat)) &
139 PRSSTAT_BREN) && --timeout)
141 if (!(prsstat & PRSSTAT_BREN)) {
142 printf("%s: Data Read Failed in PIO Mode\n",
146 for (i = 0; i < wml && size; i++) {
147 databuf = in_le32(®s->datport);
148 memcpy(buffer, &databuf, sizeof(databuf));
156 wml = (wml & WML_WR_WML_MASK) >> 16;
157 blocks = data->blocks;
158 buffer = (char *)data->src; /* cast away 'const' */
160 timeout = PIO_TIMEOUT;
161 size = data->blocksize;
163 !(esdhc_read32(®s->irqstat) & IRQSTAT_TC)) {
167 while (!((prsstat = esdhc_read32(®s->prsstat)) &
168 PRSSTAT_BWEN) && --timeout)
170 if (!(prsstat & PRSSTAT_BWEN)) {
171 printf("%s: Data Write Failed in PIO Mode\n",
175 for (i = 0; i < wml && size; i++) {
176 memcpy(&databuf, buffer, sizeof(databuf));
177 out_le32(®s->datport, databuf);
188 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
191 struct fsl_esdhc_cfg *cfg = mmc->priv;
192 struct fsl_esdhc *regs = cfg->esdhc_base;
193 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
196 wml_value = data->blocksize / 4;
198 if (data->flags & MMC_DATA_READ) {
199 if (wml_value > WML_RD_WML_MAX)
200 wml_value = WML_RD_WML_MAX_VAL;
202 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
203 esdhc_write32(®s->dsaddr, (u32)data->dest);
205 if (wml_value > WML_WR_WML_MAX)
206 wml_value = WML_WR_WML_MAX_VAL;
207 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
208 printf("The SD card is locked. Can not write to a locked card.\n");
212 flush_dcache_range((unsigned long)data->src,
213 (unsigned long)data->src + data->blocks * data->blocksize);
214 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
216 esdhc_write32(®s->dsaddr, (u32)data->src);
218 #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
219 if (!(data->flags & MMC_DATA_READ)) {
220 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
221 printf("The SD card is locked. Can not write to a locked card.\n");
224 esdhc_write32(®s->dsaddr, (u32)data->src);
226 esdhc_write32(®s->dsaddr, (u32)data->dest);
228 #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
230 esdhc_write32(®s->blkattr, (data->blocks << 16) | data->blocksize);
232 /* Calculate the timeout period for data transactions */
234 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
235 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
236 * So, Number of SD Clock cycles for 0.25sec should be minimum
237 * (SD Clock/sec * 0.25 sec) SD Clock cycles
238 * = (mmc->tran_speed * 1/4) SD Clock cycles
240 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
241 * Taking log2 both the sides
242 * => timeout + 13 >= log2(mmc->tran_speed/4)
243 * Rounding up to next power of 2
244 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
245 * => timeout + 13 = fls(mmc->tran_speed/4)
247 timeout = fls(mmc->tran_speed / 4);
252 else if (timeout < 0)
255 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
256 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
259 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
264 static void check_and_invalidate_dcache_range(struct mmc_cmd *cmd,
265 struct mmc_data *data)
267 unsigned start = (unsigned)data->dest;
268 unsigned size = roundup(ARCH_DMA_MINALIGN,
269 data->blocks * data->blocksize);
270 unsigned end = start + size;
272 invalidate_dcache_range(start, end);
276 * Sends a command out on the bus. Takes the mmc pointer,
277 * a command pointer, and an optional data pointer.
280 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
284 struct fsl_esdhc_cfg *cfg = mmc->priv;
285 volatile struct fsl_esdhc *regs = cfg->esdhc_base;
288 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
289 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
292 esdhc_write32(®s->irqstat, -1);
296 start = get_timer_masked();
297 /* Wait for the bus to be idle */
298 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
299 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) {
300 if (get_timer(start) > CONFIG_SYS_HZ) {
301 printf("%s: Timeout waiting for bus idle\n", __func__);
306 start = get_timer_masked();
307 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) {
308 if (get_timer(start) > CONFIG_SYS_HZ)
312 /* Wait at least 8 SD clock cycles before the next command */
314 * Note: This is way more than 8 cycles, but 1ms seems to
315 * resolve timing issues with some cards
319 /* Set up for a data transfer if we have one */
323 err = esdhc_setup_data(mmc, data);
328 /* Figure out the transfer arguments */
329 xfertyp = esdhc_xfertyp(cmd, data);
331 /* Send the command */
332 esdhc_write32(®s->cmdarg, cmd->cmdarg);
333 #if defined(CONFIG_FSL_USDHC)
334 esdhc_write32(®s->mixctrl,
335 (esdhc_read32(®s->mixctrl) & ~0x7f) | (xfertyp & 0x7F));
336 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
338 esdhc_write32(®s->xfertyp, xfertyp);
342 esdhc_write32(®s->irqsigen, 0);
344 start = get_timer_masked();
345 /* Wait for the command to complete */
346 while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) {
347 if (get_timer(start) > CONFIG_SYS_HZ) {
348 printf("%s: Timeout waiting for cmd completion\n", __func__);
353 if (data && (data->flags & MMC_DATA_READ))
354 check_and_invalidate_dcache_range(cmd, data);
356 irqstat = esdhc_read32(®s->irqstat);
357 esdhc_write32(®s->irqstat, irqstat);
359 /* Reset CMD and DATA portions on error */
360 if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
361 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
363 start = get_timer_masked();
364 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) {
365 if (get_timer(start) > CONFIG_SYS_HZ)
370 esdhc_write32(®s->sysctl,
371 esdhc_read32(®s->sysctl) |
373 start = get_timer_masked();
374 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) {
375 if (get_timer(start) > CONFIG_SYS_HZ)
381 if (irqstat & CMD_ERR)
384 if (irqstat & IRQSTAT_CTOE)
387 /* Workaround for ESDHC errata ENGcm03648 */
388 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
391 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
392 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
399 printf("Timeout waiting for DAT0 to go high!\n");
404 /* Copy the response to the response buffer */
405 if (cmd->resp_type & MMC_RSP_136) {
406 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
408 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
409 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
410 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
411 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
412 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
413 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
414 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
415 cmd->response[3] = (cmdrsp0 << 8);
417 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
419 /* Wait until all of the blocks are transferred */
421 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
422 esdhc_pio_read_write(mmc, data);
424 unsigned long start = get_timer_masked();
425 unsigned long data_timeout = data->blocks *
426 data->blocksize * 100 / mmc->bus_width /
427 (mmc->tran_speed / CONFIG_SYS_HZ) + CONFIG_SYS_HZ;
430 irqstat = esdhc_read32(®s->irqstat);
432 if (irqstat & IRQSTAT_DTOE) {
433 printf("MMC/SD data %s timeout\n",
434 data->flags & MMC_DATA_READ ?
439 if (irqstat & DATA_ERR) {
440 printf("MMC/SD data error\n");
444 if (get_timer(start) > data_timeout) {
445 printf("MMC/SD timeout waiting for %s xfer completion\n",
446 data->flags & MMC_DATA_READ ?
450 } while (!(irqstat & IRQSTAT_TC) &&
451 (esdhc_read32(®s->prsstat) & PRSSTAT_DLA));
453 check_and_invalidate_dcache_range(cmd, data);
457 esdhc_write32(®s->irqstat, irqstat);
462 static void set_sysctl(struct mmc *mmc, uint clock)
465 struct fsl_esdhc_cfg *cfg = mmc->priv;
466 volatile struct fsl_esdhc *regs = cfg->esdhc_base;
467 int sdhc_clk = cfg->sdhc_clk;
470 if (clock < mmc->f_min)
473 if (sdhc_clk / 16 > clock) {
474 for (pre_div = 2; pre_div < 256; pre_div *= 2)
475 if ((sdhc_clk / pre_div) <= (clock * 16))
480 for (div = 1; div <= 16; div++)
481 if ((sdhc_clk / (div * pre_div)) <= clock)
487 clk = (pre_div << 8) | (div << 4);
489 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
491 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
495 clk = SYSCTL_PEREN | SYSCTL_CKEN;
497 esdhc_setbits32(®s->sysctl, clk);
500 static void esdhc_set_ios(struct mmc *mmc)
502 struct fsl_esdhc_cfg *cfg = mmc->priv;
503 struct fsl_esdhc *regs = cfg->esdhc_base;
505 /* Set the clock speed */
506 set_sysctl(mmc, mmc->clock);
508 /* Set the bus width */
509 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
511 if (mmc->bus_width == 4)
512 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
513 else if (mmc->bus_width == 8)
514 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
518 static int esdhc_init(struct mmc *mmc)
520 struct fsl_esdhc_cfg *cfg = mmc->priv;
521 struct fsl_esdhc *regs = cfg->esdhc_base;
524 /* Reset the entire host controller */
525 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
527 /* Wait until the controller is available */
528 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
532 /* Enable cache snooping */
533 esdhc_write32(®s->scr, 0x00000040);
536 esdhc_write32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
538 /* Set the initial clock speed */
539 mmc_set_clock(mmc, 400000);
541 /* Disable the BRR and BWR bits in IRQSTAT */
542 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
544 /* Put the PROCTL reg back to the default */
545 esdhc_write32(®s->proctl, PROCTL_INIT);
547 /* Set timout to the maximum value */
548 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
553 static int esdhc_getcd(struct mmc *mmc)
555 struct fsl_esdhc_cfg *cfg = mmc->priv;
556 struct fsl_esdhc *regs = cfg->esdhc_base;
559 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
565 static void esdhc_reset(struct fsl_esdhc *regs)
567 unsigned long timeout = 100; /* wait max 100 ms */
569 /* reset the controller */
570 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
572 /* hardware clears the bit when it is done */
573 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
576 printf("MMC/SD: Reset never completed.\n");
579 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
581 struct fsl_esdhc *regs;
583 u32 caps, voltage_caps;
588 mmc = kzalloc(sizeof(struct mmc), GFP_KERNEL);
592 sprintf(mmc->name, "FSL_SDHC");
593 regs = cfg->esdhc_base;
595 /* First reset the eSDHC controller */
598 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
599 | SYSCTL_IPGEN | SYSCTL_CKEN);
602 mmc->send_cmd = esdhc_send_cmd;
603 mmc->set_ios = esdhc_set_ios;
604 mmc->init = esdhc_init;
605 mmc->getcd = esdhc_getcd;
608 caps = regs->hostcapblt;
610 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
611 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
612 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
614 if (caps & ESDHC_HOSTCAPBLT_VS18)
615 voltage_caps |= MMC_VDD_165_195;
616 if (caps & ESDHC_HOSTCAPBLT_VS30)
617 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
618 if (caps & ESDHC_HOSTCAPBLT_VS33)
619 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
621 #ifdef CONFIG_SYS_SD_VOLTAGE
622 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
624 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
626 if ((mmc->voltages & voltage_caps) == 0) {
627 printf("voltage not supported by controller\n");
631 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
633 if (caps & ESDHC_HOSTCAPBLT_HSS)
634 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
637 mmc->f_max = MIN(cfg->sdhc_clk, 52000000);
645 int fsl_esdhc_mmc_init(bd_t *bis)
647 struct fsl_esdhc_cfg *cfg;
649 cfg = kzalloc(sizeof(struct fsl_esdhc_cfg), GFP_KERNEL);
652 cfg->esdhc_base = (void __iomem *)CONFIG_SYS_FSL_ESDHC_ADDR;
653 cfg->sdhc_clk = gd->arch.sdhc_clk;
654 return fsl_esdhc_initialize(bis, cfg);
657 #ifdef CONFIG_OF_LIBFDT
658 void fdt_fixup_esdhc(void *blob, bd_t *bd)
660 const char *compat = "fsl,esdhc";
662 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
663 if (!hwconfig("esdhc")) {
664 do_fixup_by_compat(blob, compat, "status", "disabled",
670 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
671 gd->arch.sdhc_clk, 1);
673 do_fixup_by_compat(blob, compat, "status", "okay",