blob: 1ccc576c347c6f0f669ab35e247641df252ba1eb [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 */
Otavio Salvadorf022d362015-02-17 10:42:43 -020057 char reserved2[100]; /* reserved */
58 uint vendorspec; /* Vendor Specific register */
Peng Fan323aaaa2015-03-10 15:35:46 +080059 char reserved3[56]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080060 uint hostver; /* Host controller version register */
Haijun.Zhang511948b2013-10-30 11:37:55 +080061 char reserved4[4]; /* reserved */
Otavio Salvadorf022d362015-02-17 10:42:43 -020062 uint dmaerraddr; /* DMA error address register */
Haijun.Zhang511948b2013-10-30 11:37:55 +080063 char reserved5[4]; /* reserved */
Otavio Salvadorf022d362015-02-17 10:42:43 -020064 uint dmaerrattr; /* DMA error attribute register */
65 char reserved6[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080066 uint hostcapblt2; /* Host controller capabilities register 2 */
Otavio Salvadorf022d362015-02-17 10:42:43 -020067 char reserved7[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080068 uint tcr; /* Tuning control register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020069 char reserved8[28]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080070 uint sddirctl; /* SD direction control register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020071 char reserved9[712]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080072 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050073};
74
75/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +000076static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -050077{
78 uint xfertyp = 0;
79
80 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +053081 xfertyp |= XFERTYP_DPSEL;
82#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
83 xfertyp |= XFERTYP_DMAEN;
84#endif
Andy Fleming50586ef2008-10-30 16:47:16 -050085 if (data->blocks > 1) {
86 xfertyp |= XFERTYP_MSBSEL;
87 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -060088#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
89 xfertyp |= XFERTYP_AC12EN;
90#endif
Andy Fleming50586ef2008-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
Yangbo Lu8b064602015-03-20 19:28:31 -0700108#if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || \
Shaohui Xie512bdbd2015-09-11 19:02:13 +0800109 defined(CONFIG_LS102XA) || defined(CONFIG_FSL_LAYERSCAPE) || \
110 defined(CONFIG_PPC_T4160)
Jason Liu4571de32011-03-22 01:32:31 +0000111 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
112 xfertyp |= XFERTYP_CMDTYP_ABORT;
113#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500114 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
115}
116
Dipen Dudhat77c14582009-10-05 15:41:58 +0530117#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
118/*
119 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
120 */
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200121static void
Dipen Dudhat77c14582009-10-05 15:41:58 +0530122esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
123{
Ira Snyder8eee2bd2011-12-23 08:30:40 +0000124 struct fsl_esdhc_cfg *cfg = mmc->priv;
125 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530126 uint blocks;
127 char *buffer;
128 uint databuf;
129 uint size;
130 uint irqstat;
131 uint timeout;
132
133 if (data->flags & MMC_DATA_READ) {
134 blocks = data->blocks;
135 buffer = data->dest;
136 while (blocks) {
137 timeout = PIO_TIMEOUT;
138 size = data->blocksize;
139 irqstat = esdhc_read32(&regs->irqstat);
140 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
141 && --timeout);
142 if (timeout <= 0) {
143 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200144 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530145 }
146 while (size && (!(irqstat & IRQSTAT_TC))) {
147 udelay(100); /* Wait before last byte transfer complete */
148 irqstat = esdhc_read32(&regs->irqstat);
149 databuf = in_le32(&regs->datport);
150 *((uint *)buffer) = databuf;
151 buffer += 4;
152 size -= 4;
153 }
154 blocks--;
155 }
156 } else {
157 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200158 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530159 while (blocks) {
160 timeout = PIO_TIMEOUT;
161 size = data->blocksize;
162 irqstat = esdhc_read32(&regs->irqstat);
163 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
164 && --timeout);
165 if (timeout <= 0) {
166 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200167 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530168 }
169 while (size && (!(irqstat & IRQSTAT_TC))) {
170 udelay(100); /* Wait before last byte transfer complete */
171 databuf = *((uint *)buffer);
172 buffer += 4;
173 size -= 4;
174 irqstat = esdhc_read32(&regs->irqstat);
175 out_le32(&regs->datport, databuf);
176 }
177 blocks--;
178 }
179 }
180}
181#endif
182
Andy Fleming50586ef2008-10-30 16:47:16 -0500183static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
184{
Andy Fleming50586ef2008-10-30 16:47:16 -0500185 int timeout;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200186 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100187 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800188#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700189 dma_addr_t addr;
190#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200191 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500192
193 wml_value = data->blocksize/4;
194
195 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530196 if (wml_value > WML_RD_WML_MAX)
197 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500198
Roy Zangab467c52010-02-09 18:23:33 +0800199 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800200#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800201#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700202 addr = virt_to_phys((void *)(data->dest));
203 if (upper_32_bits(addr))
204 printf("Error found for upper 32 bits\n");
205 else
206 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
207#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100208 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800209#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700210#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500211 } else {
Ye.Li71689772014-02-20 18:00:57 +0800212#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000213 flush_dcache_range((ulong)data->src,
214 (ulong)data->src+data->blocks
215 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800216#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530217 if (wml_value > WML_WR_WML_MAX)
218 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100219 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500220 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
221 return TIMEOUT;
222 }
Roy Zangab467c52010-02-09 18:23:33 +0800223
224 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
225 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800226#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800227#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700228 addr = virt_to_phys((void *)(data->src));
229 if (upper_32_bits(addr))
230 printf("Error found for upper 32 bits\n");
231 else
232 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
233#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100234 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800235#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700236#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500237 }
238
Stefano Babicc67bee12010-02-05 15:11:27 +0100239 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500240
241 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530242 /*
243 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
244 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
245 * So, Number of SD Clock cycles for 0.25sec should be minimum
246 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500247 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530248 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500249 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530250 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500251 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530252 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500253 * => timeout + 13 = log2(mmc->clock/4) + 1
254 * => timeout + 13 = fls(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530255 */
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500256 timeout = fls(mmc->clock/4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500257 timeout -= 13;
258
259 if (timeout > 14)
260 timeout = 14;
261
262 if (timeout < 0)
263 timeout = 0;
264
Kumar Gala5103a032011-01-29 15:36:10 -0600265#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
266 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
267 timeout++;
268#endif
269
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800270#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
271 timeout = 0xE;
272#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100273 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500274
275 return 0;
276}
277
Eric Nelsone576bd92012-04-25 14:28:48 +0000278static void check_and_invalidate_dcache_range
279 (struct mmc_cmd *cmd,
280 struct mmc_data *data) {
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800281#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700282 unsigned start = 0;
283#else
Eric Nelsone576bd92012-04-25 14:28:48 +0000284 unsigned start = (unsigned)data->dest ;
Yangbo Lu8b064602015-03-20 19:28:31 -0700285#endif
Eric Nelsone576bd92012-04-25 14:28:48 +0000286 unsigned size = roundup(ARCH_DMA_MINALIGN,
287 data->blocks*data->blocksize);
288 unsigned end = start+size ;
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800289#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700290 dma_addr_t addr;
291
292 addr = virt_to_phys((void *)(data->dest));
293 if (upper_32_bits(addr))
294 printf("Error found for upper 32 bits\n");
295 else
296 start = lower_32_bits(addr);
297#endif
Eric Nelsone576bd92012-04-25 14:28:48 +0000298 invalidate_dcache_range(start, end);
299}
Tom Rini10dc7772014-05-23 09:19:05 -0400300
Andy Fleming50586ef2008-10-30 16:47:16 -0500301/*
302 * Sends a command out on the bus. Takes the mmc pointer,
303 * a command pointer, and an optional data pointer.
304 */
305static int
306esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
307{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500308 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500309 uint xfertyp;
310 uint irqstat;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200311 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100312 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500313
Jerry Huangd621da02011-01-06 23:42:19 -0600314#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
315 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
316 return 0;
317#endif
318
Stefano Babicc67bee12010-02-05 15:11:27 +0100319 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500320
321 sync();
322
323 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100324 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
325 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
326 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500327
Stefano Babicc67bee12010-02-05 15:11:27 +0100328 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
329 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500330
331 /* Wait at least 8 SD clock cycles before the next command */
332 /*
333 * Note: This is way more than 8 cycles, but 1ms seems to
334 * resolve timing issues with some cards
335 */
336 udelay(1000);
337
338 /* Set up for a data transfer if we have one */
339 if (data) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500340 err = esdhc_setup_data(mmc, data);
341 if(err)
342 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800343
344 if (data->flags & MMC_DATA_READ)
345 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500346 }
347
348 /* Figure out the transfer arguments */
349 xfertyp = esdhc_xfertyp(cmd, data);
350
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500351 /* Mask all irqs */
352 esdhc_write32(&regs->irqsigen, 0);
353
Andy Fleming50586ef2008-10-30 16:47:16 -0500354 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100355 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000356#if defined(CONFIG_FSL_USDHC)
357 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500358 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
359 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000360 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
361#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100362 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000363#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000364
Andy Fleming50586ef2008-10-30 16:47:16 -0500365 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000366 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100367 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500368
Stefano Babicc67bee12010-02-05 15:11:27 +0100369 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500370
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500371 if (irqstat & CMD_ERR) {
372 err = COMM_ERR;
373 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000374 }
375
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500376 if (irqstat & IRQSTAT_CTOE) {
377 err = TIMEOUT;
378 goto out;
379 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500380
Otavio Salvadorf022d362015-02-17 10:42:43 -0200381 /* Switch voltage to 1.8V if CMD11 succeeded */
382 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
383 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
384
385 printf("Run CMD11 1.8V switch\n");
386 /* Sleep for 5 ms - max time for card to switch to 1.8V */
387 udelay(5000);
388 }
389
Dirk Behme7a5b8022012-03-26 03:13:05 +0000390 /* Workaround for ESDHC errata ENGcm03648 */
391 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800392 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000393
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800394 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000395 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
396 PRSSTAT_DAT0)) {
397 udelay(100);
398 timeout--;
399 }
400
401 if (timeout <= 0) {
402 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500403 err = TIMEOUT;
404 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000405 }
406 }
407
Andy Fleming50586ef2008-10-30 16:47:16 -0500408 /* Copy the response to the response buffer */
409 if (cmd->resp_type & MMC_RSP_136) {
410 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
411
Stefano Babicc67bee12010-02-05 15:11:27 +0100412 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
413 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
414 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
415 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530416 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
417 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
418 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
419 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500420 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100421 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500422
423 /* Wait until all of the blocks are transferred */
424 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530425#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
426 esdhc_pio_read_write(mmc, data);
427#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500428 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100429 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500430
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500431 if (irqstat & IRQSTAT_DTOE) {
432 err = TIMEOUT;
433 goto out;
434 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000435
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500436 if (irqstat & DATA_ERR) {
437 err = COMM_ERR;
438 goto out;
439 }
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000440 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800441
Peng Fan4683b222015-06-25 10:32:26 +0800442 /*
443 * Need invalidate the dcache here again to avoid any
444 * cache-fill during the DMA operations such as the
445 * speculative pre-fetching etc.
446 */
Eric Nelson54899fc2013-04-03 12:31:56 +0000447 if (data->flags & MMC_DATA_READ)
448 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800449#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500450 }
451
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500452out:
453 /* Reset CMD and DATA portions on error */
454 if (err) {
455 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
456 SYSCTL_RSTC);
457 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
458 ;
459
460 if (data) {
461 esdhc_write32(&regs->sysctl,
462 esdhc_read32(&regs->sysctl) |
463 SYSCTL_RSTD);
464 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
465 ;
466 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200467
468 /* If this was CMD11, then notify that power cycle is needed */
469 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
470 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500471 }
472
Stefano Babicc67bee12010-02-05 15:11:27 +0100473 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500474
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500475 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500476}
477
Kim Phillipseafa90a2012-10-29 13:34:44 +0000478static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500479{
Andy Fleming50586ef2008-10-30 16:47:16 -0500480 int div, pre_div;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200481 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100482 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeaua2ac1b32012-10-01 08:36:25 +0000483 int sdhc_clk = cfg->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500484 uint clk;
485
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200486 if (clock < mmc->cfg->f_min)
487 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100488
Andy Fleming50586ef2008-10-30 16:47:16 -0500489 if (sdhc_clk / 16 > clock) {
490 for (pre_div = 2; pre_div < 256; pre_div *= 2)
491 if ((sdhc_clk / pre_div) <= (clock * 16))
492 break;
493 } else
494 pre_div = 2;
495
496 for (div = 1; div <= 16; div++)
497 if ((sdhc_clk / (div * pre_div)) <= clock)
498 break;
499
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500500 pre_div >>= mmc->ddr_mode ? 2 : 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500501 div -= 1;
502
503 clk = (pre_div << 8) | (div << 4);
504
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700505#ifdef CONFIG_FSL_USDHC
506 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
507#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500508 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700509#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100510
511 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500512
513 udelay(10000);
514
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700515#ifdef CONFIG_FSL_USDHC
516 esdhc_clrbits32(&regs->sysctl, SYSCTL_RSTA);
517#else
518 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
519#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100520
Andy Fleming50586ef2008-10-30 16:47:16 -0500521}
522
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800523#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
524static void esdhc_clock_control(struct mmc *mmc, bool enable)
525{
526 struct fsl_esdhc_cfg *cfg = mmc->priv;
527 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
528 u32 value;
529 u32 time_out;
530
531 value = esdhc_read32(&regs->sysctl);
532
533 if (enable)
534 value |= SYSCTL_CKEN;
535 else
536 value &= ~SYSCTL_CKEN;
537
538 esdhc_write32(&regs->sysctl, value);
539
540 time_out = 20;
541 value = PRSSTAT_SDSTB;
542 while (!(esdhc_read32(&regs->prsstat) & value)) {
543 if (time_out == 0) {
544 printf("fsl_esdhc: Internal clock never stabilised.\n");
545 break;
546 }
547 time_out--;
548 mdelay(1);
549 }
550}
551#endif
552
Andy Fleming50586ef2008-10-30 16:47:16 -0500553static void esdhc_set_ios(struct mmc *mmc)
554{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200555 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100556 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500557
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800558#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
559 /* Select to use peripheral clock */
560 esdhc_clock_control(mmc, false);
561 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
562 esdhc_clock_control(mmc, true);
563#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500564 /* Set the clock speed */
565 set_sysctl(mmc, mmc->clock);
566
567 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100568 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500569
570 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100571 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500572 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100573 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
574
Andy Fleming50586ef2008-10-30 16:47:16 -0500575}
576
577static int esdhc_init(struct mmc *mmc)
578{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200579 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100580 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500581 int timeout = 1000;
582
Stefano Babicc67bee12010-02-05 15:11:27 +0100583 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200584 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100585
586 /* Wait until the controller is available */
587 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
588 udelay(1000);
589
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000590#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530591 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000592 esdhc_write32(&regs->scr, 0x00000040);
593#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530594
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700595#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +0200596 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700597#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500598
599 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000600 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500601
602 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100603 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500604
605 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100606 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500607
Stefano Babicc67bee12010-02-05 15:11:27 +0100608 /* Set timout to the maximum value */
609 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500610
Otavio Salvadoree0c5382015-02-17 10:42:44 -0200611#ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
612 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
613#endif
614
Thierry Redingd48d2e22012-01-02 01:15:38 +0000615 return 0;
616}
Andy Fleming50586ef2008-10-30 16:47:16 -0500617
Thierry Redingd48d2e22012-01-02 01:15:38 +0000618static int esdhc_getcd(struct mmc *mmc)
619{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200620 struct fsl_esdhc_cfg *cfg = mmc->priv;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000621 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
622 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100623
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800624#ifdef CONFIG_ESDHC_DETECT_QUIRK
625 if (CONFIG_ESDHC_DETECT_QUIRK)
626 return 1;
627#endif
Thierry Redingd48d2e22012-01-02 01:15:38 +0000628 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
629 udelay(1000);
630
631 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500632}
633
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500634static void esdhc_reset(struct fsl_esdhc *regs)
635{
636 unsigned long timeout = 100; /* wait max 100 ms */
637
638 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200639 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500640
641 /* hardware clears the bit when it is done */
642 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
643 udelay(1000);
644 if (!timeout)
645 printf("MMC/SD: Reset never completed.\n");
646}
647
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200648static const struct mmc_ops esdhc_ops = {
649 .send_cmd = esdhc_send_cmd,
650 .set_ios = esdhc_set_ios,
651 .init = esdhc_init,
652 .getcd = esdhc_getcd,
653};
654
Stefano Babicc67bee12010-02-05 15:11:27 +0100655int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500656{
Stefano Babicc67bee12010-02-05 15:11:27 +0100657 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500658 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000659 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500660
Stefano Babicc67bee12010-02-05 15:11:27 +0100661 if (!cfg)
662 return -1;
663
Stefano Babicc67bee12010-02-05 15:11:27 +0100664 regs = (struct fsl_esdhc *)cfg->esdhc_base;
665
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500666 /* First reset the eSDHC controller */
667 esdhc_reset(regs);
668
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700669#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +0000670 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
671 | SYSCTL_IPGEN | SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700672#endif
Jerry Huang975324a2012-05-17 23:57:02 +0000673
Ye.Lia3d6e382014-11-04 15:35:49 +0800674 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200675 memset(&cfg->cfg, 0, sizeof(cfg->cfg));
676
Li Yang030955c2010-11-25 17:06:09 +0000677 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800678 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600679
680#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
681 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
682 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
683#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800684
685/* T4240 host controller capabilities register should have VS33 bit */
686#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
687 caps = caps | ESDHC_HOSTCAPBLT_VS33;
688#endif
689
Andy Fleming50586ef2008-10-30 16:47:16 -0500690 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000691 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500692 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000693 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500694 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000695 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
696
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200697 cfg->cfg.name = "FSL_SDHC";
698 cfg->cfg.ops = &esdhc_ops;
Li Yang030955c2010-11-25 17:06:09 +0000699#ifdef CONFIG_SYS_SD_VOLTAGE
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200700 cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000701#else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200702 cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000703#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200704 if ((cfg->cfg.voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000705 printf("voltage not supported by controller\n");
706 return -1;
707 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500708
Rob Herring5a203972015-03-23 17:56:59 -0500709 cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500710#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
711 cfg->cfg.host_caps |= MMC_MODE_DDR_52MHz;
712#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500713
Abbas Razaaad46592013-03-25 09:13:34 +0000714 if (cfg->max_bus_width > 0) {
715 if (cfg->max_bus_width < 8)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200716 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000717 if (cfg->max_bus_width < 4)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200718 cfg->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000719 }
720
Andy Fleming50586ef2008-10-30 16:47:16 -0500721 if (caps & ESDHC_HOSTCAPBLT_HSS)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200722 cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500723
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800724#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
725 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200726 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800727#endif
728
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200729 cfg->cfg.f_min = 400000;
Tom Rini21008ad2014-11-26 11:22:29 -0500730 cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500731
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200732 cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
733
734 mmc = mmc_create(&cfg->cfg, cfg);
735 if (mmc == NULL)
736 return -1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500737
738 return 0;
739}
740
741int fsl_esdhc_mmc_init(bd_t *bis)
742{
Stefano Babicc67bee12010-02-05 15:11:27 +0100743 struct fsl_esdhc_cfg *cfg;
744
Fabio Estevam88227a12012-12-27 08:51:08 +0000745 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100746 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000747 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100748 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500749}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400750
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800751#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
752void mmc_adapter_card_type_ident(void)
753{
754 u8 card_id;
755 u8 value;
756
757 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
758 gd->arch.sdhc_adapter = card_id;
759
760 switch (card_id) {
761 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800762 value = QIXIS_READ(brdcfg[5]);
763 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
764 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800765 break;
766 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800767 value = QIXIS_READ(pwr_ctl[1]);
768 value |= QIXIS_EVDD_BY_SDHC_VS;
769 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800770 break;
771 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
772 value = QIXIS_READ(brdcfg[5]);
773 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
774 QIXIS_WRITE(brdcfg[5], value);
775 break;
776 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
777 break;
778 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
779 break;
780 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
781 break;
782 case QIXIS_ESDHC_NO_ADAPTER:
783 break;
784 default:
785 break;
786 }
787}
788#endif
789
Stefano Babicc67bee12010-02-05 15:11:27 +0100790#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400791void fdt_fixup_esdhc(void *blob, bd_t *bd)
792{
793 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400794
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800795#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400796 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800797 do_fixup_by_compat(blob, compat, "status", "disabled",
798 8 + 1, 1);
799 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400800 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800801#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400802
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800803#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
804 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
805 gd->arch.sdhc_clk, 1);
806#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400807 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000808 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800809#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800810#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
811 do_fixup_by_compat_u32(blob, compat, "adapter-type",
812 (u32)(gd->arch.sdhc_adapter), 1);
813#endif
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800814 do_fixup_by_compat(blob, compat, "status", "okay",
815 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400816}
Stefano Babicc67bee12010-02-05 15:11:27 +0100817#endif