blob: c5e270dd57df6ba67b4a4c71ff08b9d7705d375e [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
Ye.Li3d46c312014-11-04 15:35:49 +080026#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
27 IRQSTATEN_CINT | \
28 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
29 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
30 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
31 IRQSTATEN_DINT)
32
Andy Fleminge52ffb82008-10-30 16:47:16 -050033struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080034 uint dsaddr; /* SDMA system address register */
35 uint blkattr; /* Block attributes register */
36 uint cmdarg; /* Command argument register */
37 uint xfertyp; /* Transfer type register */
38 uint cmdrsp0; /* Command response 0 register */
39 uint cmdrsp1; /* Command response 1 register */
40 uint cmdrsp2; /* Command response 2 register */
41 uint cmdrsp3; /* Command response 3 register */
42 uint datport; /* Buffer data port register */
43 uint prsstat; /* Present state register */
44 uint proctl; /* Protocol control register */
45 uint sysctl; /* System Control Register */
46 uint irqstat; /* Interrupt status register */
47 uint irqstaten; /* Interrupt status enable register */
48 uint irqsigen; /* Interrupt signal enable register */
49 uint autoc12err; /* Auto CMD error status register */
50 uint hostcapblt; /* Host controller capabilities register */
51 uint wml; /* Watermark level register */
52 uint mixctrl; /* For USDHC */
53 char reserved1[4]; /* reserved */
54 uint fevt; /* Force event register */
55 uint admaes; /* ADMA error status register */
56 uint adsaddr; /* ADMA system address register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020057 char reserved2[100]; /* reserved */
58 uint vendorspec; /* Vendor Specific register */
59 char reserved3[59]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080060 uint hostver; /* Host controller version register */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080061 char reserved4[4]; /* reserved */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020062 uint dmaerraddr; /* DMA error address register */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080063 char reserved5[4]; /* reserved */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020064 uint dmaerrattr; /* DMA error attribute register */
65 char reserved6[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080066 uint hostcapblt2; /* Host controller capabilities register 2 */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020067 char reserved7[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080068 uint tcr; /* Tuning control register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020069 char reserved8[28]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080070 uint sddirctl; /* SD direction control register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020071 char reserved9[712]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080072 uint scr; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050073};
74
75/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +000076static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -050077{
78 uint xfertyp = 0;
79
80 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +053081 xfertyp |= XFERTYP_DPSEL;
82#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
83 xfertyp |= XFERTYP_DMAEN;
84#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050085 if (data->blocks > 1) {
86 xfertyp |= XFERTYP_MSBSEL;
87 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -060088#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
89 xfertyp |= XFERTYP_AC12EN;
90#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050091 }
92
93 if (data->flags & MMC_DATA_READ)
94 xfertyp |= XFERTYP_DTDSEL;
95 }
96
97 if (cmd->resp_type & MMC_RSP_CRC)
98 xfertyp |= XFERTYP_CCCEN;
99 if (cmd->resp_type & MMC_RSP_OPCODE)
100 xfertyp |= XFERTYP_CICEN;
101 if (cmd->resp_type & MMC_RSP_136)
102 xfertyp |= XFERTYP_RSPTYP_136;
103 else if (cmd->resp_type & MMC_RSP_BUSY)
104 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
105 else if (cmd->resp_type & MMC_RSP_PRESENT)
106 xfertyp |= XFERTYP_RSPTYP_48;
107
Wang Huanc9292132014-09-05 13:52:40 +0800108#if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || defined(CONFIG_LS102XA)
Jason Liubef0ff02011-03-22 01:32:31 +0000109 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
110 xfertyp |= XFERTYP_CMDTYP_ABORT;
111#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500112 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
113}
114
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530115#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
116/*
117 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
118 */
Wolfgang Denka40545c2010-05-09 23:52:59 +0200119static void
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530120esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
121{
Ira Snyder66a722e2011-12-23 08:30:40 +0000122 struct fsl_esdhc_cfg *cfg = mmc->priv;
123 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530124 uint blocks;
125 char *buffer;
126 uint databuf;
127 uint size;
128 uint irqstat;
129 uint timeout;
130
131 if (data->flags & MMC_DATA_READ) {
132 blocks = data->blocks;
133 buffer = data->dest;
134 while (blocks) {
135 timeout = PIO_TIMEOUT;
136 size = data->blocksize;
137 irqstat = esdhc_read32(&regs->irqstat);
138 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
139 && --timeout);
140 if (timeout <= 0) {
141 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200142 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530143 }
144 while (size && (!(irqstat & IRQSTAT_TC))) {
145 udelay(100); /* Wait before last byte transfer complete */
146 irqstat = esdhc_read32(&regs->irqstat);
147 databuf = in_le32(&regs->datport);
148 *((uint *)buffer) = databuf;
149 buffer += 4;
150 size -= 4;
151 }
152 blocks--;
153 }
154 } else {
155 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200156 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530157 while (blocks) {
158 timeout = PIO_TIMEOUT;
159 size = data->blocksize;
160 irqstat = esdhc_read32(&regs->irqstat);
161 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
162 && --timeout);
163 if (timeout <= 0) {
164 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200165 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530166 }
167 while (size && (!(irqstat & IRQSTAT_TC))) {
168 udelay(100); /* Wait before last byte transfer complete */
169 databuf = *((uint *)buffer);
170 buffer += 4;
171 size -= 4;
172 irqstat = esdhc_read32(&regs->irqstat);
173 out_le32(&regs->datport, databuf);
174 }
175 blocks--;
176 }
177 }
178}
179#endif
180
Andy Fleminge52ffb82008-10-30 16:47:16 -0500181static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
182{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500183 int timeout;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200184 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100185 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Ye.Li33a56b12014-02-20 18:00:57 +0800186
Wolfgang Denka40545c2010-05-09 23:52:59 +0200187 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500188
189 wml_value = data->blocksize/4;
190
191 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530192 if (wml_value > WML_RD_WML_MAX)
193 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500194
Roy Zange5853af2010-02-09 18:23:33 +0800195 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800196#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100197 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800198#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500199 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800200#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000201 flush_dcache_range((ulong)data->src,
202 (ulong)data->src+data->blocks
203 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800204#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530205 if (wml_value > WML_WR_WML_MAX)
206 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100207 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500208 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
209 return TIMEOUT;
210 }
Roy Zange5853af2010-02-09 18:23:33 +0800211
212 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
213 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800214#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100215 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800216#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500217 }
218
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100219 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500220
221 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530222 /*
223 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
224 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
225 * So, Number of SD Clock cycles for 0.25sec should be minimum
226 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500227 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530228 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500229 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530230 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500231 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530232 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500233 * => timeout + 13 = log2(mmc->clock/4) + 1
234 * => timeout + 13 = fls(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530235 */
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500236 timeout = fls(mmc->clock/4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500237 timeout -= 13;
238
239 if (timeout > 14)
240 timeout = 14;
241
242 if (timeout < 0)
243 timeout = 0;
244
Kumar Gala9a878d52011-01-29 15:36:10 -0600245#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
246 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
247 timeout++;
248#endif
249
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800250#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
251 timeout = 0xE;
252#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100253 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500254
255 return 0;
256}
257
Tom Rini239dd252014-05-23 09:19:05 -0400258#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000259static void check_and_invalidate_dcache_range
260 (struct mmc_cmd *cmd,
261 struct mmc_data *data) {
262 unsigned start = (unsigned)data->dest ;
263 unsigned size = roundup(ARCH_DMA_MINALIGN,
264 data->blocks*data->blocksize);
265 unsigned end = start+size ;
266 invalidate_dcache_range(start, end);
267}
Tom Rini239dd252014-05-23 09:19:05 -0400268#endif
269
Andy Fleminge52ffb82008-10-30 16:47:16 -0500270/*
271 * Sends a command out on the bus. Takes the mmc pointer,
272 * a command pointer, and an optional data pointer.
273 */
274static int
275esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
276{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500277 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500278 uint xfertyp;
279 uint irqstat;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200280 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100281 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500282
Jerry Huanged413672011-01-06 23:42:19 -0600283#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
284 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
285 return 0;
286#endif
287
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100288 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500289
290 sync();
291
292 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100293 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
294 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
295 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500296
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100297 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
298 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500299
300 /* Wait at least 8 SD clock cycles before the next command */
301 /*
302 * Note: This is way more than 8 cycles, but 1ms seems to
303 * resolve timing issues with some cards
304 */
305 udelay(1000);
306
307 /* Set up for a data transfer if we have one */
308 if (data) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500309 err = esdhc_setup_data(mmc, data);
310 if(err)
311 return err;
312 }
313
314 /* Figure out the transfer arguments */
315 xfertyp = esdhc_xfertyp(cmd, data);
316
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500317 /* Mask all irqs */
318 esdhc_write32(&regs->irqsigen, 0);
319
Andy Fleminge52ffb82008-10-30 16:47:16 -0500320 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100321 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000322#if defined(CONFIG_FSL_USDHC)
323 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500324 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
325 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu9919d642011-11-25 00:18:04 +0000326 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
327#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100328 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000329#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000330
Andy Fleminge52ffb82008-10-30 16:47:16 -0500331 /* Wait for the command to complete */
Dirk Behmed8552d62012-03-26 03:13:05 +0000332 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100333 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500334
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100335 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500336
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500337 if (irqstat & CMD_ERR) {
338 err = COMM_ERR;
339 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000340 }
341
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500342 if (irqstat & IRQSTAT_CTOE) {
343 err = TIMEOUT;
344 goto out;
345 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500346
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200347 /* Switch voltage to 1.8V if CMD11 succeeded */
348 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
349 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
350
351 printf("Run CMD11 1.8V switch\n");
352 /* Sleep for 5 ms - max time for card to switch to 1.8V */
353 udelay(5000);
354 }
355
Dirk Behmed8552d62012-03-26 03:13:05 +0000356 /* Workaround for ESDHC errata ENGcm03648 */
357 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
358 int timeout = 2500;
359
360 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
361 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
362 PRSSTAT_DAT0)) {
363 udelay(100);
364 timeout--;
365 }
366
367 if (timeout <= 0) {
368 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500369 err = TIMEOUT;
370 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000371 }
372 }
373
Andy Fleminge52ffb82008-10-30 16:47:16 -0500374 /* Copy the response to the response buffer */
375 if (cmd->resp_type & MMC_RSP_136) {
376 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
377
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100378 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
379 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
380 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
381 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530382 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
383 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
384 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
385 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500386 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100387 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500388
389 /* Wait until all of the blocks are transferred */
390 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530391#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
392 esdhc_pio_read_write(mmc, data);
393#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500394 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100395 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500396
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500397 if (irqstat & IRQSTAT_DTOE) {
398 err = TIMEOUT;
399 goto out;
400 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000401
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500402 if (irqstat & DATA_ERR) {
403 err = COMM_ERR;
404 goto out;
405 }
Andrew Gabbasov4a929622013-04-07 23:06:08 +0000406 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800407
Eric Nelson70e68692013-04-03 12:31:56 +0000408 if (data->flags & MMC_DATA_READ)
409 check_and_invalidate_dcache_range(cmd, data);
Ye.Li33a56b12014-02-20 18:00:57 +0800410#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500411 }
412
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500413out:
414 /* Reset CMD and DATA portions on error */
415 if (err) {
416 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
417 SYSCTL_RSTC);
418 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
419 ;
420
421 if (data) {
422 esdhc_write32(&regs->sysctl,
423 esdhc_read32(&regs->sysctl) |
424 SYSCTL_RSTD);
425 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
426 ;
427 }
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200428
429 /* If this was CMD11, then notify that power cycle is needed */
430 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
431 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500432 }
433
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100434 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500435
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500436 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500437}
438
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000439static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500440{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500441 int div, pre_div;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200442 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100443 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeauc58ff342012-10-01 08:36:25 +0000444 int sdhc_clk = cfg->sdhc_clk;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500445 uint clk;
446
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200447 if (clock < mmc->cfg->f_min)
448 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100449
Andy Fleminge52ffb82008-10-30 16:47:16 -0500450 if (sdhc_clk / 16 > clock) {
451 for (pre_div = 2; pre_div < 256; pre_div *= 2)
452 if ((sdhc_clk / pre_div) <= (clock * 16))
453 break;
454 } else
455 pre_div = 2;
456
457 for (div = 1; div <= 16; div++)
458 if ((sdhc_clk / (div * pre_div)) <= clock)
459 break;
460
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500461 pre_div >>= mmc->ddr_mode ? 2 : 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500462 div -= 1;
463
464 clk = (pre_div << 8) | (div << 4);
465
Kumar Gala09876a32010-03-18 15:51:05 -0500466 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100467
468 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500469
470 udelay(10000);
471
Kumar Gala09876a32010-03-18 15:51:05 -0500472 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100473
474 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475}
476
477static void esdhc_set_ios(struct mmc *mmc)
478{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200479 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100480 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500481
482 /* Set the clock speed */
483 set_sysctl(mmc, mmc->clock);
484
485 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100486 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500487
488 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100489 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500490 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100491 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
492
Andy Fleminge52ffb82008-10-30 16:47:16 -0500493}
494
495static int esdhc_init(struct mmc *mmc)
496{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200497 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100498 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500499 int timeout = 1000;
500
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100501 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200502 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100503
504 /* Wait until the controller is available */
505 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
506 udelay(1000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500507
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000508#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530509 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000510 esdhc_write32(&regs->scr, 0x00000040);
511#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530512
Dirk Behmedbe67252013-07-15 15:44:29 +0200513 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500514
515 /* Set the initial clock speed */
Jerry Huang0caea1a2010-11-25 17:06:07 +0000516 mmc_set_clock(mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500517
518 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100519 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500520
521 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100522 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500523
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100524 /* Set timout to the maximum value */
525 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500526
Otavio Salvador12b2a872015-02-17 10:42:44 -0200527#ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
528 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
529#endif
530
Thierry Reding8cee4c982012-01-02 01:15:38 +0000531 return 0;
532}
533
534static int esdhc_getcd(struct mmc *mmc)
535{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200536 struct fsl_esdhc_cfg *cfg = mmc->priv;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000537 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
538 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500539
Haijun.Zhang05f58542014-01-10 13:52:17 +0800540#ifdef CONFIG_ESDHC_DETECT_QUIRK
541 if (CONFIG_ESDHC_DETECT_QUIRK)
542 return 1;
543#endif
Thierry Reding8cee4c982012-01-02 01:15:38 +0000544 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
545 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100546
Thierry Reding8cee4c982012-01-02 01:15:38 +0000547 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500548}
549
Jerry Huangb7ef7562010-03-18 15:57:06 -0500550static void esdhc_reset(struct fsl_esdhc *regs)
551{
552 unsigned long timeout = 100; /* wait max 100 ms */
553
554 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200555 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500556
557 /* hardware clears the bit when it is done */
558 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
559 udelay(1000);
560 if (!timeout)
561 printf("MMC/SD: Reset never completed.\n");
562}
563
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200564static const struct mmc_ops esdhc_ops = {
565 .send_cmd = esdhc_send_cmd,
566 .set_ios = esdhc_set_ios,
567 .init = esdhc_init,
568 .getcd = esdhc_getcd,
569};
570
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100571int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500572{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100573 struct fsl_esdhc *regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500574 struct mmc *mmc;
Li Yangd4933f22010-11-25 17:06:09 +0000575 u32 caps, voltage_caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500576
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100577 if (!cfg)
578 return -1;
579
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100580 regs = (struct fsl_esdhc *)cfg->esdhc_base;
581
Jerry Huangb7ef7562010-03-18 15:57:06 -0500582 /* First reset the eSDHC controller */
583 esdhc_reset(regs);
584
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000585 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
586 | SYSCTL_IPGEN | SYSCTL_CKEN);
587
Ye.Li3d46c312014-11-04 15:35:49 +0800588 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200589 memset(&cfg->cfg, 0, sizeof(cfg->cfg));
590
Li Yangd4933f22010-11-25 17:06:09 +0000591 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +0800592 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600593
594#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
595 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
596 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
597#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800598
599/* T4240 host controller capabilities register should have VS33 bit */
600#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
601 caps = caps | ESDHC_HOSTCAPBLT_VS33;
602#endif
603
Andy Fleminge52ffb82008-10-30 16:47:16 -0500604 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000605 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500606 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000607 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500608 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000609 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
610
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200611 cfg->cfg.name = "FSL_SDHC";
612 cfg->cfg.ops = &esdhc_ops;
Li Yangd4933f22010-11-25 17:06:09 +0000613#ifdef CONFIG_SYS_SD_VOLTAGE
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200614 cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +0000615#else
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200616 cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000617#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200618 if ((cfg->cfg.voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +0000619 printf("voltage not supported by controller\n");
620 return -1;
621 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500622
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200623 cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500624#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
625 cfg->cfg.host_caps |= MMC_MODE_DDR_52MHz;
626#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500627
Abbas Razae6bf9772013-03-25 09:13:34 +0000628 if (cfg->max_bus_width > 0) {
629 if (cfg->max_bus_width < 8)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200630 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000631 if (cfg->max_bus_width < 4)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200632 cfg->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000633 }
634
Andy Fleminge52ffb82008-10-30 16:47:16 -0500635 if (caps & ESDHC_HOSTCAPBLT_HSS)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200636 cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500637
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800638#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
639 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200640 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800641#endif
642
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200643 cfg->cfg.f_min = 400000;
Tom Rini2907a302014-11-26 11:22:29 -0500644 cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500645
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200646 cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
647
648 mmc = mmc_create(&cfg->cfg, cfg);
649 if (mmc == NULL)
650 return -1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500651
652 return 0;
653}
654
655int fsl_esdhc_mmc_init(bd_t *bis)
656{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100657 struct fsl_esdhc_cfg *cfg;
658
Fabio Estevam6592a992012-12-27 08:51:08 +0000659 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100660 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000661 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100662 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500663}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400664
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100665#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400666void fdt_fixup_esdhc(void *blob, bd_t *bd)
667{
668 const char *compat = "fsl,esdhc";
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400669
Chenhui Zhao025eab02011-01-04 17:23:05 +0800670#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400671 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800672 do_fixup_by_compat(blob, compat, "status", "disabled",
673 8 + 1, 1);
674 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400675 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800676#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400677
678 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000679 gd->arch.sdhc_clk, 1);
Chenhui Zhao025eab02011-01-04 17:23:05 +0800680
681 do_fixup_by_compat(blob, compat, "status", "okay",
682 4 + 1, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400683}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100684#endif