blob: 3acf9e88202f6c253f9ada4ea08cce3041ff1c09 [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>
Peng Fan96f04072016-03-25 14:16:56 +080023#include <dm.h>
24#include <asm-generic/gpio.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050025
Andy Fleming50586ef2008-10-30 16:47:16 -050026DECLARE_GLOBAL_DATA_PTR;
27
Ye.Lia3d6e382014-11-04 15:35:49 +080028#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
29 IRQSTATEN_CINT | \
30 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
31 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
32 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
33 IRQSTATEN_DINT)
34
Andy Fleming50586ef2008-10-30 16:47:16 -050035struct fsl_esdhc {
Haijun.Zhang511948b2013-10-30 11:37:55 +080036 uint dsaddr; /* SDMA system address register */
37 uint blkattr; /* Block attributes register */
38 uint cmdarg; /* Command argument register */
39 uint xfertyp; /* Transfer type register */
40 uint cmdrsp0; /* Command response 0 register */
41 uint cmdrsp1; /* Command response 1 register */
42 uint cmdrsp2; /* Command response 2 register */
43 uint cmdrsp3; /* Command response 3 register */
44 uint datport; /* Buffer data port register */
45 uint prsstat; /* Present state register */
46 uint proctl; /* Protocol control register */
47 uint sysctl; /* System Control Register */
48 uint irqstat; /* Interrupt status register */
49 uint irqstaten; /* Interrupt status enable register */
50 uint irqsigen; /* Interrupt signal enable register */
51 uint autoc12err; /* Auto CMD error status register */
52 uint hostcapblt; /* Host controller capabilities register */
53 uint wml; /* Watermark level register */
54 uint mixctrl; /* For USDHC */
55 char reserved1[4]; /* reserved */
56 uint fevt; /* Force event register */
57 uint admaes; /* ADMA error status register */
58 uint adsaddr; /* ADMA system address register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020059 char reserved2[100]; /* reserved */
60 uint vendorspec; /* Vendor Specific register */
Peng Fan323aaaa2015-03-10 15:35:46 +080061 char reserved3[56]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080062 uint hostver; /* Host controller version register */
Haijun.Zhang511948b2013-10-30 11:37:55 +080063 char reserved4[4]; /* reserved */
Otavio Salvadorf022d362015-02-17 10:42:43 -020064 uint dmaerraddr; /* DMA error address register */
Haijun.Zhang511948b2013-10-30 11:37:55 +080065 char reserved5[4]; /* reserved */
Otavio Salvadorf022d362015-02-17 10:42:43 -020066 uint dmaerrattr; /* DMA error attribute register */
67 char reserved6[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080068 uint hostcapblt2; /* Host controller capabilities register 2 */
Otavio Salvadorf022d362015-02-17 10:42:43 -020069 char reserved7[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080070 uint tcr; /* Tuning control register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020071 char reserved8[28]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080072 uint sddirctl; /* SD direction control register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020073 char reserved9[712]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080074 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050075};
76
Peng Fan96f04072016-03-25 14:16:56 +080077/**
78 * struct fsl_esdhc_priv
79 *
80 * @esdhc_regs: registers of the sdhc controller
81 * @sdhc_clk: Current clk of the sdhc controller
82 * @bus_width: bus width, 1bit, 4bit or 8bit
83 * @cfg: mmc config
84 * @mmc: mmc
85 * Following is used when Driver Model is enabled for MMC
86 * @dev: pointer for the device
87 * @non_removable: 0: removable; 1: non-removable
88 * @cd_gpio: gpio for card detection
89 */
90struct fsl_esdhc_priv {
91 struct fsl_esdhc *esdhc_regs;
92 unsigned int sdhc_clk;
93 unsigned int bus_width;
94 struct mmc_config cfg;
95 struct mmc *mmc;
96 struct udevice *dev;
97 int non_removable;
98 struct gpio_desc cd_gpio;
99};
100
Andy Fleming50586ef2008-10-30 16:47:16 -0500101/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +0000102static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500103{
104 uint xfertyp = 0;
105
106 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530107 xfertyp |= XFERTYP_DPSEL;
108#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
109 xfertyp |= XFERTYP_DMAEN;
110#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500111 if (data->blocks > 1) {
112 xfertyp |= XFERTYP_MSBSEL;
113 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600114#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
115 xfertyp |= XFERTYP_AC12EN;
116#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500117 }
118
119 if (data->flags & MMC_DATA_READ)
120 xfertyp |= XFERTYP_DTDSEL;
121 }
122
123 if (cmd->resp_type & MMC_RSP_CRC)
124 xfertyp |= XFERTYP_CCCEN;
125 if (cmd->resp_type & MMC_RSP_OPCODE)
126 xfertyp |= XFERTYP_CICEN;
127 if (cmd->resp_type & MMC_RSP_136)
128 xfertyp |= XFERTYP_RSPTYP_136;
129 else if (cmd->resp_type & MMC_RSP_BUSY)
130 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
131 else if (cmd->resp_type & MMC_RSP_PRESENT)
132 xfertyp |= XFERTYP_RSPTYP_48;
133
Jason Liu4571de32011-03-22 01:32:31 +0000134 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
135 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800136
Andy Fleming50586ef2008-10-30 16:47:16 -0500137 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
138}
139
Dipen Dudhat77c14582009-10-05 15:41:58 +0530140#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
141/*
142 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
143 */
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200144static void
Dipen Dudhat77c14582009-10-05 15:41:58 +0530145esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
146{
Peng Fan96f04072016-03-25 14:16:56 +0800147 struct fsl_esdhc_priv *priv = mmc->priv;
148 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530149 uint blocks;
150 char *buffer;
151 uint databuf;
152 uint size;
153 uint irqstat;
154 uint timeout;
155
156 if (data->flags & MMC_DATA_READ) {
157 blocks = data->blocks;
158 buffer = data->dest;
159 while (blocks) {
160 timeout = PIO_TIMEOUT;
161 size = data->blocksize;
162 irqstat = esdhc_read32(&regs->irqstat);
163 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
164 && --timeout);
165 if (timeout <= 0) {
166 printf("\nData Read 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 irqstat = esdhc_read32(&regs->irqstat);
172 databuf = in_le32(&regs->datport);
173 *((uint *)buffer) = databuf;
174 buffer += 4;
175 size -= 4;
176 }
177 blocks--;
178 }
179 } else {
180 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200181 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530182 while (blocks) {
183 timeout = PIO_TIMEOUT;
184 size = data->blocksize;
185 irqstat = esdhc_read32(&regs->irqstat);
186 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
187 && --timeout);
188 if (timeout <= 0) {
189 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200190 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530191 }
192 while (size && (!(irqstat & IRQSTAT_TC))) {
193 udelay(100); /* Wait before last byte transfer complete */
194 databuf = *((uint *)buffer);
195 buffer += 4;
196 size -= 4;
197 irqstat = esdhc_read32(&regs->irqstat);
198 out_le32(&regs->datport, databuf);
199 }
200 blocks--;
201 }
202 }
203}
204#endif
205
Andy Fleming50586ef2008-10-30 16:47:16 -0500206static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
207{
Andy Fleming50586ef2008-10-30 16:47:16 -0500208 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800209 struct fsl_esdhc_priv *priv = mmc->priv;
210 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800211#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700212 dma_addr_t addr;
213#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200214 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500215
216 wml_value = data->blocksize/4;
217
218 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530219 if (wml_value > WML_RD_WML_MAX)
220 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500221
Roy Zangab467c52010-02-09 18:23:33 +0800222 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
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->dest));
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->dest);
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 } else {
Ye.Li71689772014-02-20 18:00:57 +0800235#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000236 flush_dcache_range((ulong)data->src,
237 (ulong)data->src+data->blocks
238 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800239#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530240 if (wml_value > WML_WR_WML_MAX)
241 wml_value = WML_WR_WML_MAX_VAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100242 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500243 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
244 return TIMEOUT;
245 }
Roy Zangab467c52010-02-09 18:23:33 +0800246
247 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
248 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800249#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800250#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700251 addr = virt_to_phys((void *)(data->src));
252 if (upper_32_bits(addr))
253 printf("Error found for upper 32 bits\n");
254 else
255 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
256#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100257 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800258#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700259#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500260 }
261
Stefano Babicc67bee12010-02-05 15:11:27 +0100262 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500263
264 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530265 /*
266 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
267 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
268 * So, Number of SD Clock cycles for 0.25sec should be minimum
269 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500270 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530271 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500272 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530273 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500274 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530275 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500276 * => timeout + 13 = log2(mmc->clock/4) + 1
277 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800278 *
279 * However, the MMC spec "It is strongly recommended for hosts to
280 * implement more than 500ms timeout value even if the card
281 * indicates the 250ms maximum busy length." Even the previous
282 * value of 300ms is known to be insufficient for some cards.
283 * So, we use
284 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530285 */
Yangbo Lue978a312015-12-30 14:19:30 +0800286 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500287 timeout -= 13;
288
289 if (timeout > 14)
290 timeout = 14;
291
292 if (timeout < 0)
293 timeout = 0;
294
Kumar Gala5103a032011-01-29 15:36:10 -0600295#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
296 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
297 timeout++;
298#endif
299
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800300#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
301 timeout = 0xE;
302#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100303 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500304
305 return 0;
306}
307
Eric Nelsone576bd92012-04-25 14:28:48 +0000308static void check_and_invalidate_dcache_range
309 (struct mmc_cmd *cmd,
310 struct mmc_data *data) {
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800311#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700312 unsigned start = 0;
313#else
Eric Nelsone576bd92012-04-25 14:28:48 +0000314 unsigned start = (unsigned)data->dest ;
Yangbo Lu8b064602015-03-20 19:28:31 -0700315#endif
Eric Nelsone576bd92012-04-25 14:28:48 +0000316 unsigned size = roundup(ARCH_DMA_MINALIGN,
317 data->blocks*data->blocksize);
318 unsigned end = start+size ;
Yangbo Lu8ef0d5c2015-10-26 19:47:55 +0800319#ifdef CONFIG_FSL_LAYERSCAPE
Yangbo Lu8b064602015-03-20 19:28:31 -0700320 dma_addr_t addr;
321
322 addr = virt_to_phys((void *)(data->dest));
323 if (upper_32_bits(addr))
324 printf("Error found for upper 32 bits\n");
325 else
326 start = lower_32_bits(addr);
327#endif
Eric Nelsone576bd92012-04-25 14:28:48 +0000328 invalidate_dcache_range(start, end);
329}
Tom Rini10dc7772014-05-23 09:19:05 -0400330
Andy Fleming50586ef2008-10-30 16:47:16 -0500331/*
332 * Sends a command out on the bus. Takes the mmc pointer,
333 * a command pointer, and an optional data pointer.
334 */
335static int
336esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
337{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500338 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500339 uint xfertyp;
340 uint irqstat;
Peng Fan96f04072016-03-25 14:16:56 +0800341 struct fsl_esdhc_priv *priv = mmc->priv;
342 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500343
Jerry Huangd621da02011-01-06 23:42:19 -0600344#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
345 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
346 return 0;
347#endif
348
Stefano Babicc67bee12010-02-05 15:11:27 +0100349 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500350
351 sync();
352
353 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100354 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
355 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
356 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500357
Stefano Babicc67bee12010-02-05 15:11:27 +0100358 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
359 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500360
361 /* Wait at least 8 SD clock cycles before the next command */
362 /*
363 * Note: This is way more than 8 cycles, but 1ms seems to
364 * resolve timing issues with some cards
365 */
366 udelay(1000);
367
368 /* Set up for a data transfer if we have one */
369 if (data) {
Andy Fleming50586ef2008-10-30 16:47:16 -0500370 err = esdhc_setup_data(mmc, data);
371 if(err)
372 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800373
374 if (data->flags & MMC_DATA_READ)
375 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500376 }
377
378 /* Figure out the transfer arguments */
379 xfertyp = esdhc_xfertyp(cmd, data);
380
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500381 /* Mask all irqs */
382 esdhc_write32(&regs->irqsigen, 0);
383
Andy Fleming50586ef2008-10-30 16:47:16 -0500384 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100385 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000386#if defined(CONFIG_FSL_USDHC)
387 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500388 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
389 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000390 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
391#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100392 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000393#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000394
Andy Fleming50586ef2008-10-30 16:47:16 -0500395 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000396 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100397 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500398
Stefano Babicc67bee12010-02-05 15:11:27 +0100399 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500400
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500401 if (irqstat & CMD_ERR) {
402 err = COMM_ERR;
403 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000404 }
405
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500406 if (irqstat & IRQSTAT_CTOE) {
407 err = TIMEOUT;
408 goto out;
409 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500410
Otavio Salvadorf022d362015-02-17 10:42:43 -0200411 /* Switch voltage to 1.8V if CMD11 succeeded */
412 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
413 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
414
415 printf("Run CMD11 1.8V switch\n");
416 /* Sleep for 5 ms - max time for card to switch to 1.8V */
417 udelay(5000);
418 }
419
Dirk Behme7a5b8022012-03-26 03:13:05 +0000420 /* Workaround for ESDHC errata ENGcm03648 */
421 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800422 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000423
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800424 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000425 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
426 PRSSTAT_DAT0)) {
427 udelay(100);
428 timeout--;
429 }
430
431 if (timeout <= 0) {
432 printf("Timeout waiting for DAT0 to go high!\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500433 err = TIMEOUT;
434 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000435 }
436 }
437
Andy Fleming50586ef2008-10-30 16:47:16 -0500438 /* Copy the response to the response buffer */
439 if (cmd->resp_type & MMC_RSP_136) {
440 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
441
Stefano Babicc67bee12010-02-05 15:11:27 +0100442 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
443 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
444 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
445 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530446 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
447 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
448 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
449 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500450 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100451 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500452
453 /* Wait until all of the blocks are transferred */
454 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530455#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
456 esdhc_pio_read_write(mmc, data);
457#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500458 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100459 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500460
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500461 if (irqstat & IRQSTAT_DTOE) {
462 err = TIMEOUT;
463 goto out;
464 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000465
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500466 if (irqstat & DATA_ERR) {
467 err = COMM_ERR;
468 goto out;
469 }
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000470 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800471
Peng Fan4683b222015-06-25 10:32:26 +0800472 /*
473 * Need invalidate the dcache here again to avoid any
474 * cache-fill during the DMA operations such as the
475 * speculative pre-fetching etc.
476 */
Eric Nelson54899fc2013-04-03 12:31:56 +0000477 if (data->flags & MMC_DATA_READ)
478 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800479#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500480 }
481
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500482out:
483 /* Reset CMD and DATA portions on error */
484 if (err) {
485 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
486 SYSCTL_RSTC);
487 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
488 ;
489
490 if (data) {
491 esdhc_write32(&regs->sysctl,
492 esdhc_read32(&regs->sysctl) |
493 SYSCTL_RSTD);
494 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
495 ;
496 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200497
498 /* If this was CMD11, then notify that power cycle is needed */
499 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
500 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500501 }
502
Stefano Babicc67bee12010-02-05 15:11:27 +0100503 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500504
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500505 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500506}
507
Kim Phillipseafa90a2012-10-29 13:34:44 +0000508static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500509{
Andy Fleming50586ef2008-10-30 16:47:16 -0500510 int div, pre_div;
Peng Fan96f04072016-03-25 14:16:56 +0800511 struct fsl_esdhc_priv *priv = mmc->priv;
512 struct fsl_esdhc *regs = priv->esdhc_regs;
513 int sdhc_clk = priv->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500514 uint clk;
515
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200516 if (clock < mmc->cfg->f_min)
517 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100518
Andy Fleming50586ef2008-10-30 16:47:16 -0500519 if (sdhc_clk / 16 > clock) {
520 for (pre_div = 2; pre_div < 256; pre_div *= 2)
521 if ((sdhc_clk / pre_div) <= (clock * 16))
522 break;
523 } else
524 pre_div = 2;
525
526 for (div = 1; div <= 16; div++)
527 if ((sdhc_clk / (div * pre_div)) <= clock)
528 break;
529
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500530 pre_div >>= mmc->ddr_mode ? 2 : 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500531 div -= 1;
532
533 clk = (pre_div << 8) | (div << 4);
534
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700535#ifdef CONFIG_FSL_USDHC
536 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
537#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500538 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700539#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100540
541 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500542
543 udelay(10000);
544
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700545#ifdef CONFIG_FSL_USDHC
546 esdhc_clrbits32(&regs->sysctl, SYSCTL_RSTA);
547#else
548 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
549#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100550
Andy Fleming50586ef2008-10-30 16:47:16 -0500551}
552
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800553#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
554static void esdhc_clock_control(struct mmc *mmc, bool enable)
555{
Peng Fan96f04072016-03-25 14:16:56 +0800556 struct fsl_esdhc_priv *priv = mmc->priv;
557 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800558 u32 value;
559 u32 time_out;
560
561 value = esdhc_read32(&regs->sysctl);
562
563 if (enable)
564 value |= SYSCTL_CKEN;
565 else
566 value &= ~SYSCTL_CKEN;
567
568 esdhc_write32(&regs->sysctl, value);
569
570 time_out = 20;
571 value = PRSSTAT_SDSTB;
572 while (!(esdhc_read32(&regs->prsstat) & value)) {
573 if (time_out == 0) {
574 printf("fsl_esdhc: Internal clock never stabilised.\n");
575 break;
576 }
577 time_out--;
578 mdelay(1);
579 }
580}
581#endif
582
Andy Fleming50586ef2008-10-30 16:47:16 -0500583static void esdhc_set_ios(struct mmc *mmc)
584{
Peng Fan96f04072016-03-25 14:16:56 +0800585 struct fsl_esdhc_priv *priv = mmc->priv;
586 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500587
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800588#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
589 /* Select to use peripheral clock */
590 esdhc_clock_control(mmc, false);
591 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
592 esdhc_clock_control(mmc, true);
593#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500594 /* Set the clock speed */
595 set_sysctl(mmc, mmc->clock);
596
597 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100598 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500599
600 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100601 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500602 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100603 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
604
Andy Fleming50586ef2008-10-30 16:47:16 -0500605}
606
607static int esdhc_init(struct mmc *mmc)
608{
Peng Fan96f04072016-03-25 14:16:56 +0800609 struct fsl_esdhc_priv *priv = mmc->priv;
610 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500611 int timeout = 1000;
612
Stefano Babicc67bee12010-02-05 15:11:27 +0100613 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200614 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100615
616 /* Wait until the controller is available */
617 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
618 udelay(1000);
619
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000620#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530621 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000622 esdhc_write32(&regs->scr, 0x00000040);
623#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530624
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700625#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +0200626 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700627#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500628
629 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000630 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500631
632 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100633 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500634
635 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100636 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500637
Stefano Babicc67bee12010-02-05 15:11:27 +0100638 /* Set timout to the maximum value */
639 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500640
Otavio Salvadoree0c5382015-02-17 10:42:44 -0200641#ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
642 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
643#endif
644
Thierry Redingd48d2e22012-01-02 01:15:38 +0000645 return 0;
646}
Andy Fleming50586ef2008-10-30 16:47:16 -0500647
Thierry Redingd48d2e22012-01-02 01:15:38 +0000648static int esdhc_getcd(struct mmc *mmc)
649{
Peng Fan96f04072016-03-25 14:16:56 +0800650 struct fsl_esdhc_priv *priv = mmc->priv;
651 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000652 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100653
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800654#ifdef CONFIG_ESDHC_DETECT_QUIRK
655 if (CONFIG_ESDHC_DETECT_QUIRK)
656 return 1;
657#endif
Peng Fan96f04072016-03-25 14:16:56 +0800658
659#ifdef CONFIG_DM_MMC
660 if (priv->non_removable)
661 return 1;
662
663 if (dm_gpio_is_valid(&priv->cd_gpio))
664 return dm_gpio_get_value(&priv->cd_gpio);
665#endif
666
Thierry Redingd48d2e22012-01-02 01:15:38 +0000667 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
668 udelay(1000);
669
670 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500671}
672
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500673static void esdhc_reset(struct fsl_esdhc *regs)
674{
675 unsigned long timeout = 100; /* wait max 100 ms */
676
677 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200678 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500679
680 /* hardware clears the bit when it is done */
681 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
682 udelay(1000);
683 if (!timeout)
684 printf("MMC/SD: Reset never completed.\n");
685}
686
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200687static const struct mmc_ops esdhc_ops = {
688 .send_cmd = esdhc_send_cmd,
689 .set_ios = esdhc_set_ios,
690 .init = esdhc_init,
691 .getcd = esdhc_getcd,
692};
693
Peng Fan96f04072016-03-25 14:16:56 +0800694static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
695 struct fsl_esdhc_priv *priv)
696{
697 if (!cfg || !priv)
698 return -EINVAL;
699
700 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
701 priv->bus_width = cfg->max_bus_width;
702 priv->sdhc_clk = cfg->sdhc_clk;
703
704 return 0;
705};
706
707static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
Andy Fleming50586ef2008-10-30 16:47:16 -0500708{
Stefano Babicc67bee12010-02-05 15:11:27 +0100709 struct fsl_esdhc *regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500710 struct mmc *mmc;
Li Yang030955c2010-11-25 17:06:09 +0000711 u32 caps, voltage_caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500712
Peng Fan96f04072016-03-25 14:16:56 +0800713 if (!priv)
714 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100715
Peng Fan96f04072016-03-25 14:16:56 +0800716 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +0100717
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500718 /* First reset the eSDHC controller */
719 esdhc_reset(regs);
720
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700721#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +0000722 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
723 | SYSCTL_IPGEN | SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700724#endif
Jerry Huang975324a2012-05-17 23:57:02 +0000725
Ye.Lia3d6e382014-11-04 15:35:49 +0800726 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Peng Fan96f04072016-03-25 14:16:56 +0800727 memset(&priv->cfg, 0, sizeof(priv->cfg));
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200728
Li Yang030955c2010-11-25 17:06:09 +0000729 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800730 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600731
732#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
733 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
734 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
735#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800736
737/* T4240 host controller capabilities register should have VS33 bit */
738#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
739 caps = caps | ESDHC_HOSTCAPBLT_VS33;
740#endif
741
Andy Fleming50586ef2008-10-30 16:47:16 -0500742 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000743 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500744 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000745 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500746 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000747 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
748
Peng Fan96f04072016-03-25 14:16:56 +0800749 priv->cfg.name = "FSL_SDHC";
750 priv->cfg.ops = &esdhc_ops;
Li Yang030955c2010-11-25 17:06:09 +0000751#ifdef CONFIG_SYS_SD_VOLTAGE
Peng Fan96f04072016-03-25 14:16:56 +0800752 priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000753#else
Peng Fan96f04072016-03-25 14:16:56 +0800754 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000755#endif
Peng Fan96f04072016-03-25 14:16:56 +0800756 if ((priv->cfg.voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000757 printf("voltage not supported by controller\n");
758 return -1;
759 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500760
Peng Fan96f04072016-03-25 14:16:56 +0800761 if (priv->bus_width == 8)
762 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
763 else if (priv->bus_width == 4)
764 priv->cfg.host_caps = MMC_MODE_4BIT;
765
766 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500767#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Peng Fan96f04072016-03-25 14:16:56 +0800768 priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500769#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500770
Peng Fan96f04072016-03-25 14:16:56 +0800771 if (priv->bus_width > 0) {
772 if (priv->bus_width < 8)
773 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
774 if (priv->bus_width < 4)
775 priv->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000776 }
777
Andy Fleming50586ef2008-10-30 16:47:16 -0500778 if (caps & ESDHC_HOSTCAPBLT_HSS)
Peng Fan96f04072016-03-25 14:16:56 +0800779 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500780
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800781#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
782 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Peng Fan96f04072016-03-25 14:16:56 +0800783 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800784#endif
785
Peng Fan96f04072016-03-25 14:16:56 +0800786 priv->cfg.f_min = 400000;
787 priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500788
Peng Fan96f04072016-03-25 14:16:56 +0800789 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200790
Peng Fan96f04072016-03-25 14:16:56 +0800791 mmc = mmc_create(&priv->cfg, priv);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200792 if (mmc == NULL)
793 return -1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500794
Peng Fan96f04072016-03-25 14:16:56 +0800795 priv->mmc = mmc;
796
797 return 0;
798}
799
800int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
801{
802 struct fsl_esdhc_priv *priv;
803 int ret;
804
805 if (!cfg)
806 return -EINVAL;
807
808 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
809 if (!priv)
810 return -ENOMEM;
811
812 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
813 if (ret) {
814 debug("%s xlate failure\n", __func__);
815 free(priv);
816 return ret;
817 }
818
819 ret = fsl_esdhc_init(priv);
820 if (ret) {
821 debug("%s init failure\n", __func__);
822 free(priv);
823 return ret;
824 }
825
Andy Fleming50586ef2008-10-30 16:47:16 -0500826 return 0;
827}
828
829int fsl_esdhc_mmc_init(bd_t *bis)
830{
Stefano Babicc67bee12010-02-05 15:11:27 +0100831 struct fsl_esdhc_cfg *cfg;
832
Fabio Estevam88227a12012-12-27 08:51:08 +0000833 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100834 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000835 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100836 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500837}
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400838
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800839#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
840void mmc_adapter_card_type_ident(void)
841{
842 u8 card_id;
843 u8 value;
844
845 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
846 gd->arch.sdhc_adapter = card_id;
847
848 switch (card_id) {
849 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800850 value = QIXIS_READ(brdcfg[5]);
851 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
852 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800853 break;
854 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800855 value = QIXIS_READ(pwr_ctl[1]);
856 value |= QIXIS_EVDD_BY_SDHC_VS;
857 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800858 break;
859 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
860 value = QIXIS_READ(brdcfg[5]);
861 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
862 QIXIS_WRITE(brdcfg[5], value);
863 break;
864 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
865 break;
866 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
867 break;
868 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
869 break;
870 case QIXIS_ESDHC_NO_ADAPTER:
871 break;
872 default:
873 break;
874 }
875}
876#endif
877
Stefano Babicc67bee12010-02-05 15:11:27 +0100878#ifdef CONFIG_OF_LIBFDT
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400879void fdt_fixup_esdhc(void *blob, bd_t *bd)
880{
881 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400882
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800883#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400884 if (!hwconfig("esdhc")) {
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800885 do_fixup_by_compat(blob, compat, "status", "disabled",
886 8 + 1, 1);
887 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400888 }
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800889#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400890
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800891#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
892 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
893 gd->arch.sdhc_clk, 1);
894#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400895 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000896 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800897#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800898#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
899 do_fixup_by_compat_u32(blob, compat, "adapter-type",
900 (u32)(gd->arch.sdhc_adapter), 1);
901#endif
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800902 do_fixup_by_compat(blob, compat, "status", "okay",
903 4 + 1, 1);
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400904}
Stefano Babicc67bee12010-02-05 15:11:27 +0100905#endif
Peng Fan96f04072016-03-25 14:16:56 +0800906
907#ifdef CONFIG_DM_MMC
908#include <asm/arch/clock.h>
909static int fsl_esdhc_probe(struct udevice *dev)
910{
911 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
912 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
913 const void *fdt = gd->fdt_blob;
914 int node = dev->of_offset;
915 fdt_addr_t addr;
916 unsigned int val;
917 int ret;
918
919 addr = dev_get_addr(dev);
920 if (addr == FDT_ADDR_T_NONE)
921 return -EINVAL;
922
923 priv->esdhc_regs = (struct fsl_esdhc *)addr;
924 priv->dev = dev;
925
926 val = fdtdec_get_int(fdt, node, "bus-width", -1);
927 if (val == 8)
928 priv->bus_width = 8;
929 else if (val == 4)
930 priv->bus_width = 4;
931 else
932 priv->bus_width = 1;
933
934 if (fdt_get_property(fdt, node, "non-removable", NULL)) {
935 priv->non_removable = 1;
936 } else {
937 priv->non_removable = 0;
938 gpio_request_by_name_nodev(fdt, node, "cd-gpios", 0,
939 &priv->cd_gpio, GPIOD_IS_IN);
940 }
941
942 /*
943 * TODO:
944 * Because lack of clk driver, if SDHC clk is not enabled,
945 * need to enable it first before this driver is invoked.
946 *
947 * we use MXC_ESDHC_CLK to get clk freq.
948 * If one would like to make this function work,
949 * the aliases should be provided in dts as this:
950 *
951 * aliases {
952 * mmc0 = &usdhc1;
953 * mmc1 = &usdhc2;
954 * mmc2 = &usdhc3;
955 * mmc3 = &usdhc4;
956 * };
957 * Then if your board only supports mmc2 and mmc3, but we can
958 * correctly get the seq as 2 and 3, then let mxc_get_clock
959 * work as expected.
960 */
961 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
962 if (priv->sdhc_clk <= 0) {
963 dev_err(dev, "Unable to get clk for %s\n", dev->name);
964 return -EINVAL;
965 }
966
967 ret = fsl_esdhc_init(priv);
968 if (ret) {
969 dev_err(dev, "fsl_esdhc_init failure\n");
970 return ret;
971 }
972
973 upriv->mmc = priv->mmc;
974
975 return 0;
976}
977
978static const struct udevice_id fsl_esdhc_ids[] = {
979 { .compatible = "fsl,imx6ul-usdhc", },
980 { .compatible = "fsl,imx6sx-usdhc", },
981 { .compatible = "fsl,imx6sl-usdhc", },
982 { .compatible = "fsl,imx6q-usdhc", },
983 { .compatible = "fsl,imx7d-usdhc", },
984 { /* sentinel */ }
985};
986
987U_BOOT_DRIVER(fsl_esdhc) = {
988 .name = "fsl-esdhc-mmc",
989 .id = UCLASS_MMC,
990 .of_match = fsl_esdhc_ids,
991 .probe = fsl_esdhc_probe,
992 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
993};
994#endif