blob: 9e34557d165a183948496eb1a59b8a97c3d7ec14 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleminge52ffb82008-10-30 16:47:16 -05002/*
Jerry Huanged413672011-01-06 23:42:19 -06003 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleminge52ffb82008-10-30 16:47:16 -05004 * Andy Fleming
5 *
6 * Based vaguely on the pxa mmc code:
7 * (C) Copyright 2003
8 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
Andy Fleminge52ffb82008-10-30 16:47:16 -05009 */
10
11#include <config.h>
12#include <common.h>
13#include <command.h>
Peng Fan4c286b72018-10-18 14:28:35 +020014#include <clk.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090015#include <errno.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040016#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050017#include <mmc.h>
18#include <part.h>
Peng Fan5eb8b432017-06-12 17:50:54 +080019#include <power/regulator.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050020#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040022#include <fdt_support.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050023#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080024#include <dm.h>
25#include <asm-generic/gpio.h>
Peng Fanc4142702018-01-21 19:00:24 +080026#include <dm/pinctrl.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050027
Andy Fleminge52ffb82008-10-30 16:47:16 -050028DECLARE_GLOBAL_DATA_PTR;
29
Ye.Li3d46c312014-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 Fanc4142702018-01-21 19:00:24 +080036#define MAX_TUNING_LOOP 40
Ye.Li3d46c312014-11-04 15:35:49 +080037
Andy Fleminge52ffb82008-10-30 16:47:16 -050038struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-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 Fana6eadd52016-06-15 10:53:00 +080062 char reserved2[4];
63 uint dllctrl;
64 uint dllstat;
65 uint clktunectrlstatus;
Peng Fanb9b42362018-01-21 19:00:22 +080066 char reserved3[4];
67 uint strobe_dllctrl;
68 uint strobe_dllstat;
69 char reserved4[72];
Peng Fana6eadd52016-06-15 10:53:00 +080070 uint vendorspec;
71 uint mmcboot;
72 uint vendorspec2;
Peng Fanb9b42362018-01-21 19:00:22 +080073 uint tuning_ctrl; /* on i.MX6/7/8 */
74 char reserved5[44];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080075 uint hostver; /* Host controller version register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020076 char reserved6[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080077 uint dmaerraddr; /* DMA error address register */
Peng Fana6eadd52016-06-15 10:53:00 +080078 char reserved7[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080079 uint dmaerrattr; /* DMA error attribute register */
80 char reserved8[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080081 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fanb9b42362018-01-21 19:00:22 +080082 char reserved9[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080083 uint tcr; /* Tuning control register */
Peng Fanb9b42362018-01-21 19:00:22 +080084 char reserved10[28]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080085 uint sddirctl; /* SD direction control register */
Peng Fanb9b42362018-01-21 19:00:22 +080086 char reserved11[712];/* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080087 uint scr; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050088};
89
Simon Glassfa02ca52017-07-29 11:35:21 -060090struct fsl_esdhc_plat {
91 struct mmc_config cfg;
92 struct mmc mmc;
93};
94
Peng Fanc4142702018-01-21 19:00:24 +080095struct esdhc_soc_data {
96 u32 flags;
97 u32 caps;
98};
99
Peng Fana4d36f72016-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 Fan01eb1c42016-06-15 10:53:02 +0800111 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fanaee78582017-06-12 17:50:53 +0800112 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fanc4142702018-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 Fana4d36f72016-03-25 14:16:56 +0800119 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +0800120 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +0800121 */
122struct fsl_esdhc_priv {
123 struct fsl_esdhc *esdhc_regs;
124 unsigned int sdhc_clk;
Peng Fan4c286b72018-10-18 14:28:35 +0200125 struct clk per_clk;
Peng Fanc4142702018-01-21 19:00:24 +0800126 unsigned int clock;
127 unsigned int mode;
Peng Fana4d36f72016-03-25 14:16:56 +0800128 unsigned int bus_width;
Simon Glass407025d2017-07-29 11:35:24 -0600129#if !CONFIG_IS_ENABLED(BLK)
Peng Fana4d36f72016-03-25 14:16:56 +0800130 struct mmc *mmc;
Simon Glass407025d2017-07-29 11:35:24 -0600131#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800132 struct udevice *dev;
133 int non_removable;
Peng Fan01eb1c42016-06-15 10:53:02 +0800134 int wp_enable;
Peng Fanaee78582017-06-12 17:50:53 +0800135 int vs18_enable;
Peng Fanc4142702018-01-21 19:00:24 +0800136 u32 flags;
137 u32 caps;
138 u32 tuning_step;
139 u32 tuning_start_tap;
140 u32 strobe_dll_delay_target;
141 u32 signal_voltage;
142#if IS_ENABLED(CONFIG_DM_REGULATOR)
143 struct udevice *vqmmc_dev;
144 struct udevice *vmmc_dev;
145#endif
Yangbo Lub99647c2016-12-07 11:54:30 +0800146#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800147 struct gpio_desc cd_gpio;
Peng Fan01eb1c42016-06-15 10:53:02 +0800148 struct gpio_desc wp_gpio;
Yangbo Lub99647c2016-12-07 11:54:30 +0800149#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800150};
151
Andy Fleminge52ffb82008-10-30 16:47:16 -0500152/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000153static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500154{
155 uint xfertyp = 0;
156
157 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530158 xfertyp |= XFERTYP_DPSEL;
159#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
160 xfertyp |= XFERTYP_DMAEN;
161#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500162 if (data->blocks > 1) {
163 xfertyp |= XFERTYP_MSBSEL;
164 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600165#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
166 xfertyp |= XFERTYP_AC12EN;
167#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500168 }
169
170 if (data->flags & MMC_DATA_READ)
171 xfertyp |= XFERTYP_DTDSEL;
172 }
173
174 if (cmd->resp_type & MMC_RSP_CRC)
175 xfertyp |= XFERTYP_CCCEN;
176 if (cmd->resp_type & MMC_RSP_OPCODE)
177 xfertyp |= XFERTYP_CICEN;
178 if (cmd->resp_type & MMC_RSP_136)
179 xfertyp |= XFERTYP_RSPTYP_136;
180 else if (cmd->resp_type & MMC_RSP_BUSY)
181 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
182 else if (cmd->resp_type & MMC_RSP_PRESENT)
183 xfertyp |= XFERTYP_RSPTYP_48;
184
Jason Liubef0ff02011-03-22 01:32:31 +0000185 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
186 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800187
Andy Fleminge52ffb82008-10-30 16:47:16 -0500188 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
189}
190
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530191#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
192/*
193 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
194 */
Simon Glass1d177d42017-07-29 11:35:17 -0600195static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
196 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530197{
Peng Fana4d36f72016-03-25 14:16:56 +0800198 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530199 uint blocks;
200 char *buffer;
201 uint databuf;
202 uint size;
203 uint irqstat;
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100204 ulong start;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530205
206 if (data->flags & MMC_DATA_READ) {
207 blocks = data->blocks;
208 buffer = data->dest;
209 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100210 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530211 size = data->blocksize;
212 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100213 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
214 if (get_timer(start) > PIO_TIMEOUT) {
215 printf("\nData Read Failed in PIO Mode.");
216 return;
217 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530218 }
219 while (size && (!(irqstat & IRQSTAT_TC))) {
220 udelay(100); /* Wait before last byte transfer complete */
221 irqstat = esdhc_read32(&regs->irqstat);
222 databuf = in_le32(&regs->datport);
223 *((uint *)buffer) = databuf;
224 buffer += 4;
225 size -= 4;
226 }
227 blocks--;
228 }
229 } else {
230 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200231 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530232 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100233 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530234 size = data->blocksize;
235 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100236 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
237 if (get_timer(start) > PIO_TIMEOUT) {
238 printf("\nData Write Failed in PIO Mode.");
239 return;
240 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530241 }
242 while (size && (!(irqstat & IRQSTAT_TC))) {
243 udelay(100); /* Wait before last byte transfer complete */
244 databuf = *((uint *)buffer);
245 buffer += 4;
246 size -= 4;
247 irqstat = esdhc_read32(&regs->irqstat);
248 out_le32(&regs->datport, databuf);
249 }
250 blocks--;
251 }
252 }
253}
254#endif
255
Simon Glass1d177d42017-07-29 11:35:17 -0600256static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
257 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500258{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500259 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800260 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Fan3364c4b2018-01-10 13:20:40 +0800261#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fan39945c12018-11-20 10:19:25 +0000262 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700263 dma_addr_t addr;
264#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200265 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500266
267 wml_value = data->blocksize/4;
268
269 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530270 if (wml_value > WML_RD_WML_MAX)
271 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500272
Roy Zange5853af2010-02-09 18:23:33 +0800273 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800274#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Fan3364c4b2018-01-10 13:20:40 +0800275#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fan39945c12018-11-20 10:19:25 +0000276 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700277 addr = virt_to_phys((void *)(data->dest));
278 if (upper_32_bits(addr))
279 printf("Error found for upper 32 bits\n");
280 else
281 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
282#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100283 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800284#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700285#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500286 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800287#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000288 flush_dcache_range((ulong)data->src,
289 (ulong)data->src+data->blocks
290 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800291#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530292 if (wml_value > WML_WR_WML_MAX)
293 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan01eb1c42016-06-15 10:53:02 +0800294 if (priv->wp_enable) {
295 if ((esdhc_read32(&regs->prsstat) &
296 PRSSTAT_WPSPL) == 0) {
297 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900298 return -ETIMEDOUT;
Peng Fan01eb1c42016-06-15 10:53:02 +0800299 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500300 }
Roy Zange5853af2010-02-09 18:23:33 +0800301
302 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
303 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800304#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Fan3364c4b2018-01-10 13:20:40 +0800305#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fan39945c12018-11-20 10:19:25 +0000306 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700307 addr = virt_to_phys((void *)(data->src));
308 if (upper_32_bits(addr))
309 printf("Error found for upper 32 bits\n");
310 else
311 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
312#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100313 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800314#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700315#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500316 }
317
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100318 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500319
320 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530321 /*
322 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
323 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
324 * So, Number of SD Clock cycles for 0.25sec should be minimum
325 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500326 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530327 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500328 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530329 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500330 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530331 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500332 * => timeout + 13 = log2(mmc->clock/4) + 1
333 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800334 *
335 * However, the MMC spec "It is strongly recommended for hosts to
336 * implement more than 500ms timeout value even if the card
337 * indicates the 250ms maximum busy length." Even the previous
338 * value of 300ms is known to be insufficient for some cards.
339 * So, we use
340 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530341 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800342 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500343 timeout -= 13;
344
345 if (timeout > 14)
346 timeout = 14;
347
348 if (timeout < 0)
349 timeout = 0;
350
Kumar Gala9a878d52011-01-29 15:36:10 -0600351#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
352 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
353 timeout++;
354#endif
355
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800356#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
357 timeout = 0xE;
358#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100359 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500360
361 return 0;
362}
363
Eric Nelson30e9cad2012-04-25 14:28:48 +0000364static void check_and_invalidate_dcache_range
365 (struct mmc_cmd *cmd,
366 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700367 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800368 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000369 unsigned size = roundup(ARCH_DMA_MINALIGN,
370 data->blocks*data->blocksize);
Peng Fan3364c4b2018-01-10 13:20:40 +0800371#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fan39945c12018-11-20 10:19:25 +0000372 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700373 dma_addr_t addr;
374
375 addr = virt_to_phys((void *)(data->dest));
376 if (upper_32_bits(addr))
377 printf("Error found for upper 32 bits\n");
378 else
379 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800380#else
381 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700382#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800383 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000384 invalidate_dcache_range(start, end);
385}
Tom Rini239dd252014-05-23 09:19:05 -0400386
Angelo Dureghello520a6692019-01-19 10:40:38 +0100387#ifdef CONFIG_MCF5441x
388/*
389 * Swaps 32-bit words to little-endian byte order.
390 */
391static inline void sd_swap_dma_buff(struct mmc_data *data)
392{
393 int i, size = data->blocksize >> 2;
394 u32 *buffer = (u32 *)data->dest;
395 u32 sw;
396
397 while (data->blocks--) {
398 for (i = 0; i < size; i++) {
399 sw = __sw32(*buffer);
400 *buffer++ = sw;
401 }
402 }
403}
404#endif
405
Andy Fleminge52ffb82008-10-30 16:47:16 -0500406/*
407 * Sends a command out on the bus. Takes the mmc pointer,
408 * a command pointer, and an optional data pointer.
409 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600410static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
411 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500412{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500413 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500414 uint xfertyp;
415 uint irqstat;
Peng Fanc4142702018-01-21 19:00:24 +0800416 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fana4d36f72016-03-25 14:16:56 +0800417 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam7300ef52018-11-19 10:31:53 -0200418 unsigned long start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500419
Jerry Huanged413672011-01-06 23:42:19 -0600420#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
421 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
422 return 0;
423#endif
424
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100425 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500426
427 sync();
428
429 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100430 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
431 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
432 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500433
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100434 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
435 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500436
437 /* Wait at least 8 SD clock cycles before the next command */
438 /*
439 * Note: This is way more than 8 cycles, but 1ms seems to
440 * resolve timing issues with some cards
441 */
442 udelay(1000);
443
444 /* Set up for a data transfer if we have one */
445 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600446 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500447 if(err)
448 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800449
450 if (data->flags & MMC_DATA_READ)
451 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500452 }
453
454 /* Figure out the transfer arguments */
455 xfertyp = esdhc_xfertyp(cmd, data);
456
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500457 /* Mask all irqs */
458 esdhc_write32(&regs->irqsigen, 0);
459
Andy Fleminge52ffb82008-10-30 16:47:16 -0500460 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100461 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000462#if defined(CONFIG_FSL_USDHC)
463 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500464 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
465 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu9919d642011-11-25 00:18:04 +0000466 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
467#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100468 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000469#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000470
Peng Fanc4142702018-01-21 19:00:24 +0800471 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
472 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
473 flags = IRQSTAT_BRR;
474
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475 /* Wait for the command to complete */
Fabio Estevam7300ef52018-11-19 10:31:53 -0200476 start = get_timer(0);
477 while (!(esdhc_read32(&regs->irqstat) & flags)) {
478 if (get_timer(start) > 1000) {
479 err = -ETIMEDOUT;
480 goto out;
481 }
482 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500483
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100484 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500485
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500486 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900487 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500488 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000489 }
490
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500491 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900492 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500493 goto out;
494 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500495
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200496 /* Switch voltage to 1.8V if CMD11 succeeded */
497 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
498 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
499
500 printf("Run CMD11 1.8V switch\n");
501 /* Sleep for 5 ms - max time for card to switch to 1.8V */
502 udelay(5000);
503 }
504
Dirk Behmed8552d62012-03-26 03:13:05 +0000505 /* Workaround for ESDHC errata ENGcm03648 */
506 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800507 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000508
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800509 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000510 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
511 PRSSTAT_DAT0)) {
512 udelay(100);
513 timeout--;
514 }
515
516 if (timeout <= 0) {
517 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900518 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500519 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000520 }
521 }
522
Andy Fleminge52ffb82008-10-30 16:47:16 -0500523 /* Copy the response to the response buffer */
524 if (cmd->resp_type & MMC_RSP_136) {
525 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
526
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100527 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
528 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
529 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
530 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530531 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
532 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
533 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
534 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500535 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100536 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500537
538 /* Wait until all of the blocks are transferred */
539 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530540#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass1d177d42017-07-29 11:35:17 -0600541 esdhc_pio_read_write(priv, data);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530542#else
Peng Fanc4142702018-01-21 19:00:24 +0800543 flags = DATA_COMPLETE;
544 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
545 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
546 flags = IRQSTAT_BRR;
547 }
548
Andy Fleminge52ffb82008-10-30 16:47:16 -0500549 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100550 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500551
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500552 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900553 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500554 goto out;
555 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000556
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500557 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900558 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500559 goto out;
560 }
Peng Fanc4142702018-01-21 19:00:24 +0800561 } while ((irqstat & flags) != flags);
Ye.Li33a56b12014-02-20 18:00:57 +0800562
Peng Fan9cb5e992015-06-25 10:32:26 +0800563 /*
564 * Need invalidate the dcache here again to avoid any
565 * cache-fill during the DMA operations such as the
566 * speculative pre-fetching etc.
567 */
Angelo Dureghello520a6692019-01-19 10:40:38 +0100568 if (data->flags & MMC_DATA_READ) {
Eric Nelson70e68692013-04-03 12:31:56 +0000569 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello520a6692019-01-19 10:40:38 +0100570#ifdef CONFIG_MCF5441x
571 sd_swap_dma_buff(data);
572#endif
573 }
Ye.Li33a56b12014-02-20 18:00:57 +0800574#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500575 }
576
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500577out:
578 /* Reset CMD and DATA portions on error */
579 if (err) {
580 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
581 SYSCTL_RSTC);
582 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
583 ;
584
585 if (data) {
586 esdhc_write32(&regs->sysctl,
587 esdhc_read32(&regs->sysctl) |
588 SYSCTL_RSTD);
589 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
590 ;
591 }
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200592
593 /* If this was CMD11, then notify that power cycle is needed */
594 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
595 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500596 }
597
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100598 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500599
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500600 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500601}
602
Simon Glass1d177d42017-07-29 11:35:17 -0600603static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500604{
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100605 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200606 int div = 1;
607#ifdef ARCH_MXC
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100608#ifdef CONFIG_MX53
609 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
610 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
611#else
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200612 int pre_div = 1;
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100613#endif
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200614#else
615 int pre_div = 2;
616#endif
617 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fana4d36f72016-03-25 14:16:56 +0800618 int sdhc_clk = priv->sdhc_clk;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500619 uint clk;
620
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200621 if (clock < mmc->cfg->f_min)
622 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100623
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200624 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
625 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500626
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200627 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
628 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500629
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200630 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500631 div -= 1;
632
633 clk = (pre_div << 8) | (div << 4);
634
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700635#ifdef CONFIG_FSL_USDHC
Ye Li5a24f292016-06-15 10:53:01 +0800636 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700637#else
Kumar Gala09876a32010-03-18 15:51:05 -0500638 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700639#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100640
641 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500642
643 udelay(10000);
644
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700645#ifdef CONFIG_FSL_USDHC
Ye Li5a24f292016-06-15 10:53:01 +0800646 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700647#else
648 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
649#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100650
Peng Fanc4142702018-01-21 19:00:24 +0800651 priv->clock = clock;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500652}
653
Yangbo Lu163beec2015-04-22 13:57:40 +0800654#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass1d177d42017-07-29 11:35:17 -0600655static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800656{
Peng Fana4d36f72016-03-25 14:16:56 +0800657 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800658 u32 value;
659 u32 time_out;
660
661 value = esdhc_read32(&regs->sysctl);
662
663 if (enable)
664 value |= SYSCTL_CKEN;
665 else
666 value &= ~SYSCTL_CKEN;
667
668 esdhc_write32(&regs->sysctl, value);
669
670 time_out = 20;
671 value = PRSSTAT_SDSTB;
672 while (!(esdhc_read32(&regs->prsstat) & value)) {
673 if (time_out == 0) {
674 printf("fsl_esdhc: Internal clock never stabilised.\n");
675 break;
676 }
677 time_out--;
678 mdelay(1);
679 }
Peng Fanc4142702018-01-21 19:00:24 +0800680}
681#endif
682
683#ifdef MMC_SUPPORTS_TUNING
684static int esdhc_change_pinstate(struct udevice *dev)
685{
686 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
687 int ret;
688
689 switch (priv->mode) {
690 case UHS_SDR50:
691 case UHS_DDR50:
692 ret = pinctrl_select_state(dev, "state_100mhz");
693 break;
694 case UHS_SDR104:
695 case MMC_HS_200:
Peng Fanddd8d752018-08-10 14:07:55 +0800696 case MMC_HS_400:
Peng Fanc4142702018-01-21 19:00:24 +0800697 ret = pinctrl_select_state(dev, "state_200mhz");
698 break;
699 default:
700 ret = pinctrl_select_state(dev, "default");
701 break;
702 }
703
704 if (ret)
705 printf("%s %d error\n", __func__, priv->mode);
706
707 return ret;
708}
709
710static void esdhc_reset_tuning(struct mmc *mmc)
711{
712 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
713 struct fsl_esdhc *regs = priv->esdhc_regs;
714
715 if (priv->flags & ESDHC_FLAG_USDHC) {
716 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
717 esdhc_clrbits32(&regs->autoc12err,
718 MIX_CTRL_SMPCLK_SEL |
719 MIX_CTRL_EXE_TUNE);
720 }
721 }
722}
723
Peng Fanddd8d752018-08-10 14:07:55 +0800724static void esdhc_set_strobe_dll(struct mmc *mmc)
725{
726 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
727 struct fsl_esdhc *regs = priv->esdhc_regs;
728 u32 val;
729
730 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
731 writel(ESDHC_STROBE_DLL_CTRL_RESET, &regs->strobe_dllctrl);
732
733 /*
734 * enable strobe dll ctrl and adjust the delay target
735 * for the uSDHC loopback read clock
736 */
737 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
738 (priv->strobe_dll_delay_target <<
739 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
740 writel(val, &regs->strobe_dllctrl);
741 /* wait 1us to make sure strobe dll status register stable */
742 mdelay(1);
743 val = readl(&regs->strobe_dllstat);
744 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
745 pr_warn("HS400 strobe DLL status REF not lock!\n");
746 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
747 pr_warn("HS400 strobe DLL status SLV not lock!\n");
748 }
749}
750
Peng Fanc4142702018-01-21 19:00:24 +0800751static int esdhc_set_timing(struct mmc *mmc)
752{
753 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
754 struct fsl_esdhc *regs = priv->esdhc_regs;
755 u32 mixctrl;
756
757 mixctrl = readl(&regs->mixctrl);
758 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
759
760 switch (mmc->selected_mode) {
761 case MMC_LEGACY:
762 case SD_LEGACY:
763 esdhc_reset_tuning(mmc);
Peng Fanddd8d752018-08-10 14:07:55 +0800764 writel(mixctrl, &regs->mixctrl);
765 break;
766 case MMC_HS_400:
767 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
768 writel(mixctrl, &regs->mixctrl);
769 esdhc_set_strobe_dll(mmc);
Peng Fanc4142702018-01-21 19:00:24 +0800770 break;
771 case MMC_HS:
772 case MMC_HS_52:
773 case MMC_HS_200:
774 case SD_HS:
775 case UHS_SDR12:
776 case UHS_SDR25:
777 case UHS_SDR50:
778 case UHS_SDR104:
779 writel(mixctrl, &regs->mixctrl);
780 break;
781 case UHS_DDR50:
782 case MMC_DDR_52:
783 mixctrl |= MIX_CTRL_DDREN;
784 writel(mixctrl, &regs->mixctrl);
785 break;
786 default:
787 printf("Not supported %d\n", mmc->selected_mode);
788 return -EINVAL;
789 }
790
791 priv->mode = mmc->selected_mode;
792
793 return esdhc_change_pinstate(mmc->dev);
Yangbo Lu163beec2015-04-22 13:57:40 +0800794}
Peng Fanc4142702018-01-21 19:00:24 +0800795
796static int esdhc_set_voltage(struct mmc *mmc)
797{
798 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
799 struct fsl_esdhc *regs = priv->esdhc_regs;
800 int ret;
801
802 priv->signal_voltage = mmc->signal_voltage;
803 switch (mmc->signal_voltage) {
804 case MMC_SIGNAL_VOLTAGE_330:
805 if (priv->vs18_enable)
806 return -EIO;
Abel Vesaf3a7c242019-02-01 16:40:11 +0000807#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanc4142702018-01-21 19:00:24 +0800808 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
809 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
810 if (ret) {
811 printf("Setting to 3.3V error");
812 return -EIO;
813 }
814 /* Wait for 5ms */
815 mdelay(5);
816 }
817#endif
818
819 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
820 if (!(esdhc_read32(&regs->vendorspec) &
821 ESDHC_VENDORSPEC_VSELECT))
822 return 0;
823
824 return -EAGAIN;
825 case MMC_SIGNAL_VOLTAGE_180:
Abel Vesaf3a7c242019-02-01 16:40:11 +0000826#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanc4142702018-01-21 19:00:24 +0800827 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
828 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
829 if (ret) {
830 printf("Setting to 1.8V error");
831 return -EIO;
832 }
833 }
Yangbo Lu163beec2015-04-22 13:57:40 +0800834#endif
Peng Fanc4142702018-01-21 19:00:24 +0800835 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
836 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
837 return 0;
838
839 return -EAGAIN;
840 case MMC_SIGNAL_VOLTAGE_120:
841 return -ENOTSUPP;
842 default:
843 return 0;
844 }
845}
846
847static void esdhc_stop_tuning(struct mmc *mmc)
848{
849 struct mmc_cmd cmd;
850
851 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
852 cmd.cmdarg = 0;
853 cmd.resp_type = MMC_RSP_R1b;
854
855 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
856}
857
858static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
859{
860 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
861 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
862 struct fsl_esdhc *regs = priv->esdhc_regs;
863 struct mmc *mmc = &plat->mmc;
864 u32 irqstaten = readl(&regs->irqstaten);
865 u32 irqsigen = readl(&regs->irqsigen);
866 int i, ret = -ETIMEDOUT;
867 u32 val, mixctrl;
868
869 /* clock tuning is not needed for upto 52MHz */
870 if (mmc->clock <= 52000000)
871 return 0;
872
873 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
874 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
875 val = readl(&regs->autoc12err);
876 mixctrl = readl(&regs->mixctrl);
877 val &= ~MIX_CTRL_SMPCLK_SEL;
878 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
879
880 val |= MIX_CTRL_EXE_TUNE;
881 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
882
883 writel(val, &regs->autoc12err);
884 writel(mixctrl, &regs->mixctrl);
885 }
886
887 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
888 mixctrl = readl(&regs->mixctrl);
889 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
890 writel(mixctrl, &regs->mixctrl);
891
892 writel(IRQSTATEN_BRR, &regs->irqstaten);
893 writel(IRQSTATEN_BRR, &regs->irqsigen);
894
895 /*
896 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
897 * of loops reaches 40 times.
898 */
899 for (i = 0; i < MAX_TUNING_LOOP; i++) {
900 u32 ctrl;
901
902 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
903 if (mmc->bus_width == 8)
904 writel(0x7080, &regs->blkattr);
905 else if (mmc->bus_width == 4)
906 writel(0x7040, &regs->blkattr);
907 } else {
908 writel(0x7040, &regs->blkattr);
909 }
910
911 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
912 val = readl(&regs->mixctrl);
913 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
914 writel(val, &regs->mixctrl);
915
916 /* We are using STD tuning, no need to check return value */
917 mmc_send_tuning(mmc, opcode, NULL);
918
919 ctrl = readl(&regs->autoc12err);
920 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
921 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
922 /*
923 * need to wait some time, make sure sd/mmc fininsh
924 * send out tuning data, otherwise, the sd/mmc can't
925 * response to any command when the card still out
926 * put the tuning data.
927 */
928 mdelay(1);
929 ret = 0;
930 break;
931 }
932
933 /* Add 1ms delay for SD and eMMC */
934 mdelay(1);
935 }
936
937 writel(irqstaten, &regs->irqstaten);
938 writel(irqsigen, &regs->irqsigen);
939
940 esdhc_stop_tuning(mmc);
941
942 return ret;
943}
944#endif
Yangbo Lu163beec2015-04-22 13:57:40 +0800945
Simon Glass6aa55dc2017-07-29 11:35:18 -0600946static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500947{
Peng Fana4d36f72016-03-25 14:16:56 +0800948 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Fanc4142702018-01-21 19:00:24 +0800949 int ret __maybe_unused;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500950
Yangbo Lu163beec2015-04-22 13:57:40 +0800951#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
952 /* Select to use peripheral clock */
Simon Glass1d177d42017-07-29 11:35:17 -0600953 esdhc_clock_control(priv, false);
Yangbo Lu163beec2015-04-22 13:57:40 +0800954 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
Simon Glass1d177d42017-07-29 11:35:17 -0600955 esdhc_clock_control(priv, true);
Yangbo Lu163beec2015-04-22 13:57:40 +0800956#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500957 /* Set the clock speed */
Peng Fanc4142702018-01-21 19:00:24 +0800958 if (priv->clock != mmc->clock)
959 set_sysctl(priv, mmc, mmc->clock);
960
961#ifdef MMC_SUPPORTS_TUNING
962 if (mmc->clk_disable) {
963#ifdef CONFIG_FSL_USDHC
964 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
965#else
966 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
967#endif
968 } else {
969#ifdef CONFIG_FSL_USDHC
970 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
971 VENDORSPEC_CKEN);
972#else
973 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
974#endif
975 }
976
977 if (priv->mode != mmc->selected_mode) {
978 ret = esdhc_set_timing(mmc);
979 if (ret) {
980 printf("esdhc_set_timing error %d\n", ret);
981 return ret;
982 }
983 }
984
985 if (priv->signal_voltage != mmc->signal_voltage) {
986 ret = esdhc_set_voltage(mmc);
987 if (ret) {
988 printf("esdhc_set_voltage error %d\n", ret);
989 return ret;
990 }
991 }
992#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500993
994 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100995 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500996
997 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100998 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500999 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001000 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1001
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +09001002 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001003}
1004
Simon Glass6aa55dc2017-07-29 11:35:18 -06001005static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -05001006{
Peng Fana4d36f72016-03-25 14:16:56 +08001007 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass0c3ef222017-07-29 11:35:20 -06001008 ulong start;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001009
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001010 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +02001011 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001012
1013 /* Wait until the controller is available */
Simon Glass0c3ef222017-07-29 11:35:20 -06001014 start = get_timer(0);
1015 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1016 if (get_timer(start) > 1000)
1017 return -ETIMEDOUT;
1018 }
Andy Fleminge52ffb82008-10-30 16:47:16 -05001019
Peng Fana6eadd52016-06-15 10:53:00 +08001020#if defined(CONFIG_FSL_USDHC)
1021 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1022 esdhc_write32(&regs->mmcboot, 0x0);
1023 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1024 esdhc_write32(&regs->mixctrl, 0x0);
1025 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1026
1027 /* Put VEND_SPEC to default value */
Peng Fan283620c2018-01-02 16:51:22 +08001028 if (priv->vs18_enable)
1029 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1030 ESDHC_VENDORSPEC_VSELECT));
1031 else
1032 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
Peng Fana6eadd52016-06-15 10:53:00 +08001033
1034 /* Disable DLL_CTRL delay line */
1035 esdhc_write32(&regs->dllctrl, 0x0);
1036#endif
1037
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +00001038#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +05301039 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +00001040 esdhc_write32(&regs->scr, 0x00000040);
1041#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +05301042
Eric Nelsonc8e615c2015-12-04 12:32:48 -07001043#ifndef CONFIG_FSL_USDHC
Dirk Behmedbe67252013-07-15 15:44:29 +02001044 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li5a24f292016-06-15 10:53:01 +08001045#else
1046 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -07001047#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -05001048
1049 /* Set the initial clock speed */
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09001050 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleminge52ffb82008-10-30 16:47:16 -05001051
1052 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001053 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -05001054
Angelo Dureghello520a6692019-01-19 10:40:38 +01001055#ifdef CONFIG_MCF5441x
1056 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1057#else
Andy Fleminge52ffb82008-10-30 16:47:16 -05001058 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001059 esdhc_write32(&regs->proctl, PROCTL_INIT);
Angelo Dureghello520a6692019-01-19 10:40:38 +01001060#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -05001061
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001062 /* Set timout to the maximum value */
1063 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -05001064
Thierry Reding8cee4c982012-01-02 01:15:38 +00001065 return 0;
1066}
1067
Simon Glass6aa55dc2017-07-29 11:35:18 -06001068static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Reding8cee4c982012-01-02 01:15:38 +00001069{
Peng Fana4d36f72016-03-25 14:16:56 +08001070 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Reding8cee4c982012-01-02 01:15:38 +00001071 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001072
Haijun.Zhang05f58542014-01-10 13:52:17 +08001073#ifdef CONFIG_ESDHC_DETECT_QUIRK
1074 if (CONFIG_ESDHC_DETECT_QUIRK)
1075 return 1;
1076#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001077
Simon Glass407025d2017-07-29 11:35:24 -06001078#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +08001079 if (priv->non_removable)
1080 return 1;
Yangbo Lub99647c2016-12-07 11:54:30 +08001081#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +08001082 if (dm_gpio_is_valid(&priv->cd_gpio))
1083 return dm_gpio_get_value(&priv->cd_gpio);
1084#endif
Yangbo Lub99647c2016-12-07 11:54:30 +08001085#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001086
Thierry Reding8cee4c982012-01-02 01:15:38 +00001087 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1088 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001089
Thierry Reding8cee4c982012-01-02 01:15:38 +00001090 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001091}
1092
Simon Glass81357b52017-07-29 11:35:19 -06001093static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huangb7ef7562010-03-18 15:57:06 -05001094{
Simon Glass81357b52017-07-29 11:35:19 -06001095 ulong start;
Jerry Huangb7ef7562010-03-18 15:57:06 -05001096
1097 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +02001098 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -05001099
1100 /* hardware clears the bit when it is done */
Simon Glass81357b52017-07-29 11:35:19 -06001101 start = get_timer(0);
1102 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1103 if (get_timer(start) > 100) {
1104 printf("MMC/SD: Reset never completed.\n");
1105 return -ETIMEDOUT;
1106 }
1107 }
1108
1109 return 0;
Jerry Huangb7ef7562010-03-18 15:57:06 -05001110}
1111
Simon Glasseba48f92017-07-29 11:35:31 -06001112#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass6aa55dc2017-07-29 11:35:18 -06001113static int esdhc_getcd(struct mmc *mmc)
1114{
1115 struct fsl_esdhc_priv *priv = mmc->priv;
1116
1117 return esdhc_getcd_common(priv);
1118}
1119
1120static int esdhc_init(struct mmc *mmc)
1121{
1122 struct fsl_esdhc_priv *priv = mmc->priv;
1123
1124 return esdhc_init_common(priv, mmc);
1125}
1126
1127static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1128 struct mmc_data *data)
1129{
1130 struct fsl_esdhc_priv *priv = mmc->priv;
1131
1132 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1133}
1134
1135static int esdhc_set_ios(struct mmc *mmc)
1136{
1137 struct fsl_esdhc_priv *priv = mmc->priv;
1138
1139 return esdhc_set_ios_common(priv, mmc);
1140}
1141
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001142static const struct mmc_ops esdhc_ops = {
Simon Glass6aa55dc2017-07-29 11:35:18 -06001143 .getcd = esdhc_getcd,
1144 .init = esdhc_init,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001145 .send_cmd = esdhc_send_cmd,
1146 .set_ios = esdhc_set_ios,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001147};
Simon Glass407025d2017-07-29 11:35:24 -06001148#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001149
Simon Glassfa02ca52017-07-29 11:35:21 -06001150static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1151 struct fsl_esdhc_plat *plat)
Andy Fleminge52ffb82008-10-30 16:47:16 -05001152{
Simon Glassfa02ca52017-07-29 11:35:21 -06001153 struct mmc_config *cfg;
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001154 struct fsl_esdhc *regs;
Li Yangd4933f22010-11-25 17:06:09 +00001155 u32 caps, voltage_caps;
Simon Glass81357b52017-07-29 11:35:19 -06001156 int ret;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001157
Peng Fana4d36f72016-03-25 14:16:56 +08001158 if (!priv)
1159 return -EINVAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001160
Peng Fana4d36f72016-03-25 14:16:56 +08001161 regs = priv->esdhc_regs;
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001162
Jerry Huangb7ef7562010-03-18 15:57:06 -05001163 /* First reset the eSDHC controller */
Simon Glass81357b52017-07-29 11:35:19 -06001164 ret = esdhc_reset(regs);
1165 if (ret)
1166 return ret;
Jerry Huangb7ef7562010-03-18 15:57:06 -05001167
Angelo Dureghello520a6692019-01-19 10:40:38 +01001168#ifdef CONFIG_MCF5441x
1169 /* ColdFire, using SDHC_DATA[3] for card detection */
1170 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1171#endif
1172
Eric Nelsonc8e615c2015-12-04 12:32:48 -07001173#ifndef CONFIG_FSL_USDHC
Jerry Huang4e3bfa02012-05-17 23:57:02 +00001174 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1175 | SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fanc4142702018-01-21 19:00:24 +08001176 /* Clearing tuning bits in case ROM has set it already */
1177 esdhc_write32(&regs->mixctrl, 0);
1178 esdhc_write32(&regs->autoc12err, 0);
1179 esdhc_write32(&regs->clktunectrlstatus, 0);
Ye Li5a24f292016-06-15 10:53:01 +08001180#else
1181 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1182 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -07001183#endif
Jerry Huang4e3bfa02012-05-17 23:57:02 +00001184
Peng Fanaee78582017-06-12 17:50:53 +08001185 if (priv->vs18_enable)
1186 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1187
Ye.Li3d46c312014-11-04 15:35:49 +08001188 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glassfa02ca52017-07-29 11:35:21 -06001189 cfg = &plat->cfg;
Simon Glass407025d2017-07-29 11:35:24 -06001190#ifndef CONFIG_DM_MMC
Simon Glassfa02ca52017-07-29 11:35:21 -06001191 memset(cfg, '\0', sizeof(*cfg));
Simon Glass407025d2017-07-29 11:35:24 -06001192#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001193
Li Yangd4933f22010-11-25 17:06:09 +00001194 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +08001195 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -06001196
Angelo Dureghello520a6692019-01-19 10:40:38 +01001197#ifdef CONFIG_MCF5441x
1198 /*
1199 * MCF5441x RM declares in more points that sdhc clock speed must
1200 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1201 * from host capabilities.
1202 */
1203 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1204#endif
1205
Roy Zang39356612011-01-07 00:06:47 -06001206#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1207 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1208 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1209#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +08001210
1211/* T4240 host controller capabilities register should have VS33 bit */
1212#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1213 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1214#endif
1215
Andy Fleminge52ffb82008-10-30 16:47:16 -05001216 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +00001217 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001218 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +00001219 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001220 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +00001221 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1222
Simon Glassfa02ca52017-07-29 11:35:21 -06001223 cfg->name = "FSL_SDHC";
Simon Glasseba48f92017-07-29 11:35:31 -06001224#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassfa02ca52017-07-29 11:35:21 -06001225 cfg->ops = &esdhc_ops;
Simon Glass407025d2017-07-29 11:35:24 -06001226#endif
Li Yangd4933f22010-11-25 17:06:09 +00001227#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glassfa02ca52017-07-29 11:35:21 -06001228 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +00001229#else
Simon Glassfa02ca52017-07-29 11:35:21 -06001230 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +00001231#endif
Simon Glassfa02ca52017-07-29 11:35:21 -06001232 if ((cfg->voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +00001233 printf("voltage not supported by controller\n");
1234 return -1;
1235 }
Andy Fleminge52ffb82008-10-30 16:47:16 -05001236
Peng Fana4d36f72016-03-25 14:16:56 +08001237 if (priv->bus_width == 8)
Simon Glassfa02ca52017-07-29 11:35:21 -06001238 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fana4d36f72016-03-25 14:16:56 +08001239 else if (priv->bus_width == 4)
Simon Glassfa02ca52017-07-29 11:35:21 -06001240 cfg->host_caps = MMC_MODE_4BIT;
Peng Fana4d36f72016-03-25 14:16:56 +08001241
Simon Glassfa02ca52017-07-29 11:35:21 -06001242 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -05001243#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glassfa02ca52017-07-29 11:35:21 -06001244 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -05001245#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -05001246
Peng Fana4d36f72016-03-25 14:16:56 +08001247 if (priv->bus_width > 0) {
1248 if (priv->bus_width < 8)
Simon Glassfa02ca52017-07-29 11:35:21 -06001249 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fana4d36f72016-03-25 14:16:56 +08001250 if (priv->bus_width < 4)
Simon Glassfa02ca52017-07-29 11:35:21 -06001251 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +00001252 }
1253
Andy Fleminge52ffb82008-10-30 16:47:16 -05001254 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glassfa02ca52017-07-29 11:35:21 -06001255 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -05001256
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +08001257#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1258 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glassfa02ca52017-07-29 11:35:21 -06001259 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +08001260#endif
1261
Peng Fanc4142702018-01-21 19:00:24 +08001262 cfg->host_caps |= priv->caps;
1263
Simon Glassfa02ca52017-07-29 11:35:21 -06001264 cfg->f_min = 400000;
Peng Fanc4142702018-01-21 19:00:24 +08001265 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -05001266
Simon Glassfa02ca52017-07-29 11:35:21 -06001267 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001268
Peng Fanc4142702018-01-21 19:00:24 +08001269 writel(0, &regs->dllctrl);
1270 if (priv->flags & ESDHC_FLAG_USDHC) {
1271 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1272 u32 val = readl(&regs->tuning_ctrl);
1273
1274 val |= ESDHC_STD_TUNING_EN;
1275 val &= ~ESDHC_TUNING_START_TAP_MASK;
1276 val |= priv->tuning_start_tap;
1277 val &= ~ESDHC_TUNING_STEP_MASK;
1278 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1279 writel(val, &regs->tuning_ctrl);
1280 }
1281 }
1282
Peng Fana4d36f72016-03-25 14:16:56 +08001283 return 0;
1284}
1285
Simon Glassb9876e22017-07-29 11:35:28 -06001286#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki3c2cc6d2017-05-12 17:18:20 +05301287static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1288 struct fsl_esdhc_priv *priv)
1289{
1290 if (!cfg || !priv)
1291 return -EINVAL;
1292
1293 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1294 priv->bus_width = cfg->max_bus_width;
1295 priv->sdhc_clk = cfg->sdhc_clk;
1296 priv->wp_enable = cfg->wp_enable;
Peng Fanaee78582017-06-12 17:50:53 +08001297 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki3c2cc6d2017-05-12 17:18:20 +05301298
1299 return 0;
1300};
1301
Peng Fana4d36f72016-03-25 14:16:56 +08001302int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
1303{
Simon Glassfa02ca52017-07-29 11:35:21 -06001304 struct fsl_esdhc_plat *plat;
Peng Fana4d36f72016-03-25 14:16:56 +08001305 struct fsl_esdhc_priv *priv;
Simon Glass5ee39802017-07-29 11:35:22 -06001306 struct mmc *mmc;
Peng Fana4d36f72016-03-25 14:16:56 +08001307 int ret;
1308
1309 if (!cfg)
1310 return -EINVAL;
1311
1312 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1313 if (!priv)
1314 return -ENOMEM;
Simon Glassfa02ca52017-07-29 11:35:21 -06001315 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1316 if (!plat) {
1317 free(priv);
1318 return -ENOMEM;
1319 }
Peng Fana4d36f72016-03-25 14:16:56 +08001320
1321 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1322 if (ret) {
1323 debug("%s xlate failure\n", __func__);
Simon Glassfa02ca52017-07-29 11:35:21 -06001324 free(plat);
Peng Fana4d36f72016-03-25 14:16:56 +08001325 free(priv);
1326 return ret;
1327 }
1328
Simon Glassfa02ca52017-07-29 11:35:21 -06001329 ret = fsl_esdhc_init(priv, plat);
Peng Fana4d36f72016-03-25 14:16:56 +08001330 if (ret) {
1331 debug("%s init failure\n", __func__);
Simon Glassfa02ca52017-07-29 11:35:21 -06001332 free(plat);
Peng Fana4d36f72016-03-25 14:16:56 +08001333 free(priv);
1334 return ret;
1335 }
1336
Simon Glass5ee39802017-07-29 11:35:22 -06001337 mmc = mmc_create(&plat->cfg, priv);
1338 if (!mmc)
1339 return -EIO;
1340
1341 priv->mmc = mmc;
1342
Andy Fleminge52ffb82008-10-30 16:47:16 -05001343 return 0;
1344}
1345
1346int fsl_esdhc_mmc_init(bd_t *bis)
1347{
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001348 struct fsl_esdhc_cfg *cfg;
1349
Fabio Estevam6592a992012-12-27 08:51:08 +00001350 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001351 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +00001352 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001353 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -05001354}
Jagan Teki3c2cc6d2017-05-12 17:18:20 +05301355#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001356
Yangbo Lub124f8a2015-04-22 13:57:00 +08001357#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1358void mmc_adapter_card_type_ident(void)
1359{
1360 u8 card_id;
1361 u8 value;
1362
1363 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
1364 gd->arch.sdhc_adapter = card_id;
1365
1366 switch (card_id) {
1367 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lu81eacd62015-09-17 10:27:12 +08001368 value = QIXIS_READ(brdcfg[5]);
1369 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
1370 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +08001371 break;
1372 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Luc6799ce2015-09-17 10:27:48 +08001373 value = QIXIS_READ(pwr_ctl[1]);
1374 value |= QIXIS_EVDD_BY_SDHC_VS;
1375 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +08001376 break;
1377 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
1378 value = QIXIS_READ(brdcfg[5]);
1379 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
1380 QIXIS_WRITE(brdcfg[5], value);
1381 break;
1382 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
1383 break;
1384 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
1385 break;
1386 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
1387 break;
1388 case QIXIS_ESDHC_NO_ADAPTER:
1389 break;
1390 default:
1391 break;
1392 }
1393}
1394#endif
1395
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001396#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +08001397__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001398{
Chenhui Zhao025eab02011-01-04 17:23:05 +08001399#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001400 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +08001401 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +08001402 sizeof("disabled"), 1);
1403 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001404 }
Chenhui Zhao025eab02011-01-04 17:23:05 +08001405#endif
Yangbo Lud84139c2017-01-17 10:43:54 +08001406 return 0;
1407}
1408
1409void fdt_fixup_esdhc(void *blob, bd_t *bd)
1410{
1411 const char *compat = "fsl,esdhc";
1412
1413 if (esdhc_status_fixup(blob, compat))
1414 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001415
Yangbo Lu163beec2015-04-22 13:57:40 +08001416#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
1417 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
1418 gd->arch.sdhc_clk, 1);
1419#else
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001420 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +00001421 gd->arch.sdhc_clk, 1);
Yangbo Lu163beec2015-04-22 13:57:40 +08001422#endif
Yangbo Lub124f8a2015-04-22 13:57:00 +08001423#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1424 do_fixup_by_compat_u32(blob, compat, "adapter-type",
1425 (u32)(gd->arch.sdhc_adapter), 1);
1426#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +04001427}
Stefano Babicff7a5ca2010-02-05 15:11:27 +01001428#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001429
Simon Glass407025d2017-07-29 11:35:24 -06001430#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +08001431#include <asm/arch/clock.h>
Peng Fanaf6dbc02017-02-22 16:21:55 +08001432__weak void init_clk_usdhc(u32 index)
1433{
1434}
1435
Peng Fana4d36f72016-03-25 14:16:56 +08001436static int fsl_esdhc_probe(struct udevice *dev)
1437{
1438 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa02ca52017-07-29 11:35:21 -06001439 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fana4d36f72016-03-25 14:16:56 +08001440 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fanc4142702018-01-21 19:00:24 +08001441 const void *fdt = gd->fdt_blob;
1442 int node = dev_of_offset(dev);
1443 struct esdhc_soc_data *data =
1444 (struct esdhc_soc_data *)dev_get_driver_data(dev);
Abel Vesaf3a7c242019-02-01 16:40:11 +00001445#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan5eb8b432017-06-12 17:50:54 +08001446 struct udevice *vqmmc_dev;
York Sun107a5e42017-08-08 15:45:13 -07001447#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001448 fdt_addr_t addr;
1449 unsigned int val;
Simon Glass407025d2017-07-29 11:35:24 -06001450 struct mmc *mmc;
Peng Fana4d36f72016-03-25 14:16:56 +08001451 int ret;
1452
Simon Glass80e9df42017-07-29 11:35:23 -06001453 addr = dev_read_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +08001454 if (addr == FDT_ADDR_T_NONE)
1455 return -EINVAL;
1456
1457 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1458 priv->dev = dev;
Peng Fanc4142702018-01-21 19:00:24 +08001459 priv->mode = -1;
1460 if (data) {
1461 priv->flags = data->flags;
1462 priv->caps = data->caps;
1463 }
Peng Fana4d36f72016-03-25 14:16:56 +08001464
Simon Glass80e9df42017-07-29 11:35:23 -06001465 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fana4d36f72016-03-25 14:16:56 +08001466 if (val == 8)
1467 priv->bus_width = 8;
1468 else if (val == 4)
1469 priv->bus_width = 4;
1470 else
1471 priv->bus_width = 1;
1472
Peng Fanc4142702018-01-21 19:00:24 +08001473 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1474 priv->tuning_step = val;
1475 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1476 ESDHC_TUNING_START_TAP_DEFAULT);
1477 priv->tuning_start_tap = val;
1478 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1479 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1480 priv->strobe_dll_delay_target = val;
1481
Simon Glass80e9df42017-07-29 11:35:23 -06001482 if (dev_read_bool(dev, "non-removable")) {
Peng Fana4d36f72016-03-25 14:16:56 +08001483 priv->non_removable = 1;
1484 } else {
1485 priv->non_removable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +08001486#ifdef CONFIG_DM_GPIO
Simon Glass80e9df42017-07-29 11:35:23 -06001487 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1488 GPIOD_IS_IN);
Yangbo Lub99647c2016-12-07 11:54:30 +08001489#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001490 }
1491
Peng Fan01eb1c42016-06-15 10:53:02 +08001492 priv->wp_enable = 1;
1493
Yangbo Lub99647c2016-12-07 11:54:30 +08001494#ifdef CONFIG_DM_GPIO
Simon Glass80e9df42017-07-29 11:35:23 -06001495 ret = gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1496 GPIOD_IS_IN);
Peng Fan01eb1c42016-06-15 10:53:02 +08001497 if (ret)
1498 priv->wp_enable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +08001499#endif
Peng Fan5eb8b432017-06-12 17:50:54 +08001500
1501 priv->vs18_enable = 0;
1502
Abel Vesaf3a7c242019-02-01 16:40:11 +00001503#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan5eb8b432017-06-12 17:50:54 +08001504 /*
1505 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1506 * otherwise, emmc will work abnormally.
1507 */
1508 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1509 if (ret) {
1510 dev_dbg(dev, "no vqmmc-supply\n");
1511 } else {
1512 ret = regulator_set_enable(vqmmc_dev, true);
1513 if (ret) {
1514 dev_err(dev, "fail to enable vqmmc-supply\n");
1515 return ret;
1516 }
1517
1518 if (regulator_get_value(vqmmc_dev) == 1800000)
1519 priv->vs18_enable = 1;
1520 }
1521#endif
1522
Peng Fanc4142702018-01-21 19:00:24 +08001523 if (fdt_get_property(fdt, node, "no-1-8-v", NULL))
Peng Fanddd8d752018-08-10 14:07:55 +08001524 priv->caps &= ~(UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_HS400);
Peng Fanc4142702018-01-21 19:00:24 +08001525
Peng Fana4d36f72016-03-25 14:16:56 +08001526 /*
1527 * TODO:
1528 * Because lack of clk driver, if SDHC clk is not enabled,
1529 * need to enable it first before this driver is invoked.
1530 *
1531 * we use MXC_ESDHC_CLK to get clk freq.
1532 * If one would like to make this function work,
1533 * the aliases should be provided in dts as this:
1534 *
1535 * aliases {
1536 * mmc0 = &usdhc1;
1537 * mmc1 = &usdhc2;
1538 * mmc2 = &usdhc3;
1539 * mmc3 = &usdhc4;
1540 * };
1541 * Then if your board only supports mmc2 and mmc3, but we can
1542 * correctly get the seq as 2 and 3, then let mxc_get_clock
1543 * work as expected.
1544 */
Peng Fanaf6dbc02017-02-22 16:21:55 +08001545
1546 init_clk_usdhc(dev->seq);
1547
Peng Fan4c286b72018-10-18 14:28:35 +02001548 if (IS_ENABLED(CONFIG_CLK)) {
1549 /* Assigned clock already set clock */
1550 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1551 if (ret) {
1552 printf("Failed to get per_clk\n");
1553 return ret;
1554 }
1555 ret = clk_enable(&priv->per_clk);
1556 if (ret) {
1557 printf("Failed to enable per_clk\n");
1558 return ret;
1559 }
1560
1561 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1562 } else {
1563 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1564 if (priv->sdhc_clk <= 0) {
1565 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1566 return -EINVAL;
1567 }
Peng Fana4d36f72016-03-25 14:16:56 +08001568 }
1569
Simon Glassfa02ca52017-07-29 11:35:21 -06001570 ret = fsl_esdhc_init(priv, plat);
Peng Fana4d36f72016-03-25 14:16:56 +08001571 if (ret) {
1572 dev_err(dev, "fsl_esdhc_init failure\n");
1573 return ret;
1574 }
1575
Simon Glass407025d2017-07-29 11:35:24 -06001576 mmc = &plat->mmc;
1577 mmc->cfg = &plat->cfg;
1578 mmc->dev = dev;
1579 upriv->mmc = mmc;
Peng Fana4d36f72016-03-25 14:16:56 +08001580
Simon Glass407025d2017-07-29 11:35:24 -06001581 return esdhc_init_common(priv, mmc);
Peng Fana4d36f72016-03-25 14:16:56 +08001582}
1583
Simon Glasseba48f92017-07-29 11:35:31 -06001584#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass407025d2017-07-29 11:35:24 -06001585static int fsl_esdhc_get_cd(struct udevice *dev)
1586{
1587 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1588
Simon Glass407025d2017-07-29 11:35:24 -06001589 return esdhc_getcd_common(priv);
1590}
1591
1592static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1593 struct mmc_data *data)
1594{
1595 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1596 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1597
1598 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1599}
1600
1601static int fsl_esdhc_set_ios(struct udevice *dev)
1602{
1603 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1604 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1605
1606 return esdhc_set_ios_common(priv, &plat->mmc);
1607}
1608
1609static const struct dm_mmc_ops fsl_esdhc_ops = {
1610 .get_cd = fsl_esdhc_get_cd,
1611 .send_cmd = fsl_esdhc_send_cmd,
1612 .set_ios = fsl_esdhc_set_ios,
Peng Fanc4142702018-01-21 19:00:24 +08001613#ifdef MMC_SUPPORTS_TUNING
1614 .execute_tuning = fsl_esdhc_execute_tuning,
1615#endif
Simon Glass407025d2017-07-29 11:35:24 -06001616};
1617#endif
1618
Peng Fanc4142702018-01-21 19:00:24 +08001619static struct esdhc_soc_data usdhc_imx7d_data = {
1620 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1621 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1622 | ESDHC_FLAG_HS400,
1623 .caps = UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_DDR_52MHz |
1624 MMC_MODE_HS_52MHz | MMC_MODE_HS,
1625};
1626
Peng Fana4d36f72016-03-25 14:16:56 +08001627static const struct udevice_id fsl_esdhc_ids[] = {
Patrick Bruenn3fba9612019-01-03 07:54:32 +01001628 { .compatible = "fsl,imx53-esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001629 { .compatible = "fsl,imx6ul-usdhc", },
1630 { .compatible = "fsl,imx6sx-usdhc", },
1631 { .compatible = "fsl,imx6sl-usdhc", },
1632 { .compatible = "fsl,imx6q-usdhc", },
Peng Fanc4142702018-01-21 19:00:24 +08001633 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Peng Fanaf6dbc02017-02-22 16:21:55 +08001634 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lu2a99b602016-12-07 11:54:31 +08001635 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001636 { /* sentinel */ }
1637};
1638
Simon Glass407025d2017-07-29 11:35:24 -06001639#if CONFIG_IS_ENABLED(BLK)
1640static int fsl_esdhc_bind(struct udevice *dev)
1641{
1642 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1643
1644 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1645}
1646#endif
1647
Peng Fana4d36f72016-03-25 14:16:56 +08001648U_BOOT_DRIVER(fsl_esdhc) = {
1649 .name = "fsl-esdhc-mmc",
1650 .id = UCLASS_MMC,
1651 .of_match = fsl_esdhc_ids,
Simon Glass407025d2017-07-29 11:35:24 -06001652 .ops = &fsl_esdhc_ops,
Simon Glass407025d2017-07-29 11:35:24 -06001653#if CONFIG_IS_ENABLED(BLK)
1654 .bind = fsl_esdhc_bind,
1655#endif
Peng Fana4d36f72016-03-25 14:16:56 +08001656 .probe = fsl_esdhc_probe,
Simon Glassfa02ca52017-07-29 11:35:21 -06001657 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fana4d36f72016-03-25 14:16:56 +08001658 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1659};
1660#endif