blob: 1e1e92d7409d3488fa9995d094f39b5d2e62137f [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>
Peng Fan5eb8b432017-06-12 17:50:54 +080019#include <power/regulator.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050020#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040022#include <fdt_support.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050023#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080024#include <dm.h>
25#include <asm-generic/gpio.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050026
Andy Fleminge52ffb82008-10-30 16:47:16 -050027DECLARE_GLOBAL_DATA_PTR;
28
Ye.Li3d46c312014-11-04 15:35:49 +080029#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
30 IRQSTATEN_CINT | \
31 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
32 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
33 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
34 IRQSTATEN_DINT)
35
Andy Fleminge52ffb82008-10-30 16:47:16 -050036struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080037 uint dsaddr; /* SDMA system address register */
38 uint blkattr; /* Block attributes register */
39 uint cmdarg; /* Command argument register */
40 uint xfertyp; /* Transfer type register */
41 uint cmdrsp0; /* Command response 0 register */
42 uint cmdrsp1; /* Command response 1 register */
43 uint cmdrsp2; /* Command response 2 register */
44 uint cmdrsp3; /* Command response 3 register */
45 uint datport; /* Buffer data port register */
46 uint prsstat; /* Present state register */
47 uint proctl; /* Protocol control register */
48 uint sysctl; /* System Control Register */
49 uint irqstat; /* Interrupt status register */
50 uint irqstaten; /* Interrupt status enable register */
51 uint irqsigen; /* Interrupt signal enable register */
52 uint autoc12err; /* Auto CMD error status register */
53 uint hostcapblt; /* Host controller capabilities register */
54 uint wml; /* Watermark level register */
55 uint mixctrl; /* For USDHC */
56 char reserved1[4]; /* reserved */
57 uint fevt; /* Force event register */
58 uint admaes; /* ADMA error status register */
59 uint adsaddr; /* ADMA system address register */
Peng Fana6eadd52016-06-15 10:53:00 +080060 char reserved2[4];
61 uint dllctrl;
62 uint dllstat;
63 uint clktunectrlstatus;
64 char reserved3[84];
65 uint vendorspec;
66 uint mmcboot;
67 uint vendorspec2;
68 char reserved4[48];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080069 uint hostver; /* Host controller version register */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080070 char reserved5[4]; /* reserved */
Peng Fana6eadd52016-06-15 10:53:00 +080071 uint dmaerraddr; /* DMA error address register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020072 char reserved6[4]; /* reserved */
Peng Fana6eadd52016-06-15 10:53:00 +080073 uint dmaerrattr; /* DMA error attribute register */
74 char reserved7[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080075 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fana6eadd52016-06-15 10:53:00 +080076 char reserved8[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080077 uint tcr; /* Tuning control register */
Peng Fana6eadd52016-06-15 10:53:00 +080078 char reserved9[28]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080079 uint sddirctl; /* SD direction control register */
Peng Fana6eadd52016-06-15 10:53:00 +080080 char reserved10[712];/* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080081 uint scr; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050082};
83
Peng Fana4d36f72016-03-25 14:16:56 +080084/**
85 * struct fsl_esdhc_priv
86 *
87 * @esdhc_regs: registers of the sdhc controller
88 * @sdhc_clk: Current clk of the sdhc controller
89 * @bus_width: bus width, 1bit, 4bit or 8bit
90 * @cfg: mmc config
91 * @mmc: mmc
92 * Following is used when Driver Model is enabled for MMC
93 * @dev: pointer for the device
94 * @non_removable: 0: removable; 1: non-removable
Peng Fan01eb1c42016-06-15 10:53:02 +080095 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fanaee78582017-06-12 17:50:53 +080096 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fana4d36f72016-03-25 14:16:56 +080097 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080098 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080099 */
100struct fsl_esdhc_priv {
101 struct fsl_esdhc *esdhc_regs;
102 unsigned int sdhc_clk;
103 unsigned int bus_width;
104 struct mmc_config cfg;
105 struct mmc *mmc;
106 struct udevice *dev;
107 int non_removable;
Peng Fan01eb1c42016-06-15 10:53:02 +0800108 int wp_enable;
Peng Fanaee78582017-06-12 17:50:53 +0800109 int vs18_enable;
Yangbo Lub99647c2016-12-07 11:54:30 +0800110#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800111 struct gpio_desc cd_gpio;
Peng Fan01eb1c42016-06-15 10:53:02 +0800112 struct gpio_desc wp_gpio;
Yangbo Lub99647c2016-12-07 11:54:30 +0800113#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800114};
115
Andy Fleminge52ffb82008-10-30 16:47:16 -0500116/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000117static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500118{
119 uint xfertyp = 0;
120
121 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530122 xfertyp |= XFERTYP_DPSEL;
123#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
124 xfertyp |= XFERTYP_DMAEN;
125#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500126 if (data->blocks > 1) {
127 xfertyp |= XFERTYP_MSBSEL;
128 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600129#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
130 xfertyp |= XFERTYP_AC12EN;
131#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500132 }
133
134 if (data->flags & MMC_DATA_READ)
135 xfertyp |= XFERTYP_DTDSEL;
136 }
137
138 if (cmd->resp_type & MMC_RSP_CRC)
139 xfertyp |= XFERTYP_CCCEN;
140 if (cmd->resp_type & MMC_RSP_OPCODE)
141 xfertyp |= XFERTYP_CICEN;
142 if (cmd->resp_type & MMC_RSP_136)
143 xfertyp |= XFERTYP_RSPTYP_136;
144 else if (cmd->resp_type & MMC_RSP_BUSY)
145 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
146 else if (cmd->resp_type & MMC_RSP_PRESENT)
147 xfertyp |= XFERTYP_RSPTYP_48;
148
Jason Liubef0ff02011-03-22 01:32:31 +0000149 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
150 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800151
Andy Fleminge52ffb82008-10-30 16:47:16 -0500152 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
153}
154
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530155#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
156/*
157 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
158 */
Simon Glass1d177d42017-07-29 11:35:17 -0600159static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
160 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530161{
Peng Fana4d36f72016-03-25 14:16:56 +0800162 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530163 uint blocks;
164 char *buffer;
165 uint databuf;
166 uint size;
167 uint irqstat;
168 uint timeout;
169
170 if (data->flags & MMC_DATA_READ) {
171 blocks = data->blocks;
172 buffer = data->dest;
173 while (blocks) {
174 timeout = PIO_TIMEOUT;
175 size = data->blocksize;
176 irqstat = esdhc_read32(&regs->irqstat);
177 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
178 && --timeout);
179 if (timeout <= 0) {
180 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200181 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530182 }
183 while (size && (!(irqstat & IRQSTAT_TC))) {
184 udelay(100); /* Wait before last byte transfer complete */
185 irqstat = esdhc_read32(&regs->irqstat);
186 databuf = in_le32(&regs->datport);
187 *((uint *)buffer) = databuf;
188 buffer += 4;
189 size -= 4;
190 }
191 blocks--;
192 }
193 } else {
194 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200195 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530196 while (blocks) {
197 timeout = PIO_TIMEOUT;
198 size = data->blocksize;
199 irqstat = esdhc_read32(&regs->irqstat);
200 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
201 && --timeout);
202 if (timeout <= 0) {
203 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200204 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530205 }
206 while (size && (!(irqstat & IRQSTAT_TC))) {
207 udelay(100); /* Wait before last byte transfer complete */
208 databuf = *((uint *)buffer);
209 buffer += 4;
210 size -= 4;
211 irqstat = esdhc_read32(&regs->irqstat);
212 out_le32(&regs->datport, databuf);
213 }
214 blocks--;
215 }
216 }
217}
218#endif
219
Simon Glass1d177d42017-07-29 11:35:17 -0600220static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
221 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500222{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500223 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800224 struct fsl_esdhc *regs = priv->esdhc_regs;
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300225#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700226 dma_addr_t addr;
227#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200228 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500229
230 wml_value = data->blocksize/4;
231
232 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530233 if (wml_value > WML_RD_WML_MAX)
234 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500235
Roy Zange5853af2010-02-09 18:23:33 +0800236 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800237#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300238#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700239 addr = virt_to_phys((void *)(data->dest));
240 if (upper_32_bits(addr))
241 printf("Error found for upper 32 bits\n");
242 else
243 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
244#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100245 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800246#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700247#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500248 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800249#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000250 flush_dcache_range((ulong)data->src,
251 (ulong)data->src+data->blocks
252 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800253#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530254 if (wml_value > WML_WR_WML_MAX)
255 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan01eb1c42016-06-15 10:53:02 +0800256 if (priv->wp_enable) {
257 if ((esdhc_read32(&regs->prsstat) &
258 PRSSTAT_WPSPL) == 0) {
259 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900260 return -ETIMEDOUT;
Peng Fan01eb1c42016-06-15 10:53:02 +0800261 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500262 }
Roy Zange5853af2010-02-09 18:23:33 +0800263
264 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
265 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800266#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300267#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700268 addr = virt_to_phys((void *)(data->src));
269 if (upper_32_bits(addr))
270 printf("Error found for upper 32 bits\n");
271 else
272 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
273#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100274 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800275#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700276#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500277 }
278
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100279 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500280
281 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530282 /*
283 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
284 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
285 * So, Number of SD Clock cycles for 0.25sec should be minimum
286 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500287 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530288 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500289 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530290 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500291 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530292 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500293 * => timeout + 13 = log2(mmc->clock/4) + 1
294 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800295 *
296 * However, the MMC spec "It is strongly recommended for hosts to
297 * implement more than 500ms timeout value even if the card
298 * indicates the 250ms maximum busy length." Even the previous
299 * value of 300ms is known to be insufficient for some cards.
300 * So, we use
301 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530302 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800303 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500304 timeout -= 13;
305
306 if (timeout > 14)
307 timeout = 14;
308
309 if (timeout < 0)
310 timeout = 0;
311
Kumar Gala9a878d52011-01-29 15:36:10 -0600312#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
313 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
314 timeout++;
315#endif
316
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800317#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
318 timeout = 0xE;
319#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100320 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500321
322 return 0;
323}
324
Eric Nelson30e9cad2012-04-25 14:28:48 +0000325static void check_and_invalidate_dcache_range
326 (struct mmc_cmd *cmd,
327 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700328 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800329 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000330 unsigned size = roundup(ARCH_DMA_MINALIGN,
331 data->blocks*data->blocksize);
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300332#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700333 dma_addr_t addr;
334
335 addr = virt_to_phys((void *)(data->dest));
336 if (upper_32_bits(addr))
337 printf("Error found for upper 32 bits\n");
338 else
339 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800340#else
341 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700342#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800343 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000344 invalidate_dcache_range(start, end);
345}
Tom Rini239dd252014-05-23 09:19:05 -0400346
Andy Fleminge52ffb82008-10-30 16:47:16 -0500347/*
348 * Sends a command out on the bus. Takes the mmc pointer,
349 * a command pointer, and an optional data pointer.
350 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600351static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
352 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500353{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500354 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500355 uint xfertyp;
356 uint irqstat;
Peng Fana4d36f72016-03-25 14:16:56 +0800357 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500358
Jerry Huanged413672011-01-06 23:42:19 -0600359#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
360 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
361 return 0;
362#endif
363
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100364 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500365
366 sync();
367
368 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100369 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
370 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
371 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500372
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100373 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
374 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500375
376 /* Wait at least 8 SD clock cycles before the next command */
377 /*
378 * Note: This is way more than 8 cycles, but 1ms seems to
379 * resolve timing issues with some cards
380 */
381 udelay(1000);
382
383 /* Set up for a data transfer if we have one */
384 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600385 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500386 if(err)
387 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800388
389 if (data->flags & MMC_DATA_READ)
390 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500391 }
392
393 /* Figure out the transfer arguments */
394 xfertyp = esdhc_xfertyp(cmd, data);
395
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500396 /* Mask all irqs */
397 esdhc_write32(&regs->irqsigen, 0);
398
Andy Fleminge52ffb82008-10-30 16:47:16 -0500399 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100400 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000401#if defined(CONFIG_FSL_USDHC)
402 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500403 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
404 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu9919d642011-11-25 00:18:04 +0000405 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
406#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100407 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000408#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000409
Andy Fleminge52ffb82008-10-30 16:47:16 -0500410 /* Wait for the command to complete */
Dirk Behmed8552d62012-03-26 03:13:05 +0000411 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100412 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500413
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100414 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500415
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500416 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900417 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500418 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000419 }
420
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500421 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900422 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500423 goto out;
424 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500425
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200426 /* Switch voltage to 1.8V if CMD11 succeeded */
427 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
428 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
429
430 printf("Run CMD11 1.8V switch\n");
431 /* Sleep for 5 ms - max time for card to switch to 1.8V */
432 udelay(5000);
433 }
434
Dirk Behmed8552d62012-03-26 03:13:05 +0000435 /* Workaround for ESDHC errata ENGcm03648 */
436 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800437 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000438
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800439 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000440 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
441 PRSSTAT_DAT0)) {
442 udelay(100);
443 timeout--;
444 }
445
446 if (timeout <= 0) {
447 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900448 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500449 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000450 }
451 }
452
Andy Fleminge52ffb82008-10-30 16:47:16 -0500453 /* Copy the response to the response buffer */
454 if (cmd->resp_type & MMC_RSP_136) {
455 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
456
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100457 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
458 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
459 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
460 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530461 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
462 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
463 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
464 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500465 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100466 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500467
468 /* Wait until all of the blocks are transferred */
469 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530470#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass1d177d42017-07-29 11:35:17 -0600471 esdhc_pio_read_write(priv, data);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530472#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500473 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100474 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500476 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900477 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500478 goto out;
479 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000480
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500481 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900482 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500483 goto out;
484 }
Andrew Gabbasov4a929622013-04-07 23:06:08 +0000485 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800486
Peng Fan9cb5e992015-06-25 10:32:26 +0800487 /*
488 * Need invalidate the dcache here again to avoid any
489 * cache-fill during the DMA operations such as the
490 * speculative pre-fetching etc.
491 */
Eric Nelson70e68692013-04-03 12:31:56 +0000492 if (data->flags & MMC_DATA_READ)
493 check_and_invalidate_dcache_range(cmd, data);
Ye.Li33a56b12014-02-20 18:00:57 +0800494#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500495 }
496
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500497out:
498 /* Reset CMD and DATA portions on error */
499 if (err) {
500 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
501 SYSCTL_RSTC);
502 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
503 ;
504
505 if (data) {
506 esdhc_write32(&regs->sysctl,
507 esdhc_read32(&regs->sysctl) |
508 SYSCTL_RSTD);
509 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
510 ;
511 }
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200512
513 /* If this was CMD11, then notify that power cycle is needed */
514 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
515 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500516 }
517
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100518 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500519
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500520 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500521}
522
Simon Glass1d177d42017-07-29 11:35:17 -0600523static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500524{
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200525 int div = 1;
526#ifdef ARCH_MXC
527 int pre_div = 1;
528#else
529 int pre_div = 2;
530#endif
531 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fana4d36f72016-03-25 14:16:56 +0800532 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
Simon Glass1d177d42017-07-29 11:35:17 -0600569static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800570{
Peng Fana4d36f72016-03-25 14:16:56 +0800571 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800572 u32 value;
573 u32 time_out;
574
575 value = esdhc_read32(&regs->sysctl);
576
577 if (enable)
578 value |= SYSCTL_CKEN;
579 else
580 value &= ~SYSCTL_CKEN;
581
582 esdhc_write32(&regs->sysctl, value);
583
584 time_out = 20;
585 value = PRSSTAT_SDSTB;
586 while (!(esdhc_read32(&regs->prsstat) & value)) {
587 if (time_out == 0) {
588 printf("fsl_esdhc: Internal clock never stabilised.\n");
589 break;
590 }
591 time_out--;
592 mdelay(1);
593 }
594}
595#endif
596
Simon Glass6aa55dc2017-07-29 11:35:18 -0600597static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500598{
Peng Fana4d36f72016-03-25 14:16:56 +0800599 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500600
Yangbo Lu163beec2015-04-22 13:57:40 +0800601#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
602 /* Select to use peripheral clock */
Simon Glass1d177d42017-07-29 11:35:17 -0600603 esdhc_clock_control(priv, false);
Yangbo Lu163beec2015-04-22 13:57:40 +0800604 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
Simon Glass1d177d42017-07-29 11:35:17 -0600605 esdhc_clock_control(priv, true);
Yangbo Lu163beec2015-04-22 13:57:40 +0800606#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500607 /* Set the clock speed */
Simon Glass1d177d42017-07-29 11:35:17 -0600608 set_sysctl(priv, mmc, mmc->clock);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500609
610 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100611 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500612
613 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100614 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500615 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100616 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
617
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900618 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500619}
620
Simon Glass6aa55dc2017-07-29 11:35:18 -0600621static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500622{
Peng Fana4d36f72016-03-25 14:16:56 +0800623 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500624 int timeout = 1000;
625
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100626 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200627 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100628
629 /* Wait until the controller is available */
630 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
631 udelay(1000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500632
Peng Fana6eadd52016-06-15 10:53:00 +0800633#if defined(CONFIG_FSL_USDHC)
634 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
635 esdhc_write32(&regs->mmcboot, 0x0);
636 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
637 esdhc_write32(&regs->mixctrl, 0x0);
638 esdhc_write32(&regs->clktunectrlstatus, 0x0);
639
640 /* Put VEND_SPEC to default value */
641 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
642
643 /* Disable DLL_CTRL delay line */
644 esdhc_write32(&regs->dllctrl, 0x0);
645#endif
646
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000647#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530648 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000649 esdhc_write32(&regs->scr, 0x00000040);
650#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530651
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700652#ifndef CONFIG_FSL_USDHC
Dirk Behmedbe67252013-07-15 15:44:29 +0200653 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li5a24f292016-06-15 10:53:01 +0800654#else
655 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700656#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500657
658 /* Set the initial clock speed */
Jerry Huang0caea1a2010-11-25 17:06:07 +0000659 mmc_set_clock(mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500660
661 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100662 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500663
664 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100665 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500666
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100667 /* Set timout to the maximum value */
668 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500669
Peng Fanaee78582017-06-12 17:50:53 +0800670 if (priv->vs18_enable)
671 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
672
Thierry Reding8cee4c982012-01-02 01:15:38 +0000673 return 0;
674}
675
Simon Glass6aa55dc2017-07-29 11:35:18 -0600676static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Reding8cee4c982012-01-02 01:15:38 +0000677{
Peng Fana4d36f72016-03-25 14:16:56 +0800678 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000679 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500680
Haijun.Zhang05f58542014-01-10 13:52:17 +0800681#ifdef CONFIG_ESDHC_DETECT_QUIRK
682 if (CONFIG_ESDHC_DETECT_QUIRK)
683 return 1;
684#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800685
686#ifdef CONFIG_DM_MMC
687 if (priv->non_removable)
688 return 1;
Yangbo Lub99647c2016-12-07 11:54:30 +0800689#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800690 if (dm_gpio_is_valid(&priv->cd_gpio))
691 return dm_gpio_get_value(&priv->cd_gpio);
692#endif
Yangbo Lub99647c2016-12-07 11:54:30 +0800693#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800694
Thierry Reding8cee4c982012-01-02 01:15:38 +0000695 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
696 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100697
Thierry Reding8cee4c982012-01-02 01:15:38 +0000698 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500699}
700
Simon Glass81357b52017-07-29 11:35:19 -0600701static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huangb7ef7562010-03-18 15:57:06 -0500702{
Simon Glass81357b52017-07-29 11:35:19 -0600703 ulong start;
Jerry Huangb7ef7562010-03-18 15:57:06 -0500704
705 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200706 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500707
708 /* hardware clears the bit when it is done */
Simon Glass81357b52017-07-29 11:35:19 -0600709 start = get_timer(0);
710 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
711 if (get_timer(start) > 100) {
712 printf("MMC/SD: Reset never completed.\n");
713 return -ETIMEDOUT;
714 }
715 }
716
717 return 0;
Jerry Huangb7ef7562010-03-18 15:57:06 -0500718}
719
Simon Glass6aa55dc2017-07-29 11:35:18 -0600720static int esdhc_getcd(struct mmc *mmc)
721{
722 struct fsl_esdhc_priv *priv = mmc->priv;
723
724 return esdhc_getcd_common(priv);
725}
726
727static int esdhc_init(struct mmc *mmc)
728{
729 struct fsl_esdhc_priv *priv = mmc->priv;
730
731 return esdhc_init_common(priv, mmc);
732}
733
734static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
735 struct mmc_data *data)
736{
737 struct fsl_esdhc_priv *priv = mmc->priv;
738
739 return esdhc_send_cmd_common(priv, mmc, cmd, data);
740}
741
742static int esdhc_set_ios(struct mmc *mmc)
743{
744 struct fsl_esdhc_priv *priv = mmc->priv;
745
746 return esdhc_set_ios_common(priv, mmc);
747}
748
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200749static const struct mmc_ops esdhc_ops = {
Simon Glass6aa55dc2017-07-29 11:35:18 -0600750 .getcd = esdhc_getcd,
751 .init = esdhc_init,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200752 .send_cmd = esdhc_send_cmd,
753 .set_ios = esdhc_set_ios,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200754};
755
Peng Fana4d36f72016-03-25 14:16:56 +0800756static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500757{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100758 struct fsl_esdhc *regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500759 struct mmc *mmc;
Li Yangd4933f22010-11-25 17:06:09 +0000760 u32 caps, voltage_caps;
Simon Glass81357b52017-07-29 11:35:19 -0600761 int ret;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500762
Peng Fana4d36f72016-03-25 14:16:56 +0800763 if (!priv)
764 return -EINVAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100765
Peng Fana4d36f72016-03-25 14:16:56 +0800766 regs = priv->esdhc_regs;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100767
Jerry Huangb7ef7562010-03-18 15:57:06 -0500768 /* First reset the eSDHC controller */
Simon Glass81357b52017-07-29 11:35:19 -0600769 ret = esdhc_reset(regs);
770 if (ret)
771 return ret;
Jerry Huangb7ef7562010-03-18 15:57:06 -0500772
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700773#ifndef CONFIG_FSL_USDHC
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000774 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
775 | SYSCTL_IPGEN | SYSCTL_CKEN);
Ye Li5a24f292016-06-15 10:53:01 +0800776#else
777 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
778 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700779#endif
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000780
Peng Fanaee78582017-06-12 17:50:53 +0800781 if (priv->vs18_enable)
782 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
783
Ye.Li3d46c312014-11-04 15:35:49 +0800784 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Peng Fana4d36f72016-03-25 14:16:56 +0800785 memset(&priv->cfg, 0, sizeof(priv->cfg));
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200786
Li Yangd4933f22010-11-25 17:06:09 +0000787 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +0800788 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600789
790#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
791 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
792 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
793#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800794
795/* T4240 host controller capabilities register should have VS33 bit */
796#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
797 caps = caps | ESDHC_HOSTCAPBLT_VS33;
798#endif
799
Andy Fleminge52ffb82008-10-30 16:47:16 -0500800 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000801 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500802 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000803 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500804 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000805 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
806
Peng Fana4d36f72016-03-25 14:16:56 +0800807 priv->cfg.name = "FSL_SDHC";
808 priv->cfg.ops = &esdhc_ops;
Li Yangd4933f22010-11-25 17:06:09 +0000809#ifdef CONFIG_SYS_SD_VOLTAGE
Peng Fana4d36f72016-03-25 14:16:56 +0800810 priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +0000811#else
Peng Fana4d36f72016-03-25 14:16:56 +0800812 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000813#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800814 if ((priv->cfg.voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +0000815 printf("voltage not supported by controller\n");
816 return -1;
817 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500818
Peng Fana4d36f72016-03-25 14:16:56 +0800819 if (priv->bus_width == 8)
820 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
821 else if (priv->bus_width == 4)
822 priv->cfg.host_caps = MMC_MODE_4BIT;
823
824 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500825#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Peng Fana4d36f72016-03-25 14:16:56 +0800826 priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500827#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500828
Peng Fana4d36f72016-03-25 14:16:56 +0800829 if (priv->bus_width > 0) {
830 if (priv->bus_width < 8)
831 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
832 if (priv->bus_width < 4)
833 priv->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000834 }
835
Andy Fleminge52ffb82008-10-30 16:47:16 -0500836 if (caps & ESDHC_HOSTCAPBLT_HSS)
Peng Fana4d36f72016-03-25 14:16:56 +0800837 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500838
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800839#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
840 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Peng Fana4d36f72016-03-25 14:16:56 +0800841 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800842#endif
843
Peng Fana4d36f72016-03-25 14:16:56 +0800844 priv->cfg.f_min = 400000;
845 priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500846
Peng Fana4d36f72016-03-25 14:16:56 +0800847 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200848
Peng Fana4d36f72016-03-25 14:16:56 +0800849 mmc = mmc_create(&priv->cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200850 if (mmc == NULL)
851 return -1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500852
Peng Fana4d36f72016-03-25 14:16:56 +0800853 priv->mmc = mmc;
854
855 return 0;
856}
857
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530858#ifndef CONFIG_DM_MMC
859static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
860 struct fsl_esdhc_priv *priv)
861{
862 if (!cfg || !priv)
863 return -EINVAL;
864
865 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
866 priv->bus_width = cfg->max_bus_width;
867 priv->sdhc_clk = cfg->sdhc_clk;
868 priv->wp_enable = cfg->wp_enable;
Peng Fanaee78582017-06-12 17:50:53 +0800869 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530870
871 return 0;
872};
873
Peng Fana4d36f72016-03-25 14:16:56 +0800874int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
875{
876 struct fsl_esdhc_priv *priv;
877 int ret;
878
879 if (!cfg)
880 return -EINVAL;
881
882 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
883 if (!priv)
884 return -ENOMEM;
885
886 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
887 if (ret) {
888 debug("%s xlate failure\n", __func__);
889 free(priv);
890 return ret;
891 }
892
893 ret = fsl_esdhc_init(priv);
894 if (ret) {
895 debug("%s init failure\n", __func__);
896 free(priv);
897 return ret;
898 }
899
Andy Fleminge52ffb82008-10-30 16:47:16 -0500900 return 0;
901}
902
903int fsl_esdhc_mmc_init(bd_t *bis)
904{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100905 struct fsl_esdhc_cfg *cfg;
906
Fabio Estevam6592a992012-12-27 08:51:08 +0000907 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100908 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000909 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100910 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500911}
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530912#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400913
Yangbo Lub124f8a2015-04-22 13:57:00 +0800914#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
915void mmc_adapter_card_type_ident(void)
916{
917 u8 card_id;
918 u8 value;
919
920 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
921 gd->arch.sdhc_adapter = card_id;
922
923 switch (card_id) {
924 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lu81eacd62015-09-17 10:27:12 +0800925 value = QIXIS_READ(brdcfg[5]);
926 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
927 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800928 break;
929 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Luc6799ce2015-09-17 10:27:48 +0800930 value = QIXIS_READ(pwr_ctl[1]);
931 value |= QIXIS_EVDD_BY_SDHC_VS;
932 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800933 break;
934 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
935 value = QIXIS_READ(brdcfg[5]);
936 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
937 QIXIS_WRITE(brdcfg[5], value);
938 break;
939 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
940 break;
941 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
942 break;
943 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
944 break;
945 case QIXIS_ESDHC_NO_ADAPTER:
946 break;
947 default:
948 break;
949 }
950}
951#endif
952
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100953#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800954__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400955{
Chenhui Zhao025eab02011-01-04 17:23:05 +0800956#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400957 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800958 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800959 sizeof("disabled"), 1);
960 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400961 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800962#endif
Yangbo Lud84139c2017-01-17 10:43:54 +0800963 return 0;
964}
965
966void fdt_fixup_esdhc(void *blob, bd_t *bd)
967{
968 const char *compat = "fsl,esdhc";
969
970 if (esdhc_status_fixup(blob, compat))
971 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400972
Yangbo Lu163beec2015-04-22 13:57:40 +0800973#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
974 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
975 gd->arch.sdhc_clk, 1);
976#else
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400977 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000978 gd->arch.sdhc_clk, 1);
Yangbo Lu163beec2015-04-22 13:57:40 +0800979#endif
Yangbo Lub124f8a2015-04-22 13:57:00 +0800980#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
981 do_fixup_by_compat_u32(blob, compat, "adapter-type",
982 (u32)(gd->arch.sdhc_adapter), 1);
983#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400984}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100985#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800986
987#ifdef CONFIG_DM_MMC
988#include <asm/arch/clock.h>
Peng Fanaf6dbc02017-02-22 16:21:55 +0800989__weak void init_clk_usdhc(u32 index)
990{
991}
992
Peng Fana4d36f72016-03-25 14:16:56 +0800993static int fsl_esdhc_probe(struct udevice *dev)
994{
995 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
996 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
997 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700998 int node = dev_of_offset(dev);
York Sun107a5e42017-08-08 15:45:13 -0700999#ifdef CONFIG_DM_REGULATOR
Peng Fan5eb8b432017-06-12 17:50:54 +08001000 struct udevice *vqmmc_dev;
York Sun107a5e42017-08-08 15:45:13 -07001001#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001002 fdt_addr_t addr;
1003 unsigned int val;
1004 int ret;
1005
Simon Glassba1dea42017-05-17 17:18:05 -06001006 addr = devfdt_get_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +08001007 if (addr == FDT_ADDR_T_NONE)
1008 return -EINVAL;
1009
1010 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1011 priv->dev = dev;
1012
1013 val = fdtdec_get_int(fdt, node, "bus-width", -1);
1014 if (val == 8)
1015 priv->bus_width = 8;
1016 else if (val == 4)
1017 priv->bus_width = 4;
1018 else
1019 priv->bus_width = 1;
1020
1021 if (fdt_get_property(fdt, node, "non-removable", NULL)) {
1022 priv->non_removable = 1;
1023 } else {
1024 priv->non_removable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +08001025#ifdef CONFIG_DM_GPIO
Simon Glass1d9af1f2017-05-30 21:47:09 -06001026 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios",
1027 0, &priv->cd_gpio, GPIOD_IS_IN);
Yangbo Lub99647c2016-12-07 11:54:30 +08001028#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001029 }
1030
Peng Fan01eb1c42016-06-15 10:53:02 +08001031 priv->wp_enable = 1;
1032
Yangbo Lub99647c2016-12-07 11:54:30 +08001033#ifdef CONFIG_DM_GPIO
Simon Glass1d9af1f2017-05-30 21:47:09 -06001034 ret = gpio_request_by_name_nodev(offset_to_ofnode(node), "wp-gpios", 0,
Peng Fan01eb1c42016-06-15 10:53:02 +08001035 &priv->wp_gpio, GPIOD_IS_IN);
1036 if (ret)
1037 priv->wp_enable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +08001038#endif
Peng Fan5eb8b432017-06-12 17:50:54 +08001039
1040 priv->vs18_enable = 0;
1041
1042#ifdef CONFIG_DM_REGULATOR
1043 /*
1044 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1045 * otherwise, emmc will work abnormally.
1046 */
1047 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1048 if (ret) {
1049 dev_dbg(dev, "no vqmmc-supply\n");
1050 } else {
1051 ret = regulator_set_enable(vqmmc_dev, true);
1052 if (ret) {
1053 dev_err(dev, "fail to enable vqmmc-supply\n");
1054 return ret;
1055 }
1056
1057 if (regulator_get_value(vqmmc_dev) == 1800000)
1058 priv->vs18_enable = 1;
1059 }
1060#endif
1061
Peng Fana4d36f72016-03-25 14:16:56 +08001062 /*
1063 * TODO:
1064 * Because lack of clk driver, if SDHC clk is not enabled,
1065 * need to enable it first before this driver is invoked.
1066 *
1067 * we use MXC_ESDHC_CLK to get clk freq.
1068 * If one would like to make this function work,
1069 * the aliases should be provided in dts as this:
1070 *
1071 * aliases {
1072 * mmc0 = &usdhc1;
1073 * mmc1 = &usdhc2;
1074 * mmc2 = &usdhc3;
1075 * mmc3 = &usdhc4;
1076 * };
1077 * Then if your board only supports mmc2 and mmc3, but we can
1078 * correctly get the seq as 2 and 3, then let mxc_get_clock
1079 * work as expected.
1080 */
Peng Fanaf6dbc02017-02-22 16:21:55 +08001081
1082 init_clk_usdhc(dev->seq);
1083
Peng Fana4d36f72016-03-25 14:16:56 +08001084 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1085 if (priv->sdhc_clk <= 0) {
1086 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1087 return -EINVAL;
1088 }
1089
1090 ret = fsl_esdhc_init(priv);
1091 if (ret) {
1092 dev_err(dev, "fsl_esdhc_init failure\n");
1093 return ret;
1094 }
1095
1096 upriv->mmc = priv->mmc;
Peng Fand0a0c1d2016-08-11 14:02:56 +08001097 priv->mmc->dev = dev;
Peng Fana4d36f72016-03-25 14:16:56 +08001098
1099 return 0;
1100}
1101
1102static const struct udevice_id fsl_esdhc_ids[] = {
1103 { .compatible = "fsl,imx6ul-usdhc", },
1104 { .compatible = "fsl,imx6sx-usdhc", },
1105 { .compatible = "fsl,imx6sl-usdhc", },
1106 { .compatible = "fsl,imx6q-usdhc", },
1107 { .compatible = "fsl,imx7d-usdhc", },
Peng Fanaf6dbc02017-02-22 16:21:55 +08001108 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lu2a99b602016-12-07 11:54:31 +08001109 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001110 { /* sentinel */ }
1111};
1112
1113U_BOOT_DRIVER(fsl_esdhc) = {
1114 .name = "fsl-esdhc-mmc",
1115 .id = UCLASS_MMC,
1116 .of_match = fsl_esdhc_ids,
1117 .probe = fsl_esdhc_probe,
1118 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1119};
1120#endif