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