blob: a7170b49db384ff20a0276eb03312192d993f1a6 [file] [log] [blame]
Andy Fleminge52ffb82008-10-30 16:47:16 -05001/*
Jerry Huanged413672011-01-06 23:42:19 -06002 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleminge52ffb82008-10-30 16:47:16 -05003 * 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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Andy Fleminge52ffb82008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040015#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050016#include <mmc.h>
17#include <part.h>
18#include <malloc.h>
19#include <mmc.h>
20#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040021#include <fdt_support.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050022#include <asm/io.h>
23
Andy Fleminge52ffb82008-10-30 16:47:16 -050024DECLARE_GLOBAL_DATA_PTR;
25
26struct fsl_esdhc {
27 uint dsaddr;
28 uint blkattr;
29 uint cmdarg;
30 uint xfertyp;
31 uint cmdrsp0;
32 uint cmdrsp1;
33 uint cmdrsp2;
34 uint cmdrsp3;
35 uint datport;
36 uint prsstat;
37 uint proctl;
38 uint sysctl;
39 uint irqstat;
40 uint irqstaten;
41 uint irqsigen;
42 uint autoc12err;
43 uint hostcapblt;
44 uint wml;
Jason Liu9919d642011-11-25 00:18:04 +000045 uint mixctrl;
46 char reserved1[4];
Andy Fleminge52ffb82008-10-30 16:47:16 -050047 uint fevt;
48 char reserved2[168];
49 uint hostver;
50 char reserved3[780];
51 uint scr;
52};
53
54/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +000055static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -050056{
57 uint xfertyp = 0;
58
59 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +053060 xfertyp |= XFERTYP_DPSEL;
61#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
62 xfertyp |= XFERTYP_DMAEN;
63#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050064 if (data->blocks > 1) {
65 xfertyp |= XFERTYP_MSBSEL;
66 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -060067#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
68 xfertyp |= XFERTYP_AC12EN;
69#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050070 }
71
72 if (data->flags & MMC_DATA_READ)
73 xfertyp |= XFERTYP_DTDSEL;
74 }
75
76 if (cmd->resp_type & MMC_RSP_CRC)
77 xfertyp |= XFERTYP_CCCEN;
78 if (cmd->resp_type & MMC_RSP_OPCODE)
79 xfertyp |= XFERTYP_CICEN;
80 if (cmd->resp_type & MMC_RSP_136)
81 xfertyp |= XFERTYP_RSPTYP_136;
82 else if (cmd->resp_type & MMC_RSP_BUSY)
83 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
84 else if (cmd->resp_type & MMC_RSP_PRESENT)
85 xfertyp |= XFERTYP_RSPTYP_48;
86
Haijun.Zhang203fa4d2013-07-01 14:26:01 +080087#if defined(CONFIG_MX53) || defined(CONFIG_T4240QDS)
Jason Liubef0ff02011-03-22 01:32:31 +000088 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
89 xfertyp |= XFERTYP_CMDTYP_ABORT;
90#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050091 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
92}
93
Dipen Dudhat5c72f352009-10-05 15:41:58 +053094#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
95/*
96 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
97 */
Wolfgang Denka40545c2010-05-09 23:52:59 +020098static void
Dipen Dudhat5c72f352009-10-05 15:41:58 +053099esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
100{
Ira Snyder66a722e2011-12-23 08:30:40 +0000101 struct fsl_esdhc_cfg *cfg = mmc->priv;
102 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530103 uint blocks;
104 char *buffer;
105 uint databuf;
106 uint size;
107 uint irqstat;
108 uint timeout;
109
110 if (data->flags & MMC_DATA_READ) {
111 blocks = data->blocks;
112 buffer = data->dest;
113 while (blocks) {
114 timeout = PIO_TIMEOUT;
115 size = data->blocksize;
116 irqstat = esdhc_read32(&regs->irqstat);
117 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
118 && --timeout);
119 if (timeout <= 0) {
120 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200121 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530122 }
123 while (size && (!(irqstat & IRQSTAT_TC))) {
124 udelay(100); /* Wait before last byte transfer complete */
125 irqstat = esdhc_read32(&regs->irqstat);
126 databuf = in_le32(&regs->datport);
127 *((uint *)buffer) = databuf;
128 buffer += 4;
129 size -= 4;
130 }
131 blocks--;
132 }
133 } else {
134 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200135 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530136 while (blocks) {
137 timeout = PIO_TIMEOUT;
138 size = data->blocksize;
139 irqstat = esdhc_read32(&regs->irqstat);
140 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
141 && --timeout);
142 if (timeout <= 0) {
143 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200144 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530145 }
146 while (size && (!(irqstat & IRQSTAT_TC))) {
147 udelay(100); /* Wait before last byte transfer complete */
148 databuf = *((uint *)buffer);
149 buffer += 4;
150 size -= 4;
151 irqstat = esdhc_read32(&regs->irqstat);
152 out_le32(&regs->datport, databuf);
153 }
154 blocks--;
155 }
156 }
157}
158#endif
159
Andy Fleminge52ffb82008-10-30 16:47:16 -0500160static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
161{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500162 int timeout;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100163 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
164 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Fabio Estevam88525ed2013-05-28 15:09:42 -0300165#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Wolfgang Denka40545c2010-05-09 23:52:59 +0200166 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500167
168 wml_value = data->blocksize/4;
169
170 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530171 if (wml_value > WML_RD_WML_MAX)
172 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500173
Roy Zange5853af2010-02-09 18:23:33 +0800174 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100175 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500176 } else {
Eric Nelson30e9cad2012-04-25 14:28:48 +0000177 flush_dcache_range((ulong)data->src,
178 (ulong)data->src+data->blocks
179 *data->blocksize);
180
Priyanka Jain02449632011-02-09 09:24:10 +0530181 if (wml_value > WML_WR_WML_MAX)
182 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100183 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500184 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
185 return TIMEOUT;
186 }
Roy Zange5853af2010-02-09 18:23:33 +0800187
188 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
189 wml_value << 16);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100190 esdhc_write32(&regs->dsaddr, (u32)data->src);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500191 }
Wolfgang Denka40545c2010-05-09 23:52:59 +0200192#else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
193 if (!(data->flags & MMC_DATA_READ)) {
194 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
195 printf("\nThe SD card is locked. "
196 "Can not write to a locked card.\n\n");
197 return TIMEOUT;
198 }
199 esdhc_write32(&regs->dsaddr, (u32)data->src);
200 } else
201 esdhc_write32(&regs->dsaddr, (u32)data->dest);
202#endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
Andy Fleminge52ffb82008-10-30 16:47:16 -0500203
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100204 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500205
206 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530207 /*
208 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
209 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
210 * So, Number of SD Clock cycles for 0.25sec should be minimum
211 * (SD Clock/sec * 0.25 sec) SD Clock cycles
212 * = (mmc->tran_speed * 1/4) SD Clock cycles
213 * As 1) >= 2)
214 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
215 * Taking log2 both the sides
216 * => timeout + 13 >= log2(mmc->tran_speed/4)
217 * Rounding up to next power of 2
218 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
219 * => timeout + 13 = fls(mmc->tran_speed/4)
220 */
221 timeout = fls(mmc->tran_speed/4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500222 timeout -= 13;
223
224 if (timeout > 14)
225 timeout = 14;
226
227 if (timeout < 0)
228 timeout = 0;
229
Kumar Gala9a878d52011-01-29 15:36:10 -0600230#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
231 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
232 timeout++;
233#endif
234
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100235 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500236
237 return 0;
238}
239
Eric Nelson30e9cad2012-04-25 14:28:48 +0000240static void check_and_invalidate_dcache_range
241 (struct mmc_cmd *cmd,
242 struct mmc_data *data) {
243 unsigned start = (unsigned)data->dest ;
244 unsigned size = roundup(ARCH_DMA_MINALIGN,
245 data->blocks*data->blocksize);
246 unsigned end = start+size ;
247 invalidate_dcache_range(start, end);
248}
Andy Fleminge52ffb82008-10-30 16:47:16 -0500249/*
250 * Sends a command out on the bus. Takes the mmc pointer,
251 * a command pointer, and an optional data pointer.
252 */
253static int
254esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
255{
256 uint xfertyp;
257 uint irqstat;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100258 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
259 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500260
Jerry Huanged413672011-01-06 23:42:19 -0600261#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
262 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
263 return 0;
264#endif
265
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100266 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500267
268 sync();
269
270 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100271 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
272 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
273 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500274
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100275 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
276 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500277
278 /* Wait at least 8 SD clock cycles before the next command */
279 /*
280 * Note: This is way more than 8 cycles, but 1ms seems to
281 * resolve timing issues with some cards
282 */
283 udelay(1000);
284
285 /* Set up for a data transfer if we have one */
286 if (data) {
287 int err;
288
289 err = esdhc_setup_data(mmc, data);
290 if(err)
291 return err;
292 }
293
294 /* Figure out the transfer arguments */
295 xfertyp = esdhc_xfertyp(cmd, data);
296
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500297 /* Mask all irqs */
298 esdhc_write32(&regs->irqsigen, 0);
299
Andy Fleminge52ffb82008-10-30 16:47:16 -0500300 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100301 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000302#if defined(CONFIG_FSL_USDHC)
303 esdhc_write32(&regs->mixctrl,
304 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
305 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
306#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100307 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000308#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000309
Andy Fleminge52ffb82008-10-30 16:47:16 -0500310 /* Wait for the command to complete */
Dirk Behmed8552d62012-03-26 03:13:05 +0000311 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100312 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500313
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100314 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500315
Dirk Behmed8552d62012-03-26 03:13:05 +0000316 /* Reset CMD and DATA portions on error */
317 if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
318 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
319 SYSCTL_RSTC);
320 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
321 ;
322
323 if (data) {
324 esdhc_write32(&regs->sysctl,
325 esdhc_read32(&regs->sysctl) |
326 SYSCTL_RSTD);
327 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
328 ;
329 }
330 }
331
Andy Fleminge52ffb82008-10-30 16:47:16 -0500332 if (irqstat & CMD_ERR)
333 return COMM_ERR;
334
335 if (irqstat & IRQSTAT_CTOE)
336 return TIMEOUT;
337
Dirk Behmed8552d62012-03-26 03:13:05 +0000338 /* Workaround for ESDHC errata ENGcm03648 */
339 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
340 int timeout = 2500;
341
342 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
343 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
344 PRSSTAT_DAT0)) {
345 udelay(100);
346 timeout--;
347 }
348
349 if (timeout <= 0) {
350 printf("Timeout waiting for DAT0 to go high!\n");
351 return TIMEOUT;
352 }
353 }
354
Andy Fleminge52ffb82008-10-30 16:47:16 -0500355 /* Copy the response to the response buffer */
356 if (cmd->resp_type & MMC_RSP_136) {
357 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
358
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100359 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
360 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
361 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
362 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530363 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
364 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
365 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
366 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500367 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100368 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500369
370 /* Wait until all of the blocks are transferred */
371 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530372#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
373 esdhc_pio_read_write(mmc, data);
374#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500375 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100376 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500377
Andy Fleminge52ffb82008-10-30 16:47:16 -0500378 if (irqstat & IRQSTAT_DTOE)
379 return TIMEOUT;
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000380
381 if (irqstat & DATA_ERR)
382 return COMM_ERR;
Andrew Gabbasov4a929622013-04-07 23:06:08 +0000383 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530384#endif
Eric Nelson70e68692013-04-03 12:31:56 +0000385 if (data->flags & MMC_DATA_READ)
386 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500387 }
388
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100389 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500390
391 return 0;
392}
393
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000394static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500395{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500396 int div, pre_div;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100397 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
398 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeauc58ff342012-10-01 08:36:25 +0000399 int sdhc_clk = cfg->sdhc_clk;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500400 uint clk;
401
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100402 if (clock < mmc->f_min)
403 clock = mmc->f_min;
404
Andy Fleminge52ffb82008-10-30 16:47:16 -0500405 if (sdhc_clk / 16 > clock) {
406 for (pre_div = 2; pre_div < 256; pre_div *= 2)
407 if ((sdhc_clk / pre_div) <= (clock * 16))
408 break;
409 } else
410 pre_div = 2;
411
412 for (div = 1; div <= 16; div++)
413 if ((sdhc_clk / (div * pre_div)) <= clock)
414 break;
415
416 pre_div >>= 1;
417 div -= 1;
418
419 clk = (pre_div << 8) | (div << 4);
420
Kumar Gala09876a32010-03-18 15:51:05 -0500421 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100422
423 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500424
425 udelay(10000);
426
Kumar Gala09876a32010-03-18 15:51:05 -0500427 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100428
429 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500430}
431
432static void esdhc_set_ios(struct mmc *mmc)
433{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100434 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
435 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500436
437 /* Set the clock speed */
438 set_sysctl(mmc, mmc->clock);
439
440 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100441 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500442
443 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100444 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500445 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100446 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
447
Andy Fleminge52ffb82008-10-30 16:47:16 -0500448}
449
450static int esdhc_init(struct mmc *mmc)
451{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100452 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
453 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500454 int timeout = 1000;
455
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100456 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200457 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100458
459 /* Wait until the controller is available */
460 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
461 udelay(1000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500462
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000463#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530464 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000465 esdhc_write32(&regs->scr, 0x00000040);
466#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530467
Dirk Behmedbe67252013-07-15 15:44:29 +0200468 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500469
470 /* Set the initial clock speed */
Jerry Huang0caea1a2010-11-25 17:06:07 +0000471 mmc_set_clock(mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500472
473 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100474 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475
476 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100477 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500478
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100479 /* Set timout to the maximum value */
480 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500481
Thierry Reding8cee4c982012-01-02 01:15:38 +0000482 return 0;
483}
484
485static int esdhc_getcd(struct mmc *mmc)
486{
487 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
488 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
489 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500490
Thierry Reding8cee4c982012-01-02 01:15:38 +0000491 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
492 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100493
Thierry Reding8cee4c982012-01-02 01:15:38 +0000494 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500495}
496
Jerry Huangb7ef7562010-03-18 15:57:06 -0500497static void esdhc_reset(struct fsl_esdhc *regs)
498{
499 unsigned long timeout = 100; /* wait max 100 ms */
500
501 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200502 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500503
504 /* hardware clears the bit when it is done */
505 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
506 udelay(1000);
507 if (!timeout)
508 printf("MMC/SD: Reset never completed.\n");
509}
510
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100511int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500512{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100513 struct fsl_esdhc *regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500514 struct mmc *mmc;
Li Yangd4933f22010-11-25 17:06:09 +0000515 u32 caps, voltage_caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500516
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100517 if (!cfg)
518 return -1;
519
Andy Fleminge52ffb82008-10-30 16:47:16 -0500520 mmc = malloc(sizeof(struct mmc));
Fabio Estevamd95b7062013-09-12 10:35:52 -0300521 if (!mmc)
522 return -ENOMEM;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500523
Jason Liu9919d642011-11-25 00:18:04 +0000524 sprintf(mmc->name, "FSL_SDHC");
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100525 regs = (struct fsl_esdhc *)cfg->esdhc_base;
526
Jerry Huangb7ef7562010-03-18 15:57:06 -0500527 /* First reset the eSDHC controller */
528 esdhc_reset(regs);
529
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000530 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
531 | SYSCTL_IPGEN | SYSCTL_CKEN);
532
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100533 mmc->priv = cfg;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500534 mmc->send_cmd = esdhc_send_cmd;
535 mmc->set_ios = esdhc_set_ios;
536 mmc->init = esdhc_init;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000537 mmc->getcd = esdhc_getcd;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000538 mmc->getwp = NULL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500539
Li Yangd4933f22010-11-25 17:06:09 +0000540 voltage_caps = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500541 caps = regs->hostcapblt;
Roy Zang39356612011-01-07 00:06:47 -0600542
543#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
544 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
545 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
546#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500547 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000548 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500549 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000550 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500551 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000552 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
553
554#ifdef CONFIG_SYS_SD_VOLTAGE
555 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
556#else
557 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
558#endif
559 if ((mmc->voltages & voltage_caps) == 0) {
560 printf("voltage not supported by controller\n");
561 return -1;
562 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500563
Shawn Guo75670762012-12-30 14:14:58 +0000564 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500565
Abbas Razae6bf9772013-03-25 09:13:34 +0000566 if (cfg->max_bus_width > 0) {
567 if (cfg->max_bus_width < 8)
568 mmc->host_caps &= ~MMC_MODE_8BIT;
569 if (cfg->max_bus_width < 4)
570 mmc->host_caps &= ~MMC_MODE_4BIT;
571 }
572
Andy Fleminge52ffb82008-10-30 16:47:16 -0500573 if (caps & ESDHC_HOSTCAPBLT_HSS)
574 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
575
576 mmc->f_min = 400000;
Simon Glass9e247d12012-12-13 20:49:05 +0000577 mmc->f_max = MIN(gd->arch.sdhc_clk, 52000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500578
Fabio Estevam1be94b72011-05-12 09:33:27 +0000579 mmc->b_max = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500580 mmc_register(mmc);
581
582 return 0;
583}
584
585int fsl_esdhc_mmc_init(bd_t *bis)
586{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100587 struct fsl_esdhc_cfg *cfg;
588
Fabio Estevam6592a992012-12-27 08:51:08 +0000589 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100590 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000591 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100592 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500593}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400594
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100595#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400596void fdt_fixup_esdhc(void *blob, bd_t *bd)
597{
598 const char *compat = "fsl,esdhc";
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400599
Chenhui Zhao025eab02011-01-04 17:23:05 +0800600#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400601 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800602 do_fixup_by_compat(blob, compat, "status", "disabled",
603 8 + 1, 1);
604 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400605 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800606#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400607
608 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000609 gd->arch.sdhc_clk, 1);
Chenhui Zhao025eab02011-01-04 17:23:05 +0800610
611 do_fixup_by_compat(blob, compat, "status", "okay",
612 4 + 1, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400613}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100614#endif