blob: a7170b49db384ff20a0276eb03312192d993f1a6 [file] [log] [blame]
Andy Fleming50586ef2008-10-30 16:47:16 -05001/*
Jerry Huangd621da02011-01-06 23:42:19 -06002 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleming50586ef2008-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 Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming50586ef2008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040015#include <hwconfig.h>
Andy Fleming50586ef2008-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 Vorontsovb33433a2009-06-10 00:25:29 +040021#include <fdt_support.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050022#include <asm/io.h>
23
Andy Fleming50586ef2008-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 Liu46927082011-11-25 00:18:04 +000045 uint mixctrl;
46 char reserved1[4];
Andy Fleming50586ef2008-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 Phillipseafa90a2012-10-29 13:34:44 +000055static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -050056{
57 uint xfertyp = 0;
58
59 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +053060 xfertyp |= XFERTYP_DPSEL;
61#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
62 xfertyp |= XFERTYP_DMAEN;
63#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050064 if (data->blocks > 1) {
65 xfertyp |= XFERTYP_MSBSEL;
66 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -060067#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
68 xfertyp |= XFERTYP_AC12EN;
69#endif
Andy Fleming50586ef2008-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.Zhangb8e5b072013-07-01 14:26:01 +080087#if defined(CONFIG_MX53) || defined(CONFIG_T4240QDS)
Jason Liu4571de32011-03-22 01:32:31 +000088 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
89 xfertyp |= XFERTYP_CMDTYP_ABORT;
90#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050091 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
92}
93
Dipen Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +020098static void
Dipen Dudhat77c14582009-10-05 15:41:58 +053099esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
100{
Ira Snyder8eee2bd2011-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 Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200121 return;
Dipen Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200135 buffer = (char *)data->src;
Dipen Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200144 return;
Dipen Dudhat77c14582009-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 Fleming50586ef2008-10-30 16:47:16 -0500160static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
161{
Andy Fleming50586ef2008-10-30 16:47:16 -0500162 int timeout;
Stefano Babicc67bee12010-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 Estevamc2137b12013-05-28 15:09:42 -0300165#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200166 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500167
168 wml_value = data->blocksize/4;
169
170 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530171 if (wml_value > WML_RD_WML_MAX)
172 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500173
Roy Zangab467c52010-02-09 18:23:33 +0800174 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Stefano Babicc67bee12010-02-05 15:11:27 +0100175 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Andy Fleming50586ef2008-10-30 16:47:16 -0500176 } else {
Eric Nelsone576bd92012-04-25 14:28:48 +0000177 flush_dcache_range((ulong)data->src,
178 (ulong)data->src+data->blocks
179 *data->blocksize);
180
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530181 if (wml_value > WML_WR_WML_MAX)
182 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100183 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-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 Zangab467c52010-02-09 18:23:33 +0800187
188 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
189 wml_value << 16);
Stefano Babicc67bee12010-02-05 15:11:27 +0100190 esdhc_write32(&regs->dsaddr, (u32)data->src);
Andy Fleming50586ef2008-10-30 16:47:16 -0500191 }
Wolfgang Denk7b43db92010-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 Fleming50586ef2008-10-30 16:47:16 -0500203
Stefano Babicc67bee12010-02-05 15:11:27 +0100204 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500205
206 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-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 Fleming50586ef2008-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 Gala5103a032011-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 Babicc67bee12010-02-05 15:11:27 +0100235 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500236
237 return 0;
238}
239
Eric Nelsone576bd92012-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 Fleming50586ef2008-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 Babicc67bee12010-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 Fleming50586ef2008-10-30 16:47:16 -0500260
Jerry Huangd621da02011-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 Babicc67bee12010-02-05 15:11:27 +0100266 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500267
268 sync();
269
270 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100271 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
272 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
273 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500274
Stefano Babicc67bee12010-02-05 15:11:27 +0100275 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
276 ;
Andy Fleming50586ef2008-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 Gabbasov01b77352013-06-11 10:34:22 -0500297 /* Mask all irqs */
298 esdhc_write32(&regs->irqsigen, 0);
299
Andy Fleming50586ef2008-10-30 16:47:16 -0500300 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100301 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-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 Babicc67bee12010-02-05 15:11:27 +0100307 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000308#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000309
Andy Fleming50586ef2008-10-30 16:47:16 -0500310 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000311 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100312 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500313
Stefano Babicc67bee12010-02-05 15:11:27 +0100314 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500315
Dirk Behme7a5b8022012-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 Fleming50586ef2008-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 Behme7a5b8022012-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 Fleming50586ef2008-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 Babicc67bee12010-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 Vincent998be3d2009-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 Fleming50586ef2008-10-30 16:47:16 -0500367 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100368 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500369
370 /* Wait until all of the blocks are transferred */
371 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530372#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
373 esdhc_pio_read_write(mmc, data);
374#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500375 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100376 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500377
Andy Fleming50586ef2008-10-30 16:47:16 -0500378 if (irqstat & IRQSTAT_DTOE)
379 return TIMEOUT;
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000380
381 if (irqstat & DATA_ERR)
382 return COMM_ERR;
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000383 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530384#endif
Eric Nelson54899fc2013-04-03 12:31:56 +0000385 if (data->flags & MMC_DATA_READ)
386 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500387 }
388
Stefano Babicc67bee12010-02-05 15:11:27 +0100389 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500390
391 return 0;
392}
393
Kim Phillipseafa90a2012-10-29 13:34:44 +0000394static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500395{
Andy Fleming50586ef2008-10-30 16:47:16 -0500396 int div, pre_div;
Stefano Babicc67bee12010-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ébaudeaua2ac1b32012-10-01 08:36:25 +0000399 int sdhc_clk = cfg->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500400 uint clk;
401
Stefano Babicc67bee12010-02-05 15:11:27 +0100402 if (clock < mmc->f_min)
403 clock = mmc->f_min;
404
Andy Fleming50586ef2008-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 Galacc4d1222010-03-18 15:51:05 -0500421 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100422
423 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500424
425 udelay(10000);
426
Kumar Galacc4d1222010-03-18 15:51:05 -0500427 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicc67bee12010-02-05 15:11:27 +0100428
429 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500430}
431
432static void esdhc_set_ios(struct mmc *mmc)
433{
Stefano Babicc67bee12010-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 Fleming50586ef2008-10-30 16:47:16 -0500436
437 /* Set the clock speed */
438 set_sysctl(mmc, mmc->clock);
439
440 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100441 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500442
443 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100444 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500445 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100446 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
447
Andy Fleming50586ef2008-10-30 16:47:16 -0500448}
449
450static int esdhc_init(struct mmc *mmc)
451{
Stefano Babicc67bee12010-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 Fleming50586ef2008-10-30 16:47:16 -0500454 int timeout = 1000;
455
Stefano Babicc67bee12010-02-05 15:11:27 +0100456 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200457 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-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);
462
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000463#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530464 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000465 esdhc_write32(&regs->scr, 0x00000040);
466#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530467
Dirk Behmea61da722013-07-15 15:44:29 +0200468 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500469
470 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000471 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500472
473 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100474 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500475
476 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100477 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500478
Stefano Babicc67bee12010-02-05 15:11:27 +0100479 /* Set timout to the maximum value */
480 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500481
Thierry Redingd48d2e22012-01-02 01:15:38 +0000482 return 0;
483}
Andy Fleming50586ef2008-10-30 16:47:16 -0500484
Thierry Redingd48d2e22012-01-02 01:15:38 +0000485static 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;
Stefano Babicc67bee12010-02-05 15:11:27 +0100490
Thierry Redingd48d2e22012-01-02 01:15:38 +0000491 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
492 udelay(1000);
493
494 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500495}
496
Jerry Huang48bb3bb2010-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 Behmea61da722013-07-15 15:44:29 +0200502 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-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 Babicc67bee12010-02-05 15:11:27 +0100511int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500512{
Stefano Babicc67bee12010-02-05 15:11:27 +0100513 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500514 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000515 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500516
Stefano Babicc67bee12010-02-05 15:11:27 +0100517 if (!cfg)
518 return -1;
519
Andy Fleming50586ef2008-10-30 16:47:16 -0500520 mmc = malloc(sizeof(struct mmc));
Fabio Estevam3f786a82013-09-12 10:35:52 -0300521 if (!mmc)
522 return -ENOMEM;
Andy Fleming50586ef2008-10-30 16:47:16 -0500523
Jason Liu46927082011-11-25 00:18:04 +0000524 sprintf(mmc->name, "FSL_SDHC");
Stefano Babicc67bee12010-02-05 15:11:27 +0100525 regs = (struct fsl_esdhc *)cfg->esdhc_base;
526
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500527 /* First reset the eSDHC controller */
528 esdhc_reset(regs);
529
Jerry Huang975324a2012-05-17 23:57:02 +0000530 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
531 | SYSCTL_IPGEN | SYSCTL_CKEN);
532
Stefano Babicc67bee12010-02-05 15:11:27 +0100533 mmc->priv = cfg;
Andy Fleming50586ef2008-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 Redingd48d2e22012-01-02 01:15:38 +0000537 mmc->getcd = esdhc_getcd;
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +0000538 mmc->getwp = NULL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500539
Li Yang030955c2010-11-25 17:06:09 +0000540 voltage_caps = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500541 caps = regs->hostcapblt;
Roy Zang3b4456e2011-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 Fleming50586ef2008-10-30 16:47:16 -0500547 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000548 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500549 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000550 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500551 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-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 Fleming50586ef2008-10-30 16:47:16 -0500563
Shawn Guofb8302b2012-12-30 14:14:58 +0000564 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Andy Fleming50586ef2008-10-30 16:47:16 -0500565
Abbas Razaaad46592013-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 Fleming50586ef2008-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 Glasse9adeca2012-12-13 20:49:05 +0000577 mmc->f_max = MIN(gd->arch.sdhc_clk, 52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500578
Fabio Estevam1ed60d72011-05-12 09:33:27 +0000579 mmc->b_max = 0;
Andy Fleming50586ef2008-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 Babicc67bee12010-02-05 15:11:27 +0100587 struct fsl_esdhc_cfg *cfg;
588
Fabio Estevam88227a12012-12-27 08:51:08 +0000589 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100590 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000591 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100592 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500593}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400594
Stefano Babicc67bee12010-02-05 15:11:27 +0100595#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400596void fdt_fixup_esdhc(void *blob, bd_t *bd)
597{
598 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400599
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800600#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400601 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800602 do_fixup_by_compat(blob, compat, "status", "disabled",
603 8 + 1, 1);
604 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400605 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800606#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400607
608 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000609 gd->arch.sdhc_clk, 1);
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800610
611 do_fixup_by_compat(blob, compat, "status", "okay",
612 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400613}
Stefano Babicc67bee12010-02-05 15:11:27 +0100614#endif