blob: d4257b29eeaf7d3aea93fca39aa55d3f64fd782e [file] [log] [blame]
Andy Fleming50586ef2008-10-30 16:47:16 -05001/*
Jerry Huangd621da02011-01-06 23:42:19 -06002 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleming50586ef2008-10-30 16:47:16 -05003 * Andy Fleming
4 *
5 * Based vaguely on the pxa mmc code:
6 * (C) Copyright 2003
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming50586ef2008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Jaehoon Chung915ffa52016-07-19 16:33:36 +090015#include <errno.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040016#include <hwconfig.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050017#include <mmc.h>
18#include <part.h>
Peng Fan4483b7e2017-06-12 17:50:54 +080019#include <power/regulator.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050020#include <malloc.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040022#include <fdt_support.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050023#include <asm/io.h>
Peng Fan96f04072016-03-25 14:16:56 +080024#include <dm.h>
25#include <asm-generic/gpio.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050026
Andy Fleming50586ef2008-10-30 16:47:16 -050027DECLARE_GLOBAL_DATA_PTR;
28
Ye.Lia3d6e382014-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 Fleming50586ef2008-10-30 16:47:16 -050036struct fsl_esdhc {
Haijun.Zhang511948b2013-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 Fanf53225c2016-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.Zhang511948b2013-10-30 11:37:55 +080069 uint hostver; /* Host controller version register */
Haijun.Zhang511948b2013-10-30 11:37:55 +080070 char reserved5[4]; /* reserved */
Peng Fanf53225c2016-06-15 10:53:00 +080071 uint dmaerraddr; /* DMA error address register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020072 char reserved6[4]; /* reserved */
Peng Fanf53225c2016-06-15 10:53:00 +080073 uint dmaerrattr; /* DMA error attribute register */
74 char reserved7[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080075 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fanf53225c2016-06-15 10:53:00 +080076 char reserved8[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080077 uint tcr; /* Tuning control register */
Peng Fanf53225c2016-06-15 10:53:00 +080078 char reserved9[28]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080079 uint sddirctl; /* SD direction control register */
Peng Fanf53225c2016-06-15 10:53:00 +080080 char reserved10[712];/* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080081 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050082};
83
Simon Glasse88e1d92017-07-29 11:35:21 -060084struct fsl_esdhc_plat {
85 struct mmc_config cfg;
86 struct mmc mmc;
87};
88
Peng Fan96f04072016-03-25 14:16:56 +080089/**
90 * struct fsl_esdhc_priv
91 *
92 * @esdhc_regs: registers of the sdhc controller
93 * @sdhc_clk: Current clk of the sdhc controller
94 * @bus_width: bus width, 1bit, 4bit or 8bit
95 * @cfg: mmc config
96 * @mmc: mmc
97 * Following is used when Driver Model is enabled for MMC
98 * @dev: pointer for the device
99 * @non_removable: 0: removable; 1: non-removable
Peng Fan14831512016-06-15 10:53:02 +0800100 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fan32a91792017-06-12 17:50:53 +0800101 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fan96f04072016-03-25 14:16:56 +0800102 * @cd_gpio: gpio for card detection
Peng Fan14831512016-06-15 10:53:02 +0800103 * @wp_gpio: gpio for write protection
Peng Fan96f04072016-03-25 14:16:56 +0800104 */
105struct fsl_esdhc_priv {
106 struct fsl_esdhc *esdhc_regs;
107 unsigned int sdhc_clk;
108 unsigned int bus_width;
Peng Fan96f04072016-03-25 14:16:56 +0800109 struct mmc *mmc;
110 struct udevice *dev;
111 int non_removable;
Peng Fan14831512016-06-15 10:53:02 +0800112 int wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +0800113 int vs18_enable;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800114#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +0800115 struct gpio_desc cd_gpio;
Peng Fan14831512016-06-15 10:53:02 +0800116 struct gpio_desc wp_gpio;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800117#endif
Peng Fan96f04072016-03-25 14:16:56 +0800118};
119
Andy Fleming50586ef2008-10-30 16:47:16 -0500120/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +0000121static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500122{
123 uint xfertyp = 0;
124
125 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530126 xfertyp |= XFERTYP_DPSEL;
127#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
128 xfertyp |= XFERTYP_DMAEN;
129#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500130 if (data->blocks > 1) {
131 xfertyp |= XFERTYP_MSBSEL;
132 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600133#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
134 xfertyp |= XFERTYP_AC12EN;
135#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500136 }
137
138 if (data->flags & MMC_DATA_READ)
139 xfertyp |= XFERTYP_DTDSEL;
140 }
141
142 if (cmd->resp_type & MMC_RSP_CRC)
143 xfertyp |= XFERTYP_CCCEN;
144 if (cmd->resp_type & MMC_RSP_OPCODE)
145 xfertyp |= XFERTYP_CICEN;
146 if (cmd->resp_type & MMC_RSP_136)
147 xfertyp |= XFERTYP_RSPTYP_136;
148 else if (cmd->resp_type & MMC_RSP_BUSY)
149 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
150 else if (cmd->resp_type & MMC_RSP_PRESENT)
151 xfertyp |= XFERTYP_RSPTYP_48;
152
Jason Liu4571de32011-03-22 01:32:31 +0000153 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
154 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800155
Andy Fleming50586ef2008-10-30 16:47:16 -0500156 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
157}
158
Dipen Dudhat77c14582009-10-05 15:41:58 +0530159#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
160/*
161 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
162 */
Simon Glass09b465f2017-07-29 11:35:17 -0600163static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
164 struct mmc_data *data)
Dipen Dudhat77c14582009-10-05 15:41:58 +0530165{
Peng Fan96f04072016-03-25 14:16:56 +0800166 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530167 uint blocks;
168 char *buffer;
169 uint databuf;
170 uint size;
171 uint irqstat;
172 uint timeout;
173
174 if (data->flags & MMC_DATA_READ) {
175 blocks = data->blocks;
176 buffer = data->dest;
177 while (blocks) {
178 timeout = PIO_TIMEOUT;
179 size = data->blocksize;
180 irqstat = esdhc_read32(&regs->irqstat);
181 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
182 && --timeout);
183 if (timeout <= 0) {
184 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200185 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530186 }
187 while (size && (!(irqstat & IRQSTAT_TC))) {
188 udelay(100); /* Wait before last byte transfer complete */
189 irqstat = esdhc_read32(&regs->irqstat);
190 databuf = in_le32(&regs->datport);
191 *((uint *)buffer) = databuf;
192 buffer += 4;
193 size -= 4;
194 }
195 blocks--;
196 }
197 } else {
198 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200199 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530200 while (blocks) {
201 timeout = PIO_TIMEOUT;
202 size = data->blocksize;
203 irqstat = esdhc_read32(&regs->irqstat);
204 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
205 && --timeout);
206 if (timeout <= 0) {
207 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200208 return;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530209 }
210 while (size && (!(irqstat & IRQSTAT_TC))) {
211 udelay(100); /* Wait before last byte transfer complete */
212 databuf = *((uint *)buffer);
213 buffer += 4;
214 size -= 4;
215 irqstat = esdhc_read32(&regs->irqstat);
216 out_le32(&regs->datport, databuf);
217 }
218 blocks--;
219 }
220 }
221}
222#endif
223
Simon Glass09b465f2017-07-29 11:35:17 -0600224static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
225 struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500226{
Andy Fleming50586ef2008-10-30 16:47:16 -0500227 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800228 struct fsl_esdhc *regs = priv->esdhc_regs;
Eddy Petrișor9702ec02016-06-05 03:43:00 +0300229#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lu8b064602015-03-20 19:28:31 -0700230 dma_addr_t addr;
231#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200232 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500233
234 wml_value = data->blocksize/4;
235
236 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530237 if (wml_value > WML_RD_WML_MAX)
238 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500239
Roy Zangab467c52010-02-09 18:23:33 +0800240 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800241#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor9702ec02016-06-05 03:43:00 +0300242#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lu8b064602015-03-20 19:28:31 -0700243 addr = virt_to_phys((void *)(data->dest));
244 if (upper_32_bits(addr))
245 printf("Error found for upper 32 bits\n");
246 else
247 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
248#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100249 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800250#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700251#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500252 } else {
Ye.Li71689772014-02-20 18:00:57 +0800253#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000254 flush_dcache_range((ulong)data->src,
255 (ulong)data->src+data->blocks
256 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800257#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530258 if (wml_value > WML_WR_WML_MAX)
259 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan14831512016-06-15 10:53:02 +0800260 if (priv->wp_enable) {
261 if ((esdhc_read32(&regs->prsstat) &
262 PRSSTAT_WPSPL) == 0) {
263 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900264 return -ETIMEDOUT;
Peng Fan14831512016-06-15 10:53:02 +0800265 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500266 }
Roy Zangab467c52010-02-09 18:23:33 +0800267
268 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
269 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800270#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor9702ec02016-06-05 03:43:00 +0300271#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lu8b064602015-03-20 19:28:31 -0700272 addr = virt_to_phys((void *)(data->src));
273 if (upper_32_bits(addr))
274 printf("Error found for upper 32 bits\n");
275 else
276 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
277#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100278 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800279#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700280#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500281 }
282
Stefano Babicc67bee12010-02-05 15:11:27 +0100283 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500284
285 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530286 /*
287 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
288 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
289 * So, Number of SD Clock cycles for 0.25sec should be minimum
290 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500291 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530292 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500293 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530294 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500295 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530296 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500297 * => timeout + 13 = log2(mmc->clock/4) + 1
298 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800299 *
300 * However, the MMC spec "It is strongly recommended for hosts to
301 * implement more than 500ms timeout value even if the card
302 * indicates the 250ms maximum busy length." Even the previous
303 * value of 300ms is known to be insufficient for some cards.
304 * So, we use
305 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530306 */
Yangbo Lue978a312015-12-30 14:19:30 +0800307 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500308 timeout -= 13;
309
310 if (timeout > 14)
311 timeout = 14;
312
313 if (timeout < 0)
314 timeout = 0;
315
Kumar Gala5103a032011-01-29 15:36:10 -0600316#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
317 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
318 timeout++;
319#endif
320
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800321#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
322 timeout = 0xE;
323#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100324 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500325
326 return 0;
327}
328
Eric Nelsone576bd92012-04-25 14:28:48 +0000329static void check_and_invalidate_dcache_range
330 (struct mmc_cmd *cmd,
331 struct mmc_data *data) {
Yangbo Lu8b064602015-03-20 19:28:31 -0700332 unsigned start = 0;
Yangbo Lucc634e22016-05-12 19:12:58 +0800333 unsigned end = 0;
Eric Nelsone576bd92012-04-25 14:28:48 +0000334 unsigned size = roundup(ARCH_DMA_MINALIGN,
335 data->blocks*data->blocksize);
Eddy Petrișor9702ec02016-06-05 03:43:00 +0300336#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lu8b064602015-03-20 19:28:31 -0700337 dma_addr_t addr;
338
339 addr = virt_to_phys((void *)(data->dest));
340 if (upper_32_bits(addr))
341 printf("Error found for upper 32 bits\n");
342 else
343 start = lower_32_bits(addr);
Yangbo Lucc634e22016-05-12 19:12:58 +0800344#else
345 start = (unsigned)data->dest;
Yangbo Lu8b064602015-03-20 19:28:31 -0700346#endif
Yangbo Lucc634e22016-05-12 19:12:58 +0800347 end = start + size;
Eric Nelsone576bd92012-04-25 14:28:48 +0000348 invalidate_dcache_range(start, end);
349}
Tom Rini10dc7772014-05-23 09:19:05 -0400350
Andy Fleming50586ef2008-10-30 16:47:16 -0500351/*
352 * Sends a command out on the bus. Takes the mmc pointer,
353 * a command pointer, and an optional data pointer.
354 */
Simon Glass9586aa62017-07-29 11:35:18 -0600355static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
356 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500357{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500358 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500359 uint xfertyp;
360 uint irqstat;
Peng Fan96f04072016-03-25 14:16:56 +0800361 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500362
Jerry Huangd621da02011-01-06 23:42:19 -0600363#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
364 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
365 return 0;
366#endif
367
Stefano Babicc67bee12010-02-05 15:11:27 +0100368 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500369
370 sync();
371
372 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100373 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
374 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
375 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500376
Stefano Babicc67bee12010-02-05 15:11:27 +0100377 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
378 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500379
380 /* Wait at least 8 SD clock cycles before the next command */
381 /*
382 * Note: This is way more than 8 cycles, but 1ms seems to
383 * resolve timing issues with some cards
384 */
385 udelay(1000);
386
387 /* Set up for a data transfer if we have one */
388 if (data) {
Simon Glass09b465f2017-07-29 11:35:17 -0600389 err = esdhc_setup_data(priv, mmc, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500390 if(err)
391 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800392
393 if (data->flags & MMC_DATA_READ)
394 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500395 }
396
397 /* Figure out the transfer arguments */
398 xfertyp = esdhc_xfertyp(cmd, data);
399
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500400 /* Mask all irqs */
401 esdhc_write32(&regs->irqsigen, 0);
402
Andy Fleming50586ef2008-10-30 16:47:16 -0500403 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100404 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000405#if defined(CONFIG_FSL_USDHC)
406 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500407 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
408 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000409 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
410#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100411 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000412#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000413
Andy Fleming50586ef2008-10-30 16:47:16 -0500414 /* Wait for the command to complete */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000415 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicc67bee12010-02-05 15:11:27 +0100416 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500417
Stefano Babicc67bee12010-02-05 15:11:27 +0100418 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500419
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500420 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900421 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500422 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000423 }
424
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500425 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900426 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500427 goto out;
428 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500429
Otavio Salvadorf022d362015-02-17 10:42:43 -0200430 /* Switch voltage to 1.8V if CMD11 succeeded */
431 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
432 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
433
434 printf("Run CMD11 1.8V switch\n");
435 /* Sleep for 5 ms - max time for card to switch to 1.8V */
436 udelay(5000);
437 }
438
Dirk Behme7a5b8022012-03-26 03:13:05 +0000439 /* Workaround for ESDHC errata ENGcm03648 */
440 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800441 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000442
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800443 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000444 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
445 PRSSTAT_DAT0)) {
446 udelay(100);
447 timeout--;
448 }
449
450 if (timeout <= 0) {
451 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900452 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500453 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000454 }
455 }
456
Andy Fleming50586ef2008-10-30 16:47:16 -0500457 /* Copy the response to the response buffer */
458 if (cmd->resp_type & MMC_RSP_136) {
459 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
460
Stefano Babicc67bee12010-02-05 15:11:27 +0100461 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
462 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
463 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
464 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530465 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
466 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
467 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
468 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500469 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100470 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500471
472 /* Wait until all of the blocks are transferred */
473 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530474#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600475 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530476#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500477 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100478 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500479
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500480 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900481 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500482 goto out;
483 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000484
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500485 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900486 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500487 goto out;
488 }
Andrew Gabbasov9b74dc52013-04-07 23:06:08 +0000489 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800490
Peng Fan4683b222015-06-25 10:32:26 +0800491 /*
492 * Need invalidate the dcache here again to avoid any
493 * cache-fill during the DMA operations such as the
494 * speculative pre-fetching etc.
495 */
Eric Nelson54899fc2013-04-03 12:31:56 +0000496 if (data->flags & MMC_DATA_READ)
497 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800498#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500499 }
500
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500501out:
502 /* Reset CMD and DATA portions on error */
503 if (err) {
504 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
505 SYSCTL_RSTC);
506 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
507 ;
508
509 if (data) {
510 esdhc_write32(&regs->sysctl,
511 esdhc_read32(&regs->sysctl) |
512 SYSCTL_RSTD);
513 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
514 ;
515 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200516
517 /* If this was CMD11, then notify that power cycle is needed */
518 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
519 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500520 }
521
Stefano Babicc67bee12010-02-05 15:11:27 +0100522 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500523
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500524 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500525}
526
Simon Glass09b465f2017-07-29 11:35:17 -0600527static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500528{
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200529 int div = 1;
530#ifdef ARCH_MXC
531 int pre_div = 1;
532#else
533 int pre_div = 2;
534#endif
535 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fan96f04072016-03-25 14:16:56 +0800536 struct fsl_esdhc *regs = priv->esdhc_regs;
537 int sdhc_clk = priv->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500538 uint clk;
539
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200540 if (clock < mmc->cfg->f_min)
541 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100542
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200543 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
544 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500545
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200546 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
547 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500548
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200549 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500550 div -= 1;
551
552 clk = (pre_div << 8) | (div << 4);
553
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700554#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800555 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700556#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500557 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700558#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100559
560 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500561
562 udelay(10000);
563
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700564#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800565 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700566#else
567 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
568#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100569
Andy Fleming50586ef2008-10-30 16:47:16 -0500570}
571
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800572#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600573static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800574{
Peng Fan96f04072016-03-25 14:16:56 +0800575 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800576 u32 value;
577 u32 time_out;
578
579 value = esdhc_read32(&regs->sysctl);
580
581 if (enable)
582 value |= SYSCTL_CKEN;
583 else
584 value &= ~SYSCTL_CKEN;
585
586 esdhc_write32(&regs->sysctl, value);
587
588 time_out = 20;
589 value = PRSSTAT_SDSTB;
590 while (!(esdhc_read32(&regs->prsstat) & value)) {
591 if (time_out == 0) {
592 printf("fsl_esdhc: Internal clock never stabilised.\n");
593 break;
594 }
595 time_out--;
596 mdelay(1);
597 }
598}
599#endif
600
Simon Glass9586aa62017-07-29 11:35:18 -0600601static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500602{
Peng Fan96f04072016-03-25 14:16:56 +0800603 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500604
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800605#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
606 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600607 esdhc_clock_control(priv, false);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800608 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600609 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800610#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500611 /* Set the clock speed */
Simon Glass09b465f2017-07-29 11:35:17 -0600612 set_sysctl(priv, mmc, mmc->clock);
Andy Fleming50586ef2008-10-30 16:47:16 -0500613
614 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100615 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500616
617 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100618 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500619 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100620 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
621
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900622 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500623}
624
Simon Glass9586aa62017-07-29 11:35:18 -0600625static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500626{
Peng Fan96f04072016-03-25 14:16:56 +0800627 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -0600628 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500629
Stefano Babicc67bee12010-02-05 15:11:27 +0100630 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200631 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100632
633 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -0600634 start = get_timer(0);
635 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
636 if (get_timer(start) > 1000)
637 return -ETIMEDOUT;
638 }
Stefano Babicc67bee12010-02-05 15:11:27 +0100639
Peng Fanf53225c2016-06-15 10:53:00 +0800640#if defined(CONFIG_FSL_USDHC)
641 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
642 esdhc_write32(&regs->mmcboot, 0x0);
643 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
644 esdhc_write32(&regs->mixctrl, 0x0);
645 esdhc_write32(&regs->clktunectrlstatus, 0x0);
646
647 /* Put VEND_SPEC to default value */
648 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
649
650 /* Disable DLL_CTRL delay line */
651 esdhc_write32(&regs->dllctrl, 0x0);
652#endif
653
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000654#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530655 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000656 esdhc_write32(&regs->scr, 0x00000040);
657#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530658
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700659#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +0200660 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li84ecdf62016-06-15 10:53:01 +0800661#else
662 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700663#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500664
665 /* Set the initial clock speed */
Jerry Huang4a6ee172010-11-25 17:06:07 +0000666 mmc_set_clock(mmc, 400000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500667
668 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100669 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500670
671 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100672 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500673
Stefano Babicc67bee12010-02-05 15:11:27 +0100674 /* Set timout to the maximum value */
675 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500676
Peng Fan32a91792017-06-12 17:50:53 +0800677 if (priv->vs18_enable)
678 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
679
Thierry Redingd48d2e22012-01-02 01:15:38 +0000680 return 0;
681}
Andy Fleming50586ef2008-10-30 16:47:16 -0500682
Simon Glass9586aa62017-07-29 11:35:18 -0600683static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +0000684{
Peng Fan96f04072016-03-25 14:16:56 +0800685 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000686 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100687
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800688#ifdef CONFIG_ESDHC_DETECT_QUIRK
689 if (CONFIG_ESDHC_DETECT_QUIRK)
690 return 1;
691#endif
Peng Fan96f04072016-03-25 14:16:56 +0800692
693#ifdef CONFIG_DM_MMC
694 if (priv->non_removable)
695 return 1;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800696#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +0800697 if (dm_gpio_is_valid(&priv->cd_gpio))
698 return dm_gpio_get_value(&priv->cd_gpio);
699#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +0800700#endif
Peng Fan96f04072016-03-25 14:16:56 +0800701
Thierry Redingd48d2e22012-01-02 01:15:38 +0000702 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
703 udelay(1000);
704
705 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500706}
707
Simon Glass446e0772017-07-29 11:35:19 -0600708static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500709{
Simon Glass446e0772017-07-29 11:35:19 -0600710 ulong start;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500711
712 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200713 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500714
715 /* hardware clears the bit when it is done */
Simon Glass446e0772017-07-29 11:35:19 -0600716 start = get_timer(0);
717 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
718 if (get_timer(start) > 100) {
719 printf("MMC/SD: Reset never completed.\n");
720 return -ETIMEDOUT;
721 }
722 }
723
724 return 0;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500725}
726
Simon Glass9586aa62017-07-29 11:35:18 -0600727static int esdhc_getcd(struct mmc *mmc)
728{
729 struct fsl_esdhc_priv *priv = mmc->priv;
730
731 return esdhc_getcd_common(priv);
732}
733
734static int esdhc_init(struct mmc *mmc)
735{
736 struct fsl_esdhc_priv *priv = mmc->priv;
737
738 return esdhc_init_common(priv, mmc);
739}
740
741static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
742 struct mmc_data *data)
743{
744 struct fsl_esdhc_priv *priv = mmc->priv;
745
746 return esdhc_send_cmd_common(priv, mmc, cmd, data);
747}
748
749static int esdhc_set_ios(struct mmc *mmc)
750{
751 struct fsl_esdhc_priv *priv = mmc->priv;
752
753 return esdhc_set_ios_common(priv, mmc);
754}
755
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200756static const struct mmc_ops esdhc_ops = {
Simon Glass9586aa62017-07-29 11:35:18 -0600757 .getcd = esdhc_getcd,
758 .init = esdhc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200759 .send_cmd = esdhc_send_cmd,
760 .set_ios = esdhc_set_ios,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200761};
762
Simon Glasse88e1d92017-07-29 11:35:21 -0600763static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
764 struct fsl_esdhc_plat *plat)
Andy Fleming50586ef2008-10-30 16:47:16 -0500765{
Simon Glasse88e1d92017-07-29 11:35:21 -0600766 struct mmc_config *cfg;
Stefano Babicc67bee12010-02-05 15:11:27 +0100767 struct fsl_esdhc *regs;
Li Yang030955c2010-11-25 17:06:09 +0000768 u32 caps, voltage_caps;
Simon Glass446e0772017-07-29 11:35:19 -0600769 int ret;
Andy Fleming50586ef2008-10-30 16:47:16 -0500770
Peng Fan96f04072016-03-25 14:16:56 +0800771 if (!priv)
772 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100773
Peng Fan96f04072016-03-25 14:16:56 +0800774 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +0100775
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500776 /* First reset the eSDHC controller */
Simon Glass446e0772017-07-29 11:35:19 -0600777 ret = esdhc_reset(regs);
778 if (ret)
779 return ret;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500780
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700781#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +0000782 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
783 | SYSCTL_IPGEN | SYSCTL_CKEN);
Ye Li84ecdf62016-06-15 10:53:01 +0800784#else
785 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
786 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700787#endif
Jerry Huang975324a2012-05-17 23:57:02 +0000788
Peng Fan32a91792017-06-12 17:50:53 +0800789 if (priv->vs18_enable)
790 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
791
Ye.Lia3d6e382014-11-04 15:35:49 +0800792 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glasse88e1d92017-07-29 11:35:21 -0600793 cfg = &plat->cfg;
794 memset(cfg, '\0', sizeof(*cfg));
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200795
Li Yang030955c2010-11-25 17:06:09 +0000796 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800797 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600798
799#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
800 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
801 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
802#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800803
804/* T4240 host controller capabilities register should have VS33 bit */
805#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
806 caps = caps | ESDHC_HOSTCAPBLT_VS33;
807#endif
808
Andy Fleming50586ef2008-10-30 16:47:16 -0500809 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000810 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500811 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000812 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500813 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000814 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
815
Simon Glasse88e1d92017-07-29 11:35:21 -0600816 cfg->name = "FSL_SDHC";
817 cfg->ops = &esdhc_ops;
Li Yang030955c2010-11-25 17:06:09 +0000818#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glasse88e1d92017-07-29 11:35:21 -0600819 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000820#else
Simon Glasse88e1d92017-07-29 11:35:21 -0600821 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000822#endif
Simon Glasse88e1d92017-07-29 11:35:21 -0600823 if ((cfg->voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000824 printf("voltage not supported by controller\n");
825 return -1;
826 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500827
Peng Fan96f04072016-03-25 14:16:56 +0800828 if (priv->bus_width == 8)
Simon Glasse88e1d92017-07-29 11:35:21 -0600829 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800830 else if (priv->bus_width == 4)
Simon Glasse88e1d92017-07-29 11:35:21 -0600831 cfg->host_caps = MMC_MODE_4BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800832
Simon Glasse88e1d92017-07-29 11:35:21 -0600833 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500834#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glasse88e1d92017-07-29 11:35:21 -0600835 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500836#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500837
Peng Fan96f04072016-03-25 14:16:56 +0800838 if (priv->bus_width > 0) {
839 if (priv->bus_width < 8)
Simon Glasse88e1d92017-07-29 11:35:21 -0600840 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800841 if (priv->bus_width < 4)
Simon Glasse88e1d92017-07-29 11:35:21 -0600842 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000843 }
844
Andy Fleming50586ef2008-10-30 16:47:16 -0500845 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -0600846 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500847
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800848#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
849 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glasse88e1d92017-07-29 11:35:21 -0600850 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800851#endif
852
Simon Glasse88e1d92017-07-29 11:35:21 -0600853 cfg->f_min = 400000;
854 cfg->f_max = min(priv->sdhc_clk, (u32)52000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500855
Simon Glasse88e1d92017-07-29 11:35:21 -0600856 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200857
Peng Fan96f04072016-03-25 14:16:56 +0800858 return 0;
859}
860
Jagan Teki2e87c442017-05-12 17:18:20 +0530861#ifndef CONFIG_DM_MMC
862static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
863 struct fsl_esdhc_priv *priv)
864{
865 if (!cfg || !priv)
866 return -EINVAL;
867
868 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
869 priv->bus_width = cfg->max_bus_width;
870 priv->sdhc_clk = cfg->sdhc_clk;
871 priv->wp_enable = cfg->wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +0800872 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki2e87c442017-05-12 17:18:20 +0530873
874 return 0;
875};
876
Peng Fan96f04072016-03-25 14:16:56 +0800877int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
878{
Simon Glasse88e1d92017-07-29 11:35:21 -0600879 struct fsl_esdhc_plat *plat;
Peng Fan96f04072016-03-25 14:16:56 +0800880 struct fsl_esdhc_priv *priv;
Simon Glassd6eb25e2017-07-29 11:35:22 -0600881 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +0800882 int ret;
883
884 if (!cfg)
885 return -EINVAL;
886
887 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
888 if (!priv)
889 return -ENOMEM;
Simon Glasse88e1d92017-07-29 11:35:21 -0600890 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
891 if (!plat) {
892 free(priv);
893 return -ENOMEM;
894 }
Peng Fan96f04072016-03-25 14:16:56 +0800895
896 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
897 if (ret) {
898 debug("%s xlate failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -0600899 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +0800900 free(priv);
901 return ret;
902 }
903
Simon Glasse88e1d92017-07-29 11:35:21 -0600904 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +0800905 if (ret) {
906 debug("%s init failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -0600907 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +0800908 free(priv);
909 return ret;
910 }
911
Simon Glassd6eb25e2017-07-29 11:35:22 -0600912 mmc = mmc_create(&plat->cfg, priv);
913 if (!mmc)
914 return -EIO;
915
916 priv->mmc = mmc;
917
Andy Fleming50586ef2008-10-30 16:47:16 -0500918 return 0;
919}
920
921int fsl_esdhc_mmc_init(bd_t *bis)
922{
Stefano Babicc67bee12010-02-05 15:11:27 +0100923 struct fsl_esdhc_cfg *cfg;
924
Fabio Estevam88227a12012-12-27 08:51:08 +0000925 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100926 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000927 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100928 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500929}
Jagan Teki2e87c442017-05-12 17:18:20 +0530930#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400931
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800932#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
933void mmc_adapter_card_type_ident(void)
934{
935 u8 card_id;
936 u8 value;
937
938 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
939 gd->arch.sdhc_adapter = card_id;
940
941 switch (card_id) {
942 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800943 value = QIXIS_READ(brdcfg[5]);
944 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
945 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800946 break;
947 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800948 value = QIXIS_READ(pwr_ctl[1]);
949 value |= QIXIS_EVDD_BY_SDHC_VS;
950 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800951 break;
952 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
953 value = QIXIS_READ(brdcfg[5]);
954 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
955 QIXIS_WRITE(brdcfg[5], value);
956 break;
957 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
958 break;
959 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
960 break;
961 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
962 break;
963 case QIXIS_ESDHC_NO_ADAPTER:
964 break;
965 default:
966 break;
967 }
968}
969#endif
970
Stefano Babicc67bee12010-02-05 15:11:27 +0100971#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +0800972__weak int esdhc_status_fixup(void *blob, const char *compat)
973{
974#ifdef CONFIG_FSL_ESDHC_PIN_MUX
975 if (!hwconfig("esdhc")) {
976 do_fixup_by_compat(blob, compat, "status", "disabled",
977 sizeof("disabled"), 1);
978 return 1;
979 }
980#endif
Yangbo Lufce1e162017-01-17 10:43:54 +0800981 return 0;
982}
983
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400984void fdt_fixup_esdhc(void *blob, bd_t *bd)
985{
986 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400987
Yangbo Lufce1e162017-01-17 10:43:54 +0800988 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800989 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400990
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800991#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
992 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
993 gd->arch.sdhc_clk, 1);
994#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400995 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000996 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800997#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800998#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
999 do_fixup_by_compat_u32(blob, compat, "adapter-type",
1000 (u32)(gd->arch.sdhc_adapter), 1);
1001#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001002}
Stefano Babicc67bee12010-02-05 15:11:27 +01001003#endif
Peng Fan96f04072016-03-25 14:16:56 +08001004
1005#ifdef CONFIG_DM_MMC
1006#include <asm/arch/clock.h>
Peng Fanb60f1452017-02-22 16:21:55 +08001007__weak void init_clk_usdhc(u32 index)
1008{
1009}
1010
Peng Fan96f04072016-03-25 14:16:56 +08001011static int fsl_esdhc_probe(struct udevice *dev)
1012{
1013 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -06001014 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001015 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
York Sun9bb272e2017-08-08 15:45:13 -07001016#ifdef CONFIG_DM_REGULATOR
Peng Fan4483b7e2017-06-12 17:50:54 +08001017 struct udevice *vqmmc_dev;
York Sun9bb272e2017-08-08 15:45:13 -07001018#endif
Peng Fan96f04072016-03-25 14:16:56 +08001019 fdt_addr_t addr;
1020 unsigned int val;
1021 int ret;
1022
Simon Glass4aac33f2017-07-29 11:35:23 -06001023 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001024 if (addr == FDT_ADDR_T_NONE)
1025 return -EINVAL;
1026
1027 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1028 priv->dev = dev;
1029
Simon Glass4aac33f2017-07-29 11:35:23 -06001030 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fan96f04072016-03-25 14:16:56 +08001031 if (val == 8)
1032 priv->bus_width = 8;
1033 else if (val == 4)
1034 priv->bus_width = 4;
1035 else
1036 priv->bus_width = 1;
1037
Simon Glass4aac33f2017-07-29 11:35:23 -06001038 if (dev_read_bool(dev, "non-removable")) {
Peng Fan96f04072016-03-25 14:16:56 +08001039 priv->non_removable = 1;
1040 } else {
1041 priv->non_removable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001042#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001043 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1044 GPIOD_IS_IN);
Yangbo Lufc8048a2016-12-07 11:54:30 +08001045#endif
Peng Fan96f04072016-03-25 14:16:56 +08001046 }
1047
Peng Fan14831512016-06-15 10:53:02 +08001048 priv->wp_enable = 1;
1049
Yangbo Lufc8048a2016-12-07 11:54:30 +08001050#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001051 ret = gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1052 GPIOD_IS_IN);
Peng Fan14831512016-06-15 10:53:02 +08001053 if (ret)
1054 priv->wp_enable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001055#endif
Peng Fan4483b7e2017-06-12 17:50:54 +08001056
1057 priv->vs18_enable = 0;
1058
1059#ifdef CONFIG_DM_REGULATOR
1060 /*
1061 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1062 * otherwise, emmc will work abnormally.
1063 */
1064 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1065 if (ret) {
1066 dev_dbg(dev, "no vqmmc-supply\n");
1067 } else {
1068 ret = regulator_set_enable(vqmmc_dev, true);
1069 if (ret) {
1070 dev_err(dev, "fail to enable vqmmc-supply\n");
1071 return ret;
1072 }
1073
1074 if (regulator_get_value(vqmmc_dev) == 1800000)
1075 priv->vs18_enable = 1;
1076 }
1077#endif
1078
Peng Fan96f04072016-03-25 14:16:56 +08001079 /*
1080 * TODO:
1081 * Because lack of clk driver, if SDHC clk is not enabled,
1082 * need to enable it first before this driver is invoked.
1083 *
1084 * we use MXC_ESDHC_CLK to get clk freq.
1085 * If one would like to make this function work,
1086 * the aliases should be provided in dts as this:
1087 *
1088 * aliases {
1089 * mmc0 = &usdhc1;
1090 * mmc1 = &usdhc2;
1091 * mmc2 = &usdhc3;
1092 * mmc3 = &usdhc4;
1093 * };
1094 * Then if your board only supports mmc2 and mmc3, but we can
1095 * correctly get the seq as 2 and 3, then let mxc_get_clock
1096 * work as expected.
1097 */
Peng Fanb60f1452017-02-22 16:21:55 +08001098
1099 init_clk_usdhc(dev->seq);
1100
Peng Fan96f04072016-03-25 14:16:56 +08001101 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1102 if (priv->sdhc_clk <= 0) {
1103 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1104 return -EINVAL;
1105 }
1106
Simon Glasse88e1d92017-07-29 11:35:21 -06001107 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001108 if (ret) {
1109 dev_err(dev, "fsl_esdhc_init failure\n");
1110 return ret;
1111 }
1112
1113 upriv->mmc = priv->mmc;
Peng Fan35ae9942016-08-11 14:02:56 +08001114 priv->mmc->dev = dev;
Peng Fan96f04072016-03-25 14:16:56 +08001115
1116 return 0;
1117}
1118
1119static const struct udevice_id fsl_esdhc_ids[] = {
1120 { .compatible = "fsl,imx6ul-usdhc", },
1121 { .compatible = "fsl,imx6sx-usdhc", },
1122 { .compatible = "fsl,imx6sl-usdhc", },
1123 { .compatible = "fsl,imx6q-usdhc", },
1124 { .compatible = "fsl,imx7d-usdhc", },
Peng Fanb60f1452017-02-22 16:21:55 +08001125 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lua6473f82016-12-07 11:54:31 +08001126 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001127 { /* sentinel */ }
1128};
1129
1130U_BOOT_DRIVER(fsl_esdhc) = {
1131 .name = "fsl-esdhc-mmc",
1132 .id = UCLASS_MMC,
1133 .of_match = fsl_esdhc_ids,
1134 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -06001135 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +08001136 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1137};
1138#endif