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); 498f4932cfdSyangbo lu value |= ESDHC_DMA_SNOOP; 499f4932cfdSyangbo lu sdhci_writel(host, value, ESDHC_DMA_SYSCTL); 5007657c3a7SAlbert Herranz return 0; 5017657c3a7SAlbert Herranz } 5027657c3a7SAlbert Herranz 50380872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 5047657c3a7SAlbert Herranz { 505e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 50619c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 5077657c3a7SAlbert Herranz 50819c3a0efSyangbo lu if (esdhc->peripheral_clock) 50919c3a0efSyangbo lu return esdhc->peripheral_clock; 51019c3a0efSyangbo lu else 511e307148fSShawn Guo return pltfm_host->clock; 5127657c3a7SAlbert Herranz } 5137657c3a7SAlbert Herranz 51480872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 5157657c3a7SAlbert Herranz { 516e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 51719c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 51819c3a0efSyangbo lu unsigned int clock; 5197657c3a7SAlbert Herranz 52019c3a0efSyangbo lu if (esdhc->peripheral_clock) 52119c3a0efSyangbo lu clock = esdhc->peripheral_clock; 52219c3a0efSyangbo lu else 52319c3a0efSyangbo lu clock = pltfm_host->clock; 52419c3a0efSyangbo lu return clock / 256 / 16; 5257657c3a7SAlbert Herranz } 5267657c3a7SAlbert Herranz 527dd3f6983Syangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable) 528dd3f6983Syangbo lu { 529dd3f6983Syangbo lu u32 val; 530dd3f6983Syangbo lu ktime_t timeout; 531dd3f6983Syangbo lu 532dd3f6983Syangbo lu val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 533dd3f6983Syangbo lu 534dd3f6983Syangbo lu if (enable) 535dd3f6983Syangbo lu val |= ESDHC_CLOCK_SDCLKEN; 536dd3f6983Syangbo lu else 537dd3f6983Syangbo lu val &= ~ESDHC_CLOCK_SDCLKEN; 538dd3f6983Syangbo lu 539dd3f6983Syangbo lu sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL); 540dd3f6983Syangbo lu 541dd3f6983Syangbo lu /* Wait max 20 ms */ 542dd3f6983Syangbo lu timeout = ktime_add_ms(ktime_get(), 20); 543dd3f6983Syangbo lu val = ESDHC_CLOCK_STABLE; 544ea6d0273SAdrian Hunter while (1) { 545ea6d0273SAdrian Hunter bool timedout = ktime_after(ktime_get(), timeout); 546ea6d0273SAdrian Hunter 547ea6d0273SAdrian Hunter if (sdhci_readl(host, ESDHC_PRSSTAT) & val) 548ea6d0273SAdrian Hunter break; 549ea6d0273SAdrian Hunter if (timedout) { 550dd3f6983Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 551dd3f6983Syangbo lu mmc_hostname(host->mmc)); 552dd3f6983Syangbo lu break; 553dd3f6983Syangbo lu } 554dd3f6983Syangbo lu udelay(10); 555dd3f6983Syangbo lu } 556dd3f6983Syangbo lu } 557dd3f6983Syangbo lu 558f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 559f060bc9cSJerry Huang { 560f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 5618605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 562bd455029SJoakim Tjernlund int pre_div = 1; 563d31fc00aSDong Aisheng int div = 1; 5646079e63cSYangbo Lu int division; 565e145ac45Syangbo lu ktime_t timeout; 56667fdfbdfSyinbo.zhu long fixup = 0; 567d31fc00aSDong Aisheng u32 temp; 568d31fc00aSDong Aisheng 5691650d0c7SRussell King host->mmc->actual_clock = 0; 5701650d0c7SRussell King 571dd3f6983Syangbo lu if (clock == 0) { 572dd3f6983Syangbo lu esdhc_clock_enable(host, false); 573373073efSRussell King return; 574dd3f6983Syangbo lu } 575d31fc00aSDong Aisheng 57677bd2f6fSYangbo Lu /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */ 577f4932cfdSyangbo lu if (esdhc->vendor_ver < VENDOR_V_23) 57877bd2f6fSYangbo Lu pre_div = 2; 57977bd2f6fSYangbo Lu 58067fdfbdfSyinbo.zhu if (host->mmc->card && mmc_card_sd(host->mmc->card) && 58167fdfbdfSyinbo.zhu esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY) 58267fdfbdfSyinbo.zhu fixup = esdhc->clk_fixup->sd_dflt_max_clk; 58367fdfbdfSyinbo.zhu else if (esdhc->clk_fixup) 58467fdfbdfSyinbo.zhu fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing]; 585a627f025Syangbo lu 58667fdfbdfSyinbo.zhu if (fixup && clock > fixup) 58767fdfbdfSyinbo.zhu clock = fixup; 588f060bc9cSJerry Huang 589d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 590e87d2db2Syangbo lu temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | 591e87d2db2Syangbo lu ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK); 592d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 593d31fc00aSDong Aisheng 594d31fc00aSDong Aisheng while (host->max_clk / pre_div / 16 > clock && pre_div < 256) 595d31fc00aSDong Aisheng pre_div *= 2; 596d31fc00aSDong Aisheng 597d31fc00aSDong Aisheng while (host->max_clk / pre_div / div > clock && div < 16) 598d31fc00aSDong Aisheng div++; 599d31fc00aSDong Aisheng 6006079e63cSYangbo Lu if (esdhc->quirk_limited_clk_division && 6016079e63cSYangbo Lu clock == MMC_HS200_MAX_DTR && 6026079e63cSYangbo Lu (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 || 6036079e63cSYangbo Lu host->flags & SDHCI_HS400_TUNING)) { 6046079e63cSYangbo Lu division = pre_div * div; 6056079e63cSYangbo Lu if (division <= 4) { 6066079e63cSYangbo Lu pre_div = 4; 6076079e63cSYangbo Lu div = 1; 6086079e63cSYangbo Lu } else if (division <= 8) { 6096079e63cSYangbo Lu pre_div = 4; 6106079e63cSYangbo Lu div = 2; 6116079e63cSYangbo Lu } else if (division <= 12) { 6126079e63cSYangbo Lu pre_div = 4; 6136079e63cSYangbo Lu div = 3; 6146079e63cSYangbo Lu } else { 615b11c36d5SColin Ian King pr_warn("%s: using unsupported clock division.\n", 6166079e63cSYangbo Lu mmc_hostname(host->mmc)); 6176079e63cSYangbo Lu } 6186079e63cSYangbo Lu } 6196079e63cSYangbo Lu 620d31fc00aSDong Aisheng dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 621e76b8559SDong Aisheng clock, host->max_clk / pre_div / div); 622bd455029SJoakim Tjernlund host->mmc->actual_clock = host->max_clk / pre_div / div; 623b1f378abSYinbo Zhu esdhc->div_ratio = pre_div * div; 624d31fc00aSDong Aisheng pre_div >>= 1; 625d31fc00aSDong Aisheng div--; 626d31fc00aSDong Aisheng 627d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 628d31fc00aSDong Aisheng temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 629d31fc00aSDong Aisheng | (div << ESDHC_DIVIDER_SHIFT) 630d31fc00aSDong Aisheng | (pre_div << ESDHC_PREDIV_SHIFT)); 631d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 632e87d2db2Syangbo lu 63354e08d9aSYangbo Lu if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400 && 63454e08d9aSYangbo Lu clock == MMC_HS200_MAX_DTR) { 63554e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_TBCTL); 63654e08d9aSYangbo Lu sdhci_writel(host, temp | ESDHC_HS400_MODE, ESDHC_TBCTL); 63754e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_SDCLKCTL); 63854e08d9aSYangbo Lu sdhci_writel(host, temp | ESDHC_CMD_CLK_CTL, ESDHC_SDCLKCTL); 63954e08d9aSYangbo Lu esdhc_clock_enable(host, true); 64054e08d9aSYangbo Lu 64154e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_DLLCFG0); 64258d0bf84SYangbo Lu temp |= ESDHC_DLL_ENABLE; 64358d0bf84SYangbo Lu if (host->mmc->actual_clock == MMC_HS200_MAX_DTR) 64458d0bf84SYangbo Lu temp |= ESDHC_DLL_FREQ_SEL; 64554e08d9aSYangbo Lu sdhci_writel(host, temp, ESDHC_DLLCFG0); 64654e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_TBCTL); 64754e08d9aSYangbo Lu sdhci_writel(host, temp | ESDHC_HS400_WNDW_ADJUST, ESDHC_TBCTL); 64854e08d9aSYangbo Lu 64954e08d9aSYangbo Lu esdhc_clock_enable(host, false); 65054e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_DMA_SYSCTL); 65154e08d9aSYangbo Lu temp |= ESDHC_FLUSH_ASYNC_FIFO; 65254e08d9aSYangbo Lu sdhci_writel(host, temp, ESDHC_DMA_SYSCTL); 65354e08d9aSYangbo Lu } 65454e08d9aSYangbo Lu 655e87d2db2Syangbo lu /* Wait max 20 ms */ 656e145ac45Syangbo lu timeout = ktime_add_ms(ktime_get(), 20); 657ea6d0273SAdrian Hunter while (1) { 658ea6d0273SAdrian Hunter bool timedout = ktime_after(ktime_get(), timeout); 659ea6d0273SAdrian Hunter 660ea6d0273SAdrian Hunter if (sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE) 661ea6d0273SAdrian Hunter break; 662ea6d0273SAdrian Hunter if (timedout) { 663e87d2db2Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 664e87d2db2Syangbo lu mmc_hostname(host->mmc)); 665e87d2db2Syangbo lu return; 666e87d2db2Syangbo lu } 667e145ac45Syangbo lu udelay(10); 668f060bc9cSJerry Huang } 669f060bc9cSJerry Huang 67054e08d9aSYangbo Lu temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 671e87d2db2Syangbo lu temp |= ESDHC_CLOCK_SDCLKEN; 672e87d2db2Syangbo lu sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 673e87d2db2Syangbo lu } 674e87d2db2Syangbo lu 6752317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) 67666b50a00SOded Gabbay { 67766b50a00SOded Gabbay u32 ctrl; 67866b50a00SOded Gabbay 679f4932cfdSyangbo lu ctrl = sdhci_readl(host, ESDHC_PROCTL); 680f4932cfdSyangbo lu ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); 68166b50a00SOded Gabbay switch (width) { 68266b50a00SOded Gabbay case MMC_BUS_WIDTH_8: 683f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_8BITBUS; 68466b50a00SOded Gabbay break; 68566b50a00SOded Gabbay 68666b50a00SOded Gabbay case MMC_BUS_WIDTH_4: 687f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_4BITBUS; 68866b50a00SOded Gabbay break; 68966b50a00SOded Gabbay 69066b50a00SOded Gabbay default: 69166b50a00SOded Gabbay break; 69266b50a00SOded Gabbay } 69366b50a00SOded Gabbay 694f4932cfdSyangbo lu sdhci_writel(host, ctrl, ESDHC_PROCTL); 69566b50a00SOded Gabbay } 69666b50a00SOded Gabbay 697304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask) 698304f0a98SAlessio Igor Bogani { 69948e304ccSYangbo Lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 70048e304ccSYangbo Lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 701f2bc6000Syinbo.zhu u32 val; 702f2bc6000Syinbo.zhu 703304f0a98SAlessio Igor Bogani sdhci_reset(host, mask); 704304f0a98SAlessio Igor Bogani 705304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); 706304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); 707f2bc6000Syinbo.zhu 7085dd19552SYinbo Zhu if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) 7095dd19552SYinbo Zhu mdelay(5); 7105dd19552SYinbo Zhu 711f2bc6000Syinbo.zhu if (mask & SDHCI_RESET_ALL) { 712f2bc6000Syinbo.zhu val = sdhci_readl(host, ESDHC_TBCTL); 713f2bc6000Syinbo.zhu val &= ~ESDHC_TB_EN; 714f2bc6000Syinbo.zhu sdhci_writel(host, val, ESDHC_TBCTL); 71548e304ccSYangbo Lu 71648e304ccSYangbo Lu if (esdhc->quirk_unreliable_pulse_detection) { 71748e304ccSYangbo Lu val = sdhci_readl(host, ESDHC_DLLCFG1); 71848e304ccSYangbo Lu val &= ~ESDHC_DLL_PD_PULSE_STRETCH_SEL; 71948e304ccSYangbo Lu sdhci_writel(host, val, ESDHC_DLLCFG1); 72048e304ccSYangbo Lu } 721f2bc6000Syinbo.zhu } 722304f0a98SAlessio Igor Bogani } 723304f0a98SAlessio Igor Bogani 724ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific 725ea35645aSyangbo lu * configuration and status registers for the device. There is a 726ea35645aSyangbo lu * SDHC IO VSEL control register on SCFG for some platforms. It's 727ea35645aSyangbo lu * used to support SDHC IO voltage switching. 728ea35645aSyangbo lu */ 729ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = { 730ea35645aSyangbo lu { .compatible = "fsl,t1040-scfg", }, 731ea35645aSyangbo lu { .compatible = "fsl,ls1012a-scfg", }, 732ea35645aSyangbo lu { .compatible = "fsl,ls1046a-scfg", }, 733ea35645aSyangbo lu {} 734ea35645aSyangbo lu }; 735ea35645aSyangbo lu 736ea35645aSyangbo lu /* SDHC IO VSEL control register definition */ 737ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR 0x408 738ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN 0x80000000 739ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL 0x60000000 740ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS 0x00000001 741ea35645aSyangbo lu 742ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc, 743ea35645aSyangbo lu struct mmc_ios *ios) 744ea35645aSyangbo lu { 745ea35645aSyangbo lu struct sdhci_host *host = mmc_priv(mmc); 746ea35645aSyangbo lu struct device_node *scfg_node; 747ea35645aSyangbo lu void __iomem *scfg_base = NULL; 748ea35645aSyangbo lu u32 sdhciovselcr; 749ea35645aSyangbo lu u32 val; 750ea35645aSyangbo lu 751ea35645aSyangbo lu /* 752ea35645aSyangbo lu * Signal Voltage Switching is only applicable for Host Controllers 753ea35645aSyangbo lu * v3.00 and above. 754ea35645aSyangbo lu */ 755ea35645aSyangbo lu if (host->version < SDHCI_SPEC_300) 756ea35645aSyangbo lu return 0; 757ea35645aSyangbo lu 758ea35645aSyangbo lu val = sdhci_readl(host, ESDHC_PROCTL); 759ea35645aSyangbo lu 760ea35645aSyangbo lu switch (ios->signal_voltage) { 761ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_330: 762ea35645aSyangbo lu val &= ~ESDHC_VOLT_SEL; 763ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 764ea35645aSyangbo lu return 0; 765ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_180: 766ea35645aSyangbo lu scfg_node = of_find_matching_node(NULL, scfg_device_ids); 767ea35645aSyangbo lu if (scfg_node) 768ea35645aSyangbo lu scfg_base = of_iomap(scfg_node, 0); 769ea35645aSyangbo lu if (scfg_base) { 770ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 771ea35645aSyangbo lu SDHCIOVSELCR_VSELVAL; 772ea35645aSyangbo lu iowrite32be(sdhciovselcr, 773ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 774ea35645aSyangbo lu 775ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 776ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 777ea35645aSyangbo lu mdelay(5); 778ea35645aSyangbo lu 779ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 780ea35645aSyangbo lu SDHCIOVSELCR_SDHC_VS; 781ea35645aSyangbo lu iowrite32be(sdhciovselcr, 782ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 783ea35645aSyangbo lu iounmap(scfg_base); 784ea35645aSyangbo lu } else { 785ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 786ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 787ea35645aSyangbo lu } 788ea35645aSyangbo lu return 0; 789ea35645aSyangbo lu default: 790ea35645aSyangbo lu return 0; 791ea35645aSyangbo lu } 792ea35645aSyangbo lu } 793ea35645aSyangbo lu 794b1f378abSYinbo Zhu static struct soc_device_attribute soc_fixup_tuning[] = { 795b1f378abSYinbo Zhu { .family = "QorIQ T1040", .revision = "1.0", }, 796b1f378abSYinbo Zhu { .family = "QorIQ T2080", .revision = "1.0", }, 797b1f378abSYinbo Zhu { .family = "QorIQ T1023", .revision = "1.0", }, 798b1f378abSYinbo Zhu { .family = "QorIQ LS1021A", .revision = "1.0", }, 799b1f378abSYinbo Zhu { .family = "QorIQ LS1080A", .revision = "1.0", }, 800b1f378abSYinbo Zhu { .family = "QorIQ LS2080A", .revision = "1.0", }, 801b1f378abSYinbo Zhu { .family = "QorIQ LS1012A", .revision = "1.0", }, 802b1f378abSYinbo Zhu { .family = "QorIQ LS1043A", .revision = "1.*", }, 803b1f378abSYinbo Zhu { .family = "QorIQ LS1046A", .revision = "1.0", }, 804b1f378abSYinbo Zhu { }, 805b1f378abSYinbo Zhu }; 806b1f378abSYinbo Zhu 80754e08d9aSYangbo Lu static void esdhc_tuning_block_enable(struct sdhci_host *host, bool enable) 808ba49cbd0Syangbo lu { 809ba49cbd0Syangbo lu u32 val; 810ba49cbd0Syangbo lu 811ba49cbd0Syangbo lu esdhc_clock_enable(host, false); 81254e08d9aSYangbo Lu 813ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 814ba49cbd0Syangbo lu val |= ESDHC_FLUSH_ASYNC_FIFO; 815ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 816ba49cbd0Syangbo lu 817ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_TBCTL); 81854e08d9aSYangbo Lu if (enable) 819ba49cbd0Syangbo lu val |= ESDHC_TB_EN; 82054e08d9aSYangbo Lu else 82154e08d9aSYangbo Lu val &= ~ESDHC_TB_EN; 822ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_TBCTL); 823ba49cbd0Syangbo lu 82454e08d9aSYangbo Lu esdhc_clock_enable(host, true); 82554e08d9aSYangbo Lu } 82654e08d9aSYangbo Lu 82754e08d9aSYangbo Lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) 82854e08d9aSYangbo Lu { 82954e08d9aSYangbo Lu struct sdhci_host *host = mmc_priv(mmc); 83054e08d9aSYangbo Lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 83154e08d9aSYangbo Lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 83254e08d9aSYangbo Lu bool hs400_tuning; 83304509d77SYangbo Lu unsigned int clk; 83454e08d9aSYangbo Lu u32 val; 83554e08d9aSYangbo Lu int ret; 83654e08d9aSYangbo Lu 83704509d77SYangbo Lu /* For tuning mode, the sd clock divisor value 83804509d77SYangbo Lu * must be larger than 3 according to reference manual. 83904509d77SYangbo Lu */ 84004509d77SYangbo Lu clk = esdhc->peripheral_clock / 3; 84104509d77SYangbo Lu if (host->clock > clk) 84204509d77SYangbo Lu esdhc_of_set_clock(host, clk); 84304509d77SYangbo Lu 8446079e63cSYangbo Lu if (esdhc->quirk_limited_clk_division && 8456079e63cSYangbo Lu host->flags & SDHCI_HS400_TUNING) 8466079e63cSYangbo Lu esdhc_of_set_clock(host, host->clock); 8476079e63cSYangbo Lu 84854e08d9aSYangbo Lu esdhc_tuning_block_enable(host, true); 84954e08d9aSYangbo Lu 85054e08d9aSYangbo Lu hs400_tuning = host->flags & SDHCI_HS400_TUNING; 85154e08d9aSYangbo Lu ret = sdhci_execute_tuning(mmc, opcode); 85254e08d9aSYangbo Lu 85354e08d9aSYangbo Lu if (hs400_tuning) { 85454e08d9aSYangbo Lu val = sdhci_readl(host, ESDHC_SDTIMNGCTL); 85554e08d9aSYangbo Lu val |= ESDHC_FLW_CTL_BG; 85654e08d9aSYangbo Lu sdhci_writel(host, val, ESDHC_SDTIMNGCTL); 85754e08d9aSYangbo Lu } 85854e08d9aSYangbo Lu 859b1f378abSYinbo Zhu if (host->tuning_err == -EAGAIN && esdhc->quirk_fixup_tuning) { 860b1f378abSYinbo Zhu 861b1f378abSYinbo Zhu /* program TBPTR[TB_WNDW_END_PTR] = 3*DIV_RATIO and 862b1f378abSYinbo Zhu * program TBPTR[TB_WNDW_START_PTR] = 5*DIV_RATIO 863b1f378abSYinbo Zhu */ 864b1f378abSYinbo Zhu val = sdhci_readl(host, ESDHC_TBPTR); 865b1f378abSYinbo Zhu val = (val & ~((0x7f << 8) | 0x7f)) | 866b1f378abSYinbo Zhu (3 * esdhc->div_ratio) | ((5 * esdhc->div_ratio) << 8); 867b1f378abSYinbo Zhu sdhci_writel(host, val, ESDHC_TBPTR); 868b1f378abSYinbo Zhu 869b1f378abSYinbo Zhu /* program the software tuning mode by setting 870b1f378abSYinbo Zhu * TBCTL[TB_MODE]=2'h3 871b1f378abSYinbo Zhu */ 872b1f378abSYinbo Zhu val = sdhci_readl(host, ESDHC_TBCTL); 873b1f378abSYinbo Zhu val |= 0x3; 874b1f378abSYinbo Zhu sdhci_writel(host, val, ESDHC_TBCTL); 875b1f378abSYinbo Zhu sdhci_execute_tuning(mmc, opcode); 876b1f378abSYinbo Zhu } 87754e08d9aSYangbo Lu return ret; 87854e08d9aSYangbo Lu } 87954e08d9aSYangbo Lu 88054e08d9aSYangbo Lu static void esdhc_set_uhs_signaling(struct sdhci_host *host, 88154e08d9aSYangbo Lu unsigned int timing) 88254e08d9aSYangbo Lu { 88354e08d9aSYangbo Lu if (timing == MMC_TIMING_MMC_HS400) 88454e08d9aSYangbo Lu esdhc_tuning_block_enable(host, true); 88554e08d9aSYangbo Lu else 88654e08d9aSYangbo Lu sdhci_set_uhs_signaling(host, timing); 887ba49cbd0Syangbo lu } 888ba49cbd0Syangbo lu 889b214fe59SYinbo Zhu static u32 esdhc_irq(struct sdhci_host *host, u32 intmask) 890b214fe59SYinbo Zhu { 891b214fe59SYinbo Zhu u32 command; 892b214fe59SYinbo Zhu 893b214fe59SYinbo Zhu if (of_find_compatible_node(NULL, NULL, 894b214fe59SYinbo Zhu "fsl,p2020-esdhc")) { 895b214fe59SYinbo Zhu command = SDHCI_GET_CMD(sdhci_readw(host, 896b214fe59SYinbo Zhu SDHCI_COMMAND)); 897b214fe59SYinbo Zhu if (command == MMC_WRITE_MULTIPLE_BLOCK && 898b214fe59SYinbo Zhu sdhci_readw(host, SDHCI_BLOCK_COUNT) && 899b214fe59SYinbo Zhu intmask & SDHCI_INT_DATA_END) { 900b214fe59SYinbo Zhu intmask &= ~SDHCI_INT_DATA_END; 901b214fe59SYinbo Zhu sdhci_writel(host, SDHCI_INT_DATA_END, 902b214fe59SYinbo Zhu SDHCI_INT_STATUS); 903b214fe59SYinbo Zhu } 904b214fe59SYinbo Zhu } 905b214fe59SYinbo Zhu return intmask; 906b214fe59SYinbo Zhu } 907b214fe59SYinbo Zhu 9089e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP 909723f7924SRussell King static u32 esdhc_proctl; 910723f7924SRussell King static int esdhc_of_suspend(struct device *dev) 911723f7924SRussell King { 912723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 913723f7924SRussell King 914f4932cfdSyangbo lu esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); 915723f7924SRussell King 916d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 917d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 918d38dcad4SAdrian Hunter 919723f7924SRussell King return sdhci_suspend_host(host); 920723f7924SRussell King } 921723f7924SRussell King 92206732b84SUlf Hansson static int esdhc_of_resume(struct device *dev) 923723f7924SRussell King { 924723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 925723f7924SRussell King int ret = sdhci_resume_host(host); 926723f7924SRussell King 927723f7924SRussell King if (ret == 0) { 928723f7924SRussell King /* Isn't this already done by sdhci_resume_host() ? --rmk */ 929723f7924SRussell King esdhc_of_enable_dma(host); 930f4932cfdSyangbo lu sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 931723f7924SRussell King } 932723f7924SRussell King return ret; 933723f7924SRussell King } 934723f7924SRussell King #endif 935723f7924SRussell King 9369e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops, 9379e48b336SUlf Hansson esdhc_of_suspend, 9389e48b336SUlf Hansson esdhc_of_resume); 9399e48b336SUlf Hansson 940f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = { 941f4932cfdSyangbo lu .read_l = esdhc_be_readl, 942f4932cfdSyangbo lu .read_w = esdhc_be_readw, 943f4932cfdSyangbo lu .read_b = esdhc_be_readb, 944f4932cfdSyangbo lu .write_l = esdhc_be_writel, 945f4932cfdSyangbo lu .write_w = esdhc_be_writew, 946f4932cfdSyangbo lu .write_b = esdhc_be_writeb, 947f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 948f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 949f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 950f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 951f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 952f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 953f4932cfdSyangbo lu .reset = esdhc_reset, 95454e08d9aSYangbo Lu .set_uhs_signaling = esdhc_set_uhs_signaling, 955b214fe59SYinbo Zhu .irq = esdhc_irq, 956f4932cfdSyangbo lu }; 957f4932cfdSyangbo lu 958f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = { 959f4932cfdSyangbo lu .read_l = esdhc_le_readl, 960f4932cfdSyangbo lu .read_w = esdhc_le_readw, 961f4932cfdSyangbo lu .read_b = esdhc_le_readb, 962f4932cfdSyangbo lu .write_l = esdhc_le_writel, 963f4932cfdSyangbo lu .write_w = esdhc_le_writew, 964f4932cfdSyangbo lu .write_b = esdhc_le_writeb, 965f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 966f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 967f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 968f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 969f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 970f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 971f4932cfdSyangbo lu .reset = esdhc_reset, 97254e08d9aSYangbo Lu .set_uhs_signaling = esdhc_set_uhs_signaling, 973b214fe59SYinbo Zhu .irq = esdhc_irq, 974f4932cfdSyangbo lu }; 975f4932cfdSyangbo lu 976f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { 977e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 978e9acc77dSyangbo lu #ifdef CONFIG_PPC 979e9acc77dSyangbo lu SDHCI_QUIRK_BROKEN_CARD_DETECTION | 980e9acc77dSyangbo lu #endif 981e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 982e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 983f4932cfdSyangbo lu .ops = &sdhci_esdhc_be_ops, 9847657c3a7SAlbert Herranz }; 98538576af1SShawn Guo 986f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { 987e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 988e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 989e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 990f4932cfdSyangbo lu .ops = &sdhci_esdhc_le_ops, 991f4932cfdSyangbo lu }; 992f4932cfdSyangbo lu 993151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = { 994151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "1.0", }, 995151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "2.0", }, 996151ede40Syangbo lu { }, 997151ede40Syangbo lu }; 998151ede40Syangbo lu 9996079e63cSYangbo Lu static struct soc_device_attribute soc_fixup_sdhc_clkdivs[] = { 10006079e63cSYangbo Lu { .family = "QorIQ LX2160A", .revision = "1.0", }, 10018e9a6919SYinbo Zhu { .family = "QorIQ LX2160A", .revision = "2.0", }, 10026079e63cSYangbo Lu { }, 10036079e63cSYangbo Lu }; 10046079e63cSYangbo Lu 100548e304ccSYangbo Lu static struct soc_device_attribute soc_unreliable_pulse_detection[] = { 100648e304ccSYangbo Lu { .family = "QorIQ LX2160A", .revision = "1.0", }, 100748e304ccSYangbo Lu { }, 100848e304ccSYangbo Lu }; 100948e304ccSYangbo Lu 1010f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) 1011f4932cfdSyangbo lu { 101267fdfbdfSyinbo.zhu const struct of_device_id *match; 1013f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host; 1014f4932cfdSyangbo lu struct sdhci_esdhc *esdhc; 101519c3a0efSyangbo lu struct device_node *np; 101619c3a0efSyangbo lu struct clk *clk; 101719c3a0efSyangbo lu u32 val; 1018f4932cfdSyangbo lu u16 host_ver; 1019f4932cfdSyangbo lu 1020f4932cfdSyangbo lu pltfm_host = sdhci_priv(host); 10218605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 1022f4932cfdSyangbo lu 1023f4932cfdSyangbo lu host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); 1024f4932cfdSyangbo lu esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> 1025f4932cfdSyangbo lu SDHCI_VENDOR_VER_SHIFT; 1026f4932cfdSyangbo lu esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; 1027151ede40Syangbo lu if (soc_device_match(soc_incorrect_hostver)) 1028151ede40Syangbo lu esdhc->quirk_incorrect_hostver = true; 1029151ede40Syangbo lu else 1030151ede40Syangbo lu esdhc->quirk_incorrect_hostver = false; 103119c3a0efSyangbo lu 10326079e63cSYangbo Lu if (soc_device_match(soc_fixup_sdhc_clkdivs)) 10336079e63cSYangbo Lu esdhc->quirk_limited_clk_division = true; 10346079e63cSYangbo Lu else 10356079e63cSYangbo Lu esdhc->quirk_limited_clk_division = false; 10366079e63cSYangbo Lu 103748e304ccSYangbo Lu if (soc_device_match(soc_unreliable_pulse_detection)) 103848e304ccSYangbo Lu esdhc->quirk_unreliable_pulse_detection = true; 103948e304ccSYangbo Lu else 104048e304ccSYangbo Lu esdhc->quirk_unreliable_pulse_detection = false; 104148e304ccSYangbo Lu 104267fdfbdfSyinbo.zhu match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node); 104367fdfbdfSyinbo.zhu if (match) 104467fdfbdfSyinbo.zhu esdhc->clk_fixup = match->data; 104519c3a0efSyangbo lu np = pdev->dev.of_node; 104619c3a0efSyangbo lu clk = of_clk_get(np, 0); 104719c3a0efSyangbo lu if (!IS_ERR(clk)) { 104819c3a0efSyangbo lu /* 104919c3a0efSyangbo lu * esdhc->peripheral_clock would be assigned with a value 105019c3a0efSyangbo lu * which is eSDHC base clock when use periperal clock. 1051*791463baSYangbo Lu * For some platforms, the clock value got by common clk 1052*791463baSYangbo Lu * API is peripheral clock while the eSDHC base clock is 1053*791463baSYangbo Lu * 1/2 peripheral clock. 105419c3a0efSyangbo lu */ 1055*791463baSYangbo Lu if (of_device_is_compatible(np, "fsl,ls1046a-esdhc") || 1056*791463baSYangbo Lu of_device_is_compatible(np, "fsl,ls1028a-esdhc")) 105719c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk) / 2; 105819c3a0efSyangbo lu else 105919c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk); 106019c3a0efSyangbo lu 106119c3a0efSyangbo lu clk_put(clk); 106219c3a0efSyangbo lu } 106319c3a0efSyangbo lu 106419c3a0efSyangbo lu if (esdhc->peripheral_clock) { 106519c3a0efSyangbo lu esdhc_clock_enable(host, false); 106619c3a0efSyangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 106719c3a0efSyangbo lu val |= ESDHC_PERIPHERAL_CLK_SEL; 106819c3a0efSyangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 106919c3a0efSyangbo lu esdhc_clock_enable(host, true); 107019c3a0efSyangbo lu } 1071f4932cfdSyangbo lu } 1072f4932cfdSyangbo lu 107354e08d9aSYangbo Lu static int esdhc_hs400_prepare_ddr(struct mmc_host *mmc) 107454e08d9aSYangbo Lu { 107554e08d9aSYangbo Lu esdhc_tuning_block_enable(mmc_priv(mmc), false); 107654e08d9aSYangbo Lu return 0; 107754e08d9aSYangbo Lu } 107854e08d9aSYangbo Lu 1079c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 108038576af1SShawn Guo { 108166b50a00SOded Gabbay struct sdhci_host *host; 1082dcaff04dSOded Gabbay struct device_node *np; 10831ef5e49eSyangbo lu struct sdhci_pltfm_host *pltfm_host; 10841ef5e49eSyangbo lu struct sdhci_esdhc *esdhc; 108566b50a00SOded Gabbay int ret; 108666b50a00SOded Gabbay 1087f4932cfdSyangbo lu np = pdev->dev.of_node; 1088f4932cfdSyangbo lu 1089150d4240SJulia Lawall if (of_property_read_bool(np, "little-endian")) 10908605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, 10918605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 1092f4932cfdSyangbo lu else 10938605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, 10948605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 1095f4932cfdSyangbo lu 109666b50a00SOded Gabbay if (IS_ERR(host)) 109766b50a00SOded Gabbay return PTR_ERR(host); 109866b50a00SOded Gabbay 1099ea35645aSyangbo lu host->mmc_host_ops.start_signal_voltage_switch = 1100ea35645aSyangbo lu esdhc_signal_voltage_switch; 1101ba49cbd0Syangbo lu host->mmc_host_ops.execute_tuning = esdhc_execute_tuning; 110254e08d9aSYangbo Lu host->mmc_host_ops.hs400_prepare_ddr = esdhc_hs400_prepare_ddr; 11036b236f37Syangbo lu host->tuning_delay = 1; 1104ea35645aSyangbo lu 1105f4932cfdSyangbo lu esdhc_init(pdev, host); 1106f4932cfdSyangbo lu 110766b50a00SOded Gabbay sdhci_get_of_property(pdev); 110866b50a00SOded Gabbay 11091ef5e49eSyangbo lu pltfm_host = sdhci_priv(host); 11108605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 1111b1f378abSYinbo Zhu if (soc_device_match(soc_fixup_tuning)) 1112b1f378abSYinbo Zhu esdhc->quirk_fixup_tuning = true; 1113b1f378abSYinbo Zhu else 1114b1f378abSYinbo Zhu esdhc->quirk_fixup_tuning = false; 1115b1f378abSYinbo Zhu 11161ef5e49eSyangbo lu if (esdhc->vendor_ver == VENDOR_V_22) 11171ef5e49eSyangbo lu host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 11181ef5e49eSyangbo lu 11191ef5e49eSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) 11201ef5e49eSyangbo lu host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 11211ef5e49eSyangbo lu 112205cb6b2aSYinbo Zhu if (of_find_compatible_node(NULL, NULL, "fsl,p2020-esdhc")) { 1123a46e4271SYinbo Zhu host->quirks2 |= SDHCI_QUIRK_RESET_AFTER_REQUEST; 112405cb6b2aSYinbo Zhu host->quirks2 |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 112505cb6b2aSYinbo Zhu } 1126a46e4271SYinbo Zhu 112774fd5e30SYangbo Lu if (of_device_is_compatible(np, "fsl,p5040-esdhc") || 112874fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p5020-esdhc") || 112974fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p4080-esdhc") || 113074fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p1020-esdhc") || 1131e9acc77dSyangbo lu of_device_is_compatible(np, "fsl,t1040-esdhc")) 113274fd5e30SYangbo Lu host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 113374fd5e30SYangbo Lu 1134a22950c8Syangbo lu if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) 1135a22950c8Syangbo lu host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 1136a22950c8Syangbo lu 11371f1929f3SYangbo Lu esdhc->quirk_ignore_data_inhibit = false; 1138dcaff04dSOded Gabbay if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { 1139dcaff04dSOded Gabbay /* 1140dcaff04dSOded Gabbay * Freescale messed up with P2020 as it has a non-standard 1141dcaff04dSOded Gabbay * host control register 1142dcaff04dSOded Gabbay */ 1143dcaff04dSOded Gabbay host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; 11441f1929f3SYangbo Lu esdhc->quirk_ignore_data_inhibit = true; 1145dcaff04dSOded Gabbay } 1146dcaff04dSOded Gabbay 114766b50a00SOded Gabbay /* call to generic mmc_of_parse to support additional capabilities */ 1148f0991408SUlf Hansson ret = mmc_of_parse(host->mmc); 1149f0991408SUlf Hansson if (ret) 1150f0991408SUlf Hansson goto err; 1151f0991408SUlf Hansson 1152490104acSHaijun Zhang mmc_of_parse_voltage(np, &host->ocr_mask); 115366b50a00SOded Gabbay 115466b50a00SOded Gabbay ret = sdhci_add_host(host); 115566b50a00SOded Gabbay if (ret) 1156f0991408SUlf Hansson goto err; 115766b50a00SOded Gabbay 1158f0991408SUlf Hansson return 0; 1159f0991408SUlf Hansson err: 1160f0991408SUlf Hansson sdhci_pltfm_free(pdev); 116166b50a00SOded Gabbay return ret; 116238576af1SShawn Guo } 116338576af1SShawn Guo 116438576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 116538576af1SShawn Guo .driver = { 116638576af1SShawn Guo .name = "sdhci-esdhc", 116738576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 11689e48b336SUlf Hansson .pm = &esdhc_of_dev_pm_ops, 116938576af1SShawn Guo }, 117038576af1SShawn Guo .probe = sdhci_esdhc_probe, 1171caebcae9SKevin Hao .remove = sdhci_pltfm_unregister, 117238576af1SShawn Guo }; 117338576af1SShawn Guo 1174d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 117538576af1SShawn Guo 117638576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 117738576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 117838576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 117938576af1SShawn Guo MODULE_LICENSE("GPL v2"); 1180