2 * Copyright 2007,2010 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 | XFERTYP_DMAEN;
77 if (data->blocks > 1) {
78 xfertyp |= XFERTYP_MSBSEL;
79 xfertyp |= XFERTYP_BCEN;
82 if (data->flags & MMC_DATA_READ)
83 xfertyp |= XFERTYP_DTDSEL;
86 if (cmd->resp_type & MMC_RSP_CRC)
87 xfertyp |= XFERTYP_CCCEN;
88 if (cmd->resp_type & MMC_RSP_OPCODE)
89 xfertyp |= XFERTYP_CICEN;
90 if (cmd->resp_type & MMC_RSP_136)
91 xfertyp |= XFERTYP_RSPTYP_136;
92 else if (cmd->resp_type & MMC_RSP_BUSY)
93 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
94 else if (cmd->resp_type & MMC_RSP_PRESENT)
95 xfertyp |= XFERTYP_RSPTYP_48;
97 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
100 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
104 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
105 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
107 wml_value = data->blocksize/4;
109 if (data->flags & MMC_DATA_READ) {
110 if (wml_value > 0x10)
113 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
114 esdhc_write32(®s->dsaddr, (u32)data->dest);
116 if (wml_value > 0x80)
118 if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
119 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
123 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
125 esdhc_write32(®s->dsaddr, (u32)data->src);
128 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
130 /* Calculate the timeout period for data transactions */
131 timeout = fls(mmc->tran_speed/10) - 1;
140 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
147 * Sends a command out on the bus. Takes the mmc pointer,
148 * a command pointer, and an optional data pointer.
151 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
155 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
156 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
158 esdhc_write32(®s->irqstat, -1);
162 /* Wait for the bus to be idle */
163 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
164 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
167 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
170 /* Wait at least 8 SD clock cycles before the next command */
172 * Note: This is way more than 8 cycles, but 1ms seems to
173 * resolve timing issues with some cards
177 /* Set up for a data transfer if we have one */
181 err = esdhc_setup_data(mmc, data);
186 /* Figure out the transfer arguments */
187 xfertyp = esdhc_xfertyp(cmd, data);
189 /* Send the command */
190 esdhc_write32(®s->cmdarg, cmd->cmdarg);
191 esdhc_write32(®s->xfertyp, xfertyp);
193 /* Wait for the command to complete */
194 while (!(esdhc_read32(®s->irqstat) & IRQSTAT_CC))
197 irqstat = esdhc_read32(®s->irqstat);
198 esdhc_write32(®s->irqstat, irqstat);
200 if (irqstat & CMD_ERR)
203 if (irqstat & IRQSTAT_CTOE)
206 /* Copy the response to the response buffer */
207 if (cmd->resp_type & MMC_RSP_136) {
208 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
210 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
211 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
212 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
213 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
214 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
215 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
216 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
217 cmd->response[3] = (cmdrsp0 << 8);
219 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
221 /* Wait until all of the blocks are transferred */
224 irqstat = esdhc_read32(®s->irqstat);
226 if (irqstat & DATA_ERR)
229 if (irqstat & IRQSTAT_DTOE)
231 } while (!(irqstat & IRQSTAT_TC) &&
232 (esdhc_read32(®s->prsstat) & PRSSTAT_DLA));
235 esdhc_write32(®s->irqstat, -1);
240 void set_sysctl(struct mmc *mmc, uint clock)
242 int sdhc_clk = gd->sdhc_clk;
244 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
245 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
248 if (clock < mmc->f_min)
251 if (sdhc_clk / 16 > clock) {
252 for (pre_div = 2; pre_div < 256; pre_div *= 2)
253 if ((sdhc_clk / pre_div) <= (clock * 16))
258 for (div = 1; div <= 16; div++)
259 if ((sdhc_clk / (div * pre_div)) <= clock)
265 clk = (pre_div << 8) | (div << 4);
267 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
269 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
273 clk = SYSCTL_PEREN | SYSCTL_CKEN;
275 esdhc_setbits32(®s->sysctl, clk);
278 static void esdhc_set_ios(struct mmc *mmc)
280 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
281 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
283 /* Set the clock speed */
284 set_sysctl(mmc, mmc->clock);
286 /* Set the bus width */
287 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
289 if (mmc->bus_width == 4)
290 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
291 else if (mmc->bus_width == 8)
292 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
296 static int esdhc_init(struct mmc *mmc)
298 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
299 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
304 /* Enable cache snooping */
305 if (cfg && !cfg->no_snoop)
306 esdhc_write32(®s->scr, 0x00000040);
308 /* Reset the entire host controller */
309 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
311 /* Wait until the controller is available */
312 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
315 esdhc_write32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
317 /* Set the initial clock speed */
318 set_sysctl(mmc, 400000);
320 /* Disable the BRR and BWR bits in IRQSTAT */
321 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
323 /* Put the PROCTL reg back to the default */
324 esdhc_write32(®s->proctl, PROCTL_INIT);
326 /* Set timout to the maximum value */
327 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
329 /* Check if there is a callback for detecting the card */
330 if (board_mmc_getcd(&card_absent, mmc)) {
332 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) &&
346 static void esdhc_reset(struct fsl_esdhc *regs)
348 unsigned long timeout = 100; /* wait max 100 ms */
350 /* reset the controller */
351 esdhc_write32(®s->sysctl, SYSCTL_RSTA);
353 /* hardware clears the bit when it is done */
354 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout)
357 printf("MMC/SD: Reset never completed.\n");
360 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
362 struct fsl_esdhc *regs;
369 mmc = malloc(sizeof(struct mmc));
371 sprintf(mmc->name, "FSL_ESDHC");
372 regs = (struct fsl_esdhc *)cfg->esdhc_base;
374 /* First reset the eSDHC controller */
378 mmc->send_cmd = esdhc_send_cmd;
379 mmc->set_ios = esdhc_set_ios;
380 mmc->init = esdhc_init;
382 caps = regs->hostcapblt;
384 if (caps & ESDHC_HOSTCAPBLT_VS18)
385 mmc->voltages |= MMC_VDD_165_195;
386 if (caps & ESDHC_HOSTCAPBLT_VS30)
387 mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
388 if (caps & ESDHC_HOSTCAPBLT_VS33)
389 mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
391 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
393 if (caps & ESDHC_HOSTCAPBLT_HSS)
394 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
397 mmc->f_max = MIN(gd->sdhc_clk, 50000000);
404 int fsl_esdhc_mmc_init(bd_t *bis)
406 struct fsl_esdhc_cfg *cfg;
408 cfg = malloc(sizeof(struct fsl_esdhc_cfg));
409 memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
410 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
411 return fsl_esdhc_initialize(bis, cfg);
414 #ifdef CONFIG_OF_LIBFDT
415 void fdt_fixup_esdhc(void *blob, bd_t *bd)
417 const char *compat = "fsl,esdhc";
418 const char *status = "okay";
420 if (!hwconfig("esdhc")) {
425 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
428 do_fixup_by_compat(blob, compat, "status", status,
429 strlen(status) + 1, 1);