xref: /linux/drivers/mmc/host/sdhci-esdhc-imx.c (revision f581e9093aa2820c315273fc0cbf7cb0294fd005)
1a6e7e407SFabio Estevam // SPDX-License-Identifier: GPL-2.0
295f25efeSWolfram Sang /*
395f25efeSWolfram Sang  * Freescale eSDHC i.MX controller driver for the platform bus.
495f25efeSWolfram Sang  *
595f25efeSWolfram Sang  * derived from the OF-version.
695f25efeSWolfram Sang  *
795f25efeSWolfram Sang  * Copyright (c) 2010 Pengutronix e.K.
8035ff831SWolfram Sang  *   Author: Wolfram Sang <kernel@pengutronix.de>
995f25efeSWolfram Sang  */
1095f25efeSWolfram Sang 
1195f25efeSWolfram Sang #include <linux/io.h>
12*f581e909SHaibo Chen #include <linux/iopoll.h>
1395f25efeSWolfram Sang #include <linux/delay.h>
1495f25efeSWolfram Sang #include <linux/err.h>
1595f25efeSWolfram Sang #include <linux/clk.h>
1666506f76SShawn Guo #include <linux/module.h>
17e149860dSRichard Zhu #include <linux/slab.h>
181c4989b0SBOUGH CHEN #include <linux/pm_qos.h>
1995f25efeSWolfram Sang #include <linux/mmc/host.h>
2058ac8177SRichard Zhu #include <linux/mmc/mmc.h>
2158ac8177SRichard Zhu #include <linux/mmc/sdio.h>
22fbe5fdd1SShawn Guo #include <linux/mmc/slot-gpio.h>
23abfafc2dSShawn Guo #include <linux/of.h>
24abfafc2dSShawn Guo #include <linux/of_device.h>
25e62d8b8fSDong Aisheng #include <linux/pinctrl/consumer.h>
2682906b13SArnd Bergmann #include <linux/platform_data/mmc-esdhc-imx.h>
2789d7e5c1SDong Aisheng #include <linux/pm_runtime.h>
2895f25efeSWolfram Sang #include "sdhci-pltfm.h"
2995f25efeSWolfram Sang #include "sdhci-esdhc.h"
30bb6e3581SBOUGH CHEN #include "cqhci.h"
3195f25efeSWolfram Sang 
32a215186dSHaibo Chen #define ESDHC_SYS_CTRL_DTOCV_MASK	0x0f
3360bf6396SShawn Guo #define	ESDHC_CTRL_D3CD			0x08
34fd44954eSHaibo Chen #define ESDHC_BURST_LEN_EN_INCR		(1 << 27)
3558ac8177SRichard Zhu /* VENDOR SPEC register */
3660bf6396SShawn Guo #define ESDHC_VENDOR_SPEC		0xc0
3760bf6396SShawn Guo #define  ESDHC_VENDOR_SPEC_SDIO_QUIRK	(1 << 1)
380322191eSDong Aisheng #define  ESDHC_VENDOR_SPEC_VSELECT	(1 << 1)
39fed2f6e2SDong Aisheng #define  ESDHC_VENDOR_SPEC_FRC_SDCLK_ON	(1 << 8)
4060bf6396SShawn Guo #define ESDHC_WTMK_LVL			0x44
41cc17e129SDong Aisheng #define  ESDHC_WTMK_DEFAULT_VAL		0x10401040
423fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_RD_WML_MASK	0x000000FF
433fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_RD_WML_SHIFT	0
443fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WR_WML_MASK	0x00FF0000
453fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WR_WML_SHIFT	16
463fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WML_VAL_DEF	64
473fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WML_VAL_MAX	128
4860bf6396SShawn Guo #define ESDHC_MIX_CTRL			0x48
49de5bdbffSDong Aisheng #define  ESDHC_MIX_CTRL_DDREN		(1 << 3)
502a15f981SShawn Guo #define  ESDHC_MIX_CTRL_AC23EN		(1 << 7)
510322191eSDong Aisheng #define  ESDHC_MIX_CTRL_EXE_TUNE	(1 << 22)
520322191eSDong Aisheng #define  ESDHC_MIX_CTRL_SMPCLK_SEL	(1 << 23)
530b330e38SDong Aisheng #define  ESDHC_MIX_CTRL_AUTO_TUNE_EN	(1 << 24)
540322191eSDong Aisheng #define  ESDHC_MIX_CTRL_FBCLK_SEL	(1 << 25)
5528b07674SHaibo Chen #define  ESDHC_MIX_CTRL_HS400_EN	(1 << 26)
56029e2476SBOUGH CHEN #define  ESDHC_MIX_CTRL_HS400_ES_EN	(1 << 27)
572a15f981SShawn Guo /* Bits 3 and 6 are not SDHCI standard definitions */
582a15f981SShawn Guo #define  ESDHC_MIX_CTRL_SDHCI_MASK	0xb7
59d131a71cSDong Aisheng /* Tuning bits */
60d131a71cSDong Aisheng #define  ESDHC_MIX_CTRL_TUNING_MASK	0x03c00000
6158ac8177SRichard Zhu 
62602519b2SDong Aisheng /* dll control register */
63602519b2SDong Aisheng #define ESDHC_DLL_CTRL			0x60
64602519b2SDong Aisheng #define ESDHC_DLL_OVERRIDE_VAL_SHIFT	9
65602519b2SDong Aisheng #define ESDHC_DLL_OVERRIDE_EN_SHIFT	8
66602519b2SDong Aisheng 
670322191eSDong Aisheng /* tune control register */
680322191eSDong Aisheng #define ESDHC_TUNE_CTRL_STATUS		0x68
690322191eSDong Aisheng #define  ESDHC_TUNE_CTRL_STEP		1
700322191eSDong Aisheng #define  ESDHC_TUNE_CTRL_MIN		0
710322191eSDong Aisheng #define  ESDHC_TUNE_CTRL_MAX		((1 << 7) - 1)
720322191eSDong Aisheng 
7328b07674SHaibo Chen /* strobe dll register */
7428b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL		0x70
7528b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL_ENABLE	(1 << 0)
7628b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL_RESET	(1 << 1)
775bd2acdcSHaibo Chen #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT	0x7
7828b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT	3
792eaf5a53SBOUGH CHEN #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT	(4 << 20)
8028b07674SHaibo Chen 
8128b07674SHaibo Chen #define ESDHC_STROBE_DLL_STATUS		0x74
8228b07674SHaibo Chen #define ESDHC_STROBE_DLL_STS_REF_LOCK	(1 << 1)
8328b07674SHaibo Chen #define ESDHC_STROBE_DLL_STS_SLV_LOCK	0x1
8428b07674SHaibo Chen 
85bcdb5301SBOUGH CHEN #define ESDHC_VEND_SPEC2		0xc8
86bcdb5301SBOUGH CHEN #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ	(1 << 8)
87bcdb5301SBOUGH CHEN 
886e9fd28eSDong Aisheng #define ESDHC_TUNING_CTRL		0xcc
896e9fd28eSDong Aisheng #define ESDHC_STD_TUNING_EN		(1 << 24)
906e9fd28eSDong Aisheng /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
91d87fc966SDong Aisheng #define ESDHC_TUNING_START_TAP_DEFAULT	0x1
92d87fc966SDong Aisheng #define ESDHC_TUNING_START_TAP_MASK	0xff
93260ecb3cSHaibo Chen #define ESDHC_TUNING_STEP_MASK		0x00070000
94d407e30bSHaibo Chen #define ESDHC_TUNING_STEP_SHIFT		16
956e9fd28eSDong Aisheng 
96ad93220dSDong Aisheng /* pinctrl state */
97ad93220dSDong Aisheng #define ESDHC_PINCTRL_STATE_100MHZ	"state_100mhz"
98ad93220dSDong Aisheng #define ESDHC_PINCTRL_STATE_200MHZ	"state_200mhz"
99ad93220dSDong Aisheng 
10058ac8177SRichard Zhu /*
101af51079eSSascha Hauer  * Our interpretation of the SDHCI_HOST_CONTROL register
102af51079eSSascha Hauer  */
103af51079eSSascha Hauer #define ESDHC_CTRL_4BITBUS		(0x1 << 1)
104af51079eSSascha Hauer #define ESDHC_CTRL_8BITBUS		(0x2 << 1)
105af51079eSSascha Hauer #define ESDHC_CTRL_BUSWIDTH_MASK	(0x3 << 1)
106af51079eSSascha Hauer 
107af51079eSSascha Hauer /*
108d04f8d5bSBenoît Thébaudeau  * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
10997e4ba6aSRichard Zhu  * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
11097e4ba6aSRichard Zhu  * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
11197e4ba6aSRichard Zhu  * Define this macro DMA error INT for fsl eSDHC
11297e4ba6aSRichard Zhu  */
11360bf6396SShawn Guo #define ESDHC_INT_VENDOR_SPEC_DMA_ERR	(1 << 28)
11497e4ba6aSRichard Zhu 
115bb6e3581SBOUGH CHEN /* the address offset of CQHCI */
116bb6e3581SBOUGH CHEN #define ESDHC_CQHCI_ADDR_OFFSET		0x100
117bb6e3581SBOUGH CHEN 
11897e4ba6aSRichard Zhu /*
11958ac8177SRichard Zhu  * The CMDTYPE of the CMD register (offset 0xE) should be set to
12058ac8177SRichard Zhu  * "11" when the STOP CMD12 is issued on imx53 to abort one
12158ac8177SRichard Zhu  * open ended multi-blk IO. Otherwise the TC INT wouldn't
12258ac8177SRichard Zhu  * be generated.
12358ac8177SRichard Zhu  * In exact block transfer, the controller doesn't complete the
12458ac8177SRichard Zhu  * operations automatically as required at the end of the
12558ac8177SRichard Zhu  * transfer and remains on hold if the abort command is not sent.
12658ac8177SRichard Zhu  * As a result, the TC flag is not asserted and SW received timeout
127d04f8d5bSBenoît Thébaudeau  * exception. Bit1 of Vendor Spec register is used to fix it.
12858ac8177SRichard Zhu  */
12931fbb301SShawn Guo #define ESDHC_FLAG_MULTIBLK_NO_INT	BIT(1)
13031fbb301SShawn Guo /*
1319d61c009SShawn Guo  * The flag tells that the ESDHC controller is an USDHC block that is
1329d61c009SShawn Guo  * integrated on the i.MX6 series.
1339d61c009SShawn Guo  */
1349d61c009SShawn Guo #define ESDHC_FLAG_USDHC		BIT(3)
1356e9fd28eSDong Aisheng /* The IP supports manual tuning process */
1366e9fd28eSDong Aisheng #define ESDHC_FLAG_MAN_TUNING		BIT(4)
1376e9fd28eSDong Aisheng /* The IP supports standard tuning process */
1386e9fd28eSDong Aisheng #define ESDHC_FLAG_STD_TUNING		BIT(5)
1396e9fd28eSDong Aisheng /* The IP has SDHCI_CAPABILITIES_1 register */
1406e9fd28eSDong Aisheng #define ESDHC_FLAG_HAVE_CAP1		BIT(6)
14118094430SDong Aisheng /*
142d04f8d5bSBenoît Thébaudeau  * The IP has erratum ERR004536
14318094430SDong Aisheng  * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
14418094430SDong Aisheng  * when reading data from the card
145667123f6SBenoît Thébaudeau  * This flag is also set for i.MX25 and i.MX35 in order to get
146667123f6SBenoît Thébaudeau  * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
14718094430SDong Aisheng  */
14818094430SDong Aisheng #define ESDHC_FLAG_ERR004536		BIT(7)
1494245afffSDong Aisheng /* The IP supports HS200 mode */
1504245afffSDong Aisheng #define ESDHC_FLAG_HS200		BIT(8)
15128b07674SHaibo Chen /* The IP supports HS400 mode */
15228b07674SHaibo Chen #define ESDHC_FLAG_HS400		BIT(9)
153af6a50d4SBOUGH CHEN /*
154af6a50d4SBOUGH CHEN  * The IP has errata ERR010450
155af6a50d4SBOUGH CHEN  * uSDHC: Due to the I/O timing limit, for SDR mode, SD card clock can't
156af6a50d4SBOUGH CHEN  * exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
157af6a50d4SBOUGH CHEN  */
158af6a50d4SBOUGH CHEN #define ESDHC_FLAG_ERR010450		BIT(10)
159029e2476SBOUGH CHEN /* The IP supports HS400ES mode */
160029e2476SBOUGH CHEN #define ESDHC_FLAG_HS400_ES		BIT(11)
161bb6e3581SBOUGH CHEN /* The IP has Host Controller Interface for Command Queuing */
162bb6e3581SBOUGH CHEN #define ESDHC_FLAG_CQHCI		BIT(12)
1631c4989b0SBOUGH CHEN /* need request pmqos during low power */
1641c4989b0SBOUGH CHEN #define ESDHC_FLAG_PMQOS		BIT(13)
165a26a4f1bSHaibo Chen /* The IP state got lost in low power mode */
166a26a4f1bSHaibo Chen #define ESDHC_FLAG_STATE_LOST_IN_LPMODE		BIT(14)
1675c11f1ffSHaibo Chen /* The IP lost clock rate in PM_RUNTIME */
1685c11f1ffSHaibo Chen #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME	BIT(15)
169e149860dSRichard Zhu 
170f47c4bbfSShawn Guo struct esdhc_soc_data {
171f47c4bbfSShawn Guo 	u32 flags;
172f47c4bbfSShawn Guo };
173f47c4bbfSShawn Guo 
1744f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx25_data = {
175667123f6SBenoît Thébaudeau 	.flags = ESDHC_FLAG_ERR004536,
176f47c4bbfSShawn Guo };
177f47c4bbfSShawn Guo 
1784f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx35_data = {
179667123f6SBenoît Thébaudeau 	.flags = ESDHC_FLAG_ERR004536,
180f47c4bbfSShawn Guo };
181f47c4bbfSShawn Guo 
1824f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx51_data = {
183f47c4bbfSShawn Guo 	.flags = 0,
184f47c4bbfSShawn Guo };
185f47c4bbfSShawn Guo 
1864f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx53_data = {
187f47c4bbfSShawn Guo 	.flags = ESDHC_FLAG_MULTIBLK_NO_INT,
188f47c4bbfSShawn Guo };
189f47c4bbfSShawn Guo 
1904f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx6q_data = {
1916e9fd28eSDong Aisheng 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
1926e9fd28eSDong Aisheng };
1936e9fd28eSDong Aisheng 
1944f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx6sl_data = {
1956e9fd28eSDong Aisheng 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1964245afffSDong Aisheng 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
1974245afffSDong Aisheng 			| ESDHC_FLAG_HS200,
19857ed3314SShawn Guo };
19957ed3314SShawn Guo 
2004f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx6sx_data = {
201913d4951SDong Aisheng 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
202a26a4f1bSHaibo Chen 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
203a26a4f1bSHaibo Chen 			| ESDHC_FLAG_STATE_LOST_IN_LPMODE,
204913d4951SDong Aisheng };
205913d4951SDong Aisheng 
206af6a50d4SBOUGH CHEN static const struct esdhc_soc_data usdhc_imx6ull_data = {
207af6a50d4SBOUGH CHEN 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
208af6a50d4SBOUGH CHEN 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
209a26a4f1bSHaibo Chen 			| ESDHC_FLAG_ERR010450
210a26a4f1bSHaibo Chen 			| ESDHC_FLAG_STATE_LOST_IN_LPMODE,
211af6a50d4SBOUGH CHEN };
212af6a50d4SBOUGH CHEN 
2134f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx7d_data = {
21428b07674SHaibo Chen 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
21528b07674SHaibo Chen 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
216a26a4f1bSHaibo Chen 			| ESDHC_FLAG_HS400
217a26a4f1bSHaibo Chen 			| ESDHC_FLAG_STATE_LOST_IN_LPMODE,
21828b07674SHaibo Chen };
21928b07674SHaibo Chen 
2201c4989b0SBOUGH CHEN static struct esdhc_soc_data usdhc_imx7ulp_data = {
2211c4989b0SBOUGH CHEN 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
2221c4989b0SBOUGH CHEN 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
223a26a4f1bSHaibo Chen 			| ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
224a26a4f1bSHaibo Chen 			| ESDHC_FLAG_STATE_LOST_IN_LPMODE,
2251c4989b0SBOUGH CHEN };
2261c4989b0SBOUGH CHEN 
227029e2476SBOUGH CHEN static struct esdhc_soc_data usdhc_imx8qxp_data = {
228029e2476SBOUGH CHEN 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
229029e2476SBOUGH CHEN 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
230bb6e3581SBOUGH CHEN 			| ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
231a26a4f1bSHaibo Chen 			| ESDHC_FLAG_CQHCI
2325c11f1ffSHaibo Chen 			| ESDHC_FLAG_STATE_LOST_IN_LPMODE
2335c11f1ffSHaibo Chen 			| ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
234029e2476SBOUGH CHEN };
235029e2476SBOUGH CHEN 
236e149860dSRichard Zhu struct pltfm_imx_data {
237e149860dSRichard Zhu 	u32 scratchpad;
238e62d8b8fSDong Aisheng 	struct pinctrl *pinctrl;
239ad93220dSDong Aisheng 	struct pinctrl_state *pins_100mhz;
240ad93220dSDong Aisheng 	struct pinctrl_state *pins_200mhz;
241f47c4bbfSShawn Guo 	const struct esdhc_soc_data *socdata;
242842afc02SShawn Guo 	struct esdhc_platform_data boarddata;
24352dac615SSascha Hauer 	struct clk *clk_ipg;
24452dac615SSascha Hauer 	struct clk *clk_ahb;
24552dac615SSascha Hauer 	struct clk *clk_per;
2463602785bSMichael Trimarchi 	unsigned int actual_clock;
247361b8482SLucas Stach 	enum {
248361b8482SLucas Stach 		NO_CMD_PENDING,      /* no multiblock command pending */
249361b8482SLucas Stach 		MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
250361b8482SLucas Stach 		WAIT_FOR_INT,        /* sent CMD12, waiting for response INT */
251361b8482SLucas Stach 	} multiblock_status;
252de5bdbffSDong Aisheng 	u32 is_ddr;
2531c4989b0SBOUGH CHEN 	struct pm_qos_request pm_qos_req;
254e149860dSRichard Zhu };
255e149860dSRichard Zhu 
256f8cbf461SKrzysztof Kozlowski static const struct platform_device_id imx_esdhc_devtype[] = {
25757ed3314SShawn Guo 	{
25857ed3314SShawn Guo 		.name = "sdhci-esdhc-imx25",
259f47c4bbfSShawn Guo 		.driver_data = (kernel_ulong_t) &esdhc_imx25_data,
26057ed3314SShawn Guo 	}, {
26157ed3314SShawn Guo 		.name = "sdhci-esdhc-imx35",
262f47c4bbfSShawn Guo 		.driver_data = (kernel_ulong_t) &esdhc_imx35_data,
26357ed3314SShawn Guo 	}, {
26457ed3314SShawn Guo 		.name = "sdhci-esdhc-imx51",
265f47c4bbfSShawn Guo 		.driver_data = (kernel_ulong_t) &esdhc_imx51_data,
26657ed3314SShawn Guo 	}, {
26757ed3314SShawn Guo 		/* sentinel */
26857ed3314SShawn Guo 	}
26957ed3314SShawn Guo };
27057ed3314SShawn Guo MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
27157ed3314SShawn Guo 
272abfafc2dSShawn Guo static const struct of_device_id imx_esdhc_dt_ids[] = {
273f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
274f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
275f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
276f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
277913d4951SDong Aisheng 	{ .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
2786e9fd28eSDong Aisheng 	{ .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
279f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
280af6a50d4SBOUGH CHEN 	{ .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
28128b07674SHaibo Chen 	{ .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
2821c4989b0SBOUGH CHEN 	{ .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
283029e2476SBOUGH CHEN 	{ .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
284abfafc2dSShawn Guo 	{ /* sentinel */ }
285abfafc2dSShawn Guo };
286abfafc2dSShawn Guo MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
287abfafc2dSShawn Guo 
28857ed3314SShawn Guo static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
28957ed3314SShawn Guo {
290f47c4bbfSShawn Guo 	return data->socdata == &esdhc_imx25_data;
29157ed3314SShawn Guo }
29257ed3314SShawn Guo 
29357ed3314SShawn Guo static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
29457ed3314SShawn Guo {
295f47c4bbfSShawn Guo 	return data->socdata == &esdhc_imx53_data;
29657ed3314SShawn Guo }
29757ed3314SShawn Guo 
29895a2482aSShawn Guo static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
29995a2482aSShawn Guo {
300f47c4bbfSShawn Guo 	return data->socdata == &usdhc_imx6q_data;
30195a2482aSShawn Guo }
30295a2482aSShawn Guo 
3039d61c009SShawn Guo static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
3049d61c009SShawn Guo {
305f47c4bbfSShawn Guo 	return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
3069d61c009SShawn Guo }
3079d61c009SShawn Guo 
30895f25efeSWolfram Sang static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
30995f25efeSWolfram Sang {
31095f25efeSWolfram Sang 	void __iomem *base = host->ioaddr + (reg & ~0x3);
31195f25efeSWolfram Sang 	u32 shift = (reg & 0x3) * 8;
31295f25efeSWolfram Sang 
31395f25efeSWolfram Sang 	writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
31495f25efeSWolfram Sang }
31595f25efeSWolfram Sang 
316*f581e909SHaibo Chen static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
317*f581e909SHaibo Chen {
318*f581e909SHaibo Chen 	u32 present_state;
319*f581e909SHaibo Chen 	int ret;
320*f581e909SHaibo Chen 
321*f581e909SHaibo Chen 	ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
322*f581e909SHaibo Chen 				(present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
323*f581e909SHaibo Chen 	if (ret == -ETIMEDOUT)
324*f581e909SHaibo Chen 		dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
325*f581e909SHaibo Chen }
326*f581e909SHaibo Chen 
3277e29c306SWolfram Sang static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
3287e29c306SWolfram Sang {
329361b8482SLucas Stach 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
330070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
331913413c3SShawn Guo 	u32 val = readl(host->ioaddr + reg);
332913413c3SShawn Guo 
3330322191eSDong Aisheng 	if (unlikely(reg == SDHCI_PRESENT_STATE)) {
3340322191eSDong Aisheng 		u32 fsl_prss = val;
3350322191eSDong Aisheng 		/* save the least 20 bits */
3360322191eSDong Aisheng 		val = fsl_prss & 0x000FFFFF;
3370322191eSDong Aisheng 		/* move dat[0-3] bits */
3380322191eSDong Aisheng 		val |= (fsl_prss & 0x0F000000) >> 4;
3390322191eSDong Aisheng 		/* move cmd line bit */
3400322191eSDong Aisheng 		val |= (fsl_prss & 0x00800000) << 1;
3410322191eSDong Aisheng 	}
3420322191eSDong Aisheng 
34397e4ba6aSRichard Zhu 	if (unlikely(reg == SDHCI_CAPABILITIES)) {
3446b4fb671SDong Aisheng 		/* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
3456b4fb671SDong Aisheng 		if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
3466b4fb671SDong Aisheng 			val &= 0xffff0000;
3476b4fb671SDong Aisheng 
34897e4ba6aSRichard Zhu 		/* In FSL esdhc IC module, only bit20 is used to indicate the
34997e4ba6aSRichard Zhu 		 * ADMA2 capability of esdhc, but this bit is messed up on
35097e4ba6aSRichard Zhu 		 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
35197e4ba6aSRichard Zhu 		 * don't actually support ADMA2). So set the BROKEN_ADMA
352d04f8d5bSBenoît Thébaudeau 		 * quirk on MX25/35 platforms.
35397e4ba6aSRichard Zhu 		 */
35497e4ba6aSRichard Zhu 
35597e4ba6aSRichard Zhu 		if (val & SDHCI_CAN_DO_ADMA1) {
35697e4ba6aSRichard Zhu 			val &= ~SDHCI_CAN_DO_ADMA1;
35797e4ba6aSRichard Zhu 			val |= SDHCI_CAN_DO_ADMA2;
35897e4ba6aSRichard Zhu 		}
35997e4ba6aSRichard Zhu 	}
36097e4ba6aSRichard Zhu 
3616e9fd28eSDong Aisheng 	if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
3626e9fd28eSDong Aisheng 		if (esdhc_is_usdhc(imx_data)) {
3636e9fd28eSDong Aisheng 			if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
3646e9fd28eSDong Aisheng 				val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
3656e9fd28eSDong Aisheng 			else
3666e9fd28eSDong Aisheng 				/* imx6q/dl does not have cap_1 register, fake one */
3670322191eSDong Aisheng 				val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
368888824bbSDong Aisheng 					| SDHCI_SUPPORT_SDR50
369da0295ffSDong Aisheng 					| SDHCI_USE_SDR50_TUNING
370da0295ffSDong Aisheng 					| (SDHCI_TUNING_MODE_3 << SDHCI_RETUNING_MODE_SHIFT);
37128b07674SHaibo Chen 
37228b07674SHaibo Chen 			if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
37328b07674SHaibo Chen 				val |= SDHCI_SUPPORT_HS400;
37492748beaSStefan Agner 
37592748beaSStefan Agner 			/*
37692748beaSStefan Agner 			 * Do not advertise faster UHS modes if there are no
37792748beaSStefan Agner 			 * pinctrl states for 100MHz/200MHz.
37892748beaSStefan Agner 			 */
37992748beaSStefan Agner 			if (IS_ERR_OR_NULL(imx_data->pins_100mhz) ||
38092748beaSStefan Agner 			    IS_ERR_OR_NULL(imx_data->pins_200mhz))
38192748beaSStefan Agner 				val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50
38292748beaSStefan Agner 					 | SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
3836e9fd28eSDong Aisheng 		}
3846e9fd28eSDong Aisheng 	}
3850322191eSDong Aisheng 
3869d61c009SShawn Guo 	if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
3870322191eSDong Aisheng 		val = 0;
3880322191eSDong Aisheng 		val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
3890322191eSDong Aisheng 		val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
3900322191eSDong Aisheng 		val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
3910322191eSDong Aisheng 	}
3920322191eSDong Aisheng 
39397e4ba6aSRichard Zhu 	if (unlikely(reg == SDHCI_INT_STATUS)) {
39460bf6396SShawn Guo 		if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
39560bf6396SShawn Guo 			val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
39697e4ba6aSRichard Zhu 			val |= SDHCI_INT_ADMA_ERROR;
39797e4ba6aSRichard Zhu 		}
398361b8482SLucas Stach 
399361b8482SLucas Stach 		/*
400361b8482SLucas Stach 		 * mask off the interrupt we get in response to the manually
401361b8482SLucas Stach 		 * sent CMD12
402361b8482SLucas Stach 		 */
403361b8482SLucas Stach 		if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
404361b8482SLucas Stach 		    ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
405361b8482SLucas Stach 			val &= ~SDHCI_INT_RESPONSE;
406361b8482SLucas Stach 			writel(SDHCI_INT_RESPONSE, host->ioaddr +
407361b8482SLucas Stach 						   SDHCI_INT_STATUS);
408361b8482SLucas Stach 			imx_data->multiblock_status = NO_CMD_PENDING;
409361b8482SLucas Stach 		}
41097e4ba6aSRichard Zhu 	}
41197e4ba6aSRichard Zhu 
4127e29c306SWolfram Sang 	return val;
4137e29c306SWolfram Sang }
4147e29c306SWolfram Sang 
4157e29c306SWolfram Sang static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
4167e29c306SWolfram Sang {
417e149860dSRichard Zhu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
418070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
4190d58864bSTony Lin 	u32 data;
420e149860dSRichard Zhu 
42177da3da0SAaron Brice 	if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
42277da3da0SAaron Brice 			reg == SDHCI_INT_STATUS)) {
423b7321042SDong Aisheng 		if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
4240d58864bSTony Lin 			/*
4250d58864bSTony Lin 			 * Clear and then set D3CD bit to avoid missing the
426d04f8d5bSBenoît Thébaudeau 			 * card interrupt. This is an eSDHC controller problem
4270d58864bSTony Lin 			 * so we need to apply the following workaround: clear
4280d58864bSTony Lin 			 * and set D3CD bit will make eSDHC re-sample the card
4290d58864bSTony Lin 			 * interrupt. In case a card interrupt was lost,
4300d58864bSTony Lin 			 * re-sample it by the following steps.
4310d58864bSTony Lin 			 */
4320d58864bSTony Lin 			data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
43360bf6396SShawn Guo 			data &= ~ESDHC_CTRL_D3CD;
4340d58864bSTony Lin 			writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
43560bf6396SShawn Guo 			data |= ESDHC_CTRL_D3CD;
4360d58864bSTony Lin 			writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
4370d58864bSTony Lin 		}
438915be485SDong Aisheng 
439915be485SDong Aisheng 		if (val & SDHCI_INT_ADMA_ERROR) {
440915be485SDong Aisheng 			val &= ~SDHCI_INT_ADMA_ERROR;
441915be485SDong Aisheng 			val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
442915be485SDong Aisheng 		}
4430d58864bSTony Lin 	}
4440d58864bSTony Lin 
445f47c4bbfSShawn Guo 	if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
44658ac8177SRichard Zhu 				&& (reg == SDHCI_INT_STATUS)
44758ac8177SRichard Zhu 				&& (val & SDHCI_INT_DATA_END))) {
44858ac8177SRichard Zhu 			u32 v;
44960bf6396SShawn Guo 			v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
45060bf6396SShawn Guo 			v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
45160bf6396SShawn Guo 			writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
452361b8482SLucas Stach 
453361b8482SLucas Stach 			if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
454361b8482SLucas Stach 			{
455361b8482SLucas Stach 				/* send a manual CMD12 with RESPTYP=none */
456361b8482SLucas Stach 				data = MMC_STOP_TRANSMISSION << 24 |
457361b8482SLucas Stach 				       SDHCI_CMD_ABORTCMD << 16;
458361b8482SLucas Stach 				writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
459361b8482SLucas Stach 				imx_data->multiblock_status = WAIT_FOR_INT;
460361b8482SLucas Stach 			}
46158ac8177SRichard Zhu 	}
46258ac8177SRichard Zhu 
4637e29c306SWolfram Sang 	writel(val, host->ioaddr + reg);
4647e29c306SWolfram Sang }
4657e29c306SWolfram Sang 
46695f25efeSWolfram Sang static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
46795f25efeSWolfram Sang {
468ef4d0888SShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
469070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
4700322191eSDong Aisheng 	u16 ret = 0;
4710322191eSDong Aisheng 	u32 val;
472ef4d0888SShawn Guo 
47395a2482aSShawn Guo 	if (unlikely(reg == SDHCI_HOST_VERSION)) {
474ef4d0888SShawn Guo 		reg ^= 2;
4759d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data)) {
47695a2482aSShawn Guo 			/*
477ef4d0888SShawn Guo 			 * The usdhc register returns a wrong host version.
478ef4d0888SShawn Guo 			 * Correct it here.
47995a2482aSShawn Guo 			 */
480ef4d0888SShawn Guo 			return SDHCI_SPEC_300;
481ef4d0888SShawn Guo 		}
48295a2482aSShawn Guo 	}
48395f25efeSWolfram Sang 
4840322191eSDong Aisheng 	if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
4850322191eSDong Aisheng 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
4860322191eSDong Aisheng 		if (val & ESDHC_VENDOR_SPEC_VSELECT)
4870322191eSDong Aisheng 			ret |= SDHCI_CTRL_VDD_180;
4880322191eSDong Aisheng 
4899d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data)) {
4906e9fd28eSDong Aisheng 			if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
4910322191eSDong Aisheng 				val = readl(host->ioaddr + ESDHC_MIX_CTRL);
4926e9fd28eSDong Aisheng 			else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
4936e9fd28eSDong Aisheng 				/* the std tuning bits is in ACMD12_ERR for imx6sl */
494869f8a69SAdrian Hunter 				val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
4956e9fd28eSDong Aisheng 		}
4966e9fd28eSDong Aisheng 
4970322191eSDong Aisheng 		if (val & ESDHC_MIX_CTRL_EXE_TUNE)
4980322191eSDong Aisheng 			ret |= SDHCI_CTRL_EXEC_TUNING;
4990322191eSDong Aisheng 		if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
5000322191eSDong Aisheng 			ret |= SDHCI_CTRL_TUNED_CLK;
5010322191eSDong Aisheng 
5020322191eSDong Aisheng 		ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
5030322191eSDong Aisheng 
5040322191eSDong Aisheng 		return ret;
5050322191eSDong Aisheng 	}
5060322191eSDong Aisheng 
5077dd109efSDong Aisheng 	if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
5087dd109efSDong Aisheng 		if (esdhc_is_usdhc(imx_data)) {
5097dd109efSDong Aisheng 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
5107dd109efSDong Aisheng 			ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
5117dd109efSDong Aisheng 			/* Swap AC23 bit */
5127dd109efSDong Aisheng 			if (m & ESDHC_MIX_CTRL_AC23EN) {
5137dd109efSDong Aisheng 				ret &= ~ESDHC_MIX_CTRL_AC23EN;
5147dd109efSDong Aisheng 				ret |= SDHCI_TRNS_AUTO_CMD23;
5157dd109efSDong Aisheng 			}
5167dd109efSDong Aisheng 		} else {
5177dd109efSDong Aisheng 			ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
5187dd109efSDong Aisheng 		}
5197dd109efSDong Aisheng 
5207dd109efSDong Aisheng 		return ret;
5217dd109efSDong Aisheng 	}
5227dd109efSDong Aisheng 
52395f25efeSWolfram Sang 	return readw(host->ioaddr + reg);
52495f25efeSWolfram Sang }
52595f25efeSWolfram Sang 
52695f25efeSWolfram Sang static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
52795f25efeSWolfram Sang {
52895f25efeSWolfram Sang 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
529070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
5300322191eSDong Aisheng 	u32 new_val = 0;
53195f25efeSWolfram Sang 
53295f25efeSWolfram Sang 	switch (reg) {
5330322191eSDong Aisheng 	case SDHCI_CLOCK_CONTROL:
5340322191eSDong Aisheng 		new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
5350322191eSDong Aisheng 		if (val & SDHCI_CLOCK_CARD_EN)
5360322191eSDong Aisheng 			new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
5370322191eSDong Aisheng 		else
5380322191eSDong Aisheng 			new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
5390322191eSDong Aisheng 		writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
540*f581e909SHaibo Chen 		if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
541*f581e909SHaibo Chen 			esdhc_wait_for_card_clock_gate_off(host);
5420322191eSDong Aisheng 		return;
5430322191eSDong Aisheng 	case SDHCI_HOST_CONTROL2:
5440322191eSDong Aisheng 		new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
5450322191eSDong Aisheng 		if (val & SDHCI_CTRL_VDD_180)
5460322191eSDong Aisheng 			new_val |= ESDHC_VENDOR_SPEC_VSELECT;
5470322191eSDong Aisheng 		else
5480322191eSDong Aisheng 			new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
5490322191eSDong Aisheng 		writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
5506e9fd28eSDong Aisheng 		if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
5510322191eSDong Aisheng 			new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
552da0295ffSDong Aisheng 			if (val & SDHCI_CTRL_TUNED_CLK) {
5530322191eSDong Aisheng 				new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
554da0295ffSDong Aisheng 				new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
555da0295ffSDong Aisheng 			} else {
5560322191eSDong Aisheng 				new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
557da0295ffSDong Aisheng 				new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
558da0295ffSDong Aisheng 			}
5590322191eSDong Aisheng 			writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
5606e9fd28eSDong Aisheng 		} else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
561869f8a69SAdrian Hunter 			u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
5626e9fd28eSDong Aisheng 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
5638b2bb0adSDong Aisheng 			if (val & SDHCI_CTRL_TUNED_CLK) {
5648b2bb0adSDong Aisheng 				v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
5656e9fd28eSDong Aisheng 			} else {
5668b2bb0adSDong Aisheng 				v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
5676e9fd28eSDong Aisheng 				m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
5680b330e38SDong Aisheng 				m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
5696e9fd28eSDong Aisheng 			}
5706e9fd28eSDong Aisheng 
5718b2bb0adSDong Aisheng 			if (val & SDHCI_CTRL_EXEC_TUNING) {
5728b2bb0adSDong Aisheng 				v |= ESDHC_MIX_CTRL_EXE_TUNE;
5738b2bb0adSDong Aisheng 				m |= ESDHC_MIX_CTRL_FBCLK_SEL;
5740b330e38SDong Aisheng 				m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
5758b2bb0adSDong Aisheng 			} else {
5768b2bb0adSDong Aisheng 				v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
5778b2bb0adSDong Aisheng 			}
5786e9fd28eSDong Aisheng 
579869f8a69SAdrian Hunter 			writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
5806e9fd28eSDong Aisheng 			writel(m, host->ioaddr + ESDHC_MIX_CTRL);
5816e9fd28eSDong Aisheng 		}
5820322191eSDong Aisheng 		return;
58395f25efeSWolfram Sang 	case SDHCI_TRANSFER_MODE:
584f47c4bbfSShawn Guo 		if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
58558ac8177SRichard Zhu 				&& (host->cmd->opcode == SD_IO_RW_EXTENDED)
58658ac8177SRichard Zhu 				&& (host->cmd->data->blocks > 1)
58758ac8177SRichard Zhu 				&& (host->cmd->data->flags & MMC_DATA_READ)) {
58858ac8177SRichard Zhu 			u32 v;
58960bf6396SShawn Guo 			v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
59060bf6396SShawn Guo 			v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
59160bf6396SShawn Guo 			writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
59258ac8177SRichard Zhu 		}
59369f54698SShawn Guo 
5949d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data)) {
5953fbd4322SAndrew Gabbasov 			u32 wml;
59669f54698SShawn Guo 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
5972a15f981SShawn Guo 			/* Swap AC23 bit */
5982a15f981SShawn Guo 			if (val & SDHCI_TRNS_AUTO_CMD23) {
5992a15f981SShawn Guo 				val &= ~SDHCI_TRNS_AUTO_CMD23;
6002a15f981SShawn Guo 				val |= ESDHC_MIX_CTRL_AC23EN;
6012a15f981SShawn Guo 			}
6022a15f981SShawn Guo 			m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
60369f54698SShawn Guo 			writel(m, host->ioaddr + ESDHC_MIX_CTRL);
6043fbd4322SAndrew Gabbasov 
6053fbd4322SAndrew Gabbasov 			/* Set watermark levels for PIO access to maximum value
6063fbd4322SAndrew Gabbasov 			 * (128 words) to accommodate full 512 bytes buffer.
6073fbd4322SAndrew Gabbasov 			 * For DMA access restore the levels to default value.
6083fbd4322SAndrew Gabbasov 			 */
6093fbd4322SAndrew Gabbasov 			m = readl(host->ioaddr + ESDHC_WTMK_LVL);
6103fbd4322SAndrew Gabbasov 			if (val & SDHCI_TRNS_DMA)
6113fbd4322SAndrew Gabbasov 				wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
6123fbd4322SAndrew Gabbasov 			else
6133fbd4322SAndrew Gabbasov 				wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
6143fbd4322SAndrew Gabbasov 			m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
6153fbd4322SAndrew Gabbasov 			       ESDHC_WTMK_LVL_WR_WML_MASK);
6163fbd4322SAndrew Gabbasov 			m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
6173fbd4322SAndrew Gabbasov 			     (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
6183fbd4322SAndrew Gabbasov 			writel(m, host->ioaddr + ESDHC_WTMK_LVL);
61969f54698SShawn Guo 		} else {
62069f54698SShawn Guo 			/*
62169f54698SShawn Guo 			 * Postpone this write, we must do it together with a
62269f54698SShawn Guo 			 * command write that is down below.
62369f54698SShawn Guo 			 */
624e149860dSRichard Zhu 			imx_data->scratchpad = val;
62569f54698SShawn Guo 		}
62695f25efeSWolfram Sang 		return;
62795f25efeSWolfram Sang 	case SDHCI_COMMAND:
628361b8482SLucas Stach 		if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
62958ac8177SRichard Zhu 			val |= SDHCI_CMD_ABORTCMD;
63095a2482aSShawn Guo 
631361b8482SLucas Stach 		if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
632f47c4bbfSShawn Guo 		    (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
633361b8482SLucas Stach 			imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
634361b8482SLucas Stach 
6359d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data))
63695a2482aSShawn Guo 			writel(val << 16,
63795a2482aSShawn Guo 			       host->ioaddr + SDHCI_TRANSFER_MODE);
63869f54698SShawn Guo 		else
639e149860dSRichard Zhu 			writel(val << 16 | imx_data->scratchpad,
64095f25efeSWolfram Sang 			       host->ioaddr + SDHCI_TRANSFER_MODE);
64195f25efeSWolfram Sang 		return;
64295f25efeSWolfram Sang 	case SDHCI_BLOCK_SIZE:
64395f25efeSWolfram Sang 		val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
64495f25efeSWolfram Sang 		break;
64595f25efeSWolfram Sang 	}
64695f25efeSWolfram Sang 	esdhc_clrset_le(host, 0xffff, val, reg);
64795f25efeSWolfram Sang }
64895f25efeSWolfram Sang 
64977da3da0SAaron Brice static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
65077da3da0SAaron Brice {
65177da3da0SAaron Brice 	u8 ret;
65277da3da0SAaron Brice 	u32 val;
65377da3da0SAaron Brice 
65477da3da0SAaron Brice 	switch (reg) {
65577da3da0SAaron Brice 	case SDHCI_HOST_CONTROL:
65677da3da0SAaron Brice 		val = readl(host->ioaddr + reg);
65777da3da0SAaron Brice 
65877da3da0SAaron Brice 		ret = val & SDHCI_CTRL_LED;
65977da3da0SAaron Brice 		ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
66077da3da0SAaron Brice 		ret |= (val & ESDHC_CTRL_4BITBUS);
66177da3da0SAaron Brice 		ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
66277da3da0SAaron Brice 		return ret;
66377da3da0SAaron Brice 	}
66477da3da0SAaron Brice 
66577da3da0SAaron Brice 	return readb(host->ioaddr + reg);
66677da3da0SAaron Brice }
66777da3da0SAaron Brice 
66895f25efeSWolfram Sang static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
66995f25efeSWolfram Sang {
6709a0985b7SWilson Callan 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
671070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
67281a0a8bcSBenoît Thébaudeau 	u32 new_val = 0;
673af51079eSSascha Hauer 	u32 mask;
67495f25efeSWolfram Sang 
67595f25efeSWolfram Sang 	switch (reg) {
67695f25efeSWolfram Sang 	case SDHCI_POWER_CONTROL:
67795f25efeSWolfram Sang 		/*
67895f25efeSWolfram Sang 		 * FSL put some DMA bits here
67995f25efeSWolfram Sang 		 * If your board has a regulator, code should be here
68095f25efeSWolfram Sang 		 */
68195f25efeSWolfram Sang 		return;
68295f25efeSWolfram Sang 	case SDHCI_HOST_CONTROL:
6836b40d182SShawn Guo 		/* FSL messed up here, so we need to manually compose it. */
684af51079eSSascha Hauer 		new_val = val & SDHCI_CTRL_LED;
6857122bbb0SMasanari Iida 		/* ensure the endianness */
68695f25efeSWolfram Sang 		new_val |= ESDHC_HOST_CONTROL_LE;
6879a0985b7SWilson Callan 		/* bits 8&9 are reserved on mx25 */
6889a0985b7SWilson Callan 		if (!is_imx25_esdhc(imx_data)) {
68995f25efeSWolfram Sang 			/* DMA mode bits are shifted */
69095f25efeSWolfram Sang 			new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
6919a0985b7SWilson Callan 		}
69295f25efeSWolfram Sang 
693af51079eSSascha Hauer 		/*
694af51079eSSascha Hauer 		 * Do not touch buswidth bits here. This is done in
695af51079eSSascha Hauer 		 * esdhc_pltfm_bus_width.
696f6825748SMartin Fuzzey 		 * Do not touch the D3CD bit either which is used for the
697d04f8d5bSBenoît Thébaudeau 		 * SDIO interrupt erratum workaround.
698af51079eSSascha Hauer 		 */
699f6825748SMartin Fuzzey 		mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
700af51079eSSascha Hauer 
701af51079eSSascha Hauer 		esdhc_clrset_le(host, mask, new_val, reg);
70295f25efeSWolfram Sang 		return;
70381a0a8bcSBenoît Thébaudeau 	case SDHCI_SOFTWARE_RESET:
70481a0a8bcSBenoît Thébaudeau 		if (val & SDHCI_RESET_DATA)
70581a0a8bcSBenoît Thébaudeau 			new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
70681a0a8bcSBenoît Thébaudeau 		break;
70795f25efeSWolfram Sang 	}
70895f25efeSWolfram Sang 	esdhc_clrset_le(host, 0xff, val, reg);
709913413c3SShawn Guo 
71081a0a8bcSBenoît Thébaudeau 	if (reg == SDHCI_SOFTWARE_RESET) {
71181a0a8bcSBenoît Thébaudeau 		if (val & SDHCI_RESET_ALL) {
712913413c3SShawn Guo 			/*
71381a0a8bcSBenoît Thébaudeau 			 * The esdhc has a design violation to SDHC spec which
71481a0a8bcSBenoît Thébaudeau 			 * tells that software reset should not affect card
71581a0a8bcSBenoît Thébaudeau 			 * detection circuit. But esdhc clears its SYSCTL
71681a0a8bcSBenoît Thébaudeau 			 * register bits [0..2] during the software reset. This
71781a0a8bcSBenoît Thébaudeau 			 * will stop those clocks that card detection circuit
71881a0a8bcSBenoît Thébaudeau 			 * relies on. To work around it, we turn the clocks on
71981a0a8bcSBenoît Thébaudeau 			 * back to keep card detection circuit functional.
720913413c3SShawn Guo 			 */
721913413c3SShawn Guo 			esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
72258c8c4fbSShawn Guo 			/*
72358c8c4fbSShawn Guo 			 * The reset on usdhc fails to clear MIX_CTRL register.
72458c8c4fbSShawn Guo 			 * Do it manually here.
72558c8c4fbSShawn Guo 			 */
726de5bdbffSDong Aisheng 			if (esdhc_is_usdhc(imx_data)) {
72781a0a8bcSBenoît Thébaudeau 				/*
72881a0a8bcSBenoît Thébaudeau 				 * the tuning bits should be kept during reset
72981a0a8bcSBenoît Thébaudeau 				 */
730d131a71cSDong Aisheng 				new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
731d131a71cSDong Aisheng 				writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
732d131a71cSDong Aisheng 						host->ioaddr + ESDHC_MIX_CTRL);
733de5bdbffSDong Aisheng 				imx_data->is_ddr = 0;
734de5bdbffSDong Aisheng 			}
73581a0a8bcSBenoît Thébaudeau 		} else if (val & SDHCI_RESET_DATA) {
73681a0a8bcSBenoît Thébaudeau 			/*
73781a0a8bcSBenoît Thébaudeau 			 * The eSDHC DAT line software reset clears at least the
73881a0a8bcSBenoît Thébaudeau 			 * data transfer width on i.MX25, so make sure that the
73981a0a8bcSBenoît Thébaudeau 			 * Host Control register is unaffected.
74081a0a8bcSBenoît Thébaudeau 			 */
74181a0a8bcSBenoît Thébaudeau 			esdhc_clrset_le(host, 0xff, new_val,
74281a0a8bcSBenoît Thébaudeau 					SDHCI_HOST_CONTROL);
74381a0a8bcSBenoît Thébaudeau 		}
74458c8c4fbSShawn Guo 	}
74595f25efeSWolfram Sang }
74695f25efeSWolfram Sang 
7470ddf03c9SLucas Stach static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
7480ddf03c9SLucas Stach {
7490ddf03c9SLucas Stach 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
7500ddf03c9SLucas Stach 
751a974862fSDong Aisheng 	return pltfm_host->clock;
7520ddf03c9SLucas Stach }
7530ddf03c9SLucas Stach 
75495f25efeSWolfram Sang static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
75595f25efeSWolfram Sang {
75695f25efeSWolfram Sang 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
75795f25efeSWolfram Sang 
758a974862fSDong Aisheng 	return pltfm_host->clock / 256 / 16;
75995f25efeSWolfram Sang }
76095f25efeSWolfram Sang 
7618ba9580aSLucas Stach static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
7628ba9580aSLucas Stach 					 unsigned int clock)
7638ba9580aSLucas Stach {
7648ba9580aSLucas Stach 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
765070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
766a974862fSDong Aisheng 	unsigned int host_clock = pltfm_host->clock;
7675143c953SBenoît Thébaudeau 	int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
7685143c953SBenoît Thébaudeau 	int pre_div = 1;
769d31fc00aSDong Aisheng 	int div = 1;
770*f581e909SHaibo Chen 	int ret;
771fed2f6e2SDong Aisheng 	u32 temp, val;
7728ba9580aSLucas Stach 
7739d61c009SShawn Guo 	if (esdhc_is_usdhc(imx_data)) {
774fed2f6e2SDong Aisheng 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
775fed2f6e2SDong Aisheng 		writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
776fed2f6e2SDong Aisheng 			host->ioaddr + ESDHC_VENDOR_SPEC);
777*f581e909SHaibo Chen 		esdhc_wait_for_card_clock_gate_off(host);
778fed2f6e2SDong Aisheng 	}
77973e736f8SStefan Agner 
78073e736f8SStefan Agner 	if (clock == 0) {
78173e736f8SStefan Agner 		host->mmc->actual_clock = 0;
782373073efSRussell King 		return;
783fed2f6e2SDong Aisheng 	}
784d31fc00aSDong Aisheng 
785499ed50fSBenoît Thébaudeau 	/* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
786499ed50fSBenoît Thébaudeau 	if (is_imx53_esdhc(imx_data)) {
787499ed50fSBenoît Thébaudeau 		/*
788499ed50fSBenoît Thébaudeau 		 * According to the i.MX53 reference manual, if DLLCTRL[10] can
789499ed50fSBenoît Thébaudeau 		 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
790499ed50fSBenoît Thébaudeau 		 */
791499ed50fSBenoît Thébaudeau 		val = readl(host->ioaddr + ESDHC_DLL_CTRL);
792499ed50fSBenoît Thébaudeau 		writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
793499ed50fSBenoît Thébaudeau 		temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
794499ed50fSBenoît Thébaudeau 		writel(val, host->ioaddr + ESDHC_DLL_CTRL);
795499ed50fSBenoît Thébaudeau 		if (temp & BIT(10))
796499ed50fSBenoît Thébaudeau 			pre_div = 2;
797499ed50fSBenoît Thébaudeau 	}
798499ed50fSBenoît Thébaudeau 
799d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
800d31fc00aSDong Aisheng 	temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
801d31fc00aSDong Aisheng 		| ESDHC_CLOCK_MASK);
802d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
803d31fc00aSDong Aisheng 
804af6a50d4SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
805af6a50d4SBOUGH CHEN 		unsigned int max_clock;
806af6a50d4SBOUGH CHEN 
807af6a50d4SBOUGH CHEN 		max_clock = imx_data->is_ddr ? 45000000 : 150000000;
808af6a50d4SBOUGH CHEN 
809af6a50d4SBOUGH CHEN 		clock = min(clock, max_clock);
810af6a50d4SBOUGH CHEN 	}
811af6a50d4SBOUGH CHEN 
8125143c953SBenoît Thébaudeau 	while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
8135143c953SBenoît Thébaudeau 			pre_div < 256)
814d31fc00aSDong Aisheng 		pre_div *= 2;
815d31fc00aSDong Aisheng 
8165143c953SBenoît Thébaudeau 	while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
817d31fc00aSDong Aisheng 		div++;
818d31fc00aSDong Aisheng 
8195143c953SBenoît Thébaudeau 	host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
820d31fc00aSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
821e76b8559SDong Aisheng 		clock, host->mmc->actual_clock);
822d31fc00aSDong Aisheng 
823d31fc00aSDong Aisheng 	pre_div >>= 1;
824d31fc00aSDong Aisheng 	div--;
825d31fc00aSDong Aisheng 
826d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
827d31fc00aSDong Aisheng 	temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
828d31fc00aSDong Aisheng 		| (div << ESDHC_DIVIDER_SHIFT)
829d31fc00aSDong Aisheng 		| (pre_div << ESDHC_PREDIV_SHIFT));
830d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
831fed2f6e2SDong Aisheng 
832*f581e909SHaibo Chen 	/* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
833*f581e909SHaibo Chen 	ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
834*f581e909SHaibo Chen 				(temp & ESDHC_CLOCK_STABLE), 2, 100);
835*f581e909SHaibo Chen 	if (ret == -ETIMEDOUT)
836*f581e909SHaibo Chen 		dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
837*f581e909SHaibo Chen 
8389d61c009SShawn Guo 	if (esdhc_is_usdhc(imx_data)) {
839fed2f6e2SDong Aisheng 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
840fed2f6e2SDong Aisheng 		writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
841fed2f6e2SDong Aisheng 			host->ioaddr + ESDHC_VENDOR_SPEC);
842fed2f6e2SDong Aisheng 	}
843fed2f6e2SDong Aisheng 
8448ba9580aSLucas Stach }
8458ba9580aSLucas Stach 
846913413c3SShawn Guo static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
847913413c3SShawn Guo {
848842afc02SShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
849070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
850842afc02SShawn Guo 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
851913413c3SShawn Guo 
852913413c3SShawn Guo 	switch (boarddata->wp_type) {
853913413c3SShawn Guo 	case ESDHC_WP_GPIO:
854fbe5fdd1SShawn Guo 		return mmc_gpio_get_ro(host->mmc);
855913413c3SShawn Guo 	case ESDHC_WP_CONTROLLER:
856913413c3SShawn Guo 		return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
857913413c3SShawn Guo 			       SDHCI_WRITE_PROTECT);
858913413c3SShawn Guo 	case ESDHC_WP_NONE:
859913413c3SShawn Guo 		break;
860913413c3SShawn Guo 	}
861913413c3SShawn Guo 
862913413c3SShawn Guo 	return -ENOSYS;
863913413c3SShawn Guo }
864913413c3SShawn Guo 
8652317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
866af51079eSSascha Hauer {
867af51079eSSascha Hauer 	u32 ctrl;
868af51079eSSascha Hauer 
869af51079eSSascha Hauer 	switch (width) {
870af51079eSSascha Hauer 	case MMC_BUS_WIDTH_8:
871af51079eSSascha Hauer 		ctrl = ESDHC_CTRL_8BITBUS;
872af51079eSSascha Hauer 		break;
873af51079eSSascha Hauer 	case MMC_BUS_WIDTH_4:
874af51079eSSascha Hauer 		ctrl = ESDHC_CTRL_4BITBUS;
875af51079eSSascha Hauer 		break;
876af51079eSSascha Hauer 	default:
877af51079eSSascha Hauer 		ctrl = 0;
878af51079eSSascha Hauer 		break;
879af51079eSSascha Hauer 	}
880af51079eSSascha Hauer 
881af51079eSSascha Hauer 	esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
882af51079eSSascha Hauer 			SDHCI_HOST_CONTROL);
883af51079eSSascha Hauer }
884af51079eSSascha Hauer 
885de3e1dd0SBOUGH CHEN static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
886de3e1dd0SBOUGH CHEN {
887de3e1dd0SBOUGH CHEN 	struct sdhci_host *host = mmc_priv(mmc);
888de3e1dd0SBOUGH CHEN 
889de3e1dd0SBOUGH CHEN 	/*
890de3e1dd0SBOUGH CHEN 	 * i.MX uSDHC internally already uses a fixed optimized timing for
891de3e1dd0SBOUGH CHEN 	 * DDR50, normally does not require tuning for DDR50 mode.
892de3e1dd0SBOUGH CHEN 	 */
893de3e1dd0SBOUGH CHEN 	if (host->timing == MMC_TIMING_UHS_DDR50)
894de3e1dd0SBOUGH CHEN 		return 0;
895de3e1dd0SBOUGH CHEN 
896de3e1dd0SBOUGH CHEN 	return sdhci_execute_tuning(mmc, opcode);
897de3e1dd0SBOUGH CHEN }
898de3e1dd0SBOUGH CHEN 
8990322191eSDong Aisheng static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
9000322191eSDong Aisheng {
9010322191eSDong Aisheng 	u32 reg;
9020322191eSDong Aisheng 
9030322191eSDong Aisheng 	/* FIXME: delay a bit for card to be ready for next tuning due to errors */
9040322191eSDong Aisheng 	mdelay(1);
9050322191eSDong Aisheng 
9060322191eSDong Aisheng 	reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
9070322191eSDong Aisheng 	reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
9080322191eSDong Aisheng 			ESDHC_MIX_CTRL_FBCLK_SEL;
9090322191eSDong Aisheng 	writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
9100322191eSDong Aisheng 	writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
9110322191eSDong Aisheng 	dev_dbg(mmc_dev(host->mmc),
912d04f8d5bSBenoît Thébaudeau 		"tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
9130322191eSDong Aisheng 			val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
9140322191eSDong Aisheng }
9150322191eSDong Aisheng 
9160322191eSDong Aisheng static void esdhc_post_tuning(struct sdhci_host *host)
9170322191eSDong Aisheng {
9180322191eSDong Aisheng 	u32 reg;
9190322191eSDong Aisheng 
9200322191eSDong Aisheng 	reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
9210322191eSDong Aisheng 	reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
922da0295ffSDong Aisheng 	reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
9230322191eSDong Aisheng 	writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
9240322191eSDong Aisheng }
9250322191eSDong Aisheng 
9260322191eSDong Aisheng static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
9270322191eSDong Aisheng {
9280322191eSDong Aisheng 	int min, max, avg, ret;
9290322191eSDong Aisheng 
9300322191eSDong Aisheng 	/* find the mininum delay first which can pass tuning */
9310322191eSDong Aisheng 	min = ESDHC_TUNE_CTRL_MIN;
9320322191eSDong Aisheng 	while (min < ESDHC_TUNE_CTRL_MAX) {
9330322191eSDong Aisheng 		esdhc_prepare_tuning(host, min);
9349979dbe5SChaotian Jing 		if (!mmc_send_tuning(host->mmc, opcode, NULL))
9350322191eSDong Aisheng 			break;
9360322191eSDong Aisheng 		min += ESDHC_TUNE_CTRL_STEP;
9370322191eSDong Aisheng 	}
9380322191eSDong Aisheng 
9390322191eSDong Aisheng 	/* find the maxinum delay which can not pass tuning */
9400322191eSDong Aisheng 	max = min + ESDHC_TUNE_CTRL_STEP;
9410322191eSDong Aisheng 	while (max < ESDHC_TUNE_CTRL_MAX) {
9420322191eSDong Aisheng 		esdhc_prepare_tuning(host, max);
9439979dbe5SChaotian Jing 		if (mmc_send_tuning(host->mmc, opcode, NULL)) {
9440322191eSDong Aisheng 			max -= ESDHC_TUNE_CTRL_STEP;
9450322191eSDong Aisheng 			break;
9460322191eSDong Aisheng 		}
9470322191eSDong Aisheng 		max += ESDHC_TUNE_CTRL_STEP;
9480322191eSDong Aisheng 	}
9490322191eSDong Aisheng 
9500322191eSDong Aisheng 	/* use average delay to get the best timing */
9510322191eSDong Aisheng 	avg = (min + max) / 2;
9520322191eSDong Aisheng 	esdhc_prepare_tuning(host, avg);
9539979dbe5SChaotian Jing 	ret = mmc_send_tuning(host->mmc, opcode, NULL);
9540322191eSDong Aisheng 	esdhc_post_tuning(host);
9550322191eSDong Aisheng 
956d04f8d5bSBenoît Thébaudeau 	dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
9570322191eSDong Aisheng 		ret ? "failed" : "passed", avg, ret);
9580322191eSDong Aisheng 
9590322191eSDong Aisheng 	return ret;
9600322191eSDong Aisheng }
9610322191eSDong Aisheng 
962029e2476SBOUGH CHEN static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
963029e2476SBOUGH CHEN {
964029e2476SBOUGH CHEN 	struct sdhci_host *host = mmc_priv(mmc);
965029e2476SBOUGH CHEN 	u32 m;
966029e2476SBOUGH CHEN 
967029e2476SBOUGH CHEN 	m = readl(host->ioaddr + ESDHC_MIX_CTRL);
968029e2476SBOUGH CHEN 	if (ios->enhanced_strobe)
969029e2476SBOUGH CHEN 		m |= ESDHC_MIX_CTRL_HS400_ES_EN;
970029e2476SBOUGH CHEN 	else
971029e2476SBOUGH CHEN 		m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
972029e2476SBOUGH CHEN 	writel(m, host->ioaddr + ESDHC_MIX_CTRL);
973029e2476SBOUGH CHEN }
974029e2476SBOUGH CHEN 
975ad93220dSDong Aisheng static int esdhc_change_pinstate(struct sdhci_host *host,
976ad93220dSDong Aisheng 						unsigned int uhs)
977ad93220dSDong Aisheng {
978ad93220dSDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
979070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
980ad93220dSDong Aisheng 	struct pinctrl_state *pinctrl;
981ad93220dSDong Aisheng 
982ad93220dSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
983ad93220dSDong Aisheng 
984ad93220dSDong Aisheng 	if (IS_ERR(imx_data->pinctrl) ||
985ad93220dSDong Aisheng 		IS_ERR(imx_data->pins_100mhz) ||
986ad93220dSDong Aisheng 		IS_ERR(imx_data->pins_200mhz))
987ad93220dSDong Aisheng 		return -EINVAL;
988ad93220dSDong Aisheng 
989ad93220dSDong Aisheng 	switch (uhs) {
990ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR50:
9919f327845SHaibo Chen 	case MMC_TIMING_UHS_DDR50:
992ad93220dSDong Aisheng 		pinctrl = imx_data->pins_100mhz;
993ad93220dSDong Aisheng 		break;
994ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR104:
995429a5b45SDong Aisheng 	case MMC_TIMING_MMC_HS200:
99628b07674SHaibo Chen 	case MMC_TIMING_MMC_HS400:
997ad93220dSDong Aisheng 		pinctrl = imx_data->pins_200mhz;
998ad93220dSDong Aisheng 		break;
999ad93220dSDong Aisheng 	default:
1000ad93220dSDong Aisheng 		/* back to default state for other legacy timing */
10012480b720SUlf Hansson 		return pinctrl_select_default_state(mmc_dev(host->mmc));
1002ad93220dSDong Aisheng 	}
1003ad93220dSDong Aisheng 
1004ad93220dSDong Aisheng 	return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1005ad93220dSDong Aisheng }
1006ad93220dSDong Aisheng 
100728b07674SHaibo Chen /*
1008d04f8d5bSBenoît Thébaudeau  * For HS400 eMMC, there is a data_strobe line. This signal is generated
100928b07674SHaibo Chen  * by the device and used for data output and CRC status response output
101028b07674SHaibo Chen  * in HS400 mode. The frequency of this signal follows the frequency of
1011d04f8d5bSBenoît Thébaudeau  * CLK generated by host. The host receives the data which is aligned to the
101228b07674SHaibo Chen  * edge of data_strobe line. Due to the time delay between CLK line and
101328b07674SHaibo Chen  * data_strobe line, if the delay time is larger than one clock cycle,
1014d04f8d5bSBenoît Thébaudeau  * then CLK and data_strobe line will be misaligned, read error shows up.
101528b07674SHaibo Chen  */
101628b07674SHaibo Chen static void esdhc_set_strobe_dll(struct sdhci_host *host)
101728b07674SHaibo Chen {
10185bd2acdcSHaibo Chen 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
10195bd2acdcSHaibo Chen 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
10205bd2acdcSHaibo Chen 	u32 strobe_delay;
102128b07674SHaibo Chen 	u32 v;
102228b07674SHaibo Chen 
10237ac6da26SDong Aisheng 	/* disable clock before enabling strobe dll */
10247ac6da26SDong Aisheng 	writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
10257ac6da26SDong Aisheng 		~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
10267ac6da26SDong Aisheng 		host->ioaddr + ESDHC_VENDOR_SPEC);
1027*f581e909SHaibo Chen 	esdhc_wait_for_card_clock_gate_off(host);
10287ac6da26SDong Aisheng 
102928b07674SHaibo Chen 	/* force a reset on strobe dll */
103028b07674SHaibo Chen 	writel(ESDHC_STROBE_DLL_CTRL_RESET,
103128b07674SHaibo Chen 		host->ioaddr + ESDHC_STROBE_DLL_CTRL);
10322eaf5a53SBOUGH CHEN 	/* clear the reset bit on strobe dll before any setting */
10332eaf5a53SBOUGH CHEN 	writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
10342eaf5a53SBOUGH CHEN 
103528b07674SHaibo Chen 	/*
103628b07674SHaibo Chen 	 * enable strobe dll ctrl and adjust the delay target
103728b07674SHaibo Chen 	 * for the uSDHC loopback read clock
103828b07674SHaibo Chen 	 */
10395bd2acdcSHaibo Chen 	if (imx_data->boarddata.strobe_dll_delay_target)
10405bd2acdcSHaibo Chen 		strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
10415bd2acdcSHaibo Chen 	else
10425bd2acdcSHaibo Chen 		strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
104328b07674SHaibo Chen 	v = ESDHC_STROBE_DLL_CTRL_ENABLE |
10442eaf5a53SBOUGH CHEN 		ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
10455bd2acdcSHaibo Chen 		(strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
104628b07674SHaibo Chen 	writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
10472eaf5a53SBOUGH CHEN 	/* wait 5us to make sure strobe dll status register stable */
10482eaf5a53SBOUGH CHEN 	udelay(5);
104928b07674SHaibo Chen 	v = readl(host->ioaddr + ESDHC_STROBE_DLL_STATUS);
105028b07674SHaibo Chen 	if (!(v & ESDHC_STROBE_DLL_STS_REF_LOCK))
105128b07674SHaibo Chen 		dev_warn(mmc_dev(host->mmc),
105228b07674SHaibo Chen 		"warning! HS400 strobe DLL status REF not lock!\n");
105328b07674SHaibo Chen 	if (!(v & ESDHC_STROBE_DLL_STS_SLV_LOCK))
105428b07674SHaibo Chen 		dev_warn(mmc_dev(host->mmc),
105528b07674SHaibo Chen 		"warning! HS400 strobe DLL status SLV not lock!\n");
105628b07674SHaibo Chen }
105728b07674SHaibo Chen 
1058d9370424SHaibo Chen static void esdhc_reset_tuning(struct sdhci_host *host)
1059d9370424SHaibo Chen {
1060d9370424SHaibo Chen 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1061d9370424SHaibo Chen 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1062d9370424SHaibo Chen 	u32 ctrl;
1063d9370424SHaibo Chen 
1064d04f8d5bSBenoît Thébaudeau 	/* Reset the tuning circuit */
1065d9370424SHaibo Chen 	if (esdhc_is_usdhc(imx_data)) {
1066d9370424SHaibo Chen 		if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1067d9370424SHaibo Chen 			ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1068d9370424SHaibo Chen 			ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1069d9370424SHaibo Chen 			ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1070d9370424SHaibo Chen 			writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1071d9370424SHaibo Chen 			writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1072d9370424SHaibo Chen 		} else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1073869f8a69SAdrian Hunter 			ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1074d9370424SHaibo Chen 			ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1075869f8a69SAdrian Hunter 			writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1076d9370424SHaibo Chen 		}
1077d9370424SHaibo Chen 	}
1078d9370424SHaibo Chen }
1079d9370424SHaibo Chen 
1080850a29b8SRussell King static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1081ad93220dSDong Aisheng {
108228b07674SHaibo Chen 	u32 m;
1083ad93220dSDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1084070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1085602519b2SDong Aisheng 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1086ad93220dSDong Aisheng 
108728b07674SHaibo Chen 	/* disable ddr mode and disable HS400 mode */
108828b07674SHaibo Chen 	m = readl(host->ioaddr + ESDHC_MIX_CTRL);
108928b07674SHaibo Chen 	m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
109028b07674SHaibo Chen 	imx_data->is_ddr = 0;
109128b07674SHaibo Chen 
1092850a29b8SRussell King 	switch (timing) {
1093ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR12:
1094ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR25:
1095ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR50:
1096ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR104:
1097de0a0decSBOUGH CHEN 	case MMC_TIMING_MMC_HS:
1098429a5b45SDong Aisheng 	case MMC_TIMING_MMC_HS200:
109928b07674SHaibo Chen 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1100ad93220dSDong Aisheng 		break;
1101ad93220dSDong Aisheng 	case MMC_TIMING_UHS_DDR50:
110269f5bf38SAisheng Dong 	case MMC_TIMING_MMC_DDR52:
110328b07674SHaibo Chen 		m |= ESDHC_MIX_CTRL_DDREN;
110428b07674SHaibo Chen 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1105de5bdbffSDong Aisheng 		imx_data->is_ddr = 1;
1106602519b2SDong Aisheng 		if (boarddata->delay_line) {
1107602519b2SDong Aisheng 			u32 v;
1108602519b2SDong Aisheng 			v = boarddata->delay_line <<
1109602519b2SDong Aisheng 				ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1110602519b2SDong Aisheng 				(1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1111602519b2SDong Aisheng 			if (is_imx53_esdhc(imx_data))
1112602519b2SDong Aisheng 				v <<= 1;
1113602519b2SDong Aisheng 			writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1114602519b2SDong Aisheng 		}
1115ad93220dSDong Aisheng 		break;
111628b07674SHaibo Chen 	case MMC_TIMING_MMC_HS400:
111728b07674SHaibo Chen 		m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
111828b07674SHaibo Chen 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
111928b07674SHaibo Chen 		imx_data->is_ddr = 1;
11207ac6da26SDong Aisheng 		/* update clock after enable DDR for strobe DLL lock */
11217ac6da26SDong Aisheng 		host->ops->set_clock(host, host->clock);
112228b07674SHaibo Chen 		esdhc_set_strobe_dll(host);
112328b07674SHaibo Chen 		break;
1124d9370424SHaibo Chen 	case MMC_TIMING_LEGACY:
1125d9370424SHaibo Chen 	default:
1126d9370424SHaibo Chen 		esdhc_reset_tuning(host);
1127d9370424SHaibo Chen 		break;
1128ad93220dSDong Aisheng 	}
1129ad93220dSDong Aisheng 
1130850a29b8SRussell King 	esdhc_change_pinstate(host, timing);
1131ad93220dSDong Aisheng }
1132ad93220dSDong Aisheng 
11330718e59aSRussell King static void esdhc_reset(struct sdhci_host *host, u8 mask)
11340718e59aSRussell King {
11350718e59aSRussell King 	sdhci_reset(host, mask);
11360718e59aSRussell King 
11370718e59aSRussell King 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
11380718e59aSRussell King 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
11390718e59aSRussell King }
11400718e59aSRussell King 
114110fd0ad9SAisheng Dong static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
114210fd0ad9SAisheng Dong {
114310fd0ad9SAisheng Dong 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1144070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
114510fd0ad9SAisheng Dong 
1146d04f8d5bSBenoît Thébaudeau 	/* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
11472fb0b02bSHaibo Chen 	return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
114810fd0ad9SAisheng Dong }
114910fd0ad9SAisheng Dong 
1150e33eb8e2SAisheng Dong static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1151e33eb8e2SAisheng Dong {
1152e33eb8e2SAisheng Dong 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1153070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1154e33eb8e2SAisheng Dong 
1155e33eb8e2SAisheng Dong 	/* use maximum timeout counter */
1156a215186dSHaibo Chen 	esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1157a215186dSHaibo Chen 			esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1158e33eb8e2SAisheng Dong 			SDHCI_TIMEOUT_CONTROL);
1159e33eb8e2SAisheng Dong }
1160e33eb8e2SAisheng Dong 
1161bb6e3581SBOUGH CHEN static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1162bb6e3581SBOUGH CHEN {
1163bb6e3581SBOUGH CHEN 	int cmd_error = 0;
1164bb6e3581SBOUGH CHEN 	int data_error = 0;
1165bb6e3581SBOUGH CHEN 
1166bb6e3581SBOUGH CHEN 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1167bb6e3581SBOUGH CHEN 		return intmask;
1168bb6e3581SBOUGH CHEN 
1169bb6e3581SBOUGH CHEN 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1170bb6e3581SBOUGH CHEN 
1171bb6e3581SBOUGH CHEN 	return 0;
1172bb6e3581SBOUGH CHEN }
1173bb6e3581SBOUGH CHEN 
11746e9fd28eSDong Aisheng static struct sdhci_ops sdhci_esdhc_ops = {
1175e149860dSRichard Zhu 	.read_l = esdhc_readl_le,
11760c6d49ceSWolfram Sang 	.read_w = esdhc_readw_le,
117777da3da0SAaron Brice 	.read_b = esdhc_readb_le,
1178e149860dSRichard Zhu 	.write_l = esdhc_writel_le,
11790c6d49ceSWolfram Sang 	.write_w = esdhc_writew_le,
11800c6d49ceSWolfram Sang 	.write_b = esdhc_writeb_le,
11818ba9580aSLucas Stach 	.set_clock = esdhc_pltfm_set_clock,
11820ddf03c9SLucas Stach 	.get_max_clock = esdhc_pltfm_get_max_clock,
11830c6d49ceSWolfram Sang 	.get_min_clock = esdhc_pltfm_get_min_clock,
118410fd0ad9SAisheng Dong 	.get_max_timeout_count = esdhc_get_max_timeout_count,
1185913413c3SShawn Guo 	.get_ro = esdhc_pltfm_get_ro,
1186e33eb8e2SAisheng Dong 	.set_timeout = esdhc_set_timeout,
11872317f56cSRussell King 	.set_bus_width = esdhc_pltfm_set_bus_width,
1188ad93220dSDong Aisheng 	.set_uhs_signaling = esdhc_set_uhs_signaling,
11890718e59aSRussell King 	.reset = esdhc_reset,
1190bb6e3581SBOUGH CHEN 	.irq = esdhc_cqhci_irq,
11910c6d49ceSWolfram Sang };
11920c6d49ceSWolfram Sang 
11931db5eebfSLars-Peter Clausen static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
119497e4ba6aSRichard Zhu 	.quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
119597e4ba6aSRichard Zhu 			| SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
119697e4ba6aSRichard Zhu 			| SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
119785d6509dSShawn Guo 			| SDHCI_QUIRK_BROKEN_CARD_DETECTION,
119885d6509dSShawn Guo 	.ops = &sdhci_esdhc_ops,
119985d6509dSShawn Guo };
120085d6509dSShawn Guo 
1201f3f5cf3dSDong Aisheng static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1202f3f5cf3dSDong Aisheng {
1203f3f5cf3dSDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1204f3f5cf3dSDong Aisheng 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
12052b16cf32SDong Aisheng 	int tmp;
1206f3f5cf3dSDong Aisheng 
1207f3f5cf3dSDong Aisheng 	if (esdhc_is_usdhc(imx_data)) {
1208f3f5cf3dSDong Aisheng 		/*
1209f3f5cf3dSDong Aisheng 		 * The imx6q ROM code will change the default watermark
1210f3f5cf3dSDong Aisheng 		 * level setting to something insane.  Change it back here.
1211f3f5cf3dSDong Aisheng 		 */
1212f3f5cf3dSDong Aisheng 		writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1213f3f5cf3dSDong Aisheng 
1214f3f5cf3dSDong Aisheng 		/*
1215f3f5cf3dSDong Aisheng 		 * ROM code will change the bit burst_length_enable setting
1216d04f8d5bSBenoît Thébaudeau 		 * to zero if this usdhc is chosen to boot system. Change
1217f3f5cf3dSDong Aisheng 		 * it back here, otherwise it will impact the performance a
1218f3f5cf3dSDong Aisheng 		 * lot. This bit is used to enable/disable the burst length
1219d04f8d5bSBenoît Thébaudeau 		 * for the external AHB2AXI bridge. It's useful especially
1220f3f5cf3dSDong Aisheng 		 * for INCR transfer because without burst length indicator,
1221f3f5cf3dSDong Aisheng 		 * the AHB2AXI bridge does not know the burst length in
1222f3f5cf3dSDong Aisheng 		 * advance. And without burst length indicator, AHB INCR
1223f3f5cf3dSDong Aisheng 		 * transfer can only be converted to singles on the AXI side.
1224f3f5cf3dSDong Aisheng 		 */
1225f3f5cf3dSDong Aisheng 		writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1226f3f5cf3dSDong Aisheng 			| ESDHC_BURST_LEN_EN_INCR,
1227f3f5cf3dSDong Aisheng 			host->ioaddr + SDHCI_HOST_CONTROL);
1228e30be063SBOUGH CHEN 
1229f3f5cf3dSDong Aisheng 		/*
1230d04f8d5bSBenoît Thébaudeau 		 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1231f3f5cf3dSDong Aisheng 		 * TO1.1, it's harmless for MX6SL
1232f3f5cf3dSDong Aisheng 		 */
1233e30be063SBOUGH CHEN 		writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1234f3f5cf3dSDong Aisheng 			host->ioaddr + 0x6c);
1235f3f5cf3dSDong Aisheng 
1236f3f5cf3dSDong Aisheng 		/* disable DLL_CTRL delay line settings */
1237f3f5cf3dSDong Aisheng 		writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
12382b16cf32SDong Aisheng 
1239bcdb5301SBOUGH CHEN 		/*
1240bcdb5301SBOUGH CHEN 		 * For the case of command with busy, if set the bit
1241bcdb5301SBOUGH CHEN 		 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1242bcdb5301SBOUGH CHEN 		 * transfer complete interrupt when busy is deasserted.
1243bcdb5301SBOUGH CHEN 		 * When CQHCI use DCMD to send a CMD need R1b respons,
1244bcdb5301SBOUGH CHEN 		 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1245bcdb5301SBOUGH CHEN 		 * otherwise DCMD will always meet timeout waiting for
1246bcdb5301SBOUGH CHEN 		 * hardware interrupt issue.
1247bcdb5301SBOUGH CHEN 		 */
1248bcdb5301SBOUGH CHEN 		if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1249bcdb5301SBOUGH CHEN 			tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1250bcdb5301SBOUGH CHEN 			tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1251bcdb5301SBOUGH CHEN 			writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1252bcdb5301SBOUGH CHEN 
1253bcdb5301SBOUGH CHEN 			host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1254bcdb5301SBOUGH CHEN 		}
1255bcdb5301SBOUGH CHEN 
12562b16cf32SDong Aisheng 		if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
12572b16cf32SDong Aisheng 			tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
12582b16cf32SDong Aisheng 			tmp |= ESDHC_STD_TUNING_EN |
12592b16cf32SDong Aisheng 				ESDHC_TUNING_START_TAP_DEFAULT;
12602b16cf32SDong Aisheng 			if (imx_data->boarddata.tuning_start_tap) {
12612b16cf32SDong Aisheng 				tmp &= ~ESDHC_TUNING_START_TAP_MASK;
12622b16cf32SDong Aisheng 				tmp |= imx_data->boarddata.tuning_start_tap;
12632b16cf32SDong Aisheng 			}
12642b16cf32SDong Aisheng 
12652b16cf32SDong Aisheng 			if (imx_data->boarddata.tuning_step) {
12662b16cf32SDong Aisheng 				tmp &= ~ESDHC_TUNING_STEP_MASK;
12672b16cf32SDong Aisheng 				tmp |= imx_data->boarddata.tuning_step
12682b16cf32SDong Aisheng 					<< ESDHC_TUNING_STEP_SHIFT;
12692b16cf32SDong Aisheng 			}
12702b16cf32SDong Aisheng 			writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1271a98c557eSBOUGH CHEN 		} else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1272a98c557eSBOUGH CHEN 			/*
1273a98c557eSBOUGH CHEN 			 * ESDHC_STD_TUNING_EN may be configed in bootloader
1274a98c557eSBOUGH CHEN 			 * or ROM code, so clear this bit here to make sure
1275a98c557eSBOUGH CHEN 			 * the manual tuning can work.
1276a98c557eSBOUGH CHEN 			 */
1277a98c557eSBOUGH CHEN 			tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1278a98c557eSBOUGH CHEN 			tmp &= ~ESDHC_STD_TUNING_EN;
1279a98c557eSBOUGH CHEN 			writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
12802b16cf32SDong Aisheng 		}
1281f3f5cf3dSDong Aisheng 	}
1282f3f5cf3dSDong Aisheng }
1283f3f5cf3dSDong Aisheng 
1284bb6e3581SBOUGH CHEN static void esdhc_cqe_enable(struct mmc_host *mmc)
1285bb6e3581SBOUGH CHEN {
1286bb6e3581SBOUGH CHEN 	struct sdhci_host *host = mmc_priv(mmc);
128785236d2bSBOUGH CHEN 	struct cqhci_host *cq_host = mmc->cqe_private;
1288bb6e3581SBOUGH CHEN 	u32 reg;
1289bb6e3581SBOUGH CHEN 	u16 mode;
1290bb6e3581SBOUGH CHEN 	int count = 10;
1291bb6e3581SBOUGH CHEN 
1292bb6e3581SBOUGH CHEN 	/*
1293bb6e3581SBOUGH CHEN 	 * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1294bb6e3581SBOUGH CHEN 	 * the case after tuning, so ensure the buffer is drained.
1295bb6e3581SBOUGH CHEN 	 */
1296bb6e3581SBOUGH CHEN 	reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1297bb6e3581SBOUGH CHEN 	while (reg & SDHCI_DATA_AVAILABLE) {
1298bb6e3581SBOUGH CHEN 		sdhci_readl(host, SDHCI_BUFFER);
1299bb6e3581SBOUGH CHEN 		reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1300bb6e3581SBOUGH CHEN 		if (count-- == 0) {
1301bb6e3581SBOUGH CHEN 			dev_warn(mmc_dev(host->mmc),
1302bb6e3581SBOUGH CHEN 				"CQE may get stuck because the Buffer Read Enable bit is set\n");
1303bb6e3581SBOUGH CHEN 			break;
1304bb6e3581SBOUGH CHEN 		}
1305bb6e3581SBOUGH CHEN 		mdelay(1);
1306bb6e3581SBOUGH CHEN 	}
1307bb6e3581SBOUGH CHEN 
1308bb6e3581SBOUGH CHEN 	/*
1309bb6e3581SBOUGH CHEN 	 * Runtime resume will reset the entire host controller, which
1310bb6e3581SBOUGH CHEN 	 * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1311bb6e3581SBOUGH CHEN 	 * Here set DMAEN and BCEN when enable CMDQ.
1312bb6e3581SBOUGH CHEN 	 */
1313bb6e3581SBOUGH CHEN 	mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1314bb6e3581SBOUGH CHEN 	if (host->flags & SDHCI_REQ_USE_DMA)
1315bb6e3581SBOUGH CHEN 		mode |= SDHCI_TRNS_DMA;
1316bb6e3581SBOUGH CHEN 	if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1317bb6e3581SBOUGH CHEN 		mode |= SDHCI_TRNS_BLK_CNT_EN;
1318bb6e3581SBOUGH CHEN 	sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1319bb6e3581SBOUGH CHEN 
132085236d2bSBOUGH CHEN 	/*
132185236d2bSBOUGH CHEN 	 * Though Runtime resume reset the entire host controller,
132285236d2bSBOUGH CHEN 	 * but do not impact the CQHCI side, need to clear the
132385236d2bSBOUGH CHEN 	 * HALT bit, avoid CQHCI stuck in the first request when
132485236d2bSBOUGH CHEN 	 * system resume back.
132585236d2bSBOUGH CHEN 	 */
132685236d2bSBOUGH CHEN 	cqhci_writel(cq_host, 0, CQHCI_CTL);
132785236d2bSBOUGH CHEN 	if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT)
132885236d2bSBOUGH CHEN 		dev_err(mmc_dev(host->mmc),
132985236d2bSBOUGH CHEN 			"failed to exit halt state when enable CQE\n");
133085236d2bSBOUGH CHEN 
133185236d2bSBOUGH CHEN 
1332bb6e3581SBOUGH CHEN 	sdhci_cqe_enable(mmc);
1333bb6e3581SBOUGH CHEN }
1334bb6e3581SBOUGH CHEN 
1335bb6e3581SBOUGH CHEN static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1336bb6e3581SBOUGH CHEN {
1337bb6e3581SBOUGH CHEN 	sdhci_dumpregs(mmc_priv(mmc));
1338bb6e3581SBOUGH CHEN }
1339bb6e3581SBOUGH CHEN 
1340bb6e3581SBOUGH CHEN static const struct cqhci_host_ops esdhc_cqhci_ops = {
1341bb6e3581SBOUGH CHEN 	.enable		= esdhc_cqe_enable,
1342bb6e3581SBOUGH CHEN 	.disable	= sdhci_cqe_disable,
1343bb6e3581SBOUGH CHEN 	.dumpregs	= esdhc_sdhci_dumpregs,
1344bb6e3581SBOUGH CHEN };
1345bb6e3581SBOUGH CHEN 
1346abfafc2dSShawn Guo #ifdef CONFIG_OF
1347c3be1efdSBill Pemberton static int
1348abfafc2dSShawn Guo sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
134907bf2b54SSascha Hauer 			 struct sdhci_host *host,
135091fa4252SDong Aisheng 			 struct pltfm_imx_data *imx_data)
1351abfafc2dSShawn Guo {
1352abfafc2dSShawn Guo 	struct device_node *np = pdev->dev.of_node;
135391fa4252SDong Aisheng 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
13544800e87aSDong Aisheng 	int ret;
1355abfafc2dSShawn Guo 
1356abfafc2dSShawn Guo 	if (of_get_property(np, "fsl,wp-controller", NULL))
1357abfafc2dSShawn Guo 		boarddata->wp_type = ESDHC_WP_CONTROLLER;
1358abfafc2dSShawn Guo 
135974ff81e1SLinus Walleij 	/*
136074ff81e1SLinus Walleij 	 * If we have this property, then activate WP check.
136174ff81e1SLinus Walleij 	 * Retrieveing and requesting the actual WP GPIO will happen
136274ff81e1SLinus Walleij 	 * in the call to mmc_of_parse().
136374ff81e1SLinus Walleij 	 */
136474ff81e1SLinus Walleij 	if (of_property_read_bool(np, "wp-gpios"))
1365abfafc2dSShawn Guo 		boarddata->wp_type = ESDHC_WP_GPIO;
1366abfafc2dSShawn Guo 
1367d407e30bSHaibo Chen 	of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1368d87fc966SDong Aisheng 	of_property_read_u32(np, "fsl,tuning-start-tap",
1369d87fc966SDong Aisheng 			     &boarddata->tuning_start_tap);
1370d407e30bSHaibo Chen 
13715bd2acdcSHaibo Chen 	of_property_read_u32(np, "fsl,strobe-dll-delay-target",
13725bd2acdcSHaibo Chen 				&boarddata->strobe_dll_delay_target);
1373ad93220dSDong Aisheng 	if (of_find_property(np, "no-1-8-v", NULL))
137486f495c5SStefan Agner 		host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1375ad93220dSDong Aisheng 
1376602519b2SDong Aisheng 	if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1377602519b2SDong Aisheng 		boarddata->delay_line = 0;
1378602519b2SDong Aisheng 
137907bf2b54SSascha Hauer 	mmc_of_parse_voltage(np, &host->ocr_mask);
138007bf2b54SSascha Hauer 
13812480b720SUlf Hansson 	if (esdhc_is_usdhc(imx_data)) {
138291fa4252SDong Aisheng 		imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
138391fa4252SDong Aisheng 						ESDHC_PINCTRL_STATE_100MHZ);
138491fa4252SDong Aisheng 		imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
138591fa4252SDong Aisheng 						ESDHC_PINCTRL_STATE_200MHZ);
138691fa4252SDong Aisheng 	}
138791fa4252SDong Aisheng 
138815064119SFabio Estevam 	/* call to generic mmc_of_parse to support additional capabilities */
13894800e87aSDong Aisheng 	ret = mmc_of_parse(host->mmc);
13904800e87aSDong Aisheng 	if (ret)
13914800e87aSDong Aisheng 		return ret;
13924800e87aSDong Aisheng 
1393287980e4SArnd Bergmann 	if (mmc_gpio_get_cd(host->mmc) >= 0)
13944800e87aSDong Aisheng 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
13954800e87aSDong Aisheng 
13964800e87aSDong Aisheng 	return 0;
1397abfafc2dSShawn Guo }
1398abfafc2dSShawn Guo #else
1399abfafc2dSShawn Guo static inline int
1400abfafc2dSShawn Guo sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
140107bf2b54SSascha Hauer 			 struct sdhci_host *host,
140291fa4252SDong Aisheng 			 struct pltfm_imx_data *imx_data)
1403abfafc2dSShawn Guo {
1404abfafc2dSShawn Guo 	return -ENODEV;
1405abfafc2dSShawn Guo }
1406abfafc2dSShawn Guo #endif
1407abfafc2dSShawn Guo 
140891fa4252SDong Aisheng static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
140991fa4252SDong Aisheng 			 struct sdhci_host *host,
141091fa4252SDong Aisheng 			 struct pltfm_imx_data *imx_data)
141191fa4252SDong Aisheng {
141291fa4252SDong Aisheng 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
141391fa4252SDong Aisheng 	int err;
141491fa4252SDong Aisheng 
141591fa4252SDong Aisheng 	if (!host->mmc->parent->platform_data) {
141691fa4252SDong Aisheng 		dev_err(mmc_dev(host->mmc), "no board data!\n");
141791fa4252SDong Aisheng 		return -EINVAL;
141891fa4252SDong Aisheng 	}
141991fa4252SDong Aisheng 
142091fa4252SDong Aisheng 	imx_data->boarddata = *((struct esdhc_platform_data *)
142191fa4252SDong Aisheng 				host->mmc->parent->platform_data);
142291fa4252SDong Aisheng 	/* write_protect */
142391fa4252SDong Aisheng 	if (boarddata->wp_type == ESDHC_WP_GPIO) {
14249073d10bSMichał Mirosław 		host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
14259073d10bSMichał Mirosław 
1426d0052ad9SMichał Mirosław 		err = mmc_gpiod_request_ro(host->mmc, "wp", 0, 0);
142791fa4252SDong Aisheng 		if (err) {
142891fa4252SDong Aisheng 			dev_err(mmc_dev(host->mmc),
142991fa4252SDong Aisheng 				"failed to request write-protect gpio!\n");
143091fa4252SDong Aisheng 			return err;
143191fa4252SDong Aisheng 		}
143291fa4252SDong Aisheng 	}
143391fa4252SDong Aisheng 
143491fa4252SDong Aisheng 	/* card_detect */
143591fa4252SDong Aisheng 	switch (boarddata->cd_type) {
143691fa4252SDong Aisheng 	case ESDHC_CD_GPIO:
1437d0052ad9SMichał Mirosław 		err = mmc_gpiod_request_cd(host->mmc, "cd", 0, false, 0);
143891fa4252SDong Aisheng 		if (err) {
143991fa4252SDong Aisheng 			dev_err(mmc_dev(host->mmc),
144091fa4252SDong Aisheng 				"failed to request card-detect gpio!\n");
144191fa4252SDong Aisheng 			return err;
144291fa4252SDong Aisheng 		}
144391fa4252SDong Aisheng 		/* fall through */
144491fa4252SDong Aisheng 
144591fa4252SDong Aisheng 	case ESDHC_CD_CONTROLLER:
144691fa4252SDong Aisheng 		/* we have a working card_detect back */
144791fa4252SDong Aisheng 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
144891fa4252SDong Aisheng 		break;
144991fa4252SDong Aisheng 
145091fa4252SDong Aisheng 	case ESDHC_CD_PERMANENT:
145191fa4252SDong Aisheng 		host->mmc->caps |= MMC_CAP_NONREMOVABLE;
145291fa4252SDong Aisheng 		break;
145391fa4252SDong Aisheng 
145491fa4252SDong Aisheng 	case ESDHC_CD_NONE:
145591fa4252SDong Aisheng 		break;
145691fa4252SDong Aisheng 	}
145791fa4252SDong Aisheng 
145891fa4252SDong Aisheng 	switch (boarddata->max_bus_width) {
145991fa4252SDong Aisheng 	case 8:
146091fa4252SDong Aisheng 		host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
146191fa4252SDong Aisheng 		break;
146291fa4252SDong Aisheng 	case 4:
146391fa4252SDong Aisheng 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
146491fa4252SDong Aisheng 		break;
146591fa4252SDong Aisheng 	case 1:
146691fa4252SDong Aisheng 	default:
146791fa4252SDong Aisheng 		host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
146891fa4252SDong Aisheng 		break;
146991fa4252SDong Aisheng 	}
147091fa4252SDong Aisheng 
147191fa4252SDong Aisheng 	return 0;
147291fa4252SDong Aisheng }
147391fa4252SDong Aisheng 
1474c3be1efdSBill Pemberton static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
147595f25efeSWolfram Sang {
1476abfafc2dSShawn Guo 	const struct of_device_id *of_id =
1477abfafc2dSShawn Guo 			of_match_device(imx_esdhc_dt_ids, &pdev->dev);
147885d6509dSShawn Guo 	struct sdhci_pltfm_host *pltfm_host;
147985d6509dSShawn Guo 	struct sdhci_host *host;
1480bb6e3581SBOUGH CHEN 	struct cqhci_host *cq_host;
14810c6d49ceSWolfram Sang 	int err;
1482e149860dSRichard Zhu 	struct pltfm_imx_data *imx_data;
148395f25efeSWolfram Sang 
1484070e6d3fSJisheng Zhang 	host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1485070e6d3fSJisheng Zhang 				sizeof(*imx_data));
148685d6509dSShawn Guo 	if (IS_ERR(host))
148785d6509dSShawn Guo 		return PTR_ERR(host);
148885d6509dSShawn Guo 
148985d6509dSShawn Guo 	pltfm_host = sdhci_priv(host);
149085d6509dSShawn Guo 
1491070e6d3fSJisheng Zhang 	imx_data = sdhci_pltfm_priv(pltfm_host);
149257ed3314SShawn Guo 
1493f47c4bbfSShawn Guo 	imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
14943770ee8fSShawn Guo 						  pdev->id_entry->driver_data;
149585d6509dSShawn Guo 
14961c4989b0SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
14971c4989b0SBOUGH CHEN 		pm_qos_add_request(&imx_data->pm_qos_req,
14981c4989b0SBOUGH CHEN 			PM_QOS_CPU_DMA_LATENCY, 0);
14991c4989b0SBOUGH CHEN 
150052dac615SSascha Hauer 	imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
150152dac615SSascha Hauer 	if (IS_ERR(imx_data->clk_ipg)) {
150252dac615SSascha Hauer 		err = PTR_ERR(imx_data->clk_ipg);
1503e3af31c6SShawn Guo 		goto free_sdhci;
150495f25efeSWolfram Sang 	}
150552dac615SSascha Hauer 
150652dac615SSascha Hauer 	imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
150752dac615SSascha Hauer 	if (IS_ERR(imx_data->clk_ahb)) {
150852dac615SSascha Hauer 		err = PTR_ERR(imx_data->clk_ahb);
1509e3af31c6SShawn Guo 		goto free_sdhci;
151052dac615SSascha Hauer 	}
151152dac615SSascha Hauer 
151252dac615SSascha Hauer 	imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
151352dac615SSascha Hauer 	if (IS_ERR(imx_data->clk_per)) {
151452dac615SSascha Hauer 		err = PTR_ERR(imx_data->clk_per);
1515e3af31c6SShawn Guo 		goto free_sdhci;
151652dac615SSascha Hauer 	}
151752dac615SSascha Hauer 
151852dac615SSascha Hauer 	pltfm_host->clk = imx_data->clk_per;
1519a974862fSDong Aisheng 	pltfm_host->clock = clk_get_rate(pltfm_host->clk);
152017b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_per);
152117b1eb7fSFabio Estevam 	if (err)
152217b1eb7fSFabio Estevam 		goto free_sdhci;
152317b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_ipg);
152417b1eb7fSFabio Estevam 	if (err)
152517b1eb7fSFabio Estevam 		goto disable_per_clk;
152617b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_ahb);
152717b1eb7fSFabio Estevam 	if (err)
152817b1eb7fSFabio Estevam 		goto disable_ipg_clk;
152995f25efeSWolfram Sang 
1530ad93220dSDong Aisheng 	imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1531e62d8b8fSDong Aisheng 	if (IS_ERR(imx_data->pinctrl)) {
1532e62d8b8fSDong Aisheng 		err = PTR_ERR(imx_data->pinctrl);
1533b62eee9fSHaibo Chen 		dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1534e62d8b8fSDong Aisheng 	}
1535e62d8b8fSDong Aisheng 
153669ed60e0SDong Aisheng 	if (esdhc_is_usdhc(imx_data)) {
153769ed60e0SDong Aisheng 		host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
153809c8192bSStefan Agner 		host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
15394245afffSDong Aisheng 		if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
15404245afffSDong Aisheng 			host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1541a75dcbf4SDong Aisheng 
1542a75dcbf4SDong Aisheng 		/* clear tuning bits in case ROM has set it already */
1543a75dcbf4SDong Aisheng 		writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1544869f8a69SAdrian Hunter 		writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1545a75dcbf4SDong Aisheng 		writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1546de3e1dd0SBOUGH CHEN 
1547de3e1dd0SBOUGH CHEN 		/*
1548de3e1dd0SBOUGH CHEN 		 * Link usdhc specific mmc_host_ops execute_tuning function,
1549de3e1dd0SBOUGH CHEN 		 * to replace the standard one in sdhci_ops.
1550de3e1dd0SBOUGH CHEN 		 */
1551de3e1dd0SBOUGH CHEN 		host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
155269ed60e0SDong Aisheng 	}
1553f750ba9bSShawn Guo 
15546e9fd28eSDong Aisheng 	if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
15556e9fd28eSDong Aisheng 		sdhci_esdhc_ops.platform_execute_tuning =
15566e9fd28eSDong Aisheng 					esdhc_executing_tuning;
15578b2bb0adSDong Aisheng 
155818094430SDong Aisheng 	if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
155918094430SDong Aisheng 		host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
156018094430SDong Aisheng 
156128b07674SHaibo Chen 	if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
156228b07674SHaibo Chen 		host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
156328b07674SHaibo Chen 
1564029e2476SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1565029e2476SBOUGH CHEN 		host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1566029e2476SBOUGH CHEN 		host->mmc_host_ops.hs400_enhanced_strobe =
1567029e2476SBOUGH CHEN 					esdhc_hs400_enhanced_strobe;
1568029e2476SBOUGH CHEN 	}
1569029e2476SBOUGH CHEN 
1570bb6e3581SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1571bcdb5301SBOUGH CHEN 		host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1572bb6e3581SBOUGH CHEN 		cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
15739a633f3bSWei Yongjun 		if (!cq_host) {
15749a633f3bSWei Yongjun 			err = -ENOMEM;
1575bb6e3581SBOUGH CHEN 			goto disable_ahb_clk;
1576bb6e3581SBOUGH CHEN 		}
1577bb6e3581SBOUGH CHEN 
1578bb6e3581SBOUGH CHEN 		cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1579bb6e3581SBOUGH CHEN 		cq_host->ops = &esdhc_cqhci_ops;
1580bb6e3581SBOUGH CHEN 
1581bb6e3581SBOUGH CHEN 		err = cqhci_init(cq_host, host->mmc, false);
1582bb6e3581SBOUGH CHEN 		if (err)
1583bb6e3581SBOUGH CHEN 			goto disable_ahb_clk;
1584bb6e3581SBOUGH CHEN 	}
1585bb6e3581SBOUGH CHEN 
158691fa4252SDong Aisheng 	if (of_id)
158791fa4252SDong Aisheng 		err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
158891fa4252SDong Aisheng 	else
158991fa4252SDong Aisheng 		err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
159091fa4252SDong Aisheng 	if (err)
159117b1eb7fSFabio Estevam 		goto disable_ahb_clk;
1592ad93220dSDong Aisheng 
1593d00ab101SBOUGH CHEN 	host->tuning_delay = 1;
1594d00ab101SBOUGH CHEN 
1595f3f5cf3dSDong Aisheng 	sdhci_esdhc_imx_hwinit(host);
1596f3f5cf3dSDong Aisheng 
159785d6509dSShawn Guo 	err = sdhci_add_host(host);
159885d6509dSShawn Guo 	if (err)
159917b1eb7fSFabio Estevam 		goto disable_ahb_clk;
160085d6509dSShawn Guo 
160189d7e5c1SDong Aisheng 	pm_runtime_set_active(&pdev->dev);
160289d7e5c1SDong Aisheng 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
160389d7e5c1SDong Aisheng 	pm_runtime_use_autosuspend(&pdev->dev);
160489d7e5c1SDong Aisheng 	pm_suspend_ignore_children(&pdev->dev, 1);
160577903c01SUlf Hansson 	pm_runtime_enable(&pdev->dev);
160689d7e5c1SDong Aisheng 
16077e29c306SWolfram Sang 	return 0;
16087e29c306SWolfram Sang 
160917b1eb7fSFabio Estevam disable_ahb_clk:
161052dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_ahb);
161117b1eb7fSFabio Estevam disable_ipg_clk:
161217b1eb7fSFabio Estevam 	clk_disable_unprepare(imx_data->clk_ipg);
161317b1eb7fSFabio Estevam disable_per_clk:
161417b1eb7fSFabio Estevam 	clk_disable_unprepare(imx_data->clk_per);
1615e3af31c6SShawn Guo free_sdhci:
16161c4989b0SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
16171c4989b0SBOUGH CHEN 		pm_qos_remove_request(&imx_data->pm_qos_req);
161885d6509dSShawn Guo 	sdhci_pltfm_free(pdev);
161985d6509dSShawn Guo 	return err;
162095f25efeSWolfram Sang }
162195f25efeSWolfram Sang 
16226e0ee714SBill Pemberton static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
162395f25efeSWolfram Sang {
162485d6509dSShawn Guo 	struct sdhci_host *host = platform_get_drvdata(pdev);
162595f25efeSWolfram Sang 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1626070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
162785d6509dSShawn Guo 	int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
162885d6509dSShawn Guo 
16290b414368SUlf Hansson 	pm_runtime_get_sync(&pdev->dev);
16300b414368SUlf Hansson 	pm_runtime_disable(&pdev->dev);
16310b414368SUlf Hansson 	pm_runtime_put_noidle(&pdev->dev);
16320b414368SUlf Hansson 
163385d6509dSShawn Guo 	sdhci_remove_host(host, dead);
16340c6d49ceSWolfram Sang 
163552dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_per);
163652dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_ipg);
163752dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_ahb);
163852dac615SSascha Hauer 
16391c4989b0SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
16401c4989b0SBOUGH CHEN 		pm_qos_remove_request(&imx_data->pm_qos_req);
16411c4989b0SBOUGH CHEN 
164285d6509dSShawn Guo 	sdhci_pltfm_free(pdev);
164385d6509dSShawn Guo 
164485d6509dSShawn Guo 	return 0;
164595f25efeSWolfram Sang }
164695f25efeSWolfram Sang 
16472788ed42SUlf Hansson #ifdef CONFIG_PM_SLEEP
164804143fbaSDong Aisheng static int sdhci_esdhc_suspend(struct device *dev)
164904143fbaSDong Aisheng {
16503e3274abSUlf Hansson 	struct sdhci_host *host = dev_get_drvdata(dev);
1651a26a4f1bSHaibo Chen 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1652a26a4f1bSHaibo Chen 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1653bb6e3581SBOUGH CHEN 	int ret;
1654bb6e3581SBOUGH CHEN 
1655bb6e3581SBOUGH CHEN 	if (host->mmc->caps2 & MMC_CAP2_CQE) {
1656bb6e3581SBOUGH CHEN 		ret = cqhci_suspend(host->mmc);
1657bb6e3581SBOUGH CHEN 		if (ret)
1658bb6e3581SBOUGH CHEN 			return ret;
1659bb6e3581SBOUGH CHEN 	}
16603e3274abSUlf Hansson 
1661a26a4f1bSHaibo Chen 	if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1662a26a4f1bSHaibo Chen 		(host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1663a26a4f1bSHaibo Chen 		mmc_retune_timer_stop(host->mmc);
1664a26a4f1bSHaibo Chen 		mmc_retune_needed(host->mmc);
1665a26a4f1bSHaibo Chen 	}
1666a26a4f1bSHaibo Chen 
1667d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1668d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
1669d38dcad4SAdrian Hunter 
16703e3274abSUlf Hansson 	return sdhci_suspend_host(host);
167104143fbaSDong Aisheng }
167204143fbaSDong Aisheng 
167304143fbaSDong Aisheng static int sdhci_esdhc_resume(struct device *dev)
167404143fbaSDong Aisheng {
1675cc17e129SDong Aisheng 	struct sdhci_host *host = dev_get_drvdata(dev);
1676bb6e3581SBOUGH CHEN 	int ret;
1677cc17e129SDong Aisheng 
167819dbfdd3SDong Aisheng 	/* re-initialize hw state in case it's lost in low power mode */
167919dbfdd3SDong Aisheng 	sdhci_esdhc_imx_hwinit(host);
1680cc17e129SDong Aisheng 
1681bb6e3581SBOUGH CHEN 	ret = sdhci_resume_host(host);
1682bb6e3581SBOUGH CHEN 	if (ret)
1683bb6e3581SBOUGH CHEN 		return ret;
1684bb6e3581SBOUGH CHEN 
1685bb6e3581SBOUGH CHEN 	if (host->mmc->caps2 & MMC_CAP2_CQE)
1686bb6e3581SBOUGH CHEN 		ret = cqhci_resume(host->mmc);
1687bb6e3581SBOUGH CHEN 
1688bb6e3581SBOUGH CHEN 	return ret;
168904143fbaSDong Aisheng }
16902788ed42SUlf Hansson #endif
169104143fbaSDong Aisheng 
16922788ed42SUlf Hansson #ifdef CONFIG_PM
169389d7e5c1SDong Aisheng static int sdhci_esdhc_runtime_suspend(struct device *dev)
169489d7e5c1SDong Aisheng {
169589d7e5c1SDong Aisheng 	struct sdhci_host *host = dev_get_drvdata(dev);
169689d7e5c1SDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1697070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
169889d7e5c1SDong Aisheng 	int ret;
169989d7e5c1SDong Aisheng 
1700bb6e3581SBOUGH CHEN 	if (host->mmc->caps2 & MMC_CAP2_CQE) {
1701bb6e3581SBOUGH CHEN 		ret = cqhci_suspend(host->mmc);
1702bb6e3581SBOUGH CHEN 		if (ret)
1703bb6e3581SBOUGH CHEN 			return ret;
1704bb6e3581SBOUGH CHEN 	}
1705bb6e3581SBOUGH CHEN 
170689d7e5c1SDong Aisheng 	ret = sdhci_runtime_suspend_host(host);
1707371d39faSMichael Trimarchi 	if (ret)
1708371d39faSMichael Trimarchi 		return ret;
170989d7e5c1SDong Aisheng 
1710d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1711d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
1712d38dcad4SAdrian Hunter 
17133602785bSMichael Trimarchi 	imx_data->actual_clock = host->mmc->actual_clock;
17143602785bSMichael Trimarchi 	esdhc_pltfm_set_clock(host, 0);
171589d7e5c1SDong Aisheng 	clk_disable_unprepare(imx_data->clk_per);
171689d7e5c1SDong Aisheng 	clk_disable_unprepare(imx_data->clk_ipg);
171789d7e5c1SDong Aisheng 	clk_disable_unprepare(imx_data->clk_ahb);
171889d7e5c1SDong Aisheng 
17191c4989b0SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
17201c4989b0SBOUGH CHEN 		pm_qos_remove_request(&imx_data->pm_qos_req);
17211c4989b0SBOUGH CHEN 
172289d7e5c1SDong Aisheng 	return ret;
172389d7e5c1SDong Aisheng }
172489d7e5c1SDong Aisheng 
172589d7e5c1SDong Aisheng static int sdhci_esdhc_runtime_resume(struct device *dev)
172689d7e5c1SDong Aisheng {
172789d7e5c1SDong Aisheng 	struct sdhci_host *host = dev_get_drvdata(dev);
172889d7e5c1SDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1729070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
173017b1eb7fSFabio Estevam 	int err;
173189d7e5c1SDong Aisheng 
17321c4989b0SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
17331c4989b0SBOUGH CHEN 		pm_qos_add_request(&imx_data->pm_qos_req,
17341c4989b0SBOUGH CHEN 			PM_QOS_CPU_DMA_LATENCY, 0);
17351c4989b0SBOUGH CHEN 
17365c11f1ffSHaibo Chen 	if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
17375c11f1ffSHaibo Chen 		clk_set_rate(imx_data->clk_per, pltfm_host->clock);
17385c11f1ffSHaibo Chen 
1739a0ad3087SMichael Trimarchi 	err = clk_prepare_enable(imx_data->clk_ahb);
1740a0ad3087SMichael Trimarchi 	if (err)
17411c4989b0SBOUGH CHEN 		goto remove_pm_qos_request;
1742a0ad3087SMichael Trimarchi 
174317b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_per);
174417b1eb7fSFabio Estevam 	if (err)
1745a0ad3087SMichael Trimarchi 		goto disable_ahb_clk;
1746af5d2b7bSUlf Hansson 
174717b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_ipg);
174817b1eb7fSFabio Estevam 	if (err)
174917b1eb7fSFabio Estevam 		goto disable_per_clk;
1750af5d2b7bSUlf Hansson 
17513602785bSMichael Trimarchi 	esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1752a0ad3087SMichael Trimarchi 
1753c6303c5dSBaolin Wang 	err = sdhci_runtime_resume_host(host, 0);
175417b1eb7fSFabio Estevam 	if (err)
1755a0ad3087SMichael Trimarchi 		goto disable_ipg_clk;
175689d7e5c1SDong Aisheng 
1757bb6e3581SBOUGH CHEN 	if (host->mmc->caps2 & MMC_CAP2_CQE)
1758bb6e3581SBOUGH CHEN 		err = cqhci_resume(host->mmc);
1759bb6e3581SBOUGH CHEN 
1760bb6e3581SBOUGH CHEN 	return err;
176117b1eb7fSFabio Estevam 
176217b1eb7fSFabio Estevam disable_ipg_clk:
176317b1eb7fSFabio Estevam 	clk_disable_unprepare(imx_data->clk_ipg);
176417b1eb7fSFabio Estevam disable_per_clk:
176517b1eb7fSFabio Estevam 	clk_disable_unprepare(imx_data->clk_per);
1766a0ad3087SMichael Trimarchi disable_ahb_clk:
1767a0ad3087SMichael Trimarchi 	clk_disable_unprepare(imx_data->clk_ahb);
17681c4989b0SBOUGH CHEN remove_pm_qos_request:
17691c4989b0SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
17701c4989b0SBOUGH CHEN 		pm_qos_remove_request(&imx_data->pm_qos_req);
177117b1eb7fSFabio Estevam 	return err;
177289d7e5c1SDong Aisheng }
177389d7e5c1SDong Aisheng #endif
177489d7e5c1SDong Aisheng 
177589d7e5c1SDong Aisheng static const struct dev_pm_ops sdhci_esdhc_pmops = {
177604143fbaSDong Aisheng 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
177789d7e5c1SDong Aisheng 	SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
177889d7e5c1SDong Aisheng 				sdhci_esdhc_runtime_resume, NULL)
177989d7e5c1SDong Aisheng };
178089d7e5c1SDong Aisheng 
178185d6509dSShawn Guo static struct platform_driver sdhci_esdhc_imx_driver = {
178285d6509dSShawn Guo 	.driver		= {
178385d6509dSShawn Guo 		.name	= "sdhci-esdhc-imx",
1784abfafc2dSShawn Guo 		.of_match_table = imx_esdhc_dt_ids,
178589d7e5c1SDong Aisheng 		.pm	= &sdhci_esdhc_pmops,
178685d6509dSShawn Guo 	},
178757ed3314SShawn Guo 	.id_table	= imx_esdhc_devtype,
178885d6509dSShawn Guo 	.probe		= sdhci_esdhc_imx_probe,
17890433c143SBill Pemberton 	.remove		= sdhci_esdhc_imx_remove,
179095f25efeSWolfram Sang };
179185d6509dSShawn Guo 
1792d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_imx_driver);
179385d6509dSShawn Guo 
179485d6509dSShawn Guo MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1795035ff831SWolfram Sang MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
179685d6509dSShawn Guo MODULE_LICENSE("GPL v2");
1797