xref: /linux/drivers/mmc/host/sdhci-esdhc-imx.c (revision 140eb5227767c6754742020a16d2691222b9c19b)
1 /*
2  * Freescale eSDHC i.MX controller driver for the platform bus.
3  *
4  * derived from the OF-version.
5  *
6  * Copyright (c) 2010 Pengutronix e.K.
7  *   Author: Wolfram Sang <kernel@pengutronix.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License.
12  */
13 
14 #include <linux/io.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/mmc.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/slot-gpio.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/platform_data/mmc-esdhc-imx.h>
30 #include <linux/pm_runtime.h>
31 #include "sdhci-pltfm.h"
32 #include "sdhci-esdhc.h"
33 
34 #define ESDHC_SYS_CTRL_DTOCV_MASK	0x0f
35 #define	ESDHC_CTRL_D3CD			0x08
36 #define ESDHC_BURST_LEN_EN_INCR		(1 << 27)
37 /* VENDOR SPEC register */
38 #define ESDHC_VENDOR_SPEC		0xc0
39 #define  ESDHC_VENDOR_SPEC_SDIO_QUIRK	(1 << 1)
40 #define  ESDHC_VENDOR_SPEC_VSELECT	(1 << 1)
41 #define  ESDHC_VENDOR_SPEC_FRC_SDCLK_ON	(1 << 8)
42 #define ESDHC_WTMK_LVL			0x44
43 #define  ESDHC_WTMK_DEFAULT_VAL		0x10401040
44 #define ESDHC_MIX_CTRL			0x48
45 #define  ESDHC_MIX_CTRL_DDREN		(1 << 3)
46 #define  ESDHC_MIX_CTRL_AC23EN		(1 << 7)
47 #define  ESDHC_MIX_CTRL_EXE_TUNE	(1 << 22)
48 #define  ESDHC_MIX_CTRL_SMPCLK_SEL	(1 << 23)
49 #define  ESDHC_MIX_CTRL_AUTO_TUNE_EN	(1 << 24)
50 #define  ESDHC_MIX_CTRL_FBCLK_SEL	(1 << 25)
51 #define  ESDHC_MIX_CTRL_HS400_EN	(1 << 26)
52 /* Bits 3 and 6 are not SDHCI standard definitions */
53 #define  ESDHC_MIX_CTRL_SDHCI_MASK	0xb7
54 /* Tuning bits */
55 #define  ESDHC_MIX_CTRL_TUNING_MASK	0x03c00000
56 
57 /* dll control register */
58 #define ESDHC_DLL_CTRL			0x60
59 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT	9
60 #define ESDHC_DLL_OVERRIDE_EN_SHIFT	8
61 
62 /* tune control register */
63 #define ESDHC_TUNE_CTRL_STATUS		0x68
64 #define  ESDHC_TUNE_CTRL_STEP		1
65 #define  ESDHC_TUNE_CTRL_MIN		0
66 #define  ESDHC_TUNE_CTRL_MAX		((1 << 7) - 1)
67 
68 /* strobe dll register */
69 #define ESDHC_STROBE_DLL_CTRL		0x70
70 #define ESDHC_STROBE_DLL_CTRL_ENABLE	(1 << 0)
71 #define ESDHC_STROBE_DLL_CTRL_RESET	(1 << 1)
72 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT	3
73 
74 #define ESDHC_STROBE_DLL_STATUS		0x74
75 #define ESDHC_STROBE_DLL_STS_REF_LOCK	(1 << 1)
76 #define ESDHC_STROBE_DLL_STS_SLV_LOCK	0x1
77 
78 #define ESDHC_TUNING_CTRL		0xcc
79 #define ESDHC_STD_TUNING_EN		(1 << 24)
80 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
81 #define ESDHC_TUNING_START_TAP_DEFAULT	0x1
82 #define ESDHC_TUNING_START_TAP_MASK	0xff
83 #define ESDHC_TUNING_STEP_MASK		0x00070000
84 #define ESDHC_TUNING_STEP_SHIFT		16
85 
86 /* pinctrl state */
87 #define ESDHC_PINCTRL_STATE_100MHZ	"state_100mhz"
88 #define ESDHC_PINCTRL_STATE_200MHZ	"state_200mhz"
89 
90 /*
91  * Our interpretation of the SDHCI_HOST_CONTROL register
92  */
93 #define ESDHC_CTRL_4BITBUS		(0x1 << 1)
94 #define ESDHC_CTRL_8BITBUS		(0x2 << 1)
95 #define ESDHC_CTRL_BUSWIDTH_MASK	(0x3 << 1)
96 
97 /*
98  * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
99  * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
100  * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
101  * Define this macro DMA error INT for fsl eSDHC
102  */
103 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR	(1 << 28)
104 
105 /*
106  * The CMDTYPE of the CMD register (offset 0xE) should be set to
107  * "11" when the STOP CMD12 is issued on imx53 to abort one
108  * open ended multi-blk IO. Otherwise the TC INT wouldn't
109  * be generated.
110  * In exact block transfer, the controller doesn't complete the
111  * operations automatically as required at the end of the
112  * transfer and remains on hold if the abort command is not sent.
113  * As a result, the TC flag is not asserted and SW received timeout
114  * exception. Bit1 of Vendor Spec register is used to fix it.
115  */
116 #define ESDHC_FLAG_MULTIBLK_NO_INT	BIT(1)
117 /*
118  * The flag tells that the ESDHC controller is an USDHC block that is
119  * integrated on the i.MX6 series.
120  */
121 #define ESDHC_FLAG_USDHC		BIT(3)
122 /* The IP supports manual tuning process */
123 #define ESDHC_FLAG_MAN_TUNING		BIT(4)
124 /* The IP supports standard tuning process */
125 #define ESDHC_FLAG_STD_TUNING		BIT(5)
126 /* The IP has SDHCI_CAPABILITIES_1 register */
127 #define ESDHC_FLAG_HAVE_CAP1		BIT(6)
128 /*
129  * The IP has erratum ERR004536
130  * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
131  * when reading data from the card
132  * This flag is also set for i.MX25 and i.MX35 in order to get
133  * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
134  */
135 #define ESDHC_FLAG_ERR004536		BIT(7)
136 /* The IP supports HS200 mode */
137 #define ESDHC_FLAG_HS200		BIT(8)
138 /* The IP supports HS400 mode */
139 #define ESDHC_FLAG_HS400		BIT(9)
140 
141 /* A clock frequency higher than this rate requires strobe dll control */
142 #define ESDHC_STROBE_DLL_CLK_FREQ	100000000
143 
144 struct esdhc_soc_data {
145 	u32 flags;
146 };
147 
148 static struct esdhc_soc_data esdhc_imx25_data = {
149 	.flags = ESDHC_FLAG_ERR004536,
150 };
151 
152 static struct esdhc_soc_data esdhc_imx35_data = {
153 	.flags = ESDHC_FLAG_ERR004536,
154 };
155 
156 static struct esdhc_soc_data esdhc_imx51_data = {
157 	.flags = 0,
158 };
159 
160 static struct esdhc_soc_data esdhc_imx53_data = {
161 	.flags = ESDHC_FLAG_MULTIBLK_NO_INT,
162 };
163 
164 static struct esdhc_soc_data usdhc_imx6q_data = {
165 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
166 };
167 
168 static struct esdhc_soc_data usdhc_imx6sl_data = {
169 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
170 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
171 			| ESDHC_FLAG_HS200,
172 };
173 
174 static struct esdhc_soc_data usdhc_imx6sx_data = {
175 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
176 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200,
177 };
178 
179 static struct esdhc_soc_data usdhc_imx7d_data = {
180 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
181 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
182 			| ESDHC_FLAG_HS400,
183 };
184 
185 struct pltfm_imx_data {
186 	u32 scratchpad;
187 	struct pinctrl *pinctrl;
188 	struct pinctrl_state *pins_default;
189 	struct pinctrl_state *pins_100mhz;
190 	struct pinctrl_state *pins_200mhz;
191 	const struct esdhc_soc_data *socdata;
192 	struct esdhc_platform_data boarddata;
193 	struct clk *clk_ipg;
194 	struct clk *clk_ahb;
195 	struct clk *clk_per;
196 	enum {
197 		NO_CMD_PENDING,      /* no multiblock command pending */
198 		MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
199 		WAIT_FOR_INT,        /* sent CMD12, waiting for response INT */
200 	} multiblock_status;
201 	u32 is_ddr;
202 };
203 
204 static const struct platform_device_id imx_esdhc_devtype[] = {
205 	{
206 		.name = "sdhci-esdhc-imx25",
207 		.driver_data = (kernel_ulong_t) &esdhc_imx25_data,
208 	}, {
209 		.name = "sdhci-esdhc-imx35",
210 		.driver_data = (kernel_ulong_t) &esdhc_imx35_data,
211 	}, {
212 		.name = "sdhci-esdhc-imx51",
213 		.driver_data = (kernel_ulong_t) &esdhc_imx51_data,
214 	}, {
215 		/* sentinel */
216 	}
217 };
218 MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
219 
220 static const struct of_device_id imx_esdhc_dt_ids[] = {
221 	{ .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
222 	{ .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
223 	{ .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
224 	{ .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
225 	{ .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
226 	{ .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
227 	{ .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
228 	{ .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
229 	{ /* sentinel */ }
230 };
231 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
232 
233 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
234 {
235 	return data->socdata == &esdhc_imx25_data;
236 }
237 
238 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
239 {
240 	return data->socdata == &esdhc_imx53_data;
241 }
242 
243 static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
244 {
245 	return data->socdata == &usdhc_imx6q_data;
246 }
247 
248 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
249 {
250 	return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
251 }
252 
253 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
254 {
255 	void __iomem *base = host->ioaddr + (reg & ~0x3);
256 	u32 shift = (reg & 0x3) * 8;
257 
258 	writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
259 }
260 
261 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
262 {
263 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
264 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
265 	u32 val = readl(host->ioaddr + reg);
266 
267 	if (unlikely(reg == SDHCI_PRESENT_STATE)) {
268 		u32 fsl_prss = val;
269 		/* save the least 20 bits */
270 		val = fsl_prss & 0x000FFFFF;
271 		/* move dat[0-3] bits */
272 		val |= (fsl_prss & 0x0F000000) >> 4;
273 		/* move cmd line bit */
274 		val |= (fsl_prss & 0x00800000) << 1;
275 	}
276 
277 	if (unlikely(reg == SDHCI_CAPABILITIES)) {
278 		/* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
279 		if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
280 			val &= 0xffff0000;
281 
282 		/* In FSL esdhc IC module, only bit20 is used to indicate the
283 		 * ADMA2 capability of esdhc, but this bit is messed up on
284 		 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
285 		 * don't actually support ADMA2). So set the BROKEN_ADMA
286 		 * quirk on MX25/35 platforms.
287 		 */
288 
289 		if (val & SDHCI_CAN_DO_ADMA1) {
290 			val &= ~SDHCI_CAN_DO_ADMA1;
291 			val |= SDHCI_CAN_DO_ADMA2;
292 		}
293 	}
294 
295 	if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
296 		if (esdhc_is_usdhc(imx_data)) {
297 			if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
298 				val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
299 			else
300 				/* imx6q/dl does not have cap_1 register, fake one */
301 				val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
302 					| SDHCI_SUPPORT_SDR50
303 					| SDHCI_USE_SDR50_TUNING
304 					| (SDHCI_TUNING_MODE_3 << SDHCI_RETUNING_MODE_SHIFT);
305 
306 			if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
307 				val |= SDHCI_SUPPORT_HS400;
308 		}
309 	}
310 
311 	if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
312 		val = 0;
313 		val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
314 		val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
315 		val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
316 	}
317 
318 	if (unlikely(reg == SDHCI_INT_STATUS)) {
319 		if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
320 			val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
321 			val |= SDHCI_INT_ADMA_ERROR;
322 		}
323 
324 		/*
325 		 * mask off the interrupt we get in response to the manually
326 		 * sent CMD12
327 		 */
328 		if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
329 		    ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
330 			val &= ~SDHCI_INT_RESPONSE;
331 			writel(SDHCI_INT_RESPONSE, host->ioaddr +
332 						   SDHCI_INT_STATUS);
333 			imx_data->multiblock_status = NO_CMD_PENDING;
334 		}
335 	}
336 
337 	return val;
338 }
339 
340 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
341 {
342 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
343 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
344 	u32 data;
345 
346 	if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
347 			reg == SDHCI_INT_STATUS)) {
348 		if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
349 			/*
350 			 * Clear and then set D3CD bit to avoid missing the
351 			 * card interrupt. This is an eSDHC controller problem
352 			 * so we need to apply the following workaround: clear
353 			 * and set D3CD bit will make eSDHC re-sample the card
354 			 * interrupt. In case a card interrupt was lost,
355 			 * re-sample it by the following steps.
356 			 */
357 			data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
358 			data &= ~ESDHC_CTRL_D3CD;
359 			writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
360 			data |= ESDHC_CTRL_D3CD;
361 			writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
362 		}
363 
364 		if (val & SDHCI_INT_ADMA_ERROR) {
365 			val &= ~SDHCI_INT_ADMA_ERROR;
366 			val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
367 		}
368 	}
369 
370 	if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
371 				&& (reg == SDHCI_INT_STATUS)
372 				&& (val & SDHCI_INT_DATA_END))) {
373 			u32 v;
374 			v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
375 			v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
376 			writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
377 
378 			if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
379 			{
380 				/* send a manual CMD12 with RESPTYP=none */
381 				data = MMC_STOP_TRANSMISSION << 24 |
382 				       SDHCI_CMD_ABORTCMD << 16;
383 				writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
384 				imx_data->multiblock_status = WAIT_FOR_INT;
385 			}
386 	}
387 
388 	writel(val, host->ioaddr + reg);
389 }
390 
391 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
392 {
393 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
394 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
395 	u16 ret = 0;
396 	u32 val;
397 
398 	if (unlikely(reg == SDHCI_HOST_VERSION)) {
399 		reg ^= 2;
400 		if (esdhc_is_usdhc(imx_data)) {
401 			/*
402 			 * The usdhc register returns a wrong host version.
403 			 * Correct it here.
404 			 */
405 			return SDHCI_SPEC_300;
406 		}
407 	}
408 
409 	if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
410 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
411 		if (val & ESDHC_VENDOR_SPEC_VSELECT)
412 			ret |= SDHCI_CTRL_VDD_180;
413 
414 		if (esdhc_is_usdhc(imx_data)) {
415 			if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
416 				val = readl(host->ioaddr + ESDHC_MIX_CTRL);
417 			else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
418 				/* the std tuning bits is in ACMD12_ERR for imx6sl */
419 				val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
420 		}
421 
422 		if (val & ESDHC_MIX_CTRL_EXE_TUNE)
423 			ret |= SDHCI_CTRL_EXEC_TUNING;
424 		if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
425 			ret |= SDHCI_CTRL_TUNED_CLK;
426 
427 		ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
428 
429 		return ret;
430 	}
431 
432 	if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
433 		if (esdhc_is_usdhc(imx_data)) {
434 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
435 			ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
436 			/* Swap AC23 bit */
437 			if (m & ESDHC_MIX_CTRL_AC23EN) {
438 				ret &= ~ESDHC_MIX_CTRL_AC23EN;
439 				ret |= SDHCI_TRNS_AUTO_CMD23;
440 			}
441 		} else {
442 			ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
443 		}
444 
445 		return ret;
446 	}
447 
448 	return readw(host->ioaddr + reg);
449 }
450 
451 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
452 {
453 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
454 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
455 	u32 new_val = 0;
456 
457 	switch (reg) {
458 	case SDHCI_CLOCK_CONTROL:
459 		new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
460 		if (val & SDHCI_CLOCK_CARD_EN)
461 			new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
462 		else
463 			new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
464 		writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
465 		return;
466 	case SDHCI_HOST_CONTROL2:
467 		new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
468 		if (val & SDHCI_CTRL_VDD_180)
469 			new_val |= ESDHC_VENDOR_SPEC_VSELECT;
470 		else
471 			new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
472 		writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
473 		if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
474 			new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
475 			if (val & SDHCI_CTRL_TUNED_CLK) {
476 				new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
477 				new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
478 			} else {
479 				new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
480 				new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
481 			}
482 			writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
483 		} else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
484 			u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
485 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
486 			if (val & SDHCI_CTRL_TUNED_CLK) {
487 				v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
488 			} else {
489 				v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
490 				m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
491 				m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
492 			}
493 
494 			if (val & SDHCI_CTRL_EXEC_TUNING) {
495 				v |= ESDHC_MIX_CTRL_EXE_TUNE;
496 				m |= ESDHC_MIX_CTRL_FBCLK_SEL;
497 				m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
498 			} else {
499 				v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
500 			}
501 
502 			writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
503 			writel(m, host->ioaddr + ESDHC_MIX_CTRL);
504 		}
505 		return;
506 	case SDHCI_TRANSFER_MODE:
507 		if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
508 				&& (host->cmd->opcode == SD_IO_RW_EXTENDED)
509 				&& (host->cmd->data->blocks > 1)
510 				&& (host->cmd->data->flags & MMC_DATA_READ)) {
511 			u32 v;
512 			v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
513 			v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
514 			writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
515 		}
516 
517 		if (esdhc_is_usdhc(imx_data)) {
518 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
519 			/* Swap AC23 bit */
520 			if (val & SDHCI_TRNS_AUTO_CMD23) {
521 				val &= ~SDHCI_TRNS_AUTO_CMD23;
522 				val |= ESDHC_MIX_CTRL_AC23EN;
523 			}
524 			m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
525 			writel(m, host->ioaddr + ESDHC_MIX_CTRL);
526 		} else {
527 			/*
528 			 * Postpone this write, we must do it together with a
529 			 * command write that is down below.
530 			 */
531 			imx_data->scratchpad = val;
532 		}
533 		return;
534 	case SDHCI_COMMAND:
535 		if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
536 			val |= SDHCI_CMD_ABORTCMD;
537 
538 		if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
539 		    (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
540 			imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
541 
542 		if (esdhc_is_usdhc(imx_data))
543 			writel(val << 16,
544 			       host->ioaddr + SDHCI_TRANSFER_MODE);
545 		else
546 			writel(val << 16 | imx_data->scratchpad,
547 			       host->ioaddr + SDHCI_TRANSFER_MODE);
548 		return;
549 	case SDHCI_BLOCK_SIZE:
550 		val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
551 		break;
552 	}
553 	esdhc_clrset_le(host, 0xffff, val, reg);
554 }
555 
556 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
557 {
558 	u8 ret;
559 	u32 val;
560 
561 	switch (reg) {
562 	case SDHCI_HOST_CONTROL:
563 		val = readl(host->ioaddr + reg);
564 
565 		ret = val & SDHCI_CTRL_LED;
566 		ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
567 		ret |= (val & ESDHC_CTRL_4BITBUS);
568 		ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
569 		return ret;
570 	}
571 
572 	return readb(host->ioaddr + reg);
573 }
574 
575 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
576 {
577 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
578 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
579 	u32 new_val = 0;
580 	u32 mask;
581 
582 	switch (reg) {
583 	case SDHCI_POWER_CONTROL:
584 		/*
585 		 * FSL put some DMA bits here
586 		 * If your board has a regulator, code should be here
587 		 */
588 		return;
589 	case SDHCI_HOST_CONTROL:
590 		/* FSL messed up here, so we need to manually compose it. */
591 		new_val = val & SDHCI_CTRL_LED;
592 		/* ensure the endianness */
593 		new_val |= ESDHC_HOST_CONTROL_LE;
594 		/* bits 8&9 are reserved on mx25 */
595 		if (!is_imx25_esdhc(imx_data)) {
596 			/* DMA mode bits are shifted */
597 			new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
598 		}
599 
600 		/*
601 		 * Do not touch buswidth bits here. This is done in
602 		 * esdhc_pltfm_bus_width.
603 		 * Do not touch the D3CD bit either which is used for the
604 		 * SDIO interrupt erratum workaround.
605 		 */
606 		mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
607 
608 		esdhc_clrset_le(host, mask, new_val, reg);
609 		return;
610 	case SDHCI_SOFTWARE_RESET:
611 		if (val & SDHCI_RESET_DATA)
612 			new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
613 		break;
614 	}
615 	esdhc_clrset_le(host, 0xff, val, reg);
616 
617 	if (reg == SDHCI_SOFTWARE_RESET) {
618 		if (val & SDHCI_RESET_ALL) {
619 			/*
620 			 * The esdhc has a design violation to SDHC spec which
621 			 * tells that software reset should not affect card
622 			 * detection circuit. But esdhc clears its SYSCTL
623 			 * register bits [0..2] during the software reset. This
624 			 * will stop those clocks that card detection circuit
625 			 * relies on. To work around it, we turn the clocks on
626 			 * back to keep card detection circuit functional.
627 			 */
628 			esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
629 			/*
630 			 * The reset on usdhc fails to clear MIX_CTRL register.
631 			 * Do it manually here.
632 			 */
633 			if (esdhc_is_usdhc(imx_data)) {
634 				/*
635 				 * the tuning bits should be kept during reset
636 				 */
637 				new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
638 				writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
639 						host->ioaddr + ESDHC_MIX_CTRL);
640 				imx_data->is_ddr = 0;
641 			}
642 		} else if (val & SDHCI_RESET_DATA) {
643 			/*
644 			 * The eSDHC DAT line software reset clears at least the
645 			 * data transfer width on i.MX25, so make sure that the
646 			 * Host Control register is unaffected.
647 			 */
648 			esdhc_clrset_le(host, 0xff, new_val,
649 					SDHCI_HOST_CONTROL);
650 		}
651 	}
652 }
653 
654 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
655 {
656 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
657 
658 	return pltfm_host->clock;
659 }
660 
661 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
662 {
663 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
664 
665 	return pltfm_host->clock / 256 / 16;
666 }
667 
668 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
669 					 unsigned int clock)
670 {
671 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
672 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
673 	unsigned int host_clock = pltfm_host->clock;
674 	int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
675 	int pre_div = 1;
676 	int div = 1;
677 	u32 temp, val;
678 
679 	if (clock == 0) {
680 		host->mmc->actual_clock = 0;
681 
682 		if (esdhc_is_usdhc(imx_data)) {
683 			val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
684 			writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
685 					host->ioaddr + ESDHC_VENDOR_SPEC);
686 		}
687 		return;
688 	}
689 
690 	/* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
691 	if (is_imx53_esdhc(imx_data)) {
692 		/*
693 		 * According to the i.MX53 reference manual, if DLLCTRL[10] can
694 		 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
695 		 */
696 		val = readl(host->ioaddr + ESDHC_DLL_CTRL);
697 		writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
698 		temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
699 		writel(val, host->ioaddr + ESDHC_DLL_CTRL);
700 		if (temp & BIT(10))
701 			pre_div = 2;
702 	}
703 
704 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
705 	temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
706 		| ESDHC_CLOCK_MASK);
707 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
708 
709 	while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
710 			pre_div < 256)
711 		pre_div *= 2;
712 
713 	while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
714 		div++;
715 
716 	host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
717 	dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
718 		clock, host->mmc->actual_clock);
719 
720 	pre_div >>= 1;
721 	div--;
722 
723 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
724 	temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
725 		| (div << ESDHC_DIVIDER_SHIFT)
726 		| (pre_div << ESDHC_PREDIV_SHIFT));
727 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
728 
729 	if (esdhc_is_usdhc(imx_data)) {
730 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
731 		writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
732 		host->ioaddr + ESDHC_VENDOR_SPEC);
733 	}
734 
735 	mdelay(1);
736 }
737 
738 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
739 {
740 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
741 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
742 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
743 
744 	switch (boarddata->wp_type) {
745 	case ESDHC_WP_GPIO:
746 		return mmc_gpio_get_ro(host->mmc);
747 	case ESDHC_WP_CONTROLLER:
748 		return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
749 			       SDHCI_WRITE_PROTECT);
750 	case ESDHC_WP_NONE:
751 		break;
752 	}
753 
754 	return -ENOSYS;
755 }
756 
757 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
758 {
759 	u32 ctrl;
760 
761 	switch (width) {
762 	case MMC_BUS_WIDTH_8:
763 		ctrl = ESDHC_CTRL_8BITBUS;
764 		break;
765 	case MMC_BUS_WIDTH_4:
766 		ctrl = ESDHC_CTRL_4BITBUS;
767 		break;
768 	default:
769 		ctrl = 0;
770 		break;
771 	}
772 
773 	esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
774 			SDHCI_HOST_CONTROL);
775 }
776 
777 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
778 {
779 	u32 reg;
780 
781 	/* FIXME: delay a bit for card to be ready for next tuning due to errors */
782 	mdelay(1);
783 
784 	reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
785 	reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
786 			ESDHC_MIX_CTRL_FBCLK_SEL;
787 	writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
788 	writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
789 	dev_dbg(mmc_dev(host->mmc),
790 		"tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
791 			val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
792 }
793 
794 static void esdhc_post_tuning(struct sdhci_host *host)
795 {
796 	u32 reg;
797 
798 	reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
799 	reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
800 	reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
801 	writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
802 }
803 
804 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
805 {
806 	int min, max, avg, ret;
807 
808 	/* find the mininum delay first which can pass tuning */
809 	min = ESDHC_TUNE_CTRL_MIN;
810 	while (min < ESDHC_TUNE_CTRL_MAX) {
811 		esdhc_prepare_tuning(host, min);
812 		if (!mmc_send_tuning(host->mmc, opcode, NULL))
813 			break;
814 		min += ESDHC_TUNE_CTRL_STEP;
815 	}
816 
817 	/* find the maxinum delay which can not pass tuning */
818 	max = min + ESDHC_TUNE_CTRL_STEP;
819 	while (max < ESDHC_TUNE_CTRL_MAX) {
820 		esdhc_prepare_tuning(host, max);
821 		if (mmc_send_tuning(host->mmc, opcode, NULL)) {
822 			max -= ESDHC_TUNE_CTRL_STEP;
823 			break;
824 		}
825 		max += ESDHC_TUNE_CTRL_STEP;
826 	}
827 
828 	/* use average delay to get the best timing */
829 	avg = (min + max) / 2;
830 	esdhc_prepare_tuning(host, avg);
831 	ret = mmc_send_tuning(host->mmc, opcode, NULL);
832 	esdhc_post_tuning(host);
833 
834 	dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
835 		ret ? "failed" : "passed", avg, ret);
836 
837 	return ret;
838 }
839 
840 static int esdhc_change_pinstate(struct sdhci_host *host,
841 						unsigned int uhs)
842 {
843 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
844 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
845 	struct pinctrl_state *pinctrl;
846 
847 	dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
848 
849 	if (IS_ERR(imx_data->pinctrl) ||
850 		IS_ERR(imx_data->pins_default) ||
851 		IS_ERR(imx_data->pins_100mhz) ||
852 		IS_ERR(imx_data->pins_200mhz))
853 		return -EINVAL;
854 
855 	switch (uhs) {
856 	case MMC_TIMING_UHS_SDR50:
857 	case MMC_TIMING_UHS_DDR50:
858 		pinctrl = imx_data->pins_100mhz;
859 		break;
860 	case MMC_TIMING_UHS_SDR104:
861 	case MMC_TIMING_MMC_HS200:
862 	case MMC_TIMING_MMC_HS400:
863 		pinctrl = imx_data->pins_200mhz;
864 		break;
865 	default:
866 		/* back to default state for other legacy timing */
867 		pinctrl = imx_data->pins_default;
868 	}
869 
870 	return pinctrl_select_state(imx_data->pinctrl, pinctrl);
871 }
872 
873 /*
874  * For HS400 eMMC, there is a data_strobe line. This signal is generated
875  * by the device and used for data output and CRC status response output
876  * in HS400 mode. The frequency of this signal follows the frequency of
877  * CLK generated by host. The host receives the data which is aligned to the
878  * edge of data_strobe line. Due to the time delay between CLK line and
879  * data_strobe line, if the delay time is larger than one clock cycle,
880  * then CLK and data_strobe line will be misaligned, read error shows up.
881  * So when the CLK is higher than 100MHz, each clock cycle is short enough,
882  * host should configure the delay target.
883  */
884 static void esdhc_set_strobe_dll(struct sdhci_host *host)
885 {
886 	u32 v;
887 
888 	if (host->mmc->actual_clock > ESDHC_STROBE_DLL_CLK_FREQ) {
889 		/* disable clock before enabling strobe dll */
890 		writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
891 		       ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
892 		       host->ioaddr + ESDHC_VENDOR_SPEC);
893 
894 		/* force a reset on strobe dll */
895 		writel(ESDHC_STROBE_DLL_CTRL_RESET,
896 			host->ioaddr + ESDHC_STROBE_DLL_CTRL);
897 		/*
898 		 * enable strobe dll ctrl and adjust the delay target
899 		 * for the uSDHC loopback read clock
900 		 */
901 		v = ESDHC_STROBE_DLL_CTRL_ENABLE |
902 			(7 << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
903 		writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
904 		/* wait 1us to make sure strobe dll status register stable */
905 		udelay(1);
906 		v = readl(host->ioaddr + ESDHC_STROBE_DLL_STATUS);
907 		if (!(v & ESDHC_STROBE_DLL_STS_REF_LOCK))
908 			dev_warn(mmc_dev(host->mmc),
909 				"warning! HS400 strobe DLL status REF not lock!\n");
910 		if (!(v & ESDHC_STROBE_DLL_STS_SLV_LOCK))
911 			dev_warn(mmc_dev(host->mmc),
912 				"warning! HS400 strobe DLL status SLV not lock!\n");
913 	}
914 }
915 
916 static void esdhc_reset_tuning(struct sdhci_host *host)
917 {
918 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
919 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
920 	u32 ctrl;
921 
922 	/* Reset the tuning circuit */
923 	if (esdhc_is_usdhc(imx_data)) {
924 		if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
925 			ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
926 			ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
927 			ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
928 			writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
929 			writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
930 		} else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
931 			ctrl = readl(host->ioaddr + SDHCI_ACMD12_ERR);
932 			ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
933 			writel(ctrl, host->ioaddr + SDHCI_ACMD12_ERR);
934 		}
935 	}
936 }
937 
938 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
939 {
940 	u32 m;
941 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
942 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
943 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
944 
945 	/* disable ddr mode and disable HS400 mode */
946 	m = readl(host->ioaddr + ESDHC_MIX_CTRL);
947 	m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
948 	imx_data->is_ddr = 0;
949 
950 	switch (timing) {
951 	case MMC_TIMING_UHS_SDR12:
952 	case MMC_TIMING_UHS_SDR25:
953 	case MMC_TIMING_UHS_SDR50:
954 	case MMC_TIMING_UHS_SDR104:
955 	case MMC_TIMING_MMC_HS200:
956 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
957 		break;
958 	case MMC_TIMING_UHS_DDR50:
959 	case MMC_TIMING_MMC_DDR52:
960 		m |= ESDHC_MIX_CTRL_DDREN;
961 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
962 		imx_data->is_ddr = 1;
963 		if (boarddata->delay_line) {
964 			u32 v;
965 			v = boarddata->delay_line <<
966 				ESDHC_DLL_OVERRIDE_VAL_SHIFT |
967 				(1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
968 			if (is_imx53_esdhc(imx_data))
969 				v <<= 1;
970 			writel(v, host->ioaddr + ESDHC_DLL_CTRL);
971 		}
972 		break;
973 	case MMC_TIMING_MMC_HS400:
974 		m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
975 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
976 		imx_data->is_ddr = 1;
977 		/* update clock after enable DDR for strobe DLL lock */
978 		host->ops->set_clock(host, host->clock);
979 		esdhc_set_strobe_dll(host);
980 		break;
981 	case MMC_TIMING_LEGACY:
982 	default:
983 		esdhc_reset_tuning(host);
984 		break;
985 	}
986 
987 	esdhc_change_pinstate(host, timing);
988 }
989 
990 static void esdhc_reset(struct sdhci_host *host, u8 mask)
991 {
992 	sdhci_reset(host, mask);
993 
994 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
995 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
996 }
997 
998 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
999 {
1000 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1001 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1002 
1003 	/* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1004 	return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1005 }
1006 
1007 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1008 {
1009 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1010 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1011 
1012 	/* use maximum timeout counter */
1013 	esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1014 			esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1015 			SDHCI_TIMEOUT_CONTROL);
1016 }
1017 
1018 static struct sdhci_ops sdhci_esdhc_ops = {
1019 	.read_l = esdhc_readl_le,
1020 	.read_w = esdhc_readw_le,
1021 	.read_b = esdhc_readb_le,
1022 	.write_l = esdhc_writel_le,
1023 	.write_w = esdhc_writew_le,
1024 	.write_b = esdhc_writeb_le,
1025 	.set_clock = esdhc_pltfm_set_clock,
1026 	.get_max_clock = esdhc_pltfm_get_max_clock,
1027 	.get_min_clock = esdhc_pltfm_get_min_clock,
1028 	.get_max_timeout_count = esdhc_get_max_timeout_count,
1029 	.get_ro = esdhc_pltfm_get_ro,
1030 	.set_timeout = esdhc_set_timeout,
1031 	.set_bus_width = esdhc_pltfm_set_bus_width,
1032 	.set_uhs_signaling = esdhc_set_uhs_signaling,
1033 	.reset = esdhc_reset,
1034 };
1035 
1036 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1037 	.quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1038 			| SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1039 			| SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1040 			| SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1041 	.ops = &sdhci_esdhc_ops,
1042 };
1043 
1044 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1045 {
1046 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1047 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1048 	int tmp;
1049 
1050 	if (esdhc_is_usdhc(imx_data)) {
1051 		/*
1052 		 * The imx6q ROM code will change the default watermark
1053 		 * level setting to something insane.  Change it back here.
1054 		 */
1055 		writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1056 
1057 		/*
1058 		 * ROM code will change the bit burst_length_enable setting
1059 		 * to zero if this usdhc is chosen to boot system. Change
1060 		 * it back here, otherwise it will impact the performance a
1061 		 * lot. This bit is used to enable/disable the burst length
1062 		 * for the external AHB2AXI bridge. It's useful especially
1063 		 * for INCR transfer because without burst length indicator,
1064 		 * the AHB2AXI bridge does not know the burst length in
1065 		 * advance. And without burst length indicator, AHB INCR
1066 		 * transfer can only be converted to singles on the AXI side.
1067 		 */
1068 		writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1069 			| ESDHC_BURST_LEN_EN_INCR,
1070 			host->ioaddr + SDHCI_HOST_CONTROL);
1071 		/*
1072 		* erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1073 		* TO1.1, it's harmless for MX6SL
1074 		*/
1075 		writel(readl(host->ioaddr + 0x6c) | BIT(7),
1076 			host->ioaddr + 0x6c);
1077 
1078 		/* disable DLL_CTRL delay line settings */
1079 		writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1080 
1081 		if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1082 			tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1083 			tmp |= ESDHC_STD_TUNING_EN |
1084 				ESDHC_TUNING_START_TAP_DEFAULT;
1085 			if (imx_data->boarddata.tuning_start_tap) {
1086 				tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1087 				tmp |= imx_data->boarddata.tuning_start_tap;
1088 			}
1089 
1090 			if (imx_data->boarddata.tuning_step) {
1091 				tmp &= ~ESDHC_TUNING_STEP_MASK;
1092 				tmp |= imx_data->boarddata.tuning_step
1093 					<< ESDHC_TUNING_STEP_SHIFT;
1094 			}
1095 			writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1096 		}
1097 	}
1098 }
1099 
1100 #ifdef CONFIG_OF
1101 static int
1102 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1103 			 struct sdhci_host *host,
1104 			 struct pltfm_imx_data *imx_data)
1105 {
1106 	struct device_node *np = pdev->dev.of_node;
1107 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1108 	int ret;
1109 
1110 	if (of_get_property(np, "fsl,wp-controller", NULL))
1111 		boarddata->wp_type = ESDHC_WP_CONTROLLER;
1112 
1113 	boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1114 	if (gpio_is_valid(boarddata->wp_gpio))
1115 		boarddata->wp_type = ESDHC_WP_GPIO;
1116 
1117 	of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1118 	of_property_read_u32(np, "fsl,tuning-start-tap",
1119 			     &boarddata->tuning_start_tap);
1120 
1121 	if (of_find_property(np, "no-1-8-v", NULL))
1122 		boarddata->support_vsel = false;
1123 	else
1124 		boarddata->support_vsel = true;
1125 
1126 	if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1127 		boarddata->delay_line = 0;
1128 
1129 	mmc_of_parse_voltage(np, &host->ocr_mask);
1130 
1131 	/* sdr50 and sdr104 need work on 1.8v signal voltage */
1132 	if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data) &&
1133 	    !IS_ERR(imx_data->pins_default)) {
1134 		imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1135 						ESDHC_PINCTRL_STATE_100MHZ);
1136 		imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1137 						ESDHC_PINCTRL_STATE_200MHZ);
1138 		if (IS_ERR(imx_data->pins_100mhz) ||
1139 				IS_ERR(imx_data->pins_200mhz)) {
1140 			dev_warn(mmc_dev(host->mmc),
1141 				"could not get ultra high speed state, work on normal mode\n");
1142 			/*
1143 			 * fall back to not supporting uhs by specifying no
1144 			 * 1.8v quirk
1145 			 */
1146 			host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1147 		}
1148 	} else {
1149 		host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1150 	}
1151 
1152 	/* call to generic mmc_of_parse to support additional capabilities */
1153 	ret = mmc_of_parse(host->mmc);
1154 	if (ret)
1155 		return ret;
1156 
1157 	if (mmc_gpio_get_cd(host->mmc) >= 0)
1158 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1159 
1160 	return 0;
1161 }
1162 #else
1163 static inline int
1164 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1165 			 struct sdhci_host *host,
1166 			 struct pltfm_imx_data *imx_data)
1167 {
1168 	return -ENODEV;
1169 }
1170 #endif
1171 
1172 static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
1173 			 struct sdhci_host *host,
1174 			 struct pltfm_imx_data *imx_data)
1175 {
1176 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1177 	int err;
1178 
1179 	if (!host->mmc->parent->platform_data) {
1180 		dev_err(mmc_dev(host->mmc), "no board data!\n");
1181 		return -EINVAL;
1182 	}
1183 
1184 	imx_data->boarddata = *((struct esdhc_platform_data *)
1185 				host->mmc->parent->platform_data);
1186 	/* write_protect */
1187 	if (boarddata->wp_type == ESDHC_WP_GPIO) {
1188 		err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
1189 		if (err) {
1190 			dev_err(mmc_dev(host->mmc),
1191 				"failed to request write-protect gpio!\n");
1192 			return err;
1193 		}
1194 		host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1195 	}
1196 
1197 	/* card_detect */
1198 	switch (boarddata->cd_type) {
1199 	case ESDHC_CD_GPIO:
1200 		err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
1201 		if (err) {
1202 			dev_err(mmc_dev(host->mmc),
1203 				"failed to request card-detect gpio!\n");
1204 			return err;
1205 		}
1206 		/* fall through */
1207 
1208 	case ESDHC_CD_CONTROLLER:
1209 		/* we have a working card_detect back */
1210 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1211 		break;
1212 
1213 	case ESDHC_CD_PERMANENT:
1214 		host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1215 		break;
1216 
1217 	case ESDHC_CD_NONE:
1218 		break;
1219 	}
1220 
1221 	switch (boarddata->max_bus_width) {
1222 	case 8:
1223 		host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1224 		break;
1225 	case 4:
1226 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1227 		break;
1228 	case 1:
1229 	default:
1230 		host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1231 		break;
1232 	}
1233 
1234 	return 0;
1235 }
1236 
1237 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1238 {
1239 	const struct of_device_id *of_id =
1240 			of_match_device(imx_esdhc_dt_ids, &pdev->dev);
1241 	struct sdhci_pltfm_host *pltfm_host;
1242 	struct sdhci_host *host;
1243 	int err;
1244 	struct pltfm_imx_data *imx_data;
1245 
1246 	host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1247 				sizeof(*imx_data));
1248 	if (IS_ERR(host))
1249 		return PTR_ERR(host);
1250 
1251 	pltfm_host = sdhci_priv(host);
1252 
1253 	imx_data = sdhci_pltfm_priv(pltfm_host);
1254 
1255 	imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
1256 						  pdev->id_entry->driver_data;
1257 
1258 	imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1259 	if (IS_ERR(imx_data->clk_ipg)) {
1260 		err = PTR_ERR(imx_data->clk_ipg);
1261 		goto free_sdhci;
1262 	}
1263 
1264 	imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1265 	if (IS_ERR(imx_data->clk_ahb)) {
1266 		err = PTR_ERR(imx_data->clk_ahb);
1267 		goto free_sdhci;
1268 	}
1269 
1270 	imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1271 	if (IS_ERR(imx_data->clk_per)) {
1272 		err = PTR_ERR(imx_data->clk_per);
1273 		goto free_sdhci;
1274 	}
1275 
1276 	pltfm_host->clk = imx_data->clk_per;
1277 	pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1278 	err = clk_prepare_enable(imx_data->clk_per);
1279 	if (err)
1280 		goto free_sdhci;
1281 	err = clk_prepare_enable(imx_data->clk_ipg);
1282 	if (err)
1283 		goto disable_per_clk;
1284 	err = clk_prepare_enable(imx_data->clk_ahb);
1285 	if (err)
1286 		goto disable_ipg_clk;
1287 
1288 	imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1289 	if (IS_ERR(imx_data->pinctrl)) {
1290 		err = PTR_ERR(imx_data->pinctrl);
1291 		goto disable_ahb_clk;
1292 	}
1293 
1294 	imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
1295 						PINCTRL_STATE_DEFAULT);
1296 	if (IS_ERR(imx_data->pins_default))
1297 		dev_warn(mmc_dev(host->mmc), "could not get default state\n");
1298 
1299 	if (esdhc_is_usdhc(imx_data)) {
1300 		host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1301 		host->mmc->caps |= MMC_CAP_1_8V_DDR;
1302 		if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1303 			host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1304 
1305 		/* clear tuning bits in case ROM has set it already */
1306 		writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1307 		writel(0x0, host->ioaddr + SDHCI_ACMD12_ERR);
1308 		writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1309 	}
1310 
1311 	if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1312 		sdhci_esdhc_ops.platform_execute_tuning =
1313 					esdhc_executing_tuning;
1314 
1315 	if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1316 		host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1317 
1318 	if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1319 		host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
1320 
1321 	if (of_id)
1322 		err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1323 	else
1324 		err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
1325 	if (err)
1326 		goto disable_ahb_clk;
1327 
1328 	sdhci_esdhc_imx_hwinit(host);
1329 
1330 	err = sdhci_add_host(host);
1331 	if (err)
1332 		goto disable_ahb_clk;
1333 
1334 	pm_runtime_set_active(&pdev->dev);
1335 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1336 	pm_runtime_use_autosuspend(&pdev->dev);
1337 	pm_suspend_ignore_children(&pdev->dev, 1);
1338 	pm_runtime_enable(&pdev->dev);
1339 
1340 	return 0;
1341 
1342 disable_ahb_clk:
1343 	clk_disable_unprepare(imx_data->clk_ahb);
1344 disable_ipg_clk:
1345 	clk_disable_unprepare(imx_data->clk_ipg);
1346 disable_per_clk:
1347 	clk_disable_unprepare(imx_data->clk_per);
1348 free_sdhci:
1349 	sdhci_pltfm_free(pdev);
1350 	return err;
1351 }
1352 
1353 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1354 {
1355 	struct sdhci_host *host = platform_get_drvdata(pdev);
1356 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1357 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1358 	int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1359 
1360 	pm_runtime_get_sync(&pdev->dev);
1361 	pm_runtime_disable(&pdev->dev);
1362 	pm_runtime_put_noidle(&pdev->dev);
1363 
1364 	sdhci_remove_host(host, dead);
1365 
1366 	clk_disable_unprepare(imx_data->clk_per);
1367 	clk_disable_unprepare(imx_data->clk_ipg);
1368 	clk_disable_unprepare(imx_data->clk_ahb);
1369 
1370 	sdhci_pltfm_free(pdev);
1371 
1372 	return 0;
1373 }
1374 
1375 #ifdef CONFIG_PM_SLEEP
1376 static int sdhci_esdhc_suspend(struct device *dev)
1377 {
1378 	struct sdhci_host *host = dev_get_drvdata(dev);
1379 
1380 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1381 		mmc_retune_needed(host->mmc);
1382 
1383 	return sdhci_suspend_host(host);
1384 }
1385 
1386 static int sdhci_esdhc_resume(struct device *dev)
1387 {
1388 	struct sdhci_host *host = dev_get_drvdata(dev);
1389 
1390 	/* re-initialize hw state in case it's lost in low power mode */
1391 	sdhci_esdhc_imx_hwinit(host);
1392 
1393 	return sdhci_resume_host(host);
1394 }
1395 #endif
1396 
1397 #ifdef CONFIG_PM
1398 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1399 {
1400 	struct sdhci_host *host = dev_get_drvdata(dev);
1401 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1402 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1403 	int ret;
1404 
1405 	ret = sdhci_runtime_suspend_host(host);
1406 
1407 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1408 		mmc_retune_needed(host->mmc);
1409 
1410 	if (!sdhci_sdio_irq_enabled(host)) {
1411 		clk_disable_unprepare(imx_data->clk_per);
1412 		clk_disable_unprepare(imx_data->clk_ipg);
1413 	}
1414 	clk_disable_unprepare(imx_data->clk_ahb);
1415 
1416 	return ret;
1417 }
1418 
1419 static int sdhci_esdhc_runtime_resume(struct device *dev)
1420 {
1421 	struct sdhci_host *host = dev_get_drvdata(dev);
1422 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1423 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1424 	int err;
1425 
1426 	if (!sdhci_sdio_irq_enabled(host)) {
1427 		err = clk_prepare_enable(imx_data->clk_per);
1428 		if (err)
1429 			return err;
1430 		err = clk_prepare_enable(imx_data->clk_ipg);
1431 		if (err)
1432 			goto disable_per_clk;
1433 	}
1434 	err = clk_prepare_enable(imx_data->clk_ahb);
1435 	if (err)
1436 		goto disable_ipg_clk;
1437 	err = sdhci_runtime_resume_host(host);
1438 	if (err)
1439 		goto disable_ahb_clk;
1440 
1441 	return 0;
1442 
1443 disable_ahb_clk:
1444 	clk_disable_unprepare(imx_data->clk_ahb);
1445 disable_ipg_clk:
1446 	if (!sdhci_sdio_irq_enabled(host))
1447 		clk_disable_unprepare(imx_data->clk_ipg);
1448 disable_per_clk:
1449 	if (!sdhci_sdio_irq_enabled(host))
1450 		clk_disable_unprepare(imx_data->clk_per);
1451 	return err;
1452 }
1453 #endif
1454 
1455 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1456 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1457 	SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1458 				sdhci_esdhc_runtime_resume, NULL)
1459 };
1460 
1461 static struct platform_driver sdhci_esdhc_imx_driver = {
1462 	.driver		= {
1463 		.name	= "sdhci-esdhc-imx",
1464 		.of_match_table = imx_esdhc_dt_ids,
1465 		.pm	= &sdhci_esdhc_pmops,
1466 	},
1467 	.id_table	= imx_esdhc_devtype,
1468 	.probe		= sdhci_esdhc_imx_probe,
1469 	.remove		= sdhci_esdhc_imx_remove,
1470 };
1471 
1472 module_platform_driver(sdhci_esdhc_imx_driver);
1473 
1474 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1475 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
1476 MODULE_LICENSE("GPL v2");
1477