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;
69 /* Return the XFERTYP flags for a given command and data packet */
70 uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
75 xfertyp |= XFERTYP_DPSEL;
76 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
77 xfertyp |= XFERTYP_DMAEN;
79 if (data->blocks > 1) {
80 xfertyp |= XFERTYP_MSBSEL;
81 xfertyp |= XFERTYP_BCEN;
82 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
83 xfertyp |= XFERTYP_AC12EN;
87 if (data->flags & MMC_DATA_READ)
88 xfertyp |= XFERTYP_DTDSEL;
91 if (cmd->resp_type & MMC_RSP_CRC)
92 xfertyp |= XFERTYP_CCCEN;
93 if (cmd->resp_type & MMC_RSP_OPCODE)
94 xfertyp |= XFERTYP_CICEN;
95 if (cmd->resp_type & MMC_RSP_136)
96 xfertyp |= XFERTYP_RSPTYP_136;
97 else if (cmd->resp_type & MMC_RSP_BUSY)
98 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
99 else if (cmd->resp_type & MMC_RSP_PRESENT)
100 xfertyp |= XFERTYP_RSPTYP_48;
102 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
105 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
107 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
110 esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
112 struct fsl_esdhc *regs = mmc->priv;
120 if (data->flags & MMC_DATA_READ) {
121 blocks = data->blocks;
124 timeout = PIO_TIMEOUT;
125 size = data->blocksize;
126 irqstat = esdhc_read32(®s->irqstat);
127 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)
130 printf("\nData Read Failed in PIO Mode.");
133 while (size && (!(irqstat & IRQSTAT_TC))) {
134 udelay(100); /* Wait before last byte transfer complete */
135 irqstat = esdhc_read32(®s->irqstat);
136 databuf = in_le32(®s->datport);
137 *((uint *)buffer) = databuf;
144 blocks = data->blocks;
145 buffer = (char *)data->src;
147 timeout = PIO_TIMEOUT;
148 size = data->blocksize;
149 irqstat = esdhc_read32(®s->irqstat);
150 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)
153 printf("\nData Write Failed in PIO Mode.");
156 while (size && (!(irqstat & IRQSTAT_TC))) {
157 udelay(100); /* Wait before last byte transfer complete */
158 databuf = *((uint *)buffer);
161 irqstat = esdhc_read32(®s->irqstat);
162 out_le32(®s->datport, databuf);
170 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
173 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
174 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
175 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
178 wml_value = data->blocksize/4;
180 if (data->flags & MMC_DATA_READ) {
181 if (wml_value > WML_RD_WML_MAX)
182 wml_value = WML_RD_WML_MAX_VAL;
184 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
185 esdhc_write32(®s->dsaddr, (u32)data->dest);
187 if (wml_value > WML_WR_WML_MAX)
188 wml_value = WML_WR_WML_MAX_VAL;
189 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
190 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
194 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
196 esdhc_write32(®s->dsaddr, (u32)data->src);
198 #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
199 if (!(data->flags & MMC_DATA_READ)) {
200 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
201 printf("\nThe SD card is locked. "
202 "Can not write to a locked card.\n\n");
205 esdhc_write32(®s->dsaddr, (u32)data->src);
207 esdhc_write32(®s->dsaddr, (u32)data->dest);
208 #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
210 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
212 /* Calculate the timeout period for data transactions */
214 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
215 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
216 * So, Number of SD Clock cycles for 0.25sec should be minimum
217 * (SD Clock/sec * 0.25 sec) SD Clock cycles
218 * = (mmc->tran_speed * 1/4) SD Clock cycles
220 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
221 * Taking log2 both the sides
222 * => timeout + 13 >= log2(mmc->tran_speed/4)
223 * Rounding up to next power of 2
224 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
225 * => timeout + 13 = fls(mmc->tran_speed/4)
227 timeout = fls(mmc->tran_speed/4);
236 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
237 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
241 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
248 * Sends a command out on the bus. Takes the mmc pointer,
249 * a command pointer, and an optional data pointer.
252 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
256 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
257 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
259 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
260 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
264 esdhc_write32(®s->irqstat, -1);
268 /* Wait for the bus to be idle */
269 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
270 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
273 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
276 /* Wait at least 8 SD clock cycles before the next command */
278 * Note: This is way more than 8 cycles, but 1ms seems to
279 * resolve timing issues with some cards
283 /* Set up for a data transfer if we have one */
287 err = esdhc_setup_data(mmc, data);
292 /* Figure out the transfer arguments */
293 xfertyp = esdhc_xfertyp(cmd, data);
295 /* Send the command */
296 esdhc_write32(®s->cmdarg, cmd->cmdarg);
297 esdhc_write32(®s->xfertyp, xfertyp);
299 /* Wait for the command to complete */
300 while (!(esdhc_read32(®s->irqstat) & IRQSTAT_CC))
303 irqstat = esdhc_read32(®s->irqstat);
304 esdhc_write32(®s->irqstat, irqstat);
306 if (irqstat & CMD_ERR)
309 if (irqstat & IRQSTAT_CTOE)
312 /* Copy the response to the response buffer */
313 if (cmd->resp_type & MMC_RSP_136) {
314 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
316 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
317 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
318 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
319 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
320 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
321 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
322 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
323 cmd->response[3] = (cmdrsp0 << 8);
325 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
327 /* Wait until all of the blocks are transferred */
329 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
330 esdhc_pio_read_write(mmc, data);
333 irqstat = esdhc_read32(®s->irqstat);
335 if (irqstat & DATA_ERR)
338 if (irqstat & IRQSTAT_DTOE)
340 } while (!(irqstat & IRQSTAT_TC) &&
341 (esdhc_read32(®s->prsstat) & PRSSTAT_DLA));
345 esdhc_write32(®s->irqstat, -1);
350 void set_sysctl(struct mmc *mmc, uint clock)
352 int sdhc_clk = gd->sdhc_clk;
354 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
355 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
358 if (clock < mmc->f_min)
361 if (sdhc_clk / 16 > clock) {
362 for (pre_div = 2; pre_div < 256; pre_div *= 2)
363 if ((sdhc_clk / pre_div) <= (clock * 16))
368 for (div = 1; div <= 16; div++)
369 if ((sdhc_clk / (div * pre_div)) <= clock)
375 clk = (pre_div << 8) | (div << 4);
377 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
379 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
383 clk = SYSCTL_PEREN | SYSCTL_CKEN;
385 esdhc_setbits32(®s->sysctl, clk);
388 static void esdhc_set_ios(struct mmc *mmc)
390 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
391 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
393 /* Set the clock speed */
394 set_sysctl(mmc, mmc->clock);
396 /* Set the bus width */
397 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
399 if (mmc->bus_width == 4)
400 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
401 else if (mmc->bus_width == 8)
402 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
406 static int esdhc_init(struct mmc *mmc)
408 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
409 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
414 /* Reset the entire host controller */
415 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
417 /* Wait until the controller is available */
418 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
421 /* Enable cache snooping */
422 if (cfg && !cfg->no_snoop)
423 esdhc_write32(®s->scr, 0x00000040);
425 esdhc_write32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
427 /* Set the initial clock speed */
428 mmc_set_clock(mmc, 400000);
430 /* Disable the BRR and BWR bits in IRQSTAT */
431 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
433 /* Put the PROCTL reg back to the default */
434 esdhc_write32(®s->proctl, PROCTL_INIT);
436 /* Set timout to the maximum value */
437 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
439 /* Check if there is a callback for detecting the card */
440 if (board_mmc_getcd(&card_absent, mmc)) {
442 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) &&
456 static void esdhc_reset(struct fsl_esdhc *regs)
458 unsigned long timeout = 100; /* wait max 100 ms */
460 /* reset the controller */
461 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
463 /* hardware clears the bit when it is done */
464 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
467 printf("MMC/SD: Reset never completed.\n");
470 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
472 struct fsl_esdhc *regs;
474 u32 caps, voltage_caps;
479 mmc = malloc(sizeof(struct mmc));
481 sprintf(mmc->name, "FSL_ESDHC");
482 regs = (struct fsl_esdhc *)cfg->esdhc_base;
484 /* First reset the eSDHC controller */
488 mmc->send_cmd = esdhc_send_cmd;
489 mmc->set_ios = esdhc_set_ios;
490 mmc->init = esdhc_init;
493 caps = regs->hostcapblt;
495 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
496 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
497 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
499 if (caps & ESDHC_HOSTCAPBLT_VS18)
500 voltage_caps |= MMC_VDD_165_195;
501 if (caps & ESDHC_HOSTCAPBLT_VS30)
502 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
503 if (caps & ESDHC_HOSTCAPBLT_VS33)
504 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
506 #ifdef CONFIG_SYS_SD_VOLTAGE
507 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
509 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
511 if ((mmc->voltages & voltage_caps) == 0) {
512 printf("voltage not supported by controller\n");
516 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
518 if (caps & ESDHC_HOSTCAPBLT_HSS)
519 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
522 mmc->f_max = MIN(gd->sdhc_clk, 52000000);
529 int fsl_esdhc_mmc_init(bd_t *bis)
531 struct fsl_esdhc_cfg *cfg;
533 cfg = malloc(sizeof(struct fsl_esdhc_cfg));
534 memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
535 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
536 return fsl_esdhc_initialize(bis, cfg);
539 #ifdef CONFIG_OF_LIBFDT
540 void fdt_fixup_esdhc(void *blob, bd_t *bd)
542 const char *compat = "fsl,esdhc";
544 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
545 if (!hwconfig("esdhc")) {
546 do_fixup_by_compat(blob, compat, "status", "disabled",
552 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
555 do_fixup_by_compat(blob, compat, "status", "okay",