xref: /openbmc/linux/drivers/mmc/host/sdhci-of-esdhc.c (revision 1f1929f3f2faf0815e602aa31d6ac955fb22163d)
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>
27b214fe59SYinbo Zhu #include <linux/mmc/mmc.h>
2838576af1SShawn Guo #include "sdhci-pltfm.h"
2980872e21SWolfram Sang #include "sdhci-esdhc.h"
307657c3a7SAlbert Herranz 
31137ccd46SJerry Huang #define VENDOR_V_22	0x12
32a4071fbbSHaijun Zhang #define VENDOR_V_23	0x13
33f4932cfdSyangbo lu 
3467fdfbdfSyinbo.zhu #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
3567fdfbdfSyinbo.zhu 
3667fdfbdfSyinbo.zhu struct esdhc_clk_fixup {
3767fdfbdfSyinbo.zhu 	const unsigned int sd_dflt_max_clk;
3867fdfbdfSyinbo.zhu 	const unsigned int max_clk[MMC_TIMING_NUM];
3967fdfbdfSyinbo.zhu };
4067fdfbdfSyinbo.zhu 
4167fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1021a_esdhc_clk = {
4267fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 25000000,
4367fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS] = 46500000,
4467fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_SD_HS] = 46500000,
4567fdfbdfSyinbo.zhu };
4667fdfbdfSyinbo.zhu 
4767fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1046a_esdhc_clk = {
4867fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 25000000,
4967fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_UHS_SDR104] = 167000000,
5067fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS200] = 167000000,
5167fdfbdfSyinbo.zhu };
5267fdfbdfSyinbo.zhu 
5367fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1012a_esdhc_clk = {
5467fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 25000000,
5567fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_UHS_SDR104] = 125000000,
5667fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS200] = 125000000,
5767fdfbdfSyinbo.zhu };
5867fdfbdfSyinbo.zhu 
5967fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup p1010_esdhc_clk = {
6067fdfbdfSyinbo.zhu 	.sd_dflt_max_clk = 20000000,
6167fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_LEGACY] = 20000000,
6267fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_MMC_HS] = 42000000,
6367fdfbdfSyinbo.zhu 	.max_clk[MMC_TIMING_SD_HS] = 40000000,
6467fdfbdfSyinbo.zhu };
6567fdfbdfSyinbo.zhu 
6667fdfbdfSyinbo.zhu static const struct of_device_id sdhci_esdhc_of_match[] = {
6767fdfbdfSyinbo.zhu 	{ .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk},
6867fdfbdfSyinbo.zhu 	{ .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk},
6967fdfbdfSyinbo.zhu 	{ .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk},
7067fdfbdfSyinbo.zhu 	{ .compatible = "fsl,p1010-esdhc",   .data = &p1010_esdhc_clk},
7167fdfbdfSyinbo.zhu 	{ .compatible = "fsl,mpc8379-esdhc" },
7267fdfbdfSyinbo.zhu 	{ .compatible = "fsl,mpc8536-esdhc" },
7367fdfbdfSyinbo.zhu 	{ .compatible = "fsl,esdhc" },
7467fdfbdfSyinbo.zhu 	{ }
7567fdfbdfSyinbo.zhu };
7667fdfbdfSyinbo.zhu MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
7767fdfbdfSyinbo.zhu 
78f4932cfdSyangbo lu struct sdhci_esdhc {
79f4932cfdSyangbo lu 	u8 vendor_ver;
80f4932cfdSyangbo lu 	u8 spec_ver;
81151ede40Syangbo lu 	bool quirk_incorrect_hostver;
826079e63cSYangbo Lu 	bool quirk_limited_clk_division;
8348e304ccSYangbo Lu 	bool quirk_unreliable_pulse_detection;
84b1f378abSYinbo Zhu 	bool quirk_fixup_tuning;
85*1f1929f3SYangbo Lu 	bool quirk_ignore_data_inhibit;
8619c3a0efSyangbo lu 	unsigned int peripheral_clock;
8767fdfbdfSyinbo.zhu 	const struct esdhc_clk_fixup *clk_fixup;
88b1f378abSYinbo Zhu 	u32 div_ratio;
89f4932cfdSyangbo lu };
90f4932cfdSyangbo lu 
91f4932cfdSyangbo lu /**
92f4932cfdSyangbo lu  * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
93f4932cfdSyangbo lu  *		       to make it compatible with SD spec.
94f4932cfdSyangbo lu  *
95f4932cfdSyangbo lu  * @host: pointer to sdhci_host
96f4932cfdSyangbo lu  * @spec_reg: SD spec register address
97f4932cfdSyangbo lu  * @value: 32bit eSDHC register value on spec_reg address
98f4932cfdSyangbo lu  *
99f4932cfdSyangbo lu  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
100f4932cfdSyangbo lu  * registers are 32 bits. There are differences in register size, register
101f4932cfdSyangbo lu  * address, register function, bit position and function between eSDHC spec
102f4932cfdSyangbo lu  * and SD spec.
103f4932cfdSyangbo lu  *
104f4932cfdSyangbo lu  * Return a fixed up register value
105f4932cfdSyangbo lu  */
106f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host,
107f4932cfdSyangbo lu 				     int spec_reg, u32 value)
108137ccd46SJerry Huang {
109f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1108605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
111137ccd46SJerry Huang 	u32 ret;
112137ccd46SJerry Huang 
113137ccd46SJerry Huang 	/*
114137ccd46SJerry Huang 	 * The bit of ADMA flag in eSDHC is not compatible with standard
115137ccd46SJerry Huang 	 * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
116137ccd46SJerry Huang 	 * supported by eSDHC.
117137ccd46SJerry Huang 	 * And for many FSL eSDHC controller, the reset value of field
118f4932cfdSyangbo lu 	 * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
119137ccd46SJerry Huang 	 * only these vendor version is greater than 2.2/0x12 support ADMA.
120137ccd46SJerry Huang 	 */
121f4932cfdSyangbo lu 	if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
122f4932cfdSyangbo lu 		if (esdhc->vendor_ver > VENDOR_V_22) {
123f4932cfdSyangbo lu 			ret = value | SDHCI_CAN_DO_ADMA2;
124f4932cfdSyangbo lu 			return ret;
125137ccd46SJerry Huang 		}
126f4932cfdSyangbo lu 	}
127b0921d5cSMichael Walle 	/*
128b0921d5cSMichael Walle 	 * The DAT[3:0] line signal levels and the CMD line signal level are
129b0921d5cSMichael Walle 	 * not compatible with standard SDHC register. The line signal levels
130b0921d5cSMichael Walle 	 * DAT[7:0] are at bits 31:24 and the command line signal level is at
131b0921d5cSMichael Walle 	 * bit 23. All other bits are the same as in the standard SDHC
132b0921d5cSMichael Walle 	 * register.
133b0921d5cSMichael Walle 	 */
134b0921d5cSMichael Walle 	if (spec_reg == SDHCI_PRESENT_STATE) {
135b0921d5cSMichael Walle 		ret = value & 0x000fffff;
136b0921d5cSMichael Walle 		ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
137b0921d5cSMichael Walle 		ret |= (value << 1) & SDHCI_CMD_LVL;
138b0921d5cSMichael Walle 		return ret;
139b0921d5cSMichael Walle 	}
140b0921d5cSMichael Walle 
1412f3110ccSyangbo lu 	/*
1422f3110ccSyangbo lu 	 * DTS properties of mmc host are used to enable each speed mode
1432f3110ccSyangbo lu 	 * according to soc and board capability. So clean up
1442f3110ccSyangbo lu 	 * SDR50/SDR104/DDR50 support bits here.
1452f3110ccSyangbo lu 	 */
1462f3110ccSyangbo lu 	if (spec_reg == SDHCI_CAPABILITIES_1) {
1472f3110ccSyangbo lu 		ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
1482f3110ccSyangbo lu 				SDHCI_SUPPORT_DDR50);
1492f3110ccSyangbo lu 		return ret;
1502f3110ccSyangbo lu 	}
1512f3110ccSyangbo lu 
152*1f1929f3SYangbo Lu 	/*
153*1f1929f3SYangbo Lu 	 * Some controllers have unreliable Data Line Active
154*1f1929f3SYangbo Lu 	 * bit for commands with busy signal. This affects
155*1f1929f3SYangbo Lu 	 * Command Inhibit (data) bit. Just ignore it since
156*1f1929f3SYangbo Lu 	 * MMC core driver has already polled card status
157*1f1929f3SYangbo Lu 	 * with CMD13 after any command with busy siganl.
158*1f1929f3SYangbo Lu 	 */
159*1f1929f3SYangbo Lu 	if ((spec_reg == SDHCI_PRESENT_STATE) &&
160*1f1929f3SYangbo Lu 	(esdhc->quirk_ignore_data_inhibit == true)) {
161*1f1929f3SYangbo Lu 		ret = value & ~SDHCI_DATA_INHIBIT;
162*1f1929f3SYangbo Lu 		return ret;
163*1f1929f3SYangbo Lu 	}
164*1f1929f3SYangbo Lu 
165f4932cfdSyangbo lu 	ret = value;
166137ccd46SJerry Huang 	return ret;
167137ccd46SJerry Huang }
168137ccd46SJerry Huang 
169f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host,
170f4932cfdSyangbo lu 				     int spec_reg, u32 value)
1717657c3a7SAlbert Herranz {
172151ede40Syangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
173151ede40Syangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
1747657c3a7SAlbert Herranz 	u16 ret;
175f4932cfdSyangbo lu 	int shift = (spec_reg & 0x2) * 8;
1767657c3a7SAlbert Herranz 
177f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_VERSION)
178f4932cfdSyangbo lu 		ret = value & 0xffff;
1797657c3a7SAlbert Herranz 	else
180f4932cfdSyangbo lu 		ret = (value >> shift) & 0xffff;
181151ede40Syangbo lu 	/* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
182151ede40Syangbo lu 	 * vendor version and spec version information.
183151ede40Syangbo lu 	 */
184151ede40Syangbo lu 	if ((spec_reg == SDHCI_HOST_VERSION) &&
185151ede40Syangbo lu 	    (esdhc->quirk_incorrect_hostver))
186151ede40Syangbo lu 		ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
187e51cbc9eSXu lei 	return ret;
188e51cbc9eSXu lei }
189e51cbc9eSXu lei 
190f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host,
191f4932cfdSyangbo lu 				     int spec_reg, u32 value)
192e51cbc9eSXu lei {
193f4932cfdSyangbo lu 	u8 ret;
194f4932cfdSyangbo lu 	u8 dma_bits;
195f4932cfdSyangbo lu 	int shift = (spec_reg & 0x3) * 8;
196f4932cfdSyangbo lu 
197f4932cfdSyangbo lu 	ret = (value >> shift) & 0xff;
198ba8c4dc9SRoy Zang 
199ba8c4dc9SRoy Zang 	/*
200ba8c4dc9SRoy Zang 	 * "DMA select" locates at offset 0x28 in SD specification, but on
201ba8c4dc9SRoy Zang 	 * P5020 or P3041, it locates at 0x29.
202ba8c4dc9SRoy Zang 	 */
203f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_CONTROL) {
204ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
205f4932cfdSyangbo lu 		dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
206ba8c4dc9SRoy Zang 		/* fixup the result */
207ba8c4dc9SRoy Zang 		ret &= ~SDHCI_CTRL_DMA_MASK;
208ba8c4dc9SRoy Zang 		ret |= dma_bits;
209ba8c4dc9SRoy Zang 	}
210f4932cfdSyangbo lu 	return ret;
211f4932cfdSyangbo lu }
212f4932cfdSyangbo lu 
213f4932cfdSyangbo lu /**
214f4932cfdSyangbo lu  * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
215f4932cfdSyangbo lu  *			written into eSDHC register.
216f4932cfdSyangbo lu  *
217f4932cfdSyangbo lu  * @host: pointer to sdhci_host
218f4932cfdSyangbo lu  * @spec_reg: SD spec register address
219f4932cfdSyangbo lu  * @value: 8/16/32bit SD spec register value that would be written
220f4932cfdSyangbo lu  * @old_value: 32bit eSDHC register value on spec_reg address
221f4932cfdSyangbo lu  *
222f4932cfdSyangbo lu  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
223f4932cfdSyangbo lu  * registers are 32 bits. There are differences in register size, register
224f4932cfdSyangbo lu  * address, register function, bit position and function between eSDHC spec
225f4932cfdSyangbo lu  * and SD spec.
226f4932cfdSyangbo lu  *
227f4932cfdSyangbo lu  * Return a fixed up register value
228f4932cfdSyangbo lu  */
229f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host,
230f4932cfdSyangbo lu 				     int spec_reg, u32 value, u32 old_value)
231f4932cfdSyangbo lu {
232f4932cfdSyangbo lu 	u32 ret;
233f4932cfdSyangbo lu 
234f4932cfdSyangbo lu 	/*
235f4932cfdSyangbo lu 	 * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
236f4932cfdSyangbo lu 	 * when SYSCTL[RSTD] is set for some special operations.
237f4932cfdSyangbo lu 	 * No any impact on other operation.
238f4932cfdSyangbo lu 	 */
239f4932cfdSyangbo lu 	if (spec_reg == SDHCI_INT_ENABLE)
240f4932cfdSyangbo lu 		ret = value | SDHCI_INT_BLK_GAP;
241f4932cfdSyangbo lu 	else
242f4932cfdSyangbo lu 		ret = value;
243ba8c4dc9SRoy Zang 
2447657c3a7SAlbert Herranz 	return ret;
2457657c3a7SAlbert Herranz }
2467657c3a7SAlbert Herranz 
247f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host,
248f4932cfdSyangbo lu 				     int spec_reg, u16 value, u32 old_value)
249a4071fbbSHaijun Zhang {
250f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
251f4932cfdSyangbo lu 	int shift = (spec_reg & 0x2) * 8;
252f4932cfdSyangbo lu 	u32 ret;
253f4932cfdSyangbo lu 
254f4932cfdSyangbo lu 	switch (spec_reg) {
255f4932cfdSyangbo lu 	case SDHCI_TRANSFER_MODE:
256a4071fbbSHaijun Zhang 		/*
257f4932cfdSyangbo lu 		 * Postpone this write, we must do it together with a
258f4932cfdSyangbo lu 		 * command write that is down below. Return old value.
259a4071fbbSHaijun Zhang 		 */
260f4932cfdSyangbo lu 		pltfm_host->xfer_mode_shadow = value;
261f4932cfdSyangbo lu 		return old_value;
262f4932cfdSyangbo lu 	case SDHCI_COMMAND:
263f4932cfdSyangbo lu 		ret = (value << 16) | pltfm_host->xfer_mode_shadow;
264f4932cfdSyangbo lu 		return ret;
265a4071fbbSHaijun Zhang 	}
266a4071fbbSHaijun Zhang 
267f4932cfdSyangbo lu 	ret = old_value & (~(0xffff << shift));
268f4932cfdSyangbo lu 	ret |= (value << shift);
269f4932cfdSyangbo lu 
270f4932cfdSyangbo lu 	if (spec_reg == SDHCI_BLOCK_SIZE) {
2717657c3a7SAlbert Herranz 		/*
2727657c3a7SAlbert Herranz 		 * Two last DMA bits are reserved, and first one is used for
2737657c3a7SAlbert Herranz 		 * non-standard blksz of 4096 bytes that we don't support
2747657c3a7SAlbert Herranz 		 * yet. So clear the DMA boundary bits.
2757657c3a7SAlbert Herranz 		 */
276f4932cfdSyangbo lu 		ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
2777657c3a7SAlbert Herranz 	}
278f4932cfdSyangbo lu 	return ret;
2797657c3a7SAlbert Herranz }
2807657c3a7SAlbert Herranz 
281f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host,
282f4932cfdSyangbo lu 				     int spec_reg, u8 value, u32 old_value)
2837657c3a7SAlbert Herranz {
284f4932cfdSyangbo lu 	u32 ret;
285f4932cfdSyangbo lu 	u32 dma_bits;
286f4932cfdSyangbo lu 	u8 tmp;
287f4932cfdSyangbo lu 	int shift = (spec_reg & 0x3) * 8;
288f4932cfdSyangbo lu 
289ba8c4dc9SRoy Zang 	/*
2909e4703dfSyangbo lu 	 * eSDHC doesn't have a standard power control register, so we do
2919e4703dfSyangbo lu 	 * nothing here to avoid incorrect operation.
2929e4703dfSyangbo lu 	 */
2939e4703dfSyangbo lu 	if (spec_reg == SDHCI_POWER_CONTROL)
2949e4703dfSyangbo lu 		return old_value;
2959e4703dfSyangbo lu 	/*
296ba8c4dc9SRoy Zang 	 * "DMA select" location is offset 0x28 in SD specification, but on
297ba8c4dc9SRoy Zang 	 * P5020 or P3041, it's located at 0x29.
298ba8c4dc9SRoy Zang 	 */
299f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_CONTROL) {
300dcaff04dSOded Gabbay 		/*
301dcaff04dSOded Gabbay 		 * If host control register is not standard, exit
302dcaff04dSOded Gabbay 		 * this function
303dcaff04dSOded Gabbay 		 */
304dcaff04dSOded Gabbay 		if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
305f4932cfdSyangbo lu 			return old_value;
306dcaff04dSOded Gabbay 
307ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
308f4932cfdSyangbo lu 		dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
309f4932cfdSyangbo lu 		ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
310f4932cfdSyangbo lu 		tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
311f4932cfdSyangbo lu 		      (old_value & SDHCI_CTRL_DMA_MASK);
312f4932cfdSyangbo lu 		ret = (ret & (~0xff)) | tmp;
313f4932cfdSyangbo lu 
314f4932cfdSyangbo lu 		/* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
315f4932cfdSyangbo lu 		ret &= ~ESDHC_HOST_CONTROL_RES;
316f4932cfdSyangbo lu 		return ret;
317ba8c4dc9SRoy Zang 	}
318ba8c4dc9SRoy Zang 
319f4932cfdSyangbo lu 	ret = (old_value & (~(0xff << shift))) | (value << shift);
320f4932cfdSyangbo lu 	return ret;
321f4932cfdSyangbo lu }
322f4932cfdSyangbo lu 
323f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
324f4932cfdSyangbo lu {
325f4932cfdSyangbo lu 	u32 ret;
326f4932cfdSyangbo lu 	u32 value;
327f4932cfdSyangbo lu 
3282f3110ccSyangbo lu 	if (reg == SDHCI_CAPABILITIES_1)
3292f3110ccSyangbo lu 		value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
3302f3110ccSyangbo lu 	else
331f4932cfdSyangbo lu 		value = ioread32be(host->ioaddr + reg);
3322f3110ccSyangbo lu 
333f4932cfdSyangbo lu 	ret = esdhc_readl_fixup(host, reg, value);
334f4932cfdSyangbo lu 
335f4932cfdSyangbo lu 	return ret;
336f4932cfdSyangbo lu }
337f4932cfdSyangbo lu 
338f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
339f4932cfdSyangbo lu {
340f4932cfdSyangbo lu 	u32 ret;
341f4932cfdSyangbo lu 	u32 value;
342f4932cfdSyangbo lu 
3432f3110ccSyangbo lu 	if (reg == SDHCI_CAPABILITIES_1)
3442f3110ccSyangbo lu 		value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
3452f3110ccSyangbo lu 	else
346f4932cfdSyangbo lu 		value = ioread32(host->ioaddr + reg);
3472f3110ccSyangbo lu 
348f4932cfdSyangbo lu 	ret = esdhc_readl_fixup(host, reg, value);
349f4932cfdSyangbo lu 
350f4932cfdSyangbo lu 	return ret;
351f4932cfdSyangbo lu }
352f4932cfdSyangbo lu 
353f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
354f4932cfdSyangbo lu {
355f4932cfdSyangbo lu 	u16 ret;
356f4932cfdSyangbo lu 	u32 value;
357f4932cfdSyangbo lu 	int base = reg & ~0x3;
358f4932cfdSyangbo lu 
359f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
360f4932cfdSyangbo lu 	ret = esdhc_readw_fixup(host, reg, value);
361f4932cfdSyangbo lu 	return ret;
362f4932cfdSyangbo lu }
363f4932cfdSyangbo lu 
364f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
365f4932cfdSyangbo lu {
366f4932cfdSyangbo lu 	u16 ret;
367f4932cfdSyangbo lu 	u32 value;
368f4932cfdSyangbo lu 	int base = reg & ~0x3;
369f4932cfdSyangbo lu 
370f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
371f4932cfdSyangbo lu 	ret = esdhc_readw_fixup(host, reg, value);
372f4932cfdSyangbo lu 	return ret;
373f4932cfdSyangbo lu }
374f4932cfdSyangbo lu 
375f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
376f4932cfdSyangbo lu {
377f4932cfdSyangbo lu 	u8 ret;
378f4932cfdSyangbo lu 	u32 value;
379f4932cfdSyangbo lu 	int base = reg & ~0x3;
380f4932cfdSyangbo lu 
381f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
382f4932cfdSyangbo lu 	ret = esdhc_readb_fixup(host, reg, value);
383f4932cfdSyangbo lu 	return ret;
384f4932cfdSyangbo lu }
385f4932cfdSyangbo lu 
386f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
387f4932cfdSyangbo lu {
388f4932cfdSyangbo lu 	u8 ret;
389f4932cfdSyangbo lu 	u32 value;
390f4932cfdSyangbo lu 	int base = reg & ~0x3;
391f4932cfdSyangbo lu 
392f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
393f4932cfdSyangbo lu 	ret = esdhc_readb_fixup(host, reg, value);
394f4932cfdSyangbo lu 	return ret;
395f4932cfdSyangbo lu }
396f4932cfdSyangbo lu 
397f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
398f4932cfdSyangbo lu {
399f4932cfdSyangbo lu 	u32 value;
400f4932cfdSyangbo lu 
401f4932cfdSyangbo lu 	value = esdhc_writel_fixup(host, reg, val, 0);
402f4932cfdSyangbo lu 	iowrite32be(value, host->ioaddr + reg);
403f4932cfdSyangbo lu }
404f4932cfdSyangbo lu 
405f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
406f4932cfdSyangbo lu {
407f4932cfdSyangbo lu 	u32 value;
408f4932cfdSyangbo lu 
409f4932cfdSyangbo lu 	value = esdhc_writel_fixup(host, reg, val, 0);
410f4932cfdSyangbo lu 	iowrite32(value, host->ioaddr + reg);
411f4932cfdSyangbo lu }
412f4932cfdSyangbo lu 
413f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
414f4932cfdSyangbo lu {
415f4932cfdSyangbo lu 	int base = reg & ~0x3;
416f4932cfdSyangbo lu 	u32 value;
417f4932cfdSyangbo lu 	u32 ret;
418f4932cfdSyangbo lu 
419f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
420f4932cfdSyangbo lu 	ret = esdhc_writew_fixup(host, reg, val, value);
421f4932cfdSyangbo lu 	if (reg != SDHCI_TRANSFER_MODE)
422f4932cfdSyangbo lu 		iowrite32be(ret, host->ioaddr + base);
423f4932cfdSyangbo lu }
424f4932cfdSyangbo lu 
425f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
426f4932cfdSyangbo lu {
427f4932cfdSyangbo lu 	int base = reg & ~0x3;
428f4932cfdSyangbo lu 	u32 value;
429f4932cfdSyangbo lu 	u32 ret;
430f4932cfdSyangbo lu 
431f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
432f4932cfdSyangbo lu 	ret = esdhc_writew_fixup(host, reg, val, value);
433f4932cfdSyangbo lu 	if (reg != SDHCI_TRANSFER_MODE)
434f4932cfdSyangbo lu 		iowrite32(ret, host->ioaddr + base);
435f4932cfdSyangbo lu }
436f4932cfdSyangbo lu 
437f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
438f4932cfdSyangbo lu {
439f4932cfdSyangbo lu 	int base = reg & ~0x3;
440f4932cfdSyangbo lu 	u32 value;
441f4932cfdSyangbo lu 	u32 ret;
442f4932cfdSyangbo lu 
443f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
444f4932cfdSyangbo lu 	ret = esdhc_writeb_fixup(host, reg, val, value);
445f4932cfdSyangbo lu 	iowrite32be(ret, host->ioaddr + base);
446f4932cfdSyangbo lu }
447f4932cfdSyangbo lu 
448f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
449f4932cfdSyangbo lu {
450f4932cfdSyangbo lu 	int base = reg & ~0x3;
451f4932cfdSyangbo lu 	u32 value;
452f4932cfdSyangbo lu 	u32 ret;
453f4932cfdSyangbo lu 
454f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
455f4932cfdSyangbo lu 	ret = esdhc_writeb_fixup(host, reg, val, value);
456f4932cfdSyangbo lu 	iowrite32(ret, host->ioaddr + base);
4577657c3a7SAlbert Herranz }
4587657c3a7SAlbert Herranz 
459a4071fbbSHaijun Zhang /*
460a4071fbbSHaijun Zhang  * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
461a4071fbbSHaijun Zhang  * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
462a4071fbbSHaijun Zhang  * and Block Gap Event(IRQSTAT[BGE]) are also set.
463a4071fbbSHaijun Zhang  * For Continue, apply soft reset for data(SYSCTL[RSTD]);
464a4071fbbSHaijun Zhang  * and re-issue the entire read transaction from beginning.
465a4071fbbSHaijun Zhang  */
466f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
467a4071fbbSHaijun Zhang {
468f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
4698605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
470a4071fbbSHaijun Zhang 	bool applicable;
471a4071fbbSHaijun Zhang 	dma_addr_t dmastart;
472a4071fbbSHaijun Zhang 	dma_addr_t dmanow;
473a4071fbbSHaijun Zhang 
474a4071fbbSHaijun Zhang 	applicable = (intmask & SDHCI_INT_DATA_END) &&
475a4071fbbSHaijun Zhang 		     (intmask & SDHCI_INT_BLK_GAP) &&
476f4932cfdSyangbo lu 		     (esdhc->vendor_ver == VENDOR_V_23);
477a4071fbbSHaijun Zhang 	if (!applicable)
478a4071fbbSHaijun Zhang 		return;
479a4071fbbSHaijun Zhang 
480a4071fbbSHaijun Zhang 	host->data->error = 0;
481a4071fbbSHaijun Zhang 	dmastart = sg_dma_address(host->data->sg);
482a4071fbbSHaijun Zhang 	dmanow = dmastart + host->data->bytes_xfered;
483a4071fbbSHaijun Zhang 	/*
484a4071fbbSHaijun Zhang 	 * Force update to the next DMA block boundary.
485a4071fbbSHaijun Zhang 	 */
486a4071fbbSHaijun Zhang 	dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
487a4071fbbSHaijun Zhang 		SDHCI_DEFAULT_BOUNDARY_SIZE;
488a4071fbbSHaijun Zhang 	host->data->bytes_xfered = dmanow - dmastart;
489a4071fbbSHaijun Zhang 	sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
490a4071fbbSHaijun Zhang }
491a4071fbbSHaijun Zhang 
49280872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host)
4937657c3a7SAlbert Herranz {
494f4932cfdSyangbo lu 	u32 value;
4955552d7adSLaurentiu Tudor 	struct device *dev = mmc_dev(host->mmc);
4965552d7adSLaurentiu Tudor 
4975552d7adSLaurentiu Tudor 	if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
4985552d7adSLaurentiu Tudor 	    of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc"))
4995552d7adSLaurentiu Tudor 		dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
500f4932cfdSyangbo lu 
501f4932cfdSyangbo lu 	value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
502f4932cfdSyangbo lu 	value |= ESDHC_DMA_SNOOP;
503f4932cfdSyangbo lu 	sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
5047657c3a7SAlbert Herranz 	return 0;
5057657c3a7SAlbert Herranz }
5067657c3a7SAlbert Herranz 
50780872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
5087657c3a7SAlbert Herranz {
509e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
51019c3a0efSyangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
5117657c3a7SAlbert Herranz 
51219c3a0efSyangbo lu 	if (esdhc->peripheral_clock)
51319c3a0efSyangbo lu 		return esdhc->peripheral_clock;
51419c3a0efSyangbo lu 	else
515e307148fSShawn Guo 		return pltfm_host->clock;
5167657c3a7SAlbert Herranz }
5177657c3a7SAlbert Herranz 
51880872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
5197657c3a7SAlbert Herranz {
520e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
52119c3a0efSyangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
52219c3a0efSyangbo lu 	unsigned int clock;
5237657c3a7SAlbert Herranz 
52419c3a0efSyangbo lu 	if (esdhc->peripheral_clock)
52519c3a0efSyangbo lu 		clock = esdhc->peripheral_clock;
52619c3a0efSyangbo lu 	else
52719c3a0efSyangbo lu 		clock = pltfm_host->clock;
52819c3a0efSyangbo lu 	return clock / 256 / 16;
5297657c3a7SAlbert Herranz }
5307657c3a7SAlbert Herranz 
531dd3f6983Syangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
532dd3f6983Syangbo lu {
533dd3f6983Syangbo lu 	u32 val;
534dd3f6983Syangbo lu 	ktime_t timeout;
535dd3f6983Syangbo lu 
536dd3f6983Syangbo lu 	val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
537dd3f6983Syangbo lu 
538dd3f6983Syangbo lu 	if (enable)
539dd3f6983Syangbo lu 		val |= ESDHC_CLOCK_SDCLKEN;
540dd3f6983Syangbo lu 	else
541dd3f6983Syangbo lu 		val &= ~ESDHC_CLOCK_SDCLKEN;
542dd3f6983Syangbo lu 
543dd3f6983Syangbo lu 	sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
544dd3f6983Syangbo lu 
545dd3f6983Syangbo lu 	/* Wait max 20 ms */
546dd3f6983Syangbo lu 	timeout = ktime_add_ms(ktime_get(), 20);
547dd3f6983Syangbo lu 	val = ESDHC_CLOCK_STABLE;
548ea6d0273SAdrian Hunter 	while  (1) {
549ea6d0273SAdrian Hunter 		bool timedout = ktime_after(ktime_get(), timeout);
550ea6d0273SAdrian Hunter 
551ea6d0273SAdrian Hunter 		if (sdhci_readl(host, ESDHC_PRSSTAT) & val)
552ea6d0273SAdrian Hunter 			break;
553ea6d0273SAdrian Hunter 		if (timedout) {
554dd3f6983Syangbo lu 			pr_err("%s: Internal clock never stabilised.\n",
555dd3f6983Syangbo lu 				mmc_hostname(host->mmc));
556dd3f6983Syangbo lu 			break;
557dd3f6983Syangbo lu 		}
558dd3f6983Syangbo lu 		udelay(10);
559dd3f6983Syangbo lu 	}
560dd3f6983Syangbo lu }
561dd3f6983Syangbo lu 
562f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
563f060bc9cSJerry Huang {
564f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
5658605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
566bd455029SJoakim Tjernlund 	int pre_div = 1;
567d31fc00aSDong Aisheng 	int div = 1;
5686079e63cSYangbo Lu 	int division;
569e145ac45Syangbo lu 	ktime_t timeout;
57067fdfbdfSyinbo.zhu 	long fixup = 0;
571d31fc00aSDong Aisheng 	u32 temp;
572d31fc00aSDong Aisheng 
5731650d0c7SRussell King 	host->mmc->actual_clock = 0;
5741650d0c7SRussell King 
575dd3f6983Syangbo lu 	if (clock == 0) {
576dd3f6983Syangbo lu 		esdhc_clock_enable(host, false);
577373073efSRussell King 		return;
578dd3f6983Syangbo lu 	}
579d31fc00aSDong Aisheng 
58077bd2f6fSYangbo Lu 	/* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
581f4932cfdSyangbo lu 	if (esdhc->vendor_ver < VENDOR_V_23)
58277bd2f6fSYangbo Lu 		pre_div = 2;
58377bd2f6fSYangbo Lu 
58467fdfbdfSyinbo.zhu 	if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
58567fdfbdfSyinbo.zhu 		esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
58667fdfbdfSyinbo.zhu 		fixup = esdhc->clk_fixup->sd_dflt_max_clk;
58767fdfbdfSyinbo.zhu 	else if (esdhc->clk_fixup)
58867fdfbdfSyinbo.zhu 		fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
589a627f025Syangbo lu 
59067fdfbdfSyinbo.zhu 	if (fixup && clock > fixup)
59167fdfbdfSyinbo.zhu 		clock = fixup;
592f060bc9cSJerry Huang 
593d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
594e87d2db2Syangbo lu 	temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
595e87d2db2Syangbo lu 		  ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
596d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
597d31fc00aSDong Aisheng 
598d31fc00aSDong Aisheng 	while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
599d31fc00aSDong Aisheng 		pre_div *= 2;
600d31fc00aSDong Aisheng 
601d31fc00aSDong Aisheng 	while (host->max_clk / pre_div / div > clock && div < 16)
602d31fc00aSDong Aisheng 		div++;
603d31fc00aSDong Aisheng 
6046079e63cSYangbo Lu 	if (esdhc->quirk_limited_clk_division &&
6056079e63cSYangbo Lu 	    clock == MMC_HS200_MAX_DTR &&
6066079e63cSYangbo Lu 	    (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 ||
6076079e63cSYangbo Lu 	     host->flags & SDHCI_HS400_TUNING)) {
6086079e63cSYangbo Lu 		division = pre_div * div;
6096079e63cSYangbo Lu 		if (division <= 4) {
6106079e63cSYangbo Lu 			pre_div = 4;
6116079e63cSYangbo Lu 			div = 1;
6126079e63cSYangbo Lu 		} else if (division <= 8) {
6136079e63cSYangbo Lu 			pre_div = 4;
6146079e63cSYangbo Lu 			div = 2;
6156079e63cSYangbo Lu 		} else if (division <= 12) {
6166079e63cSYangbo Lu 			pre_div = 4;
6176079e63cSYangbo Lu 			div = 3;
6186079e63cSYangbo Lu 		} else {
619b11c36d5SColin Ian King 			pr_warn("%s: using unsupported clock division.\n",
6206079e63cSYangbo Lu 				mmc_hostname(host->mmc));
6216079e63cSYangbo Lu 		}
6226079e63cSYangbo Lu 	}
6236079e63cSYangbo Lu 
624d31fc00aSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
625e76b8559SDong Aisheng 		clock, host->max_clk / pre_div / div);
626bd455029SJoakim Tjernlund 	host->mmc->actual_clock = host->max_clk / pre_div / div;
627b1f378abSYinbo Zhu 	esdhc->div_ratio = pre_div * div;
628d31fc00aSDong Aisheng 	pre_div >>= 1;
629d31fc00aSDong Aisheng 	div--;
630d31fc00aSDong Aisheng 
631d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
632d31fc00aSDong Aisheng 	temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
633d31fc00aSDong Aisheng 		| (div << ESDHC_DIVIDER_SHIFT)
634d31fc00aSDong Aisheng 		| (pre_div << ESDHC_PREDIV_SHIFT));
635d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
636e87d2db2Syangbo lu 
63754e08d9aSYangbo Lu 	if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 &&
63854e08d9aSYangbo Lu 	    clock == MMC_HS200_MAX_DTR) {
63954e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_TBCTL);
64054e08d9aSYangbo Lu 		sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL);
64154e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_SDCLKCTL);
64254e08d9aSYangbo Lu 		sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL);
64354e08d9aSYangbo Lu 		esdhc_clock_enable(host, true);
64454e08d9aSYangbo Lu 
64554e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_DLLCFG0);
64658d0bf84SYangbo Lu 		temp |= ESDHC_DLL_ENABLE;
64758d0bf84SYangbo Lu 		if (host->mmc->actual_clock == MMC_HS200_MAX_DTR)
64858d0bf84SYangbo Lu 			temp |= ESDHC_DLL_FREQ_SEL;
64954e08d9aSYangbo Lu 		sdhci_writel(host, temp, ESDHC_DLLCFG0);
65054e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_TBCTL);
65154e08d9aSYangbo Lu 		sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL);
65254e08d9aSYangbo Lu 
65354e08d9aSYangbo Lu 		esdhc_clock_enable(host, false);
65454e08d9aSYangbo Lu 		temp = sdhci_readl(host, ESDHC_DMA_SYSCTL);
65554e08d9aSYangbo Lu 		temp |= ESDHC_FLUSH_ASYNC_FIFO;
65654e08d9aSYangbo Lu 		sdhci_writel(host, temp, ESDHC_DMA_SYSCTL);
65754e08d9aSYangbo Lu 	}
65854e08d9aSYangbo Lu 
659e87d2db2Syangbo lu 	/* Wait max 20 ms */
660e145ac45Syangbo lu 	timeout = ktime_add_ms(ktime_get(), 20);
661ea6d0273SAdrian Hunter 	while (1) {
662ea6d0273SAdrian Hunter 		bool timedout = ktime_after(ktime_get(), timeout);
663ea6d0273SAdrian Hunter 
664ea6d0273SAdrian Hunter 		if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
665ea6d0273SAdrian Hunter 			break;
666ea6d0273SAdrian Hunter 		if (timedout) {
667e87d2db2Syangbo lu 			pr_err("%s: Internal clock never stabilised.\n",
668e87d2db2Syangbo lu 				mmc_hostname(host->mmc));
669e87d2db2Syangbo lu 			return;
670e87d2db2Syangbo lu 		}
671e145ac45Syangbo lu 		udelay(10);
672f060bc9cSJerry Huang 	}
673f060bc9cSJerry Huang 
67454e08d9aSYangbo Lu 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
675e87d2db2Syangbo lu 	temp |= ESDHC_CLOCK_SDCLKEN;
676e87d2db2Syangbo lu 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
677e87d2db2Syangbo lu }
678e87d2db2Syangbo lu 
6792317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
68066b50a00SOded Gabbay {
68166b50a00SOded Gabbay 	u32 ctrl;
68266b50a00SOded Gabbay 
683f4932cfdSyangbo lu 	ctrl = sdhci_readl(host, ESDHC_PROCTL);
684f4932cfdSyangbo lu 	ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
68566b50a00SOded Gabbay 	switch (width) {
68666b50a00SOded Gabbay 	case MMC_BUS_WIDTH_8:
687f4932cfdSyangbo lu 		ctrl |= ESDHC_CTRL_8BITBUS;
68866b50a00SOded Gabbay 		break;
68966b50a00SOded Gabbay 
69066b50a00SOded Gabbay 	case MMC_BUS_WIDTH_4:
691f4932cfdSyangbo lu 		ctrl |= ESDHC_CTRL_4BITBUS;
69266b50a00SOded Gabbay 		break;
69366b50a00SOded Gabbay 
69466b50a00SOded Gabbay 	default:
69566b50a00SOded Gabbay 		break;
69666b50a00SOded Gabbay 	}
69766b50a00SOded Gabbay 
698f4932cfdSyangbo lu 	sdhci_writel(host, ctrl, ESDHC_PROCTL);
69966b50a00SOded Gabbay }
70066b50a00SOded Gabbay 
701304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask)
702304f0a98SAlessio Igor Bogani {
70348e304ccSYangbo Lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
70448e304ccSYangbo Lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
705f2bc6000Syinbo.zhu 	u32 val;
706f2bc6000Syinbo.zhu 
707304f0a98SAlessio Igor Bogani 	sdhci_reset(host, mask);
708304f0a98SAlessio Igor Bogani 
709304f0a98SAlessio Igor Bogani 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
710304f0a98SAlessio Igor Bogani 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
711f2bc6000Syinbo.zhu 
7125dd19552SYinbo Zhu 	if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc"))
7135dd19552SYinbo Zhu 		mdelay(5);
7145dd19552SYinbo Zhu 
715f2bc6000Syinbo.zhu 	if (mask & SDHCI_RESET_ALL) {
716f2bc6000Syinbo.zhu 		val = sdhci_readl(host, ESDHC_TBCTL);
717f2bc6000Syinbo.zhu 		val &= ~ESDHC_TB_EN;
718f2bc6000Syinbo.zhu 		sdhci_writel(host, val, ESDHC_TBCTL);
71948e304ccSYangbo Lu 
72048e304ccSYangbo Lu 		if (esdhc->quirk_unreliable_pulse_detection) {
72148e304ccSYangbo Lu 			val = sdhci_readl(host, ESDHC_DLLCFG1);
72248e304ccSYangbo Lu 			val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL;
72348e304ccSYangbo Lu 			sdhci_writel(host, val, ESDHC_DLLCFG1);
72448e304ccSYangbo Lu 		}
725f2bc6000Syinbo.zhu 	}
726304f0a98SAlessio Igor Bogani }
727304f0a98SAlessio Igor Bogani 
728ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific
729ea35645aSyangbo lu  * configuration and status registers for the device. There is a
730ea35645aSyangbo lu  * SDHC IO VSEL control register on SCFG for some platforms. It's
731ea35645aSyangbo lu  * used to support SDHC IO voltage switching.
732ea35645aSyangbo lu  */
733ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = {
734ea35645aSyangbo lu 	{ .compatible = "fsl,t1040-scfg", },
735ea35645aSyangbo lu 	{ .compatible = "fsl,ls1012a-scfg", },
736ea35645aSyangbo lu 	{ .compatible = "fsl,ls1046a-scfg", },
737ea35645aSyangbo lu 	{}
738ea35645aSyangbo lu };
739ea35645aSyangbo lu 
740ea35645aSyangbo lu /* SDHC IO VSEL control register definition */
741ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR	0x408
742ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN	0x80000000
743ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL	0x60000000
744ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS	0x00000001
745ea35645aSyangbo lu 
746ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
747ea35645aSyangbo lu 				       struct mmc_ios *ios)
748ea35645aSyangbo lu {
749ea35645aSyangbo lu 	struct sdhci_host *host = mmc_priv(mmc);
750ea35645aSyangbo lu 	struct device_node *scfg_node;
751ea35645aSyangbo lu 	void __iomem *scfg_base = NULL;
752ea35645aSyangbo lu 	u32 sdhciovselcr;
753ea35645aSyangbo lu 	u32 val;
754ea35645aSyangbo lu 
755ea35645aSyangbo lu 	/*
756ea35645aSyangbo lu 	 * Signal Voltage Switching is only applicable for Host Controllers
757ea35645aSyangbo lu 	 * v3.00 and above.
758ea35645aSyangbo lu 	 */
759ea35645aSyangbo lu 	if (host->version < SDHCI_SPEC_300)
760ea35645aSyangbo lu 		return 0;
761ea35645aSyangbo lu 
762ea35645aSyangbo lu 	val = sdhci_readl(host, ESDHC_PROCTL);
763ea35645aSyangbo lu 
764ea35645aSyangbo lu 	switch (ios->signal_voltage) {
765ea35645aSyangbo lu 	case MMC_SIGNAL_VOLTAGE_330:
766ea35645aSyangbo lu 		val &= ~ESDHC_VOLT_SEL;
767ea35645aSyangbo lu 		sdhci_writel(host, val, ESDHC_PROCTL);
768ea35645aSyangbo lu 		return 0;
769ea35645aSyangbo lu 	case MMC_SIGNAL_VOLTAGE_180:
770ea35645aSyangbo lu 		scfg_node = of_find_matching_node(NULL, scfg_device_ids);
771ea35645aSyangbo lu 		if (scfg_node)
772ea35645aSyangbo lu 			scfg_base = of_iomap(scfg_node, 0);
773ea35645aSyangbo lu 		if (scfg_base) {
774ea35645aSyangbo lu 			sdhciovselcr = SDHCIOVSELCR_TGLEN |
775ea35645aSyangbo lu 				       SDHCIOVSELCR_VSELVAL;
776ea35645aSyangbo lu 			iowrite32be(sdhciovselcr,
777ea35645aSyangbo lu 				scfg_base + SCFG_SDHCIOVSELCR);
778ea35645aSyangbo lu 
779ea35645aSyangbo lu 			val |= ESDHC_VOLT_SEL;
780ea35645aSyangbo lu 			sdhci_writel(host, val, ESDHC_PROCTL);
781ea35645aSyangbo lu 			mdelay(5);
782ea35645aSyangbo lu 
783ea35645aSyangbo lu 			sdhciovselcr = SDHCIOVSELCR_TGLEN |
784ea35645aSyangbo lu 				       SDHCIOVSELCR_SDHC_VS;
785ea35645aSyangbo lu 			iowrite32be(sdhciovselcr,
786ea35645aSyangbo lu 				scfg_base + SCFG_SDHCIOVSELCR);
787ea35645aSyangbo lu 			iounmap(scfg_base);
788ea35645aSyangbo lu 		} else {
789ea35645aSyangbo lu 			val |= ESDHC_VOLT_SEL;
790ea35645aSyangbo lu 			sdhci_writel(host, val, ESDHC_PROCTL);
791ea35645aSyangbo lu 		}
792ea35645aSyangbo lu 		return 0;
793ea35645aSyangbo lu 	default:
794ea35645aSyangbo lu 		return 0;
795ea35645aSyangbo lu 	}
796ea35645aSyangbo lu }
797ea35645aSyangbo lu 
798b1f378abSYinbo Zhu static struct soc_device_attribute soc_fixup_tuning[] = {
799b1f378abSYinbo Zhu 	{ .family = "QorIQ T1040", .revision = "1.0", },
800b1f378abSYinbo Zhu 	{ .family = "QorIQ T2080", .revision = "1.0", },
801b1f378abSYinbo Zhu 	{ .family = "QorIQ T1023", .revision = "1.0", },
802b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1021A", .revision = "1.0", },
803b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1080A", .revision = "1.0", },
804b1f378abSYinbo Zhu 	{ .family = "QorIQ LS2080A", .revision = "1.0", },
805b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1012A", .revision = "1.0", },
806b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1043A", .revision = "1.*", },
807b1f378abSYinbo Zhu 	{ .family = "QorIQ LS1046A", .revision = "1.0", },
808b1f378abSYinbo Zhu 	{ },
809b1f378abSYinbo Zhu };
810b1f378abSYinbo Zhu 
81154e08d9aSYangbo Lu static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable)
812ba49cbd0Syangbo lu {
813ba49cbd0Syangbo lu 	u32 val;
814ba49cbd0Syangbo lu 
815ba49cbd0Syangbo lu 	esdhc_clock_enable(host, false);
81654e08d9aSYangbo Lu 
817ba49cbd0Syangbo lu 	val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
818ba49cbd0Syangbo lu 	val |= ESDHC_FLUSH_ASYNC_FIFO;
819ba49cbd0Syangbo lu 	sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
820ba49cbd0Syangbo lu 
821ba49cbd0Syangbo lu 	val = sdhci_readl(host, ESDHC_TBCTL);
82254e08d9aSYangbo Lu 	if (enable)
823ba49cbd0Syangbo lu 		val |= ESDHC_TB_EN;
82454e08d9aSYangbo Lu 	else
82554e08d9aSYangbo Lu 		val &= ~ESDHC_TB_EN;
826ba49cbd0Syangbo lu 	sdhci_writel(host, val, ESDHC_TBCTL);
827ba49cbd0Syangbo lu 
82854e08d9aSYangbo Lu 	esdhc_clock_enable(host, true);
82954e08d9aSYangbo Lu }
83054e08d9aSYangbo Lu 
83154e08d9aSYangbo Lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
83254e08d9aSYangbo Lu {
83354e08d9aSYangbo Lu 	struct sdhci_host *host = mmc_priv(mmc);
83454e08d9aSYangbo Lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
83554e08d9aSYangbo Lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
83654e08d9aSYangbo Lu 	bool hs400_tuning;
83754e08d9aSYangbo Lu 	u32 val;
83854e08d9aSYangbo Lu 	int ret;
83954e08d9aSYangbo Lu 
8406079e63cSYangbo Lu 	if (esdhc->quirk_limited_clk_division &&
8416079e63cSYangbo Lu 	    host->flags & SDHCI_HS400_TUNING)
8426079e63cSYangbo Lu 		esdhc_of_set_clock(host, host->clock);
8436079e63cSYangbo Lu 
84454e08d9aSYangbo Lu 	esdhc_tuning_block_enable(host, true);
84554e08d9aSYangbo Lu 
84654e08d9aSYangbo Lu 	hs400_tuning = host->flags & SDHCI_HS400_TUNING;
84754e08d9aSYangbo Lu 	ret = sdhci_execute_tuning(mmc, opcode);
84854e08d9aSYangbo Lu 
84954e08d9aSYangbo Lu 	if (hs400_tuning) {
85054e08d9aSYangbo Lu 		val = sdhci_readl(host, ESDHC_SDTIMNGCTL);
85154e08d9aSYangbo Lu 		val |= ESDHC_FLW_CTL_BG;
85254e08d9aSYangbo Lu 		sdhci_writel(host, val, ESDHC_SDTIMNGCTL);
85354e08d9aSYangbo Lu 	}
85454e08d9aSYangbo Lu 
855b1f378abSYinbo Zhu 	if (host->tuning_err == -EAGAIN && esdhc->quirk_fixup_tuning) {
856b1f378abSYinbo Zhu 
857b1f378abSYinbo Zhu 		/* program TBPTR[TB_WNDW_END_PTR] = 3*DIV_RATIO and
858b1f378abSYinbo Zhu 		 * program TBPTR[TB_WNDW_START_PTR] = 5*DIV_RATIO
859b1f378abSYinbo Zhu 		 */
860b1f378abSYinbo Zhu 		val = sdhci_readl(host, ESDHC_TBPTR);
861b1f378abSYinbo Zhu 		val = (val & ~((0x7f << 8) | 0x7f)) |
862b1f378abSYinbo Zhu 		(3 * esdhc->div_ratio) | ((5 * esdhc->div_ratio) << 8);
863b1f378abSYinbo Zhu 		sdhci_writel(host, val, ESDHC_TBPTR);
864b1f378abSYinbo Zhu 
865b1f378abSYinbo Zhu 		/* program the software tuning mode by setting
866b1f378abSYinbo Zhu 		 * TBCTL[TB_MODE]=2'h3
867b1f378abSYinbo Zhu 		 */
868b1f378abSYinbo Zhu 		val = sdhci_readl(host, ESDHC_TBCTL);
869b1f378abSYinbo Zhu 		val |= 0x3;
870b1f378abSYinbo Zhu 		sdhci_writel(host, val, ESDHC_TBCTL);
871b1f378abSYinbo Zhu 		sdhci_execute_tuning(mmc, opcode);
872b1f378abSYinbo Zhu 	}
87354e08d9aSYangbo Lu 	return ret;
87454e08d9aSYangbo Lu }
87554e08d9aSYangbo Lu 
87654e08d9aSYangbo Lu static void esdhc_set_uhs_signaling(struct sdhci_host *host,
87754e08d9aSYangbo Lu 				   unsigned int timing)
87854e08d9aSYangbo Lu {
87954e08d9aSYangbo Lu 	if (timing == MMC_TIMING_MMC_HS400)
88054e08d9aSYangbo Lu 		esdhc_tuning_block_enable(host, true);
88154e08d9aSYangbo Lu 	else
88254e08d9aSYangbo Lu 		sdhci_set_uhs_signaling(host, timing);
883ba49cbd0Syangbo lu }
884ba49cbd0Syangbo lu 
885b214fe59SYinbo Zhu static u32 esdhc_irq(struct sdhci_host *host, u32 intmask)
886b214fe59SYinbo Zhu {
887b214fe59SYinbo Zhu 	u32 command;
888b214fe59SYinbo Zhu 
889b214fe59SYinbo Zhu 	if (of_find_compatible_node(NULL, NULL,
890b214fe59SYinbo Zhu 				"fsl,p2020-esdhc")) {
891b214fe59SYinbo Zhu 		command = SDHCI_GET_CMD(sdhci_readw(host,
892b214fe59SYinbo Zhu 					SDHCI_COMMAND));
893b214fe59SYinbo Zhu 		if (command == MMC_WRITE_MULTIPLE_BLOCK &&
894b214fe59SYinbo Zhu 				sdhci_readw(host, SDHCI_BLOCK_COUNT) &&
895b214fe59SYinbo Zhu 				intmask & SDHCI_INT_DATA_END) {
896b214fe59SYinbo Zhu 			intmask &= ~SDHCI_INT_DATA_END;
897b214fe59SYinbo Zhu 			sdhci_writel(host, SDHCI_INT_DATA_END,
898b214fe59SYinbo Zhu 					SDHCI_INT_STATUS);
899b214fe59SYinbo Zhu 		}
900b214fe59SYinbo Zhu 	}
901b214fe59SYinbo Zhu 	return intmask;
902b214fe59SYinbo Zhu }
903b214fe59SYinbo Zhu 
9049e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP
905723f7924SRussell King static u32 esdhc_proctl;
906723f7924SRussell King static int esdhc_of_suspend(struct device *dev)
907723f7924SRussell King {
908723f7924SRussell King 	struct sdhci_host *host = dev_get_drvdata(dev);
909723f7924SRussell King 
910f4932cfdSyangbo lu 	esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
911723f7924SRussell King 
912d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
913d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
914d38dcad4SAdrian Hunter 
915723f7924SRussell King 	return sdhci_suspend_host(host);
916723f7924SRussell King }
917723f7924SRussell King 
91806732b84SUlf Hansson static int esdhc_of_resume(struct device *dev)
919723f7924SRussell King {
920723f7924SRussell King 	struct sdhci_host *host = dev_get_drvdata(dev);
921723f7924SRussell King 	int ret = sdhci_resume_host(host);
922723f7924SRussell King 
923723f7924SRussell King 	if (ret == 0) {
924723f7924SRussell King 		/* Isn't this already done by sdhci_resume_host() ? --rmk */
925723f7924SRussell King 		esdhc_of_enable_dma(host);
926f4932cfdSyangbo lu 		sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
927723f7924SRussell King 	}
928723f7924SRussell King 	return ret;
929723f7924SRussell King }
930723f7924SRussell King #endif
931723f7924SRussell King 
9329e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
9339e48b336SUlf Hansson 			esdhc_of_suspend,
9349e48b336SUlf Hansson 			esdhc_of_resume);
9359e48b336SUlf Hansson 
936f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = {
937f4932cfdSyangbo lu 	.read_l = esdhc_be_readl,
938f4932cfdSyangbo lu 	.read_w = esdhc_be_readw,
939f4932cfdSyangbo lu 	.read_b = esdhc_be_readb,
940f4932cfdSyangbo lu 	.write_l = esdhc_be_writel,
941f4932cfdSyangbo lu 	.write_w = esdhc_be_writew,
942f4932cfdSyangbo lu 	.write_b = esdhc_be_writeb,
943f4932cfdSyangbo lu 	.set_clock = esdhc_of_set_clock,
944f4932cfdSyangbo lu 	.enable_dma = esdhc_of_enable_dma,
945f4932cfdSyangbo lu 	.get_max_clock = esdhc_of_get_max_clock,
946f4932cfdSyangbo lu 	.get_min_clock = esdhc_of_get_min_clock,
947f4932cfdSyangbo lu 	.adma_workaround = esdhc_of_adma_workaround,
948f4932cfdSyangbo lu 	.set_bus_width = esdhc_pltfm_set_bus_width,
949f4932cfdSyangbo lu 	.reset = esdhc_reset,
95054e08d9aSYangbo Lu 	.set_uhs_signaling = esdhc_set_uhs_signaling,
951b214fe59SYinbo Zhu 	.irq = esdhc_irq,
952f4932cfdSyangbo lu };
953f4932cfdSyangbo lu 
954f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = {
955f4932cfdSyangbo lu 	.read_l = esdhc_le_readl,
956f4932cfdSyangbo lu 	.read_w = esdhc_le_readw,
957f4932cfdSyangbo lu 	.read_b = esdhc_le_readb,
958f4932cfdSyangbo lu 	.write_l = esdhc_le_writel,
959f4932cfdSyangbo lu 	.write_w = esdhc_le_writew,
960f4932cfdSyangbo lu 	.write_b = esdhc_le_writeb,
961f4932cfdSyangbo lu 	.set_clock = esdhc_of_set_clock,
962f4932cfdSyangbo lu 	.enable_dma = esdhc_of_enable_dma,
963f4932cfdSyangbo lu 	.get_max_clock = esdhc_of_get_max_clock,
964f4932cfdSyangbo lu 	.get_min_clock = esdhc_of_get_min_clock,
965f4932cfdSyangbo lu 	.adma_workaround = esdhc_of_adma_workaround,
966f4932cfdSyangbo lu 	.set_bus_width = esdhc_pltfm_set_bus_width,
967f4932cfdSyangbo lu 	.reset = esdhc_reset,
96854e08d9aSYangbo Lu 	.set_uhs_signaling = esdhc_set_uhs_signaling,
969b214fe59SYinbo Zhu 	.irq = esdhc_irq,
970f4932cfdSyangbo lu };
971f4932cfdSyangbo lu 
972f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
973e9acc77dSyangbo lu 	.quirks = ESDHC_DEFAULT_QUIRKS |
974e9acc77dSyangbo lu #ifdef CONFIG_PPC
975e9acc77dSyangbo lu 		  SDHCI_QUIRK_BROKEN_CARD_DETECTION |
976e9acc77dSyangbo lu #endif
977e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_CARD_NO_RESET |
978e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
979f4932cfdSyangbo lu 	.ops = &sdhci_esdhc_be_ops,
9807657c3a7SAlbert Herranz };
98138576af1SShawn Guo 
982f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
983e9acc77dSyangbo lu 	.quirks = ESDHC_DEFAULT_QUIRKS |
984e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_CARD_NO_RESET |
985e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
986f4932cfdSyangbo lu 	.ops = &sdhci_esdhc_le_ops,
987f4932cfdSyangbo lu };
988f4932cfdSyangbo lu 
989151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = {
990151ede40Syangbo lu 	{ .family = "QorIQ T4240", .revision = "1.0", },
991151ede40Syangbo lu 	{ .family = "QorIQ T4240", .revision = "2.0", },
992151ede40Syangbo lu 	{ },
993151ede40Syangbo lu };
994151ede40Syangbo lu 
9956079e63cSYangbo Lu static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = {
9966079e63cSYangbo Lu 	{ .family = "QorIQ LX2160A", .revision = "1.0", },
9978e9a6919SYinbo Zhu 	{ .family = "QorIQ LX2160A", .revision = "2.0", },
9986079e63cSYangbo Lu 	{ },
9996079e63cSYangbo Lu };
10006079e63cSYangbo Lu 
100148e304ccSYangbo Lu static struct soc_device_attribute soc_unreliable_pulse_detection[] = {
100248e304ccSYangbo Lu 	{ .family = "QorIQ LX2160A", .revision = "1.0", },
100348e304ccSYangbo Lu 	{ },
100448e304ccSYangbo Lu };
100548e304ccSYangbo Lu 
1006f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
1007f4932cfdSyangbo lu {
100867fdfbdfSyinbo.zhu 	const struct of_device_id *match;
1009f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host;
1010f4932cfdSyangbo lu 	struct sdhci_esdhc *esdhc;
101119c3a0efSyangbo lu 	struct device_node *np;
101219c3a0efSyangbo lu 	struct clk *clk;
101319c3a0efSyangbo lu 	u32 val;
1014f4932cfdSyangbo lu 	u16 host_ver;
1015f4932cfdSyangbo lu 
1016f4932cfdSyangbo lu 	pltfm_host = sdhci_priv(host);
10178605e7aeSJisheng Zhang 	esdhc = sdhci_pltfm_priv(pltfm_host);
1018f4932cfdSyangbo lu 
1019f4932cfdSyangbo lu 	host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
1020f4932cfdSyangbo lu 	esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
1021f4932cfdSyangbo lu 			     SDHCI_VENDOR_VER_SHIFT;
1022f4932cfdSyangbo lu 	esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
1023151ede40Syangbo lu 	if (soc_device_match(soc_incorrect_hostver))
1024151ede40Syangbo lu 		esdhc->quirk_incorrect_hostver = true;
1025151ede40Syangbo lu 	else
1026151ede40Syangbo lu 		esdhc->quirk_incorrect_hostver = false;
102719c3a0efSyangbo lu 
10286079e63cSYangbo Lu 	if (soc_device_match(soc_fixup_sdhc_clkdivs))
10296079e63cSYangbo Lu 		esdhc->quirk_limited_clk_division = true;
10306079e63cSYangbo Lu 	else
10316079e63cSYangbo Lu 		esdhc->quirk_limited_clk_division = false;
10326079e63cSYangbo Lu 
103348e304ccSYangbo Lu 	if (soc_device_match(soc_unreliable_pulse_detection))
103448e304ccSYangbo Lu 		esdhc->quirk_unreliable_pulse_detection = true;
103548e304ccSYangbo Lu 	else
103648e304ccSYangbo Lu 		esdhc->quirk_unreliable_pulse_detection = false;
103748e304ccSYangbo Lu 
103867fdfbdfSyinbo.zhu 	match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
103967fdfbdfSyinbo.zhu 	if (match)
104067fdfbdfSyinbo.zhu 		esdhc->clk_fixup = match->data;
104119c3a0efSyangbo lu 	np = pdev->dev.of_node;
104219c3a0efSyangbo lu 	clk = of_clk_get(np, 0);
104319c3a0efSyangbo lu 	if (!IS_ERR(clk)) {
104419c3a0efSyangbo lu 		/*
104519c3a0efSyangbo lu 		 * esdhc->peripheral_clock would be assigned with a value
104619c3a0efSyangbo lu 		 * which is eSDHC base clock when use periperal clock.
104719c3a0efSyangbo lu 		 * For ls1046a, the clock value got by common clk API is
104819c3a0efSyangbo lu 		 * peripheral clock while the eSDHC base clock is 1/2
104919c3a0efSyangbo lu 		 * peripheral clock.
105019c3a0efSyangbo lu 		 */
105119c3a0efSyangbo lu 		if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
105219c3a0efSyangbo lu 			esdhc->peripheral_clock = clk_get_rate(clk) / 2;
105319c3a0efSyangbo lu 		else
105419c3a0efSyangbo lu 			esdhc->peripheral_clock = clk_get_rate(clk);
105519c3a0efSyangbo lu 
105619c3a0efSyangbo lu 		clk_put(clk);
105719c3a0efSyangbo lu 	}
105819c3a0efSyangbo lu 
105919c3a0efSyangbo lu 	if (esdhc->peripheral_clock) {
106019c3a0efSyangbo lu 		esdhc_clock_enable(host, false);
106119c3a0efSyangbo lu 		val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
106219c3a0efSyangbo lu 		val |= ESDHC_PERIPHERAL_CLK_SEL;
106319c3a0efSyangbo lu 		sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
106419c3a0efSyangbo lu 		esdhc_clock_enable(host, true);
106519c3a0efSyangbo lu 	}
1066f4932cfdSyangbo lu }
1067f4932cfdSyangbo lu 
106854e08d9aSYangbo Lu static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc)
106954e08d9aSYangbo Lu {
107054e08d9aSYangbo Lu 	esdhc_tuning_block_enable(mmc_priv(mmc), false);
107154e08d9aSYangbo Lu 	return 0;
107254e08d9aSYangbo Lu }
107354e08d9aSYangbo Lu 
1074c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev)
107538576af1SShawn Guo {
107666b50a00SOded Gabbay 	struct sdhci_host *host;
1077dcaff04dSOded Gabbay 	struct device_node *np;
10781ef5e49eSyangbo lu 	struct sdhci_pltfm_host *pltfm_host;
10791ef5e49eSyangbo lu 	struct sdhci_esdhc *esdhc;
108066b50a00SOded Gabbay 	int ret;
108166b50a00SOded Gabbay 
1082f4932cfdSyangbo lu 	np = pdev->dev.of_node;
1083f4932cfdSyangbo lu 
1084150d4240SJulia Lawall 	if (of_property_read_bool(np, "little-endian"))
10858605e7aeSJisheng Zhang 		host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
10868605e7aeSJisheng Zhang 					sizeof(struct sdhci_esdhc));
1087f4932cfdSyangbo lu 	else
10888605e7aeSJisheng Zhang 		host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
10898605e7aeSJisheng Zhang 					sizeof(struct sdhci_esdhc));
1090f4932cfdSyangbo lu 
109166b50a00SOded Gabbay 	if (IS_ERR(host))
109266b50a00SOded Gabbay 		return PTR_ERR(host);
109366b50a00SOded Gabbay 
1094ea35645aSyangbo lu 	host->mmc_host_ops.start_signal_voltage_switch =
1095ea35645aSyangbo lu 		esdhc_signal_voltage_switch;
1096ba49cbd0Syangbo lu 	host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
109754e08d9aSYangbo Lu 	host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr;
10986b236f37Syangbo lu 	host->tuning_delay = 1;
1099ea35645aSyangbo lu 
1100f4932cfdSyangbo lu 	esdhc_init(pdev, host);
1101f4932cfdSyangbo lu 
110266b50a00SOded Gabbay 	sdhci_get_of_property(pdev);
110366b50a00SOded Gabbay 
11041ef5e49eSyangbo lu 	pltfm_host = sdhci_priv(host);
11058605e7aeSJisheng Zhang 	esdhc = sdhci_pltfm_priv(pltfm_host);
1106b1f378abSYinbo Zhu 	if (soc_device_match(soc_fixup_tuning))
1107b1f378abSYinbo Zhu 		esdhc->quirk_fixup_tuning = true;
1108b1f378abSYinbo Zhu 	else
1109b1f378abSYinbo Zhu 		esdhc->quirk_fixup_tuning = false;
1110b1f378abSYinbo Zhu 
11111ef5e49eSyangbo lu 	if (esdhc->vendor_ver == VENDOR_V_22)
11121ef5e49eSyangbo lu 		host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
11131ef5e49eSyangbo lu 
11141ef5e49eSyangbo lu 	if (esdhc->vendor_ver > VENDOR_V_22)
11151ef5e49eSyangbo lu 		host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
11161ef5e49eSyangbo lu 
111705cb6b2aSYinbo Zhu 	if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) {
1118a46e4271SYinbo Zhu 		host->quirks2 |= SDHCI_QUIRK_RESET_AFTER_REQUEST;
111905cb6b2aSYinbo Zhu 		host->quirks2 |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
112005cb6b2aSYinbo Zhu 	}
1121a46e4271SYinbo Zhu 
112274fd5e30SYangbo Lu 	if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
112374fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p5020-esdhc") ||
112474fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p4080-esdhc") ||
112574fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p1020-esdhc") ||
1126e9acc77dSyangbo lu 	    of_device_is_compatible(np, "fsl,t1040-esdhc"))
112774fd5e30SYangbo Lu 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
112874fd5e30SYangbo Lu 
1129a22950c8Syangbo lu 	if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
1130a22950c8Syangbo lu 		host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1131a22950c8Syangbo lu 
1132*1f1929f3SYangbo Lu 	esdhc->quirk_ignore_data_inhibit = false;
1133dcaff04dSOded Gabbay 	if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
1134dcaff04dSOded Gabbay 		/*
1135dcaff04dSOded Gabbay 		 * Freescale messed up with P2020 as it has a non-standard
1136dcaff04dSOded Gabbay 		 * host control register
1137dcaff04dSOded Gabbay 		 */
1138dcaff04dSOded Gabbay 		host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
1139*1f1929f3SYangbo Lu 		esdhc->quirk_ignore_data_inhibit = true;
1140dcaff04dSOded Gabbay 	}
1141dcaff04dSOded Gabbay 
114266b50a00SOded Gabbay 	/* call to generic mmc_of_parse to support additional capabilities */
1143f0991408SUlf Hansson 	ret = mmc_of_parse(host->mmc);
1144f0991408SUlf Hansson 	if (ret)
1145f0991408SUlf Hansson 		goto err;
1146f0991408SUlf Hansson 
1147490104acSHaijun Zhang 	mmc_of_parse_voltage(np, &host->ocr_mask);
114866b50a00SOded Gabbay 
114966b50a00SOded Gabbay 	ret = sdhci_add_host(host);
115066b50a00SOded Gabbay 	if (ret)
1151f0991408SUlf Hansson 		goto err;
115266b50a00SOded Gabbay 
1153f0991408SUlf Hansson 	return 0;
1154f0991408SUlf Hansson  err:
1155f0991408SUlf Hansson 	sdhci_pltfm_free(pdev);
115666b50a00SOded Gabbay 	return ret;
115738576af1SShawn Guo }
115838576af1SShawn Guo 
115938576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = {
116038576af1SShawn Guo 	.driver = {
116138576af1SShawn Guo 		.name = "sdhci-esdhc",
116238576af1SShawn Guo 		.of_match_table = sdhci_esdhc_of_match,
11639e48b336SUlf Hansson 		.pm = &esdhc_of_dev_pm_ops,
116438576af1SShawn Guo 	},
116538576af1SShawn Guo 	.probe = sdhci_esdhc_probe,
1166caebcae9SKevin Hao 	.remove = sdhci_pltfm_unregister,
116738576af1SShawn Guo };
116838576af1SShawn Guo 
1169d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver);
117038576af1SShawn Guo 
117138576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
117238576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
117338576af1SShawn Guo 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
117438576af1SShawn Guo MODULE_LICENSE("GPL v2");
1175