GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / mmc / host / sdhci-of-esdhc.c
1 /*
2  * Freescale eSDHC controller driver.
3  *
4  * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
5  * 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
16 #include <linux/err.h>
17 #include <linux/io.h>
18 #include <linux/of.h>
19 #include <linux/of_address.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/sys_soc.h>
23 #include <linux/clk.h>
24 #include <linux/ktime.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/mmc/host.h>
27 #include "sdhci-pltfm.h"
28 #include "sdhci-esdhc.h"
29
30 #define VENDOR_V_22     0x12
31 #define VENDOR_V_23     0x13
32
33 struct sdhci_esdhc {
34         u8 vendor_ver;
35         u8 spec_ver;
36         bool quirk_incorrect_hostver;
37         unsigned int peripheral_clock;
38 };
39
40 /**
41  * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
42  *                     to make it compatible with SD spec.
43  *
44  * @host: pointer to sdhci_host
45  * @spec_reg: SD spec register address
46  * @value: 32bit eSDHC register value on spec_reg address
47  *
48  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
49  * registers are 32 bits. There are differences in register size, register
50  * address, register function, bit position and function between eSDHC spec
51  * and SD spec.
52  *
53  * Return a fixed up register value
54  */
55 static u32 esdhc_readl_fixup(struct sdhci_host *host,
56                                      int spec_reg, u32 value)
57 {
58         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
59         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
60         u32 ret;
61
62         /*
63          * The bit of ADMA flag in eSDHC is not compatible with standard
64          * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
65          * supported by eSDHC.
66          * And for many FSL eSDHC controller, the reset value of field
67          * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
68          * only these vendor version is greater than 2.2/0x12 support ADMA.
69          */
70         if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
71                 if (esdhc->vendor_ver > VENDOR_V_22) {
72                         ret = value | SDHCI_CAN_DO_ADMA2;
73                         return ret;
74                 }
75         }
76         /*
77          * The DAT[3:0] line signal levels and the CMD line signal level are
78          * not compatible with standard SDHC register. The line signal levels
79          * DAT[7:0] are at bits 31:24 and the command line signal level is at
80          * bit 23. All other bits are the same as in the standard SDHC
81          * register.
82          */
83         if (spec_reg == SDHCI_PRESENT_STATE) {
84                 ret = value & 0x000fffff;
85                 ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
86                 ret |= (value << 1) & SDHCI_CMD_LVL;
87                 return ret;
88         }
89
90         /*
91          * DTS properties of mmc host are used to enable each speed mode
92          * according to soc and board capability. So clean up
93          * SDR50/SDR104/DDR50 support bits here.
94          */
95         if (spec_reg == SDHCI_CAPABILITIES_1) {
96                 ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
97                                 SDHCI_SUPPORT_DDR50);
98                 return ret;
99         }
100
101         ret = value;
102         return ret;
103 }
104
105 static u16 esdhc_readw_fixup(struct sdhci_host *host,
106                                      int spec_reg, u32 value)
107 {
108         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
109         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
110         u16 ret;
111         int shift = (spec_reg & 0x2) * 8;
112
113         if (spec_reg == SDHCI_HOST_VERSION)
114                 ret = value & 0xffff;
115         else
116                 ret = (value >> shift) & 0xffff;
117         /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
118          * vendor version and spec version information.
119          */
120         if ((spec_reg == SDHCI_HOST_VERSION) &&
121             (esdhc->quirk_incorrect_hostver))
122                 ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
123         return ret;
124 }
125
126 static u8 esdhc_readb_fixup(struct sdhci_host *host,
127                                      int spec_reg, u32 value)
128 {
129         u8 ret;
130         u8 dma_bits;
131         int shift = (spec_reg & 0x3) * 8;
132
133         ret = (value >> shift) & 0xff;
134
135         /*
136          * "DMA select" locates at offset 0x28 in SD specification, but on
137          * P5020 or P3041, it locates at 0x29.
138          */
139         if (spec_reg == SDHCI_HOST_CONTROL) {
140                 /* DMA select is 22,23 bits in Protocol Control Register */
141                 dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
142                 /* fixup the result */
143                 ret &= ~SDHCI_CTRL_DMA_MASK;
144                 ret |= dma_bits;
145         }
146         return ret;
147 }
148
149 /**
150  * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
151  *                      written into eSDHC register.
152  *
153  * @host: pointer to sdhci_host
154  * @spec_reg: SD spec register address
155  * @value: 8/16/32bit SD spec register value that would be written
156  * @old_value: 32bit eSDHC register value on spec_reg address
157  *
158  * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
159  * registers are 32 bits. There are differences in register size, register
160  * address, register function, bit position and function between eSDHC spec
161  * and SD spec.
162  *
163  * Return a fixed up register value
164  */
165 static u32 esdhc_writel_fixup(struct sdhci_host *host,
166                                      int spec_reg, u32 value, u32 old_value)
167 {
168         u32 ret;
169
170         /*
171          * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
172          * when SYSCTL[RSTD] is set for some special operations.
173          * No any impact on other operation.
174          */
175         if (spec_reg == SDHCI_INT_ENABLE)
176                 ret = value | SDHCI_INT_BLK_GAP;
177         else
178                 ret = value;
179
180         return ret;
181 }
182
183 static u32 esdhc_writew_fixup(struct sdhci_host *host,
184                                      int spec_reg, u16 value, u32 old_value)
185 {
186         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
187         int shift = (spec_reg & 0x2) * 8;
188         u32 ret;
189
190         switch (spec_reg) {
191         case SDHCI_TRANSFER_MODE:
192                 /*
193                  * Postpone this write, we must do it together with a
194                  * command write that is down below. Return old value.
195                  */
196                 pltfm_host->xfer_mode_shadow = value;
197                 return old_value;
198         case SDHCI_COMMAND:
199                 ret = (value << 16) | pltfm_host->xfer_mode_shadow;
200                 return ret;
201         }
202
203         ret = old_value & (~(0xffff << shift));
204         ret |= (value << shift);
205
206         if (spec_reg == SDHCI_BLOCK_SIZE) {
207                 /*
208                  * Two last DMA bits are reserved, and first one is used for
209                  * non-standard blksz of 4096 bytes that we don't support
210                  * yet. So clear the DMA boundary bits.
211                  */
212                 ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
213         }
214         return ret;
215 }
216
217 static u32 esdhc_writeb_fixup(struct sdhci_host *host,
218                                      int spec_reg, u8 value, u32 old_value)
219 {
220         u32 ret;
221         u32 dma_bits;
222         u8 tmp;
223         int shift = (spec_reg & 0x3) * 8;
224
225         /*
226          * eSDHC doesn't have a standard power control register, so we do
227          * nothing here to avoid incorrect operation.
228          */
229         if (spec_reg == SDHCI_POWER_CONTROL)
230                 return old_value;
231         /*
232          * "DMA select" location is offset 0x28 in SD specification, but on
233          * P5020 or P3041, it's located at 0x29.
234          */
235         if (spec_reg == SDHCI_HOST_CONTROL) {
236                 /*
237                  * If host control register is not standard, exit
238                  * this function
239                  */
240                 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
241                         return old_value;
242
243                 /* DMA select is 22,23 bits in Protocol Control Register */
244                 dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
245                 ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
246                 tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
247                       (old_value & SDHCI_CTRL_DMA_MASK);
248                 ret = (ret & (~0xff)) | tmp;
249
250                 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
251                 ret &= ~ESDHC_HOST_CONTROL_RES;
252                 return ret;
253         }
254
255         ret = (old_value & (~(0xff << shift))) | (value << shift);
256         return ret;
257 }
258
259 static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
260 {
261         u32 ret;
262         u32 value;
263
264         if (reg == SDHCI_CAPABILITIES_1)
265                 value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
266         else
267                 value = ioread32be(host->ioaddr + reg);
268
269         ret = esdhc_readl_fixup(host, reg, value);
270
271         return ret;
272 }
273
274 static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
275 {
276         u32 ret;
277         u32 value;
278
279         if (reg == SDHCI_CAPABILITIES_1)
280                 value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
281         else
282                 value = ioread32(host->ioaddr + reg);
283
284         ret = esdhc_readl_fixup(host, reg, value);
285
286         return ret;
287 }
288
289 static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
290 {
291         u16 ret;
292         u32 value;
293         int base = reg & ~0x3;
294
295         value = ioread32be(host->ioaddr + base);
296         ret = esdhc_readw_fixup(host, reg, value);
297         return ret;
298 }
299
300 static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
301 {
302         u16 ret;
303         u32 value;
304         int base = reg & ~0x3;
305
306         value = ioread32(host->ioaddr + base);
307         ret = esdhc_readw_fixup(host, reg, value);
308         return ret;
309 }
310
311 static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
312 {
313         u8 ret;
314         u32 value;
315         int base = reg & ~0x3;
316
317         value = ioread32be(host->ioaddr + base);
318         ret = esdhc_readb_fixup(host, reg, value);
319         return ret;
320 }
321
322 static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
323 {
324         u8 ret;
325         u32 value;
326         int base = reg & ~0x3;
327
328         value = ioread32(host->ioaddr + base);
329         ret = esdhc_readb_fixup(host, reg, value);
330         return ret;
331 }
332
333 static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
334 {
335         u32 value;
336
337         value = esdhc_writel_fixup(host, reg, val, 0);
338         iowrite32be(value, host->ioaddr + reg);
339 }
340
341 static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
342 {
343         u32 value;
344
345         value = esdhc_writel_fixup(host, reg, val, 0);
346         iowrite32(value, host->ioaddr + reg);
347 }
348
349 static void esdhc_be_writew(struct sdhci_host *host, u16 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_writew_fixup(host, reg, val, value);
357         if (reg != SDHCI_TRANSFER_MODE)
358                 iowrite32be(ret, host->ioaddr + base);
359 }
360
361 static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
362 {
363         int base = reg & ~0x3;
364         u32 value;
365         u32 ret;
366
367         value = ioread32(host->ioaddr + base);
368         ret = esdhc_writew_fixup(host, reg, val, value);
369         if (reg != SDHCI_TRANSFER_MODE)
370                 iowrite32(ret, host->ioaddr + base);
371 }
372
373 static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
374 {
375         int base = reg & ~0x3;
376         u32 value;
377         u32 ret;
378
379         value = ioread32be(host->ioaddr + base);
380         ret = esdhc_writeb_fixup(host, reg, val, value);
381         iowrite32be(ret, host->ioaddr + base);
382 }
383
384 static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
385 {
386         int base = reg & ~0x3;
387         u32 value;
388         u32 ret;
389
390         value = ioread32(host->ioaddr + base);
391         ret = esdhc_writeb_fixup(host, reg, val, value);
392         iowrite32(ret, host->ioaddr + base);
393 }
394
395 /*
396  * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
397  * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
398  * and Block Gap Event(IRQSTAT[BGE]) are also set.
399  * For Continue, apply soft reset for data(SYSCTL[RSTD]);
400  * and re-issue the entire read transaction from beginning.
401  */
402 static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
403 {
404         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
405         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
406         bool applicable;
407         dma_addr_t dmastart;
408         dma_addr_t dmanow;
409
410         applicable = (intmask & SDHCI_INT_DATA_END) &&
411                      (intmask & SDHCI_INT_BLK_GAP) &&
412                      (esdhc->vendor_ver == VENDOR_V_23);
413         if (!applicable)
414                 return;
415
416         host->data->error = 0;
417         dmastart = sg_dma_address(host->data->sg);
418         dmanow = dmastart + host->data->bytes_xfered;
419         /*
420          * Force update to the next DMA block boundary.
421          */
422         dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
423                 SDHCI_DEFAULT_BOUNDARY_SIZE;
424         host->data->bytes_xfered = dmanow - dmastart;
425         sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
426 }
427
428 static int esdhc_of_enable_dma(struct sdhci_host *host)
429 {
430         int ret;
431         u32 value;
432         struct device *dev = mmc_dev(host->mmc);
433
434         if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
435             of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc")) {
436                 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
437                 if (ret)
438                         return ret;
439         }
440
441         value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
442
443         if (of_dma_is_coherent(dev->of_node))
444                 value |= ESDHC_DMA_SNOOP;
445         else
446                 value &= ~ESDHC_DMA_SNOOP;
447
448         sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
449         return 0;
450 }
451
452 static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
453 {
454         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
455         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
456
457         if (esdhc->peripheral_clock)
458                 return esdhc->peripheral_clock;
459         else
460                 return pltfm_host->clock;
461 }
462
463 static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
464 {
465         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
466         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
467         unsigned int clock;
468
469         if (esdhc->peripheral_clock)
470                 clock = esdhc->peripheral_clock;
471         else
472                 clock = pltfm_host->clock;
473         return clock / 256 / 16;
474 }
475
476 static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
477 {
478         u32 val;
479         ktime_t timeout;
480
481         val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
482
483         if (enable)
484                 val |= ESDHC_CLOCK_SDCLKEN;
485         else
486                 val &= ~ESDHC_CLOCK_SDCLKEN;
487
488         sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
489
490         /* Wait max 20 ms */
491         timeout = ktime_add_ms(ktime_get(), 20);
492         val = ESDHC_CLOCK_STABLE;
493         while  (1) {
494                 bool timedout = ktime_after(ktime_get(), timeout);
495
496                 if (sdhci_readl(host, ESDHC_PRSSTAT) & val)
497                         break;
498                 if (timedout) {
499                         pr_err("%s: Internal clock never stabilised.\n",
500                                 mmc_hostname(host->mmc));
501                         break;
502                 }
503                 udelay(10);
504         }
505 }
506
507 static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
508 {
509         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
510         struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
511         int pre_div = 1;
512         int div = 1;
513         ktime_t timeout;
514         u32 temp;
515
516         host->mmc->actual_clock = 0;
517
518         if (clock == 0) {
519                 esdhc_clock_enable(host, false);
520                 return;
521         }
522
523         /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
524         if (esdhc->vendor_ver < VENDOR_V_23)
525                 pre_div = 2;
526
527         /*
528          * Limit SD clock to 167MHz for ls1046a according to its datasheet
529          */
530         if (clock > 167000000 &&
531             of_find_compatible_node(NULL, NULL, "fsl,ls1046a-esdhc"))
532                 clock = 167000000;
533
534         /*
535          * Limit SD clock to 125MHz for ls1012a according to its datasheet
536          */
537         if (clock > 125000000 &&
538             of_find_compatible_node(NULL, NULL, "fsl,ls1012a-esdhc"))
539                 clock = 125000000;
540
541         /* Workaround to reduce the clock frequency for p1010 esdhc */
542         if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) {
543                 if (clock > 20000000)
544                         clock -= 5000000;
545                 if (clock > 40000000)
546                         clock -= 5000000;
547         }
548
549         temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
550         temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
551                   ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
552         sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
553
554         while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
555                 pre_div *= 2;
556
557         while (host->max_clk / pre_div / div > clock && div < 16)
558                 div++;
559
560         dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
561                 clock, host->max_clk / pre_div / div);
562         host->mmc->actual_clock = host->max_clk / pre_div / div;
563         pre_div >>= 1;
564         div--;
565
566         temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
567         temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
568                 | (div << ESDHC_DIVIDER_SHIFT)
569                 | (pre_div << ESDHC_PREDIV_SHIFT));
570         sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
571
572         /* Wait max 20 ms */
573         timeout = ktime_add_ms(ktime_get(), 20);
574         while (1) {
575                 bool timedout = ktime_after(ktime_get(), timeout);
576
577                 if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
578                         break;
579                 if (timedout) {
580                         pr_err("%s: Internal clock never stabilised.\n",
581                                 mmc_hostname(host->mmc));
582                         return;
583                 }
584                 udelay(10);
585         }
586
587         temp |= ESDHC_CLOCK_SDCLKEN;
588         sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
589 }
590
591 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
592 {
593         u32 ctrl;
594
595         ctrl = sdhci_readl(host, ESDHC_PROCTL);
596         ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
597         switch (width) {
598         case MMC_BUS_WIDTH_8:
599                 ctrl |= ESDHC_CTRL_8BITBUS;
600                 break;
601
602         case MMC_BUS_WIDTH_4:
603                 ctrl |= ESDHC_CTRL_4BITBUS;
604                 break;
605
606         default:
607                 break;
608         }
609
610         sdhci_writel(host, ctrl, ESDHC_PROCTL);
611 }
612
613 static void esdhc_reset(struct sdhci_host *host, u8 mask)
614 {
615         u32 val;
616
617         sdhci_reset(host, mask);
618
619         sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
620         sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
621
622         if (mask & SDHCI_RESET_ALL) {
623                 val = sdhci_readl(host, ESDHC_TBCTL);
624                 val &= ~ESDHC_TB_EN;
625                 sdhci_writel(host, val, ESDHC_TBCTL);
626         }
627 }
628
629 /* The SCFG, Supplemental Configuration Unit, provides SoC specific
630  * configuration and status registers for the device. There is a
631  * SDHC IO VSEL control register on SCFG for some platforms. It's
632  * used to support SDHC IO voltage switching.
633  */
634 static const struct of_device_id scfg_device_ids[] = {
635         { .compatible = "fsl,t1040-scfg", },
636         { .compatible = "fsl,ls1012a-scfg", },
637         { .compatible = "fsl,ls1046a-scfg", },
638         {}
639 };
640
641 /* SDHC IO VSEL control register definition */
642 #define SCFG_SDHCIOVSELCR       0x408
643 #define SDHCIOVSELCR_TGLEN      0x80000000
644 #define SDHCIOVSELCR_VSELVAL    0x60000000
645 #define SDHCIOVSELCR_SDHC_VS    0x00000001
646
647 static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
648                                        struct mmc_ios *ios)
649 {
650         struct sdhci_host *host = mmc_priv(mmc);
651         struct device_node *scfg_node;
652         void __iomem *scfg_base = NULL;
653         u32 sdhciovselcr;
654         u32 val;
655
656         /*
657          * Signal Voltage Switching is only applicable for Host Controllers
658          * v3.00 and above.
659          */
660         if (host->version < SDHCI_SPEC_300)
661                 return 0;
662
663         val = sdhci_readl(host, ESDHC_PROCTL);
664
665         switch (ios->signal_voltage) {
666         case MMC_SIGNAL_VOLTAGE_330:
667                 val &= ~ESDHC_VOLT_SEL;
668                 sdhci_writel(host, val, ESDHC_PROCTL);
669                 return 0;
670         case MMC_SIGNAL_VOLTAGE_180:
671                 scfg_node = of_find_matching_node(NULL, scfg_device_ids);
672                 if (scfg_node)
673                         scfg_base = of_iomap(scfg_node, 0);
674                 if (scfg_base) {
675                         sdhciovselcr = SDHCIOVSELCR_TGLEN |
676                                        SDHCIOVSELCR_VSELVAL;
677                         iowrite32be(sdhciovselcr,
678                                 scfg_base + SCFG_SDHCIOVSELCR);
679
680                         val |= ESDHC_VOLT_SEL;
681                         sdhci_writel(host, val, ESDHC_PROCTL);
682                         mdelay(5);
683
684                         sdhciovselcr = SDHCIOVSELCR_TGLEN |
685                                        SDHCIOVSELCR_SDHC_VS;
686                         iowrite32be(sdhciovselcr,
687                                 scfg_base + SCFG_SDHCIOVSELCR);
688                         iounmap(scfg_base);
689                 } else {
690                         val |= ESDHC_VOLT_SEL;
691                         sdhci_writel(host, val, ESDHC_PROCTL);
692                 }
693                 return 0;
694         default:
695                 return 0;
696         }
697 }
698
699 static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
700 {
701         struct sdhci_host *host = mmc_priv(mmc);
702         u32 val;
703
704         /* Use tuning block for tuning procedure */
705         esdhc_clock_enable(host, false);
706         val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
707         val |= ESDHC_FLUSH_ASYNC_FIFO;
708         sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
709
710         val = sdhci_readl(host, ESDHC_TBCTL);
711         val |= ESDHC_TB_EN;
712         sdhci_writel(host, val, ESDHC_TBCTL);
713         esdhc_clock_enable(host, true);
714
715         return sdhci_execute_tuning(mmc, opcode);
716 }
717
718 #ifdef CONFIG_PM_SLEEP
719 static u32 esdhc_proctl;
720 static int esdhc_of_suspend(struct device *dev)
721 {
722         struct sdhci_host *host = dev_get_drvdata(dev);
723
724         esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
725
726         if (host->tuning_mode != SDHCI_TUNING_MODE_3)
727                 mmc_retune_needed(host->mmc);
728
729         return sdhci_suspend_host(host);
730 }
731
732 static int esdhc_of_resume(struct device *dev)
733 {
734         struct sdhci_host *host = dev_get_drvdata(dev);
735         int ret = sdhci_resume_host(host);
736
737         if (ret == 0) {
738                 /* Isn't this already done by sdhci_resume_host() ? --rmk */
739                 esdhc_of_enable_dma(host);
740                 sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
741         }
742         return ret;
743 }
744 #endif
745
746 static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
747                         esdhc_of_suspend,
748                         esdhc_of_resume);
749
750 static const struct sdhci_ops sdhci_esdhc_be_ops = {
751         .read_l = esdhc_be_readl,
752         .read_w = esdhc_be_readw,
753         .read_b = esdhc_be_readb,
754         .write_l = esdhc_be_writel,
755         .write_w = esdhc_be_writew,
756         .write_b = esdhc_be_writeb,
757         .set_clock = esdhc_of_set_clock,
758         .enable_dma = esdhc_of_enable_dma,
759         .get_max_clock = esdhc_of_get_max_clock,
760         .get_min_clock = esdhc_of_get_min_clock,
761         .adma_workaround = esdhc_of_adma_workaround,
762         .set_bus_width = esdhc_pltfm_set_bus_width,
763         .reset = esdhc_reset,
764         .set_uhs_signaling = sdhci_set_uhs_signaling,
765 };
766
767 static const struct sdhci_ops sdhci_esdhc_le_ops = {
768         .read_l = esdhc_le_readl,
769         .read_w = esdhc_le_readw,
770         .read_b = esdhc_le_readb,
771         .write_l = esdhc_le_writel,
772         .write_w = esdhc_le_writew,
773         .write_b = esdhc_le_writeb,
774         .set_clock = esdhc_of_set_clock,
775         .enable_dma = esdhc_of_enable_dma,
776         .get_max_clock = esdhc_of_get_max_clock,
777         .get_min_clock = esdhc_of_get_min_clock,
778         .adma_workaround = esdhc_of_adma_workaround,
779         .set_bus_width = esdhc_pltfm_set_bus_width,
780         .reset = esdhc_reset,
781         .set_uhs_signaling = sdhci_set_uhs_signaling,
782 };
783
784 static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
785         .quirks = ESDHC_DEFAULT_QUIRKS |
786 #ifdef CONFIG_PPC
787                   SDHCI_QUIRK_BROKEN_CARD_DETECTION |
788 #endif
789                   SDHCI_QUIRK_NO_CARD_NO_RESET |
790                   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
791         .ops = &sdhci_esdhc_be_ops,
792 };
793
794 static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
795         .quirks = ESDHC_DEFAULT_QUIRKS |
796                   SDHCI_QUIRK_NO_CARD_NO_RESET |
797                   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
798         .ops = &sdhci_esdhc_le_ops,
799 };
800
801 static struct soc_device_attribute soc_incorrect_hostver[] = {
802         { .family = "QorIQ T4240", .revision = "1.0", },
803         { .family = "QorIQ T4240", .revision = "2.0", },
804         { },
805 };
806
807 static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
808 {
809         struct sdhci_pltfm_host *pltfm_host;
810         struct sdhci_esdhc *esdhc;
811         struct device_node *np;
812         struct clk *clk;
813         u32 val;
814         u16 host_ver;
815
816         pltfm_host = sdhci_priv(host);
817         esdhc = sdhci_pltfm_priv(pltfm_host);
818
819         host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
820         esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
821                              SDHCI_VENDOR_VER_SHIFT;
822         esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
823         if (soc_device_match(soc_incorrect_hostver))
824                 esdhc->quirk_incorrect_hostver = true;
825         else
826                 esdhc->quirk_incorrect_hostver = false;
827
828         np = pdev->dev.of_node;
829         clk = of_clk_get(np, 0);
830         if (!IS_ERR(clk)) {
831                 /*
832                  * esdhc->peripheral_clock would be assigned with a value
833                  * which is eSDHC base clock when use periperal clock.
834                  * For ls1046a, the clock value got by common clk API is
835                  * peripheral clock while the eSDHC base clock is 1/2
836                  * peripheral clock.
837                  */
838                 if (of_device_is_compatible(np, "fsl,ls1046a-esdhc"))
839                         esdhc->peripheral_clock = clk_get_rate(clk) / 2;
840                 else
841                         esdhc->peripheral_clock = clk_get_rate(clk);
842
843                 clk_put(clk);
844         }
845
846         if (esdhc->peripheral_clock) {
847                 esdhc_clock_enable(host, false);
848                 val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
849                 val |= ESDHC_PERIPHERAL_CLK_SEL;
850                 sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
851                 esdhc_clock_enable(host, true);
852         }
853 }
854
855 static int sdhci_esdhc_probe(struct platform_device *pdev)
856 {
857         struct sdhci_host *host;
858         struct device_node *np;
859         struct sdhci_pltfm_host *pltfm_host;
860         struct sdhci_esdhc *esdhc;
861         int ret;
862
863         np = pdev->dev.of_node;
864
865         if (of_property_read_bool(np, "little-endian"))
866                 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
867                                         sizeof(struct sdhci_esdhc));
868         else
869                 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
870                                         sizeof(struct sdhci_esdhc));
871
872         if (IS_ERR(host))
873                 return PTR_ERR(host);
874
875         host->mmc_host_ops.start_signal_voltage_switch =
876                 esdhc_signal_voltage_switch;
877         host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
878         host->tuning_delay = 1;
879
880         esdhc_init(pdev, host);
881
882         sdhci_get_of_property(pdev);
883
884         pltfm_host = sdhci_priv(host);
885         esdhc = sdhci_pltfm_priv(pltfm_host);
886         if (esdhc->vendor_ver == VENDOR_V_22)
887                 host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
888
889         if (esdhc->vendor_ver > VENDOR_V_22)
890                 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
891
892         if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) {
893                 host->quirks |= SDHCI_QUIRK_RESET_AFTER_REQUEST;
894                 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
895         }
896
897         if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
898             of_device_is_compatible(np, "fsl,p5020-esdhc") ||
899             of_device_is_compatible(np, "fsl,p4080-esdhc") ||
900             of_device_is_compatible(np, "fsl,p1020-esdhc") ||
901             of_device_is_compatible(np, "fsl,t1040-esdhc"))
902                 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
903
904         if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
905                 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
906
907         if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
908                 /*
909                  * Freescale messed up with P2020 as it has a non-standard
910                  * host control register
911                  */
912                 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
913         }
914
915         /* call to generic mmc_of_parse to support additional capabilities */
916         ret = mmc_of_parse(host->mmc);
917         if (ret)
918                 goto err;
919
920         mmc_of_parse_voltage(np, &host->ocr_mask);
921
922         ret = sdhci_add_host(host);
923         if (ret)
924                 goto err;
925
926         return 0;
927  err:
928         sdhci_pltfm_free(pdev);
929         return ret;
930 }
931
932 static const struct of_device_id sdhci_esdhc_of_match[] = {
933         { .compatible = "fsl,mpc8379-esdhc" },
934         { .compatible = "fsl,mpc8536-esdhc" },
935         { .compatible = "fsl,esdhc" },
936         { }
937 };
938 MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
939
940 static struct platform_driver sdhci_esdhc_driver = {
941         .driver = {
942                 .name = "sdhci-esdhc",
943                 .of_match_table = sdhci_esdhc_of_match,
944                 .pm = &esdhc_of_dev_pm_ops,
945         },
946         .probe = sdhci_esdhc_probe,
947         .remove = sdhci_pltfm_unregister,
948 };
949
950 module_platform_driver(sdhci_esdhc_driver);
951
952 MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
953 MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
954               "Anton Vorontsov <avorontsov@ru.mvista.com>");
955 MODULE_LICENSE("GPL v2");