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;
83 bool quirk_delay_before_data_reset;
85 unsigned int peripheral_clock;
86 const struct esdhc_clk_fixup *clk_fixup;
91 * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register
92 * to make it compatible with SD spec.
94 * @host: pointer to sdhci_host
95 * @spec_reg: SD spec register address
96 * @value: 32bit eSDHC register value on spec_reg address
98 * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
99 * registers are 32 bits. There are differences in register size, register
100 * address, register function, bit position and function between eSDHC spec
103 * Return a fixed up register value
105 static u32 esdhc_readl_fixup(struct sdhci_host *host,
106 int spec_reg, u32 value)
108 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
109 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
113 * The bit of ADMA flag in eSDHC is not compatible with standard
114 * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is
115 * supported by eSDHC.
116 * And for many FSL eSDHC controller, the reset value of field
117 * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA,
118 * only these vendor version is greater than 2.2/0x12 support ADMA.
120 if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) {
121 if (esdhc->vendor_ver > VENDOR_V_22) {
122 ret = value | SDHCI_CAN_DO_ADMA2;
127 * The DAT[3:0] line signal levels and the CMD line signal level are
128 * not compatible with standard SDHC register. The line signal levels
129 * DAT[7:0] are at bits 31:24 and the command line signal level is at
130 * bit 23. All other bits are the same as in the standard SDHC
133 if (spec_reg == SDHCI_PRESENT_STATE) {
134 ret = value & 0x000fffff;
135 ret |= (value >> 4) & SDHCI_DATA_LVL_MASK;
136 ret |= (value << 1) & SDHCI_CMD_LVL;
141 * DTS properties of mmc host are used to enable each speed mode
142 * according to soc and board capability. So clean up
143 * SDR50/SDR104/DDR50 support bits here.
145 if (spec_reg == SDHCI_CAPABILITIES_1) {
146 ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
147 SDHCI_SUPPORT_DDR50);
152 * Some controllers have unreliable Data Line Active
153 * bit for commands with busy signal. This affects
154 * Command Inhibit (data) bit. Just ignore it since
155 * MMC core driver has already polled card status
156 * with CMD13 after any command with busy siganl.
158 if ((spec_reg == SDHCI_PRESENT_STATE) &&
159 (esdhc->quirk_ignore_data_inhibit == true)) {
160 ret = value & ~SDHCI_DATA_INHIBIT;
168 static u16 esdhc_readw_fixup(struct sdhci_host *host,
169 int spec_reg, u32 value)
171 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
172 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
174 int shift = (spec_reg & 0x2) * 8;
176 if (spec_reg == SDHCI_HOST_VERSION)
177 ret = value & 0xffff;
179 ret = (value >> shift) & 0xffff;
180 /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect
181 * vendor version and spec version information.
183 if ((spec_reg == SDHCI_HOST_VERSION) &&
184 (esdhc->quirk_incorrect_hostver))
185 ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200;
189 static u8 esdhc_readb_fixup(struct sdhci_host *host,
190 int spec_reg, u32 value)
194 int shift = (spec_reg & 0x3) * 8;
196 ret = (value >> shift) & 0xff;
199 * "DMA select" locates at offset 0x28 in SD specification, but on
200 * P5020 or P3041, it locates at 0x29.
202 if (spec_reg == SDHCI_HOST_CONTROL) {
203 /* DMA select is 22,23 bits in Protocol Control Register */
204 dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK;
205 /* fixup the result */
206 ret &= ~SDHCI_CTRL_DMA_MASK;
213 * esdhc_write*_fixup - Fixup the SD spec register value so that it could be
214 * written into eSDHC register.
216 * @host: pointer to sdhci_host
217 * @spec_reg: SD spec register address
218 * @value: 8/16/32bit SD spec register value that would be written
219 * @old_value: 32bit eSDHC register value on spec_reg address
221 * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC
222 * registers are 32 bits. There are differences in register size, register
223 * address, register function, bit position and function between eSDHC spec
226 * Return a fixed up register value
228 static u32 esdhc_writel_fixup(struct sdhci_host *host,
229 int spec_reg, u32 value, u32 old_value)
234 * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE]
235 * when SYSCTL[RSTD] is set for some special operations.
236 * No any impact on other operation.
238 if (spec_reg == SDHCI_INT_ENABLE)
239 ret = value | SDHCI_INT_BLK_GAP;
246 static u32 esdhc_writew_fixup(struct sdhci_host *host,
247 int spec_reg, u16 value, u32 old_value)
249 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
250 int shift = (spec_reg & 0x2) * 8;
254 case SDHCI_TRANSFER_MODE:
256 * Postpone this write, we must do it together with a
257 * command write that is down below. Return old value.
259 pltfm_host->xfer_mode_shadow = value;
262 ret = (value << 16) | pltfm_host->xfer_mode_shadow;
266 ret = old_value & (~(0xffff << shift));
267 ret |= (value << shift);
269 if (spec_reg == SDHCI_BLOCK_SIZE) {
271 * Two last DMA bits are reserved, and first one is used for
272 * non-standard blksz of 4096 bytes that we don't support
273 * yet. So clear the DMA boundary bits.
275 ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0));
280 static u32 esdhc_writeb_fixup(struct sdhci_host *host,
281 int spec_reg, u8 value, u32 old_value)
286 int shift = (spec_reg & 0x3) * 8;
289 * eSDHC doesn't have a standard power control register, so we do
290 * nothing here to avoid incorrect operation.
292 if (spec_reg == SDHCI_POWER_CONTROL)
295 * "DMA select" location is offset 0x28 in SD specification, but on
296 * P5020 or P3041, it's located at 0x29.
298 if (spec_reg == SDHCI_HOST_CONTROL) {
300 * If host control register is not standard, exit
303 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL)
306 /* DMA select is 22,23 bits in Protocol Control Register */
307 dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5;
308 ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits;
309 tmp = (value & (~SDHCI_CTRL_DMA_MASK)) |
310 (old_value & SDHCI_CTRL_DMA_MASK);
311 ret = (ret & (~0xff)) | tmp;
313 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */
314 ret &= ~ESDHC_HOST_CONTROL_RES;
318 ret = (old_value & (~(0xff << shift))) | (value << shift);
322 static u32 esdhc_be_readl(struct sdhci_host *host, int reg)
327 if (reg == SDHCI_CAPABILITIES_1)
328 value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1);
330 value = ioread32be(host->ioaddr + reg);
332 ret = esdhc_readl_fixup(host, reg, value);
337 static u32 esdhc_le_readl(struct sdhci_host *host, int reg)
342 if (reg == SDHCI_CAPABILITIES_1)
343 value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1);
345 value = ioread32(host->ioaddr + reg);
347 ret = esdhc_readl_fixup(host, reg, value);
352 static u16 esdhc_be_readw(struct sdhci_host *host, int reg)
356 int base = reg & ~0x3;
358 value = ioread32be(host->ioaddr + base);
359 ret = esdhc_readw_fixup(host, reg, value);
363 static u16 esdhc_le_readw(struct sdhci_host *host, int reg)
367 int base = reg & ~0x3;
369 value = ioread32(host->ioaddr + base);
370 ret = esdhc_readw_fixup(host, reg, value);
374 static u8 esdhc_be_readb(struct sdhci_host *host, int reg)
378 int base = reg & ~0x3;
380 value = ioread32be(host->ioaddr + base);
381 ret = esdhc_readb_fixup(host, reg, value);
385 static u8 esdhc_le_readb(struct sdhci_host *host, int reg)
389 int base = reg & ~0x3;
391 value = ioread32(host->ioaddr + base);
392 ret = esdhc_readb_fixup(host, reg, value);
396 static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg)
400 value = esdhc_writel_fixup(host, reg, val, 0);
401 iowrite32be(value, host->ioaddr + reg);
404 static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg)
408 value = esdhc_writel_fixup(host, reg, val, 0);
409 iowrite32(value, host->ioaddr + reg);
412 static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg)
414 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
415 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
416 int base = reg & ~0x3;
420 value = ioread32be(host->ioaddr + base);
421 ret = esdhc_writew_fixup(host, reg, val, value);
422 if (reg != SDHCI_TRANSFER_MODE)
423 iowrite32be(ret, host->ioaddr + base);
425 /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
426 * 1us later after ESDHC_EXTN is set.
428 if (base == ESDHC_SYSTEM_CONTROL_2) {
429 if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
430 esdhc->in_sw_tuning) {
432 ret |= ESDHC_SMPCLKSEL;
433 iowrite32be(ret, host->ioaddr + base);
438 static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg)
440 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
441 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
442 int base = reg & ~0x3;
446 value = ioread32(host->ioaddr + base);
447 ret = esdhc_writew_fixup(host, reg, val, value);
448 if (reg != SDHCI_TRANSFER_MODE)
449 iowrite32(ret, host->ioaddr + base);
451 /* Starting SW tuning requires ESDHC_SMPCLKSEL to be set
452 * 1us later after ESDHC_EXTN is set.
454 if (base == ESDHC_SYSTEM_CONTROL_2) {
455 if (!(value & ESDHC_EXTN) && (ret & ESDHC_EXTN) &&
456 esdhc->in_sw_tuning) {
458 ret |= ESDHC_SMPCLKSEL;
459 iowrite32(ret, host->ioaddr + base);
464 static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg)
466 int base = reg & ~0x3;
470 value = ioread32be(host->ioaddr + base);
471 ret = esdhc_writeb_fixup(host, reg, val, value);
472 iowrite32be(ret, host->ioaddr + base);
475 static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg)
477 int base = reg & ~0x3;
481 value = ioread32(host->ioaddr + base);
482 ret = esdhc_writeb_fixup(host, reg, val, value);
483 iowrite32(ret, host->ioaddr + base);
487 * For Abort or Suspend after Stop at Block Gap, ignore the ADMA
488 * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC])
489 * and Block Gap Event(IRQSTAT[BGE]) are also set.
490 * For Continue, apply soft reset for data(SYSCTL[RSTD]);
491 * and re-issue the entire read transaction from beginning.
493 static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask)
495 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
496 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
501 applicable = (intmask & SDHCI_INT_DATA_END) &&
502 (intmask & SDHCI_INT_BLK_GAP) &&
503 (esdhc->vendor_ver == VENDOR_V_23);
507 host->data->error = 0;
508 dmastart = sg_dma_address(host->data->sg);
509 dmanow = dmastart + host->data->bytes_xfered;
511 * Force update to the next DMA block boundary.
513 dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
514 SDHCI_DEFAULT_BOUNDARY_SIZE;
515 host->data->bytes_xfered = dmanow - dmastart;
516 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
519 static int esdhc_of_enable_dma(struct sdhci_host *host)
522 struct device *dev = mmc_dev(host->mmc);
524 if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") ||
525 of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc"))
526 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
528 value = sdhci_readl(host, ESDHC_DMA_SYSCTL);
530 if (of_dma_is_coherent(dev->of_node))
531 value |= ESDHC_DMA_SNOOP;
533 value &= ~ESDHC_DMA_SNOOP;
535 sdhci_writel(host, value, ESDHC_DMA_SYSCTL);
539 static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
541 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
542 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
544 if (esdhc->peripheral_clock)
545 return esdhc->peripheral_clock;
547 return pltfm_host->clock;
550 static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
552 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
553 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
556 if (esdhc->peripheral_clock)
557 clock = esdhc->peripheral_clock;
559 clock = pltfm_host->clock;
560 return clock / 256 / 16;
563 static void esdhc_clock_enable(struct sdhci_host *host, bool enable)
568 val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
571 val |= ESDHC_CLOCK_SDCLKEN;
573 val &= ~ESDHC_CLOCK_SDCLKEN;
575 sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL);
578 timeout = ktime_add_ms(ktime_get(), 20);
579 val = ESDHC_CLOCK_STABLE;
581 bool timedout = ktime_after(ktime_get(), timeout);
583 if (sdhci_readl(host, ESDHC_PRSSTAT) & val)
586 pr_err("%s: Internal clock never stabilised.\n",
587 mmc_hostname(host->mmc));
594 static void esdhc_flush_async_fifo(struct sdhci_host *host)
599 val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
600 val |= ESDHC_FLUSH_ASYNC_FIFO;
601 sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
604 timeout = ktime_add_ms(ktime_get(), 20);
606 bool timedout = ktime_after(ktime_get(), timeout);
608 if (!(sdhci_readl(host, ESDHC_DMA_SYSCTL) &
609 ESDHC_FLUSH_ASYNC_FIFO))
612 pr_err("%s: flushing asynchronous FIFO timeout.\n",
613 mmc_hostname(host->mmc));
616 usleep_range(10, 20);
620 static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock)
622 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
623 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
631 host->mmc->actual_clock = 0;
634 esdhc_clock_enable(host, false);
638 /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */
639 if (esdhc->vendor_ver < VENDOR_V_23)
642 if (host->mmc->card && mmc_card_sd(host->mmc->card) &&
643 esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY)
644 fixup = esdhc->clk_fixup->sd_dflt_max_clk;
645 else if (esdhc->clk_fixup)
646 fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing];
648 if (fixup && clock > fixup)
651 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
652 temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN |
653 ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
654 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
656 while (host->max_clk / pre_div / 16 > clock && pre_div < 256)
659 while (host->max_clk / pre_div / div > clock && div < 16)
662 if (esdhc->quirk_limited_clk_division &&
663 clock == MMC_HS200_MAX_DTR &&
664 (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 ||
665 host->flags & SDHCI_HS400_TUNING)) {
666 division = pre_div * div;
670 } else if (division <= 8) {
673 } else if (division <= 12) {
677 pr_warn("%s: using unsupported clock division.\n",
678 mmc_hostname(host->mmc));
682 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
683 clock, host->max_clk / pre_div / div);
684 host->mmc->actual_clock = host->max_clk / pre_div / div;
685 esdhc->div_ratio = pre_div * div;
689 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
690 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
691 | (div << ESDHC_DIVIDER_SHIFT)
692 | (pre_div << ESDHC_PREDIV_SHIFT));
693 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
695 if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 &&
696 clock == MMC_HS200_MAX_DTR) {
697 temp = sdhci_readl(host, ESDHC_TBCTL);
698 sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL);
699 temp = sdhci_readl(host, ESDHC_SDCLKCTL);
700 sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL);
701 esdhc_clock_enable(host, true);
703 temp = sdhci_readl(host, ESDHC_DLLCFG0);
704 temp |= ESDHC_DLL_ENABLE;
705 if (host->mmc->actual_clock == MMC_HS200_MAX_DTR)
706 temp |= ESDHC_DLL_FREQ_SEL;
707 sdhci_writel(host, temp, ESDHC_DLLCFG0);
708 temp = sdhci_readl(host, ESDHC_TBCTL);
709 sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL);
711 esdhc_clock_enable(host, false);
712 esdhc_flush_async_fifo(host);
716 timeout = ktime_add_ms(ktime_get(), 20);
718 bool timedout = ktime_after(ktime_get(), timeout);
720 if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)
723 pr_err("%s: Internal clock never stabilised.\n",
724 mmc_hostname(host->mmc));
730 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
731 temp |= ESDHC_CLOCK_SDCLKEN;
732 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
735 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
739 ctrl = sdhci_readl(host, ESDHC_PROCTL);
740 ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK);
742 case MMC_BUS_WIDTH_8:
743 ctrl |= ESDHC_CTRL_8BITBUS;
746 case MMC_BUS_WIDTH_4:
747 ctrl |= ESDHC_CTRL_4BITBUS;
754 sdhci_writel(host, ctrl, ESDHC_PROCTL);
757 static void esdhc_reset(struct sdhci_host *host, u8 mask)
759 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
760 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
763 if (esdhc->quirk_delay_before_data_reset &&
764 (mask & SDHCI_RESET_DATA) &&
765 (host->flags & SDHCI_REQ_USE_DMA))
768 sdhci_reset(host, mask);
770 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
771 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
773 if (mask & SDHCI_RESET_ALL) {
774 val = sdhci_readl(host, ESDHC_TBCTL);
776 sdhci_writel(host, val, ESDHC_TBCTL);
778 if (esdhc->quirk_unreliable_pulse_detection) {
779 val = sdhci_readl(host, ESDHC_DLLCFG1);
780 val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL;
781 sdhci_writel(host, val, ESDHC_DLLCFG1);
786 /* The SCFG, Supplemental Configuration Unit, provides SoC specific
787 * configuration and status registers for the device. There is a
788 * SDHC IO VSEL control register on SCFG for some platforms. It's
789 * used to support SDHC IO voltage switching.
791 static const struct of_device_id scfg_device_ids[] = {
792 { .compatible = "fsl,t1040-scfg", },
793 { .compatible = "fsl,ls1012a-scfg", },
794 { .compatible = "fsl,ls1046a-scfg", },
798 /* SDHC IO VSEL control register definition */
799 #define SCFG_SDHCIOVSELCR 0x408
800 #define SDHCIOVSELCR_TGLEN 0x80000000
801 #define SDHCIOVSELCR_VSELVAL 0x60000000
802 #define SDHCIOVSELCR_SDHC_VS 0x00000001
804 static int esdhc_signal_voltage_switch(struct mmc_host *mmc,
807 struct sdhci_host *host = mmc_priv(mmc);
808 struct device_node *scfg_node;
809 void __iomem *scfg_base = NULL;
814 * Signal Voltage Switching is only applicable for Host Controllers
817 if (host->version < SDHCI_SPEC_300)
820 val = sdhci_readl(host, ESDHC_PROCTL);
822 switch (ios->signal_voltage) {
823 case MMC_SIGNAL_VOLTAGE_330:
824 val &= ~ESDHC_VOLT_SEL;
825 sdhci_writel(host, val, ESDHC_PROCTL);
827 case MMC_SIGNAL_VOLTAGE_180:
828 scfg_node = of_find_matching_node(NULL, scfg_device_ids);
830 scfg_base = of_iomap(scfg_node, 0);
832 sdhciovselcr = SDHCIOVSELCR_TGLEN |
833 SDHCIOVSELCR_VSELVAL;
834 iowrite32be(sdhciovselcr,
835 scfg_base + SCFG_SDHCIOVSELCR);
837 val |= ESDHC_VOLT_SEL;
838 sdhci_writel(host, val, ESDHC_PROCTL);
841 sdhciovselcr = SDHCIOVSELCR_TGLEN |
842 SDHCIOVSELCR_SDHC_VS;
843 iowrite32be(sdhciovselcr,
844 scfg_base + SCFG_SDHCIOVSELCR);
847 val |= ESDHC_VOLT_SEL;
848 sdhci_writel(host, val, ESDHC_PROCTL);
856 static struct soc_device_attribute soc_tuning_erratum_type1[] = {
857 { .family = "QorIQ T1023", .revision = "1.0", },
858 { .family = "QorIQ T1040", .revision = "1.0", },
859 { .family = "QorIQ T2080", .revision = "1.0", },
860 { .family = "QorIQ LS1021A", .revision = "1.0", },
864 static struct soc_device_attribute soc_tuning_erratum_type2[] = {
865 { .family = "QorIQ LS1012A", .revision = "1.0", },
866 { .family = "QorIQ LS1043A", .revision = "1.*", },
867 { .family = "QorIQ LS1046A", .revision = "1.0", },
868 { .family = "QorIQ LS1080A", .revision = "1.0", },
869 { .family = "QorIQ LS2080A", .revision = "1.0", },
870 { .family = "QorIQ LA1575A", .revision = "1.0", },
874 static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable)
878 esdhc_clock_enable(host, false);
879 esdhc_flush_async_fifo(host);
881 val = sdhci_readl(host, ESDHC_TBCTL);
886 sdhci_writel(host, val, ESDHC_TBCTL);
888 esdhc_clock_enable(host, true);
891 static void esdhc_prepare_sw_tuning(struct sdhci_host *host, u8 *window_start,
894 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
895 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
896 u8 tbstat_15_8, tbstat_7_0;
899 if (esdhc->quirk_tuning_erratum_type1) {
900 *window_start = 5 * esdhc->div_ratio;
901 *window_end = 3 * esdhc->div_ratio;
905 /* Write TBCTL[11:8]=4'h8 */
906 val = sdhci_readl(host, ESDHC_TBCTL);
909 sdhci_writel(host, val, ESDHC_TBCTL);
913 /* Read TBCTL[31:0] register and rewrite again */
914 val = sdhci_readl(host, ESDHC_TBCTL);
915 sdhci_writel(host, val, ESDHC_TBCTL);
919 /* Read the TBSTAT[31:0] register twice */
920 val = sdhci_readl(host, ESDHC_TBSTAT);
921 val = sdhci_readl(host, ESDHC_TBSTAT);
923 /* Reset data lines by setting ESDHCCTL[RSTD] */
924 sdhci_reset(host, SDHCI_RESET_DATA);
925 /* Write 32'hFFFF_FFFF to IRQSTAT register */
926 sdhci_writel(host, 0xFFFFFFFF, SDHCI_INT_STATUS);
928 /* If TBSTAT[15:8]-TBSTAT[7:0] > 4 * div_ratio
929 * or TBSTAT[7:0]-TBSTAT[15:8] > 4 * div_ratio,
930 * then program TBPTR[TB_WNDW_END_PTR] = 4 * div_ratio
931 * and program TBPTR[TB_WNDW_START_PTR] = 8 * div_ratio.
933 tbstat_7_0 = val & 0xff;
934 tbstat_15_8 = (val >> 8) & 0xff;
936 if (abs(tbstat_15_8 - tbstat_7_0) > (4 * esdhc->div_ratio)) {
937 *window_start = 8 * esdhc->div_ratio;
938 *window_end = 4 * esdhc->div_ratio;
940 *window_start = 5 * esdhc->div_ratio;
941 *window_end = 3 * esdhc->div_ratio;
945 static int esdhc_execute_sw_tuning(struct mmc_host *mmc, u32 opcode,
946 u8 window_start, u8 window_end)
948 struct sdhci_host *host = mmc_priv(mmc);
949 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
950 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
954 /* Program TBPTR[TB_WNDW_END_PTR] and TBPTR[TB_WNDW_START_PTR] */
955 val = ((u32)window_start << ESDHC_WNDW_STRT_PTR_SHIFT) &
956 ESDHC_WNDW_STRT_PTR_MASK;
957 val |= window_end & ESDHC_WNDW_END_PTR_MASK;
958 sdhci_writel(host, val, ESDHC_TBPTR);
960 /* Program the software tuning mode by setting TBCTL[TB_MODE]=2'h3 */
961 val = sdhci_readl(host, ESDHC_TBCTL);
962 val &= ~ESDHC_TB_MODE_MASK;
963 val |= ESDHC_TB_MODE_SW;
964 sdhci_writel(host, val, ESDHC_TBCTL);
966 esdhc->in_sw_tuning = true;
967 ret = sdhci_execute_tuning(mmc, opcode);
968 esdhc->in_sw_tuning = false;
972 static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
974 struct sdhci_host *host = mmc_priv(mmc);
975 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
976 struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host);
977 u8 window_start, window_end;
978 int ret, retries = 1;
983 /* For tuning mode, the sd clock divisor value
984 * must be larger than 3 according to reference manual.
986 clk = esdhc->peripheral_clock / 3;
987 if (host->clock > clk)
988 esdhc_of_set_clock(host, clk);
990 esdhc_tuning_block_enable(host, true);
992 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
995 if (esdhc->quirk_limited_clk_division &&
997 esdhc_of_set_clock(host, host->clock);
1000 val = sdhci_readl(host, ESDHC_TBCTL);
1001 val &= ~ESDHC_TB_MODE_MASK;
1002 val |= ESDHC_TB_MODE_3;
1003 sdhci_writel(host, val, ESDHC_TBCTL);
1005 ret = sdhci_execute_tuning(mmc, opcode);
1009 /* If HW tuning fails and triggers erratum,
1012 ret = host->tuning_err;
1013 if (ret == -EAGAIN &&
1014 (esdhc->quirk_tuning_erratum_type1 ||
1015 esdhc->quirk_tuning_erratum_type2)) {
1016 /* Recover HS400 tuning flag */
1018 host->flags |= SDHCI_HS400_TUNING;
1019 pr_info("%s: Hold on to use fixed sampling clock. Try SW tuning!\n",
1022 esdhc_prepare_sw_tuning(host, &window_start,
1024 ret = esdhc_execute_sw_tuning(mmc, opcode,
1030 /* Retry both HW/SW tuning with reduced clock. */
1031 ret = host->tuning_err;
1032 if (ret == -EAGAIN && retries) {
1033 /* Recover HS400 tuning flag */
1035 host->flags |= SDHCI_HS400_TUNING;
1037 clk = host->max_clk / (esdhc->div_ratio + 1);
1038 esdhc_of_set_clock(host, clk);
1039 pr_info("%s: Hold on to use fixed sampling clock. Try tuning with reduced clock!\n",
1047 } while (retries--);
1050 esdhc_tuning_block_enable(host, false);
1051 } else if (hs400_tuning) {
1052 val = sdhci_readl(host, ESDHC_SDTIMNGCTL);
1053 val |= ESDHC_FLW_CTL_BG;
1054 sdhci_writel(host, val, ESDHC_SDTIMNGCTL);
1060 static void esdhc_set_uhs_signaling(struct sdhci_host *host,
1061 unsigned int timing)
1063 if (timing == MMC_TIMING_MMC_HS400)
1064 esdhc_tuning_block_enable(host, true);
1066 sdhci_set_uhs_signaling(host, timing);
1069 static u32 esdhc_irq(struct sdhci_host *host, u32 intmask)
1073 if (of_find_compatible_node(NULL, NULL,
1074 "fsl,p2020-esdhc")) {
1075 command = SDHCI_GET_CMD(sdhci_readw(host,
1077 if (command == MMC_WRITE_MULTIPLE_BLOCK &&
1078 sdhci_readw(host, SDHCI_BLOCK_COUNT) &&
1079 intmask & SDHCI_INT_DATA_END) {
1080 intmask &= ~SDHCI_INT_DATA_END;
1081 sdhci_writel(host, SDHCI_INT_DATA_END,
1088 #ifdef CONFIG_PM_SLEEP
1089 static u32 esdhc_proctl;
1090 static int esdhc_of_suspend(struct device *dev)
1092 struct sdhci_host *host = dev_get_drvdata(dev);
1094 esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL);
1096 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1097 mmc_retune_needed(host->mmc);
1099 return sdhci_suspend_host(host);
1102 static int esdhc_of_resume(struct device *dev)
1104 struct sdhci_host *host = dev_get_drvdata(dev);
1105 int ret = sdhci_resume_host(host);
1108 /* Isn't this already done by sdhci_resume_host() ? --rmk */
1109 esdhc_of_enable_dma(host);
1110 sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
1116 static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops,
1120 static const struct sdhci_ops sdhci_esdhc_be_ops = {
1121 .read_l = esdhc_be_readl,
1122 .read_w = esdhc_be_readw,
1123 .read_b = esdhc_be_readb,
1124 .write_l = esdhc_be_writel,
1125 .write_w = esdhc_be_writew,
1126 .write_b = esdhc_be_writeb,
1127 .set_clock = esdhc_of_set_clock,
1128 .enable_dma = esdhc_of_enable_dma,
1129 .get_max_clock = esdhc_of_get_max_clock,
1130 .get_min_clock = esdhc_of_get_min_clock,
1131 .adma_workaround = esdhc_of_adma_workaround,
1132 .set_bus_width = esdhc_pltfm_set_bus_width,
1133 .reset = esdhc_reset,
1134 .set_uhs_signaling = esdhc_set_uhs_signaling,
1138 static const struct sdhci_ops sdhci_esdhc_le_ops = {
1139 .read_l = esdhc_le_readl,
1140 .read_w = esdhc_le_readw,
1141 .read_b = esdhc_le_readb,
1142 .write_l = esdhc_le_writel,
1143 .write_w = esdhc_le_writew,
1144 .write_b = esdhc_le_writeb,
1145 .set_clock = esdhc_of_set_clock,
1146 .enable_dma = esdhc_of_enable_dma,
1147 .get_max_clock = esdhc_of_get_max_clock,
1148 .get_min_clock = esdhc_of_get_min_clock,
1149 .adma_workaround = esdhc_of_adma_workaround,
1150 .set_bus_width = esdhc_pltfm_set_bus_width,
1151 .reset = esdhc_reset,
1152 .set_uhs_signaling = esdhc_set_uhs_signaling,
1156 static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = {
1157 .quirks = ESDHC_DEFAULT_QUIRKS |
1159 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
1161 SDHCI_QUIRK_NO_CARD_NO_RESET |
1162 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
1163 .ops = &sdhci_esdhc_be_ops,
1166 static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = {
1167 .quirks = ESDHC_DEFAULT_QUIRKS |
1168 SDHCI_QUIRK_NO_CARD_NO_RESET |
1169 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
1170 .ops = &sdhci_esdhc_le_ops,
1173 static struct soc_device_attribute soc_incorrect_hostver[] = {
1174 { .family = "QorIQ T4240", .revision = "1.0", },
1175 { .family = "QorIQ T4240", .revision = "2.0", },
1179 static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = {
1180 { .family = "QorIQ LX2160A", .revision = "1.0", },
1181 { .family = "QorIQ LX2160A", .revision = "2.0", },
1182 { .family = "QorIQ LS1028A", .revision = "1.0", },
1186 static struct soc_device_attribute soc_unreliable_pulse_detection[] = {
1187 { .family = "QorIQ LX2160A", .revision = "1.0", },
1191 static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host)
1193 const struct of_device_id *match;
1194 struct sdhci_pltfm_host *pltfm_host;
1195 struct sdhci_esdhc *esdhc;
1196 struct device_node *np;
1201 pltfm_host = sdhci_priv(host);
1202 esdhc = sdhci_pltfm_priv(pltfm_host);
1204 host_ver = sdhci_readw(host, SDHCI_HOST_VERSION);
1205 esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >>
1206 SDHCI_VENDOR_VER_SHIFT;
1207 esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK;
1208 if (soc_device_match(soc_incorrect_hostver))
1209 esdhc->quirk_incorrect_hostver = true;
1211 esdhc->quirk_incorrect_hostver = false;
1213 if (soc_device_match(soc_fixup_sdhc_clkdivs))
1214 esdhc->quirk_limited_clk_division = true;
1216 esdhc->quirk_limited_clk_division = false;
1218 if (soc_device_match(soc_unreliable_pulse_detection))
1219 esdhc->quirk_unreliable_pulse_detection = true;
1221 esdhc->quirk_unreliable_pulse_detection = false;
1223 match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node);
1225 esdhc->clk_fixup = match->data;
1226 np = pdev->dev.of_node;
1228 if (of_device_is_compatible(np, "fsl,p2020-esdhc"))
1229 esdhc->quirk_delay_before_data_reset = true;
1231 clk = of_clk_get(np, 0);
1234 * esdhc->peripheral_clock would be assigned with a value
1235 * which is eSDHC base clock when use periperal clock.
1236 * For some platforms, the clock value got by common clk
1237 * API is peripheral clock while the eSDHC base clock is
1238 * 1/2 peripheral clock.
1240 if (of_device_is_compatible(np, "fsl,ls1046a-esdhc") ||
1241 of_device_is_compatible(np, "fsl,ls1028a-esdhc") ||
1242 of_device_is_compatible(np, "fsl,ls1088a-esdhc"))
1243 esdhc->peripheral_clock = clk_get_rate(clk) / 2;
1245 esdhc->peripheral_clock = clk_get_rate(clk);
1250 if (esdhc->peripheral_clock) {
1251 esdhc_clock_enable(host, false);
1252 val = sdhci_readl(host, ESDHC_DMA_SYSCTL);
1253 val |= ESDHC_PERIPHERAL_CLK_SEL;
1254 sdhci_writel(host, val, ESDHC_DMA_SYSCTL);
1255 esdhc_clock_enable(host, true);
1259 static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc)
1261 esdhc_tuning_block_enable(mmc_priv(mmc), false);
1265 static int sdhci_esdhc_probe(struct platform_device *pdev)
1267 struct sdhci_host *host;
1268 struct device_node *np;
1269 struct sdhci_pltfm_host *pltfm_host;
1270 struct sdhci_esdhc *esdhc;
1273 np = pdev->dev.of_node;
1275 if (of_property_read_bool(np, "little-endian"))
1276 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata,
1277 sizeof(struct sdhci_esdhc));
1279 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata,
1280 sizeof(struct sdhci_esdhc));
1283 return PTR_ERR(host);
1285 host->mmc_host_ops.start_signal_voltage_switch =
1286 esdhc_signal_voltage_switch;
1287 host->mmc_host_ops.execute_tuning = esdhc_execute_tuning;
1288 host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr;
1289 host->tuning_delay = 1;
1291 esdhc_init(pdev, host);
1293 sdhci_get_of_property(pdev);
1295 pltfm_host = sdhci_priv(host);
1296 esdhc = sdhci_pltfm_priv(pltfm_host);
1297 if (soc_device_match(soc_tuning_erratum_type1))
1298 esdhc->quirk_tuning_erratum_type1 = true;
1300 esdhc->quirk_tuning_erratum_type1 = false;
1302 if (soc_device_match(soc_tuning_erratum_type2))
1303 esdhc->quirk_tuning_erratum_type2 = true;
1305 esdhc->quirk_tuning_erratum_type2 = false;
1307 if (esdhc->vendor_ver == VENDOR_V_22)
1308 host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23;
1310 if (esdhc->vendor_ver > VENDOR_V_22)
1311 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1313 if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) {
1314 host->quirks |= SDHCI_QUIRK_RESET_AFTER_REQUEST;
1315 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1318 if (of_device_is_compatible(np, "fsl,p5040-esdhc") ||
1319 of_device_is_compatible(np, "fsl,p5020-esdhc") ||
1320 of_device_is_compatible(np, "fsl,p4080-esdhc") ||
1321 of_device_is_compatible(np, "fsl,p1020-esdhc") ||
1322 of_device_is_compatible(np, "fsl,t1040-esdhc"))
1323 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1325 if (of_device_is_compatible(np, "fsl,ls1021a-esdhc"))
1326 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1328 esdhc->quirk_ignore_data_inhibit = false;
1329 if (of_device_is_compatible(np, "fsl,p2020-esdhc")) {
1331 * Freescale messed up with P2020 as it has a non-standard
1332 * host control register
1334 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL;
1335 esdhc->quirk_ignore_data_inhibit = true;
1338 /* call to generic mmc_of_parse to support additional capabilities */
1339 ret = mmc_of_parse(host->mmc);
1343 mmc_of_parse_voltage(np, &host->ocr_mask);
1345 ret = sdhci_add_host(host);
1351 sdhci_pltfm_free(pdev);
1355 static struct platform_driver sdhci_esdhc_driver = {
1357 .name = "sdhci-esdhc",
1358 .of_match_table = sdhci_esdhc_of_match,
1359 .pm = &esdhc_of_dev_pm_ops,
1361 .probe = sdhci_esdhc_probe,
1362 .remove = sdhci_pltfm_unregister,
1365 module_platform_driver(sdhci_esdhc_driver);
1367 MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
1368 MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
1369 "Anton Vorontsov <avorontsov@ru.mvista.com>");
1370 MODULE_LICENSE("GPL v2");