2 * Copyright 2007, 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>
41 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 | XFERTYP_DMAEN;
78 if (data->blocks > 1) {
79 xfertyp |= XFERTYP_MSBSEL;
80 xfertyp |= XFERTYP_BCEN;
83 if (data->flags & MMC_DATA_READ)
84 xfertyp |= XFERTYP_DTDSEL;
87 if (cmd->resp_type & MMC_RSP_CRC)
88 xfertyp |= XFERTYP_CCCEN;
89 if (cmd->resp_type & MMC_RSP_OPCODE)
90 xfertyp |= XFERTYP_CICEN;
91 if (cmd->resp_type & MMC_RSP_136)
92 xfertyp |= XFERTYP_RSPTYP_136;
93 else if (cmd->resp_type & MMC_RSP_BUSY)
94 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
95 else if (cmd->resp_type & MMC_RSP_PRESENT)
96 xfertyp |= XFERTYP_RSPTYP_48;
98 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
101 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
105 struct fsl_esdhc *regs = mmc->priv;
107 wml_value = data->blocksize/4;
109 if (data->flags & MMC_DATA_READ) {
110 if (wml_value > 0x10)
113 wml_value = 0x100000 | wml_value;
115 out_be32(®s->dsaddr, (u32)data->dest);
117 if (wml_value > 0x80)
119 if ((in_be32(®s->prsstat) & PRSSTAT_WPSPL) == 0) {
120 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
123 wml_value = wml_value << 16 | 0x10;
124 out_be32(®s->dsaddr, (u32)data->src);
127 out_be32(®s->wml, wml_value);
129 out_be32(®s->blkattr, data->blocks << 16 | data->blocksize);
131 /* Calculate the timeout period for data transactions */
132 timeout = __ilog2(mmc->tran_speed/10);
141 clrsetbits_be32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
148 * Sends a command out on the bus. Takes the mmc pointer,
149 * a command pointer, and an optional data pointer.
152 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
156 volatile struct fsl_esdhc *regs = mmc->priv;
158 out_be32(®s->irqstat, -1);
162 /* Wait for the bus to be idle */
163 while ((in_be32(®s->prsstat) & PRSSTAT_CICHB) ||
164 (in_be32(®s->prsstat) & PRSSTAT_CIDHB));
166 while (in_be32(®s->prsstat) & PRSSTAT_DLA);
168 /* Wait at least 8 SD clock cycles before the next command */
170 * Note: This is way more than 8 cycles, but 1ms seems to
171 * resolve timing issues with some cards
175 /* Set up for a data transfer if we have one */
179 err = esdhc_setup_data(mmc, data);
184 /* Figure out the transfer arguments */
185 xfertyp = esdhc_xfertyp(cmd, data);
187 /* Send the command */
188 out_be32(®s->cmdarg, cmd->cmdarg);
189 out_be32(®s->xfertyp, xfertyp);
191 /* Wait for the command to complete */
192 while (!(in_be32(®s->irqstat) & IRQSTAT_CC));
194 irqstat = in_be32(®s->irqstat);
195 out_be32(®s->irqstat, irqstat);
197 if (irqstat & CMD_ERR)
200 if (irqstat & IRQSTAT_CTOE)
203 /* Copy the response to the response buffer */
204 if (cmd->resp_type & MMC_RSP_136) {
205 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
207 cmdrsp3 = in_be32(®s->cmdrsp3);
208 cmdrsp2 = in_be32(®s->cmdrsp2);
209 cmdrsp1 = in_be32(®s->cmdrsp1);
210 cmdrsp0 = in_be32(®s->cmdrsp0);
211 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
212 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
213 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
214 cmd->response[3] = (cmdrsp0 << 8);
216 cmd->response[0] = in_be32(®s->cmdrsp0);
218 /* Wait until all of the blocks are transferred */
221 irqstat = in_be32(®s->irqstat);
223 if (irqstat & DATA_ERR)
226 if (irqstat & IRQSTAT_DTOE)
228 } while (!(irqstat & IRQSTAT_TC) &&
229 (in_be32(®s->prsstat) & PRSSTAT_DLA));
232 out_be32(®s->irqstat, -1);
237 void set_sysctl(struct mmc *mmc, uint clock)
239 int sdhc_clk = gd->sdhc_clk;
241 volatile struct fsl_esdhc *regs = mmc->priv;
244 if (sdhc_clk / 16 > clock) {
245 for (pre_div = 2; pre_div < 256; pre_div *= 2)
246 if ((sdhc_clk / pre_div) <= (clock * 16))
251 for (div = 1; div <= 16; div++)
252 if ((sdhc_clk / (div * pre_div)) <= clock)
258 clk = (pre_div << 8) | (div << 4);
260 clrsetbits_be32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
264 setbits_be32(®s->sysctl, SYSCTL_PEREN);
267 static void esdhc_set_ios(struct mmc *mmc)
269 struct fsl_esdhc *regs = mmc->priv;
271 /* Set the clock speed */
272 set_sysctl(mmc, mmc->clock);
274 /* Set the bus width */
275 clrbits_be32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
277 if (mmc->bus_width == 4)
278 setbits_be32(®s->proctl, PROCTL_DTW_4);
279 else if (mmc->bus_width == 8)
280 setbits_be32(®s->proctl, PROCTL_DTW_8);
283 static int esdhc_init(struct mmc *mmc)
285 struct fsl_esdhc *regs = mmc->priv;
288 /* Enable cache snooping */
289 out_be32(®s->scr, 0x00000040);
291 out_be32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
293 /* Set the initial clock speed */
294 set_sysctl(mmc, 400000);
296 /* Disable the BRR and BWR bits in IRQSTAT */
297 clrbits_be32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
299 /* Put the PROCTL reg back to the default */
300 out_be32(®s->proctl, PROCTL_INIT);
302 while (!(in_be32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
311 static int esdhc_initialize(bd_t *bis)
313 struct fsl_esdhc *regs = (struct fsl_esdhc *)CONFIG_SYS_FSL_ESDHC_ADDR;
317 mmc = malloc(sizeof(struct mmc));
319 sprintf(mmc->name, "FSL_ESDHC");
321 mmc->send_cmd = esdhc_send_cmd;
322 mmc->set_ios = esdhc_set_ios;
323 mmc->init = esdhc_init;
325 caps = regs->hostcapblt;
327 if (caps & ESDHC_HOSTCAPBLT_VS18)
328 mmc->voltages |= MMC_VDD_165_195;
329 if (caps & ESDHC_HOSTCAPBLT_VS30)
330 mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
331 if (caps & ESDHC_HOSTCAPBLT_VS33)
332 mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
334 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
336 if (caps & ESDHC_HOSTCAPBLT_HSS)
337 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
340 mmc->f_max = MIN(gd->sdhc_clk, 50000000);
347 int fsl_esdhc_mmc_init(bd_t *bis)
349 return esdhc_initialize(bis);
352 void fdt_fixup_esdhc(void *blob, bd_t *bd)
354 const char *compat = "fsl,esdhc";
355 const char *status = "okay";
357 if (!hwconfig("esdhc")) {
362 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
365 do_fixup_by_compat(blob, compat, "status", status,
366 strlen(status) + 1, 1);