blob: ea5f4bf6c0e2fdad333019576a9732e6690f515a [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
Jason Liu4571de32011-03-22 01:32:31 +0000108 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
109 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800110
Andy Fleming50586ef2008-10-30 16:47:16 -0500111 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
112}
113
Dipen Dudhat77c14582009-10-05 15:41:58 +0530114#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
115/*
116 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
117 */
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200118static void
Dipen Dudhat77c14582009-10-05 15:41:58 +0530119esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
120{
Ira Snyder8eee2bd2011-12-23 08:30:40 +0000121 struct fsl_esdhc_cfg *cfg = mmc->priv;
122 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530123 uint blocks;
124 char *buffer;
125 uint databuf;
126 uint size;
127 uint irqstat;
128 uint timeout;
129
130 if (data->flags & MMC_DATA_READ) {
131 blocks = data->blocks;
132 buffer = data->dest;
133 while (blocks) {
134 timeout = PIO_TIMEOUT;
135 size = data->blocksize;
136 irqstat = esdhc_read32(&regs->irqstat);
137 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
138 && --timeout);
139 if (timeout <= 0) {
140 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200141 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530142 }
143 while (size && (!(irqstat & IRQSTAT_TC))) {
144 udelay(100); /* Wait before last byte transfer complete */
145 irqstat = esdhc_read32(&regs->irqstat);
146 databuf = in_le32(&regs->datport);
147 *((uint *)buffer) = databuf;
148 buffer += 4;
149 size -= 4;
150 }
151 blocks--;
152 }
153 } else {
154 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200155 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530156 while (blocks) {
157 timeout = PIO_TIMEOUT;
158 size = data->blocksize;
159 irqstat = esdhc_read32(&regs->irqstat);
160 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
161 && --timeout);
162 if (timeout <= 0) {
163 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200164 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530165 }
166 while (size && (!(irqstat & IRQSTAT_TC))) {
167 udelay(100); /* Wait before last byte transfer complete */
168 databuf = *((uint *)buffer);
169 buffer += 4;
170 size -= 4;
171 irqstat = esdhc_read32(&regs->irqstat);
172 out_le32(&regs->datport, databuf);
173 }
174 blocks--;
175 }
176 }
177}
178#endif
179
Andy Fleming50586ef2008-10-30 16:47:16 -0500180static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
181{
Andy Fleming50586ef2008-10-30 16:47:16 -0500182 int timeout;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200183 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100184 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800185#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700186 dma_addr_t addr;
187#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200188 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500189
190 wml_value = data->blocksize/4;
191
192 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530193 if (wml_value > WML_RD_WML_MAX)
194 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500195
Roy Zangab467c52010-02-09 18:23:33 +0800196 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800197#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800198#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700199 addr = virt_to_phys((void *)(data->dest));
200 if (upper_32_bits(addr))
201 printf("Error found for upper 32 bits\n");
202 else
203 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
204#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100205 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800206#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700207#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500208 } else {
Ye.Li71689772014-02-20 18:00:57 +0800209#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000210 flush_dcache_range((ulong)data->src,
211 (ulong)data->src+data->blocks
212 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800213#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530214 if (wml_value > WML_WR_WML_MAX)
215 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100216 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500217 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
218 return TIMEOUT;
219 }
Roy Zangab467c52010-02-09 18:23:33 +0800220
221 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
222 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800223#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800224#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700225 addr = virt_to_phys((void *)(data->src));
226 if (upper_32_bits(addr))
227 printf("Error found for upper 32 bits\n");
228 else
229 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
230#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100231 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800232#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700233#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500234 }
235
Stefano Babicc67bee12010-02-05 15:11:27 +0100236 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500237
238 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530239 /*
240 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
241 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
242 * So, Number of SD Clock cycles for 0.25sec should be minimum
243 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500244 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530245 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500246 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530247 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500248 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530249 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500250 * => timeout + 13 = log2(mmc->clock/4) + 1
251 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800252 *
253 * However, the MMC spec "It is strongly recommended for hosts to
254 * implement more than 500ms timeout value even if the card
255 * indicates the 250ms maximum busy length." Even the previous
256 * value of 300ms is known to be insufficient for some cards.
257 * So, we use
258 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530259 */
Yangbo Lue978a312015-12-30 14:19:30 +0800260 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500261 timeout -= 13;
262
263 if (timeout > 14)
264 timeout = 14;
265
266 if (timeout < 0)
267 timeout = 0;
268
Kumar Gala5103a032011-01-29 15:36:10 -0600269#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
270 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
271 timeout++;
272#endif
273
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800274#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
275 timeout = 0xE;
276#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100277 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500278
279 return 0;
280}
281
Eric Nelsone576bd92012-04-25 14:28:48 +0000282static void check_and_invalidate_dcache_range
283 (struct mmc_cmd *cmd,
284 struct mmc_data *data) {
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800285#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700286 unsigned start = 0;
287#else
Eric Nelsone576bd92012-04-25 14:28:48 +0000288 unsigned start = (unsigned)data->dest ;
Yangbo Lu8b064602015-03-20 19:28:31 -0700289#endif
Eric Nelsone576bd92012-04-25 14:28:48 +0000290 unsigned size = roundup(ARCH_DMA_MINALIGN,
291 data->blocks*data->blocksize);
292 unsigned end = start+size ;
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800293#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700294 dma_addr_t addr;
295
296 addr = virt_to_phys((void *)(data->dest));
297 if (upper_32_bits(addr))
298 printf("Error found for upper 32 bits\n");
299 else
300 start = lower_32_bits(addr);
301#endif
Eric Nelsone576bd92012-04-25 14:28:48 +0000302 invalidate_dcache_range(start, end);
303}
Tom Rini10dc7772014-05-23 09:19:05 -0400304
Andy Fleming50586ef2008-10-30 16:47:16 -0500305/*
306 * Sends a command out on the bus. Takes the mmc pointer,
307 * a command pointer, and an optional data pointer.
308 */
309static int
310esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
311{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500312 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500313 uint xfertyp;
314 uint irqstat;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200315 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100316 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500317
Jerry Huangd621da02011-01-06 23:42:19 -0600318#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
319 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
320 return 0;
321#endif
322
Stefano Babicc67bee12010-02-05 15:11:27 +0100323 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500324
325 sync();
326
327 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100328 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
329 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
330 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500331
Stefano Babicc67bee12010-02-05 15:11:27 +0100332 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
333 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500334
335 /* Wait at least 8 SD clock cycles before the next command */
336 /*
337 * Note: This is way more than 8 cycles, but 1ms seems to
338 * resolve timing issues with some cards
339 */
340 udelay(1000);
341
342 /* Set up for a data transfer if we have one */
343 if (data) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500344 err = esdhc_setup_data(mmc, data);
345 if(err)
346 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800347
348 if (data->flags & MMC_DATA_READ)
349 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500350 }
351
352 /* Figure out the transfer arguments */
353 xfertyp = esdhc_xfertyp(cmd, data);
354
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500355 /* Mask all irqs */
356 esdhc_write32(&regs->irqsigen, 0);
357
Andy Fleming50586ef2008-10-30 16:47:16 -0500358 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100359 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000360#if defined(CONFIG_FSL_USDHC)
361 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500362 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
363 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000364 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
365#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100366 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000367#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000368
Andy Fleming50586ef2008-10-30 16:47:16 -0500369 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000370 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100371 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500372
Stefano Babicc67bee12010-02-05 15:11:27 +0100373 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500374
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500375 if (irqstat & CMD_ERR) {
376 err = COMM_ERR;
377 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000378 }
379
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500380 if (irqstat & IRQSTAT_CTOE) {
381 err = TIMEOUT;
382 goto out;
383 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500384
Otavio Salvadorf022d362015-02-17 10:42:43 -0200385 /* Switch voltage to 1.8V if CMD11 succeeded */
386 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
387 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
388
389 printf("Run CMD11 1.8V switch\n");
390 /* Sleep for 5 ms - max time for card to switch to 1.8V */
391 udelay(5000);
392 }
393
Dirk Behme7a5b8022012-03-26 03:13:05 +0000394 /* Workaround for ESDHC errata ENGcm03648 */
395 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800396 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000397
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800398 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000399 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
400 PRSSTAT_DAT0)) {
401 udelay(100);
402 timeout--;
403 }
404
405 if (timeout <= 0) {
406 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500407 err = TIMEOUT;
408 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000409 }
410 }
411
Andy Fleming50586ef2008-10-30 16:47:16 -0500412 /* Copy the response to the response buffer */
413 if (cmd->resp_type & MMC_RSP_136) {
414 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
415
Stefano Babicc67bee12010-02-05 15:11:27 +0100416 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
417 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
418 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
419 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530420 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
421 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
422 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
423 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500424 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100425 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500426
427 /* Wait until all of the blocks are transferred */
428 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530429#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
430 esdhc_pio_read_write(mmc, data);
431#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500432 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100433 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500434
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500435 if (irqstat & IRQSTAT_DTOE) {
436 err = TIMEOUT;
437 goto out;
438 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000439
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500440 if (irqstat & DATA_ERR) {
441 err = COMM_ERR;
442 goto out;
443 }
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000444 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800445
Peng Fan4683b222015-06-25 10:32:26 +0800446 /*
447 * Need invalidate the dcache here again to avoid any
448 * cache-fill during the DMA operations such as the
449 * speculative pre-fetching etc.
450 */
Eric Nelson54899fc2013-04-03 12:31:56 +0000451 if (data->flags & MMC_DATA_READ)
452 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800453#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500454 }
455
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500456out:
457 /* Reset CMD and DATA portions on error */
458 if (err) {
459 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
460 SYSCTL_RSTC);
461 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
462 ;
463
464 if (data) {
465 esdhc_write32(&regs->sysctl,
466 esdhc_read32(&regs->sysctl) |
467 SYSCTL_RSTD);
468 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
469 ;
470 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200471
472 /* If this was CMD11, then notify that power cycle is needed */
473 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
474 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500475 }
476
Stefano Babicc67bee12010-02-05 15:11:27 +0100477 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500478
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500479 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500480}
481
Kim Phillipseafa90a2012-10-29 13:34:44 +0000482static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500483{
Andy Fleming50586ef2008-10-30 16:47:16 -0500484 int div, pre_div;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200485 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100486 volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Benoît Thébaudeaua2ac1b32012-10-01 08:36:25 +0000487 int sdhc_clk = cfg->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500488 uint clk;
489
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200490 if (clock < mmc->cfg->f_min)
491 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100492
Andy Fleming50586ef2008-10-30 16:47:16 -0500493 if (sdhc_clk / 16 > clock) {
494 for (pre_div = 2; pre_div < 256; pre_div *= 2)
495 if ((sdhc_clk / pre_div) <= (clock * 16))
496 break;
497 } else
498 pre_div = 2;
499
500 for (div = 1; div <= 16; div++)
501 if ((sdhc_clk / (div * pre_div)) <= clock)
502 break;
503
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500504 pre_div >>= mmc->ddr_mode ? 2 : 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500505 div -= 1;
506
507 clk = (pre_div << 8) | (div << 4);
508
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700509#ifdef CONFIG_FSL_USDHC
510 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
511#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500512 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700513#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100514
515 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500516
517 udelay(10000);
518
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700519#ifdef CONFIG_FSL_USDHC
520 esdhc_clrbits32(&regs->sysctl, SYSCTL_RSTA);
521#else
522 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
523#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100524
Andy Fleming50586ef2008-10-30 16:47:16 -0500525}
526
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800527#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
528static void esdhc_clock_control(struct mmc *mmc, bool enable)
529{
530 struct fsl_esdhc_cfg *cfg = mmc->priv;
531 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
532 u32 value;
533 u32 time_out;
534
535 value = esdhc_read32(&regs->sysctl);
536
537 if (enable)
538 value |= SYSCTL_CKEN;
539 else
540 value &= ~SYSCTL_CKEN;
541
542 esdhc_write32(&regs->sysctl, value);
543
544 time_out = 20;
545 value = PRSSTAT_SDSTB;
546 while (!(esdhc_read32(&regs->prsstat) & value)) {
547 if (time_out == 0) {
548 printf("fsl_esdhc: Internal clock never stabilised.\n");
549 break;
550 }
551 time_out--;
552 mdelay(1);
553 }
554}
555#endif
556
Andy Fleming50586ef2008-10-30 16:47:16 -0500557static void esdhc_set_ios(struct mmc *mmc)
558{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200559 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100560 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500561
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800562#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
563 /* Select to use peripheral clock */
564 esdhc_clock_control(mmc, false);
565 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
566 esdhc_clock_control(mmc, true);
567#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500568 /* Set the clock speed */
569 set_sysctl(mmc, mmc->clock);
570
571 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100572 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500573
574 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100575 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500576 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100577 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
578
Andy Fleming50586ef2008-10-30 16:47:16 -0500579}
580
581static int esdhc_init(struct mmc *mmc)
582{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200583 struct fsl_esdhc_cfg *cfg = mmc->priv;
Stefano Babicc67bee12010-02-05 15:11:27 +0100584 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
Andy Fleming50586ef2008-10-30 16:47:16 -0500585 int timeout = 1000;
586
Stefano Babicc67bee12010-02-05 15:11:27 +0100587 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200588 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100589
590 /* Wait until the controller is available */
591 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
592 udelay(1000);
593
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000594#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530595 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000596 esdhc_write32(&regs->scr, 0x00000040);
597#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530598
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700599#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +0200600 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700601#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500602
603 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000604 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500605
606 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100607 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500608
609 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100610 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500611
Stefano Babicc67bee12010-02-05 15:11:27 +0100612 /* Set timout to the maximum value */
613 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500614
Otavio Salvadoree0c5382015-02-17 10:42:44 -0200615#ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
616 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
617#endif
618
Thierry Redingd48d2e22012-01-02 01:15:38 +0000619 return 0;
620}
Andy Fleming50586ef2008-10-30 16:47:16 -0500621
Thierry Redingd48d2e22012-01-02 01:15:38 +0000622static int esdhc_getcd(struct mmc *mmc)
623{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200624 struct fsl_esdhc_cfg *cfg = mmc->priv;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000625 struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base;
626 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100627
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800628#ifdef CONFIG_ESDHC_DETECT_QUIRK
629 if (CONFIG_ESDHC_DETECT_QUIRK)
630 return 1;
631#endif
Thierry Redingd48d2e22012-01-02 01:15:38 +0000632 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
633 udelay(1000);
634
635 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500636}
637
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500638static void esdhc_reset(struct fsl_esdhc *regs)
639{
640 unsigned long timeout = 100; /* wait max 100 ms */
641
642 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200643 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500644
645 /* hardware clears the bit when it is done */
646 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
647 udelay(1000);
648 if (!timeout)
649 printf("MMC/SD: Reset never completed.\n");
650}
651
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200652static const struct mmc_ops esdhc_ops = {
653 .send_cmd = esdhc_send_cmd,
654 .set_ios = esdhc_set_ios,
655 .init = esdhc_init,
656 .getcd = esdhc_getcd,
657};
658
Stefano Babicc67bee12010-02-05 15:11:27 +0100659int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500660{
Stefano Babicc67bee12010-02-05 15:11:27 +0100661 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500662 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000663 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500664
Stefano Babicc67bee12010-02-05 15:11:27 +0100665 if (!cfg)
666 return -1;
667
Stefano Babicc67bee12010-02-05 15:11:27 +0100668 regs = (struct fsl_esdhc *)cfg->esdhc_base;
669
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500670 /* First reset the eSDHC controller */
671 esdhc_reset(regs);
672
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700673#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +0000674 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
675 | SYSCTL_IPGEN | SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700676#endif
Jerry Huang975324a2012-05-17 23:57:02 +0000677
Ye.Lia3d6e382014-11-04 15:35:49 +0800678 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200679 memset(&cfg->cfg, 0, sizeof(cfg->cfg));
680
Li Yang030955c2010-11-25 17:06:09 +0000681 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800682 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600683
684#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
685 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
686 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
687#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800688
689/* T4240 host controller capabilities register should have VS33 bit */
690#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
691 caps = caps | ESDHC_HOSTCAPBLT_VS33;
692#endif
693
Andy Fleming50586ef2008-10-30 16:47:16 -0500694 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000695 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500696 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000697 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500698 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000699 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
700
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200701 cfg->cfg.name = "FSL_SDHC";
702 cfg->cfg.ops = &esdhc_ops;
Li Yang030955c2010-11-25 17:06:09 +0000703#ifdef CONFIG_SYS_SD_VOLTAGE
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200704 cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000705#else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200706 cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000707#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200708 if ((cfg->cfg.voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000709 printf("voltage not supported by controller\n");
710 return -1;
711 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500712
Rob Herring5a203972015-03-23 17:56:59 -0500713 cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500714#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
715 cfg->cfg.host_caps |= MMC_MODE_DDR_52MHz;
716#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500717
Abbas Razaaad46592013-03-25 09:13:34 +0000718 if (cfg->max_bus_width > 0) {
719 if (cfg->max_bus_width < 8)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200720 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000721 if (cfg->max_bus_width < 4)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200722 cfg->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000723 }
724
Andy Fleming50586ef2008-10-30 16:47:16 -0500725 if (caps & ESDHC_HOSTCAPBLT_HSS)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200726 cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500727
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800728#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
729 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200730 cfg->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800731#endif
732
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200733 cfg->cfg.f_min = 400000;
Tom Rini21008ad2014-11-26 11:22:29 -0500734 cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500735
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200736 cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
737
738 mmc = mmc_create(&cfg->cfg, cfg);
739 if (mmc == NULL)
740 return -1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500741
742 return 0;
743}
744
745int fsl_esdhc_mmc_init(bd_t *bis)
746{
Stefano Babicc67bee12010-02-05 15:11:27 +0100747 struct fsl_esdhc_cfg *cfg;
748
Fabio Estevam88227a12012-12-27 08:51:08 +0000749 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100750 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000751 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100752 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500753}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400754
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800755#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
756void mmc_adapter_card_type_ident(void)
757{
758 u8 card_id;
759 u8 value;
760
761 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
762 gd->arch.sdhc_adapter = card_id;
763
764 switch (card_id) {
765 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800766 value = QIXIS_READ(brdcfg[5]);
767 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
768 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800769 break;
770 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800771 value = QIXIS_READ(pwr_ctl[1]);
772 value |= QIXIS_EVDD_BY_SDHC_VS;
773 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800774 break;
775 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
776 value = QIXIS_READ(brdcfg[5]);
777 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
778 QIXIS_WRITE(brdcfg[5], value);
779 break;
780 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
781 break;
782 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
783 break;
784 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
785 break;
786 case QIXIS_ESDHC_NO_ADAPTER:
787 break;
788 default:
789 break;
790 }
791}
792#endif
793
Stefano Babicc67bee12010-02-05 15:11:27 +0100794#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400795void fdt_fixup_esdhc(void *blob, bd_t *bd)
796{
797 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400798
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800799#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400800 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800801 do_fixup_by_compat(blob, compat, "status", "disabled",
802 8 + 1, 1);
803 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400804 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800805#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400806
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800807#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
808 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
809 gd->arch.sdhc_clk, 1);
810#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400811 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000812 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800813#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800814#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
815 do_fixup_by_compat_u32(blob, compat, "adapter-type",
816 (u32)(gd->arch.sdhc_adapter), 1);
817#endif
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800818 do_fixup_by_compat(blob, compat, "status", "okay",
819 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400820}
Stefano Babicc67bee12010-02-05 15:11:27 +0100821#endif