Merge branch 'fixes' into next
[linux-2.6-microblaze.git] / drivers / mmc / host / sdhci-of-esdhc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Freescale eSDHC controller driver.
4  *
5  * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
6  * Copyright (c) 2009 MontaVista Software, Inc.
7  *
8  * Authors: Xiaobo Xie <X.Xie@freescale.com>
9  *          Anton Vorontsov <avorontsov@ru.mvista.com>
10  */
11
12 #include <linux/err.h>
13 #include <linux/io.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/sys_soc.h>
19 #include <linux/clk.h>
20 #include <linux/ktime.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/mmc.h>
24 #include "sdhci-pltfm.h"
25 #include "sdhci-esdhc.h"
26
27 #define VENDOR_V_22     0x12
28 #define VENDOR_V_23     0x13
29
30 #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
31
32 struct esdhc_clk_fixup {
33         const unsigned int sd_dflt_max_clk;
34         const unsigned int max_clk[MMC_TIMING_NUM];
35 };
36
37 static const struct esdhc_clk_fixup ls1021a_esdhc_clk = {
38         .sd_dflt_max_clk = 25000000,
39         .max_clk[MMC_TIMING_MMC_HS] = 46500000,
40         .max_clk[MMC_TIMING_SD_HS] = 46500000,
41 };
42
43 static const struct esdhc_clk_fixup ls1046a_esdhc_clk = {
44         .sd_dflt_max_clk = 25000000,
45         .max_clk[MMC_TIMING_UHS_SDR104] = 167000000,
46         .max_clk[MMC_TIMING_MMC_HS200] = 167000000,
47 };
48
49 static const struct esdhc_clk_fixup ls1012a_esdhc_clk = {
50         .sd_dflt_max_clk = 25000000,
51         .max_clk[MMC_TIMING_UHS_SDR104] = 125000000,
52         .max_clk[MMC_TIMING_MMC_HS200] = 125000000,
53 };
54
55 static const struct esdhc_clk_fixup p1010_esdhc_clk = {
56         .sd_dflt_max_clk = 20000000,
57         .max_clk[MMC_TIMING_LEGACY] = 20000000,
58         .max_clk[MMC_TIMING_MMC_HS] = 42000000,
59         .max_clk[MMC_TIMING_SD_HS] = 40000000,
60 };
61
62 static const struct of_device_id sdhci_esdhc_of_match[] = {
63         { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk},
64         { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk},
65         { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk},
66         { .compatible = "fsl,p1010-esdhc",   .data = &p1010_esdhc_clk},
67         { .compatible = "fsl,mpc8379-esdhc" },
68         { .compatible = "fsl,mpc8536-esdhc" },
69         { .compatible = "fsl,esdhc" },
70         { }
71 };
72 MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
73
74 struct sdhci_esdhc {
75         u8 vendor_ver;
76         u8 spec_ver;
77         bool quirk_incorrect_hostver;
78         bool quirk_limited_clk_division;
79         bool quirk_unreliable_pulse_detection;
80         bool quirk_tuning_erratum_type1;
81         bool quirk_tuning_erratum_type2;
82         bool quirk_ignore_data_inhibit;
83         bool in_sw_tuning;
84         unsigned int peripheral_clock;
85         const struct esdhc_clk_fixup *clk_fixup;
86         u32 div_ratio;
87 };
88
89 /**
90  * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
91  *                     to make it compatible with SD spec.
92  *
93  * @host: pointer to sdhci_host
94  * @spec_reg: SD spec register address
95  * @value: 32bit eSDHC register value on spec_reg address
96  *
97  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
98  * registers are 32 bits. There are differences in register size, register
99  * address, register function, bit position and function between eSDHC spec
100  * and SD spec.
101  *
102  * Return a fixed up register value
103  */
104 static u32 esdhc_readl_fixup(struct sdhci_host *host,
105                                      int spec_reg, u32 value)
106 {
107         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
108         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
109         u32 ret;
110
111         /*
112          * The bit of ADMA flag in eSDHC is not compatible with standard
113          * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
114          * supported by eSDHC.
115          * And for many FSL eSDHC controller, the reset value of field
116          * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
117          * only these vendor version is greater than 2.2/0x12 support ADMA.
118          */
119         if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
120                 if (esdhc->vendor_ver > VENDOR_V_22) {
121                         ret = value | SDHCI_CAN_DO_ADMA2;
122                         return ret;
123                 }
124         }
125         /*
126          * The DAT[3:0] line signal levels and the CMD line signal level are
127          * not compatible with standard SDHC register. The line signal levels
128          * DAT[7:0] are at bits 31:24 and the command line signal level is at
129          * bit 23. All other bits are the same as in the standard SDHC
130          * register.
131          */
132         if (spec_reg == SDHCI_PRESENT_STATE) {
133                 ret = value & 0x000fffff;
134                 ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
135                 ret |= (value << 1) & SDHCI_CMD_LVL;
136                 return ret;
137         }
138
139         /*
140          * DTS properties of mmc host are used to enable each speed mode
141          * according to soc and board capability. So clean up
142          * SDR50/SDR104/DDR50 support bits here.
143          */
144         if (spec_reg == SDHCI_CAPABILITIES_1) {
145                 ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
146                                 SDHCI_SUPPORT_DDR50);
147                 return ret;
148         }
149
150         /*
151          * Some controllers have unreliable Data Line Active
152          * bit for commands with busy signal. This affects
153          * Command Inhibit (data) bit. Just ignore it since
154          * MMC core driver has already polled card status
155          * with CMD13 after any command with busy siganl.
156          */
157         if ((spec_reg == SDHCI_PRESENT_STATE) &&
158         (esdhc->quirk_ignore_data_inhibit == true)) {
159                 ret = value & ~SDHCI_DATA_INHIBIT;
160                 return ret;
161         }
162
163         ret = value;
164         return ret;
165 }
166
167 static u16 esdhc_readw_fixup(struct sdhci_host *host,
168                                      int spec_reg, u32 value)
169 {
170         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
171         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
172         u16 ret;
173         int shift = (spec_reg & 0x2) * 8;
174
175         if (spec_reg == SDHCI_HOST_VERSION)
176                 ret = value & 0xffff;
177         else
178                 ret = (value >> shift) & 0xffff;
179         /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
180          * vendor version and spec version information.
181          */
182         if ((spec_reg == SDHCI_HOST_VERSION) &&
183             (esdhc->quirk_incorrect_hostver))
184                 ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
185         return ret;
186 }
187
188 static u8 esdhc_readb_fixup(struct sdhci_host *host,
189                                      int spec_reg, u32 value)
190 {
191         u8 ret;
192         u8 dma_bits;
193         int shift = (spec_reg & 0x3) * 8;
194
195         ret = (value >> shift) & 0xff;
196
197         /*
198          * "DMA select" locates at offset 0x28 in SD specification, but on
199          * P5020 or P3041, it locates at 0x29.
200          */
201         if (spec_reg == SDHCI_HOST_CONTROL) {
202                 /* DMA select is 22,23 bits in Protocol Control Register */
203                 dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
204                 /* fixup the result */
205                 ret &= ~SDHCI_CTRL_DMA_MASK;
206                 ret |= dma_bits;
207         }
208         return ret;
209 }
210
211 /**
212  * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
213  *                      written into eSDHC register.
214  *
215  * @host: pointer to sdhci_host
216  * @spec_reg: SD spec register address
217  * @value: 8/16/32bit SD spec register value that would be written
218  * @old_value: 32bit eSDHC register value on spec_reg address
219  *
220  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
221  * registers are 32 bits. There are differences in register size, register
222  * address, register function, bit position and function between eSDHC spec
223  * and SD spec.
224  *
225  * Return a fixed up register value
226  */
227 static u32 esdhc_writel_fixup(struct sdhci_host *host,
228                                      int spec_reg, u32 value, u32 old_value)
229 {
230         u32 ret;
231
232         /*
233          * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
234          * when SYSCTL[RSTD] is set for some special operations.
235          * No any impact on other operation.
236          */
237         if (spec_reg == SDHCI_INT_ENABLE)
238                 ret = value | SDHCI_INT_BLK_GAP;
239         else
240                 ret = value;
241
242         return ret;
243 }
244
245 static u32 esdhc_writew_fixup(struct sdhci_host *host,
246                                      int spec_reg, u16 value, u32 old_value)
247 {
248         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
249         int shift = (spec_reg & 0x2) * 8;
250         u32 ret;
251
252         switch (spec_reg) {
253         case SDHCI_TRANSFER_MODE:
254                 /*
255                  * Postpone this write, we must do it together with a
256                  * command write that is down below. Return old value.
257                  */
258                 pltfm_host->xfer_mode_shadow = value;
259                 return old_value;
260         case SDHCI_COMMAND:
261                 ret = (value << 16) | pltfm_host->xfer_mode_shadow;
262                 return ret;
263         }
264
265         ret = old_value & (~(0xffff << shift));
266         ret |= (value << shift);
267
268         if (spec_reg == SDHCI_BLOCK_SIZE) {
269                 /*
270                  * Two last DMA bits are reserved, and first one is used for
271                  * non-standard blksz of 4096 bytes that we don't support
272                  * yet. So clear the DMA boundary bits.
273                  */
274                 ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
275         }
276         return ret;
277 }
278
279 static u32 esdhc_writeb_fixup(struct sdhci_host *host,
280                                      int spec_reg, u8 value, u32 old_value)
281 {
282         u32 ret;
283         u32 dma_bits;
284         u8 tmp;
285         int shift = (spec_reg & 0x3) * 8;
286
287         /*
288          * eSDHC doesn't have a standard power control register, so we do
289          * nothing here to avoid incorrect operation.
290          */
291         if (spec_reg == SDHCI_POWER_CONTROL)
292                 return old_value;
293         /*
294          * "DMA select" location is offset 0x28 in SD specification, but on
295          * P5020 or P3041, it's located at 0x29.
296          */
297         if (spec_reg == SDHCI_HOST_CONTROL) {
298                 /*
299                  * If host control register is not standard, exit
300                  * this function
301                  */
302                 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
303                         return old_value;
304
305                 /* DMA select is 22,23 bits in Protocol Control Register */
306                 dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
307                 ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
308                 tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
309                       (old_value & SDHCI_CTRL_DMA_MASK);
310                 ret = (ret & (~0xff)) | tmp;
311
312                 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
313                 ret &= ~ESDHC_HOST_CONTROL_RES;
314                 return ret;
315         }
316
317         ret = (old_value & (~(0xff << shift))) | (value << shift);
318         return ret;
319 }
320
321 static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
322 {
323         u32 ret;
324         u32 value;
325
326         if (reg == SDHCI_CAPABILITIES_1)
327                 value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
328         else
329                 value = ioread32be(host->ioaddr + reg);
330
331         ret = esdhc_readl_fixup(host, reg, value);
332
333         return ret;
334 }
335
336 static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
337 {
338         u32 ret;
339         u32 value;
340
341         if (reg == SDHCI_CAPABILITIES_1)
342                 value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
343         else
344                 value = ioread32(host->ioaddr + reg);
345
346         ret = esdhc_readl_fixup(host, reg, value);
347
348         return ret;
349 }
350
351 static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
352 {
353         u16 ret;
354         u32 value;
355         int base = reg & ~0x3;
356
357         value = ioread32be(host->ioaddr + base);
358         ret = esdhc_readw_fixup(host, reg, value);
359         return ret;
360 }
361
362 static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
363 {
364         u16 ret;
365         u32 value;
366         int base = reg & ~0x3;
367
368         value = ioread32(host->ioaddr + base);
369         ret = esdhc_readw_fixup(host, reg, value);
370         return ret;
371 }
372
373 static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
374 {
375         u8 ret;
376         u32 value;
377         int base = reg & ~0x3;
378
379         value = ioread32be(host->ioaddr + base);
380         ret = esdhc_readb_fixup(host, reg, value);
381         return ret;
382 }
383
384 static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
385 {
386         u8 ret;
387         u32 value;
388         int base = reg & ~0x3;
389
390         value = ioread32(host->ioaddr + base);
391         ret = esdhc_readb_fixup(host, reg, value);
392         return ret;
393 }
394
395 static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
396 {
397         u32 value;
398
399         value = esdhc_writel_fixup(host, reg, val, 0);
400         iowrite32be(value, host->ioaddr + reg);
401 }
402
403 static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
404 {
405         u32 value;
406
407         value = esdhc_writel_fixup(host, reg, val, 0);
408         iowrite32(value, host->ioaddr + reg);
409 }
410
411 static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
412 {
413         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
414         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
415         int base = reg & ~0x3;
416         u32 value;
417         u32 ret;
418
419         value = ioread32be(host->ioaddr + base);
420         ret = esdhc_writew_fixup(host, reg, val, value);
421         if (reg != SDHCI_TRANSFER_MODE)
422                 iowrite32be(ret, host->ioaddr + base);
423
424         /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
425          * 1us later after ESDHC_EXTN is set.
426          */
427         if (base == ESDHC_SYSTEM_CONTROL_2) {
428                 if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
429                     esdhc->in_sw_tuning) {
430                         udelay(1);
431                         ret |= ESDHC_SMPCLKSEL;
432                         iowrite32be(ret, host->ioaddr + base);
433                 }
434         }
435 }
436
437 static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
438 {
439         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
440         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
441         int base = reg & ~0x3;
442         u32 value;
443         u32 ret;
444
445         value = ioread32(host->ioaddr + base);
446         ret = esdhc_writew_fixup(host, reg, val, value);
447         if (reg != SDHCI_TRANSFER_MODE)
448                 iowrite32(ret, host->ioaddr + base);
449
450         /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
451          * 1us later after ESDHC_EXTN is set.
452          */
453         if (base == ESDHC_SYSTEM_CONTROL_2) {
454                 if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
455                     esdhc->in_sw_tuning) {
456                         udelay(1);
457                         ret |= ESDHC_SMPCLKSEL;
458                         iowrite32(ret, host->ioaddr + base);
459                 }
460         }
461 }
462
463 static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
464 {
465         int base = reg & ~0x3;
466         u32 value;
467         u32 ret;
468
469         value = ioread32be(host->ioaddr + base);
470         ret = esdhc_writeb_fixup(host, reg, val, value);
471         iowrite32be(ret, host->ioaddr + base);
472 }
473
474 static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
475 {
476         int base = reg & ~0x3;
477         u32 value;
478         u32 ret;
479
480         value = ioread32(host->ioaddr + base);
481         ret = esdhc_writeb_fixup(host, reg, val, value);
482         iowrite32(ret, host->ioaddr + base);
483 }
484
485 /*
486  * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
487  * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
488  * and Block Gap Event(IRQSTAT[BGE]) are also set.
489  * For Continue, apply soft reset for data(SYSCTL[RSTD]);
490  * and re-issue the entire read transaction from beginning.
491  */
492 static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
493 {
494         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
495         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
496         bool applicable;
497         dma_addr_t dmastart;
498         dma_addr_t dmanow;
499
500         applicable = (intmask & SDHCI_INT_DATA_END) &&
501                      (intmask & SDHCI_INT_BLK_GAP) &&
502                      (esdhc->vendor_ver == VENDOR_V_23);
503         if (!applicable)
504                 return;
505
506         host->data->error = 0;
507         dmastart = sg_dma_address(host->data->sg);
508         dmanow = dmastart + host->data->bytes_xfered;
509         /*
510          * Force update to the next DMA block boundary.
511          */
512         dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
513                 SDHCI_DEFAULT_BOUNDARY_SIZE;
514         host->data->bytes_xfered = dmanow - dmastart;
515         sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
516 }
517
518 static int esdhc_of_enable_dma(struct sdhci_host *host)
519 {
520         u32 value;
521         struct device *dev = mmc_dev(host->mmc);
522
523         if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
524             of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc"))
525                 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
526
527         value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
528
529         if (of_dma_is_coherent(dev->of_node))
530                 value |= ESDHC_DMA_SNOOP;
531         else
532                 value &= ~ESDHC_DMA_SNOOP;
533
534         sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
535         return 0;
536 }
537
538 static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
539 {
540         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
541         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
542
543         if (esdhc->peripheral_clock)
544                 return esdhc->peripheral_clock;
545         else
546                 return pltfm_host->clock;
547 }
548
549 static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
550 {
551         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
552         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
553         unsigned int clock;
554
555         if (esdhc->peripheral_clock)
556                 clock = esdhc->peripheral_clock;
557         else
558                 clock = pltfm_host->clock;
559         return clock / 256 / 16;
560 }
561
562 static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
563 {
564         u32 val;
565         ktime_t timeout;
566
567         val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
568
569         if (enable)
570                 val |= ESDHC_CLOCK_SDCLKEN;
571         else
572                 val &= ~ESDHC_CLOCK_SDCLKEN;
573
574         sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
575
576         /* Wait max 20 ms */
577         timeout = ktime_add_ms(ktime_get(), 20);
578         val = ESDHC_CLOCK_STABLE;
579         while  (1) {
580                 bool timedout = ktime_after(ktime_get(), timeout);
581
582                 if (sdhci_readl(host, ESDHC_PRSSTAT) & val)
583                         break;
584                 if (timedout) {
585                         pr_err("%s: Internal clock never stabilised.\n",
586                                 mmc_hostname(host->mmc));
587                         break;
588                 }
589                 udelay(10);
590         }
591 }
592
593 static void esdhc_flush_async_fifo(struct sdhci_host *host)
594 {
595         ktime_t timeout;
596         u32 val;
597
598         val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
599         val |= ESDHC_FLUSH_ASYNC_FIFO;
600         sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
601
602         /* Wait max 20 ms */
603         timeout = ktime_add_ms(ktime_get(), 20);
604         while (1) {
605                 bool timedout = ktime_after(ktime_get(), timeout);
606
607                 if (!(sdhci_readl(host, ESDHC_DMA_SYSCTL) &
608                       ESDHC_FLUSH_ASYNC_FIFO))
609                         break;
610                 if (timedout) {
611                         pr_err("%s: flushing asynchronous FIFO timeout.\n",
612                                 mmc_hostname(host->mmc));
613                         break;
614                 }
615                 usleep_range(10, 20);
616         }
617 }
618
619 static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
620 {
621         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
622         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
623         int pre_div = 1;
624         int div = 1;
625         int division;
626         ktime_t timeout;
627         long fixup = 0;
628         u32 temp;
629
630         host->mmc->actual_clock = 0;
631
632         if (clock == 0) {
633                 esdhc_clock_enable(host, false);
634                 return;
635         }
636
637         /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
638         if (esdhc->vendor_ver < VENDOR_V_23)
639                 pre_div = 2;
640
641         if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
642                 esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
643                 fixup = esdhc->clk_fixup->sd_dflt_max_clk;
644         else if (esdhc->clk_fixup)
645                 fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
646
647         if (fixup && clock > fixup)
648                 clock = fixup;
649
650         temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
651         temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
652                   ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
653         sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
654
655         while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
656                 pre_div *= 2;
657
658         while (host->max_clk / pre_div / div > clock && div < 16)
659                 div++;
660
661         if (esdhc->quirk_limited_clk_division &&
662             clock == MMC_HS200_MAX_DTR &&
663             (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 ||
664              host->flags & SDHCI_HS400_TUNING)) {
665                 division = pre_div * div;
666                 if (division <= 4) {
667                         pre_div = 4;
668                         div = 1;
669                 } else if (division <= 8) {
670                         pre_div = 4;
671                         div = 2;
672                 } else if (division <= 12) {
673                         pre_div = 4;
674                         div = 3;
675                 } else {
676                         pr_warn("%s: using unsupported clock division.\n",
677                                 mmc_hostname(host->mmc));
678                 }
679         }
680
681         dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
682                 clock, host->max_clk / pre_div / div);
683         host->mmc->actual_clock = host->max_clk / pre_div / div;
684         esdhc->div_ratio = pre_div * div;
685         pre_div >>= 1;
686         div--;
687
688         temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
689         temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
690                 | (div << ESDHC_DIVIDER_SHIFT)
691                 | (pre_div << ESDHC_PREDIV_SHIFT));
692         sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
693
694         if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 &&
695             clock == MMC_HS200_MAX_DTR) {
696                 temp = sdhci_readl(host, ESDHC_TBCTL);
697                 sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL);
698                 temp = sdhci_readl(host, ESDHC_SDCLKCTL);
699                 sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL);
700                 esdhc_clock_enable(host, true);
701
702                 temp = sdhci_readl(host, ESDHC_DLLCFG0);
703                 temp |= ESDHC_DLL_ENABLE;
704                 if (host->mmc->actual_clock == MMC_HS200_MAX_DTR)
705                         temp |= ESDHC_DLL_FREQ_SEL;
706                 sdhci_writel(host, temp, ESDHC_DLLCFG0);
707                 temp = sdhci_readl(host, ESDHC_TBCTL);
708                 sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL);
709
710                 esdhc_clock_enable(host, false);
711                 esdhc_flush_async_fifo(host);
712         }
713
714         /* Wait max 20 ms */
715         timeout = ktime_add_ms(ktime_get(), 20);
716         while (1) {
717                 bool timedout = ktime_after(ktime_get(), timeout);
718
719                 if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
720                         break;
721                 if (timedout) {
722                         pr_err("%s: Internal clock never stabilised.\n",
723                                 mmc_hostname(host->mmc));
724                         return;
725                 }
726                 udelay(10);
727         }
728
729         temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
730         temp |= ESDHC_CLOCK_SDCLKEN;
731         sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
732 }
733
734 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
735 {
736         u32 ctrl;
737
738         ctrl = sdhci_readl(host, ESDHC_PROCTL);
739         ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
740         switch (width) {
741         case MMC_BUS_WIDTH_8:
742                 ctrl |= ESDHC_CTRL_8BITBUS;
743                 break;
744
745         case MMC_BUS_WIDTH_4:
746                 ctrl |= ESDHC_CTRL_4BITBUS;
747                 break;
748
749         default:
750                 break;
751         }
752
753         sdhci_writel(host, ctrl, ESDHC_PROCTL);
754 }
755
756 static void esdhc_reset(struct sdhci_host *host, u8 mask)
757 {
758         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
759         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
760         u32 val;
761
762         sdhci_reset(host, mask);
763
764         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
765         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
766
767         if (mask & SDHCI_RESET_ALL) {
768                 val = sdhci_readl(host, ESDHC_TBCTL);
769                 val &= ~ESDHC_TB_EN;
770                 sdhci_writel(host, val, ESDHC_TBCTL);
771
772                 if (esdhc->quirk_unreliable_pulse_detection) {
773                         val = sdhci_readl(host, ESDHC_DLLCFG1);
774                         val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL;
775                         sdhci_writel(host, val, ESDHC_DLLCFG1);
776                 }
777         }
778 }
779
780 /* The SCFG, Supplemental Configuration Unit, provides SoC specific
781  * configuration and status registers for the device. There is a
782  * SDHC IO VSEL control register on SCFG for some platforms. It's
783  * used to support SDHC IO voltage switching.
784  */
785 static const struct of_device_id scfg_device_ids[] = {
786         { .compatible = "fsl,t1040-scfg", },
787         { .compatible = "fsl,ls1012a-scfg", },
788         { .compatible = "fsl,ls1046a-scfg", },
789         {}
790 };
791
792 /* SDHC IO VSEL control register definition */
793 #define SCFG_SDHCIOVSELCR       0x408
794 #define SDHCIOVSELCR_TGLEN      0x80000000
795 #define SDHCIOVSELCR_VSELVAL    0x60000000
796 #define SDHCIOVSELCR_SDHC_VS    0x00000001
797
798 static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
799                                        struct mmc_ios *ios)
800 {
801         struct sdhci_host *host = mmc_priv(mmc);
802         struct device_node *scfg_node;
803         void __iomem *scfg_base = NULL;
804         u32 sdhciovselcr;
805         u32 val;
806
807         /*
808          * Signal Voltage Switching is only applicable for Host Controllers
809          * v3.00 and above.
810          */
811         if (host->version < SDHCI_SPEC_300)
812                 return 0;
813
814         val = sdhci_readl(host, ESDHC_PROCTL);
815
816         switch (ios->signal_voltage) {
817         case MMC_SIGNAL_VOLTAGE_330:
818                 val &= ~ESDHC_VOLT_SEL;
819                 sdhci_writel(host, val, ESDHC_PROCTL);
820                 return 0;
821         case MMC_SIGNAL_VOLTAGE_180:
822                 scfg_node = of_find_matching_node(NULL, scfg_device_ids);
823                 if (scfg_node)
824                         scfg_base = of_iomap(scfg_node, 0);
825                 if (scfg_base) {
826                         sdhciovselcr = SDHCIOVSELCR_TGLEN |
827                                        SDHCIOVSELCR_VSELVAL;
828                         iowrite32be(sdhciovselcr,
829                                 scfg_base + SCFG_SDHCIOVSELCR);
830
831                         val |= ESDHC_VOLT_SEL;
832                         sdhci_writel(host, val, ESDHC_PROCTL);
833                         mdelay(5);
834
835                         sdhciovselcr = SDHCIOVSELCR_TGLEN |
836                                        SDHCIOVSELCR_SDHC_VS;
837                         iowrite32be(sdhciovselcr,
838                                 scfg_base + SCFG_SDHCIOVSELCR);
839                         iounmap(scfg_base);
840                 } else {
841                         val |= ESDHC_VOLT_SEL;
842                         sdhci_writel(host, val, ESDHC_PROCTL);
843                 }
844                 return 0;
845         default:
846                 return 0;
847         }
848 }
849
850 static struct soc_device_attribute soc_tuning_erratum_type1[] = {
851         { .family = "QorIQ T1023", .revision = "1.0", },
852         { .family = "QorIQ T1040", .revision = "1.0", },
853         { .family = "QorIQ T2080", .revision = "1.0", },
854         { .family = "QorIQ LS1021A", .revision = "1.0", },
855         { },
856 };
857
858 static struct soc_device_attribute soc_tuning_erratum_type2[] = {
859         { .family = "QorIQ LS1012A", .revision = "1.0", },
860         { .family = "QorIQ LS1043A", .revision = "1.*", },
861         { .family = "QorIQ LS1046A", .revision = "1.0", },
862         { .family = "QorIQ LS1080A", .revision = "1.0", },
863         { .family = "QorIQ LS2080A", .revision = "1.0", },
864         { .family = "QorIQ LA1575A", .revision = "1.0", },
865         { },
866 };
867
868 static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable)
869 {
870         u32 val;
871
872         esdhc_clock_enable(host, false);
873         esdhc_flush_async_fifo(host);
874
875         val = sdhci_readl(host, ESDHC_TBCTL);
876         if (enable)
877                 val |= ESDHC_TB_EN;
878         else
879                 val &= ~ESDHC_TB_EN;
880         sdhci_writel(host, val, ESDHC_TBCTL);
881
882         esdhc_clock_enable(host, true);
883 }
884
885 static void esdhc_prepare_sw_tuning(struct sdhci_host *host, u8 *window_start,
886                                     u8 *window_end)
887 {
888         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
889         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
890         u8 tbstat_15_8, tbstat_7_0;
891         u32 val;
892
893         if (esdhc->quirk_tuning_erratum_type1) {
894                 *window_start = 5 * esdhc->div_ratio;
895                 *window_end = 3 * esdhc->div_ratio;
896                 return;
897         }
898
899         /* Write TBCTL[11:8]=4'h8 */
900         val = sdhci_readl(host, ESDHC_TBCTL);
901         val &= ~(0xf << 8);
902         val |= 8 << 8;
903         sdhci_writel(host, val, ESDHC_TBCTL);
904
905         mdelay(1);
906
907         /* Read TBCTL[31:0] register and rewrite again */
908         val = sdhci_readl(host, ESDHC_TBCTL);
909         sdhci_writel(host, val, ESDHC_TBCTL);
910
911         mdelay(1);
912
913         /* Read the TBSTAT[31:0] register twice */
914         val = sdhci_readl(host, ESDHC_TBSTAT);
915         val = sdhci_readl(host, ESDHC_TBSTAT);
916
917         /* Reset data lines by setting ESDHCCTL[RSTD] */
918         sdhci_reset(host, SDHCI_RESET_DATA);
919         /* Write 32'hFFFF_FFFF to IRQSTAT register */
920         sdhci_writel(host, 0xFFFFFFFF, SDHCI_INT_STATUS);
921
922         /* If TBSTAT[15:8]-TBSTAT[7:0] > 4 * div_ratio
923          * or TBSTAT[7:0]-TBSTAT[15:8] > 4 * div_ratio,
924          * then program TBPTR[TB_WNDW_END_PTR] = 4 * div_ratio
925          * and program TBPTR[TB_WNDW_START_PTR] = 8 * div_ratio.
926          */
927         tbstat_7_0 = val & 0xff;
928         tbstat_15_8 = (val >> 8) & 0xff;
929
930         if (abs(tbstat_15_8 - tbstat_7_0) > (4 * esdhc->div_ratio)) {
931                 *window_start = 8 * esdhc->div_ratio;
932                 *window_end = 4 * esdhc->div_ratio;
933         } else {
934                 *window_start = 5 * esdhc->div_ratio;
935                 *window_end = 3 * esdhc->div_ratio;
936         }
937 }
938
939 static int esdhc_execute_sw_tuning(struct mmc_host *mmc, u32 opcode,
940                                    u8 window_start, u8 window_end)
941 {
942         struct sdhci_host *host = mmc_priv(mmc);
943         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
944         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
945         u32 val;
946         int ret;
947
948         /* Program TBPTR[TB_WNDW_END_PTR] and TBPTR[TB_WNDW_START_PTR] */
949         val = ((u32)window_start << ESDHC_WNDW_STRT_PTR_SHIFT) &
950               ESDHC_WNDW_STRT_PTR_MASK;
951         val |= window_end & ESDHC_WNDW_END_PTR_MASK;
952         sdhci_writel(host, val, ESDHC_TBPTR);
953
954         /* Program the software tuning mode by setting TBCTL[TB_MODE]=2'h3 */
955         val = sdhci_readl(host, ESDHC_TBCTL);
956         val &= ~ESDHC_TB_MODE_MASK;
957         val |= ESDHC_TB_MODE_SW;
958         sdhci_writel(host, val, ESDHC_TBCTL);
959
960         esdhc->in_sw_tuning = true;
961         ret = sdhci_execute_tuning(mmc, opcode);
962         esdhc->in_sw_tuning = false;
963         return ret;
964 }
965
966 static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
967 {
968         struct sdhci_host *host = mmc_priv(mmc);
969         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
970         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
971         u8 window_start, window_end;
972         int ret, retries = 1;
973         bool hs400_tuning;
974         unsigned int clk;
975         u32 val;
976
977         /* For tuning mode, the sd clock divisor value
978          * must be larger than 3 according to reference manual.
979          */
980         clk = esdhc->peripheral_clock / 3;
981         if (host->clock > clk)
982                 esdhc_of_set_clock(host, clk);
983
984         esdhc_tuning_block_enable(host, true);
985
986         hs400_tuning = host->flags & SDHCI_HS400_TUNING;
987
988         do {
989                 if (esdhc->quirk_limited_clk_division &&
990                     hs400_tuning)
991                         esdhc_of_set_clock(host, host->clock);
992
993                 /* Do HW tuning */
994                 val = sdhci_readl(host, ESDHC_TBCTL);
995                 val &= ~ESDHC_TB_MODE_MASK;
996                 val |= ESDHC_TB_MODE_3;
997                 sdhci_writel(host, val, ESDHC_TBCTL);
998
999                 ret = sdhci_execute_tuning(mmc, opcode);
1000                 if (ret)
1001                         break;
1002
1003                 /* If HW tuning fails and triggers erratum,
1004                  * try workaround.
1005                  */
1006                 ret = host->tuning_err;
1007                 if (ret == -EAGAIN &&
1008                     (esdhc->quirk_tuning_erratum_type1 ||
1009                      esdhc->quirk_tuning_erratum_type2)) {
1010                         /* Recover HS400 tuning flag */
1011                         if (hs400_tuning)
1012                                 host->flags |= SDHCI_HS400_TUNING;
1013                         pr_info("%s: Hold on to use fixed sampling clock. Try SW tuning!\n",
1014                                 mmc_hostname(mmc));
1015                         /* Do SW tuning */
1016                         esdhc_prepare_sw_tuning(host, &window_start,
1017                                                 &window_end);
1018                         ret = esdhc_execute_sw_tuning(mmc, opcode,
1019                                                       window_start,
1020                                                       window_end);
1021                         if (ret)
1022                                 break;
1023
1024                         /* Retry both HW/SW tuning with reduced clock. */
1025                         ret = host->tuning_err;
1026                         if (ret == -EAGAIN && retries) {
1027                                 /* Recover HS400 tuning flag */
1028                                 if (hs400_tuning)
1029                                         host->flags |= SDHCI_HS400_TUNING;
1030
1031                                 clk = host->max_clk / (esdhc->div_ratio + 1);
1032                                 esdhc_of_set_clock(host, clk);
1033                                 pr_info("%s: Hold on to use fixed sampling clock. Try tuning with reduced clock!\n",
1034                                         mmc_hostname(mmc));
1035                         } else {
1036                                 break;
1037                         }
1038                 } else {
1039                         break;
1040                 }
1041         } while (retries--);
1042
1043         if (ret) {
1044                 esdhc_tuning_block_enable(host, false);
1045         } else if (hs400_tuning) {
1046                 val = sdhci_readl(host, ESDHC_SDTIMNGCTL);
1047                 val |= ESDHC_FLW_CTL_BG;
1048                 sdhci_writel(host, val, ESDHC_SDTIMNGCTL);
1049         }
1050
1051         return ret;
1052 }
1053
1054 static void esdhc_set_uhs_signaling(struct sdhci_host *host,
1055                                    unsigned int timing)
1056 {
1057         if (timing == MMC_TIMING_MMC_HS400)
1058                 esdhc_tuning_block_enable(host, true);
1059         else
1060                 sdhci_set_uhs_signaling(host, timing);
1061 }
1062
1063 static u32 esdhc_irq(struct sdhci_host *host, u32 intmask)
1064 {
1065         u32 command;
1066
1067         if (of_find_compatible_node(NULL, NULL,
1068                                 "fsl,p2020-esdhc")) {
1069                 command = SDHCI_GET_CMD(sdhci_readw(host,
1070                                         SDHCI_COMMAND));
1071                 if (command == MMC_WRITE_MULTIPLE_BLOCK &&
1072                                 sdhci_readw(host, SDHCI_BLOCK_COUNT) &&
1073                                 intmask & SDHCI_INT_DATA_END) {
1074                         intmask &= ~SDHCI_INT_DATA_END;
1075                         sdhci_writel(host, SDHCI_INT_DATA_END,
1076                                         SDHCI_INT_STATUS);
1077                 }
1078         }
1079         return intmask;
1080 }
1081
1082 #ifdef CONFIG_PM_SLEEP
1083 static u32 esdhc_proctl;
1084 static int esdhc_of_suspend(struct device *dev)
1085 {
1086         struct sdhci_host *host = dev_get_drvdata(dev);
1087
1088         esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
1089
1090         if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1091                 mmc_retune_needed(host->mmc);
1092
1093         return sdhci_suspend_host(host);
1094 }
1095
1096 static int esdhc_of_resume(struct device *dev)
1097 {
1098         struct sdhci_host *host = dev_get_drvdata(dev);
1099         int ret = sdhci_resume_host(host);
1100
1101         if (ret == 0) {
1102                 /* Isn't this already done by sdhci_resume_host() ? --rmk */
1103                 esdhc_of_enable_dma(host);
1104                 sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
1105         }
1106         return ret;
1107 }
1108 #endif
1109
1110 static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
1111                         esdhc_of_suspend,
1112                         esdhc_of_resume);
1113
1114 static const struct sdhci_ops sdhci_esdhc_be_ops = {
1115         .read_l = esdhc_be_readl,
1116         .read_w = esdhc_be_readw,
1117         .read_b = esdhc_be_readb,
1118         .write_l = esdhc_be_writel,
1119         .write_w = esdhc_be_writew,
1120         .write_b = esdhc_be_writeb,
1121         .set_clock = esdhc_of_set_clock,
1122         .enable_dma = esdhc_of_enable_dma,
1123         .get_max_clock = esdhc_of_get_max_clock,
1124         .get_min_clock = esdhc_of_get_min_clock,
1125         .adma_workaround = esdhc_of_adma_workaround,
1126         .set_bus_width = esdhc_pltfm_set_bus_width,
1127         .reset = esdhc_reset,
1128         .set_uhs_signaling = esdhc_set_uhs_signaling,
1129         .irq = esdhc_irq,
1130 };
1131
1132 static const struct sdhci_ops sdhci_esdhc_le_ops = {
1133         .read_l = esdhc_le_readl,
1134         .read_w = esdhc_le_readw,
1135         .read_b = esdhc_le_readb,
1136         .write_l = esdhc_le_writel,
1137         .write_w = esdhc_le_writew,
1138         .write_b = esdhc_le_writeb,
1139         .set_clock = esdhc_of_set_clock,
1140         .enable_dma = esdhc_of_enable_dma,
1141         .get_max_clock = esdhc_of_get_max_clock,
1142         .get_min_clock = esdhc_of_get_min_clock,
1143         .adma_workaround = esdhc_of_adma_workaround,
1144         .set_bus_width = esdhc_pltfm_set_bus_width,
1145         .reset = esdhc_reset,
1146         .set_uhs_signaling = esdhc_set_uhs_signaling,
1147         .irq = esdhc_irq,
1148 };
1149
1150 static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
1151         .quirks = ESDHC_DEFAULT_QUIRKS |
1152 #ifdef CONFIG_PPC
1153                   SDHCI_QUIRK_BROKEN_CARD_DETECTION |
1154 #endif
1155                   SDHCI_QUIRK_NO_CARD_NO_RESET |
1156                   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
1157         .ops = &sdhci_esdhc_be_ops,
1158 };
1159
1160 static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
1161         .quirks = ESDHC_DEFAULT_QUIRKS |
1162                   SDHCI_QUIRK_NO_CARD_NO_RESET |
1163                   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
1164         .ops = &sdhci_esdhc_le_ops,
1165 };
1166
1167 static struct soc_device_attribute soc_incorrect_hostver[] = {
1168         { .family = "QorIQ T4240", .revision = "1.0", },
1169         { .family = "QorIQ T4240", .revision = "2.0", },
1170         { },
1171 };
1172
1173 static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = {
1174         { .family = "QorIQ LX2160A", .revision = "1.0", },
1175         { .family = "QorIQ LX2160A", .revision = "2.0", },
1176         { .family = "QorIQ LS1028A", .revision = "1.0", },
1177         { },
1178 };
1179
1180 static struct soc_device_attribute soc_unreliable_pulse_detection[] = {
1181         { .family = "QorIQ LX2160A", .revision = "1.0", },
1182         { },
1183 };
1184
1185 static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
1186 {
1187         const struct of_device_id *match;
1188         struct sdhci_pltfm_host *pltfm_host;
1189         struct sdhci_esdhc *esdhc;
1190         struct device_node *np;
1191         struct clk *clk;
1192         u32 val;
1193         u16 host_ver;
1194
1195         pltfm_host = sdhci_priv(host);
1196         esdhc = sdhci_pltfm_priv(pltfm_host);
1197
1198         host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
1199         esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
1200                              SDHCI_VENDOR_VER_SHIFT;
1201         esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
1202         if (soc_device_match(soc_incorrect_hostver))
1203                 esdhc->quirk_incorrect_hostver = true;
1204         else
1205                 esdhc->quirk_incorrect_hostver = false;
1206
1207         if (soc_device_match(soc_fixup_sdhc_clkdivs))
1208                 esdhc->quirk_limited_clk_division = true;
1209         else
1210                 esdhc->quirk_limited_clk_division = false;
1211
1212         if (soc_device_match(soc_unreliable_pulse_detection))
1213                 esdhc->quirk_unreliable_pulse_detection = true;
1214         else
1215                 esdhc->quirk_unreliable_pulse_detection = false;
1216
1217         match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
1218         if (match)
1219                 esdhc->clk_fixup = match->data;
1220         np = pdev->dev.of_node;
1221         clk = of_clk_get(np, 0);
1222         if (!IS_ERR(clk)) {
1223                 /*
1224                  * esdhc->peripheral_clock would be assigned with a value
1225                  * which is eSDHC base clock when use periperal clock.
1226                  * For some platforms, the clock value got by common clk
1227                  * API is peripheral clock while the eSDHC base clock is
1228                  * 1/2 peripheral clock.
1229                  */
1230                 if (of_device_is_compatible(np, "fsl,ls1046a-esdhc") ||
1231                     of_device_is_compatible(np, "fsl,ls1028a-esdhc") ||
1232                     of_device_is_compatible(np, "fsl,ls1088a-esdhc"))
1233                         esdhc->peripheral_clock = clk_get_rate(clk) / 2;
1234                 else
1235                         esdhc->peripheral_clock = clk_get_rate(clk);
1236
1237                 clk_put(clk);
1238         }
1239
1240         if (esdhc->peripheral_clock) {
1241                 esdhc_clock_enable(host, false);
1242                 val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
1243                 val |= ESDHC_PERIPHERAL_CLK_SEL;
1244                 sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
1245                 esdhc_clock_enable(host, true);
1246         }
1247 }
1248
1249 static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc)
1250 {
1251         esdhc_tuning_block_enable(mmc_priv(mmc), false);
1252         return 0;
1253 }
1254
1255 static int sdhci_esdhc_probe(struct platform_device *pdev)
1256 {
1257         struct sdhci_host *host;
1258         struct device_node *np;
1259         struct sdhci_pltfm_host *pltfm_host;
1260         struct sdhci_esdhc *esdhc;
1261         int ret;
1262
1263         np = pdev->dev.of_node;
1264
1265         if (of_property_read_bool(np, "little-endian"))
1266                 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
1267                                         sizeof(struct sdhci_esdhc));
1268         else
1269                 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
1270                                         sizeof(struct sdhci_esdhc));
1271
1272         if (IS_ERR(host))
1273                 return PTR_ERR(host);
1274
1275         host->mmc_host_ops.start_signal_voltage_switch =
1276                 esdhc_signal_voltage_switch;
1277         host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
1278         host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr;
1279         host->tuning_delay = 1;
1280
1281         esdhc_init(pdev, host);
1282
1283         sdhci_get_of_property(pdev);
1284
1285         pltfm_host = sdhci_priv(host);
1286         esdhc = sdhci_pltfm_priv(pltfm_host);
1287         if (soc_device_match(soc_tuning_erratum_type1))
1288                 esdhc->quirk_tuning_erratum_type1 = true;
1289         else
1290                 esdhc->quirk_tuning_erratum_type1 = false;
1291
1292         if (soc_device_match(soc_tuning_erratum_type2))
1293                 esdhc->quirk_tuning_erratum_type2 = true;
1294         else
1295                 esdhc->quirk_tuning_erratum_type2 = false;
1296
1297         if (esdhc->vendor_ver == VENDOR_V_22)
1298                 host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
1299
1300         if (esdhc->vendor_ver > VENDOR_V_22)
1301                 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1302
1303         if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) {
1304                 host->quirks |= SDHCI_QUIRK_RESET_AFTER_REQUEST;
1305                 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1306         }
1307
1308         if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
1309             of_device_is_compatible(np, "fsl,p5020-esdhc") ||
1310             of_device_is_compatible(np, "fsl,p4080-esdhc") ||
1311             of_device_is_compatible(np, "fsl,p1020-esdhc") ||
1312             of_device_is_compatible(np, "fsl,t1040-esdhc"))
1313                 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1314
1315         if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
1316                 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1317
1318         esdhc->quirk_ignore_data_inhibit = false;
1319         if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
1320                 /*
1321                  * Freescale messed up with P2020 as it has a non-standard
1322                  * host control register
1323                  */
1324                 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
1325                 esdhc->quirk_ignore_data_inhibit = true;
1326         }
1327
1328         /* call to generic mmc_of_parse to support additional capabilities */
1329         ret = mmc_of_parse(host->mmc);
1330         if (ret)
1331                 goto err;
1332
1333         mmc_of_parse_voltage(np, &host->ocr_mask);
1334
1335         ret = sdhci_add_host(host);
1336         if (ret)
1337                 goto err;
1338
1339         return 0;
1340  err:
1341         sdhci_pltfm_free(pdev);
1342         return ret;
1343 }
1344
1345 static struct platform_driver sdhci_esdhc_driver = {
1346         .driver = {
1347                 .name = "sdhci-esdhc",
1348                 .of_match_table = sdhci_esdhc_of_match,
1349                 .pm = &esdhc_of_dev_pm_ops,
1350         },
1351         .probe = sdhci_esdhc_probe,
1352         .remove = sdhci_pltfm_unregister,
1353 };
1354
1355 module_platform_driver(sdhci_esdhc_driver);
1356
1357 MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
1358 MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
1359               "Anton Vorontsov <avorontsov@ru.mvista.com>");
1360 MODULE_LICENSE("GPL v2");