12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 27657c3a7SAlbert Herranz /* 37657c3a7SAlbert Herranz * Freescale eSDHC controller driver. 47657c3a7SAlbert Herranz * 5f060bc9cSJerry Huang * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc. 67657c3a7SAlbert Herranz * Copyright (c) 2009 MontaVista Software, Inc. 77657c3a7SAlbert Herranz * 87657c3a7SAlbert Herranz * Authors: Xiaobo Xie <X.Xie@freescale.com> 97657c3a7SAlbert Herranz * Anton Vorontsov <avorontsov@ru.mvista.com> 107657c3a7SAlbert Herranz */ 117657c3a7SAlbert Herranz 1266b50a00SOded Gabbay #include <linux/err.h> 137657c3a7SAlbert Herranz #include <linux/io.h> 14f060bc9cSJerry Huang #include <linux/of.h> 15ea35645aSyangbo lu #include <linux/of_address.h> 167657c3a7SAlbert Herranz #include <linux/delay.h> 1788b47679SPaul Gortmaker #include <linux/module.h> 18151ede40Syangbo lu #include <linux/sys_soc.h> 1919c3a0efSyangbo lu #include <linux/clk.h> 2019c3a0efSyangbo lu #include <linux/ktime.h> 215552d7adSLaurentiu Tudor #include <linux/dma-mapping.h> 227657c3a7SAlbert Herranz #include <linux/mmc/host.h> 23b214fe59SYinbo Zhu #include <linux/mmc/mmc.h> 2438576af1SShawn Guo #include "sdhci-pltfm.h" 2580872e21SWolfram Sang #include "sdhci-esdhc.h" 267657c3a7SAlbert Herranz 27137ccd46SJerry Huang #define VENDOR_V_22 0x12 28a4071fbbSHaijun Zhang #define VENDOR_V_23 0x13 29f4932cfdSyangbo lu 3067fdfbdfSyinbo.zhu #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1) 3167fdfbdfSyinbo.zhu 3267fdfbdfSyinbo.zhu struct esdhc_clk_fixup { 3367fdfbdfSyinbo.zhu const unsigned int sd_dflt_max_clk; 3467fdfbdfSyinbo.zhu const unsigned int max_clk[MMC_TIMING_NUM]; 3567fdfbdfSyinbo.zhu }; 3667fdfbdfSyinbo.zhu 3767fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1021a_esdhc_clk = { 3867fdfbdfSyinbo.zhu .sd_dflt_max_clk = 25000000, 3967fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS] = 46500000, 4067fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_SD_HS] = 46500000, 4167fdfbdfSyinbo.zhu }; 4267fdfbdfSyinbo.zhu 4367fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1046a_esdhc_clk = { 4467fdfbdfSyinbo.zhu .sd_dflt_max_clk = 25000000, 4567fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_UHS_SDR104] = 167000000, 4667fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS200] = 167000000, 4767fdfbdfSyinbo.zhu }; 4867fdfbdfSyinbo.zhu 4967fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1012a_esdhc_clk = { 5067fdfbdfSyinbo.zhu .sd_dflt_max_clk = 25000000, 5167fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_UHS_SDR104] = 125000000, 5267fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS200] = 125000000, 5367fdfbdfSyinbo.zhu }; 5467fdfbdfSyinbo.zhu 5567fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup p1010_esdhc_clk = { 5667fdfbdfSyinbo.zhu .sd_dflt_max_clk = 20000000, 5767fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_LEGACY] = 20000000, 5867fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS] = 42000000, 5967fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_SD_HS] = 40000000, 6067fdfbdfSyinbo.zhu }; 6167fdfbdfSyinbo.zhu 6267fdfbdfSyinbo.zhu static const struct of_device_id sdhci_esdhc_of_match[] = { 6367fdfbdfSyinbo.zhu { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk}, 6467fdfbdfSyinbo.zhu { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk}, 6567fdfbdfSyinbo.zhu { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk}, 6667fdfbdfSyinbo.zhu { .compatible = "fsl,p1010-esdhc", .data = &p1010_esdhc_clk}, 6767fdfbdfSyinbo.zhu { .compatible = "fsl,mpc8379-esdhc" }, 6867fdfbdfSyinbo.zhu { .compatible = "fsl,mpc8536-esdhc" }, 6967fdfbdfSyinbo.zhu { .compatible = "fsl,esdhc" }, 7067fdfbdfSyinbo.zhu { } 7167fdfbdfSyinbo.zhu }; 7267fdfbdfSyinbo.zhu MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); 7367fdfbdfSyinbo.zhu 74f4932cfdSyangbo lu struct sdhci_esdhc { 75f4932cfdSyangbo lu u8 vendor_ver; 76f4932cfdSyangbo lu u8 spec_ver; 77151ede40Syangbo lu bool quirk_incorrect_hostver; 786079e63cSYangbo Lu bool quirk_limited_clk_division; 7948e304ccSYangbo Lu bool quirk_unreliable_pulse_detection; 80b1f378abSYinbo Zhu bool quirk_fixup_tuning; 811f1929f3SYangbo Lu bool quirk_ignore_data_inhibit; 8219c3a0efSyangbo lu unsigned int peripheral_clock; 8367fdfbdfSyinbo.zhu const struct esdhc_clk_fixup *clk_fixup; 84b1f378abSYinbo Zhu u32 div_ratio; 85f4932cfdSyangbo lu }; 86f4932cfdSyangbo lu 87f4932cfdSyangbo lu /** 88f4932cfdSyangbo lu * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register 89f4932cfdSyangbo lu * to make it compatible with SD spec. 90f4932cfdSyangbo lu * 91f4932cfdSyangbo lu * @host: pointer to sdhci_host 92f4932cfdSyangbo lu * @spec_reg: SD spec register address 93f4932cfdSyangbo lu * @value: 32bit eSDHC register value on spec_reg address 94f4932cfdSyangbo lu * 95f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 96f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 97f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 98f4932cfdSyangbo lu * and SD spec. 99f4932cfdSyangbo lu * 100f4932cfdSyangbo lu * Return a fixed up register value 101f4932cfdSyangbo lu */ 102f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host, 103f4932cfdSyangbo lu int spec_reg, u32 value) 104137ccd46SJerry Huang { 105f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1068605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 107137ccd46SJerry Huang u32 ret; 108137ccd46SJerry Huang 109137ccd46SJerry Huang /* 110137ccd46SJerry Huang * The bit of ADMA flag in eSDHC is not compatible with standard 111137ccd46SJerry Huang * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is 112137ccd46SJerry Huang * supported by eSDHC. 113137ccd46SJerry Huang * And for many FSL eSDHC controller, the reset value of field 114f4932cfdSyangbo lu * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA, 115137ccd46SJerry Huang * only these vendor version is greater than 2.2/0x12 support ADMA. 116137ccd46SJerry Huang */ 117f4932cfdSyangbo lu if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) { 118f4932cfdSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) { 119f4932cfdSyangbo lu ret = value | SDHCI_CAN_DO_ADMA2; 120f4932cfdSyangbo lu return ret; 121137ccd46SJerry Huang } 122f4932cfdSyangbo lu } 123b0921d5cSMichael Walle /* 124b0921d5cSMichael Walle * The DAT[3:0] line signal levels and the CMD line signal level are 125b0921d5cSMichael Walle * not compatible with standard SDHC register. The line signal levels 126b0921d5cSMichael Walle * DAT[7:0] are at bits 31:24 and the command line signal level is at 127b0921d5cSMichael Walle * bit 23. All other bits are the same as in the standard SDHC 128b0921d5cSMichael Walle * register. 129b0921d5cSMichael Walle */ 130b0921d5cSMichael Walle if (spec_reg == SDHCI_PRESENT_STATE) { 131b0921d5cSMichael Walle ret = value & 0x000fffff; 132b0921d5cSMichael Walle ret |= (value >> 4) & SDHCI_DATA_LVL_MASK; 133b0921d5cSMichael Walle ret |= (value << 1) & SDHCI_CMD_LVL; 134b0921d5cSMichael Walle return ret; 135b0921d5cSMichael Walle } 136b0921d5cSMichael Walle 1372f3110ccSyangbo lu /* 1382f3110ccSyangbo lu * DTS properties of mmc host are used to enable each speed mode 1392f3110ccSyangbo lu * according to soc and board capability. So clean up 1402f3110ccSyangbo lu * SDR50/SDR104/DDR50 support bits here. 1412f3110ccSyangbo lu */ 1422f3110ccSyangbo lu if (spec_reg == SDHCI_CAPABILITIES_1) { 1432f3110ccSyangbo lu ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 | 1442f3110ccSyangbo lu SDHCI_SUPPORT_DDR50); 1452f3110ccSyangbo lu return ret; 1462f3110ccSyangbo lu } 1472f3110ccSyangbo lu 1481f1929f3SYangbo Lu /* 1491f1929f3SYangbo Lu * Some controllers have unreliable Data Line Active 1501f1929f3SYangbo Lu * bit for commands with busy signal. This affects 1511f1929f3SYangbo Lu * Command Inhibit (data) bit. Just ignore it since 1521f1929f3SYangbo Lu * MMC core driver has already polled card status 1531f1929f3SYangbo Lu * with CMD13 after any command with busy siganl. 1541f1929f3SYangbo Lu */ 1551f1929f3SYangbo Lu if ((spec_reg == SDHCI_PRESENT_STATE) && 1561f1929f3SYangbo Lu (esdhc->quirk_ignore_data_inhibit == true)) { 1571f1929f3SYangbo Lu ret = value & ~SDHCI_DATA_INHIBIT; 1581f1929f3SYangbo Lu return ret; 1591f1929f3SYangbo Lu } 1601f1929f3SYangbo Lu 161f4932cfdSyangbo lu ret = value; 162137ccd46SJerry Huang return ret; 163137ccd46SJerry Huang } 164137ccd46SJerry Huang 165f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host, 166f4932cfdSyangbo lu int spec_reg, u32 value) 1677657c3a7SAlbert Herranz { 168151ede40Syangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 169151ede40Syangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 1707657c3a7SAlbert Herranz u16 ret; 171f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 1727657c3a7SAlbert Herranz 173f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_VERSION) 174f4932cfdSyangbo lu ret = value & 0xffff; 1757657c3a7SAlbert Herranz else 176f4932cfdSyangbo lu ret = (value >> shift) & 0xffff; 177151ede40Syangbo lu /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect 178151ede40Syangbo lu * vendor version and spec version information. 179151ede40Syangbo lu */ 180151ede40Syangbo lu if ((spec_reg == SDHCI_HOST_VERSION) && 181151ede40Syangbo lu (esdhc->quirk_incorrect_hostver)) 182151ede40Syangbo lu ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200; 183e51cbc9eSXu lei return ret; 184e51cbc9eSXu lei } 185e51cbc9eSXu lei 186f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host, 187f4932cfdSyangbo lu int spec_reg, u32 value) 188e51cbc9eSXu lei { 189f4932cfdSyangbo lu u8 ret; 190f4932cfdSyangbo lu u8 dma_bits; 191f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 192f4932cfdSyangbo lu 193f4932cfdSyangbo lu ret = (value >> shift) & 0xff; 194ba8c4dc9SRoy Zang 195ba8c4dc9SRoy Zang /* 196ba8c4dc9SRoy Zang * "DMA select" locates at offset 0x28 in SD specification, but on 197ba8c4dc9SRoy Zang * P5020 or P3041, it locates at 0x29. 198ba8c4dc9SRoy Zang */ 199f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 200ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 201f4932cfdSyangbo lu dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK; 202ba8c4dc9SRoy Zang /* fixup the result */ 203ba8c4dc9SRoy Zang ret &= ~SDHCI_CTRL_DMA_MASK; 204ba8c4dc9SRoy Zang ret |= dma_bits; 205ba8c4dc9SRoy Zang } 206f4932cfdSyangbo lu return ret; 207f4932cfdSyangbo lu } 208f4932cfdSyangbo lu 209f4932cfdSyangbo lu /** 210f4932cfdSyangbo lu * esdhc_write*_fixup - Fixup the SD spec register value so that it could be 211f4932cfdSyangbo lu * written into eSDHC register. 212f4932cfdSyangbo lu * 213f4932cfdSyangbo lu * @host: pointer to sdhci_host 214f4932cfdSyangbo lu * @spec_reg: SD spec register address 215f4932cfdSyangbo lu * @value: 8/16/32bit SD spec register value that would be written 216f4932cfdSyangbo lu * @old_value: 32bit eSDHC register value on spec_reg address 217f4932cfdSyangbo lu * 218f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 219f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 220f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 221f4932cfdSyangbo lu * and SD spec. 222f4932cfdSyangbo lu * 223f4932cfdSyangbo lu * Return a fixed up register value 224f4932cfdSyangbo lu */ 225f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host, 226f4932cfdSyangbo lu int spec_reg, u32 value, u32 old_value) 227f4932cfdSyangbo lu { 228f4932cfdSyangbo lu u32 ret; 229f4932cfdSyangbo lu 230f4932cfdSyangbo lu /* 231f4932cfdSyangbo lu * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] 232f4932cfdSyangbo lu * when SYSCTL[RSTD] is set for some special operations. 233f4932cfdSyangbo lu * No any impact on other operation. 234f4932cfdSyangbo lu */ 235f4932cfdSyangbo lu if (spec_reg == SDHCI_INT_ENABLE) 236f4932cfdSyangbo lu ret = value | SDHCI_INT_BLK_GAP; 237f4932cfdSyangbo lu else 238f4932cfdSyangbo lu ret = value; 239ba8c4dc9SRoy Zang 2407657c3a7SAlbert Herranz return ret; 2417657c3a7SAlbert Herranz } 2427657c3a7SAlbert Herranz 243f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host, 244f4932cfdSyangbo lu int spec_reg, u16 value, u32 old_value) 245a4071fbbSHaijun Zhang { 246f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 247f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 248f4932cfdSyangbo lu u32 ret; 249f4932cfdSyangbo lu 250f4932cfdSyangbo lu switch (spec_reg) { 251f4932cfdSyangbo lu case SDHCI_TRANSFER_MODE: 252a4071fbbSHaijun Zhang /* 253f4932cfdSyangbo lu * Postpone this write, we must do it together with a 254f4932cfdSyangbo lu * command write that is down below. Return old value. 255a4071fbbSHaijun Zhang */ 256f4932cfdSyangbo lu pltfm_host->xfer_mode_shadow = value; 257f4932cfdSyangbo lu return old_value; 258f4932cfdSyangbo lu case SDHCI_COMMAND: 259f4932cfdSyangbo lu ret = (value << 16) | pltfm_host->xfer_mode_shadow; 260f4932cfdSyangbo lu return ret; 261a4071fbbSHaijun Zhang } 262a4071fbbSHaijun Zhang 263f4932cfdSyangbo lu ret = old_value & (~(0xffff << shift)); 264f4932cfdSyangbo lu ret |= (value << shift); 265f4932cfdSyangbo lu 266f4932cfdSyangbo lu if (spec_reg == SDHCI_BLOCK_SIZE) { 2677657c3a7SAlbert Herranz /* 2687657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 2697657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 2707657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 2717657c3a7SAlbert Herranz */ 272f4932cfdSyangbo lu ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0)); 2737657c3a7SAlbert Herranz } 274f4932cfdSyangbo lu return ret; 2757657c3a7SAlbert Herranz } 2767657c3a7SAlbert Herranz 277f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host, 278f4932cfdSyangbo lu int spec_reg, u8 value, u32 old_value) 2797657c3a7SAlbert Herranz { 280f4932cfdSyangbo lu u32 ret; 281f4932cfdSyangbo lu u32 dma_bits; 282f4932cfdSyangbo lu u8 tmp; 283f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 284f4932cfdSyangbo lu 285ba8c4dc9SRoy Zang /* 2869e4703dfSyangbo lu * eSDHC doesn't have a standard power control register, so we do 2879e4703dfSyangbo lu * nothing here to avoid incorrect operation. 2889e4703dfSyangbo lu */ 2899e4703dfSyangbo lu if (spec_reg == SDHCI_POWER_CONTROL) 2909e4703dfSyangbo lu return old_value; 2919e4703dfSyangbo lu /* 292ba8c4dc9SRoy Zang * "DMA select" location is offset 0x28 in SD specification, but on 293ba8c4dc9SRoy Zang * P5020 or P3041, it's located at 0x29. 294ba8c4dc9SRoy Zang */ 295f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 296dcaff04dSOded Gabbay /* 297dcaff04dSOded Gabbay * If host control register is not standard, exit 298dcaff04dSOded Gabbay * this function 299dcaff04dSOded Gabbay */ 300dcaff04dSOded Gabbay if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL) 301f4932cfdSyangbo lu return old_value; 302dcaff04dSOded Gabbay 303ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 304f4932cfdSyangbo lu dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5; 305f4932cfdSyangbo lu ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits; 306f4932cfdSyangbo lu tmp = (value & (~SDHCI_CTRL_DMA_MASK)) | 307f4932cfdSyangbo lu (old_value & SDHCI_CTRL_DMA_MASK); 308f4932cfdSyangbo lu ret = (ret & (~0xff)) | tmp; 309f4932cfdSyangbo lu 310f4932cfdSyangbo lu /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */ 311f4932cfdSyangbo lu ret &= ~ESDHC_HOST_CONTROL_RES; 312f4932cfdSyangbo lu return ret; 313ba8c4dc9SRoy Zang } 314ba8c4dc9SRoy Zang 315f4932cfdSyangbo lu ret = (old_value & (~(0xff << shift))) | (value << shift); 316f4932cfdSyangbo lu return ret; 317f4932cfdSyangbo lu } 318f4932cfdSyangbo lu 319f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg) 320f4932cfdSyangbo lu { 321f4932cfdSyangbo lu u32 ret; 322f4932cfdSyangbo lu u32 value; 323f4932cfdSyangbo lu 3242f3110ccSyangbo lu if (reg == SDHCI_CAPABILITIES_1) 3252f3110ccSyangbo lu value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1); 3262f3110ccSyangbo lu else 327f4932cfdSyangbo lu value = ioread32be(host->ioaddr + reg); 3282f3110ccSyangbo lu 329f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 330f4932cfdSyangbo lu 331f4932cfdSyangbo lu return ret; 332f4932cfdSyangbo lu } 333f4932cfdSyangbo lu 334f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg) 335f4932cfdSyangbo lu { 336f4932cfdSyangbo lu u32 ret; 337f4932cfdSyangbo lu u32 value; 338f4932cfdSyangbo lu 3392f3110ccSyangbo lu if (reg == SDHCI_CAPABILITIES_1) 3402f3110ccSyangbo lu value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1); 3412f3110ccSyangbo lu else 342f4932cfdSyangbo lu value = ioread32(host->ioaddr + reg); 3432f3110ccSyangbo lu 344f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 345f4932cfdSyangbo lu 346f4932cfdSyangbo lu return ret; 347f4932cfdSyangbo lu } 348f4932cfdSyangbo lu 349f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg) 350f4932cfdSyangbo lu { 351f4932cfdSyangbo lu u16 ret; 352f4932cfdSyangbo lu u32 value; 353f4932cfdSyangbo lu int base = reg & ~0x3; 354f4932cfdSyangbo lu 355f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 356f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 357f4932cfdSyangbo lu return ret; 358f4932cfdSyangbo lu } 359f4932cfdSyangbo lu 360f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg) 361f4932cfdSyangbo lu { 362f4932cfdSyangbo lu u16 ret; 363f4932cfdSyangbo lu u32 value; 364f4932cfdSyangbo lu int base = reg & ~0x3; 365f4932cfdSyangbo lu 366f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 367f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 368f4932cfdSyangbo lu return ret; 369f4932cfdSyangbo lu } 370f4932cfdSyangbo lu 371f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg) 372f4932cfdSyangbo lu { 373f4932cfdSyangbo lu u8 ret; 374f4932cfdSyangbo lu u32 value; 375f4932cfdSyangbo lu int base = reg & ~0x3; 376f4932cfdSyangbo lu 377f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 378f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 379f4932cfdSyangbo lu return ret; 380f4932cfdSyangbo lu } 381f4932cfdSyangbo lu 382f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg) 383f4932cfdSyangbo lu { 384f4932cfdSyangbo lu u8 ret; 385f4932cfdSyangbo lu u32 value; 386f4932cfdSyangbo lu int base = reg & ~0x3; 387f4932cfdSyangbo lu 388f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 389f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 390f4932cfdSyangbo lu return ret; 391f4932cfdSyangbo lu } 392f4932cfdSyangbo lu 393f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg) 394f4932cfdSyangbo lu { 395f4932cfdSyangbo lu u32 value; 396f4932cfdSyangbo lu 397f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 398f4932cfdSyangbo lu iowrite32be(value, host->ioaddr + reg); 399f4932cfdSyangbo lu } 400f4932cfdSyangbo lu 401f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg) 402f4932cfdSyangbo lu { 403f4932cfdSyangbo lu u32 value; 404f4932cfdSyangbo lu 405f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 406f4932cfdSyangbo lu iowrite32(value, host->ioaddr + reg); 407f4932cfdSyangbo lu } 408f4932cfdSyangbo lu 409f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg) 410f4932cfdSyangbo lu { 411f4932cfdSyangbo lu int base = reg & ~0x3; 412f4932cfdSyangbo lu u32 value; 413f4932cfdSyangbo lu u32 ret; 414f4932cfdSyangbo lu 415f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 416f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 417f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 418f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 419f4932cfdSyangbo lu } 420f4932cfdSyangbo lu 421f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg) 422f4932cfdSyangbo lu { 423f4932cfdSyangbo lu int base = reg & ~0x3; 424f4932cfdSyangbo lu u32 value; 425f4932cfdSyangbo lu u32 ret; 426f4932cfdSyangbo lu 427f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 428f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 429f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 430f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 431f4932cfdSyangbo lu } 432f4932cfdSyangbo lu 433f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg) 434f4932cfdSyangbo lu { 435f4932cfdSyangbo lu int base = reg & ~0x3; 436f4932cfdSyangbo lu u32 value; 437f4932cfdSyangbo lu u32 ret; 438f4932cfdSyangbo lu 439f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 440f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 441f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 442f4932cfdSyangbo lu } 443f4932cfdSyangbo lu 444f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg) 445f4932cfdSyangbo lu { 446f4932cfdSyangbo lu int base = reg & ~0x3; 447f4932cfdSyangbo lu u32 value; 448f4932cfdSyangbo lu u32 ret; 449f4932cfdSyangbo lu 450f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 451f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 452f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 4537657c3a7SAlbert Herranz } 4547657c3a7SAlbert Herranz 455a4071fbbSHaijun Zhang /* 456a4071fbbSHaijun Zhang * For Abort or Suspend after Stop at Block Gap, ignore the ADMA 457a4071fbbSHaijun Zhang * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) 458a4071fbbSHaijun Zhang * and Block Gap Event(IRQSTAT[BGE]) are also set. 459a4071fbbSHaijun Zhang * For Continue, apply soft reset for data(SYSCTL[RSTD]); 460a4071fbbSHaijun Zhang * and re-issue the entire read transaction from beginning. 461a4071fbbSHaijun Zhang */ 462f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask) 463a4071fbbSHaijun Zhang { 464f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 4658605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 466a4071fbbSHaijun Zhang bool applicable; 467a4071fbbSHaijun Zhang dma_addr_t dmastart; 468a4071fbbSHaijun Zhang dma_addr_t dmanow; 469a4071fbbSHaijun Zhang 470a4071fbbSHaijun Zhang applicable = (intmask & SDHCI_INT_DATA_END) && 471a4071fbbSHaijun Zhang (intmask & SDHCI_INT_BLK_GAP) && 472f4932cfdSyangbo lu (esdhc->vendor_ver == VENDOR_V_23); 473a4071fbbSHaijun Zhang if (!applicable) 474a4071fbbSHaijun Zhang return; 475a4071fbbSHaijun Zhang 476a4071fbbSHaijun Zhang host->data->error = 0; 477a4071fbbSHaijun Zhang dmastart = sg_dma_address(host->data->sg); 478a4071fbbSHaijun Zhang dmanow = dmastart + host->data->bytes_xfered; 479a4071fbbSHaijun Zhang /* 480a4071fbbSHaijun Zhang * Force update to the next DMA block boundary. 481a4071fbbSHaijun Zhang */ 482a4071fbbSHaijun Zhang dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + 483a4071fbbSHaijun Zhang SDHCI_DEFAULT_BOUNDARY_SIZE; 484a4071fbbSHaijun Zhang host->data->bytes_xfered = dmanow - dmastart; 485a4071fbbSHaijun Zhang sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); 486a4071fbbSHaijun Zhang } 487a4071fbbSHaijun Zhang 48880872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host) 4897657c3a7SAlbert Herranz { 490f4932cfdSyangbo lu u32 value; 4915552d7adSLaurentiu Tudor struct device *dev = mmc_dev(host->mmc); 4925552d7adSLaurentiu Tudor 4935552d7adSLaurentiu Tudor if (of_device_is_compatible(dev->of_node, "fsl,ls1043a-esdhc") || 4945552d7adSLaurentiu Tudor of_device_is_compatible(dev->of_node, "fsl,ls1046a-esdhc")) 4955552d7adSLaurentiu Tudor dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); 496f4932cfdSyangbo lu 497f4932cfdSyangbo lu value = sdhci_readl(host, ESDHC_DMA_SYSCTL); 498121bd08bSRussell King 499121bd08bSRussell King if (of_dma_is_coherent(dev->of_node)) 500f4932cfdSyangbo lu value |= ESDHC_DMA_SNOOP; 501121bd08bSRussell King else 502121bd08bSRussell King value &= ~ESDHC_DMA_SNOOP; 503121bd08bSRussell King 504f4932cfdSyangbo lu sdhci_writel(host, value, ESDHC_DMA_SYSCTL); 5057657c3a7SAlbert Herranz return 0; 5067657c3a7SAlbert Herranz } 5077657c3a7SAlbert Herranz 50880872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 5097657c3a7SAlbert Herranz { 510e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 51119c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 5127657c3a7SAlbert Herranz 51319c3a0efSyangbo lu if (esdhc->peripheral_clock) 51419c3a0efSyangbo lu return esdhc->peripheral_clock; 51519c3a0efSyangbo lu else 516e307148fSShawn Guo return pltfm_host->clock; 5177657c3a7SAlbert Herranz } 5187657c3a7SAlbert Herranz 51980872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 5207657c3a7SAlbert Herranz { 521e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 52219c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 52319c3a0efSyangbo lu unsigned int clock; 5247657c3a7SAlbert Herranz 52519c3a0efSyangbo lu if (esdhc->peripheral_clock) 52619c3a0efSyangbo lu clock = esdhc->peripheral_clock; 52719c3a0efSyangbo lu else 52819c3a0efSyangbo lu clock = pltfm_host->clock; 52919c3a0efSyangbo lu return clock / 256 / 16; 5307657c3a7SAlbert Herranz } 5317657c3a7SAlbert Herranz 532dd3f6983Syangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable) 533dd3f6983Syangbo lu { 534dd3f6983Syangbo lu u32 val; 535dd3f6983Syangbo lu ktime_t timeout; 536dd3f6983Syangbo lu 537dd3f6983Syangbo lu val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 538dd3f6983Syangbo lu 539dd3f6983Syangbo lu if (enable) 540dd3f6983Syangbo lu val |= ESDHC_CLOCK_SDCLKEN; 541dd3f6983Syangbo lu else 542dd3f6983Syangbo lu val &= ~ESDHC_CLOCK_SDCLKEN; 543dd3f6983Syangbo lu 544dd3f6983Syangbo lu sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL); 545dd3f6983Syangbo lu 546dd3f6983Syangbo lu /* Wait max 20 ms */ 547dd3f6983Syangbo lu timeout = ktime_add_ms(ktime_get(), 20); 548dd3f6983Syangbo lu val = ESDHC_CLOCK_STABLE; 549ea6d0273SAdrian Hunter while (1) { 550ea6d0273SAdrian Hunter bool timedout = ktime_after(ktime_get(), timeout); 551ea6d0273SAdrian Hunter 552ea6d0273SAdrian Hunter if (sdhci_readl(host, ESDHC_PRSSTAT) & val) 553ea6d0273SAdrian Hunter break; 554ea6d0273SAdrian Hunter if (timedout) { 555dd3f6983Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 556dd3f6983Syangbo lu mmc_hostname(host->mmc)); 557dd3f6983Syangbo lu break; 558dd3f6983Syangbo lu } 559dd3f6983Syangbo lu udelay(10); 560dd3f6983Syangbo lu } 561dd3f6983Syangbo lu } 562dd3f6983Syangbo lu 563*6e32f65cSYangbo Lu static void esdhc_flush_async_fifo(struct sdhci_host *host) 564*6e32f65cSYangbo Lu { 565*6e32f65cSYangbo Lu ktime_t timeout; 566*6e32f65cSYangbo Lu u32 val; 567*6e32f65cSYangbo Lu 568*6e32f65cSYangbo Lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 569*6e32f65cSYangbo Lu val |= ESDHC_FLUSH_ASYNC_FIFO; 570*6e32f65cSYangbo Lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 571*6e32f65cSYangbo Lu 572*6e32f65cSYangbo Lu /* Wait max 20 ms */ 573*6e32f65cSYangbo Lu timeout = ktime_add_ms(ktime_get(), 20); 574*6e32f65cSYangbo Lu while (1) { 575*6e32f65cSYangbo Lu bool timedout = ktime_after(ktime_get(), timeout); 576*6e32f65cSYangbo Lu 577*6e32f65cSYangbo Lu if (!(sdhci_readl(host, ESDHC_DMA_SYSCTL) & 578*6e32f65cSYangbo Lu ESDHC_FLUSH_ASYNC_FIFO)) 579*6e32f65cSYangbo Lu break; 580*6e32f65cSYangbo Lu if (timedout) { 581*6e32f65cSYangbo Lu pr_err("%s: flushing asynchronous FIFO timeout.\n", 582*6e32f65cSYangbo Lu mmc_hostname(host->mmc)); 583*6e32f65cSYangbo Lu break; 584*6e32f65cSYangbo Lu } 585*6e32f65cSYangbo Lu usleep_range(10, 20); 586*6e32f65cSYangbo Lu } 587*6e32f65cSYangbo Lu } 588*6e32f65cSYangbo Lu 589f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 590f060bc9cSJerry Huang { 591f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 5928605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 593bd455029SJoakim Tjernlund int pre_div = 1; 594d31fc00aSDong Aisheng int div = 1; 5956079e63cSYangbo Lu int division; 596e145ac45Syangbo lu ktime_t timeout; 59767fdfbdfSyinbo.zhu long fixup = 0; 598d31fc00aSDong Aisheng u32 temp; 599d31fc00aSDong Aisheng 6001650d0c7SRussell King host->mmc->actual_clock = 0; 6011650d0c7SRussell King 602dd3f6983Syangbo lu if (clock == 0) { 603dd3f6983Syangbo lu esdhc_clock_enable(host, false); 604373073efSRussell King return; 605dd3f6983Syangbo lu } 606d31fc00aSDong Aisheng 60777bd2f6fSYangbo Lu /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */ 608f4932cfdSyangbo lu if (esdhc->vendor_ver < VENDOR_V_23) 60977bd2f6fSYangbo Lu pre_div = 2; 61077bd2f6fSYangbo Lu 61167fdfbdfSyinbo.zhu if (host->mmc->card && mmc_card_sd(host->mmc->card) && 61267fdfbdfSyinbo.zhu esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY) 61367fdfbdfSyinbo.zhu fixup = esdhc->clk_fixup->sd_dflt_max_clk; 61467fdfbdfSyinbo.zhu else if (esdhc->clk_fixup) 61567fdfbdfSyinbo.zhu fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing]; 616a627f025Syangbo lu 61767fdfbdfSyinbo.zhu if (fixup && clock > fixup) 61867fdfbdfSyinbo.zhu clock = fixup; 619f060bc9cSJerry Huang 620d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 621e87d2db2Syangbo lu temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | 622e87d2db2Syangbo lu ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK); 623d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 624d31fc00aSDong Aisheng 625d31fc00aSDong Aisheng while (host->max_clk / pre_div / 16 > clock && pre_div < 256) 626d31fc00aSDong Aisheng pre_div *= 2; 627d31fc00aSDong Aisheng 628d31fc00aSDong Aisheng while (host->max_clk / pre_div / div > clock && div < 16) 629d31fc00aSDong Aisheng div++; 630d31fc00aSDong Aisheng 6316079e63cSYangbo Lu if (esdhc->quirk_limited_clk_division && 6326079e63cSYangbo Lu clock == MMC_HS200_MAX_DTR && 6336079e63cSYangbo Lu (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 || 6346079e63cSYangbo Lu host->flags & SDHCI_HS400_TUNING)) { 6356079e63cSYangbo Lu division = pre_div * div; 6366079e63cSYangbo Lu if (division <= 4) { 6376079e63cSYangbo Lu pre_div = 4; 6386079e63cSYangbo Lu div = 1; 6396079e63cSYangbo Lu } else if (division <= 8) { 6406079e63cSYangbo Lu pre_div = 4; 6416079e63cSYangbo Lu div = 2; 6426079e63cSYangbo Lu } else if (division <= 12) { 6436079e63cSYangbo Lu pre_div = 4; 6446079e63cSYangbo Lu div = 3; 6456079e63cSYangbo Lu } else { 646b11c36d5SColin Ian King pr_warn("%s: using unsupported clock division.\n", 6476079e63cSYangbo Lu mmc_hostname(host->mmc)); 6486079e63cSYangbo Lu } 6496079e63cSYangbo Lu } 6506079e63cSYangbo Lu 651d31fc00aSDong Aisheng dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 652e76b8559SDong Aisheng clock, host->max_clk / pre_div / div); 653bd455029SJoakim Tjernlund host->mmc->actual_clock = host->max_clk / pre_div / div; 654b1f378abSYinbo Zhu esdhc->div_ratio = pre_div * div; 655d31fc00aSDong Aisheng pre_div >>= 1; 656d31fc00aSDong Aisheng div--; 657d31fc00aSDong Aisheng 658d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 659d31fc00aSDong Aisheng temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 660d31fc00aSDong Aisheng | (div << ESDHC_DIVIDER_SHIFT) 661d31fc00aSDong Aisheng | (pre_div << ESDHC_PREDIV_SHIFT)); 662d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 663e87d2db2Syangbo lu 66454e08d9aSYangbo Lu if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 && 66554e08d9aSYangbo Lu clock == MMC_HS200_MAX_DTR) { 66654e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_TBCTL); 66754e08d9aSYangbo Lu sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL); 66854e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_SDCLKCTL); 66954e08d9aSYangbo Lu sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL); 67054e08d9aSYangbo Lu esdhc_clock_enable(host, true); 67154e08d9aSYangbo Lu 67254e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_DLLCFG0); 67358d0bf84SYangbo Lu temp |= ESDHC_DLL_ENABLE; 67458d0bf84SYangbo Lu if (host->mmc->actual_clock == MMC_HS200_MAX_DTR) 67558d0bf84SYangbo Lu temp |= ESDHC_DLL_FREQ_SEL; 67654e08d9aSYangbo Lu sdhci_writel(host, temp, ESDHC_DLLCFG0); 67754e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_TBCTL); 67854e08d9aSYangbo Lu sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL); 67954e08d9aSYangbo Lu 68054e08d9aSYangbo Lu esdhc_clock_enable(host, false); 681*6e32f65cSYangbo Lu esdhc_flush_async_fifo(host); 68254e08d9aSYangbo Lu } 68354e08d9aSYangbo Lu 684e87d2db2Syangbo lu /* Wait max 20 ms */ 685e145ac45Syangbo lu timeout = ktime_add_ms(ktime_get(), 20); 686ea6d0273SAdrian Hunter while (1) { 687ea6d0273SAdrian Hunter bool timedout = ktime_after(ktime_get(), timeout); 688ea6d0273SAdrian Hunter 689ea6d0273SAdrian Hunter if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE) 690ea6d0273SAdrian Hunter break; 691ea6d0273SAdrian Hunter if (timedout) { 692e87d2db2Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 693e87d2db2Syangbo lu mmc_hostname(host->mmc)); 694e87d2db2Syangbo lu return; 695e87d2db2Syangbo lu } 696e145ac45Syangbo lu udelay(10); 697f060bc9cSJerry Huang } 698f060bc9cSJerry Huang 69954e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 700e87d2db2Syangbo lu temp |= ESDHC_CLOCK_SDCLKEN; 701e87d2db2Syangbo lu sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 702e87d2db2Syangbo lu } 703e87d2db2Syangbo lu 7042317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) 70566b50a00SOded Gabbay { 70666b50a00SOded Gabbay u32 ctrl; 70766b50a00SOded Gabbay 708f4932cfdSyangbo lu ctrl = sdhci_readl(host, ESDHC_PROCTL); 709f4932cfdSyangbo lu ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); 71066b50a00SOded Gabbay switch (width) { 71166b50a00SOded Gabbay case MMC_BUS_WIDTH_8: 712f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_8BITBUS; 71366b50a00SOded Gabbay break; 71466b50a00SOded Gabbay 71566b50a00SOded Gabbay case MMC_BUS_WIDTH_4: 716f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_4BITBUS; 71766b50a00SOded Gabbay break; 71866b50a00SOded Gabbay 71966b50a00SOded Gabbay default: 72066b50a00SOded Gabbay break; 72166b50a00SOded Gabbay } 72266b50a00SOded Gabbay 723f4932cfdSyangbo lu sdhci_writel(host, ctrl, ESDHC_PROCTL); 72466b50a00SOded Gabbay } 72566b50a00SOded Gabbay 726304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask) 727304f0a98SAlessio Igor Bogani { 72848e304ccSYangbo Lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 72948e304ccSYangbo Lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 730f2bc6000Syinbo.zhu u32 val; 731f2bc6000Syinbo.zhu 732304f0a98SAlessio Igor Bogani sdhci_reset(host, mask); 733304f0a98SAlessio Igor Bogani 734304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); 735304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); 736f2bc6000Syinbo.zhu 7375dd19552SYinbo Zhu if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) 7385dd19552SYinbo Zhu mdelay(5); 7395dd19552SYinbo Zhu 740f2bc6000Syinbo.zhu if (mask & SDHCI_RESET_ALL) { 741f2bc6000Syinbo.zhu val = sdhci_readl(host, ESDHC_TBCTL); 742f2bc6000Syinbo.zhu val &= ~ESDHC_TB_EN; 743f2bc6000Syinbo.zhu sdhci_writel(host, val, ESDHC_TBCTL); 74448e304ccSYangbo Lu 74548e304ccSYangbo Lu if (esdhc->quirk_unreliable_pulse_detection) { 74648e304ccSYangbo Lu val = sdhci_readl(host, ESDHC_DLLCFG1); 74748e304ccSYangbo Lu val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL; 74848e304ccSYangbo Lu sdhci_writel(host, val, ESDHC_DLLCFG1); 74948e304ccSYangbo Lu } 750f2bc6000Syinbo.zhu } 751304f0a98SAlessio Igor Bogani } 752304f0a98SAlessio Igor Bogani 753ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific 754ea35645aSyangbo lu * configuration and status registers for the device. There is a 755ea35645aSyangbo lu * SDHC IO VSEL control register on SCFG for some platforms. It's 756ea35645aSyangbo lu * used to support SDHC IO voltage switching. 757ea35645aSyangbo lu */ 758ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = { 759ea35645aSyangbo lu { .compatible = "fsl,t1040-scfg", }, 760ea35645aSyangbo lu { .compatible = "fsl,ls1012a-scfg", }, 761ea35645aSyangbo lu { .compatible = "fsl,ls1046a-scfg", }, 762ea35645aSyangbo lu {} 763ea35645aSyangbo lu }; 764ea35645aSyangbo lu 765ea35645aSyangbo lu /* SDHC IO VSEL control register definition */ 766ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR 0x408 767ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN 0x80000000 768ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL 0x60000000 769ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS 0x00000001 770ea35645aSyangbo lu 771ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc, 772ea35645aSyangbo lu struct mmc_ios *ios) 773ea35645aSyangbo lu { 774ea35645aSyangbo lu struct sdhci_host *host = mmc_priv(mmc); 775ea35645aSyangbo lu struct device_node *scfg_node; 776ea35645aSyangbo lu void __iomem *scfg_base = NULL; 777ea35645aSyangbo lu u32 sdhciovselcr; 778ea35645aSyangbo lu u32 val; 779ea35645aSyangbo lu 780ea35645aSyangbo lu /* 781ea35645aSyangbo lu * Signal Voltage Switching is only applicable for Host Controllers 782ea35645aSyangbo lu * v3.00 and above. 783ea35645aSyangbo lu */ 784ea35645aSyangbo lu if (host->version < SDHCI_SPEC_300) 785ea35645aSyangbo lu return 0; 786ea35645aSyangbo lu 787ea35645aSyangbo lu val = sdhci_readl(host, ESDHC_PROCTL); 788ea35645aSyangbo lu 789ea35645aSyangbo lu switch (ios->signal_voltage) { 790ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_330: 791ea35645aSyangbo lu val &= ~ESDHC_VOLT_SEL; 792ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 793ea35645aSyangbo lu return 0; 794ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_180: 795ea35645aSyangbo lu scfg_node = of_find_matching_node(NULL, scfg_device_ids); 796ea35645aSyangbo lu if (scfg_node) 797ea35645aSyangbo lu scfg_base = of_iomap(scfg_node, 0); 798ea35645aSyangbo lu if (scfg_base) { 799ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 800ea35645aSyangbo lu SDHCIOVSELCR_VSELVAL; 801ea35645aSyangbo lu iowrite32be(sdhciovselcr, 802ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 803ea35645aSyangbo lu 804ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 805ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 806ea35645aSyangbo lu mdelay(5); 807ea35645aSyangbo lu 808ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 809ea35645aSyangbo lu SDHCIOVSELCR_SDHC_VS; 810ea35645aSyangbo lu iowrite32be(sdhciovselcr, 811ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 812ea35645aSyangbo lu iounmap(scfg_base); 813ea35645aSyangbo lu } else { 814ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 815ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 816ea35645aSyangbo lu } 817ea35645aSyangbo lu return 0; 818ea35645aSyangbo lu default: 819ea35645aSyangbo lu return 0; 820ea35645aSyangbo lu } 821ea35645aSyangbo lu } 822ea35645aSyangbo lu 823b1f378abSYinbo Zhu static struct soc_device_attribute soc_fixup_tuning[] = { 824b1f378abSYinbo Zhu { .family = "QorIQ T1040", .revision = "1.0", }, 825b1f378abSYinbo Zhu { .family = "QorIQ T2080", .revision = "1.0", }, 826b1f378abSYinbo Zhu { .family = "QorIQ T1023", .revision = "1.0", }, 827b1f378abSYinbo Zhu { .family = "QorIQ LS1021A", .revision = "1.0", }, 828b1f378abSYinbo Zhu { .family = "QorIQ LS1080A", .revision = "1.0", }, 829b1f378abSYinbo Zhu { .family = "QorIQ LS2080A", .revision = "1.0", }, 830b1f378abSYinbo Zhu { .family = "QorIQ LS1012A", .revision = "1.0", }, 831b1f378abSYinbo Zhu { .family = "QorIQ LS1043A", .revision = "1.*", }, 832b1f378abSYinbo Zhu { .family = "QorIQ LS1046A", .revision = "1.0", }, 833b1f378abSYinbo Zhu { }, 834b1f378abSYinbo Zhu }; 835b1f378abSYinbo Zhu 83654e08d9aSYangbo Lu static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable) 837ba49cbd0Syangbo lu { 838ba49cbd0Syangbo lu u32 val; 839ba49cbd0Syangbo lu 840ba49cbd0Syangbo lu esdhc_clock_enable(host, false); 841*6e32f65cSYangbo Lu esdhc_flush_async_fifo(host); 842ba49cbd0Syangbo lu 843ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_TBCTL); 84454e08d9aSYangbo Lu if (enable) 845ba49cbd0Syangbo lu val |= ESDHC_TB_EN; 84654e08d9aSYangbo Lu else 84754e08d9aSYangbo Lu val &= ~ESDHC_TB_EN; 848ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_TBCTL); 849ba49cbd0Syangbo lu 85054e08d9aSYangbo Lu esdhc_clock_enable(host, true); 85154e08d9aSYangbo Lu } 85254e08d9aSYangbo Lu 85354e08d9aSYangbo Lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) 85454e08d9aSYangbo Lu { 85554e08d9aSYangbo Lu struct sdhci_host *host = mmc_priv(mmc); 85654e08d9aSYangbo Lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 85754e08d9aSYangbo Lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 85854e08d9aSYangbo Lu bool hs400_tuning; 85904509d77SYangbo Lu unsigned int clk; 86054e08d9aSYangbo Lu u32 val; 86154e08d9aSYangbo Lu int ret; 86254e08d9aSYangbo Lu 86304509d77SYangbo Lu /* For tuning mode, the sd clock divisor value 86404509d77SYangbo Lu * must be larger than 3 according to reference manual. 86504509d77SYangbo Lu */ 86604509d77SYangbo Lu clk = esdhc->peripheral_clock / 3; 86704509d77SYangbo Lu if (host->clock > clk) 86804509d77SYangbo Lu esdhc_of_set_clock(host, clk); 86904509d77SYangbo Lu 8706079e63cSYangbo Lu if (esdhc->quirk_limited_clk_division && 8716079e63cSYangbo Lu host->flags & SDHCI_HS400_TUNING) 8726079e63cSYangbo Lu esdhc_of_set_clock(host, host->clock); 8736079e63cSYangbo Lu 87454e08d9aSYangbo Lu esdhc_tuning_block_enable(host, true); 87554e08d9aSYangbo Lu 87654e08d9aSYangbo Lu hs400_tuning = host->flags & SDHCI_HS400_TUNING; 87754e08d9aSYangbo Lu ret = sdhci_execute_tuning(mmc, opcode); 87854e08d9aSYangbo Lu 87954e08d9aSYangbo Lu if (hs400_tuning) { 88054e08d9aSYangbo Lu val = sdhci_readl(host, ESDHC_SDTIMNGCTL); 88154e08d9aSYangbo Lu val |= ESDHC_FLW_CTL_BG; 88254e08d9aSYangbo Lu sdhci_writel(host, val, ESDHC_SDTIMNGCTL); 88354e08d9aSYangbo Lu } 88454e08d9aSYangbo Lu 885b1f378abSYinbo Zhu if (host->tuning_err == -EAGAIN && esdhc->quirk_fixup_tuning) { 886b1f378abSYinbo Zhu 887b1f378abSYinbo Zhu /* program TBPTR[TB_WNDW_END_PTR] = 3*DIV_RATIO and 888b1f378abSYinbo Zhu * program TBPTR[TB_WNDW_START_PTR] = 5*DIV_RATIO 889b1f378abSYinbo Zhu */ 890b1f378abSYinbo Zhu val = sdhci_readl(host, ESDHC_TBPTR); 891b1f378abSYinbo Zhu val = (val & ~((0x7f << 8) | 0x7f)) | 892b1f378abSYinbo Zhu (3 * esdhc->div_ratio) | ((5 * esdhc->div_ratio) << 8); 893b1f378abSYinbo Zhu sdhci_writel(host, val, ESDHC_TBPTR); 894b1f378abSYinbo Zhu 895b1f378abSYinbo Zhu /* program the software tuning mode by setting 896b1f378abSYinbo Zhu * TBCTL[TB_MODE]=2'h3 897b1f378abSYinbo Zhu */ 898b1f378abSYinbo Zhu val = sdhci_readl(host, ESDHC_TBCTL); 899b1f378abSYinbo Zhu val |= 0x3; 900b1f378abSYinbo Zhu sdhci_writel(host, val, ESDHC_TBCTL); 901b1f378abSYinbo Zhu sdhci_execute_tuning(mmc, opcode); 902b1f378abSYinbo Zhu } 90354e08d9aSYangbo Lu return ret; 90454e08d9aSYangbo Lu } 90554e08d9aSYangbo Lu 90654e08d9aSYangbo Lu static void esdhc_set_uhs_signaling(struct sdhci_host *host, 90754e08d9aSYangbo Lu unsigned int timing) 90854e08d9aSYangbo Lu { 90954e08d9aSYangbo Lu if (timing == MMC_TIMING_MMC_HS400) 91054e08d9aSYangbo Lu esdhc_tuning_block_enable(host, true); 91154e08d9aSYangbo Lu else 91254e08d9aSYangbo Lu sdhci_set_uhs_signaling(host, timing); 913ba49cbd0Syangbo lu } 914ba49cbd0Syangbo lu 915b214fe59SYinbo Zhu static u32 esdhc_irq(struct sdhci_host *host, u32 intmask) 916b214fe59SYinbo Zhu { 917b214fe59SYinbo Zhu u32 command; 918b214fe59SYinbo Zhu 919b214fe59SYinbo Zhu if (of_find_compatible_node(NULL, NULL, 920b214fe59SYinbo Zhu "fsl,p2020-esdhc")) { 921b214fe59SYinbo Zhu command = SDHCI_GET_CMD(sdhci_readw(host, 922b214fe59SYinbo Zhu SDHCI_COMMAND)); 923b214fe59SYinbo Zhu if (command == MMC_WRITE_MULTIPLE_BLOCK && 924b214fe59SYinbo Zhu sdhci_readw(host, SDHCI_BLOCK_COUNT) && 925b214fe59SYinbo Zhu intmask & SDHCI_INT_DATA_END) { 926b214fe59SYinbo Zhu intmask &= ~SDHCI_INT_DATA_END; 927b214fe59SYinbo Zhu sdhci_writel(host, SDHCI_INT_DATA_END, 928b214fe59SYinbo Zhu SDHCI_INT_STATUS); 929b214fe59SYinbo Zhu } 930b214fe59SYinbo Zhu } 931b214fe59SYinbo Zhu return intmask; 932b214fe59SYinbo Zhu } 933b214fe59SYinbo Zhu 9349e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP 935723f7924SRussell King static u32 esdhc_proctl; 936723f7924SRussell King static int esdhc_of_suspend(struct device *dev) 937723f7924SRussell King { 938723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 939723f7924SRussell King 940f4932cfdSyangbo lu esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); 941723f7924SRussell King 942d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 943d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 944d38dcad4SAdrian Hunter 945723f7924SRussell King return sdhci_suspend_host(host); 946723f7924SRussell King } 947723f7924SRussell King 94806732b84SUlf Hansson static int esdhc_of_resume(struct device *dev) 949723f7924SRussell King { 950723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 951723f7924SRussell King int ret = sdhci_resume_host(host); 952723f7924SRussell King 953723f7924SRussell King if (ret == 0) { 954723f7924SRussell King /* Isn't this already done by sdhci_resume_host() ? --rmk */ 955723f7924SRussell King esdhc_of_enable_dma(host); 956f4932cfdSyangbo lu sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 957723f7924SRussell King } 958723f7924SRussell King return ret; 959723f7924SRussell King } 960723f7924SRussell King #endif 961723f7924SRussell King 9629e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops, 9639e48b336SUlf Hansson esdhc_of_suspend, 9649e48b336SUlf Hansson esdhc_of_resume); 9659e48b336SUlf Hansson 966f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = { 967f4932cfdSyangbo lu .read_l = esdhc_be_readl, 968f4932cfdSyangbo lu .read_w = esdhc_be_readw, 969f4932cfdSyangbo lu .read_b = esdhc_be_readb, 970f4932cfdSyangbo lu .write_l = esdhc_be_writel, 971f4932cfdSyangbo lu .write_w = esdhc_be_writew, 972f4932cfdSyangbo lu .write_b = esdhc_be_writeb, 973f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 974f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 975f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 976f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 977f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 978f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 979f4932cfdSyangbo lu .reset = esdhc_reset, 98054e08d9aSYangbo Lu .set_uhs_signaling = esdhc_set_uhs_signaling, 981b214fe59SYinbo Zhu .irq = esdhc_irq, 982f4932cfdSyangbo lu }; 983f4932cfdSyangbo lu 984f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = { 985f4932cfdSyangbo lu .read_l = esdhc_le_readl, 986f4932cfdSyangbo lu .read_w = esdhc_le_readw, 987f4932cfdSyangbo lu .read_b = esdhc_le_readb, 988f4932cfdSyangbo lu .write_l = esdhc_le_writel, 989f4932cfdSyangbo lu .write_w = esdhc_le_writew, 990f4932cfdSyangbo lu .write_b = esdhc_le_writeb, 991f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 992f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 993f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 994f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 995f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 996f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 997f4932cfdSyangbo lu .reset = esdhc_reset, 99854e08d9aSYangbo Lu .set_uhs_signaling = esdhc_set_uhs_signaling, 999b214fe59SYinbo Zhu .irq = esdhc_irq, 1000f4932cfdSyangbo lu }; 1001f4932cfdSyangbo lu 1002f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { 1003e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 1004e9acc77dSyangbo lu #ifdef CONFIG_PPC 1005e9acc77dSyangbo lu SDHCI_QUIRK_BROKEN_CARD_DETECTION | 1006e9acc77dSyangbo lu #endif 1007e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 1008e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 1009f4932cfdSyangbo lu .ops = &sdhci_esdhc_be_ops, 10107657c3a7SAlbert Herranz }; 101138576af1SShawn Guo 1012f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { 1013e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 1014e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 1015e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 1016f4932cfdSyangbo lu .ops = &sdhci_esdhc_le_ops, 1017f4932cfdSyangbo lu }; 1018f4932cfdSyangbo lu 1019151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = { 1020151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "1.0", }, 1021151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "2.0", }, 1022151ede40Syangbo lu { }, 1023151ede40Syangbo lu }; 1024151ede40Syangbo lu 10256079e63cSYangbo Lu static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = { 10266079e63cSYangbo Lu { .family = "QorIQ LX2160A", .revision = "1.0", }, 10278e9a6919SYinbo Zhu { .family = "QorIQ LX2160A", .revision = "2.0", }, 10285f3ad196SYinbo Zhu { .family = "QorIQ LS1028A", .revision = "1.0", }, 10296079e63cSYangbo Lu { }, 10306079e63cSYangbo Lu }; 10316079e63cSYangbo Lu 103248e304ccSYangbo Lu static struct soc_device_attribute soc_unreliable_pulse_detection[] = { 103348e304ccSYangbo Lu { .family = "QorIQ LX2160A", .revision = "1.0", }, 103448e304ccSYangbo Lu { }, 103548e304ccSYangbo Lu }; 103648e304ccSYangbo Lu 1037f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) 1038f4932cfdSyangbo lu { 103967fdfbdfSyinbo.zhu const struct of_device_id *match; 1040f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host; 1041f4932cfdSyangbo lu struct sdhci_esdhc *esdhc; 104219c3a0efSyangbo lu struct device_node *np; 104319c3a0efSyangbo lu struct clk *clk; 104419c3a0efSyangbo lu u32 val; 1045f4932cfdSyangbo lu u16 host_ver; 1046f4932cfdSyangbo lu 1047f4932cfdSyangbo lu pltfm_host = sdhci_priv(host); 10488605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 1049f4932cfdSyangbo lu 1050f4932cfdSyangbo lu host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); 1051f4932cfdSyangbo lu esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> 1052f4932cfdSyangbo lu SDHCI_VENDOR_VER_SHIFT; 1053f4932cfdSyangbo lu esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; 1054151ede40Syangbo lu if (soc_device_match(soc_incorrect_hostver)) 1055151ede40Syangbo lu esdhc->quirk_incorrect_hostver = true; 1056151ede40Syangbo lu else 1057151ede40Syangbo lu esdhc->quirk_incorrect_hostver = false; 105819c3a0efSyangbo lu 10596079e63cSYangbo Lu if (soc_device_match(soc_fixup_sdhc_clkdivs)) 10606079e63cSYangbo Lu esdhc->quirk_limited_clk_division = true; 10616079e63cSYangbo Lu else 10626079e63cSYangbo Lu esdhc->quirk_limited_clk_division = false; 10636079e63cSYangbo Lu 106448e304ccSYangbo Lu if (soc_device_match(soc_unreliable_pulse_detection)) 106548e304ccSYangbo Lu esdhc->quirk_unreliable_pulse_detection = true; 106648e304ccSYangbo Lu else 106748e304ccSYangbo Lu esdhc->quirk_unreliable_pulse_detection = false; 106848e304ccSYangbo Lu 106967fdfbdfSyinbo.zhu match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node); 107067fdfbdfSyinbo.zhu if (match) 107167fdfbdfSyinbo.zhu esdhc->clk_fixup = match->data; 107219c3a0efSyangbo lu np = pdev->dev.of_node; 107319c3a0efSyangbo lu clk = of_clk_get(np, 0); 107419c3a0efSyangbo lu if (!IS_ERR(clk)) { 107519c3a0efSyangbo lu /* 107619c3a0efSyangbo lu * esdhc->peripheral_clock would be assigned with a value 107719c3a0efSyangbo lu * which is eSDHC base clock when use periperal clock. 1078791463baSYangbo Lu * For some platforms, the clock value got by common clk 1079791463baSYangbo Lu * API is peripheral clock while the eSDHC base clock is 1080791463baSYangbo Lu * 1/2 peripheral clock. 108119c3a0efSyangbo lu */ 1082791463baSYangbo Lu if (of_device_is_compatible(np, "fsl,ls1046a-esdhc") || 1083791463baSYangbo Lu of_device_is_compatible(np, "fsl,ls1028a-esdhc")) 108419c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk) / 2; 108519c3a0efSyangbo lu else 108619c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk); 108719c3a0efSyangbo lu 108819c3a0efSyangbo lu clk_put(clk); 108919c3a0efSyangbo lu } 109019c3a0efSyangbo lu 109119c3a0efSyangbo lu if (esdhc->peripheral_clock) { 109219c3a0efSyangbo lu esdhc_clock_enable(host, false); 109319c3a0efSyangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 109419c3a0efSyangbo lu val |= ESDHC_PERIPHERAL_CLK_SEL; 109519c3a0efSyangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 109619c3a0efSyangbo lu esdhc_clock_enable(host, true); 109719c3a0efSyangbo lu } 1098f4932cfdSyangbo lu } 1099f4932cfdSyangbo lu 110054e08d9aSYangbo Lu static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc) 110154e08d9aSYangbo Lu { 110254e08d9aSYangbo Lu esdhc_tuning_block_enable(mmc_priv(mmc), false); 110354e08d9aSYangbo Lu return 0; 110454e08d9aSYangbo Lu } 110554e08d9aSYangbo Lu 1106c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 110738576af1SShawn Guo { 110866b50a00SOded Gabbay struct sdhci_host *host; 1109dcaff04dSOded Gabbay struct device_node *np; 11101ef5e49eSyangbo lu struct sdhci_pltfm_host *pltfm_host; 11111ef5e49eSyangbo lu struct sdhci_esdhc *esdhc; 111266b50a00SOded Gabbay int ret; 111366b50a00SOded Gabbay 1114f4932cfdSyangbo lu np = pdev->dev.of_node; 1115f4932cfdSyangbo lu 1116150d4240SJulia Lawall if (of_property_read_bool(np, "little-endian")) 11178605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, 11188605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 1119f4932cfdSyangbo lu else 11208605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, 11218605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 1122f4932cfdSyangbo lu 112366b50a00SOded Gabbay if (IS_ERR(host)) 112466b50a00SOded Gabbay return PTR_ERR(host); 112566b50a00SOded Gabbay 1126ea35645aSyangbo lu host->mmc_host_ops.start_signal_voltage_switch = 1127ea35645aSyangbo lu esdhc_signal_voltage_switch; 1128ba49cbd0Syangbo lu host->mmc_host_ops.execute_tuning = esdhc_execute_tuning; 112954e08d9aSYangbo Lu host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr; 11306b236f37Syangbo lu host->tuning_delay = 1; 1131ea35645aSyangbo lu 1132f4932cfdSyangbo lu esdhc_init(pdev, host); 1133f4932cfdSyangbo lu 113466b50a00SOded Gabbay sdhci_get_of_property(pdev); 113566b50a00SOded Gabbay 11361ef5e49eSyangbo lu pltfm_host = sdhci_priv(host); 11378605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 1138b1f378abSYinbo Zhu if (soc_device_match(soc_fixup_tuning)) 1139b1f378abSYinbo Zhu esdhc->quirk_fixup_tuning = true; 1140b1f378abSYinbo Zhu else 1141b1f378abSYinbo Zhu esdhc->quirk_fixup_tuning = false; 1142b1f378abSYinbo Zhu 11431ef5e49eSyangbo lu if (esdhc->vendor_ver == VENDOR_V_22) 11441ef5e49eSyangbo lu host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 11451ef5e49eSyangbo lu 11461ef5e49eSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) 11471ef5e49eSyangbo lu host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 11481ef5e49eSyangbo lu 114905cb6b2aSYinbo Zhu if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) { 1150a46e4271SYinbo Zhu host->quirks2 |= SDHCI_QUIRK_RESET_AFTER_REQUEST; 115105cb6b2aSYinbo Zhu host->quirks2 |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 115205cb6b2aSYinbo Zhu } 1153a46e4271SYinbo Zhu 115474fd5e30SYangbo Lu if (of_device_is_compatible(np, "fsl,p5040-esdhc") || 115574fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p5020-esdhc") || 115674fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p4080-esdhc") || 115774fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p1020-esdhc") || 1158e9acc77dSyangbo lu of_device_is_compatible(np, "fsl,t1040-esdhc")) 115974fd5e30SYangbo Lu host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 116074fd5e30SYangbo Lu 1161a22950c8Syangbo lu if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) 1162a22950c8Syangbo lu host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 1163a22950c8Syangbo lu 11641f1929f3SYangbo Lu esdhc->quirk_ignore_data_inhibit = false; 1165dcaff04dSOded Gabbay if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { 1166dcaff04dSOded Gabbay /* 1167dcaff04dSOded Gabbay * Freescale messed up with P2020 as it has a non-standard 1168dcaff04dSOded Gabbay * host control register 1169dcaff04dSOded Gabbay */ 1170dcaff04dSOded Gabbay host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; 11711f1929f3SYangbo Lu esdhc->quirk_ignore_data_inhibit = true; 1172dcaff04dSOded Gabbay } 1173dcaff04dSOded Gabbay 117466b50a00SOded Gabbay /* call to generic mmc_of_parse to support additional capabilities */ 1175f0991408SUlf Hansson ret = mmc_of_parse(host->mmc); 1176f0991408SUlf Hansson if (ret) 1177f0991408SUlf Hansson goto err; 1178f0991408SUlf Hansson 1179490104acSHaijun Zhang mmc_of_parse_voltage(np, &host->ocr_mask); 118066b50a00SOded Gabbay 118166b50a00SOded Gabbay ret = sdhci_add_host(host); 118266b50a00SOded Gabbay if (ret) 1183f0991408SUlf Hansson goto err; 118466b50a00SOded Gabbay 1185f0991408SUlf Hansson return 0; 1186f0991408SUlf Hansson err: 1187f0991408SUlf Hansson sdhci_pltfm_free(pdev); 118866b50a00SOded Gabbay return ret; 118938576af1SShawn Guo } 119038576af1SShawn Guo 119138576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 119238576af1SShawn Guo .driver = { 119338576af1SShawn Guo .name = "sdhci-esdhc", 119438576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 11959e48b336SUlf Hansson .pm = &esdhc_of_dev_pm_ops, 119638576af1SShawn Guo }, 119738576af1SShawn Guo .probe = sdhci_esdhc_probe, 1198caebcae9SKevin Hao .remove = sdhci_pltfm_unregister, 119938576af1SShawn Guo }; 120038576af1SShawn Guo 1201d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 120238576af1SShawn Guo 120338576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 120438576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 120538576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 120638576af1SShawn Guo MODULE_LICENSE("GPL v2"); 1207