blob: e665b5ebd8a7f10d62877c5e97482ceabc07e095 [file] [log] [blame]
Andy Fleming50586ef2008-10-30 16:47:16 -05001/*
2 * Copyright 2007, Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
5 * Based vaguely on the pxa mmc code:
6 * (C) Copyright 2003
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307 USA
26 */
27
28#include <config.h>
29#include <common.h>
30#include <command.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040031#include <hwconfig.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050032#include <mmc.h>
33#include <part.h>
34#include <malloc.h>
35#include <mmc.h>
36#include <fsl_esdhc.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040037#include <fdt_support.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050038#include <asm/io.h>
39
Andy Fleming50586ef2008-10-30 16:47:16 -050040DECLARE_GLOBAL_DATA_PTR;
41
42struct fsl_esdhc {
43 uint dsaddr;
44 uint blkattr;
45 uint cmdarg;
46 uint xfertyp;
47 uint cmdrsp0;
48 uint cmdrsp1;
49 uint cmdrsp2;
50 uint cmdrsp3;
51 uint datport;
52 uint prsstat;
53 uint proctl;
54 uint sysctl;
55 uint irqstat;
56 uint irqstaten;
57 uint irqsigen;
58 uint autoc12err;
59 uint hostcapblt;
60 uint wml;
61 char reserved1[8];
62 uint fevt;
63 char reserved2[168];
64 uint hostver;
65 char reserved3[780];
66 uint scr;
67};
68
69/* Return the XFERTYP flags for a given command and data packet */
70uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
71{
72 uint xfertyp = 0;
73
74 if (data) {
75 xfertyp |= XFERTYP_DPSEL | XFERTYP_DMAEN;
76
77 if (data->blocks > 1) {
78 xfertyp |= XFERTYP_MSBSEL;
79 xfertyp |= XFERTYP_BCEN;
80 }
81
82 if (data->flags & MMC_DATA_READ)
83 xfertyp |= XFERTYP_DTDSEL;
84 }
85
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;
96
97 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
98}
99
100static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
101{
102 uint wml_value;
103 int timeout;
Stefano Babicc67bee12010-02-05 15:11:27 +0100104 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
105 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500106
107 wml_value = data->blocksize/4;
108
109 if (data->flags & MMC_DATA_READ) {
110 if (wml_value > 0x10)
111 wml_value = 0x10;
112
113 wml_value = 0x100000 | wml_value;
114
Stefano Babicc67bee12010-02-05 15:11:27 +0100115 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Andy Fleming50586ef2008-10-30 16:47:16 -0500116 } else {
117 if (wml_value > 0x80)
118 wml_value = 0x80;
Stefano Babicc67bee12010-02-05 15:11:27 +0100119 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500120 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
121 return TIMEOUT;
122 }
123 wml_value = wml_value << 16 | 0x10;
Stefano Babicc67bee12010-02-05 15:11:27 +0100124 esdhc_write32(&regs->dsaddr, (u32)data->src);
Andy Fleming50586ef2008-10-30 16:47:16 -0500125 }
126
Stefano Babicc67bee12010-02-05 15:11:27 +0100127 esdhc_write32(&regs->wml, wml_value);
Andy Fleming50586ef2008-10-30 16:47:16 -0500128
Stefano Babicc67bee12010-02-05 15:11:27 +0100129 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500130
131 /* Calculate the timeout period for data transactions */
Stefano Babicc67bee12010-02-05 15:11:27 +0100132 timeout = fls(mmc->tran_speed/10) - 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500133 timeout -= 13;
134
135 if (timeout > 14)
136 timeout = 14;
137
138 if (timeout < 0)
139 timeout = 0;
140
Stefano Babicc67bee12010-02-05 15:11:27 +0100141 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500142
143 return 0;
144}
145
146
147/*
148 * Sends a command out on the bus. Takes the mmc pointer,
149 * a command pointer, and an optional data pointer.
150 */
151static int
152esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
153{
154 uint xfertyp;
155 uint irqstat;
Stefano Babicc67bee12010-02-05 15:11:27 +0100156 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
157 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500158
Stefano Babicc67bee12010-02-05 15:11:27 +0100159 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500160
161 sync();
162
163 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100164 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
165 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
166 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500167
Stefano Babicc67bee12010-02-05 15:11:27 +0100168 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
169 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500170
171 /* Wait at least 8 SD clock cycles before the next command */
172 /*
173 * Note: This is way more than 8 cycles, but 1ms seems to
174 * resolve timing issues with some cards
175 */
176 udelay(1000);
177
178 /* Set up for a data transfer if we have one */
179 if (data) {
180 int err;
181
182 err = esdhc_setup_data(mmc, data);
183 if(err)
184 return err;
185 }
186
187 /* Figure out the transfer arguments */
188 xfertyp = esdhc_xfertyp(cmd, data);
189
190 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100191 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
192 esdhc_write32(&regs->xfertyp, xfertyp);
Andy Fleming50586ef2008-10-30 16:47:16 -0500193
194 /* Wait for the command to complete */
Stefano Babicc67bee12010-02-05 15:11:27 +0100195 while (!(esdhc_read32(&regs->irqstat) & IRQSTAT_CC))
196 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500197
Stefano Babicc67bee12010-02-05 15:11:27 +0100198 irqstat = esdhc_read32(&regs->irqstat);
199 esdhc_write32(&regs->irqstat, irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500200
201 if (irqstat & CMD_ERR)
202 return COMM_ERR;
203
204 if (irqstat & IRQSTAT_CTOE)
205 return TIMEOUT;
206
207 /* Copy the response to the response buffer */
208 if (cmd->resp_type & MMC_RSP_136) {
209 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
210
Stefano Babicc67bee12010-02-05 15:11:27 +0100211 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
212 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
213 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
214 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530215 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
216 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
217 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
218 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500219 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100220 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500221
222 /* Wait until all of the blocks are transferred */
223 if (data) {
224 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100225 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500226
227 if (irqstat & DATA_ERR)
228 return COMM_ERR;
229
230 if (irqstat & IRQSTAT_DTOE)
231 return TIMEOUT;
232 } while (!(irqstat & IRQSTAT_TC) &&
Stefano Babicc67bee12010-02-05 15:11:27 +0100233 (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
Andy Fleming50586ef2008-10-30 16:47:16 -0500234 }
235
Stefano Babicc67bee12010-02-05 15:11:27 +0100236 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500237
238 return 0;
239}
240
241void set_sysctl(struct mmc *mmc, uint clock)
242{
243 int sdhc_clk = gd->sdhc_clk;
244 int div, pre_div;
Stefano Babicc67bee12010-02-05 15:11:27 +0100245 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
246 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500247 uint clk;
248
Stefano Babicc67bee12010-02-05 15:11:27 +0100249 if (clock < mmc->f_min)
250 clock = mmc->f_min;
251
Andy Fleming50586ef2008-10-30 16:47:16 -0500252 if (sdhc_clk / 16 > clock) {
253 for (pre_div = 2; pre_div < 256; pre_div *= 2)
254 if ((sdhc_clk / pre_div) <= (clock * 16))
255 break;
256 } else
257 pre_div = 2;
258
259 for (div = 1; div <= 16; div++)
260 if ((sdhc_clk / (div * pre_div)) <= clock)
261 break;
262
263 pre_div >>= 1;
264 div -= 1;
265
266 clk = (pre_div << 8) | (div << 4);
267
Stefano Babicc67bee12010-02-05 15:11:27 +0100268 /* On imx the clock must be stopped before changing frequency */
269 if (cfg->clk_enable)
270 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
271
272 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500273
274 udelay(10000);
275
Stefano Babicc67bee12010-02-05 15:11:27 +0100276 clk = SYSCTL_PEREN;
277 /* On imx systems the clock must be explicitely enabled */
278 if (cfg->clk_enable)
279 clk |= SYSCTL_CKEN;
280
281 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500282}
283
284static void esdhc_set_ios(struct mmc *mmc)
285{
Stefano Babicc67bee12010-02-05 15:11:27 +0100286 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
287 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500288
289 /* Set the clock speed */
290 set_sysctl(mmc, mmc->clock);
291
292 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100293 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500294
295 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100296 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500297 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100298 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
299
Andy Fleming50586ef2008-10-30 16:47:16 -0500300}
301
302static int esdhc_init(struct mmc *mmc)
303{
Stefano Babicc67bee12010-02-05 15:11:27 +0100304 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
305 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500306 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100307 int ret = 0;
308 u8 card_absent;
Andy Fleming50586ef2008-10-30 16:47:16 -0500309
310 /* Enable cache snooping */
Stefano Babicc67bee12010-02-05 15:11:27 +0100311 if (cfg && !cfg->no_snoop)
312 esdhc_write32(&regs->scr, 0x00000040);
Andy Fleming50586ef2008-10-30 16:47:16 -0500313
Stefano Babicc67bee12010-02-05 15:11:27 +0100314 /* Reset the entire host controller */
315 esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
316
317 /* Wait until the controller is available */
318 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
319 udelay(1000);
320
321 esdhc_write32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500322
323 /* Set the initial clock speed */
324 set_sysctl(mmc, 400000);
325
326 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100327 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500328
329 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100330 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500331
Stefano Babicc67bee12010-02-05 15:11:27 +0100332 /* Set timout to the maximum value */
333 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500334
Stefano Babicc67bee12010-02-05 15:11:27 +0100335 /* Check if there is a callback for detecting the card */
336 if (board_mmc_getcd(&card_absent, mmc)) {
337 timeout = 1000;
338 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) &&
339 --timeout)
340 udelay(1000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500341
Stefano Babicc67bee12010-02-05 15:11:27 +0100342 if (timeout <= 0)
343 ret = NO_CARD_ERR;
344 } else {
345 if (card_absent)
346 ret = NO_CARD_ERR;
347 }
348
349 return ret;
Andy Fleming50586ef2008-10-30 16:47:16 -0500350}
351
Stefano Babicc67bee12010-02-05 15:11:27 +0100352int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500353{
Stefano Babicc67bee12010-02-05 15:11:27 +0100354 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500355 struct mmc *mmc;
356 u32 caps;
357
Stefano Babicc67bee12010-02-05 15:11:27 +0100358 if (!cfg)
359 return -1;
360
Andy Fleming50586ef2008-10-30 16:47:16 -0500361 mmc = malloc(sizeof(struct mmc));
362
363 sprintf(mmc->name, "FSL_ESDHC");
Stefano Babicc67bee12010-02-05 15:11:27 +0100364 regs = (struct fsl_esdhc *)cfg->esdhc_base;
365
366 mmc->priv = cfg;
Andy Fleming50586ef2008-10-30 16:47:16 -0500367 mmc->send_cmd = esdhc_send_cmd;
368 mmc->set_ios = esdhc_set_ios;
369 mmc->init = esdhc_init;
370
371 caps = regs->hostcapblt;
372
373 if (caps & ESDHC_HOSTCAPBLT_VS18)
374 mmc->voltages |= MMC_VDD_165_195;
375 if (caps & ESDHC_HOSTCAPBLT_VS30)
376 mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
377 if (caps & ESDHC_HOSTCAPBLT_VS33)
378 mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
379
380 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
381
382 if (caps & ESDHC_HOSTCAPBLT_HSS)
383 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
384
385 mmc->f_min = 400000;
386 mmc->f_max = MIN(gd->sdhc_clk, 50000000);
387
388 mmc_register(mmc);
389
390 return 0;
391}
392
393int fsl_esdhc_mmc_init(bd_t *bis)
394{
Stefano Babicc67bee12010-02-05 15:11:27 +0100395 struct fsl_esdhc_cfg *cfg;
396
397 cfg = malloc(sizeof(struct fsl_esdhc_cfg));
398 memset(cfg, 0, sizeof(struct fsl_esdhc_cfg));
399 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
400 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500401}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400402
Stefano Babicc67bee12010-02-05 15:11:27 +0100403#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400404void fdt_fixup_esdhc(void *blob, bd_t *bd)
405{
406 const char *compat = "fsl,esdhc";
407 const char *status = "okay";
408
409 if (!hwconfig("esdhc")) {
410 status = "disabled";
411 goto out;
412 }
413
414 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
415 gd->sdhc_clk, 1);
416out:
417 do_fixup_by_compat(blob, compat, "status", status,
418 strlen(status) + 1, 1);
419}
Stefano Babicc67bee12010-02-05 15:11:27 +0100420#endif