xref: /openbmc/linux/drivers/mmc/host/sdhci-of-esdhc.c (revision 54e08d9a95ca54a192a9aef76742a4f7e81f6780)
17657c3a7SAlbert Herranz /*
27657c3a7SAlbert Herranz  * Freescale eSDHC controller driver.
37657c3a7SAlbert Herranz  *
4f060bc9cSJerry Huang  * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
57657c3a7SAlbert Herranz  * Copyright (c) 2009 MontaVista Software, Inc.
67657c3a7SAlbert Herranz  *
77657c3a7SAlbert Herranz  * Authors: Xiaobo Xie <X.Xie@freescale.com>
87657c3a7SAlbert Herranz  *	    Anton Vorontsov <avorontsov@ru.mvista.com>
97657c3a7SAlbert Herranz  *
107657c3a7SAlbert Herranz  * This program is free software; you can redistribute it and/or modify
117657c3a7SAlbert Herranz  * it under the terms of the GNU General Public License as published by
127657c3a7SAlbert Herranz  * the Free Software Foundation; either version 2 of the License, or (at
137657c3a7SAlbert Herranz  * your option) any later version.
147657c3a7SAlbert Herranz  */
157657c3a7SAlbert Herranz 
1666b50a00SOded Gabbay #include <linux/err.h>
177657c3a7SAlbert Herranz #include <linux/io.h>
18f060bc9cSJerry Huang #include <linux/of.h>
19ea35645aSyangbo lu #include <linux/of_address.h>
207657c3a7SAlbert Herranz #include <linux/delay.h>
2188b47679SPaul Gortmaker #include <linux/module.h>
22151ede40Syangbo lu #include <linux/sys_soc.h>
2319c3a0efSyangbo lu #include <linux/clk.h>
2419c3a0efSyangbo lu #include <linux/ktime.h>
255552d7adSLaurentiu Tudor #include <linux/dma-mapping.h>
267657c3a7SAlbert Herranz #include <linux/mmc/host.h>
2738576af1SShawn Guo #include "sdhci-pltfm.h"
2880872e21SWolfram Sang #include "sdhci-esdhc.h"
297657c3a7SAlbert Herranz 
30137ccd46SJerry Huang #define VENDOR_V_22	0x12
31a4071fbbSHaijun Zhang #define VENDOR_V_23	0x13
32f4932cfdSyangbo lu 
3367fdfbdfSyinbo.zhu #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
3467fdfbdfSyinbo.zhu 
3567fdfbdfSyinbo.zhu struct esdhc_clk_fixup {
3667fdfbdfSyinbo.zhu 	const unsigned int sd_dflt_max_clk;
3767fdfbdfSyinbo.zhu 	const unsigned int max_clk[MMC_TIMING_NUM];
3867fdfbdfSyinbo.zhu };
3967fdfbdfSyinbo.zhu 
4067fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1021a_esdhc_clk = {
4167fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 25000000,
4267fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS] = 46500000,
4367fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_SD_HS] = 46500000,
4467fdfbdfSyinbo.zhu };
4567fdfbdfSyinbo.zhu 
4667fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1046a_esdhc_clk = {
4767fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 25000000,
4867fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_UHS_SDR104] = 167000000,
4967fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS200] = 167000000,
5067fdfbdfSyinbo.zhu };
5167fdfbdfSyinbo.zhu 
5267fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1012a_esdhc_clk = {
5367fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 25000000,
5467fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_UHS_SDR104] = 125000000,
5567fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS200] = 125000000,
5667fdfbdfSyinbo.zhu };
5767fdfbdfSyinbo.zhu 
5867fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup p1010_esdhc_clk = {
5967fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 20000000,
6067fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_LEGACY] = 20000000,
6167fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS] = 42000000,
6267fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_SD_HS] = 40000000,
6367fdfbdfSyinbo.zhu };
6467fdfbdfSyinbo.zhu 
6567fdfbdfSyinbo.zhu static const struct of_device_id sdhci_esdhc_of_match[] = {
6667fdfbdfSyinbo.zhu 	{ .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk},
6767fdfbdfSyinbo.zhu 	{ .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk},
6867fdfbdfSyinbo.zhu 	{ .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk},
6967fdfbdfSyinbo.zhu 	{ .compatible = "fsl,p1010-esdhc",   .data = &p1010_esdhc_clk},
7067fdfbdfSyinbo.zhu 	{ .compatible = "fsl,mpc8379-esdhc" },
7167fdfbdfSyinbo.zhu 	{ .compatible = "fsl,mpc8536-esdhc" },
7267fdfbdfSyinbo.zhu 	{ .compatible = "fsl,esdhc" },
7367fdfbdfSyinbo.zhu 	{ }
7467fdfbdfSyinbo.zhu };
7567fdfbdfSyinbo.zhu MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
7667fdfbdfSyinbo.zhu 
77f4932cfdSyangbo lu struct sdhci_esdhc {
78f4932cfdSyangbo lu 	u8 vendor_ver;
79f4932cfdSyangbo lu 	u8 spec_ver;
80151ede40Syangbo lu 	bool quirk_incorrect_hostver;
81b1f378abSYinbo Zhu 	bool quirk_fixup_tuning;
8219c3a0efSyangbo lu 	unsigned int peripheral_clock;
8367fdfbdfSyinbo.zhu 	const struct esdhc_clk_fixup *clk_fixup;
84b1f378abSYinbo Zhu 	u32 div_ratio;
85f4932cfdSyangbo lu };
86f4932cfdSyangbo lu 
87f4932cfdSyangbo lu /**
88f4932cfdSyangbo lu  * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
89f4932cfdSyangbo lu  *		       to make it compatible with SD spec.
90f4932cfdSyangbo lu  *
91f4932cfdSyangbo lu  * @host: pointer to sdhci_host
92f4932cfdSyangbo lu  * @spec_reg: SD spec register address
93f4932cfdSyangbo lu  * @value: 32bit eSDHC register value on spec_reg address
94f4932cfdSyangbo lu  *
95f4932cfdSyangbo lu  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
96f4932cfdSyangbo lu  * registers are 32 bits. There are differences in register size, register
97f4932cfdSyangbo lu  * address, register function, bit position and function between eSDHC spec
98f4932cfdSyangbo lu  * and SD spec.
99f4932cfdSyangbo lu  *
100f4932cfdSyangbo lu  * Return a fixed up register value
101f4932cfdSyangbo lu  */
102f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host,
103f4932cfdSyangbo lu 				     int spec_reg, u32 value)
104137ccd46SJerry Huang {
105f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1068605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
107137ccd46SJerry Huang 	u32 ret;
108137ccd46SJerry Huang 
109137ccd46SJerry Huang 	/*
110137ccd46SJerry Huang 	 * The bit of ADMA flag in eSDHC is not compatible with standard
111137ccd46SJerry Huang 	 * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
112137ccd46SJerry Huang 	 * supported by eSDHC.
113137ccd46SJerry Huang 	 * And for many FSL eSDHC controller, the reset value of field
114f4932cfdSyangbo lu 	 * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
115137ccd46SJerry Huang 	 * only these vendor version is greater than 2.2/0x12 support ADMA.
116137ccd46SJerry Huang 	 */
117f4932cfdSyangbo lu 	if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
118f4932cfdSyangbo lu 		if (esdhc->vendor_ver > VENDOR_V_22) {
119f4932cfdSyangbo lu 			ret = value | SDHCI_CAN_DO_ADMA2;
120f4932cfdSyangbo lu 			return ret;
121137ccd46SJerry Huang 		}
122f4932cfdSyangbo lu 	}
123b0921d5cSMichael Walle 	/*
124b0921d5cSMichael Walle 	 * The DAT[3:0] line signal levels and the CMD line signal level are
125b0921d5cSMichael Walle 	 * not compatible with standard SDHC register. The line signal levels
126b0921d5cSMichael Walle 	 * DAT[7:0] are at bits 31:24 and the command line signal level is at
127b0921d5cSMichael Walle 	 * bit 23. All other bits are the same as in the standard SDHC
128b0921d5cSMichael Walle 	 * register.
129b0921d5cSMichael Walle 	 */
130b0921d5cSMichael Walle 	if (spec_reg == SDHCI_PRESENT_STATE) {
131b0921d5cSMichael Walle 		ret = value & 0x000fffff;
132b0921d5cSMichael Walle 		ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
133b0921d5cSMichael Walle 		ret |= (value << 1) & SDHCI_CMD_LVL;
134b0921d5cSMichael Walle 		return ret;
135b0921d5cSMichael Walle 	}
136b0921d5cSMichael Walle 
1372f3110ccSyangbo lu 	/*
1382f3110ccSyangbo lu 	 * DTS properties of mmc host are used to enable each speed mode
1392f3110ccSyangbo lu 	 * according to soc and board capability. So clean up
1402f3110ccSyangbo lu 	 * SDR50/SDR104/DDR50 support bits here.
1412f3110ccSyangbo lu 	 */
1422f3110ccSyangbo lu 	if (spec_reg == SDHCI_CAPABILITIES_1) {
1432f3110ccSyangbo lu 		ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
1442f3110ccSyangbo lu 				SDHCI_SUPPORT_DDR50);
1452f3110ccSyangbo lu 		return ret;
1462f3110ccSyangbo lu 	}
1472f3110ccSyangbo lu 
148f4932cfdSyangbo lu 	ret = value;
149137ccd46SJerry Huang 	return ret;
150137ccd46SJerry Huang }
151137ccd46SJerry Huang 
152f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host,
153f4932cfdSyangbo lu 				     int spec_reg, u32 value)
1547657c3a7SAlbert Herranz {
155151ede40Syangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
156151ede40Syangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
1577657c3a7SAlbert Herranz 	u16 ret;
158f4932cfdSyangbo lu 	int shift = (spec_reg & 0x2) * 8;
1597657c3a7SAlbert Herranz 
160f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_VERSION)
161f4932cfdSyangbo lu 		ret = value & 0xffff;
1627657c3a7SAlbert Herranz 	else
163f4932cfdSyangbo lu 		ret = (value >> shift) & 0xffff;
164151ede40Syangbo lu 	/* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
165151ede40Syangbo lu 	 * vendor version and spec version information.
166151ede40Syangbo lu 	 */
167151ede40Syangbo lu 	if ((spec_reg == SDHCI_HOST_VERSION) &&
168151ede40Syangbo lu 	    (esdhc->quirk_incorrect_hostver))
169151ede40Syangbo lu 		ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
170e51cbc9eSXu lei 	return ret;
171e51cbc9eSXu lei }
172e51cbc9eSXu lei 
173f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host,
174f4932cfdSyangbo lu 				     int spec_reg, u32 value)
175e51cbc9eSXu lei {
176f4932cfdSyangbo lu 	u8 ret;
177f4932cfdSyangbo lu 	u8 dma_bits;
178f4932cfdSyangbo lu 	int shift = (spec_reg & 0x3) * 8;
179f4932cfdSyangbo lu 
180f4932cfdSyangbo lu 	ret = (value >> shift) & 0xff;
181ba8c4dc9SRoy Zang 
182ba8c4dc9SRoy Zang 	/*
183ba8c4dc9SRoy Zang 	 * "DMA select" locates at offset 0x28 in SD specification, but on
184ba8c4dc9SRoy Zang 	 * P5020 or P3041, it locates at 0x29.
185ba8c4dc9SRoy Zang 	 */
186f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_CONTROL) {
187ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
188f4932cfdSyangbo lu 		dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
189ba8c4dc9SRoy Zang 		/* fixup the result */
190ba8c4dc9SRoy Zang 		ret &= ~SDHCI_CTRL_DMA_MASK;
191ba8c4dc9SRoy Zang 		ret |= dma_bits;
192ba8c4dc9SRoy Zang 	}
193f4932cfdSyangbo lu 	return ret;
194f4932cfdSyangbo lu }
195f4932cfdSyangbo lu 
196f4932cfdSyangbo lu /**
197f4932cfdSyangbo lu  * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
198f4932cfdSyangbo lu  *			written into eSDHC register.
199f4932cfdSyangbo lu  *
200f4932cfdSyangbo lu  * @host: pointer to sdhci_host
201f4932cfdSyangbo lu  * @spec_reg: SD spec register address
202f4932cfdSyangbo lu  * @value: 8/16/32bit SD spec register value that would be written
203f4932cfdSyangbo lu  * @old_value: 32bit eSDHC register value on spec_reg address
204f4932cfdSyangbo lu  *
205f4932cfdSyangbo lu  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
206f4932cfdSyangbo lu  * registers are 32 bits. There are differences in register size, register
207f4932cfdSyangbo lu  * address, register function, bit position and function between eSDHC spec
208f4932cfdSyangbo lu  * and SD spec.
209f4932cfdSyangbo lu  *
210f4932cfdSyangbo lu  * Return a fixed up register value
211f4932cfdSyangbo lu  */
212f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host,
213f4932cfdSyangbo lu 				     int spec_reg, u32 value, u32 old_value)
214f4932cfdSyangbo lu {
215f4932cfdSyangbo lu 	u32 ret;
216f4932cfdSyangbo lu 
217f4932cfdSyangbo lu 	/*
218f4932cfdSyangbo lu 	 * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
219f4932cfdSyangbo lu 	 * when SYSCTL[RSTD] is set for some special operations.
220f4932cfdSyangbo lu 	 * No any impact on other operation.
221f4932cfdSyangbo lu 	 */
222f4932cfdSyangbo lu 	if (spec_reg == SDHCI_INT_ENABLE)
223f4932cfdSyangbo lu 		ret = value | SDHCI_INT_BLK_GAP;
224f4932cfdSyangbo lu 	else
225f4932cfdSyangbo lu 		ret = value;
226ba8c4dc9SRoy Zang 
2277657c3a7SAlbert Herranz 	return ret;
2287657c3a7SAlbert Herranz }
2297657c3a7SAlbert Herranz 
230f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host,
231f4932cfdSyangbo lu 				     int spec_reg, u16 value, u32 old_value)
232a4071fbbSHaijun Zhang {
233f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
234f4932cfdSyangbo lu 	int shift = (spec_reg & 0x2) * 8;
235f4932cfdSyangbo lu 	u32 ret;
236f4932cfdSyangbo lu 
237f4932cfdSyangbo lu 	switch (spec_reg) {
238f4932cfdSyangbo lu 	case SDHCI_TRANSFER_MODE:
239a4071fbbSHaijun Zhang 		/*
240f4932cfdSyangbo lu 		 * Postpone this write, we must do it together with a
241f4932cfdSyangbo lu 		 * command write that is down below. Return old value.
242a4071fbbSHaijun Zhang 		 */
243f4932cfdSyangbo lu 		pltfm_host->xfer_mode_shadow = value;
244f4932cfdSyangbo lu 		return old_value;
245f4932cfdSyangbo lu 	case SDHCI_COMMAND:
246f4932cfdSyangbo lu 		ret = (value << 16) | pltfm_host->xfer_mode_shadow;
247f4932cfdSyangbo lu 		return ret;
248a4071fbbSHaijun Zhang 	}
249a4071fbbSHaijun Zhang 
250f4932cfdSyangbo lu 	ret = old_value & (~(0xffff << shift));
251f4932cfdSyangbo lu 	ret |= (value << shift);
252f4932cfdSyangbo lu 
253f4932cfdSyangbo lu 	if (spec_reg == SDHCI_BLOCK_SIZE) {
2547657c3a7SAlbert Herranz 		/*
2557657c3a7SAlbert Herranz 		 * Two last DMA bits are reserved, and first one is used for
2567657c3a7SAlbert Herranz 		 * non-standard blksz of 4096 bytes that we don't support
2577657c3a7SAlbert Herranz 		 * yet. So clear the DMA boundary bits.
2587657c3a7SAlbert Herranz 		 */
259f4932cfdSyangbo lu 		ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
2607657c3a7SAlbert Herranz 	}
261f4932cfdSyangbo lu 	return ret;
2627657c3a7SAlbert Herranz }
2637657c3a7SAlbert Herranz 
264f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host,
265f4932cfdSyangbo lu 				     int spec_reg, u8 value, u32 old_value)
2667657c3a7SAlbert Herranz {
267f4932cfdSyangbo lu 	u32 ret;
268f4932cfdSyangbo lu 	u32 dma_bits;
269f4932cfdSyangbo lu 	u8 tmp;
270f4932cfdSyangbo lu 	int shift = (spec_reg & 0x3) * 8;
271f4932cfdSyangbo lu 
272ba8c4dc9SRoy Zang 	/*
2739e4703dfSyangbo lu 	 * eSDHC doesn't have a standard power control register, so we do
2749e4703dfSyangbo lu 	 * nothing here to avoid incorrect operation.
2759e4703dfSyangbo lu 	 */
2769e4703dfSyangbo lu 	if (spec_reg == SDHCI_POWER_CONTROL)
2779e4703dfSyangbo lu 		return old_value;
2789e4703dfSyangbo lu 	/*
279ba8c4dc9SRoy Zang 	 * "DMA select" location is offset 0x28 in SD specification, but on
280ba8c4dc9SRoy Zang 	 * P5020 or P3041, it's located at 0x29.
281ba8c4dc9SRoy Zang 	 */
282f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_CONTROL) {
283dcaff04dSOded Gabbay 		/*
284dcaff04dSOded Gabbay 		 * If host control register is not standard, exit
285dcaff04dSOded Gabbay 		 * this function
286dcaff04dSOded Gabbay 		 */
287dcaff04dSOded Gabbay 		if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
288f4932cfdSyangbo lu 			return old_value;
289dcaff04dSOded Gabbay 
290ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
291f4932cfdSyangbo lu 		dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
292f4932cfdSyangbo lu 		ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
293f4932cfdSyangbo lu 		tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
294f4932cfdSyangbo lu 		      (old_value & SDHCI_CTRL_DMA_MASK);
295f4932cfdSyangbo lu 		ret = (ret & (~0xff)) | tmp;
296f4932cfdSyangbo lu 
297f4932cfdSyangbo lu 		/* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
298f4932cfdSyangbo lu 		ret &= ~ESDHC_HOST_CONTROL_RES;
299f4932cfdSyangbo lu 		return ret;
300ba8c4dc9SRoy Zang 	}
301ba8c4dc9SRoy Zang 
302f4932cfdSyangbo lu 	ret = (old_value & (~(0xff << shift))) | (value << shift);
303f4932cfdSyangbo lu 	return ret;
304f4932cfdSyangbo lu }
305f4932cfdSyangbo lu 
306f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
307f4932cfdSyangbo lu {
308f4932cfdSyangbo lu 	u32 ret;
309f4932cfdSyangbo lu 	u32 value;
310f4932cfdSyangbo lu 
3112f3110ccSyangbo lu 	if (reg == SDHCI_CAPABILITIES_1)
3122f3110ccSyangbo lu 		value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
3132f3110ccSyangbo lu 	else
314f4932cfdSyangbo lu 		value = ioread32be(host->ioaddr + reg);
3152f3110ccSyangbo lu 
316f4932cfdSyangbo lu 	ret = esdhc_readl_fixup(host, reg, value);
317f4932cfdSyangbo lu 
318f4932cfdSyangbo lu 	return ret;
319f4932cfdSyangbo lu }
320f4932cfdSyangbo lu 
321f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
322f4932cfdSyangbo lu {
323f4932cfdSyangbo lu 	u32 ret;
324f4932cfdSyangbo lu 	u32 value;
325f4932cfdSyangbo lu 
3262f3110ccSyangbo lu 	if (reg == SDHCI_CAPABILITIES_1)
3272f3110ccSyangbo lu 		value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
3282f3110ccSyangbo lu 	else
329f4932cfdSyangbo lu 		value = ioread32(host->ioaddr + reg);
3302f3110ccSyangbo lu 
331f4932cfdSyangbo lu 	ret = esdhc_readl_fixup(host, reg, value);
332f4932cfdSyangbo lu 
333f4932cfdSyangbo lu 	return ret;
334f4932cfdSyangbo lu }
335f4932cfdSyangbo lu 
336f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
337f4932cfdSyangbo lu {
338f4932cfdSyangbo lu 	u16 ret;
339f4932cfdSyangbo lu 	u32 value;
340f4932cfdSyangbo lu 	int base = reg & ~0x3;
341f4932cfdSyangbo lu 
342f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
343f4932cfdSyangbo lu 	ret = esdhc_readw_fixup(host, reg, value);
344f4932cfdSyangbo lu 	return ret;
345f4932cfdSyangbo lu }
346f4932cfdSyangbo lu 
347f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
348f4932cfdSyangbo lu {
349f4932cfdSyangbo lu 	u16 ret;
350f4932cfdSyangbo lu 	u32 value;
351f4932cfdSyangbo lu 	int base = reg & ~0x3;
352f4932cfdSyangbo lu 
353f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
354f4932cfdSyangbo lu 	ret = esdhc_readw_fixup(host, reg, value);
355f4932cfdSyangbo lu 	return ret;
356f4932cfdSyangbo lu }
357f4932cfdSyangbo lu 
358f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
359f4932cfdSyangbo lu {
360f4932cfdSyangbo lu 	u8 ret;
361f4932cfdSyangbo lu 	u32 value;
362f4932cfdSyangbo lu 	int base = reg & ~0x3;
363f4932cfdSyangbo lu 
364f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
365f4932cfdSyangbo lu 	ret = esdhc_readb_fixup(host, reg, value);
366f4932cfdSyangbo lu 	return ret;
367f4932cfdSyangbo lu }
368f4932cfdSyangbo lu 
369f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
370f4932cfdSyangbo lu {
371f4932cfdSyangbo lu 	u8 ret;
372f4932cfdSyangbo lu 	u32 value;
373f4932cfdSyangbo lu 	int base = reg & ~0x3;
374f4932cfdSyangbo lu 
375f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
376f4932cfdSyangbo lu 	ret = esdhc_readb_fixup(host, reg, value);
377f4932cfdSyangbo lu 	return ret;
378f4932cfdSyangbo lu }
379f4932cfdSyangbo lu 
380f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
381f4932cfdSyangbo lu {
382f4932cfdSyangbo lu 	u32 value;
383f4932cfdSyangbo lu 
384f4932cfdSyangbo lu 	value = esdhc_writel_fixup(host, reg, val, 0);
385f4932cfdSyangbo lu 	iowrite32be(value, host->ioaddr + reg);
386f4932cfdSyangbo lu }
387f4932cfdSyangbo lu 
388f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
389f4932cfdSyangbo lu {
390f4932cfdSyangbo lu 	u32 value;
391f4932cfdSyangbo lu 
392f4932cfdSyangbo lu 	value = esdhc_writel_fixup(host, reg, val, 0);
393f4932cfdSyangbo lu 	iowrite32(value, host->ioaddr + reg);
394f4932cfdSyangbo lu }
395f4932cfdSyangbo lu 
396f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
397f4932cfdSyangbo lu {
398f4932cfdSyangbo lu 	int base = reg & ~0x3;
399f4932cfdSyangbo lu 	u32 value;
400f4932cfdSyangbo lu 	u32 ret;
401f4932cfdSyangbo lu 
402f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
403f4932cfdSyangbo lu 	ret = esdhc_writew_fixup(host, reg, val, value);
404f4932cfdSyangbo lu 	if (reg != SDHCI_TRANSFER_MODE)
405f4932cfdSyangbo lu 		iowrite32be(ret, host->ioaddr + base);
406f4932cfdSyangbo lu }
407f4932cfdSyangbo lu 
408f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
409f4932cfdSyangbo lu {
410f4932cfdSyangbo lu 	int base = reg & ~0x3;
411f4932cfdSyangbo lu 	u32 value;
412f4932cfdSyangbo lu 	u32 ret;
413f4932cfdSyangbo lu 
414f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
415f4932cfdSyangbo lu 	ret = esdhc_writew_fixup(host, reg, val, value);
416f4932cfdSyangbo lu 	if (reg != SDHCI_TRANSFER_MODE)
417f4932cfdSyangbo lu 		iowrite32(ret, host->ioaddr + base);
418f4932cfdSyangbo lu }
419f4932cfdSyangbo lu 
420f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
421f4932cfdSyangbo lu {
422f4932cfdSyangbo lu 	int base = reg & ~0x3;
423f4932cfdSyangbo lu 	u32 value;
424f4932cfdSyangbo lu 	u32 ret;
425f4932cfdSyangbo lu 
426f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
427f4932cfdSyangbo lu 	ret = esdhc_writeb_fixup(host, reg, val, value);
428f4932cfdSyangbo lu 	iowrite32be(ret, host->ioaddr + base);
429f4932cfdSyangbo lu }
430f4932cfdSyangbo lu 
431f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
432f4932cfdSyangbo lu {
433f4932cfdSyangbo lu 	int base = reg & ~0x3;
434f4932cfdSyangbo lu 	u32 value;
435f4932cfdSyangbo lu 	u32 ret;
436f4932cfdSyangbo lu 
437f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
438f4932cfdSyangbo lu 	ret = esdhc_writeb_fixup(host, reg, val, value);
439f4932cfdSyangbo lu 	iowrite32(ret, host->ioaddr + base);
4407657c3a7SAlbert Herranz }
4417657c3a7SAlbert Herranz 
442a4071fbbSHaijun Zhang /*
443a4071fbbSHaijun Zhang  * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
444a4071fbbSHaijun Zhang  * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
445a4071fbbSHaijun Zhang  * and Block Gap Event(IRQSTAT[BGE]) are also set.
446a4071fbbSHaijun Zhang  * For Continue, apply soft reset for data(SYSCTL[RSTD]);
447a4071fbbSHaijun Zhang  * and re-issue the entire read transaction from beginning.
448a4071fbbSHaijun Zhang  */
449f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
450a4071fbbSHaijun Zhang {
451f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
4528605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
453a4071fbbSHaijun Zhang 	bool applicable;
454a4071fbbSHaijun Zhang 	dma_addr_t dmastart;
455a4071fbbSHaijun Zhang 	dma_addr_t dmanow;
456a4071fbbSHaijun Zhang 
457a4071fbbSHaijun Zhang 	applicable = (intmask & SDHCI_INT_DATA_END) &&
458a4071fbbSHaijun Zhang 		     (intmask & SDHCI_INT_BLK_GAP) &&
459f4932cfdSyangbo lu 		     (esdhc->vendor_ver == VENDOR_V_23);
460a4071fbbSHaijun Zhang 	if (!applicable)
461a4071fbbSHaijun Zhang 		return;
462a4071fbbSHaijun Zhang 
463a4071fbbSHaijun Zhang 	host->data->error = 0;
464a4071fbbSHaijun Zhang 	dmastart = sg_dma_address(host->data->sg);
465a4071fbbSHaijun Zhang 	dmanow = dmastart + host->data->bytes_xfered;
466a4071fbbSHaijun Zhang 	/*
467a4071fbbSHaijun Zhang 	 * Force update to the next DMA block boundary.
468a4071fbbSHaijun Zhang 	 */
469a4071fbbSHaijun Zhang 	dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
470a4071fbbSHaijun Zhang 		SDHCI_DEFAULT_BOUNDARY_SIZE;
471a4071fbbSHaijun Zhang 	host->data->bytes_xfered = dmanow - dmastart;
472a4071fbbSHaijun Zhang 	sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
473a4071fbbSHaijun Zhang }
474a4071fbbSHaijun Zhang 
47580872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host)
4767657c3a7SAlbert Herranz {
477f4932cfdSyangbo lu 	u32 value;
4785552d7adSLaurentiu Tudor 	struct device *dev = mmc_dev(host->mmc);
4795552d7adSLaurentiu Tudor 
4805552d7adSLaurentiu Tudor 	if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
4815552d7adSLaurentiu Tudor 	    of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc"))
4825552d7adSLaurentiu Tudor 		dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
483f4932cfdSyangbo lu 
484f4932cfdSyangbo lu 	value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
485f4932cfdSyangbo lu 	value |= ESDHC_DMA_SNOOP;
486f4932cfdSyangbo lu 	sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
4877657c3a7SAlbert Herranz 	return 0;
4887657c3a7SAlbert Herranz }
4897657c3a7SAlbert Herranz 
49080872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
4917657c3a7SAlbert Herranz {
492e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
49319c3a0efSyangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
4947657c3a7SAlbert Herranz 
49519c3a0efSyangbo lu 	if (esdhc->peripheral_clock)
49619c3a0efSyangbo lu 		return esdhc->peripheral_clock;
49719c3a0efSyangbo lu 	else
498e307148fSShawn Guo 		return pltfm_host->clock;
4997657c3a7SAlbert Herranz }
5007657c3a7SAlbert Herranz 
50180872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
5027657c3a7SAlbert Herranz {
503e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
50419c3a0efSyangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
50519c3a0efSyangbo lu 	unsigned int clock;
5067657c3a7SAlbert Herranz 
50719c3a0efSyangbo lu 	if (esdhc->peripheral_clock)
50819c3a0efSyangbo lu 		clock = esdhc->peripheral_clock;
50919c3a0efSyangbo lu 	else
51019c3a0efSyangbo lu 		clock = pltfm_host->clock;
51119c3a0efSyangbo lu 	return clock / 256 / 16;
5127657c3a7SAlbert Herranz }
5137657c3a7SAlbert Herranz 
514dd3f6983Syangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
515dd3f6983Syangbo lu {
516dd3f6983Syangbo lu 	u32 val;
517dd3f6983Syangbo lu 	ktime_t timeout;
518dd3f6983Syangbo lu 
519dd3f6983Syangbo lu 	val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
520dd3f6983Syangbo lu 
521dd3f6983Syangbo lu 	if (enable)
522dd3f6983Syangbo lu 		val |= ESDHC_CLOCK_SDCLKEN;
523dd3f6983Syangbo lu 	else
524dd3f6983Syangbo lu 		val &= ~ESDHC_CLOCK_SDCLKEN;
525dd3f6983Syangbo lu 
526dd3f6983Syangbo lu 	sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
527dd3f6983Syangbo lu 
528dd3f6983Syangbo lu 	/* Wait max 20 ms */
529dd3f6983Syangbo lu 	timeout = ktime_add_ms(ktime_get(), 20);
530dd3f6983Syangbo lu 	val = ESDHC_CLOCK_STABLE;
531dd3f6983Syangbo lu 	while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) {
532dd3f6983Syangbo lu 		if (ktime_after(ktime_get(), timeout)) {
533dd3f6983Syangbo lu 			pr_err("%s: Internal clock never stabilised.\n",
534dd3f6983Syangbo lu 				mmc_hostname(host->mmc));
535dd3f6983Syangbo lu 			break;
536dd3f6983Syangbo lu 		}
537dd3f6983Syangbo lu 		udelay(10);
538dd3f6983Syangbo lu 	}
539dd3f6983Syangbo lu }
540dd3f6983Syangbo lu 
541f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
542f060bc9cSJerry Huang {
543f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
5448605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
545bd455029SJoakim Tjernlund 	int pre_div = 1;
546d31fc00aSDong Aisheng 	int div = 1;
547e145ac45Syangbo lu 	ktime_t timeout;
54867fdfbdfSyinbo.zhu 	long fixup = 0;
549d31fc00aSDong Aisheng 	u32 temp;
550d31fc00aSDong Aisheng 
5511650d0c7SRussell King 	host->mmc->actual_clock = 0;
5521650d0c7SRussell King 
553dd3f6983Syangbo lu 	if (clock == 0) {
554dd3f6983Syangbo lu 		esdhc_clock_enable(host, false);
555373073efSRussell King 		return;
556dd3f6983Syangbo lu 	}
557d31fc00aSDong Aisheng 
55877bd2f6fSYangbo Lu 	/* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
559f4932cfdSyangbo lu 	if (esdhc->vendor_ver < VENDOR_V_23)
56077bd2f6fSYangbo Lu 		pre_div = 2;
56177bd2f6fSYangbo Lu 
56267fdfbdfSyinbo.zhu 	if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
56367fdfbdfSyinbo.zhu 		esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
56467fdfbdfSyinbo.zhu 		fixup = esdhc->clk_fixup->sd_dflt_max_clk;
56567fdfbdfSyinbo.zhu 	else if (esdhc->clk_fixup)
56667fdfbdfSyinbo.zhu 		fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
567a627f025Syangbo lu 
56867fdfbdfSyinbo.zhu 	if (fixup && clock > fixup)
56967fdfbdfSyinbo.zhu 		clock = fixup;
570f060bc9cSJerry Huang 
571d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
572e87d2db2Syangbo lu 	temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
573e87d2db2Syangbo lu 		  ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
574d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
575d31fc00aSDong Aisheng 
576d31fc00aSDong Aisheng 	while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
577d31fc00aSDong Aisheng 		pre_div *= 2;
578d31fc00aSDong Aisheng 
579d31fc00aSDong Aisheng 	while (host->max_clk / pre_div / div > clock && div < 16)
580d31fc00aSDong Aisheng 		div++;
581d31fc00aSDong Aisheng 
582d31fc00aSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
583e76b8559SDong Aisheng 		clock, host->max_clk / pre_div / div);
584bd455029SJoakim Tjernlund 	host->mmc->actual_clock = host->max_clk / pre_div / div;
585b1f378abSYinbo Zhu 	esdhc->div_ratio = pre_div * div;
586d31fc00aSDong Aisheng 	pre_div >>= 1;
587d31fc00aSDong Aisheng 	div--;
588d31fc00aSDong Aisheng 
589d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
590d31fc00aSDong Aisheng 	temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
591d31fc00aSDong Aisheng 		| (div << ESDHC_DIVIDER_SHIFT)
592d31fc00aSDong Aisheng 		| (pre_div << ESDHC_PREDIV_SHIFT));
593d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
594e87d2db2Syangbo lu 
595*54e08d9aSYangbo Lu 	if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 &&
596*54e08d9aSYangbo Lu 	    clock == MMC_HS200_MAX_DTR) {
597*54e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_TBCTL);
598*54e08d9aSYangbo Lu 		sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL);
599*54e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_SDCLKCTL);
600*54e08d9aSYangbo Lu 		sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL);
601*54e08d9aSYangbo Lu 		esdhc_clock_enable(host, true);
602*54e08d9aSYangbo Lu 
603*54e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_DLLCFG0);
604*54e08d9aSYangbo Lu 		temp |= ESDHC_DLL_ENABLE | ESDHC_DLL_FREQ_SEL;
605*54e08d9aSYangbo Lu 		sdhci_writel(host, temp, ESDHC_DLLCFG0);
606*54e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_TBCTL);
607*54e08d9aSYangbo Lu 		sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL);
608*54e08d9aSYangbo Lu 
609*54e08d9aSYangbo Lu 		esdhc_clock_enable(host, false);
610*54e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_DMA_SYSCTL);
611*54e08d9aSYangbo Lu 		temp |= ESDHC_FLUSH_ASYNC_FIFO;
612*54e08d9aSYangbo Lu 		sdhci_writel(host, temp, ESDHC_DMA_SYSCTL);
613*54e08d9aSYangbo Lu 	}
614*54e08d9aSYangbo Lu 
615e87d2db2Syangbo lu 	/* Wait max 20 ms */
616e145ac45Syangbo lu 	timeout = ktime_add_ms(ktime_get(), 20);
617e87d2db2Syangbo lu 	while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) {
618e145ac45Syangbo lu 		if (ktime_after(ktime_get(), timeout)) {
619e87d2db2Syangbo lu 			pr_err("%s: Internal clock never stabilised.\n",
620e87d2db2Syangbo lu 				mmc_hostname(host->mmc));
621e87d2db2Syangbo lu 			return;
622e87d2db2Syangbo lu 		}
623e145ac45Syangbo lu 		udelay(10);
624f060bc9cSJerry Huang 	}
625f060bc9cSJerry Huang 
626*54e08d9aSYangbo Lu 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
627e87d2db2Syangbo lu 	temp |= ESDHC_CLOCK_SDCLKEN;
628e87d2db2Syangbo lu 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
629e87d2db2Syangbo lu }
630e87d2db2Syangbo lu 
6312317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
63266b50a00SOded Gabbay {
63366b50a00SOded Gabbay 	u32 ctrl;
63466b50a00SOded Gabbay 
635f4932cfdSyangbo lu 	ctrl = sdhci_readl(host, ESDHC_PROCTL);
636f4932cfdSyangbo lu 	ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
63766b50a00SOded Gabbay 	switch (width) {
63866b50a00SOded Gabbay 	case MMC_BUS_WIDTH_8:
639f4932cfdSyangbo lu 		ctrl |= ESDHC_CTRL_8BITBUS;
64066b50a00SOded Gabbay 		break;
64166b50a00SOded Gabbay 
64266b50a00SOded Gabbay 	case MMC_BUS_WIDTH_4:
643f4932cfdSyangbo lu 		ctrl |= ESDHC_CTRL_4BITBUS;
64466b50a00SOded Gabbay 		break;
64566b50a00SOded Gabbay 
64666b50a00SOded Gabbay 	default:
64766b50a00SOded Gabbay 		break;
64866b50a00SOded Gabbay 	}
64966b50a00SOded Gabbay 
650f4932cfdSyangbo lu 	sdhci_writel(host, ctrl, ESDHC_PROCTL);
65166b50a00SOded Gabbay }
65266b50a00SOded Gabbay 
653304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask)
654304f0a98SAlessio Igor Bogani {
655f2bc6000Syinbo.zhu 	u32 val;
656f2bc6000Syinbo.zhu 
657304f0a98SAlessio Igor Bogani 	sdhci_reset(host, mask);
658304f0a98SAlessio Igor Bogani 
659304f0a98SAlessio Igor Bogani 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
660304f0a98SAlessio Igor Bogani 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
661f2bc6000Syinbo.zhu 
662f2bc6000Syinbo.zhu 	if (mask & SDHCI_RESET_ALL) {
663f2bc6000Syinbo.zhu 		val = sdhci_readl(host, ESDHC_TBCTL);
664f2bc6000Syinbo.zhu 		val &= ~ESDHC_TB_EN;
665f2bc6000Syinbo.zhu 		sdhci_writel(host, val, ESDHC_TBCTL);
666f2bc6000Syinbo.zhu 	}
667304f0a98SAlessio Igor Bogani }
668304f0a98SAlessio Igor Bogani 
669ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific
670ea35645aSyangbo lu  * configuration and status registers for the device. There is a
671ea35645aSyangbo lu  * SDHC IO VSEL control register on SCFG for some platforms. It's
672ea35645aSyangbo lu  * used to support SDHC IO voltage switching.
673ea35645aSyangbo lu  */
674ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = {
675ea35645aSyangbo lu 	{ .compatible = "fsl,t1040-scfg", },
676ea35645aSyangbo lu 	{ .compatible = "fsl,ls1012a-scfg", },
677ea35645aSyangbo lu 	{ .compatible = "fsl,ls1046a-scfg", },
678ea35645aSyangbo lu 	{}
679ea35645aSyangbo lu };
680ea35645aSyangbo lu 
681ea35645aSyangbo lu /* SDHC IO VSEL control register definition */
682ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR	0x408
683ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN	0x80000000
684ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL	0x60000000
685ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS	0x00000001
686ea35645aSyangbo lu 
687ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
688ea35645aSyangbo lu 				       struct mmc_ios *ios)
689ea35645aSyangbo lu {
690ea35645aSyangbo lu 	struct sdhci_host *host = mmc_priv(mmc);
691ea35645aSyangbo lu 	struct device_node *scfg_node;
692ea35645aSyangbo lu 	void __iomem *scfg_base = NULL;
693ea35645aSyangbo lu 	u32 sdhciovselcr;
694ea35645aSyangbo lu 	u32 val;
695ea35645aSyangbo lu 
696ea35645aSyangbo lu 	/*
697ea35645aSyangbo lu 	 * Signal Voltage Switching is only applicable for Host Controllers
698ea35645aSyangbo lu 	 * v3.00 and above.
699ea35645aSyangbo lu 	 */
700ea35645aSyangbo lu 	if (host->version < SDHCI_SPEC_300)
701ea35645aSyangbo lu 		return 0;
702ea35645aSyangbo lu 
703ea35645aSyangbo lu 	val = sdhci_readl(host, ESDHC_PROCTL);
704ea35645aSyangbo lu 
705ea35645aSyangbo lu 	switch (ios->signal_voltage) {
706ea35645aSyangbo lu 	case MMC_SIGNAL_VOLTAGE_330:
707ea35645aSyangbo lu 		val &= ~ESDHC_VOLT_SEL;
708ea35645aSyangbo lu 		sdhci_writel(host, val, ESDHC_PROCTL);
709ea35645aSyangbo lu 		return 0;
710ea35645aSyangbo lu 	case MMC_SIGNAL_VOLTAGE_180:
711ea35645aSyangbo lu 		scfg_node = of_find_matching_node(NULL, scfg_device_ids);
712ea35645aSyangbo lu 		if (scfg_node)
713ea35645aSyangbo lu 			scfg_base = of_iomap(scfg_node, 0);
714ea35645aSyangbo lu 		if (scfg_base) {
715ea35645aSyangbo lu 			sdhciovselcr = SDHCIOVSELCR_TGLEN |
716ea35645aSyangbo lu 				       SDHCIOVSELCR_VSELVAL;
717ea35645aSyangbo lu 			iowrite32be(sdhciovselcr,
718ea35645aSyangbo lu 				scfg_base + SCFG_SDHCIOVSELCR);
719ea35645aSyangbo lu 
720ea35645aSyangbo lu 			val |= ESDHC_VOLT_SEL;
721ea35645aSyangbo lu 			sdhci_writel(host, val, ESDHC_PROCTL);
722ea35645aSyangbo lu 			mdelay(5);
723ea35645aSyangbo lu 
724ea35645aSyangbo lu 			sdhciovselcr = SDHCIOVSELCR_TGLEN |
725ea35645aSyangbo lu 				       SDHCIOVSELCR_SDHC_VS;
726ea35645aSyangbo lu 			iowrite32be(sdhciovselcr,
727ea35645aSyangbo lu 				scfg_base + SCFG_SDHCIOVSELCR);
728ea35645aSyangbo lu 			iounmap(scfg_base);
729ea35645aSyangbo lu 		} else {
730ea35645aSyangbo lu 			val |= ESDHC_VOLT_SEL;
731ea35645aSyangbo lu 			sdhci_writel(host, val, ESDHC_PROCTL);
732ea35645aSyangbo lu 		}
733ea35645aSyangbo lu 		return 0;
734ea35645aSyangbo lu 	default:
735ea35645aSyangbo lu 		return 0;
736ea35645aSyangbo lu 	}
737ea35645aSyangbo lu }
738ea35645aSyangbo lu 
739b1f378abSYinbo Zhu static struct soc_device_attribute soc_fixup_tuning[] = {
740b1f378abSYinbo Zhu 	{ .family = "QorIQ T1040", .revision = "1.0", },
741b1f378abSYinbo Zhu 	{ .family = "QorIQ T2080", .revision = "1.0", },
742b1f378abSYinbo Zhu 	{ .family = "QorIQ T1023", .revision = "1.0", },
743b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1021A", .revision = "1.0", },
744b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1080A", .revision = "1.0", },
745b1f378abSYinbo Zhu 	{ .family = "QorIQ LS2080A", .revision = "1.0", },
746b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1012A", .revision = "1.0", },
747b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1043A", .revision = "1.*", },
748b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1046A", .revision = "1.0", },
749b1f378abSYinbo Zhu 	{ },
750b1f378abSYinbo Zhu };
751b1f378abSYinbo Zhu 
752*54e08d9aSYangbo Lu static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable)
753ba49cbd0Syangbo lu {
754ba49cbd0Syangbo lu 	u32 val;
755ba49cbd0Syangbo lu 
756ba49cbd0Syangbo lu 	esdhc_clock_enable(host, false);
757*54e08d9aSYangbo Lu 
758ba49cbd0Syangbo lu 	val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
759ba49cbd0Syangbo lu 	val |= ESDHC_FLUSH_ASYNC_FIFO;
760ba49cbd0Syangbo lu 	sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
761ba49cbd0Syangbo lu 
762ba49cbd0Syangbo lu 	val = sdhci_readl(host, ESDHC_TBCTL);
763*54e08d9aSYangbo Lu 	if (enable)
764ba49cbd0Syangbo lu 		val |= ESDHC_TB_EN;
765*54e08d9aSYangbo Lu 	else
766*54e08d9aSYangbo Lu 		val &= ~ESDHC_TB_EN;
767ba49cbd0Syangbo lu 	sdhci_writel(host, val, ESDHC_TBCTL);
768ba49cbd0Syangbo lu 
769*54e08d9aSYangbo Lu 	esdhc_clock_enable(host, true);
770*54e08d9aSYangbo Lu }
771*54e08d9aSYangbo Lu 
772*54e08d9aSYangbo Lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
773*54e08d9aSYangbo Lu {
774*54e08d9aSYangbo Lu 	struct sdhci_host *host = mmc_priv(mmc);
775*54e08d9aSYangbo Lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
776*54e08d9aSYangbo Lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
777*54e08d9aSYangbo Lu 	bool hs400_tuning;
778*54e08d9aSYangbo Lu 	u32 val;
779*54e08d9aSYangbo Lu 	int ret;
780*54e08d9aSYangbo Lu 
781*54e08d9aSYangbo Lu 	esdhc_tuning_block_enable(host, true);
782*54e08d9aSYangbo Lu 
783*54e08d9aSYangbo Lu 	hs400_tuning = host->flags & SDHCI_HS400_TUNING;
784*54e08d9aSYangbo Lu 	ret = sdhci_execute_tuning(mmc, opcode);
785*54e08d9aSYangbo Lu 
786*54e08d9aSYangbo Lu 	if (hs400_tuning) {
787*54e08d9aSYangbo Lu 		val = sdhci_readl(host, ESDHC_SDTIMNGCTL);
788*54e08d9aSYangbo Lu 		val |= ESDHC_FLW_CTL_BG;
789*54e08d9aSYangbo Lu 		sdhci_writel(host, val, ESDHC_SDTIMNGCTL);
790*54e08d9aSYangbo Lu 	}
791*54e08d9aSYangbo Lu 
792b1f378abSYinbo Zhu 	if (host->tuning_err == -EAGAIN && esdhc->quirk_fixup_tuning) {
793b1f378abSYinbo Zhu 
794b1f378abSYinbo Zhu 		/* program TBPTR[TB_WNDW_END_PTR] = 3*DIV_RATIO and
795b1f378abSYinbo Zhu 		 * program TBPTR[TB_WNDW_START_PTR] = 5*DIV_RATIO
796b1f378abSYinbo Zhu 		 */
797b1f378abSYinbo Zhu 		val = sdhci_readl(host, ESDHC_TBPTR);
798b1f378abSYinbo Zhu 		val = (val & ~((0x7f << 8) | 0x7f)) |
799b1f378abSYinbo Zhu 		(3 * esdhc->div_ratio) | ((5 * esdhc->div_ratio) << 8);
800b1f378abSYinbo Zhu 		sdhci_writel(host, val, ESDHC_TBPTR);
801b1f378abSYinbo Zhu 
802b1f378abSYinbo Zhu 		/* program the software tuning mode by setting
803b1f378abSYinbo Zhu 		 * TBCTL[TB_MODE]=2'h3
804b1f378abSYinbo Zhu 		 */
805b1f378abSYinbo Zhu 		val = sdhci_readl(host, ESDHC_TBCTL);
806b1f378abSYinbo Zhu 		val |= 0x3;
807b1f378abSYinbo Zhu 		sdhci_writel(host, val, ESDHC_TBCTL);
808b1f378abSYinbo Zhu 		sdhci_execute_tuning(mmc, opcode);
809b1f378abSYinbo Zhu 	}
810*54e08d9aSYangbo Lu 	return ret;
811*54e08d9aSYangbo Lu }
812*54e08d9aSYangbo Lu 
813*54e08d9aSYangbo Lu static void esdhc_set_uhs_signaling(struct sdhci_host *host,
814*54e08d9aSYangbo Lu 				   unsigned int timing)
815*54e08d9aSYangbo Lu {
816*54e08d9aSYangbo Lu 	if (timing == MMC_TIMING_MMC_HS400)
817*54e08d9aSYangbo Lu 		esdhc_tuning_block_enable(host, true);
818*54e08d9aSYangbo Lu 	else
819*54e08d9aSYangbo Lu 		sdhci_set_uhs_signaling(host, timing);
820ba49cbd0Syangbo lu }
821ba49cbd0Syangbo lu 
8229e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP
823723f7924SRussell King static u32 esdhc_proctl;
824723f7924SRussell King static int esdhc_of_suspend(struct device *dev)
825723f7924SRussell King {
826723f7924SRussell King 	struct sdhci_host *host = dev_get_drvdata(dev);
827723f7924SRussell King 
828f4932cfdSyangbo lu 	esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
829723f7924SRussell King 
830d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
831d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
832d38dcad4SAdrian Hunter 
833723f7924SRussell King 	return sdhci_suspend_host(host);
834723f7924SRussell King }
835723f7924SRussell King 
83606732b84SUlf Hansson static int esdhc_of_resume(struct device *dev)
837723f7924SRussell King {
838723f7924SRussell King 	struct sdhci_host *host = dev_get_drvdata(dev);
839723f7924SRussell King 	int ret = sdhci_resume_host(host);
840723f7924SRussell King 
841723f7924SRussell King 	if (ret == 0) {
842723f7924SRussell King 		/* Isn't this already done by sdhci_resume_host() ? --rmk */
843723f7924SRussell King 		esdhc_of_enable_dma(host);
844f4932cfdSyangbo lu 		sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
845723f7924SRussell King 	}
846723f7924SRussell King 	return ret;
847723f7924SRussell King }
848723f7924SRussell King #endif
849723f7924SRussell King 
8509e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
8519e48b336SUlf Hansson 			esdhc_of_suspend,
8529e48b336SUlf Hansson 			esdhc_of_resume);
8539e48b336SUlf Hansson 
854f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = {
855f4932cfdSyangbo lu 	.read_l = esdhc_be_readl,
856f4932cfdSyangbo lu 	.read_w = esdhc_be_readw,
857f4932cfdSyangbo lu 	.read_b = esdhc_be_readb,
858f4932cfdSyangbo lu 	.write_l = esdhc_be_writel,
859f4932cfdSyangbo lu 	.write_w = esdhc_be_writew,
860f4932cfdSyangbo lu 	.write_b = esdhc_be_writeb,
861f4932cfdSyangbo lu 	.set_clock = esdhc_of_set_clock,
862f4932cfdSyangbo lu 	.enable_dma = esdhc_of_enable_dma,
863f4932cfdSyangbo lu 	.get_max_clock = esdhc_of_get_max_clock,
864f4932cfdSyangbo lu 	.get_min_clock = esdhc_of_get_min_clock,
865f4932cfdSyangbo lu 	.adma_workaround = esdhc_of_adma_workaround,
866f4932cfdSyangbo lu 	.set_bus_width = esdhc_pltfm_set_bus_width,
867f4932cfdSyangbo lu 	.reset = esdhc_reset,
868*54e08d9aSYangbo Lu 	.set_uhs_signaling = esdhc_set_uhs_signaling,
869f4932cfdSyangbo lu };
870f4932cfdSyangbo lu 
871f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = {
872f4932cfdSyangbo lu 	.read_l = esdhc_le_readl,
873f4932cfdSyangbo lu 	.read_w = esdhc_le_readw,
874f4932cfdSyangbo lu 	.read_b = esdhc_le_readb,
875f4932cfdSyangbo lu 	.write_l = esdhc_le_writel,
876f4932cfdSyangbo lu 	.write_w = esdhc_le_writew,
877f4932cfdSyangbo lu 	.write_b = esdhc_le_writeb,
878f4932cfdSyangbo lu 	.set_clock = esdhc_of_set_clock,
879f4932cfdSyangbo lu 	.enable_dma = esdhc_of_enable_dma,
880f4932cfdSyangbo lu 	.get_max_clock = esdhc_of_get_max_clock,
881f4932cfdSyangbo lu 	.get_min_clock = esdhc_of_get_min_clock,
882f4932cfdSyangbo lu 	.adma_workaround = esdhc_of_adma_workaround,
883f4932cfdSyangbo lu 	.set_bus_width = esdhc_pltfm_set_bus_width,
884f4932cfdSyangbo lu 	.reset = esdhc_reset,
885*54e08d9aSYangbo Lu 	.set_uhs_signaling = esdhc_set_uhs_signaling,
886f4932cfdSyangbo lu };
887f4932cfdSyangbo lu 
888f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
889e9acc77dSyangbo lu 	.quirks = ESDHC_DEFAULT_QUIRKS |
890e9acc77dSyangbo lu #ifdef CONFIG_PPC
891e9acc77dSyangbo lu 		  SDHCI_QUIRK_BROKEN_CARD_DETECTION |
892e9acc77dSyangbo lu #endif
893e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_CARD_NO_RESET |
894e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
895f4932cfdSyangbo lu 	.ops = &sdhci_esdhc_be_ops,
8967657c3a7SAlbert Herranz };
89738576af1SShawn Guo 
898f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
899e9acc77dSyangbo lu 	.quirks = ESDHC_DEFAULT_QUIRKS |
900e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_CARD_NO_RESET |
901e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
902f4932cfdSyangbo lu 	.ops = &sdhci_esdhc_le_ops,
903f4932cfdSyangbo lu };
904f4932cfdSyangbo lu 
905151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = {
906151ede40Syangbo lu 	{ .family = "QorIQ T4240", .revision = "1.0", },
907151ede40Syangbo lu 	{ .family = "QorIQ T4240", .revision = "2.0", },
908151ede40Syangbo lu 	{ },
909151ede40Syangbo lu };
910151ede40Syangbo lu 
911f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
912f4932cfdSyangbo lu {
91367fdfbdfSyinbo.zhu 	const struct of_device_id *match;
914f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host;
915f4932cfdSyangbo lu 	struct sdhci_esdhc *esdhc;
91619c3a0efSyangbo lu 	struct device_node *np;
91719c3a0efSyangbo lu 	struct clk *clk;
91819c3a0efSyangbo lu 	u32 val;
919f4932cfdSyangbo lu 	u16 host_ver;
920f4932cfdSyangbo lu 
921f4932cfdSyangbo lu 	pltfm_host = sdhci_priv(host);
9228605e7aeSJisheng Zhang 	esdhc = sdhci_pltfm_priv(pltfm_host);
923f4932cfdSyangbo lu 
924f4932cfdSyangbo lu 	host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
925f4932cfdSyangbo lu 	esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
926f4932cfdSyangbo lu 			     SDHCI_VENDOR_VER_SHIFT;
927f4932cfdSyangbo lu 	esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
928151ede40Syangbo lu 	if (soc_device_match(soc_incorrect_hostver))
929151ede40Syangbo lu 		esdhc->quirk_incorrect_hostver = true;
930151ede40Syangbo lu 	else
931151ede40Syangbo lu 		esdhc->quirk_incorrect_hostver = false;
93219c3a0efSyangbo lu 
93367fdfbdfSyinbo.zhu 	match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
93467fdfbdfSyinbo.zhu 	if (match)
93567fdfbdfSyinbo.zhu 		esdhc->clk_fixup = match->data;
93619c3a0efSyangbo lu 	np = pdev->dev.of_node;
93719c3a0efSyangbo lu 	clk = of_clk_get(np, 0);
93819c3a0efSyangbo lu 	if (!IS_ERR(clk)) {
93919c3a0efSyangbo lu 		/*
94019c3a0efSyangbo lu 		 * esdhc->peripheral_clock would be assigned with a value
94119c3a0efSyangbo lu 		 * which is eSDHC base clock when use periperal clock.
94219c3a0efSyangbo lu 		 * For ls1046a, the clock value got by common clk API is
94319c3a0efSyangbo lu 		 * peripheral clock while the eSDHC base clock is 1/2
94419c3a0efSyangbo lu 		 * peripheral clock.
94519c3a0efSyangbo lu 		 */
94619c3a0efSyangbo lu 		if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
94719c3a0efSyangbo lu 			esdhc->peripheral_clock = clk_get_rate(clk) / 2;
94819c3a0efSyangbo lu 		else
94919c3a0efSyangbo lu 			esdhc->peripheral_clock = clk_get_rate(clk);
95019c3a0efSyangbo lu 
95119c3a0efSyangbo lu 		clk_put(clk);
95219c3a0efSyangbo lu 	}
95319c3a0efSyangbo lu 
95419c3a0efSyangbo lu 	if (esdhc->peripheral_clock) {
95519c3a0efSyangbo lu 		esdhc_clock_enable(host, false);
95619c3a0efSyangbo lu 		val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
95719c3a0efSyangbo lu 		val |= ESDHC_PERIPHERAL_CLK_SEL;
95819c3a0efSyangbo lu 		sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
95919c3a0efSyangbo lu 		esdhc_clock_enable(host, true);
96019c3a0efSyangbo lu 	}
961f4932cfdSyangbo lu }
962f4932cfdSyangbo lu 
963*54e08d9aSYangbo Lu static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc)
964*54e08d9aSYangbo Lu {
965*54e08d9aSYangbo Lu 	esdhc_tuning_block_enable(mmc_priv(mmc), false);
966*54e08d9aSYangbo Lu 	return 0;
967*54e08d9aSYangbo Lu }
968*54e08d9aSYangbo Lu 
969c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev)
97038576af1SShawn Guo {
97166b50a00SOded Gabbay 	struct sdhci_host *host;
972dcaff04dSOded Gabbay 	struct device_node *np;
9731ef5e49eSyangbo lu 	struct sdhci_pltfm_host *pltfm_host;
9741ef5e49eSyangbo lu 	struct sdhci_esdhc *esdhc;
97566b50a00SOded Gabbay 	int ret;
97666b50a00SOded Gabbay 
977f4932cfdSyangbo lu 	np = pdev->dev.of_node;
978f4932cfdSyangbo lu 
979150d4240SJulia Lawall 	if (of_property_read_bool(np, "little-endian"))
9808605e7aeSJisheng Zhang 		host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
9818605e7aeSJisheng Zhang 					sizeof(struct sdhci_esdhc));
982f4932cfdSyangbo lu 	else
9838605e7aeSJisheng Zhang 		host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
9848605e7aeSJisheng Zhang 					sizeof(struct sdhci_esdhc));
985f4932cfdSyangbo lu 
98666b50a00SOded Gabbay 	if (IS_ERR(host))
98766b50a00SOded Gabbay 		return PTR_ERR(host);
98866b50a00SOded Gabbay 
989ea35645aSyangbo lu 	host->mmc_host_ops.start_signal_voltage_switch =
990ea35645aSyangbo lu 		esdhc_signal_voltage_switch;
991ba49cbd0Syangbo lu 	host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
992*54e08d9aSYangbo Lu 	host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr;
9936b236f37Syangbo lu 	host->tuning_delay = 1;
994ea35645aSyangbo lu 
995f4932cfdSyangbo lu 	esdhc_init(pdev, host);
996f4932cfdSyangbo lu 
99766b50a00SOded Gabbay 	sdhci_get_of_property(pdev);
99866b50a00SOded Gabbay 
9991ef5e49eSyangbo lu 	pltfm_host = sdhci_priv(host);
10008605e7aeSJisheng Zhang 	esdhc = sdhci_pltfm_priv(pltfm_host);
1001b1f378abSYinbo Zhu 	if (soc_device_match(soc_fixup_tuning))
1002b1f378abSYinbo Zhu 		esdhc->quirk_fixup_tuning = true;
1003b1f378abSYinbo Zhu 	else
1004b1f378abSYinbo Zhu 		esdhc->quirk_fixup_tuning = false;
1005b1f378abSYinbo Zhu 
10061ef5e49eSyangbo lu 	if (esdhc->vendor_ver == VENDOR_V_22)
10071ef5e49eSyangbo lu 		host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
10081ef5e49eSyangbo lu 
10091ef5e49eSyangbo lu 	if (esdhc->vendor_ver > VENDOR_V_22)
10101ef5e49eSyangbo lu 		host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
10111ef5e49eSyangbo lu 
101274fd5e30SYangbo Lu 	if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
101374fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p5020-esdhc") ||
101474fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p4080-esdhc") ||
101574fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p1020-esdhc") ||
1016e9acc77dSyangbo lu 	    of_device_is_compatible(np, "fsl,t1040-esdhc"))
101774fd5e30SYangbo Lu 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
101874fd5e30SYangbo Lu 
1019a22950c8Syangbo lu 	if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
1020a22950c8Syangbo lu 		host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1021a22950c8Syangbo lu 
1022dcaff04dSOded Gabbay 	if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
1023dcaff04dSOded Gabbay 		/*
1024dcaff04dSOded Gabbay 		 * Freescale messed up with P2020 as it has a non-standard
1025dcaff04dSOded Gabbay 		 * host control register
1026dcaff04dSOded Gabbay 		 */
1027dcaff04dSOded Gabbay 		host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
1028dcaff04dSOded Gabbay 	}
1029dcaff04dSOded Gabbay 
103066b50a00SOded Gabbay 	/* call to generic mmc_of_parse to support additional capabilities */
1031f0991408SUlf Hansson 	ret = mmc_of_parse(host->mmc);
1032f0991408SUlf Hansson 	if (ret)
1033f0991408SUlf Hansson 		goto err;
1034f0991408SUlf Hansson 
1035490104acSHaijun Zhang 	mmc_of_parse_voltage(np, &host->ocr_mask);
103666b50a00SOded Gabbay 
103766b50a00SOded Gabbay 	ret = sdhci_add_host(host);
103866b50a00SOded Gabbay 	if (ret)
1039f0991408SUlf Hansson 		goto err;
104066b50a00SOded Gabbay 
1041f0991408SUlf Hansson 	return 0;
1042f0991408SUlf Hansson  err:
1043f0991408SUlf Hansson 	sdhci_pltfm_free(pdev);
104466b50a00SOded Gabbay 	return ret;
104538576af1SShawn Guo }
104638576af1SShawn Guo 
104738576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = {
104838576af1SShawn Guo 	.driver = {
104938576af1SShawn Guo 		.name = "sdhci-esdhc",
105038576af1SShawn Guo 		.of_match_table = sdhci_esdhc_of_match,
10519e48b336SUlf Hansson 		.pm = &esdhc_of_dev_pm_ops,
105238576af1SShawn Guo 	},
105338576af1SShawn Guo 	.probe = sdhci_esdhc_probe,
1054caebcae9SKevin Hao 	.remove = sdhci_pltfm_unregister,
105538576af1SShawn Guo };
105638576af1SShawn Guo 
1057d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver);
105838576af1SShawn Guo 
105938576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
106038576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
106138576af1SShawn Guo 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
106238576af1SShawn Guo MODULE_LICENSE("GPL v2");
1063