1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale eSDHC controller driver.
5 * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc.
6 * Copyright (c) 2009 MontaVista Software, Inc.
8 * Authors: Xiaobo Xie <X.Xie@freescale.com>
9 * Anton Vorontsov <avorontsov@ru.mvista.com>
12 #include <linux/err.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"
27 #define VENDOR_V_22 0x12
28 #define VENDOR_V_23 0x13
30 #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1)
32 struct esdhc_clk_fixup {
33 const unsigned int sd_dflt_max_clk;
34 const unsigned int max_clk[MMC_TIMING_NUM];
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,
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,
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,
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,
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" },
72 MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
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;
84 unsigned int peripheral_clock;
85 const struct esdhc_clk_fixup *clk_fixup;
90 * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
91 * to make it compatible with SD spec.
93 * @host: pointer to sdhci_host
94 * @spec_reg: SD spec register address
95 * @value: 32bit eSDHC register value on spec_reg address
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
102 * Return a fixed up register value
104 static u32 esdhc_readl_fixup(struct sdhci_host *host,
105 int spec_reg, u32 value)
107 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
108 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
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.
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;
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
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;
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.
144 if (spec_reg == SDHCI_CAPABILITIES_1) {
145 ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
146 SDHCI_SUPPORT_DDR50);
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.
157 if ((spec_reg == SDHCI_PRESENT_STATE) &&
158 (esdhc->quirk_ignore_data_inhibit == true)) {
159 ret = value & ~SDHCI_DATA_INHIBIT;
167 static u16 esdhc_readw_fixup(struct sdhci_host *host,
168 int spec_reg, u32 value)
170 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
171 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
173 int shift = (spec_reg & 0x2) * 8;
175 if (spec_reg == SDHCI_HOST_VERSION)
176 ret = value & 0xffff;
178 ret = (value >> shift) & 0xffff;
179 /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
180 * vendor version and spec version information.
182 if ((spec_reg == SDHCI_HOST_VERSION) &&
183 (esdhc->quirk_incorrect_hostver))
184 ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
188 static u8 esdhc_readb_fixup(struct sdhci_host *host,
189 int spec_reg, u32 value)
193 int shift = (spec_reg & 0x3) * 8;
195 ret = (value >> shift) & 0xff;
198 * "DMA select" locates at offset 0x28 in SD specification, but on
199 * P5020 or P3041, it locates at 0x29.
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;
212 * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
213 * written into eSDHC register.
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
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
225 * Return a fixed up register value
227 static u32 esdhc_writel_fixup(struct sdhci_host *host,
228 int spec_reg, u32 value, u32 old_value)
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.
237 if (spec_reg == SDHCI_INT_ENABLE)
238 ret = value | SDHCI_INT_BLK_GAP;
245 static u32 esdhc_writew_fixup(struct sdhci_host *host,
246 int spec_reg, u16 value, u32 old_value)
248 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
249 int shift = (spec_reg & 0x2) * 8;
253 case SDHCI_TRANSFER_MODE:
255 * Postpone this write, we must do it together with a
256 * command write that is down below. Return old value.
258 pltfm_host->xfer_mode_shadow = value;
261 ret = (value << 16) | pltfm_host->xfer_mode_shadow;
265 ret = old_value & (~(0xffff << shift));
266 ret |= (value << shift);
268 if (spec_reg == SDHCI_BLOCK_SIZE) {
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.
274 ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
279 static u32 esdhc_writeb_fixup(struct sdhci_host *host,
280 int spec_reg, u8 value, u32 old_value)
285 int shift = (spec_reg & 0x3) * 8;
288 * eSDHC doesn't have a standard power control register, so we do
289 * nothing here to avoid incorrect operation.
291 if (spec_reg == SDHCI_POWER_CONTROL)
294 * "DMA select" location is offset 0x28 in SD specification, but on
295 * P5020 or P3041, it's located at 0x29.
297 if (spec_reg == SDHCI_HOST_CONTROL) {
299 * If host control register is not standard, exit
302 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
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;
312 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
313 ret &= ~ESDHC_HOST_CONTROL_RES;
317 ret = (old_value & (~(0xff << shift))) | (value << shift);
321 static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
326 if (reg == SDHCI_CAPABILITIES_1)
327 value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
329 value = ioread32be(host->ioaddr + reg);
331 ret = esdhc_readl_fixup(host, reg, value);
336 static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
341 if (reg == SDHCI_CAPABILITIES_1)
342 value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
344 value = ioread32(host->ioaddr + reg);
346 ret = esdhc_readl_fixup(host, reg, value);
351 static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
355 int base = reg & ~0x3;
357 value = ioread32be(host->ioaddr + base);
358 ret = esdhc_readw_fixup(host, reg, value);
362 static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
366 int base = reg & ~0x3;
368 value = ioread32(host->ioaddr + base);
369 ret = esdhc_readw_fixup(host, reg, value);
373 static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
377 int base = reg & ~0x3;
379 value = ioread32be(host->ioaddr + base);
380 ret = esdhc_readb_fixup(host, reg, value);
384 static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
388 int base = reg & ~0x3;
390 value = ioread32(host->ioaddr + base);
391 ret = esdhc_readb_fixup(host, reg, value);
395 static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
399 value = esdhc_writel_fixup(host, reg, val, 0);
400 iowrite32be(value, host->ioaddr + reg);
403 static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
407 value = esdhc_writel_fixup(host, reg, val, 0);
408 iowrite32(value, host->ioaddr + reg);
411 static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
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;
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);
424 /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
425 * 1us later after ESDHC_EXTN is set.
427 if (base == ESDHC_SYSTEM_CONTROL_2) {
428 if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
429 esdhc->in_sw_tuning) {
431 ret |= ESDHC_SMPCLKSEL;
432 iowrite32be(ret, host->ioaddr + base);
437 static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
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;
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);
450 /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
451 * 1us later after ESDHC_EXTN is set.
453 if (base == ESDHC_SYSTEM_CONTROL_2) {
454 if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
455 esdhc->in_sw_tuning) {
457 ret |= ESDHC_SMPCLKSEL;
458 iowrite32(ret, host->ioaddr + base);
463 static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
465 int base = reg & ~0x3;
469 value = ioread32be(host->ioaddr + base);
470 ret = esdhc_writeb_fixup(host, reg, val, value);
471 iowrite32be(ret, host->ioaddr + base);
474 static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
476 int base = reg & ~0x3;
480 value = ioread32(host->ioaddr + base);
481 ret = esdhc_writeb_fixup(host, reg, val, value);
482 iowrite32(ret, host->ioaddr + base);
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.
492 static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
494 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
495 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
500 applicable = (intmask & SDHCI_INT_DATA_END) &&
501 (intmask & SDHCI_INT_BLK_GAP) &&
502 (esdhc->vendor_ver == VENDOR_V_23);
506 host->data->error = 0;
507 dmastart = sg_dma_address(host->data->sg);
508 dmanow = dmastart + host->data->bytes_xfered;
510 * Force update to the next DMA block boundary.
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);
518 static int esdhc_of_enable_dma(struct sdhci_host *host)
521 struct device *dev = mmc_dev(host->mmc);
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));
527 value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
529 if (of_dma_is_coherent(dev->of_node))
530 value |= ESDHC_DMA_SNOOP;
532 value &= ~ESDHC_DMA_SNOOP;
534 sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
538 static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
540 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
541 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
543 if (esdhc->peripheral_clock)
544 return esdhc->peripheral_clock;
546 return pltfm_host->clock;
549 static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
551 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
552 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
555 if (esdhc->peripheral_clock)
556 clock = esdhc->peripheral_clock;
558 clock = pltfm_host->clock;
559 return clock / 256 / 16;
562 static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
567 val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
570 val |= ESDHC_CLOCK_SDCLKEN;
572 val &= ~ESDHC_CLOCK_SDCLKEN;
574 sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
577 timeout = ktime_add_ms(ktime_get(), 20);
578 val = ESDHC_CLOCK_STABLE;
580 bool timedout = ktime_after(ktime_get(), timeout);
582 if (sdhci_readl(host, ESDHC_PRSSTAT) & val)
585 pr_err("%s: Internal clock never stabilised.\n",
586 mmc_hostname(host->mmc));
593 static void esdhc_flush_async_fifo(struct sdhci_host *host)
598 val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
599 val |= ESDHC_FLUSH_ASYNC_FIFO;
600 sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
603 timeout = ktime_add_ms(ktime_get(), 20);
605 bool timedout = ktime_after(ktime_get(), timeout);
607 if (!(sdhci_readl(host, ESDHC_DMA_SYSCTL) &
608 ESDHC_FLUSH_ASYNC_FIFO))
611 pr_err("%s: flushing asynchronous FIFO timeout.\n",
612 mmc_hostname(host->mmc));
615 usleep_range(10, 20);
619 static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
621 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
622 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
630 host->mmc->actual_clock = 0;
633 esdhc_clock_enable(host, false);
637 /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
638 if (esdhc->vendor_ver < VENDOR_V_23)
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];
647 if (fixup && clock > fixup)
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);
655 while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
658 while (host->max_clk / pre_div / div > clock && div < 16)
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;
669 } else if (division <= 8) {
672 } else if (division <= 12) {
676 pr_warn("%s: using unsupported clock division.\n",
677 mmc_hostname(host->mmc));
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;
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);
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);
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);
710 esdhc_clock_enable(host, false);
711 esdhc_flush_async_fifo(host);
715 timeout = ktime_add_ms(ktime_get(), 20);
717 bool timedout = ktime_after(ktime_get(), timeout);
719 if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
722 pr_err("%s: Internal clock never stabilised.\n",
723 mmc_hostname(host->mmc));
729 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
730 temp |= ESDHC_CLOCK_SDCLKEN;
731 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
734 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
738 ctrl = sdhci_readl(host, ESDHC_PROCTL);
739 ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
741 case MMC_BUS_WIDTH_8:
742 ctrl |= ESDHC_CTRL_8BITBUS;
745 case MMC_BUS_WIDTH_4:
746 ctrl |= ESDHC_CTRL_4BITBUS;
753 sdhci_writel(host, ctrl, ESDHC_PROCTL);
756 static void esdhc_reset(struct sdhci_host *host, u8 mask)
758 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
759 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
762 sdhci_reset(host, mask);
764 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
765 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
767 if (mask & SDHCI_RESET_ALL) {
768 val = sdhci_readl(host, ESDHC_TBCTL);
770 sdhci_writel(host, val, ESDHC_TBCTL);
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);
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.
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", },
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
798 static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
801 struct sdhci_host *host = mmc_priv(mmc);
802 struct device_node *scfg_node;
803 void __iomem *scfg_base = NULL;
808 * Signal Voltage Switching is only applicable for Host Controllers
811 if (host->version < SDHCI_SPEC_300)
814 val = sdhci_readl(host, ESDHC_PROCTL);
816 switch (ios->signal_voltage) {
817 case MMC_SIGNAL_VOLTAGE_330:
818 val &= ~ESDHC_VOLT_SEL;
819 sdhci_writel(host, val, ESDHC_PROCTL);
821 case MMC_SIGNAL_VOLTAGE_180:
822 scfg_node = of_find_matching_node(NULL, scfg_device_ids);
824 scfg_base = of_iomap(scfg_node, 0);
826 sdhciovselcr = SDHCIOVSELCR_TGLEN |
827 SDHCIOVSELCR_VSELVAL;
828 iowrite32be(sdhciovselcr,
829 scfg_base + SCFG_SDHCIOVSELCR);
831 val |= ESDHC_VOLT_SEL;
832 sdhci_writel(host, val, ESDHC_PROCTL);
835 sdhciovselcr = SDHCIOVSELCR_TGLEN |
836 SDHCIOVSELCR_SDHC_VS;
837 iowrite32be(sdhciovselcr,
838 scfg_base + SCFG_SDHCIOVSELCR);
841 val |= ESDHC_VOLT_SEL;
842 sdhci_writel(host, val, ESDHC_PROCTL);
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", },
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", },
868 static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable)
872 esdhc_clock_enable(host, false);
873 esdhc_flush_async_fifo(host);
875 val = sdhci_readl(host, ESDHC_TBCTL);
880 sdhci_writel(host, val, ESDHC_TBCTL);
882 esdhc_clock_enable(host, true);
885 static void esdhc_prepare_sw_tuning(struct sdhci_host *host, u8 *window_start,
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;
893 if (esdhc->quirk_tuning_erratum_type1) {
894 *window_start = 5 * esdhc->div_ratio;
895 *window_end = 3 * esdhc->div_ratio;
899 /* Write TBCTL[11:8]=4'h8 */
900 val = sdhci_readl(host, ESDHC_TBCTL);
903 sdhci_writel(host, val, ESDHC_TBCTL);
907 /* Read TBCTL[31:0] register and rewrite again */
908 val = sdhci_readl(host, ESDHC_TBCTL);
909 sdhci_writel(host, val, ESDHC_TBCTL);
913 /* Read the TBSTAT[31:0] register twice */
914 val = sdhci_readl(host, ESDHC_TBSTAT);
915 val = sdhci_readl(host, ESDHC_TBSTAT);
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);
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.
927 tbstat_7_0 = val & 0xff;
928 tbstat_15_8 = (val >> 8) & 0xff;
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;
934 *window_start = 5 * esdhc->div_ratio;
935 *window_end = 3 * esdhc->div_ratio;
939 static int esdhc_execute_sw_tuning(struct mmc_host *mmc, u32 opcode,
940 u8 window_start, u8 window_end)
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);
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);
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);
960 esdhc->in_sw_tuning = true;
961 ret = sdhci_execute_tuning(mmc, opcode);
962 esdhc->in_sw_tuning = false;
966 static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
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;
977 /* For tuning mode, the sd clock divisor value
978 * must be larger than 3 according to reference manual.
980 clk = esdhc->peripheral_clock / 3;
981 if (host->clock > clk)
982 esdhc_of_set_clock(host, clk);
984 esdhc_tuning_block_enable(host, true);
986 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
989 if (esdhc->quirk_limited_clk_division &&
991 esdhc_of_set_clock(host, host->clock);
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);
999 ret = sdhci_execute_tuning(mmc, opcode);
1003 /* If HW tuning fails and triggers erratum,
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 */
1012 host->flags |= SDHCI_HS400_TUNING;
1013 pr_info("%s: Hold on to use fixed sampling clock. Try SW tuning!\n",
1016 esdhc_prepare_sw_tuning(host, &window_start,
1018 ret = esdhc_execute_sw_tuning(mmc, opcode,
1024 /* Retry both HW/SW tuning with reduced clock. */
1025 ret = host->tuning_err;
1026 if (ret == -EAGAIN && retries) {
1027 /* Recover HS400 tuning flag */
1029 host->flags |= SDHCI_HS400_TUNING;
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",
1041 } while (retries--);
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);
1054 static void esdhc_set_uhs_signaling(struct sdhci_host *host,
1055 unsigned int timing)
1057 if (timing == MMC_TIMING_MMC_HS400)
1058 esdhc_tuning_block_enable(host, true);
1060 sdhci_set_uhs_signaling(host, timing);
1063 static u32 esdhc_irq(struct sdhci_host *host, u32 intmask)
1067 if (of_find_compatible_node(NULL, NULL,
1068 "fsl,p2020-esdhc")) {
1069 command = SDHCI_GET_CMD(sdhci_readw(host,
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,
1082 #ifdef CONFIG_PM_SLEEP
1083 static u32 esdhc_proctl;
1084 static int esdhc_of_suspend(struct device *dev)
1086 struct sdhci_host *host = dev_get_drvdata(dev);
1088 esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
1090 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1091 mmc_retune_needed(host->mmc);
1093 return sdhci_suspend_host(host);
1096 static int esdhc_of_resume(struct device *dev)
1098 struct sdhci_host *host = dev_get_drvdata(dev);
1099 int ret = sdhci_resume_host(host);
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);
1110 static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
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,
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,
1150 static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
1151 .quirks = ESDHC_DEFAULT_QUIRKS |
1153 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
1155 SDHCI_QUIRK_NO_CARD_NO_RESET |
1156 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
1157 .ops = &sdhci_esdhc_be_ops,
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,
1167 static struct soc_device_attribute soc_incorrect_hostver[] = {
1168 { .family = "QorIQ T4240", .revision = "1.0", },
1169 { .family = "QorIQ T4240", .revision = "2.0", },
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", },
1180 static struct soc_device_attribute soc_unreliable_pulse_detection[] = {
1181 { .family = "QorIQ LX2160A", .revision = "1.0", },
1185 static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
1187 const struct of_device_id *match;
1188 struct sdhci_pltfm_host *pltfm_host;
1189 struct sdhci_esdhc *esdhc;
1190 struct device_node *np;
1195 pltfm_host = sdhci_priv(host);
1196 esdhc = sdhci_pltfm_priv(pltfm_host);
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;
1205 esdhc->quirk_incorrect_hostver = false;
1207 if (soc_device_match(soc_fixup_sdhc_clkdivs))
1208 esdhc->quirk_limited_clk_division = true;
1210 esdhc->quirk_limited_clk_division = false;
1212 if (soc_device_match(soc_unreliable_pulse_detection))
1213 esdhc->quirk_unreliable_pulse_detection = true;
1215 esdhc->quirk_unreliable_pulse_detection = false;
1217 match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
1219 esdhc->clk_fixup = match->data;
1220 np = pdev->dev.of_node;
1221 clk = of_clk_get(np, 0);
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.
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;
1235 esdhc->peripheral_clock = clk_get_rate(clk);
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);
1249 static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc)
1251 esdhc_tuning_block_enable(mmc_priv(mmc), false);
1255 static int sdhci_esdhc_probe(struct platform_device *pdev)
1257 struct sdhci_host *host;
1258 struct device_node *np;
1259 struct sdhci_pltfm_host *pltfm_host;
1260 struct sdhci_esdhc *esdhc;
1263 np = pdev->dev.of_node;
1265 if (of_property_read_bool(np, "little-endian"))
1266 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
1267 sizeof(struct sdhci_esdhc));
1269 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
1270 sizeof(struct sdhci_esdhc));
1273 return PTR_ERR(host);
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;
1281 esdhc_init(pdev, host);
1283 sdhci_get_of_property(pdev);
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;
1290 esdhc->quirk_tuning_erratum_type1 = false;
1292 if (soc_device_match(soc_tuning_erratum_type2))
1293 esdhc->quirk_tuning_erratum_type2 = true;
1295 esdhc->quirk_tuning_erratum_type2 = false;
1297 if (esdhc->vendor_ver == VENDOR_V_22)
1298 host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
1300 if (esdhc->vendor_ver > VENDOR_V_22)
1301 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
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;
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;
1315 if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
1316 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1318 esdhc->quirk_ignore_data_inhibit = false;
1319 if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
1321 * Freescale messed up with P2020 as it has a non-standard
1322 * host control register
1324 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
1325 esdhc->quirk_ignore_data_inhibit = true;
1328 /* call to generic mmc_of_parse to support additional capabilities */
1329 ret = mmc_of_parse(host->mmc);
1333 mmc_of_parse_voltage(np, &host->ocr_mask);
1335 ret = sdhci_add_host(host);
1341 sdhci_pltfm_free(pdev);
1345 static struct platform_driver sdhci_esdhc_driver = {
1347 .name = "sdhci-esdhc",
1348 .of_match_table = sdhci_esdhc_of_match,
1349 .pm = &esdhc_of_dev_pm_ops,
1351 .probe = sdhci_esdhc_probe,
1352 .remove = sdhci_pltfm_unregister,
1355 module_platform_driver(sdhci_esdhc_driver);
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");