blob: f5d2ccba159f110667f30e8a25d7591a2825d34c [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 */
57 char reserved2[160]; /* reserved */
58 uint hostver; /* Host controller version register */
59 char reserved3[4]; /* reserved */
60 uint dmaerraddr; /* DMA error address register */
61 char reserved4[4]; /* reserved */
62 uint dmaerrattr; /* DMA error attribute register */
63 char reserved5[4]; /* reserved */
64 uint hostcapblt2; /* Host controller capabilities register 2 */
65 char reserved6[8]; /* reserved */
66 uint tcr; /* Tuning control register */
67 char reserved7[28]; /* reserved */
68 uint sddirctl; /* SD direction control register */
69 char reserved8[712]; /* reserved */
70 uint scr; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050071};
72
73/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +000074static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -050075{
76 uint xfertyp = 0;
77
78 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +053079 xfertyp |= XFERTYP_DPSEL;
80#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
81 xfertyp |= XFERTYP_DMAEN;
82#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050083 if (data->blocks > 1) {
84 xfertyp |= XFERTYP_MSBSEL;
85 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -060086#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
87 xfertyp |= XFERTYP_AC12EN;
88#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -050089 }
90
91 if (data->flags & MMC_DATA_READ)
92 xfertyp |= XFERTYP_DTDSEL;
93 }
94
95 if (cmd->resp_type & MMC_RSP_CRC)
96 xfertyp |= XFERTYP_CCCEN;
97 if (cmd->resp_type & MMC_RSP_OPCODE)
98 xfertyp |= XFERTYP_CICEN;
99 if (cmd->resp_type & MMC_RSP_136)
100 xfertyp |= XFERTYP_RSPTYP_136;
101 else if (cmd->resp_type & MMC_RSP_BUSY)
102 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
103 else if (cmd->resp_type & MMC_RSP_PRESENT)
104 xfertyp |= XFERTYP_RSPTYP_48;
105
Wang Huanc9292132014-09-05 13:52:40 +0800106#if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || defined(CONFIG_LS102XA)
Jason Liubef0ff02011-03-22 01:32:31 +0000107 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
108 xfertyp |= XFERTYP_CMDTYP_ABORT;
109#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500110 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
111}
112
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530113#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
114/*
115 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
116 */
Wolfgang Denka40545c2010-05-09 23:52:59 +0200117static void
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530118esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
119{
Ira Snyder66a722e2011-12-23 08:30:40 +0000120 struct fsl_esdhc_cfg *cfg = mmc->priv;
121 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530122 uint blocks;
123 char *buffer;
124 uint databuf;
125 uint size;
126 uint irqstat;
127 uint timeout;
128
129 if (data->flags & MMC_DATA_READ) {
130 blocks = data->blocks;
131 buffer = data->dest;
132 while (blocks) {
133 timeout = PIO_TIMEOUT;
134 size = data->blocksize;
135 irqstat = esdhc_read32(&regs->irqstat);
136 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
137 && --timeout);
138 if (timeout <= 0) {
139 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200140 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530141 }
142 while (size && (!(irqstat & IRQSTAT_TC))) {
143 udelay(100); /* Wait before last byte transfer complete */
144 irqstat = esdhc_read32(&regs->irqstat);
145 databuf = in_le32(&regs->datport);
146 *((uint *)buffer) = databuf;
147 buffer += 4;
148 size -= 4;
149 }
150 blocks--;
151 }
152 } else {
153 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200154 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530155 while (blocks) {
156 timeout = PIO_TIMEOUT;
157 size = data->blocksize;
158 irqstat = esdhc_read32(&regs->irqstat);
159 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
160 && --timeout);
161 if (timeout <= 0) {
162 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200163 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530164 }
165 while (size && (!(irqstat & IRQSTAT_TC))) {
166 udelay(100); /* Wait before last byte transfer complete */
167 databuf = *((uint *)buffer);
168 buffer += 4;
169 size -= 4;
170 irqstat = esdhc_read32(&regs->irqstat);
171 out_le32(&regs->datport, databuf);
172 }
173 blocks--;
174 }
175 }
176}
177#endif
178
Andy Fleminge52ffb82008-10-30 16:47:16 -0500179static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
180{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500181 int timeout;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200182 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100183 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Ye.Li33a56b12014-02-20 18:00:57 +0800184
Wolfgang Denka40545c2010-05-09 23:52:59 +0200185 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500186
187 wml_value = data->blocksize/4;
188
189 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530190 if (wml_value > WML_RD_WML_MAX)
191 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500192
Roy Zange5853af2010-02-09 18:23:33 +0800193 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800194#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100195 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800196#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500197 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800198#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000199 flush_dcache_range((ulong)data->src,
200 (ulong)data->src+data->blocks
201 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800202#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530203 if (wml_value > WML_WR_WML_MAX)
204 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100205 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500206 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
207 return TIMEOUT;
208 }
Roy Zange5853af2010-02-09 18:23:33 +0800209
210 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
211 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800212#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100213 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800214#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500215 }
216
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100217 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500218
219 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530220 /*
221 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
222 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
223 * So, Number of SD Clock cycles for 0.25sec should be minimum
224 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500225 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530226 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500227 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530228 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500229 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530230 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500231 * => timeout + 13 = log2(mmc->clock/4) + 1
232 * => timeout + 13 = fls(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530233 */
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500234 timeout = fls(mmc->clock/4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500235 timeout -= 13;
236
237 if (timeout > 14)
238 timeout = 14;
239
240 if (timeout < 0)
241 timeout = 0;
242
Kumar Gala9a878d52011-01-29 15:36:10 -0600243#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
244 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
245 timeout++;
246#endif
247
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800248#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
249 timeout = 0xE;
250#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100251 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500252
253 return 0;
254}
255
Tom Rini239dd252014-05-23 09:19:05 -0400256#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000257static void check_and_invalidate_dcache_range
258 (struct mmc_cmd *cmd,
259 struct mmc_data *data) {
260 unsigned start = (unsigned)data->dest ;
261 unsigned size = roundup(ARCH_DMA_MINALIGN,
262 data->blocks*data->blocksize);
263 unsigned end = start+size ;
264 invalidate_dcache_range(start, end);
265}
Tom Rini239dd252014-05-23 09:19:05 -0400266#endif
267
Andy Fleminge52ffb82008-10-30 16:47:16 -0500268/*
269 * Sends a command out on the bus. Takes the mmc pointer,
270 * a command pointer, and an optional data pointer.
271 */
272static int
273esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
274{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500275 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500276 uint xfertyp;
277 uint irqstat;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200278 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100279 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500280
Jerry Huanged413672011-01-06 23:42:19 -0600281#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
282 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
283 return 0;
284#endif
285
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100286 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500287
288 sync();
289
290 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100291 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
292 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
293 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500294
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100295 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
296 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500297
298 /* Wait at least 8 SD clock cycles before the next command */
299 /*
300 * Note: This is way more than 8 cycles, but 1ms seems to
301 * resolve timing issues with some cards
302 */
303 udelay(1000);
304
305 /* Set up for a data transfer if we have one */
306 if (data) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500307 err = esdhc_setup_data(mmc, data);
308 if(err)
309 return err;
310 }
311
312 /* Figure out the transfer arguments */
313 xfertyp = esdhc_xfertyp(cmd, data);
314
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500315 /* Mask all irqs */
316 esdhc_write32(&regs->irqsigen, 0);
317
Andy Fleminge52ffb82008-10-30 16:47:16 -0500318 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100319 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000320#if defined(CONFIG_FSL_USDHC)
321 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500322 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
323 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu9919d642011-11-25 00:18:04 +0000324 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
325#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100326 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000327#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000328
Andy Fleminge52ffb82008-10-30 16:47:16 -0500329 /* Wait for the command to complete */
Dirk Behmed8552d62012-03-26 03:13:05 +0000330 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100331 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500332
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100333 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500334
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500335 if (irqstat & CMD_ERR) {
336 err = COMM_ERR;
337 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000338 }
339
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500340 if (irqstat & IRQSTAT_CTOE) {
341 err = TIMEOUT;
342 goto out;
343 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500344
Dirk Behmed8552d62012-03-26 03:13:05 +0000345 /* Workaround for ESDHC errata ENGcm03648 */
346 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
347 int timeout = 2500;
348
349 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
350 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
351 PRSSTAT_DAT0)) {
352 udelay(100);
353 timeout--;
354 }
355
356 if (timeout <= 0) {
357 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500358 err = TIMEOUT;
359 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000360 }
361 }
362
Andy Fleminge52ffb82008-10-30 16:47:16 -0500363 /* Copy the response to the response buffer */
364 if (cmd->resp_type & MMC_RSP_136) {
365 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
366
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100367 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
368 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
369 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
370 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530371 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
372 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
373 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
374 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500375 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100376 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500377
378 /* Wait until all of the blocks are transferred */
379 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530380#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
381 esdhc_pio_read_write(mmc, data);
382#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500383 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100384 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500385
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500386 if (irqstat & IRQSTAT_DTOE) {
387 err = TIMEOUT;
388 goto out;
389 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000390
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500391 if (irqstat & DATA_ERR) {
392 err = COMM_ERR;
393 goto out;
394 }
Andrew Gabbasov4a929622013-04-07 23:06:08 +0000395 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800396
Eric Nelson70e68692013-04-03 12:31:56 +0000397 if (data->flags & MMC_DATA_READ)
398 check_and_invalidate_dcache_range(cmd, data);
Ye.Li33a56b12014-02-20 18:00:57 +0800399#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500400 }
401
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500402out:
403 /* Reset CMD and DATA portions on error */
404 if (err) {
405 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
406 SYSCTL_RSTC);
407 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
408 ;
409
410 if (data) {
411 esdhc_write32(&regs->sysctl,
412 esdhc_read32(&regs->sysctl) |
413 SYSCTL_RSTD);
414 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
415 ;
416 }
417 }
418
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100419 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500420
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500421 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500422}
423
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000424static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500425{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500426 int div, pre_div;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200427 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100428 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeauc58ff342012-10-01 08:36:25 +0000429 int sdhc_clk = cfg->sdhc_clk;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500430 uint clk;
431
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200432 if (clock < mmc->cfg->f_min)
433 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100434
Andy Fleminge52ffb82008-10-30 16:47:16 -0500435 if (sdhc_clk / 16 > clock) {
436 for (pre_div = 2; pre_div < 256; pre_div *= 2)
437 if ((sdhc_clk / pre_div) <= (clock * 16))
438 break;
439 } else
440 pre_div = 2;
441
442 for (div = 1; div <= 16; div++)
443 if ((sdhc_clk / (div * pre_div)) <= clock)
444 break;
445
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500446 pre_div >>= mmc->ddr_mode ? 2 : 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500447 div -= 1;
448
449 clk = (pre_div << 8) | (div << 4);
450
Kumar Gala09876a32010-03-18 15:51:05 -0500451 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100452
453 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500454
455 udelay(10000);
456
Kumar Gala09876a32010-03-18 15:51:05 -0500457 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100458
459 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500460}
461
462static void esdhc_set_ios(struct mmc *mmc)
463{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200464 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100465 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500466
467 /* Set the clock speed */
468 set_sysctl(mmc, mmc->clock);
469
470 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100471 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500472
473 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100474 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100476 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
477
Andy Fleminge52ffb82008-10-30 16:47:16 -0500478}
479
480static int esdhc_init(struct mmc *mmc)
481{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200482 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100483 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500484 int timeout = 1000;
485
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100486 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200487 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100488
489 /* Wait until the controller is available */
490 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
491 udelay(1000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500492
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000493#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530494 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000495 esdhc_write32(&regs->scr, 0x00000040);
496#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530497
Dirk Behmedbe67252013-07-15 15:44:29 +0200498 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500499
500 /* Set the initial clock speed */
Jerry Huang0caea1a2010-11-25 17:06:07 +0000501 mmc_set_clock(mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500502
503 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100504 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500505
506 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100507 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500508
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100509 /* Set timout to the maximum value */
510 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500511
Thierry Reding8cee4c982012-01-02 01:15:38 +0000512 return 0;
513}
514
515static int esdhc_getcd(struct mmc *mmc)
516{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200517 struct fsl_esdhc_cfg *cfg = mmc->priv;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000518 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
519 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500520
Haijun.Zhang05f58542014-01-10 13:52:17 +0800521#ifdef CONFIG_ESDHC_DETECT_QUIRK
522 if (CONFIG_ESDHC_DETECT_QUIRK)
523 return 1;
524#endif
Thierry Reding8cee4c982012-01-02 01:15:38 +0000525 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
526 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100527
Thierry Reding8cee4c982012-01-02 01:15:38 +0000528 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500529}
530
Jerry Huangb7ef7562010-03-18 15:57:06 -0500531static void esdhc_reset(struct fsl_esdhc *regs)
532{
533 unsigned long timeout = 100; /* wait max 100 ms */
534
535 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200536 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500537
538 /* hardware clears the bit when it is done */
539 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
540 udelay(1000);
541 if (!timeout)
542 printf("MMC/SD: Reset never completed.\n");
543}
544
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200545static const struct mmc_ops esdhc_ops = {
546 .send_cmd = esdhc_send_cmd,
547 .set_ios = esdhc_set_ios,
548 .init = esdhc_init,
549 .getcd = esdhc_getcd,
550};
551
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100552int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500553{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100554 struct fsl_esdhc *regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500555 struct mmc *mmc;
Li Yangd4933f22010-11-25 17:06:09 +0000556 u32 caps, voltage_caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500557
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100558 if (!cfg)
559 return -1;
560
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100561 regs = (struct fsl_esdhc *)cfg->esdhc_base;
562
Jerry Huangb7ef7562010-03-18 15:57:06 -0500563 /* First reset the eSDHC controller */
564 esdhc_reset(regs);
565
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000566 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
567 | SYSCTL_IPGEN | SYSCTL_CKEN);
568
Ye.Li3d46c312014-11-04 15:35:49 +0800569 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200570 memset(&cfg->cfg, 0, sizeof(cfg->cfg));
571
Li Yangd4933f22010-11-25 17:06:09 +0000572 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +0800573 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600574
575#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
576 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
577 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
578#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800579
580/* T4240 host controller capabilities register should have VS33 bit */
581#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
582 caps = caps | ESDHC_HOSTCAPBLT_VS33;
583#endif
584
Andy Fleminge52ffb82008-10-30 16:47:16 -0500585 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000586 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500587 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000588 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500589 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000590 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
591
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200592 cfg->cfg.name = "FSL_SDHC";
593 cfg->cfg.ops = &esdhc_ops;
Li Yangd4933f22010-11-25 17:06:09 +0000594#ifdef CONFIG_SYS_SD_VOLTAGE
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200595 cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +0000596#else
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200597 cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000598#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200599 if ((cfg->cfg.voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +0000600 printf("voltage not supported by controller\n");
601 return -1;
602 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500603
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200604 cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500605#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
606 cfg->cfg.host_caps |= MMC_MODE_DDR_52MHz;
607#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500608
Abbas Razae6bf9772013-03-25 09:13:34 +0000609 if (cfg->max_bus_width > 0) {
610 if (cfg->max_bus_width < 8)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200611 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000612 if (cfg->max_bus_width < 4)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200613 cfg->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000614 }
615
Andy Fleminge52ffb82008-10-30 16:47:16 -0500616 if (caps & ESDHC_HOSTCAPBLT_HSS)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200617 cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500618
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800619#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
620 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200621 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800622#endif
623
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200624 cfg->cfg.f_min = 400000;
Tom Rini2907a302014-11-26 11:22:29 -0500625 cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500626
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200627 cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
628
629 mmc = mmc_create(&cfg->cfg, cfg);
630 if (mmc == NULL)
631 return -1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500632
633 return 0;
634}
635
636int fsl_esdhc_mmc_init(bd_t *bis)
637{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100638 struct fsl_esdhc_cfg *cfg;
639
Fabio Estevam6592a992012-12-27 08:51:08 +0000640 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100641 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000642 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100643 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500644}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400645
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100646#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400647void fdt_fixup_esdhc(void *blob, bd_t *bd)
648{
649 const char *compat = "fsl,esdhc";
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400650
Chenhui Zhao025eab02011-01-04 17:23:05 +0800651#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400652 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800653 do_fixup_by_compat(blob, compat, "status", "disabled",
654 8 + 1, 1);
655 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400656 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800657#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400658
659 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000660 gd->arch.sdhc_clk, 1);
Chenhui Zhao025eab02011-01-04 17:23:05 +0800661
662 do_fixup_by_compat(blob, compat, "status", "okay",
663 4 + 1, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400664}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100665#endif