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