blob: 6018f84307d18793f87f1f45e2da57861f3ff767 [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>
Peng Fan51313b42018-01-21 19:00:24 +080026#include <dm/pinctrl.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050027
Andy Fleming50586ef2008-10-30 16:47:16 -050028DECLARE_GLOBAL_DATA_PTR;
29
Ye.Lia3d6e382014-11-04 15:35:49 +080030#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
31 IRQSTATEN_CINT | \
32 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
33 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
34 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
35 IRQSTATEN_DINT)
Peng Fan51313b42018-01-21 19:00:24 +080036#define MAX_TUNING_LOOP 40
Ye.Lia3d6e382014-11-04 15:35:49 +080037
Andy Fleming50586ef2008-10-30 16:47:16 -050038struct fsl_esdhc {
Haijun.Zhang511948b2013-10-30 11:37:55 +080039 uint dsaddr; /* SDMA system address register */
40 uint blkattr; /* Block attributes register */
41 uint cmdarg; /* Command argument register */
42 uint xfertyp; /* Transfer type register */
43 uint cmdrsp0; /* Command response 0 register */
44 uint cmdrsp1; /* Command response 1 register */
45 uint cmdrsp2; /* Command response 2 register */
46 uint cmdrsp3; /* Command response 3 register */
47 uint datport; /* Buffer data port register */
48 uint prsstat; /* Present state register */
49 uint proctl; /* Protocol control register */
50 uint sysctl; /* System Control Register */
51 uint irqstat; /* Interrupt status register */
52 uint irqstaten; /* Interrupt status enable register */
53 uint irqsigen; /* Interrupt signal enable register */
54 uint autoc12err; /* Auto CMD error status register */
55 uint hostcapblt; /* Host controller capabilities register */
56 uint wml; /* Watermark level register */
57 uint mixctrl; /* For USDHC */
58 char reserved1[4]; /* reserved */
59 uint fevt; /* Force event register */
60 uint admaes; /* ADMA error status register */
61 uint adsaddr; /* ADMA system address register */
Peng Fanf53225c2016-06-15 10:53:00 +080062 char reserved2[4];
63 uint dllctrl;
64 uint dllstat;
65 uint clktunectrlstatus;
Peng Fan59d37822018-01-21 19:00:22 +080066 char reserved3[4];
67 uint strobe_dllctrl;
68 uint strobe_dllstat;
69 char reserved4[72];
Peng Fanf53225c2016-06-15 10:53:00 +080070 uint vendorspec;
71 uint mmcboot;
72 uint vendorspec2;
Peng Fan59d37822018-01-21 19:00:22 +080073 uint tuning_ctrl; /* on i.MX6/7/8 */
74 char reserved5[44];
Haijun.Zhang511948b2013-10-30 11:37:55 +080075 uint hostver; /* Host controller version register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020076 char reserved6[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080077 uint dmaerraddr; /* DMA error address register */
Peng Fanf53225c2016-06-15 10:53:00 +080078 char reserved7[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080079 uint dmaerrattr; /* DMA error attribute register */
80 char reserved8[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080081 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fan59d37822018-01-21 19:00:22 +080082 char reserved9[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080083 uint tcr; /* Tuning control register */
Peng Fan59d37822018-01-21 19:00:22 +080084 char reserved10[28]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080085 uint sddirctl; /* SD direction control register */
Peng Fan59d37822018-01-21 19:00:22 +080086 char reserved11[712];/* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080087 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050088};
89
Simon Glasse88e1d92017-07-29 11:35:21 -060090struct fsl_esdhc_plat {
91 struct mmc_config cfg;
92 struct mmc mmc;
93};
94
Peng Fan51313b42018-01-21 19:00:24 +080095struct esdhc_soc_data {
96 u32 flags;
97 u32 caps;
98};
99
Peng Fan96f04072016-03-25 14:16:56 +0800100/**
101 * struct fsl_esdhc_priv
102 *
103 * @esdhc_regs: registers of the sdhc controller
104 * @sdhc_clk: Current clk of the sdhc controller
105 * @bus_width: bus width, 1bit, 4bit or 8bit
106 * @cfg: mmc config
107 * @mmc: mmc
108 * Following is used when Driver Model is enabled for MMC
109 * @dev: pointer for the device
110 * @non_removable: 0: removable; 1: non-removable
Peng Fan14831512016-06-15 10:53:02 +0800111 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fan32a91792017-06-12 17:50:53 +0800112 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fan51313b42018-01-21 19:00:24 +0800113 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc.h
114 * @caps: controller capabilities
115 * @tuning_step: tuning step setting in tuning_ctrl register
116 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
117 * @strobe_dll_delay_target: settings in strobe_dllctrl
118 * @signal_voltage: indicating the current voltage
Peng Fan96f04072016-03-25 14:16:56 +0800119 * @cd_gpio: gpio for card detection
Peng Fan14831512016-06-15 10:53:02 +0800120 * @wp_gpio: gpio for write protection
Peng Fan96f04072016-03-25 14:16:56 +0800121 */
122struct fsl_esdhc_priv {
123 struct fsl_esdhc *esdhc_regs;
124 unsigned int sdhc_clk;
Peng Fan51313b42018-01-21 19:00:24 +0800125 unsigned int clock;
126 unsigned int mode;
Peng Fan96f04072016-03-25 14:16:56 +0800127 unsigned int bus_width;
Simon Glass653282b2017-07-29 11:35:24 -0600128#if !CONFIG_IS_ENABLED(BLK)
Peng Fan96f04072016-03-25 14:16:56 +0800129 struct mmc *mmc;
Simon Glass653282b2017-07-29 11:35:24 -0600130#endif
Peng Fan96f04072016-03-25 14:16:56 +0800131 struct udevice *dev;
132 int non_removable;
Peng Fan14831512016-06-15 10:53:02 +0800133 int wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +0800134 int vs18_enable;
Peng Fan51313b42018-01-21 19:00:24 +0800135 u32 flags;
136 u32 caps;
137 u32 tuning_step;
138 u32 tuning_start_tap;
139 u32 strobe_dll_delay_target;
140 u32 signal_voltage;
141#if IS_ENABLED(CONFIG_DM_REGULATOR)
142 struct udevice *vqmmc_dev;
143 struct udevice *vmmc_dev;
144#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +0800145#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +0800146 struct gpio_desc cd_gpio;
Peng Fan14831512016-06-15 10:53:02 +0800147 struct gpio_desc wp_gpio;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800148#endif
Peng Fan96f04072016-03-25 14:16:56 +0800149};
150
Andy Fleming50586ef2008-10-30 16:47:16 -0500151/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +0000152static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500153{
154 uint xfertyp = 0;
155
156 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530157 xfertyp |= XFERTYP_DPSEL;
158#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
159 xfertyp |= XFERTYP_DMAEN;
160#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500161 if (data->blocks > 1) {
162 xfertyp |= XFERTYP_MSBSEL;
163 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600164#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
165 xfertyp |= XFERTYP_AC12EN;
166#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500167 }
168
169 if (data->flags & MMC_DATA_READ)
170 xfertyp |= XFERTYP_DTDSEL;
171 }
172
173 if (cmd->resp_type & MMC_RSP_CRC)
174 xfertyp |= XFERTYP_CCCEN;
175 if (cmd->resp_type & MMC_RSP_OPCODE)
176 xfertyp |= XFERTYP_CICEN;
177 if (cmd->resp_type & MMC_RSP_136)
178 xfertyp |= XFERTYP_RSPTYP_136;
179 else if (cmd->resp_type & MMC_RSP_BUSY)
180 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
181 else if (cmd->resp_type & MMC_RSP_PRESENT)
182 xfertyp |= XFERTYP_RSPTYP_48;
183
Jason Liu4571de32011-03-22 01:32:31 +0000184 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
185 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800186
Andy Fleming50586ef2008-10-30 16:47:16 -0500187 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
188}
189
Dipen Dudhat77c14582009-10-05 15:41:58 +0530190#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
191/*
192 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
193 */
Simon Glass09b465f2017-07-29 11:35:17 -0600194static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
195 struct mmc_data *data)
Dipen Dudhat77c14582009-10-05 15:41:58 +0530196{
Peng Fan96f04072016-03-25 14:16:56 +0800197 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530198 uint blocks;
199 char *buffer;
200 uint databuf;
201 uint size;
202 uint irqstat;
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100203 ulong start;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530204
205 if (data->flags & MMC_DATA_READ) {
206 blocks = data->blocks;
207 buffer = data->dest;
208 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100209 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530210 size = data->blocksize;
211 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100212 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
213 if (get_timer(start) > PIO_TIMEOUT) {
214 printf("\nData Read Failed in PIO Mode.");
215 return;
216 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530217 }
218 while (size && (!(irqstat & IRQSTAT_TC))) {
219 udelay(100); /* Wait before last byte transfer complete */
220 irqstat = esdhc_read32(&regs->irqstat);
221 databuf = in_le32(&regs->datport);
222 *((uint *)buffer) = databuf;
223 buffer += 4;
224 size -= 4;
225 }
226 blocks--;
227 }
228 } else {
229 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200230 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530231 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100232 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530233 size = data->blocksize;
234 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100235 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
236 if (get_timer(start) > PIO_TIMEOUT) {
237 printf("\nData Write Failed in PIO Mode.");
238 return;
239 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530240 }
241 while (size && (!(irqstat & IRQSTAT_TC))) {
242 udelay(100); /* Wait before last byte transfer complete */
243 databuf = *((uint *)buffer);
244 buffer += 4;
245 size -= 4;
246 irqstat = esdhc_read32(&regs->irqstat);
247 out_le32(&regs->datport, databuf);
248 }
249 blocks--;
250 }
251 }
252}
253#endif
254
Simon Glass09b465f2017-07-29 11:35:17 -0600255static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
256 struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500257{
Andy Fleming50586ef2008-10-30 16:47:16 -0500258 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800259 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Faneec2d432018-01-10 13:20:40 +0800260#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
261 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700262 dma_addr_t addr;
263#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200264 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500265
266 wml_value = data->blocksize/4;
267
268 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530269 if (wml_value > WML_RD_WML_MAX)
270 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500271
Roy Zangab467c52010-02-09 18:23:33 +0800272 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800273#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Faneec2d432018-01-10 13:20:40 +0800274#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
275 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700276 addr = virt_to_phys((void *)(data->dest));
277 if (upper_32_bits(addr))
278 printf("Error found for upper 32 bits\n");
279 else
280 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
281#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100282 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800283#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700284#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500285 } else {
Ye.Li71689772014-02-20 18:00:57 +0800286#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000287 flush_dcache_range((ulong)data->src,
288 (ulong)data->src+data->blocks
289 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800290#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530291 if (wml_value > WML_WR_WML_MAX)
292 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan14831512016-06-15 10:53:02 +0800293 if (priv->wp_enable) {
294 if ((esdhc_read32(&regs->prsstat) &
295 PRSSTAT_WPSPL) == 0) {
296 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900297 return -ETIMEDOUT;
Peng Fan14831512016-06-15 10:53:02 +0800298 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500299 }
Roy Zangab467c52010-02-09 18:23:33 +0800300
301 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
302 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800303#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Faneec2d432018-01-10 13:20:40 +0800304#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
305 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700306 addr = virt_to_phys((void *)(data->src));
307 if (upper_32_bits(addr))
308 printf("Error found for upper 32 bits\n");
309 else
310 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
311#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100312 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800313#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700314#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500315 }
316
Stefano Babicc67bee12010-02-05 15:11:27 +0100317 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500318
319 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530320 /*
321 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
322 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
323 * So, Number of SD Clock cycles for 0.25sec should be minimum
324 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500325 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530326 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500327 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530328 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500329 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530330 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500331 * => timeout + 13 = log2(mmc->clock/4) + 1
332 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800333 *
334 * However, the MMC spec "It is strongly recommended for hosts to
335 * implement more than 500ms timeout value even if the card
336 * indicates the 250ms maximum busy length." Even the previous
337 * value of 300ms is known to be insufficient for some cards.
338 * So, we use
339 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530340 */
Yangbo Lue978a312015-12-30 14:19:30 +0800341 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500342 timeout -= 13;
343
344 if (timeout > 14)
345 timeout = 14;
346
347 if (timeout < 0)
348 timeout = 0;
349
Kumar Gala5103a032011-01-29 15:36:10 -0600350#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
351 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
352 timeout++;
353#endif
354
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800355#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
356 timeout = 0xE;
357#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100358 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500359
360 return 0;
361}
362
Eric Nelsone576bd92012-04-25 14:28:48 +0000363static void check_and_invalidate_dcache_range
364 (struct mmc_cmd *cmd,
365 struct mmc_data *data) {
Yangbo Lu8b064602015-03-20 19:28:31 -0700366 unsigned start = 0;
Yangbo Lucc634e22016-05-12 19:12:58 +0800367 unsigned end = 0;
Eric Nelsone576bd92012-04-25 14:28:48 +0000368 unsigned size = roundup(ARCH_DMA_MINALIGN,
369 data->blocks*data->blocksize);
Peng Faneec2d432018-01-10 13:20:40 +0800370#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
371 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700372 dma_addr_t addr;
373
374 addr = virt_to_phys((void *)(data->dest));
375 if (upper_32_bits(addr))
376 printf("Error found for upper 32 bits\n");
377 else
378 start = lower_32_bits(addr);
Yangbo Lucc634e22016-05-12 19:12:58 +0800379#else
380 start = (unsigned)data->dest;
Yangbo Lu8b064602015-03-20 19:28:31 -0700381#endif
Yangbo Lucc634e22016-05-12 19:12:58 +0800382 end = start + size;
Eric Nelsone576bd92012-04-25 14:28:48 +0000383 invalidate_dcache_range(start, end);
384}
Tom Rini10dc7772014-05-23 09:19:05 -0400385
Andy Fleming50586ef2008-10-30 16:47:16 -0500386/*
387 * Sends a command out on the bus. Takes the mmc pointer,
388 * a command pointer, and an optional data pointer.
389 */
Simon Glass9586aa62017-07-29 11:35:18 -0600390static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
391 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500392{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500393 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500394 uint xfertyp;
395 uint irqstat;
Peng Fan51313b42018-01-21 19:00:24 +0800396 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fan96f04072016-03-25 14:16:56 +0800397 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500398
Jerry Huangd621da02011-01-06 23:42:19 -0600399#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
400 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
401 return 0;
402#endif
403
Stefano Babicc67bee12010-02-05 15:11:27 +0100404 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500405
406 sync();
407
408 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100409 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
410 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
411 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500412
Stefano Babicc67bee12010-02-05 15:11:27 +0100413 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
414 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500415
416 /* Wait at least 8 SD clock cycles before the next command */
417 /*
418 * Note: This is way more than 8 cycles, but 1ms seems to
419 * resolve timing issues with some cards
420 */
421 udelay(1000);
422
423 /* Set up for a data transfer if we have one */
424 if (data) {
Simon Glass09b465f2017-07-29 11:35:17 -0600425 err = esdhc_setup_data(priv, mmc, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500426 if(err)
427 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800428
429 if (data->flags & MMC_DATA_READ)
430 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500431 }
432
433 /* Figure out the transfer arguments */
434 xfertyp = esdhc_xfertyp(cmd, data);
435
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500436 /* Mask all irqs */
437 esdhc_write32(&regs->irqsigen, 0);
438
Andy Fleming50586ef2008-10-30 16:47:16 -0500439 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100440 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000441#if defined(CONFIG_FSL_USDHC)
442 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500443 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
444 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000445 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
446#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100447 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000448#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000449
Peng Fan51313b42018-01-21 19:00:24 +0800450 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
451 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
452 flags = IRQSTAT_BRR;
453
Andy Fleming50586ef2008-10-30 16:47:16 -0500454 /* Wait for the command to complete */
Peng Fan51313b42018-01-21 19:00:24 +0800455 while (!(esdhc_read32(&regs->irqstat) & flags))
Stefano Babicc67bee12010-02-05 15:11:27 +0100456 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500457
Stefano Babicc67bee12010-02-05 15:11:27 +0100458 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500459
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500460 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900461 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500462 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000463 }
464
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500465 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900466 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500467 goto out;
468 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500469
Otavio Salvadorf022d362015-02-17 10:42:43 -0200470 /* Switch voltage to 1.8V if CMD11 succeeded */
471 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
472 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
473
474 printf("Run CMD11 1.8V switch\n");
475 /* Sleep for 5 ms - max time for card to switch to 1.8V */
476 udelay(5000);
477 }
478
Dirk Behme7a5b8022012-03-26 03:13:05 +0000479 /* Workaround for ESDHC errata ENGcm03648 */
480 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800481 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000482
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800483 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000484 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
485 PRSSTAT_DAT0)) {
486 udelay(100);
487 timeout--;
488 }
489
490 if (timeout <= 0) {
491 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900492 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500493 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000494 }
495 }
496
Andy Fleming50586ef2008-10-30 16:47:16 -0500497 /* Copy the response to the response buffer */
498 if (cmd->resp_type & MMC_RSP_136) {
499 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
500
Stefano Babicc67bee12010-02-05 15:11:27 +0100501 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
502 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
503 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
504 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530505 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
506 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
507 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
508 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500509 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100510 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500511
512 /* Wait until all of the blocks are transferred */
513 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530514#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600515 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530516#else
Peng Fan51313b42018-01-21 19:00:24 +0800517 flags = DATA_COMPLETE;
518 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
519 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
520 flags = IRQSTAT_BRR;
521 }
522
Andy Fleming50586ef2008-10-30 16:47:16 -0500523 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100524 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500525
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500526 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900527 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500528 goto out;
529 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000530
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500531 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900532 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500533 goto out;
534 }
Peng Fan51313b42018-01-21 19:00:24 +0800535 } while ((irqstat & flags) != flags);
Ye.Li71689772014-02-20 18:00:57 +0800536
Peng Fan4683b222015-06-25 10:32:26 +0800537 /*
538 * Need invalidate the dcache here again to avoid any
539 * cache-fill during the DMA operations such as the
540 * speculative pre-fetching etc.
541 */
Eric Nelson54899fc2013-04-03 12:31:56 +0000542 if (data->flags & MMC_DATA_READ)
543 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800544#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500545 }
546
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500547out:
548 /* Reset CMD and DATA portions on error */
549 if (err) {
550 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
551 SYSCTL_RSTC);
552 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
553 ;
554
555 if (data) {
556 esdhc_write32(&regs->sysctl,
557 esdhc_read32(&regs->sysctl) |
558 SYSCTL_RSTD);
559 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
560 ;
561 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200562
563 /* If this was CMD11, then notify that power cycle is needed */
564 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
565 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500566 }
567
Stefano Babicc67bee12010-02-05 15:11:27 +0100568 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500569
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500570 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500571}
572
Simon Glass09b465f2017-07-29 11:35:17 -0600573static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500574{
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100575 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200576 int div = 1;
577#ifdef ARCH_MXC
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100578#ifdef CONFIG_MX53
579 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
580 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
581#else
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200582 int pre_div = 1;
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100583#endif
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200584#else
585 int pre_div = 2;
586#endif
587 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fan96f04072016-03-25 14:16:56 +0800588 int sdhc_clk = priv->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500589 uint clk;
590
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200591 if (clock < mmc->cfg->f_min)
592 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100593
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200594 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
595 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500596
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200597 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
598 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500599
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200600 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500601 div -= 1;
602
603 clk = (pre_div << 8) | (div << 4);
604
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700605#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800606 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700607#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500608 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700609#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100610
611 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500612
613 udelay(10000);
614
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700615#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800616 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700617#else
618 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
619#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100620
Peng Fan51313b42018-01-21 19:00:24 +0800621 priv->clock = clock;
Andy Fleming50586ef2008-10-30 16:47:16 -0500622}
623
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800624#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600625static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800626{
Peng Fan96f04072016-03-25 14:16:56 +0800627 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800628 u32 value;
629 u32 time_out;
630
631 value = esdhc_read32(&regs->sysctl);
632
633 if (enable)
634 value |= SYSCTL_CKEN;
635 else
636 value &= ~SYSCTL_CKEN;
637
638 esdhc_write32(&regs->sysctl, value);
639
640 time_out = 20;
641 value = PRSSTAT_SDSTB;
642 while (!(esdhc_read32(&regs->prsstat) & value)) {
643 if (time_out == 0) {
644 printf("fsl_esdhc: Internal clock never stabilised.\n");
645 break;
646 }
647 time_out--;
648 mdelay(1);
649 }
650}
651#endif
652
Peng Fan51313b42018-01-21 19:00:24 +0800653#ifdef MMC_SUPPORTS_TUNING
654static int esdhc_change_pinstate(struct udevice *dev)
655{
656 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
657 int ret;
658
659 switch (priv->mode) {
660 case UHS_SDR50:
661 case UHS_DDR50:
662 ret = pinctrl_select_state(dev, "state_100mhz");
663 break;
664 case UHS_SDR104:
665 case MMC_HS_200:
666 ret = pinctrl_select_state(dev, "state_200mhz");
667 break;
668 default:
669 ret = pinctrl_select_state(dev, "default");
670 break;
671 }
672
673 if (ret)
674 printf("%s %d error\n", __func__, priv->mode);
675
676 return ret;
677}
678
679static void esdhc_reset_tuning(struct mmc *mmc)
680{
681 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
682 struct fsl_esdhc *regs = priv->esdhc_regs;
683
684 if (priv->flags & ESDHC_FLAG_USDHC) {
685 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
686 esdhc_clrbits32(&regs->autoc12err,
687 MIX_CTRL_SMPCLK_SEL |
688 MIX_CTRL_EXE_TUNE);
689 }
690 }
691}
692
693static int esdhc_set_timing(struct mmc *mmc)
694{
695 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
696 struct fsl_esdhc *regs = priv->esdhc_regs;
697 u32 mixctrl;
698
699 mixctrl = readl(&regs->mixctrl);
700 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
701
702 switch (mmc->selected_mode) {
703 case MMC_LEGACY:
704 case SD_LEGACY:
705 esdhc_reset_tuning(mmc);
706 break;
707 case MMC_HS:
708 case MMC_HS_52:
709 case MMC_HS_200:
710 case SD_HS:
711 case UHS_SDR12:
712 case UHS_SDR25:
713 case UHS_SDR50:
714 case UHS_SDR104:
715 writel(mixctrl, &regs->mixctrl);
716 break;
717 case UHS_DDR50:
718 case MMC_DDR_52:
719 mixctrl |= MIX_CTRL_DDREN;
720 writel(mixctrl, &regs->mixctrl);
721 break;
722 default:
723 printf("Not supported %d\n", mmc->selected_mode);
724 return -EINVAL;
725 }
726
727 priv->mode = mmc->selected_mode;
728
729 return esdhc_change_pinstate(mmc->dev);
730}
731
732static int esdhc_set_voltage(struct mmc *mmc)
733{
734 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
735 struct fsl_esdhc *regs = priv->esdhc_regs;
736 int ret;
737
738 priv->signal_voltage = mmc->signal_voltage;
739 switch (mmc->signal_voltage) {
740 case MMC_SIGNAL_VOLTAGE_330:
741 if (priv->vs18_enable)
742 return -EIO;
743#ifdef CONFIG_DM_REGULATOR
744 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
745 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
746 if (ret) {
747 printf("Setting to 3.3V error");
748 return -EIO;
749 }
750 /* Wait for 5ms */
751 mdelay(5);
752 }
753#endif
754
755 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
756 if (!(esdhc_read32(&regs->vendorspec) &
757 ESDHC_VENDORSPEC_VSELECT))
758 return 0;
759
760 return -EAGAIN;
761 case MMC_SIGNAL_VOLTAGE_180:
762#ifdef CONFIG_DM_REGULATOR
763 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
764 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
765 if (ret) {
766 printf("Setting to 1.8V error");
767 return -EIO;
768 }
769 }
770#endif
771 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
772 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
773 return 0;
774
775 return -EAGAIN;
776 case MMC_SIGNAL_VOLTAGE_120:
777 return -ENOTSUPP;
778 default:
779 return 0;
780 }
781}
782
783static void esdhc_stop_tuning(struct mmc *mmc)
784{
785 struct mmc_cmd cmd;
786
787 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
788 cmd.cmdarg = 0;
789 cmd.resp_type = MMC_RSP_R1b;
790
791 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
792}
793
794static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
795{
796 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
797 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
798 struct fsl_esdhc *regs = priv->esdhc_regs;
799 struct mmc *mmc = &plat->mmc;
800 u32 irqstaten = readl(&regs->irqstaten);
801 u32 irqsigen = readl(&regs->irqsigen);
802 int i, ret = -ETIMEDOUT;
803 u32 val, mixctrl;
804
805 /* clock tuning is not needed for upto 52MHz */
806 if (mmc->clock <= 52000000)
807 return 0;
808
809 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
810 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
811 val = readl(&regs->autoc12err);
812 mixctrl = readl(&regs->mixctrl);
813 val &= ~MIX_CTRL_SMPCLK_SEL;
814 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
815
816 val |= MIX_CTRL_EXE_TUNE;
817 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
818
819 writel(val, &regs->autoc12err);
820 writel(mixctrl, &regs->mixctrl);
821 }
822
823 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
824 mixctrl = readl(&regs->mixctrl);
825 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
826 writel(mixctrl, &regs->mixctrl);
827
828 writel(IRQSTATEN_BRR, &regs->irqstaten);
829 writel(IRQSTATEN_BRR, &regs->irqsigen);
830
831 /*
832 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
833 * of loops reaches 40 times.
834 */
835 for (i = 0; i < MAX_TUNING_LOOP; i++) {
836 u32 ctrl;
837
838 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
839 if (mmc->bus_width == 8)
840 writel(0x7080, &regs->blkattr);
841 else if (mmc->bus_width == 4)
842 writel(0x7040, &regs->blkattr);
843 } else {
844 writel(0x7040, &regs->blkattr);
845 }
846
847 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
848 val = readl(&regs->mixctrl);
849 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
850 writel(val, &regs->mixctrl);
851
852 /* We are using STD tuning, no need to check return value */
853 mmc_send_tuning(mmc, opcode, NULL);
854
855 ctrl = readl(&regs->autoc12err);
856 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
857 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
858 /*
859 * need to wait some time, make sure sd/mmc fininsh
860 * send out tuning data, otherwise, the sd/mmc can't
861 * response to any command when the card still out
862 * put the tuning data.
863 */
864 mdelay(1);
865 ret = 0;
866 break;
867 }
868
869 /* Add 1ms delay for SD and eMMC */
870 mdelay(1);
871 }
872
873 writel(irqstaten, &regs->irqstaten);
874 writel(irqsigen, &regs->irqsigen);
875
876 esdhc_stop_tuning(mmc);
877
878 return ret;
879}
880#endif
881
Simon Glass9586aa62017-07-29 11:35:18 -0600882static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500883{
Peng Fan96f04072016-03-25 14:16:56 +0800884 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Fan51313b42018-01-21 19:00:24 +0800885 int ret __maybe_unused;
Andy Fleming50586ef2008-10-30 16:47:16 -0500886
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800887#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
888 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600889 esdhc_clock_control(priv, false);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800890 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600891 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800892#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500893 /* Set the clock speed */
Peng Fan51313b42018-01-21 19:00:24 +0800894 if (priv->clock != mmc->clock)
895 set_sysctl(priv, mmc, mmc->clock);
896
897#ifdef MMC_SUPPORTS_TUNING
898 if (mmc->clk_disable) {
899#ifdef CONFIG_FSL_USDHC
900 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
901#else
902 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
903#endif
904 } else {
905#ifdef CONFIG_FSL_USDHC
906 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
907 VENDORSPEC_CKEN);
908#else
909 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
910#endif
911 }
912
913 if (priv->mode != mmc->selected_mode) {
914 ret = esdhc_set_timing(mmc);
915 if (ret) {
916 printf("esdhc_set_timing error %d\n", ret);
917 return ret;
918 }
919 }
920
921 if (priv->signal_voltage != mmc->signal_voltage) {
922 ret = esdhc_set_voltage(mmc);
923 if (ret) {
924 printf("esdhc_set_voltage error %d\n", ret);
925 return ret;
926 }
927 }
928#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500929
930 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100931 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500932
933 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100934 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500935 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100936 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
937
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900938 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500939}
940
Simon Glass9586aa62017-07-29 11:35:18 -0600941static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500942{
Peng Fan96f04072016-03-25 14:16:56 +0800943 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -0600944 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500945
Stefano Babicc67bee12010-02-05 15:11:27 +0100946 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200947 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100948
949 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -0600950 start = get_timer(0);
951 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
952 if (get_timer(start) > 1000)
953 return -ETIMEDOUT;
954 }
Stefano Babicc67bee12010-02-05 15:11:27 +0100955
Peng Fanf53225c2016-06-15 10:53:00 +0800956#if defined(CONFIG_FSL_USDHC)
957 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
958 esdhc_write32(&regs->mmcboot, 0x0);
959 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
960 esdhc_write32(&regs->mixctrl, 0x0);
961 esdhc_write32(&regs->clktunectrlstatus, 0x0);
962
963 /* Put VEND_SPEC to default value */
Peng Fandb359ef2018-01-02 16:51:22 +0800964 if (priv->vs18_enable)
965 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
966 ESDHC_VENDORSPEC_VSELECT));
967 else
968 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
Peng Fanf53225c2016-06-15 10:53:00 +0800969
970 /* Disable DLL_CTRL delay line */
971 esdhc_write32(&regs->dllctrl, 0x0);
972#endif
973
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000974#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530975 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000976 esdhc_write32(&regs->scr, 0x00000040);
977#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530978
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700979#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +0200980 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li84ecdf62016-06-15 10:53:01 +0800981#else
982 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700983#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500984
985 /* Set the initial clock speed */
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +0200986 mmc_set_clock(mmc, 400000, false);
Andy Fleming50586ef2008-10-30 16:47:16 -0500987
988 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100989 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500990
991 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100992 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500993
Stefano Babicc67bee12010-02-05 15:11:27 +0100994 /* Set timout to the maximum value */
995 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500996
Thierry Redingd48d2e22012-01-02 01:15:38 +0000997 return 0;
998}
Andy Fleming50586ef2008-10-30 16:47:16 -0500999
Simon Glass9586aa62017-07-29 11:35:18 -06001000static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +00001001{
Peng Fan96f04072016-03-25 14:16:56 +08001002 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +00001003 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +01001004
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +08001005#ifdef CONFIG_ESDHC_DETECT_QUIRK
1006 if (CONFIG_ESDHC_DETECT_QUIRK)
1007 return 1;
1008#endif
Peng Fan96f04072016-03-25 14:16:56 +08001009
Simon Glass653282b2017-07-29 11:35:24 -06001010#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +08001011 if (priv->non_removable)
1012 return 1;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001013#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +08001014 if (dm_gpio_is_valid(&priv->cd_gpio))
1015 return dm_gpio_get_value(&priv->cd_gpio);
1016#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +08001017#endif
Peng Fan96f04072016-03-25 14:16:56 +08001018
Thierry Redingd48d2e22012-01-02 01:15:38 +00001019 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1020 udelay(1000);
1021
1022 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -05001023}
1024
Simon Glass446e0772017-07-29 11:35:19 -06001025static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001026{
Simon Glass446e0772017-07-29 11:35:19 -06001027 ulong start;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001028
1029 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +02001030 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001031
1032 /* hardware clears the bit when it is done */
Simon Glass446e0772017-07-29 11:35:19 -06001033 start = get_timer(0);
1034 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1035 if (get_timer(start) > 100) {
1036 printf("MMC/SD: Reset never completed.\n");
1037 return -ETIMEDOUT;
1038 }
1039 }
1040
1041 return 0;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001042}
1043
Simon Glasse7881d82017-07-29 11:35:31 -06001044#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass9586aa62017-07-29 11:35:18 -06001045static int esdhc_getcd(struct mmc *mmc)
1046{
1047 struct fsl_esdhc_priv *priv = mmc->priv;
1048
1049 return esdhc_getcd_common(priv);
1050}
1051
1052static int esdhc_init(struct mmc *mmc)
1053{
1054 struct fsl_esdhc_priv *priv = mmc->priv;
1055
1056 return esdhc_init_common(priv, mmc);
1057}
1058
1059static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1060 struct mmc_data *data)
1061{
1062 struct fsl_esdhc_priv *priv = mmc->priv;
1063
1064 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1065}
1066
1067static int esdhc_set_ios(struct mmc *mmc)
1068{
1069 struct fsl_esdhc_priv *priv = mmc->priv;
1070
1071 return esdhc_set_ios_common(priv, mmc);
1072}
1073
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001074static const struct mmc_ops esdhc_ops = {
Simon Glass9586aa62017-07-29 11:35:18 -06001075 .getcd = esdhc_getcd,
1076 .init = esdhc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001077 .send_cmd = esdhc_send_cmd,
1078 .set_ios = esdhc_set_ios,
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001079};
Simon Glass653282b2017-07-29 11:35:24 -06001080#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001081
Simon Glasse88e1d92017-07-29 11:35:21 -06001082static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1083 struct fsl_esdhc_plat *plat)
Andy Fleming50586ef2008-10-30 16:47:16 -05001084{
Simon Glasse88e1d92017-07-29 11:35:21 -06001085 struct mmc_config *cfg;
Stefano Babicc67bee12010-02-05 15:11:27 +01001086 struct fsl_esdhc *regs;
Li Yang030955c2010-11-25 17:06:09 +00001087 u32 caps, voltage_caps;
Simon Glass446e0772017-07-29 11:35:19 -06001088 int ret;
Andy Fleming50586ef2008-10-30 16:47:16 -05001089
Peng Fan96f04072016-03-25 14:16:56 +08001090 if (!priv)
1091 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +01001092
Peng Fan96f04072016-03-25 14:16:56 +08001093 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +01001094
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001095 /* First reset the eSDHC controller */
Simon Glass446e0772017-07-29 11:35:19 -06001096 ret = esdhc_reset(regs);
1097 if (ret)
1098 return ret;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001099
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001100#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +00001101 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1102 | SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fan51313b42018-01-21 19:00:24 +08001103 /* Clearing tuning bits in case ROM has set it already */
1104 esdhc_write32(&regs->mixctrl, 0);
1105 esdhc_write32(&regs->autoc12err, 0);
1106 esdhc_write32(&regs->clktunectrlstatus, 0);
Ye Li84ecdf62016-06-15 10:53:01 +08001107#else
1108 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1109 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001110#endif
Jerry Huang975324a2012-05-17 23:57:02 +00001111
Peng Fan32a91792017-06-12 17:50:53 +08001112 if (priv->vs18_enable)
1113 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1114
Ye.Lia3d6e382014-11-04 15:35:49 +08001115 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glasse88e1d92017-07-29 11:35:21 -06001116 cfg = &plat->cfg;
Simon Glass653282b2017-07-29 11:35:24 -06001117#ifndef CONFIG_DM_MMC
Simon Glasse88e1d92017-07-29 11:35:21 -06001118 memset(cfg, '\0', sizeof(*cfg));
Simon Glass653282b2017-07-29 11:35:24 -06001119#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001120
Li Yang030955c2010-11-25 17:06:09 +00001121 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +08001122 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -06001123
1124#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1125 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1126 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1127#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +08001128
1129/* T4240 host controller capabilities register should have VS33 bit */
1130#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1131 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1132#endif
1133
Andy Fleming50586ef2008-10-30 16:47:16 -05001134 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +00001135 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -05001136 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +00001137 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -05001138 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +00001139 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1140
Simon Glasse88e1d92017-07-29 11:35:21 -06001141 cfg->name = "FSL_SDHC";
Simon Glasse7881d82017-07-29 11:35:31 -06001142#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glasse88e1d92017-07-29 11:35:21 -06001143 cfg->ops = &esdhc_ops;
Simon Glass653282b2017-07-29 11:35:24 -06001144#endif
Li Yang030955c2010-11-25 17:06:09 +00001145#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glasse88e1d92017-07-29 11:35:21 -06001146 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +00001147#else
Simon Glasse88e1d92017-07-29 11:35:21 -06001148 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +00001149#endif
Simon Glasse88e1d92017-07-29 11:35:21 -06001150 if ((cfg->voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +00001151 printf("voltage not supported by controller\n");
1152 return -1;
1153 }
Andy Fleming50586ef2008-10-30 16:47:16 -05001154
Peng Fan96f04072016-03-25 14:16:56 +08001155 if (priv->bus_width == 8)
Simon Glasse88e1d92017-07-29 11:35:21 -06001156 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001157 else if (priv->bus_width == 4)
Simon Glasse88e1d92017-07-29 11:35:21 -06001158 cfg->host_caps = MMC_MODE_4BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001159
Simon Glasse88e1d92017-07-29 11:35:21 -06001160 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -05001161#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glasse88e1d92017-07-29 11:35:21 -06001162 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -05001163#endif
Andy Fleming50586ef2008-10-30 16:47:16 -05001164
Peng Fan96f04072016-03-25 14:16:56 +08001165 if (priv->bus_width > 0) {
1166 if (priv->bus_width < 8)
Simon Glasse88e1d92017-07-29 11:35:21 -06001167 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001168 if (priv->bus_width < 4)
Simon Glasse88e1d92017-07-29 11:35:21 -06001169 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +00001170 }
1171
Andy Fleming50586ef2008-10-30 16:47:16 -05001172 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -06001173 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -05001174
Haijun.Zhangd47e3d22014-01-10 13:52:18 +08001175#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1176 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glasse88e1d92017-07-29 11:35:21 -06001177 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +08001178#endif
1179
Peng Fan51313b42018-01-21 19:00:24 +08001180 cfg->host_caps |= priv->caps;
1181
Simon Glasse88e1d92017-07-29 11:35:21 -06001182 cfg->f_min = 400000;
Peng Fan51313b42018-01-21 19:00:24 +08001183 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleming50586ef2008-10-30 16:47:16 -05001184
Simon Glasse88e1d92017-07-29 11:35:21 -06001185 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001186
Peng Fan51313b42018-01-21 19:00:24 +08001187 writel(0, &regs->dllctrl);
1188 if (priv->flags & ESDHC_FLAG_USDHC) {
1189 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1190 u32 val = readl(&regs->tuning_ctrl);
1191
1192 val |= ESDHC_STD_TUNING_EN;
1193 val &= ~ESDHC_TUNING_START_TAP_MASK;
1194 val |= priv->tuning_start_tap;
1195 val &= ~ESDHC_TUNING_STEP_MASK;
1196 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1197 writel(val, &regs->tuning_ctrl);
1198 }
1199 }
1200
Peng Fan96f04072016-03-25 14:16:56 +08001201 return 0;
1202}
1203
Simon Glass52489302017-07-29 11:35:28 -06001204#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki2e87c442017-05-12 17:18:20 +05301205static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1206 struct fsl_esdhc_priv *priv)
1207{
1208 if (!cfg || !priv)
1209 return -EINVAL;
1210
1211 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1212 priv->bus_width = cfg->max_bus_width;
1213 priv->sdhc_clk = cfg->sdhc_clk;
1214 priv->wp_enable = cfg->wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +08001215 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki2e87c442017-05-12 17:18:20 +05301216
1217 return 0;
1218};
1219
Peng Fan96f04072016-03-25 14:16:56 +08001220int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
1221{
Simon Glasse88e1d92017-07-29 11:35:21 -06001222 struct fsl_esdhc_plat *plat;
Peng Fan96f04072016-03-25 14:16:56 +08001223 struct fsl_esdhc_priv *priv;
Simon Glassd6eb25e2017-07-29 11:35:22 -06001224 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001225 int ret;
1226
1227 if (!cfg)
1228 return -EINVAL;
1229
1230 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1231 if (!priv)
1232 return -ENOMEM;
Simon Glasse88e1d92017-07-29 11:35:21 -06001233 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1234 if (!plat) {
1235 free(priv);
1236 return -ENOMEM;
1237 }
Peng Fan96f04072016-03-25 14:16:56 +08001238
1239 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1240 if (ret) {
1241 debug("%s xlate failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -06001242 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +08001243 free(priv);
1244 return ret;
1245 }
1246
Simon Glasse88e1d92017-07-29 11:35:21 -06001247 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001248 if (ret) {
1249 debug("%s init failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -06001250 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +08001251 free(priv);
1252 return ret;
1253 }
1254
Simon Glassd6eb25e2017-07-29 11:35:22 -06001255 mmc = mmc_create(&plat->cfg, priv);
1256 if (!mmc)
1257 return -EIO;
1258
1259 priv->mmc = mmc;
1260
Andy Fleming50586ef2008-10-30 16:47:16 -05001261 return 0;
1262}
1263
1264int fsl_esdhc_mmc_init(bd_t *bis)
1265{
Stefano Babicc67bee12010-02-05 15:11:27 +01001266 struct fsl_esdhc_cfg *cfg;
1267
Fabio Estevam88227a12012-12-27 08:51:08 +00001268 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +01001269 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +00001270 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +01001271 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -05001272}
Jagan Teki2e87c442017-05-12 17:18:20 +05301273#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001274
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001275#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1276void mmc_adapter_card_type_ident(void)
1277{
1278 u8 card_id;
1279 u8 value;
1280
1281 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
1282 gd->arch.sdhc_adapter = card_id;
1283
1284 switch (card_id) {
1285 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +08001286 value = QIXIS_READ(brdcfg[5]);
1287 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
1288 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001289 break;
1290 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +08001291 value = QIXIS_READ(pwr_ctl[1]);
1292 value |= QIXIS_EVDD_BY_SDHC_VS;
1293 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001294 break;
1295 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
1296 value = QIXIS_READ(brdcfg[5]);
1297 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
1298 QIXIS_WRITE(brdcfg[5], value);
1299 break;
1300 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
1301 break;
1302 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
1303 break;
1304 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
1305 break;
1306 case QIXIS_ESDHC_NO_ADAPTER:
1307 break;
1308 default:
1309 break;
1310 }
1311}
1312#endif
1313
Stefano Babicc67bee12010-02-05 15:11:27 +01001314#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +08001315__weak int esdhc_status_fixup(void *blob, const char *compat)
1316{
1317#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1318 if (!hwconfig("esdhc")) {
1319 do_fixup_by_compat(blob, compat, "status", "disabled",
1320 sizeof("disabled"), 1);
1321 return 1;
1322 }
1323#endif
Yangbo Lufce1e162017-01-17 10:43:54 +08001324 return 0;
1325}
1326
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001327void fdt_fixup_esdhc(void *blob, bd_t *bd)
1328{
1329 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001330
Yangbo Lufce1e162017-01-17 10:43:54 +08001331 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +08001332 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001333
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +08001334#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
1335 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
1336 gd->arch.sdhc_clk, 1);
1337#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001338 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +00001339 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +08001340#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001341#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1342 do_fixup_by_compat_u32(blob, compat, "adapter-type",
1343 (u32)(gd->arch.sdhc_adapter), 1);
1344#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001345}
Stefano Babicc67bee12010-02-05 15:11:27 +01001346#endif
Peng Fan96f04072016-03-25 14:16:56 +08001347
Simon Glass653282b2017-07-29 11:35:24 -06001348#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +08001349#include <asm/arch/clock.h>
Peng Fanb60f1452017-02-22 16:21:55 +08001350__weak void init_clk_usdhc(u32 index)
1351{
1352}
1353
Peng Fan96f04072016-03-25 14:16:56 +08001354static int fsl_esdhc_probe(struct udevice *dev)
1355{
1356 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -06001357 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001358 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fan51313b42018-01-21 19:00:24 +08001359 const void *fdt = gd->fdt_blob;
1360 int node = dev_of_offset(dev);
1361 struct esdhc_soc_data *data =
1362 (struct esdhc_soc_data *)dev_get_driver_data(dev);
York Sun9bb272e2017-08-08 15:45:13 -07001363#ifdef CONFIG_DM_REGULATOR
Peng Fan4483b7e2017-06-12 17:50:54 +08001364 struct udevice *vqmmc_dev;
York Sun9bb272e2017-08-08 15:45:13 -07001365#endif
Peng Fan96f04072016-03-25 14:16:56 +08001366 fdt_addr_t addr;
1367 unsigned int val;
Simon Glass653282b2017-07-29 11:35:24 -06001368 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001369 int ret;
1370
Simon Glass4aac33f2017-07-29 11:35:23 -06001371 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001372 if (addr == FDT_ADDR_T_NONE)
1373 return -EINVAL;
1374
1375 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1376 priv->dev = dev;
Peng Fan51313b42018-01-21 19:00:24 +08001377 priv->mode = -1;
1378 if (data) {
1379 priv->flags = data->flags;
1380 priv->caps = data->caps;
1381 }
Peng Fan96f04072016-03-25 14:16:56 +08001382
Simon Glass4aac33f2017-07-29 11:35:23 -06001383 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fan96f04072016-03-25 14:16:56 +08001384 if (val == 8)
1385 priv->bus_width = 8;
1386 else if (val == 4)
1387 priv->bus_width = 4;
1388 else
1389 priv->bus_width = 1;
1390
Peng Fan51313b42018-01-21 19:00:24 +08001391 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1392 priv->tuning_step = val;
1393 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1394 ESDHC_TUNING_START_TAP_DEFAULT);
1395 priv->tuning_start_tap = val;
1396 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1397 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1398 priv->strobe_dll_delay_target = val;
1399
Simon Glass4aac33f2017-07-29 11:35:23 -06001400 if (dev_read_bool(dev, "non-removable")) {
Peng Fan96f04072016-03-25 14:16:56 +08001401 priv->non_removable = 1;
1402 } else {
1403 priv->non_removable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001404#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001405 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1406 GPIOD_IS_IN);
Yangbo Lufc8048a2016-12-07 11:54:30 +08001407#endif
Peng Fan96f04072016-03-25 14:16:56 +08001408 }
1409
Peng Fan14831512016-06-15 10:53:02 +08001410 priv->wp_enable = 1;
1411
Yangbo Lufc8048a2016-12-07 11:54:30 +08001412#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001413 ret = gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1414 GPIOD_IS_IN);
Peng Fan14831512016-06-15 10:53:02 +08001415 if (ret)
1416 priv->wp_enable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001417#endif
Peng Fan4483b7e2017-06-12 17:50:54 +08001418
1419 priv->vs18_enable = 0;
1420
1421#ifdef CONFIG_DM_REGULATOR
1422 /*
1423 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1424 * otherwise, emmc will work abnormally.
1425 */
1426 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1427 if (ret) {
1428 dev_dbg(dev, "no vqmmc-supply\n");
1429 } else {
1430 ret = regulator_set_enable(vqmmc_dev, true);
1431 if (ret) {
1432 dev_err(dev, "fail to enable vqmmc-supply\n");
1433 return ret;
1434 }
1435
1436 if (regulator_get_value(vqmmc_dev) == 1800000)
1437 priv->vs18_enable = 1;
1438 }
1439#endif
1440
Peng Fan51313b42018-01-21 19:00:24 +08001441 if (fdt_get_property(fdt, node, "no-1-8-v", NULL))
1442 priv->caps &= ~(UHS_CAPS | MMC_MODE_HS200);
1443
Peng Fan96f04072016-03-25 14:16:56 +08001444 /*
1445 * TODO:
1446 * Because lack of clk driver, if SDHC clk is not enabled,
1447 * need to enable it first before this driver is invoked.
1448 *
1449 * we use MXC_ESDHC_CLK to get clk freq.
1450 * If one would like to make this function work,
1451 * the aliases should be provided in dts as this:
1452 *
1453 * aliases {
1454 * mmc0 = &usdhc1;
1455 * mmc1 = &usdhc2;
1456 * mmc2 = &usdhc3;
1457 * mmc3 = &usdhc4;
1458 * };
1459 * Then if your board only supports mmc2 and mmc3, but we can
1460 * correctly get the seq as 2 and 3, then let mxc_get_clock
1461 * work as expected.
1462 */
Peng Fanb60f1452017-02-22 16:21:55 +08001463
1464 init_clk_usdhc(dev->seq);
1465
Peng Fan96f04072016-03-25 14:16:56 +08001466 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1467 if (priv->sdhc_clk <= 0) {
1468 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1469 return -EINVAL;
1470 }
1471
Simon Glasse88e1d92017-07-29 11:35:21 -06001472 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001473 if (ret) {
1474 dev_err(dev, "fsl_esdhc_init failure\n");
1475 return ret;
1476 }
1477
Simon Glass653282b2017-07-29 11:35:24 -06001478 mmc = &plat->mmc;
1479 mmc->cfg = &plat->cfg;
1480 mmc->dev = dev;
1481 upriv->mmc = mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001482
Simon Glass653282b2017-07-29 11:35:24 -06001483 return esdhc_init_common(priv, mmc);
Peng Fan96f04072016-03-25 14:16:56 +08001484}
1485
Simon Glasse7881d82017-07-29 11:35:31 -06001486#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass653282b2017-07-29 11:35:24 -06001487static int fsl_esdhc_get_cd(struct udevice *dev)
1488{
1489 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1490
1491 return true;
1492 return esdhc_getcd_common(priv);
1493}
1494
1495static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1496 struct mmc_data *data)
1497{
1498 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1499 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1500
1501 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1502}
1503
1504static int fsl_esdhc_set_ios(struct udevice *dev)
1505{
1506 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1507 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1508
1509 return esdhc_set_ios_common(priv, &plat->mmc);
1510}
1511
1512static const struct dm_mmc_ops fsl_esdhc_ops = {
1513 .get_cd = fsl_esdhc_get_cd,
1514 .send_cmd = fsl_esdhc_send_cmd,
1515 .set_ios = fsl_esdhc_set_ios,
Peng Fan51313b42018-01-21 19:00:24 +08001516#ifdef MMC_SUPPORTS_TUNING
1517 .execute_tuning = fsl_esdhc_execute_tuning,
1518#endif
Simon Glass653282b2017-07-29 11:35:24 -06001519};
1520#endif
1521
Peng Fan51313b42018-01-21 19:00:24 +08001522static struct esdhc_soc_data usdhc_imx7d_data = {
1523 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1524 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1525 | ESDHC_FLAG_HS400,
1526 .caps = UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_DDR_52MHz |
1527 MMC_MODE_HS_52MHz | MMC_MODE_HS,
1528};
1529
Peng Fan96f04072016-03-25 14:16:56 +08001530static const struct udevice_id fsl_esdhc_ids[] = {
1531 { .compatible = "fsl,imx6ul-usdhc", },
1532 { .compatible = "fsl,imx6sx-usdhc", },
1533 { .compatible = "fsl,imx6sl-usdhc", },
1534 { .compatible = "fsl,imx6q-usdhc", },
Peng Fan51313b42018-01-21 19:00:24 +08001535 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Peng Fanb60f1452017-02-22 16:21:55 +08001536 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lua6473f82016-12-07 11:54:31 +08001537 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001538 { /* sentinel */ }
1539};
1540
Simon Glass653282b2017-07-29 11:35:24 -06001541#if CONFIG_IS_ENABLED(BLK)
1542static int fsl_esdhc_bind(struct udevice *dev)
1543{
1544 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1545
1546 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1547}
1548#endif
1549
Peng Fan96f04072016-03-25 14:16:56 +08001550U_BOOT_DRIVER(fsl_esdhc) = {
1551 .name = "fsl-esdhc-mmc",
1552 .id = UCLASS_MMC,
1553 .of_match = fsl_esdhc_ids,
Simon Glass653282b2017-07-29 11:35:24 -06001554 .ops = &fsl_esdhc_ops,
Simon Glass653282b2017-07-29 11:35:24 -06001555#if CONFIG_IS_ENABLED(BLK)
1556 .bind = fsl_esdhc_bind,
1557#endif
Peng Fan96f04072016-03-25 14:16:56 +08001558 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -06001559 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +08001560 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1561};
1562#endif