blob: 4c3b93d413dec99e361338fd8d7a6c360a54981b [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 {
Haijun.Zhang511948b2013-10-30 11:37:55 +080027 uint dsaddr; /* SDMA system address register */
28 uint blkattr; /* Block attributes register */
29 uint cmdarg; /* Command argument register */
30 uint xfertyp; /* Transfer type register */
31 uint cmdrsp0; /* Command response 0 register */
32 uint cmdrsp1; /* Command response 1 register */
33 uint cmdrsp2; /* Command response 2 register */
34 uint cmdrsp3; /* Command response 3 register */
35 uint datport; /* Buffer data port register */
36 uint prsstat; /* Present state register */
37 uint proctl; /* Protocol control register */
38 uint sysctl; /* System Control Register */
39 uint irqstat; /* Interrupt status register */
40 uint irqstaten; /* Interrupt status enable register */
41 uint irqsigen; /* Interrupt signal enable register */
42 uint autoc12err; /* Auto CMD error status register */
43 uint hostcapblt; /* Host controller capabilities register */
44 uint wml; /* Watermark level register */
45 uint mixctrl; /* For USDHC */
46 char reserved1[4]; /* reserved */
47 uint fevt; /* Force event register */
48 uint admaes; /* ADMA error status register */
49 uint adsaddr; /* ADMA system address register */
50 char reserved2[160]; /* reserved */
51 uint hostver; /* Host controller version register */
52 char reserved3[4]; /* reserved */
53 uint dmaerraddr; /* DMA error address register */
54 char reserved4[4]; /* reserved */
55 uint dmaerrattr; /* DMA error attribute register */
56 char reserved5[4]; /* reserved */
57 uint hostcapblt2; /* Host controller capabilities register 2 */
58 char reserved6[8]; /* reserved */
59 uint tcr; /* Tuning control register */
60 char reserved7[28]; /* reserved */
61 uint sddirctl; /* SD direction control register */
62 char reserved8[712]; /* reserved */
63 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050064};
65
66/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +000067static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -050068{
69 uint xfertyp = 0;
70
71 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +053072 xfertyp |= XFERTYP_DPSEL;
73#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
74 xfertyp |= XFERTYP_DMAEN;
75#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050076 if (data->blocks > 1) {
77 xfertyp |= XFERTYP_MSBSEL;
78 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -060079#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
80 xfertyp |= XFERTYP_AC12EN;
81#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050082 }
83
84 if (data->flags & MMC_DATA_READ)
85 xfertyp |= XFERTYP_DTDSEL;
86 }
87
88 if (cmd->resp_type & MMC_RSP_CRC)
89 xfertyp |= XFERTYP_CCCEN;
90 if (cmd->resp_type & MMC_RSP_OPCODE)
91 xfertyp |= XFERTYP_CICEN;
92 if (cmd->resp_type & MMC_RSP_136)
93 xfertyp |= XFERTYP_RSPTYP_136;
94 else if (cmd->resp_type & MMC_RSP_BUSY)
95 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
96 else if (cmd->resp_type & MMC_RSP_PRESENT)
97 xfertyp |= XFERTYP_RSPTYP_48;
98
Haijun.Zhangb8e5b072013-07-01 14:26:01 +080099#if defined(CONFIG_MX53) || defined(CONFIG_T4240QDS)
Jason Liu4571de32011-03-22 01:32:31 +0000100 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
101 xfertyp |= XFERTYP_CMDTYP_ABORT;
102#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500103 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
104}
105
Dipen Dudhat77c14582009-10-05 15:41:58 +0530106#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
107/*
108 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
109 */
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200110static void
Dipen Dudhat77c14582009-10-05 15:41:58 +0530111esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
112{
Ira Snyder8eee2bd2011-12-23 08:30:40 +0000113 struct fsl_esdhc_cfg *cfg = mmc->priv;
114 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530115 uint blocks;
116 char *buffer;
117 uint databuf;
118 uint size;
119 uint irqstat;
120 uint timeout;
121
122 if (data->flags & MMC_DATA_READ) {
123 blocks = data->blocks;
124 buffer = data->dest;
125 while (blocks) {
126 timeout = PIO_TIMEOUT;
127 size = data->blocksize;
128 irqstat = esdhc_read32(&regs->irqstat);
129 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
130 && --timeout);
131 if (timeout <= 0) {
132 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200133 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530134 }
135 while (size && (!(irqstat & IRQSTAT_TC))) {
136 udelay(100); /* Wait before last byte transfer complete */
137 irqstat = esdhc_read32(&regs->irqstat);
138 databuf = in_le32(&regs->datport);
139 *((uint *)buffer) = databuf;
140 buffer += 4;
141 size -= 4;
142 }
143 blocks--;
144 }
145 } else {
146 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200147 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530148 while (blocks) {
149 timeout = PIO_TIMEOUT;
150 size = data->blocksize;
151 irqstat = esdhc_read32(&regs->irqstat);
152 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
153 && --timeout);
154 if (timeout <= 0) {
155 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200156 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530157 }
158 while (size && (!(irqstat & IRQSTAT_TC))) {
159 udelay(100); /* Wait before last byte transfer complete */
160 databuf = *((uint *)buffer);
161 buffer += 4;
162 size -= 4;
163 irqstat = esdhc_read32(&regs->irqstat);
164 out_le32(&regs->datport, databuf);
165 }
166 blocks--;
167 }
168 }
169}
170#endif
171
Andy Fleming50586ef2008-10-30 16:47:16 -0500172static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
173{
Andy Fleming50586ef2008-10-30 16:47:16 -0500174 int timeout;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200175 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100176 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Fabio Estevamc2137b12013-05-28 15:09:42 -0300177#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200178 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500179
180 wml_value = data->blocksize/4;
181
182 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530183 if (wml_value > WML_RD_WML_MAX)
184 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500185
Roy Zangab467c52010-02-09 18:23:33 +0800186 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Stefano Babicc67bee12010-02-05 15:11:27 +0100187 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Andy Fleming50586ef2008-10-30 16:47:16 -0500188 } else {
Eric Nelsone576bd92012-04-25 14:28:48 +0000189 flush_dcache_range((ulong)data->src,
190 (ulong)data->src+data->blocks
191 *data->blocksize);
192
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530193 if (wml_value > WML_WR_WML_MAX)
194 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100195 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500196 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
197 return TIMEOUT;
198 }
Roy Zangab467c52010-02-09 18:23:33 +0800199
200 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
201 wml_value << 16);
Stefano Babicc67bee12010-02-05 15:11:27 +0100202 esdhc_write32(&regs->dsaddr, (u32)data->src);
Andy Fleming50586ef2008-10-30 16:47:16 -0500203 }
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200204#else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
205 if (!(data->flags & MMC_DATA_READ)) {
206 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
207 printf("\nThe SD card is locked. "
208 "Can not write to a locked card.\n\n");
209 return TIMEOUT;
210 }
211 esdhc_write32(&regs->dsaddr, (u32)data->src);
212 } else
213 esdhc_write32(&regs->dsaddr, (u32)data->dest);
214#endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
Andy Fleming50586ef2008-10-30 16:47:16 -0500215
Stefano Babicc67bee12010-02-05 15:11:27 +0100216 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500217
218 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530219 /*
220 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
221 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
222 * So, Number of SD Clock cycles for 0.25sec should be minimum
223 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500224 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530225 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500226 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530227 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500228 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530229 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500230 * => timeout + 13 = log2(mmc->clock/4) + 1
231 * => timeout + 13 = fls(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530232 */
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500233 timeout = fls(mmc->clock/4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500234 timeout -= 13;
235
236 if (timeout > 14)
237 timeout = 14;
238
239 if (timeout < 0)
240 timeout = 0;
241
Kumar Gala5103a032011-01-29 15:36:10 -0600242#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
243 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
244 timeout++;
245#endif
246
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800247#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
248 timeout = 0xE;
249#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100250 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500251
252 return 0;
253}
254
Eric Nelsone576bd92012-04-25 14:28:48 +0000255static void check_and_invalidate_dcache_range
256 (struct mmc_cmd *cmd,
257 struct mmc_data *data) {
258 unsigned start = (unsigned)data->dest ;
259 unsigned size = roundup(ARCH_DMA_MINALIGN,
260 data->blocks*data->blocksize);
261 unsigned end = start+size ;
262 invalidate_dcache_range(start, end);
263}
Andy Fleming50586ef2008-10-30 16:47:16 -0500264/*
265 * Sends a command out on the bus. Takes the mmc pointer,
266 * a command pointer, and an optional data pointer.
267 */
268static int
269esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
270{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500271 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500272 uint xfertyp;
273 uint irqstat;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200274 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100275 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) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500303 err = esdhc_setup_data(mmc, data);
304 if(err)
305 return err;
306 }
307
308 /* Figure out the transfer arguments */
309 xfertyp = esdhc_xfertyp(cmd, data);
310
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500311 /* Mask all irqs */
312 esdhc_write32(&regs->irqsigen, 0);
313
Andy Fleming50586ef2008-10-30 16:47:16 -0500314 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100315 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000316#if defined(CONFIG_FSL_USDHC)
317 esdhc_write32(&regs->mixctrl,
318 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
319 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
320#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100321 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000322#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000323
Andy Fleming50586ef2008-10-30 16:47:16 -0500324 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000325 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100326 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500327
Stefano Babicc67bee12010-02-05 15:11:27 +0100328 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500329
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500330 if (irqstat & CMD_ERR) {
331 err = COMM_ERR;
332 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000333 }
334
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500335 if (irqstat & IRQSTAT_CTOE) {
336 err = TIMEOUT;
337 goto out;
338 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500339
Dirk Behme7a5b8022012-03-26 03:13:05 +0000340 /* Workaround for ESDHC errata ENGcm03648 */
341 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
342 int timeout = 2500;
343
344 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
345 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
346 PRSSTAT_DAT0)) {
347 udelay(100);
348 timeout--;
349 }
350
351 if (timeout <= 0) {
352 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500353 err = TIMEOUT;
354 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000355 }
356 }
357
Andy Fleming50586ef2008-10-30 16:47:16 -0500358 /* Copy the response to the response buffer */
359 if (cmd->resp_type & MMC_RSP_136) {
360 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
361
Stefano Babicc67bee12010-02-05 15:11:27 +0100362 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
363 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
364 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
365 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530366 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
367 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
368 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
369 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500370 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100371 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500372
373 /* Wait until all of the blocks are transferred */
374 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530375#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
376 esdhc_pio_read_write(mmc, data);
377#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500378 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100379 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500380
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500381 if (irqstat & IRQSTAT_DTOE) {
382 err = TIMEOUT;
383 goto out;
384 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000385
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500386 if (irqstat & DATA_ERR) {
387 err = COMM_ERR;
388 goto out;
389 }
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000390 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530391#endif
Eric Nelson54899fc2013-04-03 12:31:56 +0000392 if (data->flags & MMC_DATA_READ)
393 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500394 }
395
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500396out:
397 /* Reset CMD and DATA portions on error */
398 if (err) {
399 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
400 SYSCTL_RSTC);
401 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
402 ;
403
404 if (data) {
405 esdhc_write32(&regs->sysctl,
406 esdhc_read32(&regs->sysctl) |
407 SYSCTL_RSTD);
408 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
409 ;
410 }
411 }
412
Stefano Babicc67bee12010-02-05 15:11:27 +0100413 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500414
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500415 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500416}
417
Kim Phillipseafa90a2012-10-29 13:34:44 +0000418static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500419{
Andy Fleming50586ef2008-10-30 16:47:16 -0500420 int div, pre_div;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200421 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100422 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeaua2ac1b32012-10-01 08:36:25 +0000423 int sdhc_clk = cfg->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500424 uint clk;
425
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200426 if (clock < mmc->cfg->f_min)
427 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100428
Andy Fleming50586ef2008-10-30 16:47:16 -0500429 if (sdhc_clk / 16 > clock) {
430 for (pre_div = 2; pre_div < 256; pre_div *= 2)
431 if ((sdhc_clk / pre_div) <= (clock * 16))
432 break;
433 } else
434 pre_div = 2;
435
436 for (div = 1; div <= 16; div++)
437 if ((sdhc_clk / (div * pre_div)) <= clock)
438 break;
439
440 pre_div >>= 1;
441 div -= 1;
442
443 clk = (pre_div << 8) | (div << 4);
444
Kumar Galacc4d1222010-03-18 15:51:05 -0500445 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100446
447 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500448
449 udelay(10000);
450
Kumar Galacc4d1222010-03-18 15:51:05 -0500451 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicc67bee12010-02-05 15:11:27 +0100452
453 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500454}
455
456static void esdhc_set_ios(struct mmc *mmc)
457{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200458 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100459 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500460
461 /* Set the clock speed */
462 set_sysctl(mmc, mmc->clock);
463
464 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100465 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500466
467 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100468 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500469 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100470 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
471
Andy Fleming50586ef2008-10-30 16:47:16 -0500472}
473
474static int esdhc_init(struct mmc *mmc)
475{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200476 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100477 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500478 int timeout = 1000;
479
Stefano Babicc67bee12010-02-05 15:11:27 +0100480 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200481 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100482
483 /* Wait until the controller is available */
484 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
485 udelay(1000);
486
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000487#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530488 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000489 esdhc_write32(&regs->scr, 0x00000040);
490#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530491
Dirk Behmea61da722013-07-15 15:44:29 +0200492 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500493
494 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000495 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500496
497 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100498 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500499
500 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100501 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500502
Stefano Babicc67bee12010-02-05 15:11:27 +0100503 /* Set timout to the maximum value */
504 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500505
Thierry Redingd48d2e22012-01-02 01:15:38 +0000506 return 0;
507}
Andy Fleming50586ef2008-10-30 16:47:16 -0500508
Thierry Redingd48d2e22012-01-02 01:15:38 +0000509static int esdhc_getcd(struct mmc *mmc)
510{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200511 struct fsl_esdhc_cfg *cfg = mmc->priv;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000512 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
513 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100514
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800515#ifdef CONFIG_ESDHC_DETECT_QUIRK
516 if (CONFIG_ESDHC_DETECT_QUIRK)
517 return 1;
518#endif
Thierry Redingd48d2e22012-01-02 01:15:38 +0000519 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
520 udelay(1000);
521
522 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500523}
524
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500525static void esdhc_reset(struct fsl_esdhc *regs)
526{
527 unsigned long timeout = 100; /* wait max 100 ms */
528
529 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200530 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500531
532 /* hardware clears the bit when it is done */
533 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
534 udelay(1000);
535 if (!timeout)
536 printf("MMC/SD: Reset never completed.\n");
537}
538
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200539static const struct mmc_ops esdhc_ops = {
540 .send_cmd = esdhc_send_cmd,
541 .set_ios = esdhc_set_ios,
542 .init = esdhc_init,
543 .getcd = esdhc_getcd,
544};
545
Stefano Babicc67bee12010-02-05 15:11:27 +0100546int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500547{
Stefano Babicc67bee12010-02-05 15:11:27 +0100548 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500549 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000550 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500551
Stefano Babicc67bee12010-02-05 15:11:27 +0100552 if (!cfg)
553 return -1;
554
Stefano Babicc67bee12010-02-05 15:11:27 +0100555 regs = (struct fsl_esdhc *)cfg->esdhc_base;
556
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500557 /* First reset the eSDHC controller */
558 esdhc_reset(regs);
559
Jerry Huang975324a2012-05-17 23:57:02 +0000560 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
561 | SYSCTL_IPGEN | SYSCTL_CKEN);
562
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200563 memset(&cfg->cfg, 0, sizeof(cfg->cfg));
564
Li Yang030955c2010-11-25 17:06:09 +0000565 voltage_caps = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500566 caps = regs->hostcapblt;
Roy Zang3b4456e2011-01-07 00:06:47 -0600567
568#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
569 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
570 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
571#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800572
573/* T4240 host controller capabilities register should have VS33 bit */
574#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
575 caps = caps | ESDHC_HOSTCAPBLT_VS33;
576#endif
577
Andy Fleming50586ef2008-10-30 16:47:16 -0500578 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000579 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500580 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000581 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500582 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000583 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
584
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200585 cfg->cfg.name = "FSL_SDHC";
586 cfg->cfg.ops = &esdhc_ops;
Li Yang030955c2010-11-25 17:06:09 +0000587#ifdef CONFIG_SYS_SD_VOLTAGE
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200588 cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000589#else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200590 cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000591#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200592 if ((cfg->cfg.voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000593 printf("voltage not supported by controller\n");
594 return -1;
595 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500596
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200597 cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Andy Fleming50586ef2008-10-30 16:47:16 -0500598
Abbas Razaaad46592013-03-25 09:13:34 +0000599 if (cfg->max_bus_width > 0) {
600 if (cfg->max_bus_width < 8)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200601 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000602 if (cfg->max_bus_width < 4)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200603 cfg->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000604 }
605
Andy Fleming50586ef2008-10-30 16:47:16 -0500606 if (caps & ESDHC_HOSTCAPBLT_HSS)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200607 cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500608
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800609#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
610 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200611 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800612#endif
613
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200614 cfg->cfg.f_min = 400000;
615 cfg->cfg.f_max = MIN(gd->arch.sdhc_clk, 52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500616
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200617 cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
618
619 mmc = mmc_create(&cfg->cfg, cfg);
620 if (mmc == NULL)
621 return -1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500622
623 return 0;
624}
625
626int fsl_esdhc_mmc_init(bd_t *bis)
627{
Stefano Babicc67bee12010-02-05 15:11:27 +0100628 struct fsl_esdhc_cfg *cfg;
629
Fabio Estevam88227a12012-12-27 08:51:08 +0000630 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100631 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000632 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100633 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500634}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400635
Stefano Babicc67bee12010-02-05 15:11:27 +0100636#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400637void fdt_fixup_esdhc(void *blob, bd_t *bd)
638{
639 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400640
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800641#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400642 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800643 do_fixup_by_compat(blob, compat, "status", "disabled",
644 8 + 1, 1);
645 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400646 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800647#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400648
649 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000650 gd->arch.sdhc_clk, 1);
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800651
652 do_fixup_by_compat(blob, compat, "status", "okay",
653 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400654}
Stefano Babicc67bee12010-02-05 15:11:27 +0100655#endif