xref: /openbmc/linux/drivers/mmc/host/sdhci-of-esdhc.c (revision a627f025eb0534052ff451427c16750b3530634c)
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>
257657c3a7SAlbert Herranz #include <linux/mmc/host.h>
2638576af1SShawn Guo #include "sdhci-pltfm.h"
2780872e21SWolfram Sang #include "sdhci-esdhc.h"
287657c3a7SAlbert Herranz 
29137ccd46SJerry Huang #define VENDOR_V_22	0x12
30a4071fbbSHaijun Zhang #define VENDOR_V_23	0x13
31f4932cfdSyangbo lu 
32f4932cfdSyangbo lu struct sdhci_esdhc {
33f4932cfdSyangbo lu 	u8 vendor_ver;
34f4932cfdSyangbo lu 	u8 spec_ver;
35151ede40Syangbo lu 	bool quirk_incorrect_hostver;
3619c3a0efSyangbo lu 	unsigned int peripheral_clock;
37f4932cfdSyangbo lu };
38f4932cfdSyangbo lu 
39f4932cfdSyangbo lu /**
40f4932cfdSyangbo lu  * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
41f4932cfdSyangbo lu  *		       to make it compatible with SD spec.
42f4932cfdSyangbo lu  *
43f4932cfdSyangbo lu  * @host: pointer to sdhci_host
44f4932cfdSyangbo lu  * @spec_reg: SD spec register address
45f4932cfdSyangbo lu  * @value: 32bit eSDHC register value on spec_reg address
46f4932cfdSyangbo lu  *
47f4932cfdSyangbo lu  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
48f4932cfdSyangbo lu  * registers are 32 bits. There are differences in register size, register
49f4932cfdSyangbo lu  * address, register function, bit position and function between eSDHC spec
50f4932cfdSyangbo lu  * and SD spec.
51f4932cfdSyangbo lu  *
52f4932cfdSyangbo lu  * Return a fixed up register value
53f4932cfdSyangbo lu  */
54f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host,
55f4932cfdSyangbo lu 				     int spec_reg, u32 value)
56137ccd46SJerry Huang {
57f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
588605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
59137ccd46SJerry Huang 	u32 ret;
60137ccd46SJerry Huang 
61137ccd46SJerry Huang 	/*
62137ccd46SJerry Huang 	 * The bit of ADMA flag in eSDHC is not compatible with standard
63137ccd46SJerry Huang 	 * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
64137ccd46SJerry Huang 	 * supported by eSDHC.
65137ccd46SJerry Huang 	 * And for many FSL eSDHC controller, the reset value of field
66f4932cfdSyangbo lu 	 * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
67137ccd46SJerry Huang 	 * only these vendor version is greater than 2.2/0x12 support ADMA.
68137ccd46SJerry Huang 	 */
69f4932cfdSyangbo lu 	if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
70f4932cfdSyangbo lu 		if (esdhc->vendor_ver > VENDOR_V_22) {
71f4932cfdSyangbo lu 			ret = value | SDHCI_CAN_DO_ADMA2;
72f4932cfdSyangbo lu 			return ret;
73137ccd46SJerry Huang 		}
74f4932cfdSyangbo lu 	}
75b0921d5cSMichael Walle 	/*
76b0921d5cSMichael Walle 	 * The DAT[3:0] line signal levels and the CMD line signal level are
77b0921d5cSMichael Walle 	 * not compatible with standard SDHC register. The line signal levels
78b0921d5cSMichael Walle 	 * DAT[7:0] are at bits 31:24 and the command line signal level is at
79b0921d5cSMichael Walle 	 * bit 23. All other bits are the same as in the standard SDHC
80b0921d5cSMichael Walle 	 * register.
81b0921d5cSMichael Walle 	 */
82b0921d5cSMichael Walle 	if (spec_reg == SDHCI_PRESENT_STATE) {
83b0921d5cSMichael Walle 		ret = value & 0x000fffff;
84b0921d5cSMichael Walle 		ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
85b0921d5cSMichael Walle 		ret |= (value << 1) & SDHCI_CMD_LVL;
86b0921d5cSMichael Walle 		return ret;
87b0921d5cSMichael Walle 	}
88b0921d5cSMichael Walle 
89f4932cfdSyangbo lu 	ret = value;
90137ccd46SJerry Huang 	return ret;
91137ccd46SJerry Huang }
92137ccd46SJerry Huang 
93f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host,
94f4932cfdSyangbo lu 				     int spec_reg, u32 value)
957657c3a7SAlbert Herranz {
96151ede40Syangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
97151ede40Syangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
987657c3a7SAlbert Herranz 	u16 ret;
99f4932cfdSyangbo lu 	int shift = (spec_reg & 0x2) * 8;
1007657c3a7SAlbert Herranz 
101f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_VERSION)
102f4932cfdSyangbo lu 		ret = value & 0xffff;
1037657c3a7SAlbert Herranz 	else
104f4932cfdSyangbo lu 		ret = (value >> shift) & 0xffff;
105151ede40Syangbo lu 	/* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
106151ede40Syangbo lu 	 * vendor version and spec version information.
107151ede40Syangbo lu 	 */
108151ede40Syangbo lu 	if ((spec_reg == SDHCI_HOST_VERSION) &&
109151ede40Syangbo lu 	    (esdhc->quirk_incorrect_hostver))
110151ede40Syangbo lu 		ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
111e51cbc9eSXu lei 	return ret;
112e51cbc9eSXu lei }
113e51cbc9eSXu lei 
114f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host,
115f4932cfdSyangbo lu 				     int spec_reg, u32 value)
116e51cbc9eSXu lei {
117f4932cfdSyangbo lu 	u8 ret;
118f4932cfdSyangbo lu 	u8 dma_bits;
119f4932cfdSyangbo lu 	int shift = (spec_reg & 0x3) * 8;
120f4932cfdSyangbo lu 
121f4932cfdSyangbo lu 	ret = (value >> shift) & 0xff;
122ba8c4dc9SRoy Zang 
123ba8c4dc9SRoy Zang 	/*
124ba8c4dc9SRoy Zang 	 * "DMA select" locates at offset 0x28 in SD specification, but on
125ba8c4dc9SRoy Zang 	 * P5020 or P3041, it locates at 0x29.
126ba8c4dc9SRoy Zang 	 */
127f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_CONTROL) {
128ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
129f4932cfdSyangbo lu 		dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
130ba8c4dc9SRoy Zang 		/* fixup the result */
131ba8c4dc9SRoy Zang 		ret &= ~SDHCI_CTRL_DMA_MASK;
132ba8c4dc9SRoy Zang 		ret |= dma_bits;
133ba8c4dc9SRoy Zang 	}
134f4932cfdSyangbo lu 	return ret;
135f4932cfdSyangbo lu }
136f4932cfdSyangbo lu 
137f4932cfdSyangbo lu /**
138f4932cfdSyangbo lu  * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
139f4932cfdSyangbo lu  *			written into eSDHC register.
140f4932cfdSyangbo lu  *
141f4932cfdSyangbo lu  * @host: pointer to sdhci_host
142f4932cfdSyangbo lu  * @spec_reg: SD spec register address
143f4932cfdSyangbo lu  * @value: 8/16/32bit SD spec register value that would be written
144f4932cfdSyangbo lu  * @old_value: 32bit eSDHC register value on spec_reg address
145f4932cfdSyangbo lu  *
146f4932cfdSyangbo lu  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
147f4932cfdSyangbo lu  * registers are 32 bits. There are differences in register size, register
148f4932cfdSyangbo lu  * address, register function, bit position and function between eSDHC spec
149f4932cfdSyangbo lu  * and SD spec.
150f4932cfdSyangbo lu  *
151f4932cfdSyangbo lu  * Return a fixed up register value
152f4932cfdSyangbo lu  */
153f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host,
154f4932cfdSyangbo lu 				     int spec_reg, u32 value, u32 old_value)
155f4932cfdSyangbo lu {
156f4932cfdSyangbo lu 	u32 ret;
157f4932cfdSyangbo lu 
158f4932cfdSyangbo lu 	/*
159f4932cfdSyangbo lu 	 * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
160f4932cfdSyangbo lu 	 * when SYSCTL[RSTD] is set for some special operations.
161f4932cfdSyangbo lu 	 * No any impact on other operation.
162f4932cfdSyangbo lu 	 */
163f4932cfdSyangbo lu 	if (spec_reg == SDHCI_INT_ENABLE)
164f4932cfdSyangbo lu 		ret = value | SDHCI_INT_BLK_GAP;
165f4932cfdSyangbo lu 	else
166f4932cfdSyangbo lu 		ret = value;
167ba8c4dc9SRoy Zang 
1687657c3a7SAlbert Herranz 	return ret;
1697657c3a7SAlbert Herranz }
1707657c3a7SAlbert Herranz 
171f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host,
172f4932cfdSyangbo lu 				     int spec_reg, u16 value, u32 old_value)
173a4071fbbSHaijun Zhang {
174f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
175f4932cfdSyangbo lu 	int shift = (spec_reg & 0x2) * 8;
176f4932cfdSyangbo lu 	u32 ret;
177f4932cfdSyangbo lu 
178f4932cfdSyangbo lu 	switch (spec_reg) {
179f4932cfdSyangbo lu 	case SDHCI_TRANSFER_MODE:
180a4071fbbSHaijun Zhang 		/*
181f4932cfdSyangbo lu 		 * Postpone this write, we must do it together with a
182f4932cfdSyangbo lu 		 * command write that is down below. Return old value.
183a4071fbbSHaijun Zhang 		 */
184f4932cfdSyangbo lu 		pltfm_host->xfer_mode_shadow = value;
185f4932cfdSyangbo lu 		return old_value;
186f4932cfdSyangbo lu 	case SDHCI_COMMAND:
187f4932cfdSyangbo lu 		ret = (value << 16) | pltfm_host->xfer_mode_shadow;
188f4932cfdSyangbo lu 		return ret;
189a4071fbbSHaijun Zhang 	}
190a4071fbbSHaijun Zhang 
191f4932cfdSyangbo lu 	ret = old_value & (~(0xffff << shift));
192f4932cfdSyangbo lu 	ret |= (value << shift);
193f4932cfdSyangbo lu 
194f4932cfdSyangbo lu 	if (spec_reg == SDHCI_BLOCK_SIZE) {
1957657c3a7SAlbert Herranz 		/*
1967657c3a7SAlbert Herranz 		 * Two last DMA bits are reserved, and first one is used for
1977657c3a7SAlbert Herranz 		 * non-standard blksz of 4096 bytes that we don't support
1987657c3a7SAlbert Herranz 		 * yet. So clear the DMA boundary bits.
1997657c3a7SAlbert Herranz 		 */
200f4932cfdSyangbo lu 		ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
2017657c3a7SAlbert Herranz 	}
202f4932cfdSyangbo lu 	return ret;
2037657c3a7SAlbert Herranz }
2047657c3a7SAlbert Herranz 
205f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host,
206f4932cfdSyangbo lu 				     int spec_reg, u8 value, u32 old_value)
2077657c3a7SAlbert Herranz {
208f4932cfdSyangbo lu 	u32 ret;
209f4932cfdSyangbo lu 	u32 dma_bits;
210f4932cfdSyangbo lu 	u8 tmp;
211f4932cfdSyangbo lu 	int shift = (spec_reg & 0x3) * 8;
212f4932cfdSyangbo lu 
213ba8c4dc9SRoy Zang 	/*
2149e4703dfSyangbo lu 	 * eSDHC doesn't have a standard power control register, so we do
2159e4703dfSyangbo lu 	 * nothing here to avoid incorrect operation.
2169e4703dfSyangbo lu 	 */
2179e4703dfSyangbo lu 	if (spec_reg == SDHCI_POWER_CONTROL)
2189e4703dfSyangbo lu 		return old_value;
2199e4703dfSyangbo lu 	/*
220ba8c4dc9SRoy Zang 	 * "DMA select" location is offset 0x28 in SD specification, but on
221ba8c4dc9SRoy Zang 	 * P5020 or P3041, it's located at 0x29.
222ba8c4dc9SRoy Zang 	 */
223f4932cfdSyangbo lu 	if (spec_reg == SDHCI_HOST_CONTROL) {
224dcaff04dSOded Gabbay 		/*
225dcaff04dSOded Gabbay 		 * If host control register is not standard, exit
226dcaff04dSOded Gabbay 		 * this function
227dcaff04dSOded Gabbay 		 */
228dcaff04dSOded Gabbay 		if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
229f4932cfdSyangbo lu 			return old_value;
230dcaff04dSOded Gabbay 
231ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
232f4932cfdSyangbo lu 		dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
233f4932cfdSyangbo lu 		ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
234f4932cfdSyangbo lu 		tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
235f4932cfdSyangbo lu 		      (old_value & SDHCI_CTRL_DMA_MASK);
236f4932cfdSyangbo lu 		ret = (ret & (~0xff)) | tmp;
237f4932cfdSyangbo lu 
238f4932cfdSyangbo lu 		/* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
239f4932cfdSyangbo lu 		ret &= ~ESDHC_HOST_CONTROL_RES;
240f4932cfdSyangbo lu 		return ret;
241ba8c4dc9SRoy Zang 	}
242ba8c4dc9SRoy Zang 
243f4932cfdSyangbo lu 	ret = (old_value & (~(0xff << shift))) | (value << shift);
244f4932cfdSyangbo lu 	return ret;
245f4932cfdSyangbo lu }
246f4932cfdSyangbo lu 
247f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
248f4932cfdSyangbo lu {
249f4932cfdSyangbo lu 	u32 ret;
250f4932cfdSyangbo lu 	u32 value;
251f4932cfdSyangbo lu 
252f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + reg);
253f4932cfdSyangbo lu 	ret = esdhc_readl_fixup(host, reg, value);
254f4932cfdSyangbo lu 
255f4932cfdSyangbo lu 	return ret;
256f4932cfdSyangbo lu }
257f4932cfdSyangbo lu 
258f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
259f4932cfdSyangbo lu {
260f4932cfdSyangbo lu 	u32 ret;
261f4932cfdSyangbo lu 	u32 value;
262f4932cfdSyangbo lu 
263f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + reg);
264f4932cfdSyangbo lu 	ret = esdhc_readl_fixup(host, reg, value);
265f4932cfdSyangbo lu 
266f4932cfdSyangbo lu 	return ret;
267f4932cfdSyangbo lu }
268f4932cfdSyangbo lu 
269f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
270f4932cfdSyangbo lu {
271f4932cfdSyangbo lu 	u16 ret;
272f4932cfdSyangbo lu 	u32 value;
273f4932cfdSyangbo lu 	int base = reg & ~0x3;
274f4932cfdSyangbo lu 
275f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
276f4932cfdSyangbo lu 	ret = esdhc_readw_fixup(host, reg, value);
277f4932cfdSyangbo lu 	return ret;
278f4932cfdSyangbo lu }
279f4932cfdSyangbo lu 
280f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
281f4932cfdSyangbo lu {
282f4932cfdSyangbo lu 	u16 ret;
283f4932cfdSyangbo lu 	u32 value;
284f4932cfdSyangbo lu 	int base = reg & ~0x3;
285f4932cfdSyangbo lu 
286f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
287f4932cfdSyangbo lu 	ret = esdhc_readw_fixup(host, reg, value);
288f4932cfdSyangbo lu 	return ret;
289f4932cfdSyangbo lu }
290f4932cfdSyangbo lu 
291f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
292f4932cfdSyangbo lu {
293f4932cfdSyangbo lu 	u8 ret;
294f4932cfdSyangbo lu 	u32 value;
295f4932cfdSyangbo lu 	int base = reg & ~0x3;
296f4932cfdSyangbo lu 
297f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
298f4932cfdSyangbo lu 	ret = esdhc_readb_fixup(host, reg, value);
299f4932cfdSyangbo lu 	return ret;
300f4932cfdSyangbo lu }
301f4932cfdSyangbo lu 
302f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
303f4932cfdSyangbo lu {
304f4932cfdSyangbo lu 	u8 ret;
305f4932cfdSyangbo lu 	u32 value;
306f4932cfdSyangbo lu 	int base = reg & ~0x3;
307f4932cfdSyangbo lu 
308f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
309f4932cfdSyangbo lu 	ret = esdhc_readb_fixup(host, reg, value);
310f4932cfdSyangbo lu 	return ret;
311f4932cfdSyangbo lu }
312f4932cfdSyangbo lu 
313f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
314f4932cfdSyangbo lu {
315f4932cfdSyangbo lu 	u32 value;
316f4932cfdSyangbo lu 
317f4932cfdSyangbo lu 	value = esdhc_writel_fixup(host, reg, val, 0);
318f4932cfdSyangbo lu 	iowrite32be(value, host->ioaddr + reg);
319f4932cfdSyangbo lu }
320f4932cfdSyangbo lu 
321f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
322f4932cfdSyangbo lu {
323f4932cfdSyangbo lu 	u32 value;
324f4932cfdSyangbo lu 
325f4932cfdSyangbo lu 	value = esdhc_writel_fixup(host, reg, val, 0);
326f4932cfdSyangbo lu 	iowrite32(value, host->ioaddr + reg);
327f4932cfdSyangbo lu }
328f4932cfdSyangbo lu 
329f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
330f4932cfdSyangbo lu {
331f4932cfdSyangbo lu 	int base = reg & ~0x3;
332f4932cfdSyangbo lu 	u32 value;
333f4932cfdSyangbo lu 	u32 ret;
334f4932cfdSyangbo lu 
335f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
336f4932cfdSyangbo lu 	ret = esdhc_writew_fixup(host, reg, val, value);
337f4932cfdSyangbo lu 	if (reg != SDHCI_TRANSFER_MODE)
338f4932cfdSyangbo lu 		iowrite32be(ret, host->ioaddr + base);
339f4932cfdSyangbo lu }
340f4932cfdSyangbo lu 
341f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
342f4932cfdSyangbo lu {
343f4932cfdSyangbo lu 	int base = reg & ~0x3;
344f4932cfdSyangbo lu 	u32 value;
345f4932cfdSyangbo lu 	u32 ret;
346f4932cfdSyangbo lu 
347f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
348f4932cfdSyangbo lu 	ret = esdhc_writew_fixup(host, reg, val, value);
349f4932cfdSyangbo lu 	if (reg != SDHCI_TRANSFER_MODE)
350f4932cfdSyangbo lu 		iowrite32(ret, host->ioaddr + base);
351f4932cfdSyangbo lu }
352f4932cfdSyangbo lu 
353f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
354f4932cfdSyangbo lu {
355f4932cfdSyangbo lu 	int base = reg & ~0x3;
356f4932cfdSyangbo lu 	u32 value;
357f4932cfdSyangbo lu 	u32 ret;
358f4932cfdSyangbo lu 
359f4932cfdSyangbo lu 	value = ioread32be(host->ioaddr + base);
360f4932cfdSyangbo lu 	ret = esdhc_writeb_fixup(host, reg, val, value);
361f4932cfdSyangbo lu 	iowrite32be(ret, host->ioaddr + base);
362f4932cfdSyangbo lu }
363f4932cfdSyangbo lu 
364f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
365f4932cfdSyangbo lu {
366f4932cfdSyangbo lu 	int base = reg & ~0x3;
367f4932cfdSyangbo lu 	u32 value;
368f4932cfdSyangbo lu 	u32 ret;
369f4932cfdSyangbo lu 
370f4932cfdSyangbo lu 	value = ioread32(host->ioaddr + base);
371f4932cfdSyangbo lu 	ret = esdhc_writeb_fixup(host, reg, val, value);
372f4932cfdSyangbo lu 	iowrite32(ret, host->ioaddr + base);
3737657c3a7SAlbert Herranz }
3747657c3a7SAlbert Herranz 
375a4071fbbSHaijun Zhang /*
376a4071fbbSHaijun Zhang  * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
377a4071fbbSHaijun Zhang  * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
378a4071fbbSHaijun Zhang  * and Block Gap Event(IRQSTAT[BGE]) are also set.
379a4071fbbSHaijun Zhang  * For Continue, apply soft reset for data(SYSCTL[RSTD]);
380a4071fbbSHaijun Zhang  * and re-issue the entire read transaction from beginning.
381a4071fbbSHaijun Zhang  */
382f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
383a4071fbbSHaijun Zhang {
384f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3858605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
386a4071fbbSHaijun Zhang 	bool applicable;
387a4071fbbSHaijun Zhang 	dma_addr_t dmastart;
388a4071fbbSHaijun Zhang 	dma_addr_t dmanow;
389a4071fbbSHaijun Zhang 
390a4071fbbSHaijun Zhang 	applicable = (intmask & SDHCI_INT_DATA_END) &&
391a4071fbbSHaijun Zhang 		     (intmask & SDHCI_INT_BLK_GAP) &&
392f4932cfdSyangbo lu 		     (esdhc->vendor_ver == VENDOR_V_23);
393a4071fbbSHaijun Zhang 	if (!applicable)
394a4071fbbSHaijun Zhang 		return;
395a4071fbbSHaijun Zhang 
396a4071fbbSHaijun Zhang 	host->data->error = 0;
397a4071fbbSHaijun Zhang 	dmastart = sg_dma_address(host->data->sg);
398a4071fbbSHaijun Zhang 	dmanow = dmastart + host->data->bytes_xfered;
399a4071fbbSHaijun Zhang 	/*
400a4071fbbSHaijun Zhang 	 * Force update to the next DMA block boundary.
401a4071fbbSHaijun Zhang 	 */
402a4071fbbSHaijun Zhang 	dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
403a4071fbbSHaijun Zhang 		SDHCI_DEFAULT_BOUNDARY_SIZE;
404a4071fbbSHaijun Zhang 	host->data->bytes_xfered = dmanow - dmastart;
405a4071fbbSHaijun Zhang 	sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
406a4071fbbSHaijun Zhang }
407a4071fbbSHaijun Zhang 
40880872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host)
4097657c3a7SAlbert Herranz {
410f4932cfdSyangbo lu 	u32 value;
411f4932cfdSyangbo lu 
412f4932cfdSyangbo lu 	value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
413f4932cfdSyangbo lu 	value |= ESDHC_DMA_SNOOP;
414f4932cfdSyangbo lu 	sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
4157657c3a7SAlbert Herranz 	return 0;
4167657c3a7SAlbert Herranz }
4177657c3a7SAlbert Herranz 
41880872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
4197657c3a7SAlbert Herranz {
420e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
42119c3a0efSyangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
4227657c3a7SAlbert Herranz 
42319c3a0efSyangbo lu 	if (esdhc->peripheral_clock)
42419c3a0efSyangbo lu 		return esdhc->peripheral_clock;
42519c3a0efSyangbo lu 	else
426e307148fSShawn Guo 		return pltfm_host->clock;
4277657c3a7SAlbert Herranz }
4287657c3a7SAlbert Herranz 
42980872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
4307657c3a7SAlbert Herranz {
431e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
43219c3a0efSyangbo lu 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
43319c3a0efSyangbo lu 	unsigned int clock;
4347657c3a7SAlbert Herranz 
43519c3a0efSyangbo lu 	if (esdhc->peripheral_clock)
43619c3a0efSyangbo lu 		clock = esdhc->peripheral_clock;
43719c3a0efSyangbo lu 	else
43819c3a0efSyangbo lu 		clock = pltfm_host->clock;
43919c3a0efSyangbo lu 	return clock / 256 / 16;
4407657c3a7SAlbert Herranz }
4417657c3a7SAlbert Herranz 
442f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
443f060bc9cSJerry Huang {
444f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
4458605e7aeSJisheng Zhang 	struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
446bd455029SJoakim Tjernlund 	int pre_div = 1;
447d31fc00aSDong Aisheng 	int div = 1;
448e145ac45Syangbo lu 	ktime_t timeout;
449d31fc00aSDong Aisheng 	u32 temp;
450d31fc00aSDong Aisheng 
4511650d0c7SRussell King 	host->mmc->actual_clock = 0;
4521650d0c7SRussell King 
453d31fc00aSDong Aisheng 	if (clock == 0)
454373073efSRussell King 		return;
455d31fc00aSDong Aisheng 
45677bd2f6fSYangbo Lu 	/* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
457f4932cfdSyangbo lu 	if (esdhc->vendor_ver < VENDOR_V_23)
45877bd2f6fSYangbo Lu 		pre_div = 2;
45977bd2f6fSYangbo Lu 
460*a627f025Syangbo lu 	/*
461*a627f025Syangbo lu 	 * Limit SD clock to 167MHz for ls1046a according to its datasheet
462*a627f025Syangbo lu 	 */
463*a627f025Syangbo lu 	if (clock > 167000000 &&
464*a627f025Syangbo lu 	    of_find_compatible_node(NULL, NULL, "fsl,ls1046a-esdhc"))
465*a627f025Syangbo lu 		clock = 167000000;
466*a627f025Syangbo lu 
467*a627f025Syangbo lu 	/*
468*a627f025Syangbo lu 	 * Limit SD clock to 125MHz for ls1012a according to its datasheet
469*a627f025Syangbo lu 	 */
470*a627f025Syangbo lu 	if (clock > 125000000 &&
471*a627f025Syangbo lu 	    of_find_compatible_node(NULL, NULL, "fsl,ls1012a-esdhc"))
472*a627f025Syangbo lu 		clock = 125000000;
473*a627f025Syangbo lu 
474f060bc9cSJerry Huang 	/* Workaround to reduce the clock frequency for p1010 esdhc */
475f060bc9cSJerry Huang 	if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
476f060bc9cSJerry Huang 		if (clock > 20000000)
477f060bc9cSJerry Huang 			clock -= 5000000;
478f060bc9cSJerry Huang 		if (clock > 40000000)
479f060bc9cSJerry Huang 			clock -= 5000000;
480f060bc9cSJerry Huang 	}
481f060bc9cSJerry Huang 
482d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
483e87d2db2Syangbo lu 	temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
484e87d2db2Syangbo lu 		  ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
485d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
486d31fc00aSDong Aisheng 
487d31fc00aSDong Aisheng 	while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
488d31fc00aSDong Aisheng 		pre_div *= 2;
489d31fc00aSDong Aisheng 
490d31fc00aSDong Aisheng 	while (host->max_clk / pre_div / div > clock && div < 16)
491d31fc00aSDong Aisheng 		div++;
492d31fc00aSDong Aisheng 
493d31fc00aSDong Aisheng 	dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
494e76b8559SDong Aisheng 		clock, host->max_clk / pre_div / div);
495bd455029SJoakim Tjernlund 	host->mmc->actual_clock = host->max_clk / pre_div / div;
496d31fc00aSDong Aisheng 	pre_div >>= 1;
497d31fc00aSDong Aisheng 	div--;
498d31fc00aSDong Aisheng 
499d31fc00aSDong Aisheng 	temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
500d31fc00aSDong Aisheng 	temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
501d31fc00aSDong Aisheng 		| (div << ESDHC_DIVIDER_SHIFT)
502d31fc00aSDong Aisheng 		| (pre_div << ESDHC_PREDIV_SHIFT));
503d31fc00aSDong Aisheng 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
504e87d2db2Syangbo lu 
505e87d2db2Syangbo lu 	/* Wait max 20 ms */
506e145ac45Syangbo lu 	timeout = ktime_add_ms(ktime_get(), 20);
507e87d2db2Syangbo lu 	while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) {
508e145ac45Syangbo lu 		if (ktime_after(ktime_get(), timeout)) {
509e87d2db2Syangbo lu 			pr_err("%s: Internal clock never stabilised.\n",
510e87d2db2Syangbo lu 				mmc_hostname(host->mmc));
511e87d2db2Syangbo lu 			return;
512e87d2db2Syangbo lu 		}
513e145ac45Syangbo lu 		udelay(10);
514f060bc9cSJerry Huang 	}
515f060bc9cSJerry Huang 
516e87d2db2Syangbo lu 	temp |= ESDHC_CLOCK_SDCLKEN;
517e87d2db2Syangbo lu 	sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
518e87d2db2Syangbo lu }
519e87d2db2Syangbo lu 
5202317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
52166b50a00SOded Gabbay {
52266b50a00SOded Gabbay 	u32 ctrl;
52366b50a00SOded Gabbay 
524f4932cfdSyangbo lu 	ctrl = sdhci_readl(host, ESDHC_PROCTL);
525f4932cfdSyangbo lu 	ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
52666b50a00SOded Gabbay 	switch (width) {
52766b50a00SOded Gabbay 	case MMC_BUS_WIDTH_8:
528f4932cfdSyangbo lu 		ctrl |= ESDHC_CTRL_8BITBUS;
52966b50a00SOded Gabbay 		break;
53066b50a00SOded Gabbay 
53166b50a00SOded Gabbay 	case MMC_BUS_WIDTH_4:
532f4932cfdSyangbo lu 		ctrl |= ESDHC_CTRL_4BITBUS;
53366b50a00SOded Gabbay 		break;
53466b50a00SOded Gabbay 
53566b50a00SOded Gabbay 	default:
53666b50a00SOded Gabbay 		break;
53766b50a00SOded Gabbay 	}
53866b50a00SOded Gabbay 
539f4932cfdSyangbo lu 	sdhci_writel(host, ctrl, ESDHC_PROCTL);
54066b50a00SOded Gabbay }
54166b50a00SOded Gabbay 
54219c3a0efSyangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
54319c3a0efSyangbo lu {
54419c3a0efSyangbo lu 	u32 val;
54519c3a0efSyangbo lu 	ktime_t timeout;
54619c3a0efSyangbo lu 
54719c3a0efSyangbo lu 	val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
54819c3a0efSyangbo lu 
54919c3a0efSyangbo lu 	if (enable)
55019c3a0efSyangbo lu 		val |= ESDHC_CLOCK_SDCLKEN;
55119c3a0efSyangbo lu 	else
55219c3a0efSyangbo lu 		val &= ~ESDHC_CLOCK_SDCLKEN;
55319c3a0efSyangbo lu 
55419c3a0efSyangbo lu 	sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
55519c3a0efSyangbo lu 
55619c3a0efSyangbo lu 	/* Wait max 20 ms */
55719c3a0efSyangbo lu 	timeout = ktime_add_ms(ktime_get(), 20);
55819c3a0efSyangbo lu 	val = ESDHC_CLOCK_STABLE;
55919c3a0efSyangbo lu 	while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) {
56019c3a0efSyangbo lu 		if (ktime_after(ktime_get(), timeout)) {
56119c3a0efSyangbo lu 			pr_err("%s: Internal clock never stabilised.\n",
56219c3a0efSyangbo lu 				mmc_hostname(host->mmc));
56319c3a0efSyangbo lu 			break;
56419c3a0efSyangbo lu 		}
56519c3a0efSyangbo lu 		udelay(10);
56619c3a0efSyangbo lu 	}
56719c3a0efSyangbo lu }
56819c3a0efSyangbo lu 
569304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask)
570304f0a98SAlessio Igor Bogani {
571304f0a98SAlessio Igor Bogani 	sdhci_reset(host, mask);
572304f0a98SAlessio Igor Bogani 
573304f0a98SAlessio Igor Bogani 	sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
574304f0a98SAlessio Igor Bogani 	sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
575304f0a98SAlessio Igor Bogani }
576304f0a98SAlessio Igor Bogani 
577ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific
578ea35645aSyangbo lu  * configuration and status registers for the device. There is a
579ea35645aSyangbo lu  * SDHC IO VSEL control register on SCFG for some platforms. It's
580ea35645aSyangbo lu  * used to support SDHC IO voltage switching.
581ea35645aSyangbo lu  */
582ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = {
583ea35645aSyangbo lu 	{ .compatible = "fsl,t1040-scfg", },
584ea35645aSyangbo lu 	{ .compatible = "fsl,ls1012a-scfg", },
585ea35645aSyangbo lu 	{ .compatible = "fsl,ls1046a-scfg", },
586ea35645aSyangbo lu 	{}
587ea35645aSyangbo lu };
588ea35645aSyangbo lu 
589ea35645aSyangbo lu /* SDHC IO VSEL control register definition */
590ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR	0x408
591ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN	0x80000000
592ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL	0x60000000
593ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS	0x00000001
594ea35645aSyangbo lu 
595ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
596ea35645aSyangbo lu 				       struct mmc_ios *ios)
597ea35645aSyangbo lu {
598ea35645aSyangbo lu 	struct sdhci_host *host = mmc_priv(mmc);
599ea35645aSyangbo lu 	struct device_node *scfg_node;
600ea35645aSyangbo lu 	void __iomem *scfg_base = NULL;
601ea35645aSyangbo lu 	u32 sdhciovselcr;
602ea35645aSyangbo lu 	u32 val;
603ea35645aSyangbo lu 
604ea35645aSyangbo lu 	/*
605ea35645aSyangbo lu 	 * Signal Voltage Switching is only applicable for Host Controllers
606ea35645aSyangbo lu 	 * v3.00 and above.
607ea35645aSyangbo lu 	 */
608ea35645aSyangbo lu 	if (host->version < SDHCI_SPEC_300)
609ea35645aSyangbo lu 		return 0;
610ea35645aSyangbo lu 
611ea35645aSyangbo lu 	val = sdhci_readl(host, ESDHC_PROCTL);
612ea35645aSyangbo lu 
613ea35645aSyangbo lu 	switch (ios->signal_voltage) {
614ea35645aSyangbo lu 	case MMC_SIGNAL_VOLTAGE_330:
615ea35645aSyangbo lu 		val &= ~ESDHC_VOLT_SEL;
616ea35645aSyangbo lu 		sdhci_writel(host, val, ESDHC_PROCTL);
617ea35645aSyangbo lu 		return 0;
618ea35645aSyangbo lu 	case MMC_SIGNAL_VOLTAGE_180:
619ea35645aSyangbo lu 		scfg_node = of_find_matching_node(NULL, scfg_device_ids);
620ea35645aSyangbo lu 		if (scfg_node)
621ea35645aSyangbo lu 			scfg_base = of_iomap(scfg_node, 0);
622ea35645aSyangbo lu 		if (scfg_base) {
623ea35645aSyangbo lu 			sdhciovselcr = SDHCIOVSELCR_TGLEN |
624ea35645aSyangbo lu 				       SDHCIOVSELCR_VSELVAL;
625ea35645aSyangbo lu 			iowrite32be(sdhciovselcr,
626ea35645aSyangbo lu 				scfg_base + SCFG_SDHCIOVSELCR);
627ea35645aSyangbo lu 
628ea35645aSyangbo lu 			val |= ESDHC_VOLT_SEL;
629ea35645aSyangbo lu 			sdhci_writel(host, val, ESDHC_PROCTL);
630ea35645aSyangbo lu 			mdelay(5);
631ea35645aSyangbo lu 
632ea35645aSyangbo lu 			sdhciovselcr = SDHCIOVSELCR_TGLEN |
633ea35645aSyangbo lu 				       SDHCIOVSELCR_SDHC_VS;
634ea35645aSyangbo lu 			iowrite32be(sdhciovselcr,
635ea35645aSyangbo lu 				scfg_base + SCFG_SDHCIOVSELCR);
636ea35645aSyangbo lu 			iounmap(scfg_base);
637ea35645aSyangbo lu 		} else {
638ea35645aSyangbo lu 			val |= ESDHC_VOLT_SEL;
639ea35645aSyangbo lu 			sdhci_writel(host, val, ESDHC_PROCTL);
640ea35645aSyangbo lu 		}
641ea35645aSyangbo lu 		return 0;
642ea35645aSyangbo lu 	default:
643ea35645aSyangbo lu 		return 0;
644ea35645aSyangbo lu 	}
645ea35645aSyangbo lu }
646ea35645aSyangbo lu 
647ba49cbd0Syangbo lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
648ba49cbd0Syangbo lu {
649ba49cbd0Syangbo lu 	struct sdhci_host *host = mmc_priv(mmc);
650ba49cbd0Syangbo lu 	u32 val;
651ba49cbd0Syangbo lu 
652ba49cbd0Syangbo lu 	/* Use tuning block for tuning procedure */
653ba49cbd0Syangbo lu 	esdhc_clock_enable(host, false);
654ba49cbd0Syangbo lu 	val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
655ba49cbd0Syangbo lu 	val |= ESDHC_FLUSH_ASYNC_FIFO;
656ba49cbd0Syangbo lu 	sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
657ba49cbd0Syangbo lu 
658ba49cbd0Syangbo lu 	val = sdhci_readl(host, ESDHC_TBCTL);
659ba49cbd0Syangbo lu 	val |= ESDHC_TB_EN;
660ba49cbd0Syangbo lu 	sdhci_writel(host, val, ESDHC_TBCTL);
661ba49cbd0Syangbo lu 	esdhc_clock_enable(host, true);
662ba49cbd0Syangbo lu 
663ba49cbd0Syangbo lu 	return sdhci_execute_tuning(mmc, opcode);
664ba49cbd0Syangbo lu }
665ba49cbd0Syangbo lu 
6669e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP
667723f7924SRussell King static u32 esdhc_proctl;
668723f7924SRussell King static int esdhc_of_suspend(struct device *dev)
669723f7924SRussell King {
670723f7924SRussell King 	struct sdhci_host *host = dev_get_drvdata(dev);
671723f7924SRussell King 
672f4932cfdSyangbo lu 	esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
673723f7924SRussell King 
674d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
675d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
676d38dcad4SAdrian Hunter 
677723f7924SRussell King 	return sdhci_suspend_host(host);
678723f7924SRussell King }
679723f7924SRussell King 
68006732b84SUlf Hansson static int esdhc_of_resume(struct device *dev)
681723f7924SRussell King {
682723f7924SRussell King 	struct sdhci_host *host = dev_get_drvdata(dev);
683723f7924SRussell King 	int ret = sdhci_resume_host(host);
684723f7924SRussell King 
685723f7924SRussell King 	if (ret == 0) {
686723f7924SRussell King 		/* Isn't this already done by sdhci_resume_host() ? --rmk */
687723f7924SRussell King 		esdhc_of_enable_dma(host);
688f4932cfdSyangbo lu 		sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
689723f7924SRussell King 	}
690723f7924SRussell King 	return ret;
691723f7924SRussell King }
692723f7924SRussell King #endif
693723f7924SRussell King 
6949e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
6959e48b336SUlf Hansson 			esdhc_of_suspend,
6969e48b336SUlf Hansson 			esdhc_of_resume);
6979e48b336SUlf Hansson 
698f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = {
699f4932cfdSyangbo lu 	.read_l = esdhc_be_readl,
700f4932cfdSyangbo lu 	.read_w = esdhc_be_readw,
701f4932cfdSyangbo lu 	.read_b = esdhc_be_readb,
702f4932cfdSyangbo lu 	.write_l = esdhc_be_writel,
703f4932cfdSyangbo lu 	.write_w = esdhc_be_writew,
704f4932cfdSyangbo lu 	.write_b = esdhc_be_writeb,
705f4932cfdSyangbo lu 	.set_clock = esdhc_of_set_clock,
706f4932cfdSyangbo lu 	.enable_dma = esdhc_of_enable_dma,
707f4932cfdSyangbo lu 	.get_max_clock = esdhc_of_get_max_clock,
708f4932cfdSyangbo lu 	.get_min_clock = esdhc_of_get_min_clock,
709f4932cfdSyangbo lu 	.adma_workaround = esdhc_of_adma_workaround,
710f4932cfdSyangbo lu 	.set_bus_width = esdhc_pltfm_set_bus_width,
711f4932cfdSyangbo lu 	.reset = esdhc_reset,
712f4932cfdSyangbo lu 	.set_uhs_signaling = sdhci_set_uhs_signaling,
713f4932cfdSyangbo lu };
714f4932cfdSyangbo lu 
715f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = {
716f4932cfdSyangbo lu 	.read_l = esdhc_le_readl,
717f4932cfdSyangbo lu 	.read_w = esdhc_le_readw,
718f4932cfdSyangbo lu 	.read_b = esdhc_le_readb,
719f4932cfdSyangbo lu 	.write_l = esdhc_le_writel,
720f4932cfdSyangbo lu 	.write_w = esdhc_le_writew,
721f4932cfdSyangbo lu 	.write_b = esdhc_le_writeb,
722f4932cfdSyangbo lu 	.set_clock = esdhc_of_set_clock,
723f4932cfdSyangbo lu 	.enable_dma = esdhc_of_enable_dma,
724f4932cfdSyangbo lu 	.get_max_clock = esdhc_of_get_max_clock,
725f4932cfdSyangbo lu 	.get_min_clock = esdhc_of_get_min_clock,
726f4932cfdSyangbo lu 	.adma_workaround = esdhc_of_adma_workaround,
727f4932cfdSyangbo lu 	.set_bus_width = esdhc_pltfm_set_bus_width,
728f4932cfdSyangbo lu 	.reset = esdhc_reset,
729f4932cfdSyangbo lu 	.set_uhs_signaling = sdhci_set_uhs_signaling,
730f4932cfdSyangbo lu };
731f4932cfdSyangbo lu 
732f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
733e9acc77dSyangbo lu 	.quirks = ESDHC_DEFAULT_QUIRKS |
734e9acc77dSyangbo lu #ifdef CONFIG_PPC
735e9acc77dSyangbo lu 		  SDHCI_QUIRK_BROKEN_CARD_DETECTION |
736e9acc77dSyangbo lu #endif
737e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_CARD_NO_RESET |
738e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
739f4932cfdSyangbo lu 	.ops = &sdhci_esdhc_be_ops,
7407657c3a7SAlbert Herranz };
74138576af1SShawn Guo 
742f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
743e9acc77dSyangbo lu 	.quirks = ESDHC_DEFAULT_QUIRKS |
744e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_CARD_NO_RESET |
745e9acc77dSyangbo lu 		  SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
746f4932cfdSyangbo lu 	.ops = &sdhci_esdhc_le_ops,
747f4932cfdSyangbo lu };
748f4932cfdSyangbo lu 
749151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = {
750151ede40Syangbo lu 	{ .family = "QorIQ T4240", .revision = "1.0", },
751151ede40Syangbo lu 	{ .family = "QorIQ T4240", .revision = "2.0", },
752151ede40Syangbo lu 	{ },
753151ede40Syangbo lu };
754151ede40Syangbo lu 
755f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
756f4932cfdSyangbo lu {
757f4932cfdSyangbo lu 	struct sdhci_pltfm_host *pltfm_host;
758f4932cfdSyangbo lu 	struct sdhci_esdhc *esdhc;
75919c3a0efSyangbo lu 	struct device_node *np;
76019c3a0efSyangbo lu 	struct clk *clk;
76119c3a0efSyangbo lu 	u32 val;
762f4932cfdSyangbo lu 	u16 host_ver;
763f4932cfdSyangbo lu 
764f4932cfdSyangbo lu 	pltfm_host = sdhci_priv(host);
7658605e7aeSJisheng Zhang 	esdhc = sdhci_pltfm_priv(pltfm_host);
766f4932cfdSyangbo lu 
767f4932cfdSyangbo lu 	host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
768f4932cfdSyangbo lu 	esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
769f4932cfdSyangbo lu 			     SDHCI_VENDOR_VER_SHIFT;
770f4932cfdSyangbo lu 	esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
771151ede40Syangbo lu 	if (soc_device_match(soc_incorrect_hostver))
772151ede40Syangbo lu 		esdhc->quirk_incorrect_hostver = true;
773151ede40Syangbo lu 	else
774151ede40Syangbo lu 		esdhc->quirk_incorrect_hostver = false;
77519c3a0efSyangbo lu 
77619c3a0efSyangbo lu 	np = pdev->dev.of_node;
77719c3a0efSyangbo lu 	clk = of_clk_get(np, 0);
77819c3a0efSyangbo lu 	if (!IS_ERR(clk)) {
77919c3a0efSyangbo lu 		/*
78019c3a0efSyangbo lu 		 * esdhc->peripheral_clock would be assigned with a value
78119c3a0efSyangbo lu 		 * which is eSDHC base clock when use periperal clock.
78219c3a0efSyangbo lu 		 * For ls1046a, the clock value got by common clk API is
78319c3a0efSyangbo lu 		 * peripheral clock while the eSDHC base clock is 1/2
78419c3a0efSyangbo lu 		 * peripheral clock.
78519c3a0efSyangbo lu 		 */
78619c3a0efSyangbo lu 		if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
78719c3a0efSyangbo lu 			esdhc->peripheral_clock = clk_get_rate(clk) / 2;
78819c3a0efSyangbo lu 		else
78919c3a0efSyangbo lu 			esdhc->peripheral_clock = clk_get_rate(clk);
79019c3a0efSyangbo lu 
79119c3a0efSyangbo lu 		clk_put(clk);
79219c3a0efSyangbo lu 	}
79319c3a0efSyangbo lu 
79419c3a0efSyangbo lu 	if (esdhc->peripheral_clock) {
79519c3a0efSyangbo lu 		esdhc_clock_enable(host, false);
79619c3a0efSyangbo lu 		val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
79719c3a0efSyangbo lu 		val |= ESDHC_PERIPHERAL_CLK_SEL;
79819c3a0efSyangbo lu 		sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
79919c3a0efSyangbo lu 		esdhc_clock_enable(host, true);
80019c3a0efSyangbo lu 	}
801f4932cfdSyangbo lu }
802f4932cfdSyangbo lu 
803c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev)
80438576af1SShawn Guo {
80566b50a00SOded Gabbay 	struct sdhci_host *host;
806dcaff04dSOded Gabbay 	struct device_node *np;
8071ef5e49eSyangbo lu 	struct sdhci_pltfm_host *pltfm_host;
8081ef5e49eSyangbo lu 	struct sdhci_esdhc *esdhc;
80966b50a00SOded Gabbay 	int ret;
81066b50a00SOded Gabbay 
811f4932cfdSyangbo lu 	np = pdev->dev.of_node;
812f4932cfdSyangbo lu 
813150d4240SJulia Lawall 	if (of_property_read_bool(np, "little-endian"))
8148605e7aeSJisheng Zhang 		host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
8158605e7aeSJisheng Zhang 					sizeof(struct sdhci_esdhc));
816f4932cfdSyangbo lu 	else
8178605e7aeSJisheng Zhang 		host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
8188605e7aeSJisheng Zhang 					sizeof(struct sdhci_esdhc));
819f4932cfdSyangbo lu 
82066b50a00SOded Gabbay 	if (IS_ERR(host))
82166b50a00SOded Gabbay 		return PTR_ERR(host);
82266b50a00SOded Gabbay 
823ea35645aSyangbo lu 	host->mmc_host_ops.start_signal_voltage_switch =
824ea35645aSyangbo lu 		esdhc_signal_voltage_switch;
825ba49cbd0Syangbo lu 	host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
8266b236f37Syangbo lu 	host->tuning_delay = 1;
827ea35645aSyangbo lu 
828f4932cfdSyangbo lu 	esdhc_init(pdev, host);
829f4932cfdSyangbo lu 
83066b50a00SOded Gabbay 	sdhci_get_of_property(pdev);
83166b50a00SOded Gabbay 
8321ef5e49eSyangbo lu 	pltfm_host = sdhci_priv(host);
8338605e7aeSJisheng Zhang 	esdhc = sdhci_pltfm_priv(pltfm_host);
8341ef5e49eSyangbo lu 	if (esdhc->vendor_ver == VENDOR_V_22)
8351ef5e49eSyangbo lu 		host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
8361ef5e49eSyangbo lu 
8371ef5e49eSyangbo lu 	if (esdhc->vendor_ver > VENDOR_V_22)
8381ef5e49eSyangbo lu 		host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
8391ef5e49eSyangbo lu 
84074fd5e30SYangbo Lu 	if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
84174fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p5020-esdhc") ||
84274fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p4080-esdhc") ||
84374fd5e30SYangbo Lu 	    of_device_is_compatible(np, "fsl,p1020-esdhc") ||
844e9acc77dSyangbo lu 	    of_device_is_compatible(np, "fsl,t1040-esdhc"))
84574fd5e30SYangbo Lu 		host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
84674fd5e30SYangbo Lu 
847a22950c8Syangbo lu 	if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
848a22950c8Syangbo lu 		host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
849a22950c8Syangbo lu 
850dcaff04dSOded Gabbay 	if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
851dcaff04dSOded Gabbay 		/*
852dcaff04dSOded Gabbay 		 * Freescale messed up with P2020 as it has a non-standard
853dcaff04dSOded Gabbay 		 * host control register
854dcaff04dSOded Gabbay 		 */
855dcaff04dSOded Gabbay 		host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
856dcaff04dSOded Gabbay 	}
857dcaff04dSOded Gabbay 
85866b50a00SOded Gabbay 	/* call to generic mmc_of_parse to support additional capabilities */
859f0991408SUlf Hansson 	ret = mmc_of_parse(host->mmc);
860f0991408SUlf Hansson 	if (ret)
861f0991408SUlf Hansson 		goto err;
862f0991408SUlf Hansson 
863490104acSHaijun Zhang 	mmc_of_parse_voltage(np, &host->ocr_mask);
86466b50a00SOded Gabbay 
86566b50a00SOded Gabbay 	ret = sdhci_add_host(host);
86666b50a00SOded Gabbay 	if (ret)
867f0991408SUlf Hansson 		goto err;
86866b50a00SOded Gabbay 
869f0991408SUlf Hansson 	return 0;
870f0991408SUlf Hansson  err:
871f0991408SUlf Hansson 	sdhci_pltfm_free(pdev);
87266b50a00SOded Gabbay 	return ret;
87338576af1SShawn Guo }
87438576af1SShawn Guo 
87538576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = {
87638576af1SShawn Guo 	{ .compatible = "fsl,mpc8379-esdhc" },
87738576af1SShawn Guo 	{ .compatible = "fsl,mpc8536-esdhc" },
87838576af1SShawn Guo 	{ .compatible = "fsl,esdhc" },
87938576af1SShawn Guo 	{ }
88038576af1SShawn Guo };
88138576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
88238576af1SShawn Guo 
88338576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = {
88438576af1SShawn Guo 	.driver = {
88538576af1SShawn Guo 		.name = "sdhci-esdhc",
88638576af1SShawn Guo 		.of_match_table = sdhci_esdhc_of_match,
8879e48b336SUlf Hansson 		.pm = &esdhc_of_dev_pm_ops,
88838576af1SShawn Guo 	},
88938576af1SShawn Guo 	.probe = sdhci_esdhc_probe,
890caebcae9SKevin Hao 	.remove = sdhci_pltfm_unregister,
89138576af1SShawn Guo };
89238576af1SShawn Guo 
893d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver);
89438576af1SShawn Guo 
89538576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
89638576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
89738576af1SShawn Guo 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
89838576af1SShawn Guo MODULE_LICENSE("GPL v2");
899