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