blob: d3aa67142839b2e36e654febd911d8e9eee129b6 [file] [log] [blame]
Albert Herranz7657c3a2009-12-17 15:27:20 -08001/*
2 * Freescale eSDHC controller driver.
3 *
Jerry Huangf060bc92012-02-14 14:05:37 +08004 * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
Albert Herranz7657c3a2009-12-17 15:27:20 -08005 * Copyright (c) 2009 MontaVista Software, Inc.
6 *
7 * Authors: Xiaobo Xie <X.Xie@freescale.com>
8 * Anton Vorontsov <avorontsov@ru.mvista.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 */
15
Oded Gabbay66b50a02013-06-27 12:00:05 -040016#include <linux/err.h>
Albert Herranz7657c3a2009-12-17 15:27:20 -080017#include <linux/io.h>
Jerry Huangf060bc92012-02-14 14:05:37 +080018#include <linux/of.h>
Albert Herranz7657c3a2009-12-17 15:27:20 -080019#include <linux/delay.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040020#include <linux/module.h>
yangbo lu151ede42016-11-09 11:14:12 +080021#include <linux/sys_soc.h>
Albert Herranz7657c3a2009-12-17 15:27:20 -080022#include <linux/mmc/host.h>
Shawn Guo38576af2011-05-27 23:48:14 +080023#include "sdhci-pltfm.h"
Wolfram Sang80872e22010-10-15 12:21:03 +020024#include "sdhci-esdhc.h"
Albert Herranz7657c3a2009-12-17 15:27:20 -080025
Jerry Huang137ccd42012-03-08 11:25:02 +080026#define VENDOR_V_22 0x12
Haijun Zhanga4071fb2012-12-04 10:41:28 +080027#define VENDOR_V_23 0x13
yangbo luf4932cf2015-10-08 18:36:36 +080028
29struct sdhci_esdhc {
30 u8 vendor_ver;
31 u8 spec_ver;
yangbo lu151ede42016-11-09 11:14:12 +080032 bool quirk_incorrect_hostver;
yangbo luf4932cf2015-10-08 18:36:36 +080033};
34
35/**
36 * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
37 * to make it compatible with SD spec.
38 *
39 * @host: pointer to sdhci_host
40 * @spec_reg: SD spec register address
41 * @value: 32bit eSDHC register value on spec_reg address
42 *
43 * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
44 * registers are 32 bits. There are differences in register size, register
45 * address, register function, bit position and function between eSDHC spec
46 * and SD spec.
47 *
48 * Return a fixed up register value
49 */
50static u32 esdhc_readl_fixup(struct sdhci_host *host,
51 int spec_reg, u32 value)
Jerry Huang137ccd42012-03-08 11:25:02 +080052{
yangbo luf4932cf2015-10-08 18:36:36 +080053 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang8605e7a2016-02-16 21:08:26 +080054 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
Jerry Huang137ccd42012-03-08 11:25:02 +080055 u32 ret;
56
Jerry Huang137ccd42012-03-08 11:25:02 +080057 /*
58 * The bit of ADMA flag in eSDHC is not compatible with standard
59 * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
60 * supported by eSDHC.
61 * And for many FSL eSDHC controller, the reset value of field
yangbo luf4932cf2015-10-08 18:36:36 +080062 * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
Jerry Huang137ccd42012-03-08 11:25:02 +080063 * only these vendor version is greater than 2.2/0x12 support ADMA.
Jerry Huang137ccd42012-03-08 11:25:02 +080064 */
yangbo luf4932cf2015-10-08 18:36:36 +080065 if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
66 if (esdhc->vendor_ver > VENDOR_V_22) {
67 ret = value | SDHCI_CAN_DO_ADMA2;
68 return ret;
69 }
Jerry Huang137ccd42012-03-08 11:25:02 +080070 }
Michael Walleb0921d52016-11-15 11:13:16 +010071 /*
72 * The DAT[3:0] line signal levels and the CMD line signal level are
73 * not compatible with standard SDHC register. The line signal levels
74 * DAT[7:0] are at bits 31:24 and the command line signal level is at
75 * bit 23. All other bits are the same as in the standard SDHC
76 * register.
77 */
78 if (spec_reg == SDHCI_PRESENT_STATE) {
79 ret = value & 0x000fffff;
80 ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
81 ret |= (value << 1) & SDHCI_CMD_LVL;
82 return ret;
83 }
84
yangbo luf4932cf2015-10-08 18:36:36 +080085 ret = value;
Jerry Huang137ccd42012-03-08 11:25:02 +080086 return ret;
87}
88
yangbo luf4932cf2015-10-08 18:36:36 +080089static u16 esdhc_readw_fixup(struct sdhci_host *host,
90 int spec_reg, u32 value)
Albert Herranz7657c3a2009-12-17 15:27:20 -080091{
yangbo lu151ede42016-11-09 11:14:12 +080092 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
93 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
Albert Herranz7657c3a2009-12-17 15:27:20 -080094 u16 ret;
yangbo luf4932cf2015-10-08 18:36:36 +080095 int shift = (spec_reg & 0x2) * 8;
Albert Herranz7657c3a2009-12-17 15:27:20 -080096
yangbo luf4932cf2015-10-08 18:36:36 +080097 if (spec_reg == SDHCI_HOST_VERSION)
98 ret = value & 0xffff;
Albert Herranz7657c3a2009-12-17 15:27:20 -080099 else
yangbo luf4932cf2015-10-08 18:36:36 +0800100 ret = (value >> shift) & 0xffff;
yangbo lu151ede42016-11-09 11:14:12 +0800101 /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
102 * vendor version and spec version information.
103 */
104 if ((spec_reg == SDHCI_HOST_VERSION) &&
105 (esdhc->quirk_incorrect_hostver))
106 ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
Xu leie51cbc92011-09-09 20:05:46 +0800107 return ret;
108}
109
yangbo luf4932cf2015-10-08 18:36:36 +0800110static u8 esdhc_readb_fixup(struct sdhci_host *host,
111 int spec_reg, u32 value)
Xu leie51cbc92011-09-09 20:05:46 +0800112{
yangbo luf4932cf2015-10-08 18:36:36 +0800113 u8 ret;
114 u8 dma_bits;
115 int shift = (spec_reg & 0x3) * 8;
116
117 ret = (value >> shift) & 0xff;
Roy Zangba8c4dc2012-01-13 15:02:01 +0800118
119 /*
120 * "DMA select" locates at offset 0x28 in SD specification, but on
121 * P5020 or P3041, it locates at 0x29.
122 */
yangbo luf4932cf2015-10-08 18:36:36 +0800123 if (spec_reg == SDHCI_HOST_CONTROL) {
Roy Zangba8c4dc2012-01-13 15:02:01 +0800124 /* DMA select is 22,23 bits in Protocol Control Register */
yangbo luf4932cf2015-10-08 18:36:36 +0800125 dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
Roy Zangba8c4dc2012-01-13 15:02:01 +0800126 /* fixup the result */
127 ret &= ~SDHCI_CTRL_DMA_MASK;
128 ret |= dma_bits;
129 }
yangbo luf4932cf2015-10-08 18:36:36 +0800130 return ret;
131}
132
133/**
134 * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
135 * written into eSDHC register.
136 *
137 * @host: pointer to sdhci_host
138 * @spec_reg: SD spec register address
139 * @value: 8/16/32bit SD spec register value that would be written
140 * @old_value: 32bit eSDHC register value on spec_reg address
141 *
142 * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
143 * registers are 32 bits. There are differences in register size, register
144 * address, register function, bit position and function between eSDHC spec
145 * and SD spec.
146 *
147 * Return a fixed up register value
148 */
149static u32 esdhc_writel_fixup(struct sdhci_host *host,
150 int spec_reg, u32 value, u32 old_value)
151{
152 u32 ret;
153
154 /*
155 * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
156 * when SYSCTL[RSTD] is set for some special operations.
157 * No any impact on other operation.
158 */
159 if (spec_reg == SDHCI_INT_ENABLE)
160 ret = value | SDHCI_INT_BLK_GAP;
161 else
162 ret = value;
Roy Zangba8c4dc2012-01-13 15:02:01 +0800163
Albert Herranz7657c3a2009-12-17 15:27:20 -0800164 return ret;
165}
166
yangbo luf4932cf2015-10-08 18:36:36 +0800167static u32 esdhc_writew_fixup(struct sdhci_host *host,
168 int spec_reg, u16 value, u32 old_value)
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800169{
yangbo luf4932cf2015-10-08 18:36:36 +0800170 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
171 int shift = (spec_reg & 0x2) * 8;
172 u32 ret;
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800173
yangbo luf4932cf2015-10-08 18:36:36 +0800174 switch (spec_reg) {
175 case SDHCI_TRANSFER_MODE:
176 /*
177 * Postpone this write, we must do it together with a
178 * command write that is down below. Return old value.
179 */
180 pltfm_host->xfer_mode_shadow = value;
181 return old_value;
182 case SDHCI_COMMAND:
183 ret = (value << 16) | pltfm_host->xfer_mode_shadow;
184 return ret;
185 }
186
187 ret = old_value & (~(0xffff << shift));
188 ret |= (value << shift);
189
190 if (spec_reg == SDHCI_BLOCK_SIZE) {
Albert Herranz7657c3a2009-12-17 15:27:20 -0800191 /*
192 * Two last DMA bits are reserved, and first one is used for
193 * non-standard blksz of 4096 bytes that we don't support
194 * yet. So clear the DMA boundary bits.
195 */
yangbo luf4932cf2015-10-08 18:36:36 +0800196 ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
Albert Herranz7657c3a2009-12-17 15:27:20 -0800197 }
yangbo luf4932cf2015-10-08 18:36:36 +0800198 return ret;
Albert Herranz7657c3a2009-12-17 15:27:20 -0800199}
200
yangbo luf4932cf2015-10-08 18:36:36 +0800201static u32 esdhc_writeb_fixup(struct sdhci_host *host,
202 int spec_reg, u8 value, u32 old_value)
Albert Herranz7657c3a2009-12-17 15:27:20 -0800203{
yangbo luf4932cf2015-10-08 18:36:36 +0800204 u32 ret;
205 u32 dma_bits;
206 u8 tmp;
207 int shift = (spec_reg & 0x3) * 8;
208
Roy Zangba8c4dc2012-01-13 15:02:01 +0800209 /*
yangbo lu9e4703d2015-10-16 15:44:03 +0800210 * eSDHC doesn't have a standard power control register, so we do
211 * nothing here to avoid incorrect operation.
212 */
213 if (spec_reg == SDHCI_POWER_CONTROL)
214 return old_value;
215 /*
Roy Zangba8c4dc2012-01-13 15:02:01 +0800216 * "DMA select" location is offset 0x28 in SD specification, but on
217 * P5020 or P3041, it's located at 0x29.
218 */
yangbo luf4932cf2015-10-08 18:36:36 +0800219 if (spec_reg == SDHCI_HOST_CONTROL) {
Oded Gabbaydcaff042013-07-05 12:48:35 -0400220 /*
221 * If host control register is not standard, exit
222 * this function
223 */
224 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
yangbo luf4932cf2015-10-08 18:36:36 +0800225 return old_value;
Oded Gabbaydcaff042013-07-05 12:48:35 -0400226
Roy Zangba8c4dc2012-01-13 15:02:01 +0800227 /* DMA select is 22,23 bits in Protocol Control Register */
yangbo luf4932cf2015-10-08 18:36:36 +0800228 dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
229 ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
230 tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
231 (old_value & SDHCI_CTRL_DMA_MASK);
232 ret = (ret & (~0xff)) | tmp;
233
234 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
235 ret &= ~ESDHC_HOST_CONTROL_RES;
236 return ret;
Roy Zangba8c4dc2012-01-13 15:02:01 +0800237 }
238
yangbo luf4932cf2015-10-08 18:36:36 +0800239 ret = (old_value & (~(0xff << shift))) | (value << shift);
240 return ret;
241}
242
243static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
244{
245 u32 ret;
246 u32 value;
247
248 value = ioread32be(host->ioaddr + reg);
249 ret = esdhc_readl_fixup(host, reg, value);
250
251 return ret;
252}
253
254static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
255{
256 u32 ret;
257 u32 value;
258
259 value = ioread32(host->ioaddr + reg);
260 ret = esdhc_readl_fixup(host, reg, value);
261
262 return ret;
263}
264
265static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
266{
267 u16 ret;
268 u32 value;
269 int base = reg & ~0x3;
270
271 value = ioread32be(host->ioaddr + base);
272 ret = esdhc_readw_fixup(host, reg, value);
273 return ret;
274}
275
276static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
277{
278 u16 ret;
279 u32 value;
280 int base = reg & ~0x3;
281
282 value = ioread32(host->ioaddr + base);
283 ret = esdhc_readw_fixup(host, reg, value);
284 return ret;
285}
286
287static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
288{
289 u8 ret;
290 u32 value;
291 int base = reg & ~0x3;
292
293 value = ioread32be(host->ioaddr + base);
294 ret = esdhc_readb_fixup(host, reg, value);
295 return ret;
296}
297
298static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
299{
300 u8 ret;
301 u32 value;
302 int base = reg & ~0x3;
303
304 value = ioread32(host->ioaddr + base);
305 ret = esdhc_readb_fixup(host, reg, value);
306 return ret;
307}
308
309static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
310{
311 u32 value;
312
313 value = esdhc_writel_fixup(host, reg, val, 0);
314 iowrite32be(value, host->ioaddr + reg);
315}
316
317static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
318{
319 u32 value;
320
321 value = esdhc_writel_fixup(host, reg, val, 0);
322 iowrite32(value, host->ioaddr + reg);
323}
324
325static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
326{
327 int base = reg & ~0x3;
328 u32 value;
329 u32 ret;
330
331 value = ioread32be(host->ioaddr + base);
332 ret = esdhc_writew_fixup(host, reg, val, value);
333 if (reg != SDHCI_TRANSFER_MODE)
334 iowrite32be(ret, host->ioaddr + base);
335}
336
337static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
338{
339 int base = reg & ~0x3;
340 u32 value;
341 u32 ret;
342
343 value = ioread32(host->ioaddr + base);
344 ret = esdhc_writew_fixup(host, reg, val, value);
345 if (reg != SDHCI_TRANSFER_MODE)
346 iowrite32(ret, host->ioaddr + base);
347}
348
349static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
350{
351 int base = reg & ~0x3;
352 u32 value;
353 u32 ret;
354
355 value = ioread32be(host->ioaddr + base);
356 ret = esdhc_writeb_fixup(host, reg, val, value);
357 iowrite32be(ret, host->ioaddr + base);
358}
359
360static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
361{
362 int base = reg & ~0x3;
363 u32 value;
364 u32 ret;
365
366 value = ioread32(host->ioaddr + base);
367 ret = esdhc_writeb_fixup(host, reg, val, value);
368 iowrite32(ret, host->ioaddr + base);
Albert Herranz7657c3a2009-12-17 15:27:20 -0800369}
370
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800371/*
372 * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
373 * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
374 * and Block Gap Event(IRQSTAT[BGE]) are also set.
375 * For Continue, apply soft reset for data(SYSCTL[RSTD]);
376 * and re-issue the entire read transaction from beginning.
377 */
yangbo luf4932cf2015-10-08 18:36:36 +0800378static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800379{
yangbo luf4932cf2015-10-08 18:36:36 +0800380 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang8605e7a2016-02-16 21:08:26 +0800381 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800382 bool applicable;
383 dma_addr_t dmastart;
384 dma_addr_t dmanow;
385
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800386 applicable = (intmask & SDHCI_INT_DATA_END) &&
yangbo luf4932cf2015-10-08 18:36:36 +0800387 (intmask & SDHCI_INT_BLK_GAP) &&
388 (esdhc->vendor_ver == VENDOR_V_23);
Haijun Zhanga4071fb2012-12-04 10:41:28 +0800389 if (!applicable)
390 return;
391
392 host->data->error = 0;
393 dmastart = sg_dma_address(host->data->sg);
394 dmanow = dmastart + host->data->bytes_xfered;
395 /*
396 * Force update to the next DMA block boundary.
397 */
398 dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
399 SDHCI_DEFAULT_BOUNDARY_SIZE;
400 host->data->bytes_xfered = dmanow - dmastart;
401 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
402}
403
Wolfram Sang80872e22010-10-15 12:21:03 +0200404static int esdhc_of_enable_dma(struct sdhci_host *host)
Albert Herranz7657c3a2009-12-17 15:27:20 -0800405{
yangbo luf4932cf2015-10-08 18:36:36 +0800406 u32 value;
407
408 value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
409 value |= ESDHC_DMA_SNOOP;
410 sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
Albert Herranz7657c3a2009-12-17 15:27:20 -0800411 return 0;
412}
413
Wolfram Sang80872e22010-10-15 12:21:03 +0200414static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
Albert Herranz7657c3a2009-12-17 15:27:20 -0800415{
Shawn Guoe3071482011-07-20 17:13:36 -0400416 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Albert Herranz7657c3a2009-12-17 15:27:20 -0800417
Shawn Guoe3071482011-07-20 17:13:36 -0400418 return pltfm_host->clock;
Albert Herranz7657c3a2009-12-17 15:27:20 -0800419}
420
Wolfram Sang80872e22010-10-15 12:21:03 +0200421static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
Albert Herranz7657c3a2009-12-17 15:27:20 -0800422{
Shawn Guoe3071482011-07-20 17:13:36 -0400423 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Albert Herranz7657c3a2009-12-17 15:27:20 -0800424
Shawn Guoe3071482011-07-20 17:13:36 -0400425 return pltfm_host->clock / 256 / 16;
Albert Herranz7657c3a2009-12-17 15:27:20 -0800426}
427
Jerry Huangf060bc92012-02-14 14:05:37 +0800428static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
429{
yangbo luf4932cf2015-10-08 18:36:36 +0800430 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang8605e7a2016-02-16 21:08:26 +0800431 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
Joakim Tjernlundbd455022015-04-20 23:12:13 +0200432 int pre_div = 1;
Dong Aishengd31fc002013-09-13 19:11:32 +0800433 int div = 1;
yangbo lue87d2db2016-12-26 17:46:30 +0800434 u32 timeout;
Dong Aishengd31fc002013-09-13 19:11:32 +0800435 u32 temp;
436
Russell King1650d0c2014-04-25 12:58:50 +0100437 host->mmc->actual_clock = 0;
438
Dong Aishengd31fc002013-09-13 19:11:32 +0800439 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +0100440 return;
Dong Aishengd31fc002013-09-13 19:11:32 +0800441
Yangbo Lu77bd2f62015-08-11 10:53:34 +0800442 /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
yangbo luf4932cf2015-10-08 18:36:36 +0800443 if (esdhc->vendor_ver < VENDOR_V_23)
Yangbo Lu77bd2f62015-08-11 10:53:34 +0800444 pre_div = 2;
445
Jerry Huangf060bc92012-02-14 14:05:37 +0800446 /* Workaround to reduce the clock frequency for p1010 esdhc */
447 if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
448 if (clock > 20000000)
449 clock -= 5000000;
450 if (clock > 40000000)
451 clock -= 5000000;
452 }
453
Dong Aishengd31fc002013-09-13 19:11:32 +0800454 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
yangbo lue87d2db2016-12-26 17:46:30 +0800455 temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
456 ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
Dong Aishengd31fc002013-09-13 19:11:32 +0800457 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
458
459 while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
460 pre_div *= 2;
461
462 while (host->max_clk / pre_div / div > clock && div < 16)
463 div++;
464
465 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
Dong Aishenge76b8552013-09-13 19:11:37 +0800466 clock, host->max_clk / pre_div / div);
Joakim Tjernlundbd455022015-04-20 23:12:13 +0200467 host->mmc->actual_clock = host->max_clk / pre_div / div;
Dong Aishengd31fc002013-09-13 19:11:32 +0800468 pre_div >>= 1;
469 div--;
470
471 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
472 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
473 | (div << ESDHC_DIVIDER_SHIFT)
474 | (pre_div << ESDHC_PREDIV_SHIFT));
475 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
yangbo lue87d2db2016-12-26 17:46:30 +0800476
477 /* Wait max 20 ms */
478 timeout = 20;
479 while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) {
480 if (timeout == 0) {
481 pr_err("%s: Internal clock never stabilised.\n",
482 mmc_hostname(host->mmc));
483 return;
484 }
485 timeout--;
486 mdelay(1);
487 }
488
489 temp |= ESDHC_CLOCK_SDCLKEN;
490 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
Jerry Huangf060bc92012-02-14 14:05:37 +0800491}
492
Russell King2317f562014-04-25 12:57:07 +0100493static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
Oded Gabbay66b50a02013-06-27 12:00:05 -0400494{
495 u32 ctrl;
496
yangbo luf4932cf2015-10-08 18:36:36 +0800497 ctrl = sdhci_readl(host, ESDHC_PROCTL);
498 ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
Oded Gabbay66b50a02013-06-27 12:00:05 -0400499 switch (width) {
500 case MMC_BUS_WIDTH_8:
yangbo luf4932cf2015-10-08 18:36:36 +0800501 ctrl |= ESDHC_CTRL_8BITBUS;
Oded Gabbay66b50a02013-06-27 12:00:05 -0400502 break;
503
504 case MMC_BUS_WIDTH_4:
yangbo luf4932cf2015-10-08 18:36:36 +0800505 ctrl |= ESDHC_CTRL_4BITBUS;
Oded Gabbay66b50a02013-06-27 12:00:05 -0400506 break;
507
508 default:
Oded Gabbay66b50a02013-06-27 12:00:05 -0400509 break;
510 }
511
yangbo luf4932cf2015-10-08 18:36:36 +0800512 sdhci_writel(host, ctrl, ESDHC_PROCTL);
Oded Gabbay66b50a02013-06-27 12:00:05 -0400513}
514
Alessio Igor Bogani304f0a92014-12-09 09:40:38 +0100515static void esdhc_reset(struct sdhci_host *host, u8 mask)
516{
517 sdhci_reset(host, mask);
518
519 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
520 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
521}
522
Ulf Hansson9e48b332016-07-27 11:01:48 +0200523#ifdef CONFIG_PM_SLEEP
Russell King723f7922014-04-25 12:59:46 +0100524static u32 esdhc_proctl;
525static int esdhc_of_suspend(struct device *dev)
526{
527 struct sdhci_host *host = dev_get_drvdata(dev);
528
yangbo luf4932cf2015-10-08 18:36:36 +0800529 esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
Russell King723f7922014-04-25 12:59:46 +0100530
531 return sdhci_suspend_host(host);
532}
533
Ulf Hansson06732b82014-05-23 10:36:44 +0200534static int esdhc_of_resume(struct device *dev)
Russell King723f7922014-04-25 12:59:46 +0100535{
536 struct sdhci_host *host = dev_get_drvdata(dev);
537 int ret = sdhci_resume_host(host);
538
539 if (ret == 0) {
540 /* Isn't this already done by sdhci_resume_host() ? --rmk */
541 esdhc_of_enable_dma(host);
yangbo luf4932cf2015-10-08 18:36:36 +0800542 sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
Russell King723f7922014-04-25 12:59:46 +0100543 }
Russell King723f7922014-04-25 12:59:46 +0100544 return ret;
545}
Russell King723f7922014-04-25 12:59:46 +0100546#endif
547
Ulf Hansson9e48b332016-07-27 11:01:48 +0200548static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
549 esdhc_of_suspend,
550 esdhc_of_resume);
551
yangbo luf4932cf2015-10-08 18:36:36 +0800552static const struct sdhci_ops sdhci_esdhc_be_ops = {
553 .read_l = esdhc_be_readl,
554 .read_w = esdhc_be_readw,
555 .read_b = esdhc_be_readb,
556 .write_l = esdhc_be_writel,
557 .write_w = esdhc_be_writew,
558 .write_b = esdhc_be_writeb,
559 .set_clock = esdhc_of_set_clock,
560 .enable_dma = esdhc_of_enable_dma,
561 .get_max_clock = esdhc_of_get_max_clock,
562 .get_min_clock = esdhc_of_get_min_clock,
563 .adma_workaround = esdhc_of_adma_workaround,
564 .set_bus_width = esdhc_pltfm_set_bus_width,
565 .reset = esdhc_reset,
566 .set_uhs_signaling = sdhci_set_uhs_signaling,
567};
568
569static const struct sdhci_ops sdhci_esdhc_le_ops = {
570 .read_l = esdhc_le_readl,
571 .read_w = esdhc_le_readw,
572 .read_b = esdhc_le_readb,
573 .write_l = esdhc_le_writel,
574 .write_w = esdhc_le_writew,
575 .write_b = esdhc_le_writeb,
576 .set_clock = esdhc_of_set_clock,
577 .enable_dma = esdhc_of_enable_dma,
578 .get_max_clock = esdhc_of_get_max_clock,
579 .get_min_clock = esdhc_of_get_min_clock,
580 .adma_workaround = esdhc_of_adma_workaround,
581 .set_bus_width = esdhc_pltfm_set_bus_width,
582 .reset = esdhc_reset,
583 .set_uhs_signaling = sdhci_set_uhs_signaling,
584};
585
586static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
yangbo lue9acc772016-12-26 17:40:44 +0800587 .quirks = ESDHC_DEFAULT_QUIRKS |
588#ifdef CONFIG_PPC
589 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
590#endif
591 SDHCI_QUIRK_NO_CARD_NO_RESET |
592 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
yangbo luf4932cf2015-10-08 18:36:36 +0800593 .ops = &sdhci_esdhc_be_ops,
Albert Herranz7657c3a2009-12-17 15:27:20 -0800594};
Shawn Guo38576af2011-05-27 23:48:14 +0800595
yangbo luf4932cf2015-10-08 18:36:36 +0800596static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
yangbo lue9acc772016-12-26 17:40:44 +0800597 .quirks = ESDHC_DEFAULT_QUIRKS |
598 SDHCI_QUIRK_NO_CARD_NO_RESET |
599 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
yangbo luf4932cf2015-10-08 18:36:36 +0800600 .ops = &sdhci_esdhc_le_ops,
601};
602
yangbo lu151ede42016-11-09 11:14:12 +0800603static struct soc_device_attribute soc_incorrect_hostver[] = {
604 { .family = "QorIQ T4240", .revision = "1.0", },
605 { .family = "QorIQ T4240", .revision = "2.0", },
606 { },
607};
608
yangbo luf4932cf2015-10-08 18:36:36 +0800609static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
610{
611 struct sdhci_pltfm_host *pltfm_host;
612 struct sdhci_esdhc *esdhc;
613 u16 host_ver;
614
615 pltfm_host = sdhci_priv(host);
Jisheng Zhang8605e7a2016-02-16 21:08:26 +0800616 esdhc = sdhci_pltfm_priv(pltfm_host);
yangbo luf4932cf2015-10-08 18:36:36 +0800617
618 host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
619 esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
620 SDHCI_VENDOR_VER_SHIFT;
621 esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
yangbo lu151ede42016-11-09 11:14:12 +0800622 if (soc_device_match(soc_incorrect_hostver))
623 esdhc->quirk_incorrect_hostver = true;
624 else
625 esdhc->quirk_incorrect_hostver = false;
yangbo luf4932cf2015-10-08 18:36:36 +0800626}
627
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500628static int sdhci_esdhc_probe(struct platform_device *pdev)
Shawn Guo38576af2011-05-27 23:48:14 +0800629{
Oded Gabbay66b50a02013-06-27 12:00:05 -0400630 struct sdhci_host *host;
Oded Gabbaydcaff042013-07-05 12:48:35 -0400631 struct device_node *np;
yangbo lu1ef5e492015-11-25 10:05:37 +0800632 struct sdhci_pltfm_host *pltfm_host;
633 struct sdhci_esdhc *esdhc;
Oded Gabbay66b50a02013-06-27 12:00:05 -0400634 int ret;
635
yangbo luf4932cf2015-10-08 18:36:36 +0800636 np = pdev->dev.of_node;
637
Julia Lawall150d4242016-08-05 10:56:46 +0200638 if (of_property_read_bool(np, "little-endian"))
Jisheng Zhang8605e7a2016-02-16 21:08:26 +0800639 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
640 sizeof(struct sdhci_esdhc));
yangbo luf4932cf2015-10-08 18:36:36 +0800641 else
Jisheng Zhang8605e7a2016-02-16 21:08:26 +0800642 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
643 sizeof(struct sdhci_esdhc));
yangbo luf4932cf2015-10-08 18:36:36 +0800644
Oded Gabbay66b50a02013-06-27 12:00:05 -0400645 if (IS_ERR(host))
646 return PTR_ERR(host);
647
yangbo luf4932cf2015-10-08 18:36:36 +0800648 esdhc_init(pdev, host);
649
Oded Gabbay66b50a02013-06-27 12:00:05 -0400650 sdhci_get_of_property(pdev);
651
yangbo lu1ef5e492015-11-25 10:05:37 +0800652 pltfm_host = sdhci_priv(host);
Jisheng Zhang8605e7a2016-02-16 21:08:26 +0800653 esdhc = sdhci_pltfm_priv(pltfm_host);
yangbo lu1ef5e492015-11-25 10:05:37 +0800654 if (esdhc->vendor_ver == VENDOR_V_22)
655 host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
656
657 if (esdhc->vendor_ver > VENDOR_V_22)
658 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
659
Yangbo Lu74fd5e32015-06-01 13:47:12 +0800660 if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
661 of_device_is_compatible(np, "fsl,p5020-esdhc") ||
662 of_device_is_compatible(np, "fsl,p4080-esdhc") ||
663 of_device_is_compatible(np, "fsl,p1020-esdhc") ||
yangbo lue9acc772016-12-26 17:40:44 +0800664 of_device_is_compatible(np, "fsl,t1040-esdhc"))
Yangbo Lu74fd5e32015-06-01 13:47:12 +0800665 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
666
yangbo lua22950c2015-10-08 18:36:57 +0800667 if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
668 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
669
Oded Gabbaydcaff042013-07-05 12:48:35 -0400670 if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
671 /*
672 * Freescale messed up with P2020 as it has a non-standard
673 * host control register
674 */
675 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
676 }
677
Oded Gabbay66b50a02013-06-27 12:00:05 -0400678 /* call to generic mmc_of_parse to support additional capabilities */
Ulf Hanssonf0991402014-12-18 10:41:41 +0100679 ret = mmc_of_parse(host->mmc);
680 if (ret)
681 goto err;
682
Haijun Zhang490104a2013-08-26 09:19:24 +0800683 mmc_of_parse_voltage(np, &host->ocr_mask);
Oded Gabbay66b50a02013-06-27 12:00:05 -0400684
685 ret = sdhci_add_host(host);
686 if (ret)
Ulf Hanssonf0991402014-12-18 10:41:41 +0100687 goto err;
Oded Gabbay66b50a02013-06-27 12:00:05 -0400688
Ulf Hanssonf0991402014-12-18 10:41:41 +0100689 return 0;
690 err:
691 sdhci_pltfm_free(pdev);
Oded Gabbay66b50a02013-06-27 12:00:05 -0400692 return ret;
Shawn Guo38576af2011-05-27 23:48:14 +0800693}
694
Shawn Guo38576af2011-05-27 23:48:14 +0800695static const struct of_device_id sdhci_esdhc_of_match[] = {
696 { .compatible = "fsl,mpc8379-esdhc" },
697 { .compatible = "fsl,mpc8536-esdhc" },
698 { .compatible = "fsl,esdhc" },
699 { }
700};
701MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
702
703static struct platform_driver sdhci_esdhc_driver = {
704 .driver = {
705 .name = "sdhci-esdhc",
Shawn Guo38576af2011-05-27 23:48:14 +0800706 .of_match_table = sdhci_esdhc_of_match,
Ulf Hansson9e48b332016-07-27 11:01:48 +0200707 .pm = &esdhc_of_dev_pm_ops,
Shawn Guo38576af2011-05-27 23:48:14 +0800708 },
709 .probe = sdhci_esdhc_probe,
Kevin Haocaebcae2015-02-27 15:47:31 +0800710 .remove = sdhci_pltfm_unregister,
Shawn Guo38576af2011-05-27 23:48:14 +0800711};
712
Axel Lind1f81a62011-11-26 12:55:43 +0800713module_platform_driver(sdhci_esdhc_driver);
Shawn Guo38576af2011-05-27 23:48:14 +0800714
715MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
716MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
717 "Anton Vorontsov <avorontsov@ru.mvista.com>");
718MODULE_LICENSE("GPL v2");