blob: 973b19f337b59b9646f58cff06454b0182b85ae5 [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 *
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;
Jason Liu46927082011-11-25 00:18:04 +000061 uint mixctrl;
62 char reserved1[4];
Andy Fleming50586ef2008-10-30 16:47:16 -050063 uint fevt;
64 char reserved2[168];
65 uint hostver;
66 char reserved3[780];
67 uint scr;
68};
69
70/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +000071static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -050072{
73 uint xfertyp = 0;
74
75 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +053076 xfertyp |= XFERTYP_DPSEL;
77#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
78 xfertyp |= XFERTYP_DMAEN;
79#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050080 if (data->blocks > 1) {
81 xfertyp |= XFERTYP_MSBSEL;
82 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -060083#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
84 xfertyp |= XFERTYP_AC12EN;
85#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050086 }
87
88 if (data->flags & MMC_DATA_READ)
89 xfertyp |= XFERTYP_DTDSEL;
90 }
91
92 if (cmd->resp_type & MMC_RSP_CRC)
93 xfertyp |= XFERTYP_CCCEN;
94 if (cmd->resp_type & MMC_RSP_OPCODE)
95 xfertyp |= XFERTYP_CICEN;
96 if (cmd->resp_type & MMC_RSP_136)
97 xfertyp |= XFERTYP_RSPTYP_136;
98 else if (cmd->resp_type & MMC_RSP_BUSY)
99 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
100 else if (cmd->resp_type & MMC_RSP_PRESENT)
101 xfertyp |= XFERTYP_RSPTYP_48;
102
Jason Liu4571de32011-03-22 01:32:31 +0000103#ifdef CONFIG_MX53
104 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
105 xfertyp |= XFERTYP_CMDTYP_ABORT;
106#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500107 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
108}
109
Dipen Dudhat77c14582009-10-05 15:41:58 +0530110#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
111/*
112 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
113 */
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200114static void
Dipen Dudhat77c14582009-10-05 15:41:58 +0530115esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
116{
Ira Snyder8eee2bd2011-12-23 08:30:40 +0000117 struct fsl_esdhc_cfg *cfg = mmc->priv;
118 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530119 uint blocks;
120 char *buffer;
121 uint databuf;
122 uint size;
123 uint irqstat;
124 uint timeout;
125
126 if (data->flags & MMC_DATA_READ) {
127 blocks = data->blocks;
128 buffer = data->dest;
129 while (blocks) {
130 timeout = PIO_TIMEOUT;
131 size = data->blocksize;
132 irqstat = esdhc_read32(&regs->irqstat);
133 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
134 && --timeout);
135 if (timeout <= 0) {
136 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200137 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530138 }
139 while (size && (!(irqstat & IRQSTAT_TC))) {
140 udelay(100); /* Wait before last byte transfer complete */
141 irqstat = esdhc_read32(&regs->irqstat);
142 databuf = in_le32(&regs->datport);
143 *((uint *)buffer) = databuf;
144 buffer += 4;
145 size -= 4;
146 }
147 blocks--;
148 }
149 } else {
150 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200151 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530152 while (blocks) {
153 timeout = PIO_TIMEOUT;
154 size = data->blocksize;
155 irqstat = esdhc_read32(&regs->irqstat);
156 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
157 && --timeout);
158 if (timeout <= 0) {
159 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200160 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530161 }
162 while (size && (!(irqstat & IRQSTAT_TC))) {
163 udelay(100); /* Wait before last byte transfer complete */
164 databuf = *((uint *)buffer);
165 buffer += 4;
166 size -= 4;
167 irqstat = esdhc_read32(&regs->irqstat);
168 out_le32(&regs->datport, databuf);
169 }
170 blocks--;
171 }
172 }
173}
174#endif
175
Andy Fleming50586ef2008-10-30 16:47:16 -0500176static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
177{
Andy Fleming50586ef2008-10-30 16:47:16 -0500178 int timeout;
Stefano Babicc67bee12010-02-05 15:11:27 +0100179 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
180 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Fabio Estevamc2137b12013-05-28 15:09:42 -0300181#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200182 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500183
184 wml_value = data->blocksize/4;
185
186 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530187 if (wml_value > WML_RD_WML_MAX)
188 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500189
Roy Zangab467c52010-02-09 18:23:33 +0800190 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Stefano Babicc67bee12010-02-05 15:11:27 +0100191 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Andy Fleming50586ef2008-10-30 16:47:16 -0500192 } else {
Eric Nelsone576bd92012-04-25 14:28:48 +0000193 flush_dcache_range((ulong)data->src,
194 (ulong)data->src+data->blocks
195 *data->blocksize);
196
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530197 if (wml_value > WML_WR_WML_MAX)
198 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100199 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500200 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
201 return TIMEOUT;
202 }
Roy Zangab467c52010-02-09 18:23:33 +0800203
204 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
205 wml_value << 16);
Stefano Babicc67bee12010-02-05 15:11:27 +0100206 esdhc_write32(&regs->dsaddr, (u32)data->src);
Andy Fleming50586ef2008-10-30 16:47:16 -0500207 }
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200208#else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
209 if (!(data->flags & MMC_DATA_READ)) {
210 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
211 printf("\nThe SD card is locked. "
212 "Can not write to a locked card.\n\n");
213 return TIMEOUT;
214 }
215 esdhc_write32(&regs->dsaddr, (u32)data->src);
216 } else
217 esdhc_write32(&regs->dsaddr, (u32)data->dest);
218#endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
Andy Fleming50586ef2008-10-30 16:47:16 -0500219
Stefano Babicc67bee12010-02-05 15:11:27 +0100220 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500221
222 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530223 /*
224 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
225 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
226 * So, Number of SD Clock cycles for 0.25sec should be minimum
227 * (SD Clock/sec * 0.25 sec) SD Clock cycles
228 * = (mmc->tran_speed * 1/4) SD Clock cycles
229 * As 1) >= 2)
230 * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
231 * Taking log2 both the sides
232 * => timeout + 13 >= log2(mmc->tran_speed/4)
233 * Rounding up to next power of 2
234 * => timeout + 13 = log2(mmc->tran_speed/4) + 1
235 * => timeout + 13 = fls(mmc->tran_speed/4)
236 */
237 timeout = fls(mmc->tran_speed/4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500238 timeout -= 13;
239
240 if (timeout > 14)
241 timeout = 14;
242
243 if (timeout < 0)
244 timeout = 0;
245
Kumar Gala5103a032011-01-29 15:36:10 -0600246#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
247 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
248 timeout++;
249#endif
250
Stefano Babicc67bee12010-02-05 15:11:27 +0100251 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500252
253 return 0;
254}
255
Eric Nelsone576bd92012-04-25 14:28:48 +0000256static void check_and_invalidate_dcache_range
257 (struct mmc_cmd *cmd,
258 struct mmc_data *data) {
259 unsigned start = (unsigned)data->dest ;
260 unsigned size = roundup(ARCH_DMA_MINALIGN,
261 data->blocks*data->blocksize);
262 unsigned end = start+size ;
263 invalidate_dcache_range(start, end);
264}
Andy Fleming50586ef2008-10-30 16:47:16 -0500265/*
266 * Sends a command out on the bus. Takes the mmc pointer,
267 * a command pointer, and an optional data pointer.
268 */
269static int
270esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
271{
272 uint xfertyp;
273 uint irqstat;
Stefano Babicc67bee12010-02-05 15:11:27 +0100274 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
275 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500276
Jerry Huangd621da02011-01-06 23:42:19 -0600277#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
278 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
279 return 0;
280#endif
281
Stefano Babicc67bee12010-02-05 15:11:27 +0100282 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500283
284 sync();
285
286 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100287 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
288 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
289 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500290
Stefano Babicc67bee12010-02-05 15:11:27 +0100291 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
292 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500293
294 /* Wait at least 8 SD clock cycles before the next command */
295 /*
296 * Note: This is way more than 8 cycles, but 1ms seems to
297 * resolve timing issues with some cards
298 */
299 udelay(1000);
300
301 /* Set up for a data transfer if we have one */
302 if (data) {
303 int err;
304
305 err = esdhc_setup_data(mmc, data);
306 if(err)
307 return err;
308 }
309
310 /* Figure out the transfer arguments */
311 xfertyp = esdhc_xfertyp(cmd, data);
312
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500313 /* Mask all irqs */
314 esdhc_write32(&regs->irqsigen, 0);
315
Andy Fleming50586ef2008-10-30 16:47:16 -0500316 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100317 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000318#if defined(CONFIG_FSL_USDHC)
319 esdhc_write32(&regs->mixctrl,
320 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
321 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
322#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100323 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000324#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000325
Andy Fleming50586ef2008-10-30 16:47:16 -0500326 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000327 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100328 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500329
Stefano Babicc67bee12010-02-05 15:11:27 +0100330 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500331
Dirk Behme7a5b8022012-03-26 03:13:05 +0000332 /* Reset CMD and DATA portions on error */
333 if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
334 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
335 SYSCTL_RSTC);
336 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
337 ;
338
339 if (data) {
340 esdhc_write32(&regs->sysctl,
341 esdhc_read32(&regs->sysctl) |
342 SYSCTL_RSTD);
343 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
344 ;
345 }
346 }
347
Andy Fleming50586ef2008-10-30 16:47:16 -0500348 if (irqstat & CMD_ERR)
349 return COMM_ERR;
350
351 if (irqstat & IRQSTAT_CTOE)
352 return TIMEOUT;
353
Dirk Behme7a5b8022012-03-26 03:13:05 +0000354 /* Workaround for ESDHC errata ENGcm03648 */
355 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
356 int timeout = 2500;
357
358 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
359 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
360 PRSSTAT_DAT0)) {
361 udelay(100);
362 timeout--;
363 }
364
365 if (timeout <= 0) {
366 printf("Timeout waiting for DAT0 to go high!\n");
367 return TIMEOUT;
368 }
369 }
370
Andy Fleming50586ef2008-10-30 16:47:16 -0500371 /* Copy the response to the response buffer */
372 if (cmd->resp_type & MMC_RSP_136) {
373 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
374
Stefano Babicc67bee12010-02-05 15:11:27 +0100375 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
376 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
377 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
378 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530379 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
380 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
381 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
382 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500383 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100384 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500385
386 /* Wait until all of the blocks are transferred */
387 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530388#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
389 esdhc_pio_read_write(mmc, data);
390#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500391 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100392 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500393
Andy Fleming50586ef2008-10-30 16:47:16 -0500394 if (irqstat & IRQSTAT_DTOE)
395 return TIMEOUT;
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000396
397 if (irqstat & DATA_ERR)
398 return COMM_ERR;
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000399 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530400#endif
Eric Nelson54899fc2013-04-03 12:31:56 +0000401 if (data->flags & MMC_DATA_READ)
402 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500403 }
404
Stefano Babicc67bee12010-02-05 15:11:27 +0100405 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500406
407 return 0;
408}
409
Kim Phillipseafa90a2012-10-29 13:34:44 +0000410static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500411{
Andy Fleming50586ef2008-10-30 16:47:16 -0500412 int div, pre_div;
Stefano Babicc67bee12010-02-05 15:11:27 +0100413 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
414 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeaua2ac1b32012-10-01 08:36:25 +0000415 int sdhc_clk = cfg->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500416 uint clk;
417
Stefano Babicc67bee12010-02-05 15:11:27 +0100418 if (clock < mmc->f_min)
419 clock = mmc->f_min;
420
Andy Fleming50586ef2008-10-30 16:47:16 -0500421 if (sdhc_clk / 16 > clock) {
422 for (pre_div = 2; pre_div < 256; pre_div *= 2)
423 if ((sdhc_clk / pre_div) <= (clock * 16))
424 break;
425 } else
426 pre_div = 2;
427
428 for (div = 1; div <= 16; div++)
429 if ((sdhc_clk / (div * pre_div)) <= clock)
430 break;
431
432 pre_div >>= 1;
433 div -= 1;
434
435 clk = (pre_div << 8) | (div << 4);
436
Kumar Galacc4d1222010-03-18 15:51:05 -0500437 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100438
439 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500440
441 udelay(10000);
442
Kumar Galacc4d1222010-03-18 15:51:05 -0500443 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicc67bee12010-02-05 15:11:27 +0100444
445 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500446}
447
448static void esdhc_set_ios(struct mmc *mmc)
449{
Stefano Babicc67bee12010-02-05 15:11:27 +0100450 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
451 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500452
453 /* Set the clock speed */
454 set_sysctl(mmc, mmc->clock);
455
456 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100457 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500458
459 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100460 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500461 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100462 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
463
Andy Fleming50586ef2008-10-30 16:47:16 -0500464}
465
466static int esdhc_init(struct mmc *mmc)
467{
Stefano Babicc67bee12010-02-05 15:11:27 +0100468 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
469 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500470 int timeout = 1000;
471
Stefano Babicc67bee12010-02-05 15:11:27 +0100472 /* Reset the entire host controller */
473 esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
474
475 /* Wait until the controller is available */
476 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
477 udelay(1000);
478
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000479#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530480 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000481 esdhc_write32(&regs->scr, 0x00000040);
482#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530483
Stefano Babicc67bee12010-02-05 15:11:27 +0100484 esdhc_write32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500485
486 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000487 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500488
489 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100490 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500491
492 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100493 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500494
Stefano Babicc67bee12010-02-05 15:11:27 +0100495 /* Set timout to the maximum value */
496 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500497
Thierry Redingd48d2e22012-01-02 01:15:38 +0000498 return 0;
499}
Andy Fleming50586ef2008-10-30 16:47:16 -0500500
Thierry Redingd48d2e22012-01-02 01:15:38 +0000501static int esdhc_getcd(struct mmc *mmc)
502{
503 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
504 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
505 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100506
Thierry Redingd48d2e22012-01-02 01:15:38 +0000507 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
508 udelay(1000);
509
510 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500511}
512
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500513static void esdhc_reset(struct fsl_esdhc *regs)
514{
515 unsigned long timeout = 100; /* wait max 100 ms */
516
517 /* reset the controller */
518 esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
519
520 /* hardware clears the bit when it is done */
521 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
522 udelay(1000);
523 if (!timeout)
524 printf("MMC/SD: Reset never completed.\n");
525}
526
Stefano Babicc67bee12010-02-05 15:11:27 +0100527int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500528{
Stefano Babicc67bee12010-02-05 15:11:27 +0100529 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500530 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000531 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500532
Stefano Babicc67bee12010-02-05 15:11:27 +0100533 if (!cfg)
534 return -1;
535
Andy Fleming50586ef2008-10-30 16:47:16 -0500536 mmc = malloc(sizeof(struct mmc));
537
Jason Liu46927082011-11-25 00:18:04 +0000538 sprintf(mmc->name, "FSL_SDHC");
Stefano Babicc67bee12010-02-05 15:11:27 +0100539 regs = (struct fsl_esdhc *)cfg->esdhc_base;
540
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500541 /* First reset the eSDHC controller */
542 esdhc_reset(regs);
543
Jerry Huang975324a2012-05-17 23:57:02 +0000544 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
545 | SYSCTL_IPGEN | SYSCTL_CKEN);
546
Stefano Babicc67bee12010-02-05 15:11:27 +0100547 mmc->priv = cfg;
Andy Fleming50586ef2008-10-30 16:47:16 -0500548 mmc->send_cmd = esdhc_send_cmd;
549 mmc->set_ios = esdhc_set_ios;
550 mmc->init = esdhc_init;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000551 mmc->getcd = esdhc_getcd;
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +0000552 mmc->getwp = NULL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500553
Li Yang030955c2010-11-25 17:06:09 +0000554 voltage_caps = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500555 caps = regs->hostcapblt;
Roy Zang3b4456e2011-01-07 00:06:47 -0600556
557#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
558 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
559 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
560#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500561 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000562 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500563 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000564 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500565 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000566 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
567
568#ifdef CONFIG_SYS_SD_VOLTAGE
569 mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
570#else
571 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
572#endif
573 if ((mmc->voltages & voltage_caps) == 0) {
574 printf("voltage not supported by controller\n");
575 return -1;
576 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500577
Shawn Guofb8302b2012-12-30 14:14:58 +0000578 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Andy Fleming50586ef2008-10-30 16:47:16 -0500579
Abbas Razaaad46592013-03-25 09:13:34 +0000580 if (cfg->max_bus_width > 0) {
581 if (cfg->max_bus_width < 8)
582 mmc->host_caps &= ~MMC_MODE_8BIT;
583 if (cfg->max_bus_width < 4)
584 mmc->host_caps &= ~MMC_MODE_4BIT;
585 }
586
Andy Fleming50586ef2008-10-30 16:47:16 -0500587 if (caps & ESDHC_HOSTCAPBLT_HSS)
588 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
589
590 mmc->f_min = 400000;
Simon Glasse9adeca2012-12-13 20:49:05 +0000591 mmc->f_max = MIN(gd->arch.sdhc_clk, 52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500592
Fabio Estevam1ed60d72011-05-12 09:33:27 +0000593 mmc->b_max = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500594 mmc_register(mmc);
595
596 return 0;
597}
598
599int fsl_esdhc_mmc_init(bd_t *bis)
600{
Stefano Babicc67bee12010-02-05 15:11:27 +0100601 struct fsl_esdhc_cfg *cfg;
602
Fabio Estevam88227a12012-12-27 08:51:08 +0000603 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100604 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000605 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100606 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500607}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400608
Stefano Babicc67bee12010-02-05 15:11:27 +0100609#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400610void fdt_fixup_esdhc(void *blob, bd_t *bd)
611{
612 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400613
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800614#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400615 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800616 do_fixup_by_compat(blob, compat, "status", "disabled",
617 8 + 1, 1);
618 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400619 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800620#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400621
622 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000623 gd->arch.sdhc_clk, 1);
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800624
625 do_fixup_by_compat(blob, compat, "status", "okay",
626 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400627}
Stefano Babicc67bee12010-02-05 15:11:27 +0100628#endif