blob: c55eb28217bc5920c3137016dc71b8a6cd58b4b4 [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
Ye.Lia3d6e382014-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 Fleming50586ef2008-10-30 16:47:16 -050033struct fsl_esdhc {
Haijun.Zhang511948b2013-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 Fleming50586ef2008-10-30 16:47:16 -050071};
72
73/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +000074static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -050075{
76 uint xfertyp = 0;
77
78 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +053079 xfertyp |= XFERTYP_DPSEL;
80#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
81 xfertyp |= XFERTYP_DMAEN;
82#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050083 if (data->blocks > 1) {
84 xfertyp |= XFERTYP_MSBSEL;
85 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -060086#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
87 xfertyp |= XFERTYP_AC12EN;
88#endif
Andy Fleming50586ef2008-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 Huan19060bd2014-09-05 13:52:40 +0800106#if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || defined(CONFIG_LS102XA)
Jason Liu4571de32011-03-22 01:32:31 +0000107 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
108 xfertyp |= XFERTYP_CMDTYP_ABORT;
109#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500110 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
111}
112
Dipen Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200117static void
Dipen Dudhat77c14582009-10-05 15:41:58 +0530118esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
119{
Ira Snyder8eee2bd2011-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 Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200140 return;
Dipen Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200154 buffer = (char *)data->src;
Dipen Dudhat77c14582009-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 Denk7b43db92010-05-09 23:52:59 +0200163 return;
Dipen Dudhat77c14582009-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 Fleming50586ef2008-10-30 16:47:16 -0500179static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
180{
Andy Fleming50586ef2008-10-30 16:47:16 -0500181 int timeout;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200182 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100183 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Ye.Li71689772014-02-20 18:00:57 +0800184
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200185 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500186
187 wml_value = data->blocksize/4;
188
189 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530190 if (wml_value > WML_RD_WML_MAX)
191 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500192
Roy Zangab467c52010-02-09 18:23:33 +0800193 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800194#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Stefano Babicc67bee12010-02-05 15:11:27 +0100195 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800196#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500197 } else {
Ye.Li71689772014-02-20 18:00:57 +0800198#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000199 flush_dcache_range((ulong)data->src,
200 (ulong)data->src+data->blocks
201 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800202#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530203 if (wml_value > WML_WR_WML_MAX)
204 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100205 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-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 Zangab467c52010-02-09 18:23:33 +0800209
210 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
211 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800212#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Stefano Babicc67bee12010-02-05 15:11:27 +0100213 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800214#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500215 }
216
Stefano Babicc67bee12010-02-05 15:11:27 +0100217 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500218
219 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-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 Gabbasovfb823982014-03-24 02:40:41 -0500225 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530226 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500227 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530228 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500229 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530230 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500231 * => timeout + 13 = log2(mmc->clock/4) + 1
232 * => timeout + 13 = fls(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530233 */
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500234 timeout = fls(mmc->clock/4);
Andy Fleming50586ef2008-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 Gala5103a032011-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.Zhang1336e2d2014-03-18 17:04:23 +0800248#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
249 timeout = 0xE;
250#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100251 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500252
253 return 0;
254}
255
Tom Rini10dc7772014-05-23 09:19:05 -0400256#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-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 Rini10dc7772014-05-23 09:19:05 -0400266#endif
267
Andy Fleming50586ef2008-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 Gabbasov8a573022014-03-24 02:41:06 -0500275 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500276 uint xfertyp;
277 uint irqstat;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200278 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100279 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500280
Jerry Huangd621da02011-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 Babicc67bee12010-02-05 15:11:27 +0100286 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500287
288 sync();
289
290 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100291 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
292 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
293 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500294
Stefano Babicc67bee12010-02-05 15:11:27 +0100295 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
296 ;
Andy Fleming50586ef2008-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 Fleming50586ef2008-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 Gabbasov01b77352013-06-11 10:34:22 -0500315 /* Mask all irqs */
316 esdhc_write32(&regs->irqsigen, 0);
317
Andy Fleming50586ef2008-10-30 16:47:16 -0500318 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100319 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000320#if defined(CONFIG_FSL_USDHC)
321 esdhc_write32(&regs->mixctrl,
322 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
323 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
324#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100325 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000326#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000327
Andy Fleming50586ef2008-10-30 16:47:16 -0500328 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000329 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100330 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500331
Stefano Babicc67bee12010-02-05 15:11:27 +0100332 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500333
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500334 if (irqstat & CMD_ERR) {
335 err = COMM_ERR;
336 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000337 }
338
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500339 if (irqstat & IRQSTAT_CTOE) {
340 err = TIMEOUT;
341 goto out;
342 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500343
Dirk Behme7a5b8022012-03-26 03:13:05 +0000344 /* Workaround for ESDHC errata ENGcm03648 */
345 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
346 int timeout = 2500;
347
348 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
349 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
350 PRSSTAT_DAT0)) {
351 udelay(100);
352 timeout--;
353 }
354
355 if (timeout <= 0) {
356 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500357 err = TIMEOUT;
358 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000359 }
360 }
361
Andy Fleming50586ef2008-10-30 16:47:16 -0500362 /* Copy the response to the response buffer */
363 if (cmd->resp_type & MMC_RSP_136) {
364 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
365
Stefano Babicc67bee12010-02-05 15:11:27 +0100366 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
367 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
368 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
369 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530370 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
371 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
372 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
373 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500374 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100375 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500376
377 /* Wait until all of the blocks are transferred */
378 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530379#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
380 esdhc_pio_read_write(mmc, data);
381#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500382 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100383 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500384
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500385 if (irqstat & IRQSTAT_DTOE) {
386 err = TIMEOUT;
387 goto out;
388 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000389
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500390 if (irqstat & DATA_ERR) {
391 err = COMM_ERR;
392 goto out;
393 }
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000394 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800395
Eric Nelson54899fc2013-04-03 12:31:56 +0000396 if (data->flags & MMC_DATA_READ)
397 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800398#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500399 }
400
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500401out:
402 /* Reset CMD and DATA portions on error */
403 if (err) {
404 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
405 SYSCTL_RSTC);
406 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
407 ;
408
409 if (data) {
410 esdhc_write32(&regs->sysctl,
411 esdhc_read32(&regs->sysctl) |
412 SYSCTL_RSTD);
413 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
414 ;
415 }
416 }
417
Stefano Babicc67bee12010-02-05 15:11:27 +0100418 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500419
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500420 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500421}
422
Kim Phillipseafa90a2012-10-29 13:34:44 +0000423static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500424{
Andy Fleming50586ef2008-10-30 16:47:16 -0500425 int div, pre_div;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200426 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100427 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeaua2ac1b32012-10-01 08:36:25 +0000428 int sdhc_clk = cfg->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500429 uint clk;
430
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200431 if (clock < mmc->cfg->f_min)
432 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100433
Andy Fleming50586ef2008-10-30 16:47:16 -0500434 if (sdhc_clk / 16 > clock) {
435 for (pre_div = 2; pre_div < 256; pre_div *= 2)
436 if ((sdhc_clk / pre_div) <= (clock * 16))
437 break;
438 } else
439 pre_div = 2;
440
441 for (div = 1; div <= 16; div++)
442 if ((sdhc_clk / (div * pre_div)) <= clock)
443 break;
444
445 pre_div >>= 1;
446 div -= 1;
447
448 clk = (pre_div << 8) | (div << 4);
449
Kumar Galacc4d1222010-03-18 15:51:05 -0500450 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100451
452 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500453
454 udelay(10000);
455
Kumar Galacc4d1222010-03-18 15:51:05 -0500456 clk = SYSCTL_PEREN | SYSCTL_CKEN;
Stefano Babicc67bee12010-02-05 15:11:27 +0100457
458 esdhc_setbits32(&regs->sysctl, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500459}
460
461static void esdhc_set_ios(struct mmc *mmc)
462{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200463 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100464 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500465
466 /* Set the clock speed */
467 set_sysctl(mmc, mmc->clock);
468
469 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100470 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500471
472 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100473 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500474 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100475 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
476
Andy Fleming50586ef2008-10-30 16:47:16 -0500477}
478
479static int esdhc_init(struct mmc *mmc)
480{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200481 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100482 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500483 int timeout = 1000;
484
Stefano Babicc67bee12010-02-05 15:11:27 +0100485 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200486 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100487
488 /* Wait until the controller is available */
489 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
490 udelay(1000);
491
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000492#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530493 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000494 esdhc_write32(&regs->scr, 0x00000040);
495#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530496
Dirk Behmea61da722013-07-15 15:44:29 +0200497 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500498
499 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000500 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500501
502 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100503 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500504
505 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100506 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500507
Stefano Babicc67bee12010-02-05 15:11:27 +0100508 /* Set timout to the maximum value */
509 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500510
Thierry Redingd48d2e22012-01-02 01:15:38 +0000511 return 0;
512}
Andy Fleming50586ef2008-10-30 16:47:16 -0500513
Thierry Redingd48d2e22012-01-02 01:15:38 +0000514static int esdhc_getcd(struct mmc *mmc)
515{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200516 struct fsl_esdhc_cfg *cfg = mmc->priv;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000517 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
518 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100519
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800520#ifdef CONFIG_ESDHC_DETECT_QUIRK
521 if (CONFIG_ESDHC_DETECT_QUIRK)
522 return 1;
523#endif
Thierry Redingd48d2e22012-01-02 01:15:38 +0000524 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
525 udelay(1000);
526
527 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500528}
529
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500530static void esdhc_reset(struct fsl_esdhc *regs)
531{
532 unsigned long timeout = 100; /* wait max 100 ms */
533
534 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200535 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500536
537 /* hardware clears the bit when it is done */
538 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
539 udelay(1000);
540 if (!timeout)
541 printf("MMC/SD: Reset never completed.\n");
542}
543
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200544static const struct mmc_ops esdhc_ops = {
545 .send_cmd = esdhc_send_cmd,
546 .set_ios = esdhc_set_ios,
547 .init = esdhc_init,
548 .getcd = esdhc_getcd,
549};
550
Stefano Babicc67bee12010-02-05 15:11:27 +0100551int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500552{
Stefano Babicc67bee12010-02-05 15:11:27 +0100553 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500554 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000555 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500556
Stefano Babicc67bee12010-02-05 15:11:27 +0100557 if (!cfg)
558 return -1;
559
Stefano Babicc67bee12010-02-05 15:11:27 +0100560 regs = (struct fsl_esdhc *)cfg->esdhc_base;
561
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500562 /* First reset the eSDHC controller */
563 esdhc_reset(regs);
564
Jerry Huang975324a2012-05-17 23:57:02 +0000565 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
566 | SYSCTL_IPGEN | SYSCTL_CKEN);
567
Ye.Lia3d6e382014-11-04 15:35:49 +0800568 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200569 memset(&cfg->cfg, 0, sizeof(cfg->cfg));
570
Li Yang030955c2010-11-25 17:06:09 +0000571 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800572 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600573
574#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
575 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
576 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
577#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800578
579/* T4240 host controller capabilities register should have VS33 bit */
580#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
581 caps = caps | ESDHC_HOSTCAPBLT_VS33;
582#endif
583
Andy Fleming50586ef2008-10-30 16:47:16 -0500584 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000585 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500586 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000587 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500588 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000589 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
590
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200591 cfg->cfg.name = "FSL_SDHC";
592 cfg->cfg.ops = &esdhc_ops;
Li Yang030955c2010-11-25 17:06:09 +0000593#ifdef CONFIG_SYS_SD_VOLTAGE
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200594 cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000595#else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200596 cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000597#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200598 if ((cfg->cfg.voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000599 printf("voltage not supported by controller\n");
600 return -1;
601 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500602
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200603 cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
Andy Fleming50586ef2008-10-30 16:47:16 -0500604
Abbas Razaaad46592013-03-25 09:13:34 +0000605 if (cfg->max_bus_width > 0) {
606 if (cfg->max_bus_width < 8)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200607 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000608 if (cfg->max_bus_width < 4)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200609 cfg->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000610 }
611
Andy Fleming50586ef2008-10-30 16:47:16 -0500612 if (caps & ESDHC_HOSTCAPBLT_HSS)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200613 cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500614
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800615#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
616 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200617 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800618#endif
619
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200620 cfg->cfg.f_min = 400000;
Tom Rini21008ad2014-11-26 11:22:29 -0500621 cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500622
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200623 cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
624
625 mmc = mmc_create(&cfg->cfg, cfg);
626 if (mmc == NULL)
627 return -1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500628
629 return 0;
630}
631
632int fsl_esdhc_mmc_init(bd_t *bis)
633{
Stefano Babicc67bee12010-02-05 15:11:27 +0100634 struct fsl_esdhc_cfg *cfg;
635
Fabio Estevam88227a12012-12-27 08:51:08 +0000636 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100637 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000638 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100639 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500640}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400641
Stefano Babicc67bee12010-02-05 15:11:27 +0100642#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400643void fdt_fixup_esdhc(void *blob, bd_t *bd)
644{
645 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400646
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800647#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400648 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800649 do_fixup_by_compat(blob, compat, "status", "disabled",
650 8 + 1, 1);
651 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400652 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800653#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400654
655 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000656 gd->arch.sdhc_clk, 1);
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800657
658 do_fixup_by_compat(blob, compat, "status", "okay",
659 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400660}
Stefano Babicc67bee12010-02-05 15:11:27 +0100661#endif