xref: /linux/drivers/mmc/host/sdhci-esdhc-imx.c (revision d00ab1010c2e8e7e710697c39fb9b7e4fa7dd9c1)
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>
1295f25efeSWolfram Sang #include <linux/delay.h>
1395f25efeSWolfram Sang #include <linux/err.h>
1495f25efeSWolfram Sang #include <linux/clk.h>
1566506f76SShawn Guo #include <linux/module.h>
16e149860dSRichard Zhu #include <linux/slab.h>
1795f25efeSWolfram Sang #include <linux/mmc/host.h>
1858ac8177SRichard Zhu #include <linux/mmc/mmc.h>
1958ac8177SRichard Zhu #include <linux/mmc/sdio.h>
20fbe5fdd1SShawn Guo #include <linux/mmc/slot-gpio.h>
21abfafc2dSShawn Guo #include <linux/of.h>
22abfafc2dSShawn Guo #include <linux/of_device.h>
23e62d8b8fSDong Aisheng #include <linux/pinctrl/consumer.h>
2482906b13SArnd Bergmann #include <linux/platform_data/mmc-esdhc-imx.h>
2589d7e5c1SDong Aisheng #include <linux/pm_runtime.h>
2695f25efeSWolfram Sang #include "sdhci-pltfm.h"
2795f25efeSWolfram Sang #include "sdhci-esdhc.h"
2895f25efeSWolfram Sang 
29a215186dSHaibo Chen #define ESDHC_SYS_CTRL_DTOCV_MASK	0x0f
3060bf6396SShawn Guo #define	ESDHC_CTRL_D3CD			0x08
31fd44954eSHaibo Chen #define ESDHC_BURST_LEN_EN_INCR		(1 << 27)
3258ac8177SRichard Zhu /* VENDOR SPEC register */
3360bf6396SShawn Guo #define ESDHC_VENDOR_SPEC		0xc0
3460bf6396SShawn Guo #define  ESDHC_VENDOR_SPEC_SDIO_QUIRK	(1 << 1)
350322191eSDong Aisheng #define  ESDHC_VENDOR_SPEC_VSELECT	(1 << 1)
36fed2f6e2SDong Aisheng #define  ESDHC_VENDOR_SPEC_FRC_SDCLK_ON	(1 << 8)
3760bf6396SShawn Guo #define ESDHC_WTMK_LVL			0x44
38cc17e129SDong Aisheng #define  ESDHC_WTMK_DEFAULT_VAL		0x10401040
393fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_RD_WML_MASK	0x000000FF
403fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_RD_WML_SHIFT	0
413fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WR_WML_MASK	0x00FF0000
423fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WR_WML_SHIFT	16
433fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WML_VAL_DEF	64
443fbd4322SAndrew Gabbasov #define  ESDHC_WTMK_LVL_WML_VAL_MAX	128
4560bf6396SShawn Guo #define ESDHC_MIX_CTRL			0x48
46de5bdbffSDong Aisheng #define  ESDHC_MIX_CTRL_DDREN		(1 << 3)
472a15f981SShawn Guo #define  ESDHC_MIX_CTRL_AC23EN		(1 << 7)
480322191eSDong Aisheng #define  ESDHC_MIX_CTRL_EXE_TUNE	(1 << 22)
490322191eSDong Aisheng #define  ESDHC_MIX_CTRL_SMPCLK_SEL	(1 << 23)
500b330e38SDong Aisheng #define  ESDHC_MIX_CTRL_AUTO_TUNE_EN	(1 << 24)
510322191eSDong Aisheng #define  ESDHC_MIX_CTRL_FBCLK_SEL	(1 << 25)
5228b07674SHaibo Chen #define  ESDHC_MIX_CTRL_HS400_EN	(1 << 26)
532a15f981SShawn Guo /* Bits 3 and 6 are not SDHCI standard definitions */
542a15f981SShawn Guo #define  ESDHC_MIX_CTRL_SDHCI_MASK	0xb7
55d131a71cSDong Aisheng /* Tuning bits */
56d131a71cSDong Aisheng #define  ESDHC_MIX_CTRL_TUNING_MASK	0x03c00000
5758ac8177SRichard Zhu 
58602519b2SDong Aisheng /* dll control register */
59602519b2SDong Aisheng #define ESDHC_DLL_CTRL			0x60
60602519b2SDong Aisheng #define ESDHC_DLL_OVERRIDE_VAL_SHIFT	9
61602519b2SDong Aisheng #define ESDHC_DLL_OVERRIDE_EN_SHIFT	8
62602519b2SDong Aisheng 
630322191eSDong Aisheng /* tune control register */
640322191eSDong Aisheng #define ESDHC_TUNE_CTRL_STATUS		0x68
650322191eSDong Aisheng #define  ESDHC_TUNE_CTRL_STEP		1
660322191eSDong Aisheng #define  ESDHC_TUNE_CTRL_MIN		0
670322191eSDong Aisheng #define  ESDHC_TUNE_CTRL_MAX		((1 << 7) - 1)
680322191eSDong Aisheng 
6928b07674SHaibo Chen /* strobe dll register */
7028b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL		0x70
7128b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL_ENABLE	(1 << 0)
7228b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL_RESET	(1 << 1)
7328b07674SHaibo Chen #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT	3
7428b07674SHaibo Chen 
7528b07674SHaibo Chen #define ESDHC_STROBE_DLL_STATUS		0x74
7628b07674SHaibo Chen #define ESDHC_STROBE_DLL_STS_REF_LOCK	(1 << 1)
7728b07674SHaibo Chen #define ESDHC_STROBE_DLL_STS_SLV_LOCK	0x1
7828b07674SHaibo Chen 
796e9fd28eSDong Aisheng #define ESDHC_TUNING_CTRL		0xcc
806e9fd28eSDong Aisheng #define ESDHC_STD_TUNING_EN		(1 << 24)
816e9fd28eSDong Aisheng /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
82d87fc966SDong Aisheng #define ESDHC_TUNING_START_TAP_DEFAULT	0x1
83d87fc966SDong Aisheng #define ESDHC_TUNING_START_TAP_MASK	0xff
84260ecb3cSHaibo Chen #define ESDHC_TUNING_STEP_MASK		0x00070000
85d407e30bSHaibo Chen #define ESDHC_TUNING_STEP_SHIFT		16
866e9fd28eSDong Aisheng 
87ad93220dSDong Aisheng /* pinctrl state */
88ad93220dSDong Aisheng #define ESDHC_PINCTRL_STATE_100MHZ	"state_100mhz"
89ad93220dSDong Aisheng #define ESDHC_PINCTRL_STATE_200MHZ	"state_200mhz"
90ad93220dSDong Aisheng 
9158ac8177SRichard Zhu /*
92af51079eSSascha Hauer  * Our interpretation of the SDHCI_HOST_CONTROL register
93af51079eSSascha Hauer  */
94af51079eSSascha Hauer #define ESDHC_CTRL_4BITBUS		(0x1 << 1)
95af51079eSSascha Hauer #define ESDHC_CTRL_8BITBUS		(0x2 << 1)
96af51079eSSascha Hauer #define ESDHC_CTRL_BUSWIDTH_MASK	(0x3 << 1)
97af51079eSSascha Hauer 
98af51079eSSascha Hauer /*
99d04f8d5bSBenoît Thébaudeau  * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
10097e4ba6aSRichard Zhu  * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
10197e4ba6aSRichard Zhu  * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
10297e4ba6aSRichard Zhu  * Define this macro DMA error INT for fsl eSDHC
10397e4ba6aSRichard Zhu  */
10460bf6396SShawn Guo #define ESDHC_INT_VENDOR_SPEC_DMA_ERR	(1 << 28)
10597e4ba6aSRichard Zhu 
10697e4ba6aSRichard Zhu /*
10758ac8177SRichard Zhu  * The CMDTYPE of the CMD register (offset 0xE) should be set to
10858ac8177SRichard Zhu  * "11" when the STOP CMD12 is issued on imx53 to abort one
10958ac8177SRichard Zhu  * open ended multi-blk IO. Otherwise the TC INT wouldn't
11058ac8177SRichard Zhu  * be generated.
11158ac8177SRichard Zhu  * In exact block transfer, the controller doesn't complete the
11258ac8177SRichard Zhu  * operations automatically as required at the end of the
11358ac8177SRichard Zhu  * transfer and remains on hold if the abort command is not sent.
11458ac8177SRichard Zhu  * As a result, the TC flag is not asserted and SW received timeout
115d04f8d5bSBenoît Thébaudeau  * exception. Bit1 of Vendor Spec register is used to fix it.
11658ac8177SRichard Zhu  */
11731fbb301SShawn Guo #define ESDHC_FLAG_MULTIBLK_NO_INT	BIT(1)
11831fbb301SShawn Guo /*
1199d61c009SShawn Guo  * The flag tells that the ESDHC controller is an USDHC block that is
1209d61c009SShawn Guo  * integrated on the i.MX6 series.
1219d61c009SShawn Guo  */
1229d61c009SShawn Guo #define ESDHC_FLAG_USDHC		BIT(3)
1236e9fd28eSDong Aisheng /* The IP supports manual tuning process */
1246e9fd28eSDong Aisheng #define ESDHC_FLAG_MAN_TUNING		BIT(4)
1256e9fd28eSDong Aisheng /* The IP supports standard tuning process */
1266e9fd28eSDong Aisheng #define ESDHC_FLAG_STD_TUNING		BIT(5)
1276e9fd28eSDong Aisheng /* The IP has SDHCI_CAPABILITIES_1 register */
1286e9fd28eSDong Aisheng #define ESDHC_FLAG_HAVE_CAP1		BIT(6)
12918094430SDong Aisheng /*
130d04f8d5bSBenoît Thébaudeau  * The IP has erratum ERR004536
13118094430SDong Aisheng  * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
13218094430SDong Aisheng  * when reading data from the card
133667123f6SBenoît Thébaudeau  * This flag is also set for i.MX25 and i.MX35 in order to get
134667123f6SBenoît Thébaudeau  * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
13518094430SDong Aisheng  */
13618094430SDong Aisheng #define ESDHC_FLAG_ERR004536		BIT(7)
1374245afffSDong Aisheng /* The IP supports HS200 mode */
1384245afffSDong Aisheng #define ESDHC_FLAG_HS200		BIT(8)
13928b07674SHaibo Chen /* The IP supports HS400 mode */
14028b07674SHaibo Chen #define ESDHC_FLAG_HS400		BIT(9)
141af6a50d4SBOUGH CHEN /*
142af6a50d4SBOUGH CHEN  * The IP has errata ERR010450
143af6a50d4SBOUGH CHEN  * uSDHC: Due to the I/O timing limit, for SDR mode, SD card clock can't
144af6a50d4SBOUGH CHEN  * exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
145af6a50d4SBOUGH CHEN  */
146af6a50d4SBOUGH CHEN #define ESDHC_FLAG_ERR010450		BIT(10)
147d04f8d5bSBenoît Thébaudeau /* A clock frequency higher than this rate requires strobe dll control */
14828b07674SHaibo Chen #define ESDHC_STROBE_DLL_CLK_FREQ	100000000
149e149860dSRichard Zhu 
150f47c4bbfSShawn Guo struct esdhc_soc_data {
151f47c4bbfSShawn Guo 	u32 flags;
152f47c4bbfSShawn Guo };
153f47c4bbfSShawn Guo 
1544f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx25_data = {
155667123f6SBenoît Thébaudeau 	.flags = ESDHC_FLAG_ERR004536,
156f47c4bbfSShawn Guo };
157f47c4bbfSShawn Guo 
1584f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx35_data = {
159667123f6SBenoît Thébaudeau 	.flags = ESDHC_FLAG_ERR004536,
160f47c4bbfSShawn Guo };
161f47c4bbfSShawn Guo 
1624f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx51_data = {
163f47c4bbfSShawn Guo 	.flags = 0,
164f47c4bbfSShawn Guo };
165f47c4bbfSShawn Guo 
1664f100012SAndrey Smirnov static const struct esdhc_soc_data esdhc_imx53_data = {
167f47c4bbfSShawn Guo 	.flags = ESDHC_FLAG_MULTIBLK_NO_INT,
168f47c4bbfSShawn Guo };
169f47c4bbfSShawn Guo 
1704f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx6q_data = {
1716e9fd28eSDong Aisheng 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
1726e9fd28eSDong Aisheng };
1736e9fd28eSDong Aisheng 
1744f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx6sl_data = {
1756e9fd28eSDong Aisheng 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1764245afffSDong Aisheng 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
1774245afffSDong Aisheng 			| ESDHC_FLAG_HS200,
17857ed3314SShawn Guo };
17957ed3314SShawn Guo 
1804f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx6sx_data = {
181913d4951SDong Aisheng 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1824245afffSDong Aisheng 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200,
183913d4951SDong Aisheng };
184913d4951SDong Aisheng 
185af6a50d4SBOUGH CHEN static const struct esdhc_soc_data usdhc_imx6ull_data = {
186af6a50d4SBOUGH CHEN 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
187af6a50d4SBOUGH CHEN 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
188af6a50d4SBOUGH CHEN 			| ESDHC_FLAG_ERR010450,
189af6a50d4SBOUGH CHEN };
190af6a50d4SBOUGH CHEN 
1914f100012SAndrey Smirnov static const struct esdhc_soc_data usdhc_imx7d_data = {
19228b07674SHaibo Chen 	.flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
19328b07674SHaibo Chen 			| ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
19428b07674SHaibo Chen 			| ESDHC_FLAG_HS400,
19528b07674SHaibo Chen };
19628b07674SHaibo Chen 
197e149860dSRichard Zhu struct pltfm_imx_data {
198e149860dSRichard Zhu 	u32 scratchpad;
199e62d8b8fSDong Aisheng 	struct pinctrl *pinctrl;
200ad93220dSDong Aisheng 	struct pinctrl_state *pins_default;
201ad93220dSDong Aisheng 	struct pinctrl_state *pins_100mhz;
202ad93220dSDong Aisheng 	struct pinctrl_state *pins_200mhz;
203f47c4bbfSShawn Guo 	const struct esdhc_soc_data *socdata;
204842afc02SShawn Guo 	struct esdhc_platform_data boarddata;
20552dac615SSascha Hauer 	struct clk *clk_ipg;
20652dac615SSascha Hauer 	struct clk *clk_ahb;
20752dac615SSascha Hauer 	struct clk *clk_per;
2083602785bSMichael Trimarchi 	unsigned int actual_clock;
209361b8482SLucas Stach 	enum {
210361b8482SLucas Stach 		NO_CMD_PENDING,      /* no multiblock command pending */
211361b8482SLucas Stach 		MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
212361b8482SLucas Stach 		WAIT_FOR_INT,        /* sent CMD12, waiting for response INT */
213361b8482SLucas Stach 	} multiblock_status;
214de5bdbffSDong Aisheng 	u32 is_ddr;
215e149860dSRichard Zhu };
216e149860dSRichard Zhu 
217f8cbf461SKrzysztof Kozlowski static const struct platform_device_id imx_esdhc_devtype[] = {
21857ed3314SShawn Guo 	{
21957ed3314SShawn Guo 		.name = "sdhci-esdhc-imx25",
220f47c4bbfSShawn Guo 		.driver_data = (kernel_ulong_t) &esdhc_imx25_data,
22157ed3314SShawn Guo 	}, {
22257ed3314SShawn Guo 		.name = "sdhci-esdhc-imx35",
223f47c4bbfSShawn Guo 		.driver_data = (kernel_ulong_t) &esdhc_imx35_data,
22457ed3314SShawn Guo 	}, {
22557ed3314SShawn Guo 		.name = "sdhci-esdhc-imx51",
226f47c4bbfSShawn Guo 		.driver_data = (kernel_ulong_t) &esdhc_imx51_data,
22757ed3314SShawn Guo 	}, {
22857ed3314SShawn Guo 		/* sentinel */
22957ed3314SShawn Guo 	}
23057ed3314SShawn Guo };
23157ed3314SShawn Guo MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
23257ed3314SShawn Guo 
233abfafc2dSShawn Guo static const struct of_device_id imx_esdhc_dt_ids[] = {
234f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
235f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
236f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
237f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
238913d4951SDong Aisheng 	{ .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
2396e9fd28eSDong Aisheng 	{ .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
240f47c4bbfSShawn Guo 	{ .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
241af6a50d4SBOUGH CHEN 	{ .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
24228b07674SHaibo Chen 	{ .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
243abfafc2dSShawn Guo 	{ /* sentinel */ }
244abfafc2dSShawn Guo };
245abfafc2dSShawn Guo MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
246abfafc2dSShawn Guo 
24757ed3314SShawn Guo static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
24857ed3314SShawn Guo {
249f47c4bbfSShawn Guo 	return data->socdata == &esdhc_imx25_data;
25057ed3314SShawn Guo }
25157ed3314SShawn Guo 
25257ed3314SShawn Guo static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
25357ed3314SShawn Guo {
254f47c4bbfSShawn Guo 	return data->socdata == &esdhc_imx53_data;
25557ed3314SShawn Guo }
25657ed3314SShawn Guo 
25795a2482aSShawn Guo static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
25895a2482aSShawn Guo {
259f47c4bbfSShawn Guo 	return data->socdata == &usdhc_imx6q_data;
26095a2482aSShawn Guo }
26195a2482aSShawn Guo 
2629d61c009SShawn Guo static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
2639d61c009SShawn Guo {
264f47c4bbfSShawn Guo 	return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
2659d61c009SShawn Guo }
2669d61c009SShawn Guo 
26795f25efeSWolfram Sang static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
26895f25efeSWolfram Sang {
26995f25efeSWolfram Sang 	void __iomem *base = host->ioaddr + (reg & ~0x3);
27095f25efeSWolfram Sang 	u32 shift = (reg & 0x3) * 8;
27195f25efeSWolfram Sang 
27295f25efeSWolfram Sang 	writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
27395f25efeSWolfram Sang }
27495f25efeSWolfram Sang 
2757e29c306SWolfram Sang static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
2767e29c306SWolfram Sang {
277361b8482SLucas Stach 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
278070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
279913413c3SShawn Guo 	u32 val = readl(host->ioaddr + reg);
280913413c3SShawn Guo 
2810322191eSDong Aisheng 	if (unlikely(reg == SDHCI_PRESENT_STATE)) {
2820322191eSDong Aisheng 		u32 fsl_prss = val;
2830322191eSDong Aisheng 		/* save the least 20 bits */
2840322191eSDong Aisheng 		val = fsl_prss & 0x000FFFFF;
2850322191eSDong Aisheng 		/* move dat[0-3] bits */
2860322191eSDong Aisheng 		val |= (fsl_prss & 0x0F000000) >> 4;
2870322191eSDong Aisheng 		/* move cmd line bit */
2880322191eSDong Aisheng 		val |= (fsl_prss & 0x00800000) << 1;
2890322191eSDong Aisheng 	}
2900322191eSDong Aisheng 
29197e4ba6aSRichard Zhu 	if (unlikely(reg == SDHCI_CAPABILITIES)) {
2926b4fb671SDong Aisheng 		/* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
2936b4fb671SDong Aisheng 		if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
2946b4fb671SDong Aisheng 			val &= 0xffff0000;
2956b4fb671SDong Aisheng 
29697e4ba6aSRichard Zhu 		/* In FSL esdhc IC module, only bit20 is used to indicate the
29797e4ba6aSRichard Zhu 		 * ADMA2 capability of esdhc, but this bit is messed up on
29897e4ba6aSRichard Zhu 		 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
29997e4ba6aSRichard Zhu 		 * don't actually support ADMA2). So set the BROKEN_ADMA
300d04f8d5bSBenoît Thébaudeau 		 * quirk on MX25/35 platforms.
30197e4ba6aSRichard Zhu 		 */
30297e4ba6aSRichard Zhu 
30397e4ba6aSRichard Zhu 		if (val & SDHCI_CAN_DO_ADMA1) {
30497e4ba6aSRichard Zhu 			val &= ~SDHCI_CAN_DO_ADMA1;
30597e4ba6aSRichard Zhu 			val |= SDHCI_CAN_DO_ADMA2;
30697e4ba6aSRichard Zhu 		}
30797e4ba6aSRichard Zhu 	}
30897e4ba6aSRichard Zhu 
3096e9fd28eSDong Aisheng 	if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
3106e9fd28eSDong Aisheng 		if (esdhc_is_usdhc(imx_data)) {
3116e9fd28eSDong Aisheng 			if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
3126e9fd28eSDong Aisheng 				val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
3136e9fd28eSDong Aisheng 			else
3146e9fd28eSDong Aisheng 				/* imx6q/dl does not have cap_1 register, fake one */
3150322191eSDong Aisheng 				val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
316888824bbSDong Aisheng 					| SDHCI_SUPPORT_SDR50
317da0295ffSDong Aisheng 					| SDHCI_USE_SDR50_TUNING
318da0295ffSDong Aisheng 					| (SDHCI_TUNING_MODE_3 << SDHCI_RETUNING_MODE_SHIFT);
31928b07674SHaibo Chen 
32028b07674SHaibo Chen 			if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
32128b07674SHaibo Chen 				val |= SDHCI_SUPPORT_HS400;
32292748beaSStefan Agner 
32392748beaSStefan Agner 			/*
32492748beaSStefan Agner 			 * Do not advertise faster UHS modes if there are no
32592748beaSStefan Agner 			 * pinctrl states for 100MHz/200MHz.
32692748beaSStefan Agner 			 */
32792748beaSStefan Agner 			if (IS_ERR_OR_NULL(imx_data->pins_100mhz) ||
32892748beaSStefan Agner 			    IS_ERR_OR_NULL(imx_data->pins_200mhz))
32992748beaSStefan Agner 				val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50
33092748beaSStefan Agner 					 | SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
3316e9fd28eSDong Aisheng 		}
3326e9fd28eSDong Aisheng 	}
3330322191eSDong Aisheng 
3349d61c009SShawn Guo 	if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
3350322191eSDong Aisheng 		val = 0;
3360322191eSDong Aisheng 		val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
3370322191eSDong Aisheng 		val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
3380322191eSDong Aisheng 		val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
3390322191eSDong Aisheng 	}
3400322191eSDong Aisheng 
34197e4ba6aSRichard Zhu 	if (unlikely(reg == SDHCI_INT_STATUS)) {
34260bf6396SShawn Guo 		if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
34360bf6396SShawn Guo 			val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
34497e4ba6aSRichard Zhu 			val |= SDHCI_INT_ADMA_ERROR;
34597e4ba6aSRichard Zhu 		}
346361b8482SLucas Stach 
347361b8482SLucas Stach 		/*
348361b8482SLucas Stach 		 * mask off the interrupt we get in response to the manually
349361b8482SLucas Stach 		 * sent CMD12
350361b8482SLucas Stach 		 */
351361b8482SLucas Stach 		if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
352361b8482SLucas Stach 		    ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
353361b8482SLucas Stach 			val &= ~SDHCI_INT_RESPONSE;
354361b8482SLucas Stach 			writel(SDHCI_INT_RESPONSE, host->ioaddr +
355361b8482SLucas Stach 						   SDHCI_INT_STATUS);
356361b8482SLucas Stach 			imx_data->multiblock_status = NO_CMD_PENDING;
357361b8482SLucas Stach 		}
35897e4ba6aSRichard Zhu 	}
35997e4ba6aSRichard Zhu 
3607e29c306SWolfram Sang 	return val;
3617e29c306SWolfram Sang }
3627e29c306SWolfram Sang 
3637e29c306SWolfram Sang static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
3647e29c306SWolfram Sang {
365e149860dSRichard Zhu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
366070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
3670d58864bSTony Lin 	u32 data;
368e149860dSRichard Zhu 
36977da3da0SAaron Brice 	if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
37077da3da0SAaron Brice 			reg == SDHCI_INT_STATUS)) {
371b7321042SDong Aisheng 		if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
3720d58864bSTony Lin 			/*
3730d58864bSTony Lin 			 * Clear and then set D3CD bit to avoid missing the
374d04f8d5bSBenoît Thébaudeau 			 * card interrupt. This is an eSDHC controller problem
3750d58864bSTony Lin 			 * so we need to apply the following workaround: clear
3760d58864bSTony Lin 			 * and set D3CD bit will make eSDHC re-sample the card
3770d58864bSTony Lin 			 * interrupt. In case a card interrupt was lost,
3780d58864bSTony Lin 			 * re-sample it by the following steps.
3790d58864bSTony Lin 			 */
3800d58864bSTony Lin 			data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
38160bf6396SShawn Guo 			data &= ~ESDHC_CTRL_D3CD;
3820d58864bSTony Lin 			writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
38360bf6396SShawn Guo 			data |= ESDHC_CTRL_D3CD;
3840d58864bSTony Lin 			writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
3850d58864bSTony Lin 		}
386915be485SDong Aisheng 
387915be485SDong Aisheng 		if (val & SDHCI_INT_ADMA_ERROR) {
388915be485SDong Aisheng 			val &= ~SDHCI_INT_ADMA_ERROR;
389915be485SDong Aisheng 			val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
390915be485SDong Aisheng 		}
3910d58864bSTony Lin 	}
3920d58864bSTony Lin 
393f47c4bbfSShawn Guo 	if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
39458ac8177SRichard Zhu 				&& (reg == SDHCI_INT_STATUS)
39558ac8177SRichard Zhu 				&& (val & SDHCI_INT_DATA_END))) {
39658ac8177SRichard Zhu 			u32 v;
39760bf6396SShawn Guo 			v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
39860bf6396SShawn Guo 			v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
39960bf6396SShawn Guo 			writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
400361b8482SLucas Stach 
401361b8482SLucas Stach 			if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
402361b8482SLucas Stach 			{
403361b8482SLucas Stach 				/* send a manual CMD12 with RESPTYP=none */
404361b8482SLucas Stach 				data = MMC_STOP_TRANSMISSION << 24 |
405361b8482SLucas Stach 				       SDHCI_CMD_ABORTCMD << 16;
406361b8482SLucas Stach 				writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
407361b8482SLucas Stach 				imx_data->multiblock_status = WAIT_FOR_INT;
408361b8482SLucas Stach 			}
40958ac8177SRichard Zhu 	}
41058ac8177SRichard Zhu 
4117e29c306SWolfram Sang 	writel(val, host->ioaddr + reg);
4127e29c306SWolfram Sang }
4137e29c306SWolfram Sang 
41495f25efeSWolfram Sang static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
41595f25efeSWolfram Sang {
416ef4d0888SShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
417070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
4180322191eSDong Aisheng 	u16 ret = 0;
4190322191eSDong Aisheng 	u32 val;
420ef4d0888SShawn Guo 
42195a2482aSShawn Guo 	if (unlikely(reg == SDHCI_HOST_VERSION)) {
422ef4d0888SShawn Guo 		reg ^= 2;
4239d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data)) {
42495a2482aSShawn Guo 			/*
425ef4d0888SShawn Guo 			 * The usdhc register returns a wrong host version.
426ef4d0888SShawn Guo 			 * Correct it here.
42795a2482aSShawn Guo 			 */
428ef4d0888SShawn Guo 			return SDHCI_SPEC_300;
429ef4d0888SShawn Guo 		}
43095a2482aSShawn Guo 	}
43195f25efeSWolfram Sang 
4320322191eSDong Aisheng 	if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
4330322191eSDong Aisheng 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
4340322191eSDong Aisheng 		if (val & ESDHC_VENDOR_SPEC_VSELECT)
4350322191eSDong Aisheng 			ret |= SDHCI_CTRL_VDD_180;
4360322191eSDong Aisheng 
4379d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data)) {
4386e9fd28eSDong Aisheng 			if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
4390322191eSDong Aisheng 				val = readl(host->ioaddr + ESDHC_MIX_CTRL);
4406e9fd28eSDong Aisheng 			else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
4416e9fd28eSDong Aisheng 				/* the std tuning bits is in ACMD12_ERR for imx6sl */
442869f8a69SAdrian Hunter 				val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
4436e9fd28eSDong Aisheng 		}
4446e9fd28eSDong Aisheng 
4450322191eSDong Aisheng 		if (val & ESDHC_MIX_CTRL_EXE_TUNE)
4460322191eSDong Aisheng 			ret |= SDHCI_CTRL_EXEC_TUNING;
4470322191eSDong Aisheng 		if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
4480322191eSDong Aisheng 			ret |= SDHCI_CTRL_TUNED_CLK;
4490322191eSDong Aisheng 
4500322191eSDong Aisheng 		ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
4510322191eSDong Aisheng 
4520322191eSDong Aisheng 		return ret;
4530322191eSDong Aisheng 	}
4540322191eSDong Aisheng 
4557dd109efSDong Aisheng 	if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
4567dd109efSDong Aisheng 		if (esdhc_is_usdhc(imx_data)) {
4577dd109efSDong Aisheng 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
4587dd109efSDong Aisheng 			ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
4597dd109efSDong Aisheng 			/* Swap AC23 bit */
4607dd109efSDong Aisheng 			if (m & ESDHC_MIX_CTRL_AC23EN) {
4617dd109efSDong Aisheng 				ret &= ~ESDHC_MIX_CTRL_AC23EN;
4627dd109efSDong Aisheng 				ret |= SDHCI_TRNS_AUTO_CMD23;
4637dd109efSDong Aisheng 			}
4647dd109efSDong Aisheng 		} else {
4657dd109efSDong Aisheng 			ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
4667dd109efSDong Aisheng 		}
4677dd109efSDong Aisheng 
4687dd109efSDong Aisheng 		return ret;
4697dd109efSDong Aisheng 	}
4707dd109efSDong Aisheng 
47195f25efeSWolfram Sang 	return readw(host->ioaddr + reg);
47295f25efeSWolfram Sang }
47395f25efeSWolfram Sang 
47495f25efeSWolfram Sang static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
47595f25efeSWolfram Sang {
47695f25efeSWolfram Sang 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
477070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
4780322191eSDong Aisheng 	u32 new_val = 0;
47995f25efeSWolfram Sang 
48095f25efeSWolfram Sang 	switch (reg) {
4810322191eSDong Aisheng 	case SDHCI_CLOCK_CONTROL:
4820322191eSDong Aisheng 		new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
4830322191eSDong Aisheng 		if (val & SDHCI_CLOCK_CARD_EN)
4840322191eSDong Aisheng 			new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
4850322191eSDong Aisheng 		else
4860322191eSDong Aisheng 			new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
4870322191eSDong Aisheng 		writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
4880322191eSDong Aisheng 		return;
4890322191eSDong Aisheng 	case SDHCI_HOST_CONTROL2:
4900322191eSDong Aisheng 		new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
4910322191eSDong Aisheng 		if (val & SDHCI_CTRL_VDD_180)
4920322191eSDong Aisheng 			new_val |= ESDHC_VENDOR_SPEC_VSELECT;
4930322191eSDong Aisheng 		else
4940322191eSDong Aisheng 			new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
4950322191eSDong Aisheng 		writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
4966e9fd28eSDong Aisheng 		if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
4970322191eSDong Aisheng 			new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
498da0295ffSDong Aisheng 			if (val & SDHCI_CTRL_TUNED_CLK) {
4990322191eSDong Aisheng 				new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
500da0295ffSDong Aisheng 				new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
501da0295ffSDong Aisheng 			} else {
5020322191eSDong Aisheng 				new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
503da0295ffSDong Aisheng 				new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
504da0295ffSDong Aisheng 			}
5050322191eSDong Aisheng 			writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
5066e9fd28eSDong Aisheng 		} else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
507869f8a69SAdrian Hunter 			u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
5086e9fd28eSDong Aisheng 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
5098b2bb0adSDong Aisheng 			if (val & SDHCI_CTRL_TUNED_CLK) {
5108b2bb0adSDong Aisheng 				v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
5116e9fd28eSDong Aisheng 			} else {
5128b2bb0adSDong Aisheng 				v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
5136e9fd28eSDong Aisheng 				m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
5140b330e38SDong Aisheng 				m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
5156e9fd28eSDong Aisheng 			}
5166e9fd28eSDong Aisheng 
5178b2bb0adSDong Aisheng 			if (val & SDHCI_CTRL_EXEC_TUNING) {
5188b2bb0adSDong Aisheng 				v |= ESDHC_MIX_CTRL_EXE_TUNE;
5198b2bb0adSDong Aisheng 				m |= ESDHC_MIX_CTRL_FBCLK_SEL;
5200b330e38SDong Aisheng 				m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
5218b2bb0adSDong Aisheng 			} else {
5228b2bb0adSDong Aisheng 				v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
5238b2bb0adSDong Aisheng 			}
5246e9fd28eSDong Aisheng 
525869f8a69SAdrian Hunter 			writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
5266e9fd28eSDong Aisheng 			writel(m, host->ioaddr + ESDHC_MIX_CTRL);
5276e9fd28eSDong Aisheng 		}
5280322191eSDong Aisheng 		return;
52995f25efeSWolfram Sang 	case SDHCI_TRANSFER_MODE:
530f47c4bbfSShawn Guo 		if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
53158ac8177SRichard Zhu 				&& (host->cmd->opcode == SD_IO_RW_EXTENDED)
53258ac8177SRichard Zhu 				&& (host->cmd->data->blocks > 1)
53358ac8177SRichard Zhu 				&& (host->cmd->data->flags & MMC_DATA_READ)) {
53458ac8177SRichard Zhu 			u32 v;
53560bf6396SShawn Guo 			v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
53660bf6396SShawn Guo 			v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
53760bf6396SShawn Guo 			writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
53858ac8177SRichard Zhu 		}
53969f54698SShawn Guo 
5409d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data)) {
5413fbd4322SAndrew Gabbasov 			u32 wml;
54269f54698SShawn Guo 			u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
5432a15f981SShawn Guo 			/* Swap AC23 bit */
5442a15f981SShawn Guo 			if (val & SDHCI_TRNS_AUTO_CMD23) {
5452a15f981SShawn Guo 				val &= ~SDHCI_TRNS_AUTO_CMD23;
5462a15f981SShawn Guo 				val |= ESDHC_MIX_CTRL_AC23EN;
5472a15f981SShawn Guo 			}
5482a15f981SShawn Guo 			m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
54969f54698SShawn Guo 			writel(m, host->ioaddr + ESDHC_MIX_CTRL);
5503fbd4322SAndrew Gabbasov 
5513fbd4322SAndrew Gabbasov 			/* Set watermark levels for PIO access to maximum value
5523fbd4322SAndrew Gabbasov 			 * (128 words) to accommodate full 512 bytes buffer.
5533fbd4322SAndrew Gabbasov 			 * For DMA access restore the levels to default value.
5543fbd4322SAndrew Gabbasov 			 */
5553fbd4322SAndrew Gabbasov 			m = readl(host->ioaddr + ESDHC_WTMK_LVL);
5563fbd4322SAndrew Gabbasov 			if (val & SDHCI_TRNS_DMA)
5573fbd4322SAndrew Gabbasov 				wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
5583fbd4322SAndrew Gabbasov 			else
5593fbd4322SAndrew Gabbasov 				wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
5603fbd4322SAndrew Gabbasov 			m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
5613fbd4322SAndrew Gabbasov 			       ESDHC_WTMK_LVL_WR_WML_MASK);
5623fbd4322SAndrew Gabbasov 			m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
5633fbd4322SAndrew Gabbasov 			     (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
5643fbd4322SAndrew Gabbasov 			writel(m, host->ioaddr + ESDHC_WTMK_LVL);
56569f54698SShawn Guo 		} else {
56669f54698SShawn Guo 			/*
56769f54698SShawn Guo 			 * Postpone this write, we must do it together with a
56869f54698SShawn Guo 			 * command write that is down below.
56969f54698SShawn Guo 			 */
570e149860dSRichard Zhu 			imx_data->scratchpad = val;
57169f54698SShawn Guo 		}
57295f25efeSWolfram Sang 		return;
57395f25efeSWolfram Sang 	case SDHCI_COMMAND:
574361b8482SLucas Stach 		if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
57558ac8177SRichard Zhu 			val |= SDHCI_CMD_ABORTCMD;
57695a2482aSShawn Guo 
577361b8482SLucas Stach 		if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
578f47c4bbfSShawn Guo 		    (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
579361b8482SLucas Stach 			imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
580361b8482SLucas Stach 
5819d61c009SShawn Guo 		if (esdhc_is_usdhc(imx_data))
58295a2482aSShawn Guo 			writel(val << 16,
58395a2482aSShawn Guo 			       host->ioaddr + SDHCI_TRANSFER_MODE);
58469f54698SShawn Guo 		else
585e149860dSRichard Zhu 			writel(val << 16 | imx_data->scratchpad,
58695f25efeSWolfram Sang 			       host->ioaddr + SDHCI_TRANSFER_MODE);
58795f25efeSWolfram Sang 		return;
58895f25efeSWolfram Sang 	case SDHCI_BLOCK_SIZE:
58995f25efeSWolfram Sang 		val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
59095f25efeSWolfram Sang 		break;
59195f25efeSWolfram Sang 	}
59295f25efeSWolfram Sang 	esdhc_clrset_le(host, 0xffff, val, reg);
59395f25efeSWolfram Sang }
59495f25efeSWolfram Sang 
59577da3da0SAaron Brice static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
59677da3da0SAaron Brice {
59777da3da0SAaron Brice 	u8 ret;
59877da3da0SAaron Brice 	u32 val;
59977da3da0SAaron Brice 
60077da3da0SAaron Brice 	switch (reg) {
60177da3da0SAaron Brice 	case SDHCI_HOST_CONTROL:
60277da3da0SAaron Brice 		val = readl(host->ioaddr + reg);
60377da3da0SAaron Brice 
60477da3da0SAaron Brice 		ret = val & SDHCI_CTRL_LED;
60577da3da0SAaron Brice 		ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
60677da3da0SAaron Brice 		ret |= (val & ESDHC_CTRL_4BITBUS);
60777da3da0SAaron Brice 		ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
60877da3da0SAaron Brice 		return ret;
60977da3da0SAaron Brice 	}
61077da3da0SAaron Brice 
61177da3da0SAaron Brice 	return readb(host->ioaddr + reg);
61277da3da0SAaron Brice }
61377da3da0SAaron Brice 
61495f25efeSWolfram Sang static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
61595f25efeSWolfram Sang {
6169a0985b7SWilson Callan 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
617070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
61881a0a8bcSBenoît Thébaudeau 	u32 new_val = 0;
619af51079eSSascha Hauer 	u32 mask;
62095f25efeSWolfram Sang 
62195f25efeSWolfram Sang 	switch (reg) {
62295f25efeSWolfram Sang 	case SDHCI_POWER_CONTROL:
62395f25efeSWolfram Sang 		/*
62495f25efeSWolfram Sang 		 * FSL put some DMA bits here
62595f25efeSWolfram Sang 		 * If your board has a regulator, code should be here
62695f25efeSWolfram Sang 		 */
62795f25efeSWolfram Sang 		return;
62895f25efeSWolfram Sang 	case SDHCI_HOST_CONTROL:
6296b40d182SShawn Guo 		/* FSL messed up here, so we need to manually compose it. */
630af51079eSSascha Hauer 		new_val = val & SDHCI_CTRL_LED;
6317122bbb0SMasanari Iida 		/* ensure the endianness */
63295f25efeSWolfram Sang 		new_val |= ESDHC_HOST_CONTROL_LE;
6339a0985b7SWilson Callan 		/* bits 8&9 are reserved on mx25 */
6349a0985b7SWilson Callan 		if (!is_imx25_esdhc(imx_data)) {
63595f25efeSWolfram Sang 			/* DMA mode bits are shifted */
63695f25efeSWolfram Sang 			new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
6379a0985b7SWilson Callan 		}
63895f25efeSWolfram Sang 
639af51079eSSascha Hauer 		/*
640af51079eSSascha Hauer 		 * Do not touch buswidth bits here. This is done in
641af51079eSSascha Hauer 		 * esdhc_pltfm_bus_width.
642f6825748SMartin Fuzzey 		 * Do not touch the D3CD bit either which is used for the
643d04f8d5bSBenoît Thébaudeau 		 * SDIO interrupt erratum workaround.
644af51079eSSascha Hauer 		 */
645f6825748SMartin Fuzzey 		mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
646af51079eSSascha Hauer 
647af51079eSSascha Hauer 		esdhc_clrset_le(host, mask, new_val, reg);
64895f25efeSWolfram Sang 		return;
64981a0a8bcSBenoît Thébaudeau 	case SDHCI_SOFTWARE_RESET:
65081a0a8bcSBenoît Thébaudeau 		if (val & SDHCI_RESET_DATA)
65181a0a8bcSBenoît Thébaudeau 			new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
65281a0a8bcSBenoît Thébaudeau 		break;
65395f25efeSWolfram Sang 	}
65495f25efeSWolfram Sang 	esdhc_clrset_le(host, 0xff, val, reg);
655913413c3SShawn Guo 
65681a0a8bcSBenoît Thébaudeau 	if (reg == SDHCI_SOFTWARE_RESET) {
65781a0a8bcSBenoît Thébaudeau 		if (val & SDHCI_RESET_ALL) {
658913413c3SShawn Guo 			/*
65981a0a8bcSBenoît Thébaudeau 			 * The esdhc has a design violation to SDHC spec which
66081a0a8bcSBenoît Thébaudeau 			 * tells that software reset should not affect card
66181a0a8bcSBenoît Thébaudeau 			 * detection circuit. But esdhc clears its SYSCTL
66281a0a8bcSBenoît Thébaudeau 			 * register bits [0..2] during the software reset. This
66381a0a8bcSBenoît Thébaudeau 			 * will stop those clocks that card detection circuit
66481a0a8bcSBenoît Thébaudeau 			 * relies on. To work around it, we turn the clocks on
66581a0a8bcSBenoît Thébaudeau 			 * back to keep card detection circuit functional.
666913413c3SShawn Guo 			 */
667913413c3SShawn Guo 			esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
66858c8c4fbSShawn Guo 			/*
66958c8c4fbSShawn Guo 			 * The reset on usdhc fails to clear MIX_CTRL register.
67058c8c4fbSShawn Guo 			 * Do it manually here.
67158c8c4fbSShawn Guo 			 */
672de5bdbffSDong Aisheng 			if (esdhc_is_usdhc(imx_data)) {
67381a0a8bcSBenoît Thébaudeau 				/*
67481a0a8bcSBenoît Thébaudeau 				 * the tuning bits should be kept during reset
67581a0a8bcSBenoît Thébaudeau 				 */
676d131a71cSDong Aisheng 				new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
677d131a71cSDong Aisheng 				writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
678d131a71cSDong Aisheng 						host->ioaddr + ESDHC_MIX_CTRL);
679de5bdbffSDong Aisheng 				imx_data->is_ddr = 0;
680de5bdbffSDong Aisheng 			}
68181a0a8bcSBenoît Thébaudeau 		} else if (val & SDHCI_RESET_DATA) {
68281a0a8bcSBenoît Thébaudeau 			/*
68381a0a8bcSBenoît Thébaudeau 			 * The eSDHC DAT line software reset clears at least the
68481a0a8bcSBenoît Thébaudeau 			 * data transfer width on i.MX25, so make sure that the
68581a0a8bcSBenoît Thébaudeau 			 * Host Control register is unaffected.
68681a0a8bcSBenoît Thébaudeau 			 */
68781a0a8bcSBenoît Thébaudeau 			esdhc_clrset_le(host, 0xff, new_val,
68881a0a8bcSBenoît Thébaudeau 					SDHCI_HOST_CONTROL);
68981a0a8bcSBenoît Thébaudeau 		}
69058c8c4fbSShawn Guo 	}
69195f25efeSWolfram Sang }
69295f25efeSWolfram Sang 
6930ddf03c9SLucas Stach static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
6940ddf03c9SLucas Stach {
6950ddf03c9SLucas Stach 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
6960ddf03c9SLucas Stach 
697a974862fSDong Aisheng 	return pltfm_host->clock;
6980ddf03c9SLucas Stach }
6990ddf03c9SLucas Stach 
70095f25efeSWolfram Sang static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
70195f25efeSWolfram Sang {
70295f25efeSWolfram Sang 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
70395f25efeSWolfram Sang 
704a974862fSDong Aisheng 	return pltfm_host->clock / 256 / 16;
70595f25efeSWolfram Sang }
70695f25efeSWolfram Sang 
7078ba9580aSLucas Stach static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
7088ba9580aSLucas Stach 					 unsigned int clock)
7098ba9580aSLucas Stach {
7108ba9580aSLucas Stach 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
711070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
712a974862fSDong Aisheng 	unsigned int host_clock = pltfm_host->clock;
7135143c953SBenoît Thébaudeau 	int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
7145143c953SBenoît Thébaudeau 	int pre_div = 1;
715d31fc00aSDong Aisheng 	int div = 1;
716fed2f6e2SDong Aisheng 	u32 temp, val;
7178ba9580aSLucas Stach 
7189d61c009SShawn Guo 	if (esdhc_is_usdhc(imx_data)) {
719fed2f6e2SDong Aisheng 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
720fed2f6e2SDong Aisheng 		writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
721fed2f6e2SDong Aisheng 			host->ioaddr + ESDHC_VENDOR_SPEC);
722fed2f6e2SDong Aisheng 	}
72373e736f8SStefan Agner 
72473e736f8SStefan Agner 	if (clock == 0) {
72573e736f8SStefan Agner 		host->mmc->actual_clock = 0;
726373073efSRussell King 		return;
727fed2f6e2SDong Aisheng 	}
728d31fc00aSDong Aisheng 
729499ed50fSBenoît Thébaudeau 	/* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
730499ed50fSBenoît Thébaudeau 	if (is_imx53_esdhc(imx_data)) {
731499ed50fSBenoît Thébaudeau 		/*
732499ed50fSBenoît Thébaudeau 		 * According to the i.MX53 reference manual, if DLLCTRL[10] can
733499ed50fSBenoît Thébaudeau 		 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
734499ed50fSBenoît Thébaudeau 		 */
735499ed50fSBenoît Thébaudeau 		val = readl(host->ioaddr + ESDHC_DLL_CTRL);
736499ed50fSBenoît Thébaudeau 		writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
737499ed50fSBenoît Thébaudeau 		temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
738499ed50fSBenoît Thébaudeau 		writel(val, host->ioaddr + ESDHC_DLL_CTRL);
739499ed50fSBenoît Thébaudeau 		if (temp & BIT(10))
740499ed50fSBenoît Thébaudeau 			pre_div = 2;
741499ed50fSBenoît Thébaudeau 	}
742499ed50fSBenoît Thébaudeau 
743d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
744d31fc00aSDong Aisheng 	temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
745d31fc00aSDong Aisheng 		| ESDHC_CLOCK_MASK);
746d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
747d31fc00aSDong Aisheng 
748af6a50d4SBOUGH CHEN 	if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
749af6a50d4SBOUGH CHEN 		unsigned int max_clock;
750af6a50d4SBOUGH CHEN 
751af6a50d4SBOUGH CHEN 		max_clock = imx_data->is_ddr ? 45000000 : 150000000;
752af6a50d4SBOUGH CHEN 
753af6a50d4SBOUGH CHEN 		clock = min(clock, max_clock);
754af6a50d4SBOUGH CHEN 	}
755af6a50d4SBOUGH CHEN 
7565143c953SBenoît Thébaudeau 	while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
7575143c953SBenoît Thébaudeau 			pre_div < 256)
758d31fc00aSDong Aisheng 		pre_div *= 2;
759d31fc00aSDong Aisheng 
7605143c953SBenoît Thébaudeau 	while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
761d31fc00aSDong Aisheng 		div++;
762d31fc00aSDong Aisheng 
7635143c953SBenoît Thébaudeau 	host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
764d31fc00aSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
765e76b8559SDong Aisheng 		clock, host->mmc->actual_clock);
766d31fc00aSDong Aisheng 
767d31fc00aSDong Aisheng 	pre_div >>= 1;
768d31fc00aSDong Aisheng 	div--;
769d31fc00aSDong Aisheng 
770d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
771d31fc00aSDong Aisheng 	temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
772d31fc00aSDong Aisheng 		| (div << ESDHC_DIVIDER_SHIFT)
773d31fc00aSDong Aisheng 		| (pre_div << ESDHC_PREDIV_SHIFT));
774d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
775fed2f6e2SDong Aisheng 
7769d61c009SShawn Guo 	if (esdhc_is_usdhc(imx_data)) {
777fed2f6e2SDong Aisheng 		val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
778fed2f6e2SDong Aisheng 		writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
779fed2f6e2SDong Aisheng 			host->ioaddr + ESDHC_VENDOR_SPEC);
780fed2f6e2SDong Aisheng 	}
781fed2f6e2SDong Aisheng 
782d31fc00aSDong Aisheng 	mdelay(1);
7838ba9580aSLucas Stach }
7848ba9580aSLucas Stach 
785913413c3SShawn Guo static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
786913413c3SShawn Guo {
787842afc02SShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
788070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
789842afc02SShawn Guo 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
790913413c3SShawn Guo 
791913413c3SShawn Guo 	switch (boarddata->wp_type) {
792913413c3SShawn Guo 	case ESDHC_WP_GPIO:
793fbe5fdd1SShawn Guo 		return mmc_gpio_get_ro(host->mmc);
794913413c3SShawn Guo 	case ESDHC_WP_CONTROLLER:
795913413c3SShawn Guo 		return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
796913413c3SShawn Guo 			       SDHCI_WRITE_PROTECT);
797913413c3SShawn Guo 	case ESDHC_WP_NONE:
798913413c3SShawn Guo 		break;
799913413c3SShawn Guo 	}
800913413c3SShawn Guo 
801913413c3SShawn Guo 	return -ENOSYS;
802913413c3SShawn Guo }
803913413c3SShawn Guo 
8042317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
805af51079eSSascha Hauer {
806af51079eSSascha Hauer 	u32 ctrl;
807af51079eSSascha Hauer 
808af51079eSSascha Hauer 	switch (width) {
809af51079eSSascha Hauer 	case MMC_BUS_WIDTH_8:
810af51079eSSascha Hauer 		ctrl = ESDHC_CTRL_8BITBUS;
811af51079eSSascha Hauer 		break;
812af51079eSSascha Hauer 	case MMC_BUS_WIDTH_4:
813af51079eSSascha Hauer 		ctrl = ESDHC_CTRL_4BITBUS;
814af51079eSSascha Hauer 		break;
815af51079eSSascha Hauer 	default:
816af51079eSSascha Hauer 		ctrl = 0;
817af51079eSSascha Hauer 		break;
818af51079eSSascha Hauer 	}
819af51079eSSascha Hauer 
820af51079eSSascha Hauer 	esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
821af51079eSSascha Hauer 			SDHCI_HOST_CONTROL);
822af51079eSSascha Hauer }
823af51079eSSascha Hauer 
824de3e1dd0SBOUGH CHEN static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
825de3e1dd0SBOUGH CHEN {
826de3e1dd0SBOUGH CHEN 	struct sdhci_host *host = mmc_priv(mmc);
827de3e1dd0SBOUGH CHEN 
828de3e1dd0SBOUGH CHEN 	/*
829de3e1dd0SBOUGH CHEN 	 * i.MX uSDHC internally already uses a fixed optimized timing for
830de3e1dd0SBOUGH CHEN 	 * DDR50, normally does not require tuning for DDR50 mode.
831de3e1dd0SBOUGH CHEN 	 */
832de3e1dd0SBOUGH CHEN 	if (host->timing == MMC_TIMING_UHS_DDR50)
833de3e1dd0SBOUGH CHEN 		return 0;
834de3e1dd0SBOUGH CHEN 
835de3e1dd0SBOUGH CHEN 	return sdhci_execute_tuning(mmc, opcode);
836de3e1dd0SBOUGH CHEN }
837de3e1dd0SBOUGH CHEN 
8380322191eSDong Aisheng static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
8390322191eSDong Aisheng {
8400322191eSDong Aisheng 	u32 reg;
8410322191eSDong Aisheng 
8420322191eSDong Aisheng 	/* FIXME: delay a bit for card to be ready for next tuning due to errors */
8430322191eSDong Aisheng 	mdelay(1);
8440322191eSDong Aisheng 
8450322191eSDong Aisheng 	reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
8460322191eSDong Aisheng 	reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
8470322191eSDong Aisheng 			ESDHC_MIX_CTRL_FBCLK_SEL;
8480322191eSDong Aisheng 	writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
8490322191eSDong Aisheng 	writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
8500322191eSDong Aisheng 	dev_dbg(mmc_dev(host->mmc),
851d04f8d5bSBenoît Thébaudeau 		"tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
8520322191eSDong Aisheng 			val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
8530322191eSDong Aisheng }
8540322191eSDong Aisheng 
8550322191eSDong Aisheng static void esdhc_post_tuning(struct sdhci_host *host)
8560322191eSDong Aisheng {
8570322191eSDong Aisheng 	u32 reg;
8580322191eSDong Aisheng 
8590322191eSDong Aisheng 	reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
8600322191eSDong Aisheng 	reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
861da0295ffSDong Aisheng 	reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
8620322191eSDong Aisheng 	writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
8630322191eSDong Aisheng }
8640322191eSDong Aisheng 
8650322191eSDong Aisheng static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
8660322191eSDong Aisheng {
8670322191eSDong Aisheng 	int min, max, avg, ret;
8680322191eSDong Aisheng 
8690322191eSDong Aisheng 	/* find the mininum delay first which can pass tuning */
8700322191eSDong Aisheng 	min = ESDHC_TUNE_CTRL_MIN;
8710322191eSDong Aisheng 	while (min < ESDHC_TUNE_CTRL_MAX) {
8720322191eSDong Aisheng 		esdhc_prepare_tuning(host, min);
8739979dbe5SChaotian Jing 		if (!mmc_send_tuning(host->mmc, opcode, NULL))
8740322191eSDong Aisheng 			break;
8750322191eSDong Aisheng 		min += ESDHC_TUNE_CTRL_STEP;
8760322191eSDong Aisheng 	}
8770322191eSDong Aisheng 
8780322191eSDong Aisheng 	/* find the maxinum delay which can not pass tuning */
8790322191eSDong Aisheng 	max = min + ESDHC_TUNE_CTRL_STEP;
8800322191eSDong Aisheng 	while (max < ESDHC_TUNE_CTRL_MAX) {
8810322191eSDong Aisheng 		esdhc_prepare_tuning(host, max);
8829979dbe5SChaotian Jing 		if (mmc_send_tuning(host->mmc, opcode, NULL)) {
8830322191eSDong Aisheng 			max -= ESDHC_TUNE_CTRL_STEP;
8840322191eSDong Aisheng 			break;
8850322191eSDong Aisheng 		}
8860322191eSDong Aisheng 		max += ESDHC_TUNE_CTRL_STEP;
8870322191eSDong Aisheng 	}
8880322191eSDong Aisheng 
8890322191eSDong Aisheng 	/* use average delay to get the best timing */
8900322191eSDong Aisheng 	avg = (min + max) / 2;
8910322191eSDong Aisheng 	esdhc_prepare_tuning(host, avg);
8929979dbe5SChaotian Jing 	ret = mmc_send_tuning(host->mmc, opcode, NULL);
8930322191eSDong Aisheng 	esdhc_post_tuning(host);
8940322191eSDong Aisheng 
895d04f8d5bSBenoît Thébaudeau 	dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
8960322191eSDong Aisheng 		ret ? "failed" : "passed", avg, ret);
8970322191eSDong Aisheng 
8980322191eSDong Aisheng 	return ret;
8990322191eSDong Aisheng }
9000322191eSDong Aisheng 
901ad93220dSDong Aisheng static int esdhc_change_pinstate(struct sdhci_host *host,
902ad93220dSDong Aisheng 						unsigned int uhs)
903ad93220dSDong Aisheng {
904ad93220dSDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
905070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
906ad93220dSDong Aisheng 	struct pinctrl_state *pinctrl;
907ad93220dSDong Aisheng 
908ad93220dSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
909ad93220dSDong Aisheng 
910ad93220dSDong Aisheng 	if (IS_ERR(imx_data->pinctrl) ||
911ad93220dSDong Aisheng 		IS_ERR(imx_data->pins_default) ||
912ad93220dSDong Aisheng 		IS_ERR(imx_data->pins_100mhz) ||
913ad93220dSDong Aisheng 		IS_ERR(imx_data->pins_200mhz))
914ad93220dSDong Aisheng 		return -EINVAL;
915ad93220dSDong Aisheng 
916ad93220dSDong Aisheng 	switch (uhs) {
917ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR50:
9189f327845SHaibo Chen 	case MMC_TIMING_UHS_DDR50:
919ad93220dSDong Aisheng 		pinctrl = imx_data->pins_100mhz;
920ad93220dSDong Aisheng 		break;
921ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR104:
922429a5b45SDong Aisheng 	case MMC_TIMING_MMC_HS200:
92328b07674SHaibo Chen 	case MMC_TIMING_MMC_HS400:
924ad93220dSDong Aisheng 		pinctrl = imx_data->pins_200mhz;
925ad93220dSDong Aisheng 		break;
926ad93220dSDong Aisheng 	default:
927ad93220dSDong Aisheng 		/* back to default state for other legacy timing */
928ad93220dSDong Aisheng 		pinctrl = imx_data->pins_default;
929ad93220dSDong Aisheng 	}
930ad93220dSDong Aisheng 
931ad93220dSDong Aisheng 	return pinctrl_select_state(imx_data->pinctrl, pinctrl);
932ad93220dSDong Aisheng }
933ad93220dSDong Aisheng 
93428b07674SHaibo Chen /*
935d04f8d5bSBenoît Thébaudeau  * For HS400 eMMC, there is a data_strobe line. This signal is generated
93628b07674SHaibo Chen  * by the device and used for data output and CRC status response output
93728b07674SHaibo Chen  * in HS400 mode. The frequency of this signal follows the frequency of
938d04f8d5bSBenoît Thébaudeau  * CLK generated by host. The host receives the data which is aligned to the
93928b07674SHaibo Chen  * edge of data_strobe line. Due to the time delay between CLK line and
94028b07674SHaibo Chen  * data_strobe line, if the delay time is larger than one clock cycle,
941d04f8d5bSBenoît Thébaudeau  * then CLK and data_strobe line will be misaligned, read error shows up.
94228b07674SHaibo Chen  * So when the CLK is higher than 100MHz, each clock cycle is short enough,
943d04f8d5bSBenoît Thébaudeau  * host should configure the delay target.
94428b07674SHaibo Chen  */
94528b07674SHaibo Chen static void esdhc_set_strobe_dll(struct sdhci_host *host)
94628b07674SHaibo Chen {
94728b07674SHaibo Chen 	u32 v;
94828b07674SHaibo Chen 
94928b07674SHaibo Chen 	if (host->mmc->actual_clock > ESDHC_STROBE_DLL_CLK_FREQ) {
9507ac6da26SDong Aisheng 		/* disable clock before enabling strobe dll */
9517ac6da26SDong Aisheng 		writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
9527ac6da26SDong Aisheng 		       ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
9537ac6da26SDong Aisheng 		       host->ioaddr + ESDHC_VENDOR_SPEC);
9547ac6da26SDong Aisheng 
95528b07674SHaibo Chen 		/* force a reset on strobe dll */
95628b07674SHaibo Chen 		writel(ESDHC_STROBE_DLL_CTRL_RESET,
95728b07674SHaibo Chen 			host->ioaddr + ESDHC_STROBE_DLL_CTRL);
95828b07674SHaibo Chen 		/*
95928b07674SHaibo Chen 		 * enable strobe dll ctrl and adjust the delay target
96028b07674SHaibo Chen 		 * for the uSDHC loopback read clock
96128b07674SHaibo Chen 		 */
96228b07674SHaibo Chen 		v = ESDHC_STROBE_DLL_CTRL_ENABLE |
96328b07674SHaibo Chen 			(7 << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
96428b07674SHaibo Chen 		writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
96528b07674SHaibo Chen 		/* wait 1us to make sure strobe dll status register stable */
96628b07674SHaibo Chen 		udelay(1);
96728b07674SHaibo Chen 		v = readl(host->ioaddr + ESDHC_STROBE_DLL_STATUS);
96828b07674SHaibo Chen 		if (!(v & ESDHC_STROBE_DLL_STS_REF_LOCK))
96928b07674SHaibo Chen 			dev_warn(mmc_dev(host->mmc),
97028b07674SHaibo Chen 				"warning! HS400 strobe DLL status REF not lock!\n");
97128b07674SHaibo Chen 		if (!(v & ESDHC_STROBE_DLL_STS_SLV_LOCK))
97228b07674SHaibo Chen 			dev_warn(mmc_dev(host->mmc),
97328b07674SHaibo Chen 				"warning! HS400 strobe DLL status SLV not lock!\n");
97428b07674SHaibo Chen 	}
97528b07674SHaibo Chen }
97628b07674SHaibo Chen 
977d9370424SHaibo Chen static void esdhc_reset_tuning(struct sdhci_host *host)
978d9370424SHaibo Chen {
979d9370424SHaibo Chen 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
980d9370424SHaibo Chen 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
981d9370424SHaibo Chen 	u32 ctrl;
982d9370424SHaibo Chen 
983d04f8d5bSBenoît Thébaudeau 	/* Reset the tuning circuit */
984d9370424SHaibo Chen 	if (esdhc_is_usdhc(imx_data)) {
985d9370424SHaibo Chen 		if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
986d9370424SHaibo Chen 			ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
987d9370424SHaibo Chen 			ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
988d9370424SHaibo Chen 			ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
989d9370424SHaibo Chen 			writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
990d9370424SHaibo Chen 			writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
991d9370424SHaibo Chen 		} else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
992869f8a69SAdrian Hunter 			ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
993d9370424SHaibo Chen 			ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
994869f8a69SAdrian Hunter 			writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
995d9370424SHaibo Chen 		}
996d9370424SHaibo Chen 	}
997d9370424SHaibo Chen }
998d9370424SHaibo Chen 
999850a29b8SRussell King static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1000ad93220dSDong Aisheng {
100128b07674SHaibo Chen 	u32 m;
1002ad93220dSDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1003070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1004602519b2SDong Aisheng 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1005ad93220dSDong Aisheng 
100628b07674SHaibo Chen 	/* disable ddr mode and disable HS400 mode */
100728b07674SHaibo Chen 	m = readl(host->ioaddr + ESDHC_MIX_CTRL);
100828b07674SHaibo Chen 	m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
100928b07674SHaibo Chen 	imx_data->is_ddr = 0;
101028b07674SHaibo Chen 
1011850a29b8SRussell King 	switch (timing) {
1012ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR12:
1013ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR25:
1014ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR50:
1015ad93220dSDong Aisheng 	case MMC_TIMING_UHS_SDR104:
1016de0a0decSBOUGH CHEN 	case MMC_TIMING_MMC_HS:
1017429a5b45SDong Aisheng 	case MMC_TIMING_MMC_HS200:
101828b07674SHaibo Chen 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1019ad93220dSDong Aisheng 		break;
1020ad93220dSDong Aisheng 	case MMC_TIMING_UHS_DDR50:
102169f5bf38SAisheng Dong 	case MMC_TIMING_MMC_DDR52:
102228b07674SHaibo Chen 		m |= ESDHC_MIX_CTRL_DDREN;
102328b07674SHaibo Chen 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1024de5bdbffSDong Aisheng 		imx_data->is_ddr = 1;
1025602519b2SDong Aisheng 		if (boarddata->delay_line) {
1026602519b2SDong Aisheng 			u32 v;
1027602519b2SDong Aisheng 			v = boarddata->delay_line <<
1028602519b2SDong Aisheng 				ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1029602519b2SDong Aisheng 				(1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1030602519b2SDong Aisheng 			if (is_imx53_esdhc(imx_data))
1031602519b2SDong Aisheng 				v <<= 1;
1032602519b2SDong Aisheng 			writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1033602519b2SDong Aisheng 		}
1034ad93220dSDong Aisheng 		break;
103528b07674SHaibo Chen 	case MMC_TIMING_MMC_HS400:
103628b07674SHaibo Chen 		m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
103728b07674SHaibo Chen 		writel(m, host->ioaddr + ESDHC_MIX_CTRL);
103828b07674SHaibo Chen 		imx_data->is_ddr = 1;
10397ac6da26SDong Aisheng 		/* update clock after enable DDR for strobe DLL lock */
10407ac6da26SDong Aisheng 		host->ops->set_clock(host, host->clock);
104128b07674SHaibo Chen 		esdhc_set_strobe_dll(host);
104228b07674SHaibo Chen 		break;
1043d9370424SHaibo Chen 	case MMC_TIMING_LEGACY:
1044d9370424SHaibo Chen 	default:
1045d9370424SHaibo Chen 		esdhc_reset_tuning(host);
1046d9370424SHaibo Chen 		break;
1047ad93220dSDong Aisheng 	}
1048ad93220dSDong Aisheng 
1049850a29b8SRussell King 	esdhc_change_pinstate(host, timing);
1050ad93220dSDong Aisheng }
1051ad93220dSDong Aisheng 
10520718e59aSRussell King static void esdhc_reset(struct sdhci_host *host, u8 mask)
10530718e59aSRussell King {
10540718e59aSRussell King 	sdhci_reset(host, mask);
10550718e59aSRussell King 
10560718e59aSRussell King 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
10570718e59aSRussell King 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
10580718e59aSRussell King }
10590718e59aSRussell King 
106010fd0ad9SAisheng Dong static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
106110fd0ad9SAisheng Dong {
106210fd0ad9SAisheng Dong 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1063070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
106410fd0ad9SAisheng Dong 
1065d04f8d5bSBenoît Thébaudeau 	/* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
10662fb0b02bSHaibo Chen 	return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
106710fd0ad9SAisheng Dong }
106810fd0ad9SAisheng Dong 
1069e33eb8e2SAisheng Dong static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1070e33eb8e2SAisheng Dong {
1071e33eb8e2SAisheng Dong 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1072070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1073e33eb8e2SAisheng Dong 
1074e33eb8e2SAisheng Dong 	/* use maximum timeout counter */
1075a215186dSHaibo Chen 	esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1076a215186dSHaibo Chen 			esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1077e33eb8e2SAisheng Dong 			SDHCI_TIMEOUT_CONTROL);
1078e33eb8e2SAisheng Dong }
1079e33eb8e2SAisheng Dong 
10806e9fd28eSDong Aisheng static struct sdhci_ops sdhci_esdhc_ops = {
1081e149860dSRichard Zhu 	.read_l = esdhc_readl_le,
10820c6d49ceSWolfram Sang 	.read_w = esdhc_readw_le,
108377da3da0SAaron Brice 	.read_b = esdhc_readb_le,
1084e149860dSRichard Zhu 	.write_l = esdhc_writel_le,
10850c6d49ceSWolfram Sang 	.write_w = esdhc_writew_le,
10860c6d49ceSWolfram Sang 	.write_b = esdhc_writeb_le,
10878ba9580aSLucas Stach 	.set_clock = esdhc_pltfm_set_clock,
10880ddf03c9SLucas Stach 	.get_max_clock = esdhc_pltfm_get_max_clock,
10890c6d49ceSWolfram Sang 	.get_min_clock = esdhc_pltfm_get_min_clock,
109010fd0ad9SAisheng Dong 	.get_max_timeout_count = esdhc_get_max_timeout_count,
1091913413c3SShawn Guo 	.get_ro = esdhc_pltfm_get_ro,
1092e33eb8e2SAisheng Dong 	.set_timeout = esdhc_set_timeout,
10932317f56cSRussell King 	.set_bus_width = esdhc_pltfm_set_bus_width,
1094ad93220dSDong Aisheng 	.set_uhs_signaling = esdhc_set_uhs_signaling,
10950718e59aSRussell King 	.reset = esdhc_reset,
10960c6d49ceSWolfram Sang };
10970c6d49ceSWolfram Sang 
10981db5eebfSLars-Peter Clausen static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
109997e4ba6aSRichard Zhu 	.quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
110097e4ba6aSRichard Zhu 			| SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
110197e4ba6aSRichard Zhu 			| SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
110285d6509dSShawn Guo 			| SDHCI_QUIRK_BROKEN_CARD_DETECTION,
110385d6509dSShawn Guo 	.ops = &sdhci_esdhc_ops,
110485d6509dSShawn Guo };
110585d6509dSShawn Guo 
1106f3f5cf3dSDong Aisheng static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1107f3f5cf3dSDong Aisheng {
1108f3f5cf3dSDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1109f3f5cf3dSDong Aisheng 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
11102b16cf32SDong Aisheng 	int tmp;
1111f3f5cf3dSDong Aisheng 
1112f3f5cf3dSDong Aisheng 	if (esdhc_is_usdhc(imx_data)) {
1113f3f5cf3dSDong Aisheng 		/*
1114f3f5cf3dSDong Aisheng 		 * The imx6q ROM code will change the default watermark
1115f3f5cf3dSDong Aisheng 		 * level setting to something insane.  Change it back here.
1116f3f5cf3dSDong Aisheng 		 */
1117f3f5cf3dSDong Aisheng 		writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1118f3f5cf3dSDong Aisheng 
1119f3f5cf3dSDong Aisheng 		/*
1120f3f5cf3dSDong Aisheng 		 * ROM code will change the bit burst_length_enable setting
1121d04f8d5bSBenoît Thébaudeau 		 * to zero if this usdhc is chosen to boot system. Change
1122f3f5cf3dSDong Aisheng 		 * it back here, otherwise it will impact the performance a
1123f3f5cf3dSDong Aisheng 		 * lot. This bit is used to enable/disable the burst length
1124d04f8d5bSBenoît Thébaudeau 		 * for the external AHB2AXI bridge. It's useful especially
1125f3f5cf3dSDong Aisheng 		 * for INCR transfer because without burst length indicator,
1126f3f5cf3dSDong Aisheng 		 * the AHB2AXI bridge does not know the burst length in
1127f3f5cf3dSDong Aisheng 		 * advance. And without burst length indicator, AHB INCR
1128f3f5cf3dSDong Aisheng 		 * transfer can only be converted to singles on the AXI side.
1129f3f5cf3dSDong Aisheng 		 */
1130f3f5cf3dSDong Aisheng 		writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1131f3f5cf3dSDong Aisheng 			| ESDHC_BURST_LEN_EN_INCR,
1132f3f5cf3dSDong Aisheng 			host->ioaddr + SDHCI_HOST_CONTROL);
1133f3f5cf3dSDong Aisheng 		/*
1134d04f8d5bSBenoît Thébaudeau 		* erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1135f3f5cf3dSDong Aisheng 		* TO1.1, it's harmless for MX6SL
1136f3f5cf3dSDong Aisheng 		*/
1137f3f5cf3dSDong Aisheng 		writel(readl(host->ioaddr + 0x6c) | BIT(7),
1138f3f5cf3dSDong Aisheng 			host->ioaddr + 0x6c);
1139f3f5cf3dSDong Aisheng 
1140f3f5cf3dSDong Aisheng 		/* disable DLL_CTRL delay line settings */
1141f3f5cf3dSDong Aisheng 		writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
11422b16cf32SDong Aisheng 
11432b16cf32SDong Aisheng 		if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
11442b16cf32SDong Aisheng 			tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
11452b16cf32SDong Aisheng 			tmp |= ESDHC_STD_TUNING_EN |
11462b16cf32SDong Aisheng 				ESDHC_TUNING_START_TAP_DEFAULT;
11472b16cf32SDong Aisheng 			if (imx_data->boarddata.tuning_start_tap) {
11482b16cf32SDong Aisheng 				tmp &= ~ESDHC_TUNING_START_TAP_MASK;
11492b16cf32SDong Aisheng 				tmp |= imx_data->boarddata.tuning_start_tap;
11502b16cf32SDong Aisheng 			}
11512b16cf32SDong Aisheng 
11522b16cf32SDong Aisheng 			if (imx_data->boarddata.tuning_step) {
11532b16cf32SDong Aisheng 				tmp &= ~ESDHC_TUNING_STEP_MASK;
11542b16cf32SDong Aisheng 				tmp |= imx_data->boarddata.tuning_step
11552b16cf32SDong Aisheng 					<< ESDHC_TUNING_STEP_SHIFT;
11562b16cf32SDong Aisheng 			}
11572b16cf32SDong Aisheng 			writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1158a98c557eSBOUGH CHEN 		} else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1159a98c557eSBOUGH CHEN 			/*
1160a98c557eSBOUGH CHEN 			 * ESDHC_STD_TUNING_EN may be configed in bootloader
1161a98c557eSBOUGH CHEN 			 * or ROM code, so clear this bit here to make sure
1162a98c557eSBOUGH CHEN 			 * the manual tuning can work.
1163a98c557eSBOUGH CHEN 			 */
1164a98c557eSBOUGH CHEN 			tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1165a98c557eSBOUGH CHEN 			tmp &= ~ESDHC_STD_TUNING_EN;
1166a98c557eSBOUGH CHEN 			writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
11672b16cf32SDong Aisheng 		}
1168f3f5cf3dSDong Aisheng 	}
1169f3f5cf3dSDong Aisheng }
1170f3f5cf3dSDong Aisheng 
1171abfafc2dSShawn Guo #ifdef CONFIG_OF
1172c3be1efdSBill Pemberton static int
1173abfafc2dSShawn Guo sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
117407bf2b54SSascha Hauer 			 struct sdhci_host *host,
117591fa4252SDong Aisheng 			 struct pltfm_imx_data *imx_data)
1176abfafc2dSShawn Guo {
1177abfafc2dSShawn Guo 	struct device_node *np = pdev->dev.of_node;
117891fa4252SDong Aisheng 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
11794800e87aSDong Aisheng 	int ret;
1180abfafc2dSShawn Guo 
1181abfafc2dSShawn Guo 	if (of_get_property(np, "fsl,wp-controller", NULL))
1182abfafc2dSShawn Guo 		boarddata->wp_type = ESDHC_WP_CONTROLLER;
1183abfafc2dSShawn Guo 
118474ff81e1SLinus Walleij 	/*
118574ff81e1SLinus Walleij 	 * If we have this property, then activate WP check.
118674ff81e1SLinus Walleij 	 * Retrieveing and requesting the actual WP GPIO will happen
118774ff81e1SLinus Walleij 	 * in the call to mmc_of_parse().
118874ff81e1SLinus Walleij 	 */
118974ff81e1SLinus Walleij 	if (of_property_read_bool(np, "wp-gpios"))
1190abfafc2dSShawn Guo 		boarddata->wp_type = ESDHC_WP_GPIO;
1191abfafc2dSShawn Guo 
1192d407e30bSHaibo Chen 	of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1193d87fc966SDong Aisheng 	of_property_read_u32(np, "fsl,tuning-start-tap",
1194d87fc966SDong Aisheng 			     &boarddata->tuning_start_tap);
1195d407e30bSHaibo Chen 
1196ad93220dSDong Aisheng 	if (of_find_property(np, "no-1-8-v", NULL))
119786f495c5SStefan Agner 		host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1198ad93220dSDong Aisheng 
1199602519b2SDong Aisheng 	if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1200602519b2SDong Aisheng 		boarddata->delay_line = 0;
1201602519b2SDong Aisheng 
120207bf2b54SSascha Hauer 	mmc_of_parse_voltage(np, &host->ocr_mask);
120307bf2b54SSascha Hauer 
120486f495c5SStefan Agner 	if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pins_default)) {
120591fa4252SDong Aisheng 		imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
120691fa4252SDong Aisheng 						ESDHC_PINCTRL_STATE_100MHZ);
120791fa4252SDong Aisheng 		imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
120891fa4252SDong Aisheng 						ESDHC_PINCTRL_STATE_200MHZ);
120991fa4252SDong Aisheng 	}
121091fa4252SDong Aisheng 
121115064119SFabio Estevam 	/* call to generic mmc_of_parse to support additional capabilities */
12124800e87aSDong Aisheng 	ret = mmc_of_parse(host->mmc);
12134800e87aSDong Aisheng 	if (ret)
12144800e87aSDong Aisheng 		return ret;
12154800e87aSDong Aisheng 
1216287980e4SArnd Bergmann 	if (mmc_gpio_get_cd(host->mmc) >= 0)
12174800e87aSDong Aisheng 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
12184800e87aSDong Aisheng 
12194800e87aSDong Aisheng 	return 0;
1220abfafc2dSShawn Guo }
1221abfafc2dSShawn Guo #else
1222abfafc2dSShawn Guo static inline int
1223abfafc2dSShawn Guo sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
122407bf2b54SSascha Hauer 			 struct sdhci_host *host,
122591fa4252SDong Aisheng 			 struct pltfm_imx_data *imx_data)
1226abfafc2dSShawn Guo {
1227abfafc2dSShawn Guo 	return -ENODEV;
1228abfafc2dSShawn Guo }
1229abfafc2dSShawn Guo #endif
1230abfafc2dSShawn Guo 
123191fa4252SDong Aisheng static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
123291fa4252SDong Aisheng 			 struct sdhci_host *host,
123391fa4252SDong Aisheng 			 struct pltfm_imx_data *imx_data)
123491fa4252SDong Aisheng {
123591fa4252SDong Aisheng 	struct esdhc_platform_data *boarddata = &imx_data->boarddata;
123691fa4252SDong Aisheng 	int err;
123791fa4252SDong Aisheng 
123891fa4252SDong Aisheng 	if (!host->mmc->parent->platform_data) {
123991fa4252SDong Aisheng 		dev_err(mmc_dev(host->mmc), "no board data!\n");
124091fa4252SDong Aisheng 		return -EINVAL;
124191fa4252SDong Aisheng 	}
124291fa4252SDong Aisheng 
124391fa4252SDong Aisheng 	imx_data->boarddata = *((struct esdhc_platform_data *)
124491fa4252SDong Aisheng 				host->mmc->parent->platform_data);
124591fa4252SDong Aisheng 	/* write_protect */
124691fa4252SDong Aisheng 	if (boarddata->wp_type == ESDHC_WP_GPIO) {
124774ff81e1SLinus Walleij 		err = mmc_gpiod_request_ro(host->mmc, "wp", 0, false, 0, NULL);
124891fa4252SDong Aisheng 		if (err) {
124991fa4252SDong Aisheng 			dev_err(mmc_dev(host->mmc),
125091fa4252SDong Aisheng 				"failed to request write-protect gpio!\n");
125191fa4252SDong Aisheng 			return err;
125291fa4252SDong Aisheng 		}
125391fa4252SDong Aisheng 		host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
125491fa4252SDong Aisheng 	}
125591fa4252SDong Aisheng 
125691fa4252SDong Aisheng 	/* card_detect */
125791fa4252SDong Aisheng 	switch (boarddata->cd_type) {
125891fa4252SDong Aisheng 	case ESDHC_CD_GPIO:
125974ff81e1SLinus Walleij 		err = mmc_gpiod_request_cd(host->mmc, "cd", 0, false, 0, NULL);
126091fa4252SDong Aisheng 		if (err) {
126191fa4252SDong Aisheng 			dev_err(mmc_dev(host->mmc),
126291fa4252SDong Aisheng 				"failed to request card-detect gpio!\n");
126391fa4252SDong Aisheng 			return err;
126491fa4252SDong Aisheng 		}
126591fa4252SDong Aisheng 		/* fall through */
126691fa4252SDong Aisheng 
126791fa4252SDong Aisheng 	case ESDHC_CD_CONTROLLER:
126891fa4252SDong Aisheng 		/* we have a working card_detect back */
126991fa4252SDong Aisheng 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
127091fa4252SDong Aisheng 		break;
127191fa4252SDong Aisheng 
127291fa4252SDong Aisheng 	case ESDHC_CD_PERMANENT:
127391fa4252SDong Aisheng 		host->mmc->caps |= MMC_CAP_NONREMOVABLE;
127491fa4252SDong Aisheng 		break;
127591fa4252SDong Aisheng 
127691fa4252SDong Aisheng 	case ESDHC_CD_NONE:
127791fa4252SDong Aisheng 		break;
127891fa4252SDong Aisheng 	}
127991fa4252SDong Aisheng 
128091fa4252SDong Aisheng 	switch (boarddata->max_bus_width) {
128191fa4252SDong Aisheng 	case 8:
128291fa4252SDong Aisheng 		host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
128391fa4252SDong Aisheng 		break;
128491fa4252SDong Aisheng 	case 4:
128591fa4252SDong Aisheng 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
128691fa4252SDong Aisheng 		break;
128791fa4252SDong Aisheng 	case 1:
128891fa4252SDong Aisheng 	default:
128991fa4252SDong Aisheng 		host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
129091fa4252SDong Aisheng 		break;
129191fa4252SDong Aisheng 	}
129291fa4252SDong Aisheng 
129391fa4252SDong Aisheng 	return 0;
129491fa4252SDong Aisheng }
129591fa4252SDong Aisheng 
1296c3be1efdSBill Pemberton static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
129795f25efeSWolfram Sang {
1298abfafc2dSShawn Guo 	const struct of_device_id *of_id =
1299abfafc2dSShawn Guo 			of_match_device(imx_esdhc_dt_ids, &pdev->dev);
130085d6509dSShawn Guo 	struct sdhci_pltfm_host *pltfm_host;
130185d6509dSShawn Guo 	struct sdhci_host *host;
13020c6d49ceSWolfram Sang 	int err;
1303e149860dSRichard Zhu 	struct pltfm_imx_data *imx_data;
130495f25efeSWolfram Sang 
1305070e6d3fSJisheng Zhang 	host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1306070e6d3fSJisheng Zhang 				sizeof(*imx_data));
130785d6509dSShawn Guo 	if (IS_ERR(host))
130885d6509dSShawn Guo 		return PTR_ERR(host);
130985d6509dSShawn Guo 
131085d6509dSShawn Guo 	pltfm_host = sdhci_priv(host);
131185d6509dSShawn Guo 
1312070e6d3fSJisheng Zhang 	imx_data = sdhci_pltfm_priv(pltfm_host);
131357ed3314SShawn Guo 
1314f47c4bbfSShawn Guo 	imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
13153770ee8fSShawn Guo 						  pdev->id_entry->driver_data;
131685d6509dSShawn Guo 
131752dac615SSascha Hauer 	imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
131852dac615SSascha Hauer 	if (IS_ERR(imx_data->clk_ipg)) {
131952dac615SSascha Hauer 		err = PTR_ERR(imx_data->clk_ipg);
1320e3af31c6SShawn Guo 		goto free_sdhci;
132195f25efeSWolfram Sang 	}
132252dac615SSascha Hauer 
132352dac615SSascha Hauer 	imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
132452dac615SSascha Hauer 	if (IS_ERR(imx_data->clk_ahb)) {
132552dac615SSascha Hauer 		err = PTR_ERR(imx_data->clk_ahb);
1326e3af31c6SShawn Guo 		goto free_sdhci;
132752dac615SSascha Hauer 	}
132852dac615SSascha Hauer 
132952dac615SSascha Hauer 	imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
133052dac615SSascha Hauer 	if (IS_ERR(imx_data->clk_per)) {
133152dac615SSascha Hauer 		err = PTR_ERR(imx_data->clk_per);
1332e3af31c6SShawn Guo 		goto free_sdhci;
133352dac615SSascha Hauer 	}
133452dac615SSascha Hauer 
133552dac615SSascha Hauer 	pltfm_host->clk = imx_data->clk_per;
1336a974862fSDong Aisheng 	pltfm_host->clock = clk_get_rate(pltfm_host->clk);
133717b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_per);
133817b1eb7fSFabio Estevam 	if (err)
133917b1eb7fSFabio Estevam 		goto free_sdhci;
134017b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_ipg);
134117b1eb7fSFabio Estevam 	if (err)
134217b1eb7fSFabio Estevam 		goto disable_per_clk;
134317b1eb7fSFabio Estevam 	err = clk_prepare_enable(imx_data->clk_ahb);
134417b1eb7fSFabio Estevam 	if (err)
134517b1eb7fSFabio Estevam 		goto disable_ipg_clk;
134695f25efeSWolfram Sang 
1347ad93220dSDong Aisheng 	imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1348e62d8b8fSDong Aisheng 	if (IS_ERR(imx_data->pinctrl)) {
1349e62d8b8fSDong Aisheng 		err = PTR_ERR(imx_data->pinctrl);
135017b1eb7fSFabio Estevam 		goto disable_ahb_clk;
1351e62d8b8fSDong Aisheng 	}
1352e62d8b8fSDong Aisheng 
1353ad93220dSDong Aisheng 	imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
1354ad93220dSDong Aisheng 						PINCTRL_STATE_DEFAULT);
1355cd529af7SDirk Behme 	if (IS_ERR(imx_data->pins_default))
1356cd529af7SDirk Behme 		dev_warn(mmc_dev(host->mmc), "could not get default state\n");
1357ad93220dSDong Aisheng 
135869ed60e0SDong Aisheng 	if (esdhc_is_usdhc(imx_data)) {
135969ed60e0SDong Aisheng 		host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
136009c8192bSStefan Agner 		host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
13614245afffSDong Aisheng 		if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
13624245afffSDong Aisheng 			host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1363a75dcbf4SDong Aisheng 
1364a75dcbf4SDong Aisheng 		/* clear tuning bits in case ROM has set it already */
1365a75dcbf4SDong Aisheng 		writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1366869f8a69SAdrian Hunter 		writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1367a75dcbf4SDong Aisheng 		writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1368de3e1dd0SBOUGH CHEN 
1369de3e1dd0SBOUGH CHEN 		/*
1370de3e1dd0SBOUGH CHEN 		 * Link usdhc specific mmc_host_ops execute_tuning function,
1371de3e1dd0SBOUGH CHEN 		 * to replace the standard one in sdhci_ops.
1372de3e1dd0SBOUGH CHEN 		 */
1373de3e1dd0SBOUGH CHEN 		host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
137469ed60e0SDong Aisheng 	}
1375f750ba9bSShawn Guo 
13766e9fd28eSDong Aisheng 	if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
13776e9fd28eSDong Aisheng 		sdhci_esdhc_ops.platform_execute_tuning =
13786e9fd28eSDong Aisheng 					esdhc_executing_tuning;
13798b2bb0adSDong Aisheng 
138018094430SDong Aisheng 	if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
138118094430SDong Aisheng 		host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
138218094430SDong Aisheng 
138328b07674SHaibo Chen 	if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
138428b07674SHaibo Chen 		host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
138528b07674SHaibo Chen 
138691fa4252SDong Aisheng 	if (of_id)
138791fa4252SDong Aisheng 		err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
138891fa4252SDong Aisheng 	else
138991fa4252SDong Aisheng 		err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
139091fa4252SDong Aisheng 	if (err)
139117b1eb7fSFabio Estevam 		goto disable_ahb_clk;
1392ad93220dSDong Aisheng 
1393*d00ab101SBOUGH CHEN 	host->tuning_delay = 1;
1394*d00ab101SBOUGH CHEN 
1395f3f5cf3dSDong Aisheng 	sdhci_esdhc_imx_hwinit(host);
1396f3f5cf3dSDong Aisheng 
139785d6509dSShawn Guo 	err = sdhci_add_host(host);
139885d6509dSShawn Guo 	if (err)
139917b1eb7fSFabio Estevam 		goto disable_ahb_clk;
140085d6509dSShawn Guo 
140189d7e5c1SDong Aisheng 	pm_runtime_set_active(&pdev->dev);
140289d7e5c1SDong Aisheng 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
140389d7e5c1SDong Aisheng 	pm_runtime_use_autosuspend(&pdev->dev);
140489d7e5c1SDong Aisheng 	pm_suspend_ignore_children(&pdev->dev, 1);
140577903c01SUlf Hansson 	pm_runtime_enable(&pdev->dev);
140689d7e5c1SDong Aisheng 
14077e29c306SWolfram Sang 	return 0;
14087e29c306SWolfram Sang 
140917b1eb7fSFabio Estevam disable_ahb_clk:
141052dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_ahb);
141117b1eb7fSFabio Estevam disable_ipg_clk:
141217b1eb7fSFabio Estevam 	clk_disable_unprepare(imx_data->clk_ipg);
141317b1eb7fSFabio Estevam disable_per_clk:
141417b1eb7fSFabio Estevam 	clk_disable_unprepare(imx_data->clk_per);
1415e3af31c6SShawn Guo free_sdhci:
141685d6509dSShawn Guo 	sdhci_pltfm_free(pdev);
141785d6509dSShawn Guo 	return err;
141895f25efeSWolfram Sang }
141995f25efeSWolfram Sang 
14206e0ee714SBill Pemberton static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
142195f25efeSWolfram Sang {
142285d6509dSShawn Guo 	struct sdhci_host *host = platform_get_drvdata(pdev);
142395f25efeSWolfram Sang 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1424070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
142585d6509dSShawn Guo 	int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
142685d6509dSShawn Guo 
14270b414368SUlf Hansson 	pm_runtime_get_sync(&pdev->dev);
14280b414368SUlf Hansson 	pm_runtime_disable(&pdev->dev);
14290b414368SUlf Hansson 	pm_runtime_put_noidle(&pdev->dev);
14300b414368SUlf Hansson 
143185d6509dSShawn Guo 	sdhci_remove_host(host, dead);
14320c6d49ceSWolfram Sang 
143352dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_per);
143452dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_ipg);
143552dac615SSascha Hauer 	clk_disable_unprepare(imx_data->clk_ahb);
143652dac615SSascha Hauer 
143785d6509dSShawn Guo 	sdhci_pltfm_free(pdev);
143885d6509dSShawn Guo 
143985d6509dSShawn Guo 	return 0;
144095f25efeSWolfram Sang }
144195f25efeSWolfram Sang 
14422788ed42SUlf Hansson #ifdef CONFIG_PM_SLEEP
144304143fbaSDong Aisheng static int sdhci_esdhc_suspend(struct device *dev)
144404143fbaSDong Aisheng {
14453e3274abSUlf Hansson 	struct sdhci_host *host = dev_get_drvdata(dev);
14463e3274abSUlf Hansson 
1447d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1448d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
1449d38dcad4SAdrian Hunter 
14503e3274abSUlf Hansson 	return sdhci_suspend_host(host);
145104143fbaSDong Aisheng }
145204143fbaSDong Aisheng 
145304143fbaSDong Aisheng static int sdhci_esdhc_resume(struct device *dev)
145404143fbaSDong Aisheng {
1455cc17e129SDong Aisheng 	struct sdhci_host *host = dev_get_drvdata(dev);
1456cc17e129SDong Aisheng 
145719dbfdd3SDong Aisheng 	/* re-initialize hw state in case it's lost in low power mode */
145819dbfdd3SDong Aisheng 	sdhci_esdhc_imx_hwinit(host);
1459cc17e129SDong Aisheng 
14603e3274abSUlf Hansson 	return sdhci_resume_host(host);
146104143fbaSDong Aisheng }
14622788ed42SUlf Hansson #endif
146304143fbaSDong Aisheng 
14642788ed42SUlf Hansson #ifdef CONFIG_PM
146589d7e5c1SDong Aisheng static int sdhci_esdhc_runtime_suspend(struct device *dev)
146689d7e5c1SDong Aisheng {
146789d7e5c1SDong Aisheng 	struct sdhci_host *host = dev_get_drvdata(dev);
146889d7e5c1SDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1469070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
147089d7e5c1SDong Aisheng 	int ret;
147189d7e5c1SDong Aisheng 
147289d7e5c1SDong Aisheng 	ret = sdhci_runtime_suspend_host(host);
1473371d39faSMichael Trimarchi 	if (ret)
1474371d39faSMichael Trimarchi 		return ret;
147589d7e5c1SDong Aisheng 
1476d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1477d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
1478d38dcad4SAdrian Hunter 
1479be138554SRussell King 	if (!sdhci_sdio_irq_enabled(host)) {
14803602785bSMichael Trimarchi 		imx_data->actual_clock = host->mmc->actual_clock;
14813602785bSMichael Trimarchi 		esdhc_pltfm_set_clock(host, 0);
148289d7e5c1SDong Aisheng 		clk_disable_unprepare(imx_data->clk_per);
148389d7e5c1SDong Aisheng 		clk_disable_unprepare(imx_data->clk_ipg);
1484be138554SRussell King 	}
148589d7e5c1SDong Aisheng 	clk_disable_unprepare(imx_data->clk_ahb);
148689d7e5c1SDong Aisheng 
148789d7e5c1SDong Aisheng 	return ret;
148889d7e5c1SDong Aisheng }
148989d7e5c1SDong Aisheng 
149089d7e5c1SDong Aisheng static int sdhci_esdhc_runtime_resume(struct device *dev)
149189d7e5c1SDong Aisheng {
149289d7e5c1SDong Aisheng 	struct sdhci_host *host = dev_get_drvdata(dev);
149389d7e5c1SDong Aisheng 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1494070e6d3fSJisheng Zhang 	struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
149517b1eb7fSFabio Estevam 	int err;
149689d7e5c1SDong Aisheng 
1497a0ad3087SMichael Trimarchi 	err = clk_prepare_enable(imx_data->clk_ahb);
1498a0ad3087SMichael Trimarchi 	if (err)
1499a0ad3087SMichael Trimarchi 		return err;
1500a0ad3087SMichael Trimarchi 
1501be138554SRussell King 	if (!sdhci_sdio_irq_enabled(host)) {
150217b1eb7fSFabio Estevam 		err = clk_prepare_enable(imx_data->clk_per);
150317b1eb7fSFabio Estevam 		if (err)
1504a0ad3087SMichael Trimarchi 			goto disable_ahb_clk;
150517b1eb7fSFabio Estevam 		err = clk_prepare_enable(imx_data->clk_ipg);
150617b1eb7fSFabio Estevam 		if (err)
150717b1eb7fSFabio Estevam 			goto disable_per_clk;
15083602785bSMichael Trimarchi 		esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1509be138554SRussell King 	}
1510a0ad3087SMichael Trimarchi 
151117b1eb7fSFabio Estevam 	err = sdhci_runtime_resume_host(host);
151217b1eb7fSFabio Estevam 	if (err)
1513a0ad3087SMichael Trimarchi 		goto disable_ipg_clk;
151489d7e5c1SDong Aisheng 
151517b1eb7fSFabio Estevam 	return 0;
151617b1eb7fSFabio Estevam 
151717b1eb7fSFabio Estevam disable_ipg_clk:
151817b1eb7fSFabio Estevam 	if (!sdhci_sdio_irq_enabled(host))
151917b1eb7fSFabio Estevam 		clk_disable_unprepare(imx_data->clk_ipg);
152017b1eb7fSFabio Estevam disable_per_clk:
152117b1eb7fSFabio Estevam 	if (!sdhci_sdio_irq_enabled(host))
152217b1eb7fSFabio Estevam 		clk_disable_unprepare(imx_data->clk_per);
1523a0ad3087SMichael Trimarchi disable_ahb_clk:
1524a0ad3087SMichael Trimarchi 	clk_disable_unprepare(imx_data->clk_ahb);
152517b1eb7fSFabio Estevam 	return err;
152689d7e5c1SDong Aisheng }
152789d7e5c1SDong Aisheng #endif
152889d7e5c1SDong Aisheng 
152989d7e5c1SDong Aisheng static const struct dev_pm_ops sdhci_esdhc_pmops = {
153004143fbaSDong Aisheng 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
153189d7e5c1SDong Aisheng 	SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
153289d7e5c1SDong Aisheng 				sdhci_esdhc_runtime_resume, NULL)
153389d7e5c1SDong Aisheng };
153489d7e5c1SDong Aisheng 
153585d6509dSShawn Guo static struct platform_driver sdhci_esdhc_imx_driver = {
153685d6509dSShawn Guo 	.driver		= {
153785d6509dSShawn Guo 		.name	= "sdhci-esdhc-imx",
1538abfafc2dSShawn Guo 		.of_match_table = imx_esdhc_dt_ids,
153989d7e5c1SDong Aisheng 		.pm	= &sdhci_esdhc_pmops,
154085d6509dSShawn Guo 	},
154157ed3314SShawn Guo 	.id_table	= imx_esdhc_devtype,
154285d6509dSShawn Guo 	.probe		= sdhci_esdhc_imx_probe,
15430433c143SBill Pemberton 	.remove		= sdhci_esdhc_imx_remove,
154495f25efeSWolfram Sang };
154585d6509dSShawn Guo 
1546d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_imx_driver);
154785d6509dSShawn Guo 
154885d6509dSShawn Guo MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1549035ff831SWolfram Sang MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
155085d6509dSShawn Guo MODULE_LICENSE("GPL v2");
1551