blob: 73748c5658c6ac8da8c08701020db1ee3c310b67 [file] [log] [blame]
Andy Fleminge52ffb82008-10-30 16:47:16 -05001/*
Jerry Huanged413672011-01-06 23:42:19 -06002 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleminge52ffb82008-10-30 16:47:16 -05003 * Andy Fleming
4 *
5 * Based vaguely on the pxa mmc code:
6 * (C) Copyright 2003
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Andy Fleminge52ffb82008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090015#include <errno.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040016#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050017#include <mmc.h>
18#include <part.h>
19#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050020#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040021#include <fdt_support.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050022#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080023#include <dm.h>
24#include <asm-generic/gpio.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050025
Andy Fleminge52ffb82008-10-30 16:47:16 -050026DECLARE_GLOBAL_DATA_PTR;
27
Ye.Li3d46c312014-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 Fleminge52ffb82008-10-30 16:47:16 -050035struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-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 */
Peng Fana6eadd52016-06-15 10:53:00 +080059 char reserved2[4];
60 uint dllctrl;
61 uint dllstat;
62 uint clktunectrlstatus;
63 char reserved3[84];
64 uint vendorspec;
65 uint mmcboot;
66 uint vendorspec2;
67 char reserved4[48];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080068 uint hostver; /* Host controller version register */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080069 char reserved5[4]; /* reserved */
Peng Fana6eadd52016-06-15 10:53:00 +080070 uint dmaerraddr; /* DMA error address register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020071 char reserved6[4]; /* reserved */
Peng Fana6eadd52016-06-15 10:53:00 +080072 uint dmaerrattr; /* DMA error attribute register */
73 char reserved7[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080074 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fana6eadd52016-06-15 10:53:00 +080075 char reserved8[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080076 uint tcr; /* Tuning control register */
Peng Fana6eadd52016-06-15 10:53:00 +080077 char reserved9[28]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080078 uint sddirctl; /* SD direction control register */
Peng Fana6eadd52016-06-15 10:53:00 +080079 char reserved10[712];/* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080080 uint scr; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050081};
82
Peng Fana4d36f72016-03-25 14:16:56 +080083/**
84 * struct fsl_esdhc_priv
85 *
86 * @esdhc_regs: registers of the sdhc controller
87 * @sdhc_clk: Current clk of the sdhc controller
88 * @bus_width: bus width, 1bit, 4bit or 8bit
89 * @cfg: mmc config
90 * @mmc: mmc
91 * Following is used when Driver Model is enabled for MMC
92 * @dev: pointer for the device
93 * @non_removable: 0: removable; 1: non-removable
Peng Fan01eb1c42016-06-15 10:53:02 +080094 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fana4d36f72016-03-25 14:16:56 +080095 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080096 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080097 */
98struct fsl_esdhc_priv {
99 struct fsl_esdhc *esdhc_regs;
100 unsigned int sdhc_clk;
101 unsigned int bus_width;
102 struct mmc_config cfg;
103 struct mmc *mmc;
104 struct udevice *dev;
105 int non_removable;
Peng Fan01eb1c42016-06-15 10:53:02 +0800106 int wp_enable;
Yangbo Lub99647c2016-12-07 11:54:30 +0800107#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800108 struct gpio_desc cd_gpio;
Peng Fan01eb1c42016-06-15 10:53:02 +0800109 struct gpio_desc wp_gpio;
Yangbo Lub99647c2016-12-07 11:54:30 +0800110#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800111};
112
Andy Fleminge52ffb82008-10-30 16:47:16 -0500113/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000114static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500115{
116 uint xfertyp = 0;
117
118 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530119 xfertyp |= XFERTYP_DPSEL;
120#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
121 xfertyp |= XFERTYP_DMAEN;
122#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500123 if (data->blocks > 1) {
124 xfertyp |= XFERTYP_MSBSEL;
125 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600126#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
127 xfertyp |= XFERTYP_AC12EN;
128#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500129 }
130
131 if (data->flags & MMC_DATA_READ)
132 xfertyp |= XFERTYP_DTDSEL;
133 }
134
135 if (cmd->resp_type & MMC_RSP_CRC)
136 xfertyp |= XFERTYP_CCCEN;
137 if (cmd->resp_type & MMC_RSP_OPCODE)
138 xfertyp |= XFERTYP_CICEN;
139 if (cmd->resp_type & MMC_RSP_136)
140 xfertyp |= XFERTYP_RSPTYP_136;
141 else if (cmd->resp_type & MMC_RSP_BUSY)
142 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
143 else if (cmd->resp_type & MMC_RSP_PRESENT)
144 xfertyp |= XFERTYP_RSPTYP_48;
145
Jason Liubef0ff02011-03-22 01:32:31 +0000146 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
147 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800148
Andy Fleminge52ffb82008-10-30 16:47:16 -0500149 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
150}
151
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530152#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
153/*
154 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
155 */
Wolfgang Denka40545c2010-05-09 23:52:59 +0200156static void
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530157esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
158{
Peng Fana4d36f72016-03-25 14:16:56 +0800159 struct fsl_esdhc_priv *priv = mmc->priv;
160 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530161 uint blocks;
162 char *buffer;
163 uint databuf;
164 uint size;
165 uint irqstat;
166 uint timeout;
167
168 if (data->flags & MMC_DATA_READ) {
169 blocks = data->blocks;
170 buffer = data->dest;
171 while (blocks) {
172 timeout = PIO_TIMEOUT;
173 size = data->blocksize;
174 irqstat = esdhc_read32(&regs->irqstat);
175 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
176 && --timeout);
177 if (timeout <= 0) {
178 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200179 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530180 }
181 while (size && (!(irqstat & IRQSTAT_TC))) {
182 udelay(100); /* Wait before last byte transfer complete */
183 irqstat = esdhc_read32(&regs->irqstat);
184 databuf = in_le32(&regs->datport);
185 *((uint *)buffer) = databuf;
186 buffer += 4;
187 size -= 4;
188 }
189 blocks--;
190 }
191 } else {
192 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200193 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530194 while (blocks) {
195 timeout = PIO_TIMEOUT;
196 size = data->blocksize;
197 irqstat = esdhc_read32(&regs->irqstat);
198 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
199 && --timeout);
200 if (timeout <= 0) {
201 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200202 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530203 }
204 while (size && (!(irqstat & IRQSTAT_TC))) {
205 udelay(100); /* Wait before last byte transfer complete */
206 databuf = *((uint *)buffer);
207 buffer += 4;
208 size -= 4;
209 irqstat = esdhc_read32(&regs->irqstat);
210 out_le32(&regs->datport, databuf);
211 }
212 blocks--;
213 }
214 }
215}
216#endif
217
Andy Fleminge52ffb82008-10-30 16:47:16 -0500218static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
219{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500220 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800221 struct fsl_esdhc_priv *priv = mmc->priv;
222 struct fsl_esdhc *regs = priv->esdhc_regs;
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300223#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700224 dma_addr_t addr;
225#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200226 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500227
228 wml_value = data->blocksize/4;
229
230 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530231 if (wml_value > WML_RD_WML_MAX)
232 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500233
Roy Zange5853af2010-02-09 18:23:33 +0800234 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800235#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300236#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700237 addr = virt_to_phys((void *)(data->dest));
238 if (upper_32_bits(addr))
239 printf("Error found for upper 32 bits\n");
240 else
241 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
242#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100243 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800244#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700245#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500246 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800247#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000248 flush_dcache_range((ulong)data->src,
249 (ulong)data->src+data->blocks
250 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800251#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530252 if (wml_value > WML_WR_WML_MAX)
253 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan01eb1c42016-06-15 10:53:02 +0800254 if (priv->wp_enable) {
255 if ((esdhc_read32(&regs->prsstat) &
256 PRSSTAT_WPSPL) == 0) {
257 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900258 return -ETIMEDOUT;
Peng Fan01eb1c42016-06-15 10:53:02 +0800259 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500260 }
Roy Zange5853af2010-02-09 18:23:33 +0800261
262 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
263 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800264#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300265#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700266 addr = virt_to_phys((void *)(data->src));
267 if (upper_32_bits(addr))
268 printf("Error found for upper 32 bits\n");
269 else
270 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
271#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100272 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800273#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700274#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500275 }
276
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100277 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500278
279 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530280 /*
281 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
282 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
283 * So, Number of SD Clock cycles for 0.25sec should be minimum
284 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500285 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530286 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500287 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530288 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500289 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530290 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500291 * => timeout + 13 = log2(mmc->clock/4) + 1
292 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800293 *
294 * However, the MMC spec "It is strongly recommended for hosts to
295 * implement more than 500ms timeout value even if the card
296 * indicates the 250ms maximum busy length." Even the previous
297 * value of 300ms is known to be insufficient for some cards.
298 * So, we use
299 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530300 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800301 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500302 timeout -= 13;
303
304 if (timeout > 14)
305 timeout = 14;
306
307 if (timeout < 0)
308 timeout = 0;
309
Kumar Gala9a878d52011-01-29 15:36:10 -0600310#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
311 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
312 timeout++;
313#endif
314
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800315#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
316 timeout = 0xE;
317#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100318 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500319
320 return 0;
321}
322
Eric Nelson30e9cad2012-04-25 14:28:48 +0000323static void check_and_invalidate_dcache_range
324 (struct mmc_cmd *cmd,
325 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700326 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800327 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000328 unsigned size = roundup(ARCH_DMA_MINALIGN,
329 data->blocks*data->blocksize);
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300330#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700331 dma_addr_t addr;
332
333 addr = virt_to_phys((void *)(data->dest));
334 if (upper_32_bits(addr))
335 printf("Error found for upper 32 bits\n");
336 else
337 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800338#else
339 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700340#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800341 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000342 invalidate_dcache_range(start, end);
343}
Tom Rini239dd252014-05-23 09:19:05 -0400344
Andy Fleminge52ffb82008-10-30 16:47:16 -0500345/*
346 * Sends a command out on the bus. Takes the mmc pointer,
347 * a command pointer, and an optional data pointer.
348 */
349static int
350esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
351{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500352 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500353 uint xfertyp;
354 uint irqstat;
Peng Fana4d36f72016-03-25 14:16:56 +0800355 struct fsl_esdhc_priv *priv = mmc->priv;
356 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500357
Jerry Huanged413672011-01-06 23:42:19 -0600358#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
359 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
360 return 0;
361#endif
362
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100363 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500364
365 sync();
366
367 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100368 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
369 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
370 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500371
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100372 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
373 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500374
375 /* Wait at least 8 SD clock cycles before the next command */
376 /*
377 * Note: This is way more than 8 cycles, but 1ms seems to
378 * resolve timing issues with some cards
379 */
380 udelay(1000);
381
382 /* Set up for a data transfer if we have one */
383 if (data) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500384 err = esdhc_setup_data(mmc, data);
385 if(err)
386 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800387
388 if (data->flags & MMC_DATA_READ)
389 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500390 }
391
392 /* Figure out the transfer arguments */
393 xfertyp = esdhc_xfertyp(cmd, data);
394
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500395 /* Mask all irqs */
396 esdhc_write32(&regs->irqsigen, 0);
397
Andy Fleminge52ffb82008-10-30 16:47:16 -0500398 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100399 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000400#if defined(CONFIG_FSL_USDHC)
401 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500402 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
403 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu9919d642011-11-25 00:18:04 +0000404 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
405#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100406 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000407#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000408
Andy Fleminge52ffb82008-10-30 16:47:16 -0500409 /* Wait for the command to complete */
Dirk Behmed8552d62012-03-26 03:13:05 +0000410 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100411 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500412
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100413 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500414
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500415 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900416 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500417 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000418 }
419
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500420 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900421 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500422 goto out;
423 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500424
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200425 /* Switch voltage to 1.8V if CMD11 succeeded */
426 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
427 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
428
429 printf("Run CMD11 1.8V switch\n");
430 /* Sleep for 5 ms - max time for card to switch to 1.8V */
431 udelay(5000);
432 }
433
Dirk Behmed8552d62012-03-26 03:13:05 +0000434 /* Workaround for ESDHC errata ENGcm03648 */
435 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800436 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000437
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800438 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000439 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
440 PRSSTAT_DAT0)) {
441 udelay(100);
442 timeout--;
443 }
444
445 if (timeout <= 0) {
446 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900447 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500448 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000449 }
450 }
451
Andy Fleminge52ffb82008-10-30 16:47:16 -0500452 /* Copy the response to the response buffer */
453 if (cmd->resp_type & MMC_RSP_136) {
454 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
455
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100456 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
457 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
458 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
459 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530460 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
461 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
462 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
463 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500464 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100465 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500466
467 /* Wait until all of the blocks are transferred */
468 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530469#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
470 esdhc_pio_read_write(mmc, data);
471#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500472 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100473 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500474
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500475 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900476 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500477 goto out;
478 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000479
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500480 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900481 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500482 goto out;
483 }
Andrew Gabbasov4a929622013-04-07 23:06:08 +0000484 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800485
Peng Fan9cb5e992015-06-25 10:32:26 +0800486 /*
487 * Need invalidate the dcache here again to avoid any
488 * cache-fill during the DMA operations such as the
489 * speculative pre-fetching etc.
490 */
Eric Nelson70e68692013-04-03 12:31:56 +0000491 if (data->flags & MMC_DATA_READ)
492 check_and_invalidate_dcache_range(cmd, data);
Ye.Li33a56b12014-02-20 18:00:57 +0800493#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500494 }
495
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500496out:
497 /* Reset CMD and DATA portions on error */
498 if (err) {
499 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
500 SYSCTL_RSTC);
501 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
502 ;
503
504 if (data) {
505 esdhc_write32(&regs->sysctl,
506 esdhc_read32(&regs->sysctl) |
507 SYSCTL_RSTD);
508 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
509 ;
510 }
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200511
512 /* If this was CMD11, then notify that power cycle is needed */
513 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
514 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500515 }
516
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100517 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500518
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500519 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500520}
521
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000522static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500523{
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200524 int div = 1;
525#ifdef ARCH_MXC
526 int pre_div = 1;
527#else
528 int pre_div = 2;
529#endif
530 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fana4d36f72016-03-25 14:16:56 +0800531 struct fsl_esdhc_priv *priv = mmc->priv;
532 struct fsl_esdhc *regs = priv->esdhc_regs;
533 int sdhc_clk = priv->sdhc_clk;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500534 uint clk;
535
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200536 if (clock < mmc->cfg->f_min)
537 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100538
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200539 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
540 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500541
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200542 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
543 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500544
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200545 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500546 div -= 1;
547
548 clk = (pre_div << 8) | (div << 4);
549
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700550#ifdef CONFIG_FSL_USDHC
Ye Li5a24f292016-06-15 10:53:01 +0800551 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700552#else
Kumar Gala09876a32010-03-18 15:51:05 -0500553 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700554#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100555
556 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500557
558 udelay(10000);
559
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700560#ifdef CONFIG_FSL_USDHC
Ye Li5a24f292016-06-15 10:53:01 +0800561 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700562#else
563 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
564#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100565
Andy Fleminge52ffb82008-10-30 16:47:16 -0500566}
567
Yangbo Lu163beec2015-04-22 13:57:40 +0800568#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
569static void esdhc_clock_control(struct mmc *mmc, bool enable)
570{
Peng Fana4d36f72016-03-25 14:16:56 +0800571 struct fsl_esdhc_priv *priv = mmc->priv;
572 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800573 u32 value;
574 u32 time_out;
575
576 value = esdhc_read32(&regs->sysctl);
577
578 if (enable)
579 value |= SYSCTL_CKEN;
580 else
581 value &= ~SYSCTL_CKEN;
582
583 esdhc_write32(&regs->sysctl, value);
584
585 time_out = 20;
586 value = PRSSTAT_SDSTB;
587 while (!(esdhc_read32(&regs->prsstat) & value)) {
588 if (time_out == 0) {
589 printf("fsl_esdhc: Internal clock never stabilised.\n");
590 break;
591 }
592 time_out--;
593 mdelay(1);
594 }
595}
596#endif
597
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900598static int esdhc_set_ios(struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500599{
Peng Fana4d36f72016-03-25 14:16:56 +0800600 struct fsl_esdhc_priv *priv = mmc->priv;
601 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500602
Yangbo Lu163beec2015-04-22 13:57:40 +0800603#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
604 /* Select to use peripheral clock */
605 esdhc_clock_control(mmc, false);
606 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
607 esdhc_clock_control(mmc, true);
608#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500609 /* Set the clock speed */
610 set_sysctl(mmc, mmc->clock);
611
612 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100613 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500614
615 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100616 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500617 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100618 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
619
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900620 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500621}
622
623static int esdhc_init(struct mmc *mmc)
624{
Peng Fana4d36f72016-03-25 14:16:56 +0800625 struct fsl_esdhc_priv *priv = mmc->priv;
626 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500627 int timeout = 1000;
628
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100629 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200630 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100631
632 /* Wait until the controller is available */
633 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
634 udelay(1000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500635
Peng Fana6eadd52016-06-15 10:53:00 +0800636#if defined(CONFIG_FSL_USDHC)
637 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
638 esdhc_write32(&regs->mmcboot, 0x0);
639 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
640 esdhc_write32(&regs->mixctrl, 0x0);
641 esdhc_write32(&regs->clktunectrlstatus, 0x0);
642
643 /* Put VEND_SPEC to default value */
644 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
645
646 /* Disable DLL_CTRL delay line */
647 esdhc_write32(&regs->dllctrl, 0x0);
648#endif
649
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000650#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530651 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000652 esdhc_write32(&regs->scr, 0x00000040);
653#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530654
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700655#ifndef CONFIG_FSL_USDHC
Dirk Behmedbe67252013-07-15 15:44:29 +0200656 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li5a24f292016-06-15 10:53:01 +0800657#else
658 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700659#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500660
661 /* Set the initial clock speed */
Jerry Huang0caea1a2010-11-25 17:06:07 +0000662 mmc_set_clock(mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500663
664 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100665 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500666
667 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100668 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500669
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100670 /* Set timout to the maximum value */
671 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500672
Otavio Salvador12b2a872015-02-17 10:42:44 -0200673#ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
674 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
675#endif
676
Thierry Reding8cee4c982012-01-02 01:15:38 +0000677 return 0;
678}
679
680static int esdhc_getcd(struct mmc *mmc)
681{
Peng Fana4d36f72016-03-25 14:16:56 +0800682 struct fsl_esdhc_priv *priv = mmc->priv;
683 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000684 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500685
Haijun.Zhang05f58542014-01-10 13:52:17 +0800686#ifdef CONFIG_ESDHC_DETECT_QUIRK
687 if (CONFIG_ESDHC_DETECT_QUIRK)
688 return 1;
689#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800690
691#ifdef CONFIG_DM_MMC
692 if (priv->non_removable)
693 return 1;
Yangbo Lub99647c2016-12-07 11:54:30 +0800694#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800695 if (dm_gpio_is_valid(&priv->cd_gpio))
696 return dm_gpio_get_value(&priv->cd_gpio);
697#endif
Yangbo Lub99647c2016-12-07 11:54:30 +0800698#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800699
Thierry Reding8cee4c982012-01-02 01:15:38 +0000700 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
701 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100702
Thierry Reding8cee4c982012-01-02 01:15:38 +0000703 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500704}
705
Jerry Huangb7ef7562010-03-18 15:57:06 -0500706static void esdhc_reset(struct fsl_esdhc *regs)
707{
708 unsigned long timeout = 100; /* wait max 100 ms */
709
710 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200711 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500712
713 /* hardware clears the bit when it is done */
714 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
715 udelay(1000);
716 if (!timeout)
717 printf("MMC/SD: Reset never completed.\n");
718}
719
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200720static const struct mmc_ops esdhc_ops = {
721 .send_cmd = esdhc_send_cmd,
722 .set_ios = esdhc_set_ios,
723 .init = esdhc_init,
724 .getcd = esdhc_getcd,
725};
726
Peng Fana4d36f72016-03-25 14:16:56 +0800727static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500728{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100729 struct fsl_esdhc *regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500730 struct mmc *mmc;
Li Yangd4933f22010-11-25 17:06:09 +0000731 u32 caps, voltage_caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500732
Peng Fana4d36f72016-03-25 14:16:56 +0800733 if (!priv)
734 return -EINVAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100735
Peng Fana4d36f72016-03-25 14:16:56 +0800736 regs = priv->esdhc_regs;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100737
Jerry Huangb7ef7562010-03-18 15:57:06 -0500738 /* First reset the eSDHC controller */
739 esdhc_reset(regs);
740
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700741#ifndef CONFIG_FSL_USDHC
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000742 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
743 | SYSCTL_IPGEN | SYSCTL_CKEN);
Ye Li5a24f292016-06-15 10:53:01 +0800744#else
745 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
746 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700747#endif
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000748
Ye.Li3d46c312014-11-04 15:35:49 +0800749 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Peng Fana4d36f72016-03-25 14:16:56 +0800750 memset(&priv->cfg, 0, sizeof(priv->cfg));
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200751
Li Yangd4933f22010-11-25 17:06:09 +0000752 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +0800753 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600754
755#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
756 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
757 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
758#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800759
760/* T4240 host controller capabilities register should have VS33 bit */
761#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
762 caps = caps | ESDHC_HOSTCAPBLT_VS33;
763#endif
764
Andy Fleminge52ffb82008-10-30 16:47:16 -0500765 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000766 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500767 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000768 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500769 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000770 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
771
Peng Fana4d36f72016-03-25 14:16:56 +0800772 priv->cfg.name = "FSL_SDHC";
773 priv->cfg.ops = &esdhc_ops;
Li Yangd4933f22010-11-25 17:06:09 +0000774#ifdef CONFIG_SYS_SD_VOLTAGE
Peng Fana4d36f72016-03-25 14:16:56 +0800775 priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +0000776#else
Peng Fana4d36f72016-03-25 14:16:56 +0800777 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000778#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800779 if ((priv->cfg.voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +0000780 printf("voltage not supported by controller\n");
781 return -1;
782 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500783
Peng Fana4d36f72016-03-25 14:16:56 +0800784 if (priv->bus_width == 8)
785 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
786 else if (priv->bus_width == 4)
787 priv->cfg.host_caps = MMC_MODE_4BIT;
788
789 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500790#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Peng Fana4d36f72016-03-25 14:16:56 +0800791 priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500792#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500793
Peng Fana4d36f72016-03-25 14:16:56 +0800794 if (priv->bus_width > 0) {
795 if (priv->bus_width < 8)
796 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
797 if (priv->bus_width < 4)
798 priv->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000799 }
800
Andy Fleminge52ffb82008-10-30 16:47:16 -0500801 if (caps & ESDHC_HOSTCAPBLT_HSS)
Peng Fana4d36f72016-03-25 14:16:56 +0800802 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500803
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800804#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
805 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Peng Fana4d36f72016-03-25 14:16:56 +0800806 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800807#endif
808
Peng Fana4d36f72016-03-25 14:16:56 +0800809 priv->cfg.f_min = 400000;
810 priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500811
Peng Fana4d36f72016-03-25 14:16:56 +0800812 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200813
Peng Fana4d36f72016-03-25 14:16:56 +0800814 mmc = mmc_create(&priv->cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200815 if (mmc == NULL)
816 return -1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500817
Peng Fana4d36f72016-03-25 14:16:56 +0800818 priv->mmc = mmc;
819
820 return 0;
821}
822
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530823#ifndef CONFIG_DM_MMC
824static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
825 struct fsl_esdhc_priv *priv)
826{
827 if (!cfg || !priv)
828 return -EINVAL;
829
830 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
831 priv->bus_width = cfg->max_bus_width;
832 priv->sdhc_clk = cfg->sdhc_clk;
833 priv->wp_enable = cfg->wp_enable;
834
835 return 0;
836};
837
Peng Fana4d36f72016-03-25 14:16:56 +0800838int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
839{
840 struct fsl_esdhc_priv *priv;
841 int ret;
842
843 if (!cfg)
844 return -EINVAL;
845
846 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
847 if (!priv)
848 return -ENOMEM;
849
850 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
851 if (ret) {
852 debug("%s xlate failure\n", __func__);
853 free(priv);
854 return ret;
855 }
856
857 ret = fsl_esdhc_init(priv);
858 if (ret) {
859 debug("%s init failure\n", __func__);
860 free(priv);
861 return ret;
862 }
863
Andy Fleminge52ffb82008-10-30 16:47:16 -0500864 return 0;
865}
866
867int fsl_esdhc_mmc_init(bd_t *bis)
868{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100869 struct fsl_esdhc_cfg *cfg;
870
Fabio Estevam6592a992012-12-27 08:51:08 +0000871 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100872 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000873 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100874 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500875}
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530876#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400877
Yangbo Lub124f8a2015-04-22 13:57:00 +0800878#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
879void mmc_adapter_card_type_ident(void)
880{
881 u8 card_id;
882 u8 value;
883
884 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
885 gd->arch.sdhc_adapter = card_id;
886
887 switch (card_id) {
888 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lu81eacd62015-09-17 10:27:12 +0800889 value = QIXIS_READ(brdcfg[5]);
890 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
891 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800892 break;
893 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Luc6799ce2015-09-17 10:27:48 +0800894 value = QIXIS_READ(pwr_ctl[1]);
895 value |= QIXIS_EVDD_BY_SDHC_VS;
896 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800897 break;
898 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
899 value = QIXIS_READ(brdcfg[5]);
900 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
901 QIXIS_WRITE(brdcfg[5], value);
902 break;
903 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
904 break;
905 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
906 break;
907 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
908 break;
909 case QIXIS_ESDHC_NO_ADAPTER:
910 break;
911 default:
912 break;
913 }
914}
915#endif
916
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100917#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800918__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400919{
Chenhui Zhao025eab02011-01-04 17:23:05 +0800920#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400921 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800922 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800923 sizeof("disabled"), 1);
924 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400925 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800926#endif
Yangbo Lud84139c2017-01-17 10:43:54 +0800927 do_fixup_by_compat(blob, compat, "status", "okay",
928 sizeof("okay"), 1);
929 return 0;
930}
931
932void fdt_fixup_esdhc(void *blob, bd_t *bd)
933{
934 const char *compat = "fsl,esdhc";
935
936 if (esdhc_status_fixup(blob, compat))
937 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400938
Yangbo Lu163beec2015-04-22 13:57:40 +0800939#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
940 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
941 gd->arch.sdhc_clk, 1);
942#else
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400943 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000944 gd->arch.sdhc_clk, 1);
Yangbo Lu163beec2015-04-22 13:57:40 +0800945#endif
Yangbo Lub124f8a2015-04-22 13:57:00 +0800946#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
947 do_fixup_by_compat_u32(blob, compat, "adapter-type",
948 (u32)(gd->arch.sdhc_adapter), 1);
949#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400950}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100951#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800952
953#ifdef CONFIG_DM_MMC
954#include <asm/arch/clock.h>
Peng Fanaf6dbc02017-02-22 16:21:55 +0800955__weak void init_clk_usdhc(u32 index)
956{
957}
958
Peng Fana4d36f72016-03-25 14:16:56 +0800959static int fsl_esdhc_probe(struct udevice *dev)
960{
961 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
962 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
963 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700964 int node = dev_of_offset(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800965 fdt_addr_t addr;
966 unsigned int val;
967 int ret;
968
Simon Glassba1dea42017-05-17 17:18:05 -0600969 addr = devfdt_get_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800970 if (addr == FDT_ADDR_T_NONE)
971 return -EINVAL;
972
973 priv->esdhc_regs = (struct fsl_esdhc *)addr;
974 priv->dev = dev;
975
976 val = fdtdec_get_int(fdt, node, "bus-width", -1);
977 if (val == 8)
978 priv->bus_width = 8;
979 else if (val == 4)
980 priv->bus_width = 4;
981 else
982 priv->bus_width = 1;
983
984 if (fdt_get_property(fdt, node, "non-removable", NULL)) {
985 priv->non_removable = 1;
986 } else {
987 priv->non_removable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +0800988#ifdef CONFIG_DM_GPIO
Simon Glass1d9af1f2017-05-30 21:47:09 -0600989 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios",
990 0, &priv->cd_gpio, GPIOD_IS_IN);
Yangbo Lub99647c2016-12-07 11:54:30 +0800991#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800992 }
993
Peng Fan01eb1c42016-06-15 10:53:02 +0800994 priv->wp_enable = 1;
995
Yangbo Lub99647c2016-12-07 11:54:30 +0800996#ifdef CONFIG_DM_GPIO
Simon Glass1d9af1f2017-05-30 21:47:09 -0600997 ret = gpio_request_by_name_nodev(offset_to_ofnode(node), "wp-gpios", 0,
Peng Fan01eb1c42016-06-15 10:53:02 +0800998 &priv->wp_gpio, GPIOD_IS_IN);
999 if (ret)
1000 priv->wp_enable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +08001001#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001002 /*
1003 * TODO:
1004 * Because lack of clk driver, if SDHC clk is not enabled,
1005 * need to enable it first before this driver is invoked.
1006 *
1007 * we use MXC_ESDHC_CLK to get clk freq.
1008 * If one would like to make this function work,
1009 * the aliases should be provided in dts as this:
1010 *
1011 * aliases {
1012 * mmc0 = &usdhc1;
1013 * mmc1 = &usdhc2;
1014 * mmc2 = &usdhc3;
1015 * mmc3 = &usdhc4;
1016 * };
1017 * Then if your board only supports mmc2 and mmc3, but we can
1018 * correctly get the seq as 2 and 3, then let mxc_get_clock
1019 * work as expected.
1020 */
Peng Fanaf6dbc02017-02-22 16:21:55 +08001021
1022 init_clk_usdhc(dev->seq);
1023
Peng Fana4d36f72016-03-25 14:16:56 +08001024 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1025 if (priv->sdhc_clk <= 0) {
1026 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1027 return -EINVAL;
1028 }
1029
1030 ret = fsl_esdhc_init(priv);
1031 if (ret) {
1032 dev_err(dev, "fsl_esdhc_init failure\n");
1033 return ret;
1034 }
1035
1036 upriv->mmc = priv->mmc;
Peng Fand0a0c1d2016-08-11 14:02:56 +08001037 priv->mmc->dev = dev;
Peng Fana4d36f72016-03-25 14:16:56 +08001038
1039 return 0;
1040}
1041
1042static const struct udevice_id fsl_esdhc_ids[] = {
1043 { .compatible = "fsl,imx6ul-usdhc", },
1044 { .compatible = "fsl,imx6sx-usdhc", },
1045 { .compatible = "fsl,imx6sl-usdhc", },
1046 { .compatible = "fsl,imx6q-usdhc", },
1047 { .compatible = "fsl,imx7d-usdhc", },
Peng Fanaf6dbc02017-02-22 16:21:55 +08001048 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lu2a99b602016-12-07 11:54:31 +08001049 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001050 { /* sentinel */ }
1051};
1052
1053U_BOOT_DRIVER(fsl_esdhc) = {
1054 .name = "fsl-esdhc-mmc",
1055 .id = UCLASS_MMC,
1056 .of_match = fsl_esdhc_ids,
1057 .probe = fsl_esdhc_probe,
1058 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1059};
1060#endif