17657c3a7SAlbert Herranz /* 27657c3a7SAlbert Herranz * Freescale eSDHC controller driver. 37657c3a7SAlbert Herranz * 4f060bc9cSJerry Huang * Copyright (c) 2007, 2010, 2012 Freescale Semiconductor, Inc. 57657c3a7SAlbert Herranz * Copyright (c) 2009 MontaVista Software, Inc. 67657c3a7SAlbert Herranz * 77657c3a7SAlbert Herranz * Authors: Xiaobo Xie <X.Xie@freescale.com> 87657c3a7SAlbert Herranz * Anton Vorontsov <avorontsov@ru.mvista.com> 97657c3a7SAlbert Herranz * 107657c3a7SAlbert Herranz * This program is free software; you can redistribute it and/or modify 117657c3a7SAlbert Herranz * it under the terms of the GNU General Public License as published by 127657c3a7SAlbert Herranz * the Free Software Foundation; either version 2 of the License, or (at 137657c3a7SAlbert Herranz * your option) any later version. 147657c3a7SAlbert Herranz */ 157657c3a7SAlbert Herranz 1666b50a00SOded Gabbay #include <linux/err.h> 177657c3a7SAlbert Herranz #include <linux/io.h> 18f060bc9cSJerry Huang #include <linux/of.h> 19ea35645aSyangbo lu #include <linux/of_address.h> 207657c3a7SAlbert Herranz #include <linux/delay.h> 2188b47679SPaul Gortmaker #include <linux/module.h> 22151ede40Syangbo lu #include <linux/sys_soc.h> 2319c3a0efSyangbo lu #include <linux/clk.h> 2419c3a0efSyangbo lu #include <linux/ktime.h> 257657c3a7SAlbert Herranz #include <linux/mmc/host.h> 2638576af1SShawn Guo #include "sdhci-pltfm.h" 2780872e21SWolfram Sang #include "sdhci-esdhc.h" 287657c3a7SAlbert Herranz 29137ccd46SJerry Huang #define VENDOR_V_22 0x12 30a4071fbbSHaijun Zhang #define VENDOR_V_23 0x13 31f4932cfdSyangbo lu 32f4932cfdSyangbo lu struct sdhci_esdhc { 33f4932cfdSyangbo lu u8 vendor_ver; 34f4932cfdSyangbo lu u8 spec_ver; 35151ede40Syangbo lu bool quirk_incorrect_hostver; 3619c3a0efSyangbo lu unsigned int peripheral_clock; 37f4932cfdSyangbo lu }; 38f4932cfdSyangbo lu 39f4932cfdSyangbo lu /** 40f4932cfdSyangbo lu * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register 41f4932cfdSyangbo lu * to make it compatible with SD spec. 42f4932cfdSyangbo lu * 43f4932cfdSyangbo lu * @host: pointer to sdhci_host 44f4932cfdSyangbo lu * @spec_reg: SD spec register address 45f4932cfdSyangbo lu * @value: 32bit eSDHC register value on spec_reg address 46f4932cfdSyangbo lu * 47f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 48f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 49f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 50f4932cfdSyangbo lu * and SD spec. 51f4932cfdSyangbo lu * 52f4932cfdSyangbo lu * Return a fixed up register value 53f4932cfdSyangbo lu */ 54f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host, 55f4932cfdSyangbo lu int spec_reg, u32 value) 56137ccd46SJerry Huang { 57f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 588605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 59137ccd46SJerry Huang u32 ret; 60137ccd46SJerry Huang 61137ccd46SJerry Huang /* 62137ccd46SJerry Huang * The bit of ADMA flag in eSDHC is not compatible with standard 63137ccd46SJerry Huang * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is 64137ccd46SJerry Huang * supported by eSDHC. 65137ccd46SJerry Huang * And for many FSL eSDHC controller, the reset value of field 66f4932cfdSyangbo lu * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA, 67137ccd46SJerry Huang * only these vendor version is greater than 2.2/0x12 support ADMA. 68137ccd46SJerry Huang */ 69f4932cfdSyangbo lu if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) { 70f4932cfdSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) { 71f4932cfdSyangbo lu ret = value | SDHCI_CAN_DO_ADMA2; 72f4932cfdSyangbo lu return ret; 73137ccd46SJerry Huang } 74f4932cfdSyangbo lu } 75b0921d5cSMichael Walle /* 76b0921d5cSMichael Walle * The DAT[3:0] line signal levels and the CMD line signal level are 77b0921d5cSMichael Walle * not compatible with standard SDHC register. The line signal levels 78b0921d5cSMichael Walle * DAT[7:0] are at bits 31:24 and the command line signal level is at 79b0921d5cSMichael Walle * bit 23. All other bits are the same as in the standard SDHC 80b0921d5cSMichael Walle * register. 81b0921d5cSMichael Walle */ 82b0921d5cSMichael Walle if (spec_reg == SDHCI_PRESENT_STATE) { 83b0921d5cSMichael Walle ret = value & 0x000fffff; 84b0921d5cSMichael Walle ret |= (value >> 4) & SDHCI_DATA_LVL_MASK; 85b0921d5cSMichael Walle ret |= (value << 1) & SDHCI_CMD_LVL; 86b0921d5cSMichael Walle return ret; 87b0921d5cSMichael Walle } 88b0921d5cSMichael Walle 89f4932cfdSyangbo lu ret = value; 90137ccd46SJerry Huang return ret; 91137ccd46SJerry Huang } 92137ccd46SJerry Huang 93f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host, 94f4932cfdSyangbo lu int spec_reg, u32 value) 957657c3a7SAlbert Herranz { 96151ede40Syangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 97151ede40Syangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 987657c3a7SAlbert Herranz u16 ret; 99f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 1007657c3a7SAlbert Herranz 101f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_VERSION) 102f4932cfdSyangbo lu ret = value & 0xffff; 1037657c3a7SAlbert Herranz else 104f4932cfdSyangbo lu ret = (value >> shift) & 0xffff; 105151ede40Syangbo lu /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect 106151ede40Syangbo lu * vendor version and spec version information. 107151ede40Syangbo lu */ 108151ede40Syangbo lu if ((spec_reg == SDHCI_HOST_VERSION) && 109151ede40Syangbo lu (esdhc->quirk_incorrect_hostver)) 110151ede40Syangbo lu ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200; 111e51cbc9eSXu lei return ret; 112e51cbc9eSXu lei } 113e51cbc9eSXu lei 114f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host, 115f4932cfdSyangbo lu int spec_reg, u32 value) 116e51cbc9eSXu lei { 117f4932cfdSyangbo lu u8 ret; 118f4932cfdSyangbo lu u8 dma_bits; 119f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 120f4932cfdSyangbo lu 121f4932cfdSyangbo lu ret = (value >> shift) & 0xff; 122ba8c4dc9SRoy Zang 123ba8c4dc9SRoy Zang /* 124ba8c4dc9SRoy Zang * "DMA select" locates at offset 0x28 in SD specification, but on 125ba8c4dc9SRoy Zang * P5020 or P3041, it locates at 0x29. 126ba8c4dc9SRoy Zang */ 127f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 128ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 129f4932cfdSyangbo lu dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK; 130ba8c4dc9SRoy Zang /* fixup the result */ 131ba8c4dc9SRoy Zang ret &= ~SDHCI_CTRL_DMA_MASK; 132ba8c4dc9SRoy Zang ret |= dma_bits; 133ba8c4dc9SRoy Zang } 134f4932cfdSyangbo lu return ret; 135f4932cfdSyangbo lu } 136f4932cfdSyangbo lu 137f4932cfdSyangbo lu /** 138f4932cfdSyangbo lu * esdhc_write*_fixup - Fixup the SD spec register value so that it could be 139f4932cfdSyangbo lu * written into eSDHC register. 140f4932cfdSyangbo lu * 141f4932cfdSyangbo lu * @host: pointer to sdhci_host 142f4932cfdSyangbo lu * @spec_reg: SD spec register address 143f4932cfdSyangbo lu * @value: 8/16/32bit SD spec register value that would be written 144f4932cfdSyangbo lu * @old_value: 32bit eSDHC register value on spec_reg address 145f4932cfdSyangbo lu * 146f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 147f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 148f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 149f4932cfdSyangbo lu * and SD spec. 150f4932cfdSyangbo lu * 151f4932cfdSyangbo lu * Return a fixed up register value 152f4932cfdSyangbo lu */ 153f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host, 154f4932cfdSyangbo lu int spec_reg, u32 value, u32 old_value) 155f4932cfdSyangbo lu { 156f4932cfdSyangbo lu u32 ret; 157f4932cfdSyangbo lu 158f4932cfdSyangbo lu /* 159f4932cfdSyangbo lu * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] 160f4932cfdSyangbo lu * when SYSCTL[RSTD] is set for some special operations. 161f4932cfdSyangbo lu * No any impact on other operation. 162f4932cfdSyangbo lu */ 163f4932cfdSyangbo lu if (spec_reg == SDHCI_INT_ENABLE) 164f4932cfdSyangbo lu ret = value | SDHCI_INT_BLK_GAP; 165f4932cfdSyangbo lu else 166f4932cfdSyangbo lu ret = value; 167ba8c4dc9SRoy Zang 1687657c3a7SAlbert Herranz return ret; 1697657c3a7SAlbert Herranz } 1707657c3a7SAlbert Herranz 171f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host, 172f4932cfdSyangbo lu int spec_reg, u16 value, u32 old_value) 173a4071fbbSHaijun Zhang { 174f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 175f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 176f4932cfdSyangbo lu u32 ret; 177f4932cfdSyangbo lu 178f4932cfdSyangbo lu switch (spec_reg) { 179f4932cfdSyangbo lu case SDHCI_TRANSFER_MODE: 180a4071fbbSHaijun Zhang /* 181f4932cfdSyangbo lu * Postpone this write, we must do it together with a 182f4932cfdSyangbo lu * command write that is down below. Return old value. 183a4071fbbSHaijun Zhang */ 184f4932cfdSyangbo lu pltfm_host->xfer_mode_shadow = value; 185f4932cfdSyangbo lu return old_value; 186f4932cfdSyangbo lu case SDHCI_COMMAND: 187f4932cfdSyangbo lu ret = (value << 16) | pltfm_host->xfer_mode_shadow; 188f4932cfdSyangbo lu return ret; 189a4071fbbSHaijun Zhang } 190a4071fbbSHaijun Zhang 191f4932cfdSyangbo lu ret = old_value & (~(0xffff << shift)); 192f4932cfdSyangbo lu ret |= (value << shift); 193f4932cfdSyangbo lu 194f4932cfdSyangbo lu if (spec_reg == SDHCI_BLOCK_SIZE) { 1957657c3a7SAlbert Herranz /* 1967657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 1977657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 1987657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 1997657c3a7SAlbert Herranz */ 200f4932cfdSyangbo lu ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0)); 2017657c3a7SAlbert Herranz } 202f4932cfdSyangbo lu return ret; 2037657c3a7SAlbert Herranz } 2047657c3a7SAlbert Herranz 205f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host, 206f4932cfdSyangbo lu int spec_reg, u8 value, u32 old_value) 2077657c3a7SAlbert Herranz { 208f4932cfdSyangbo lu u32 ret; 209f4932cfdSyangbo lu u32 dma_bits; 210f4932cfdSyangbo lu u8 tmp; 211f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 212f4932cfdSyangbo lu 213ba8c4dc9SRoy Zang /* 2149e4703dfSyangbo lu * eSDHC doesn't have a standard power control register, so we do 2159e4703dfSyangbo lu * nothing here to avoid incorrect operation. 2169e4703dfSyangbo lu */ 2179e4703dfSyangbo lu if (spec_reg == SDHCI_POWER_CONTROL) 2189e4703dfSyangbo lu return old_value; 2199e4703dfSyangbo lu /* 220ba8c4dc9SRoy Zang * "DMA select" location is offset 0x28 in SD specification, but on 221ba8c4dc9SRoy Zang * P5020 or P3041, it's located at 0x29. 222ba8c4dc9SRoy Zang */ 223f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 224dcaff04dSOded Gabbay /* 225dcaff04dSOded Gabbay * If host control register is not standard, exit 226dcaff04dSOded Gabbay * this function 227dcaff04dSOded Gabbay */ 228dcaff04dSOded Gabbay if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL) 229f4932cfdSyangbo lu return old_value; 230dcaff04dSOded Gabbay 231ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 232f4932cfdSyangbo lu dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5; 233f4932cfdSyangbo lu ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits; 234f4932cfdSyangbo lu tmp = (value & (~SDHCI_CTRL_DMA_MASK)) | 235f4932cfdSyangbo lu (old_value & SDHCI_CTRL_DMA_MASK); 236f4932cfdSyangbo lu ret = (ret & (~0xff)) | tmp; 237f4932cfdSyangbo lu 238f4932cfdSyangbo lu /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */ 239f4932cfdSyangbo lu ret &= ~ESDHC_HOST_CONTROL_RES; 240f4932cfdSyangbo lu return ret; 241ba8c4dc9SRoy Zang } 242ba8c4dc9SRoy Zang 243f4932cfdSyangbo lu ret = (old_value & (~(0xff << shift))) | (value << shift); 244f4932cfdSyangbo lu return ret; 245f4932cfdSyangbo lu } 246f4932cfdSyangbo lu 247f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg) 248f4932cfdSyangbo lu { 249f4932cfdSyangbo lu u32 ret; 250f4932cfdSyangbo lu u32 value; 251f4932cfdSyangbo lu 252f4932cfdSyangbo lu value = ioread32be(host->ioaddr + reg); 253f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 254f4932cfdSyangbo lu 255f4932cfdSyangbo lu return ret; 256f4932cfdSyangbo lu } 257f4932cfdSyangbo lu 258f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg) 259f4932cfdSyangbo lu { 260f4932cfdSyangbo lu u32 ret; 261f4932cfdSyangbo lu u32 value; 262f4932cfdSyangbo lu 263f4932cfdSyangbo lu value = ioread32(host->ioaddr + reg); 264f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 265f4932cfdSyangbo lu 266f4932cfdSyangbo lu return ret; 267f4932cfdSyangbo lu } 268f4932cfdSyangbo lu 269f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg) 270f4932cfdSyangbo lu { 271f4932cfdSyangbo lu u16 ret; 272f4932cfdSyangbo lu u32 value; 273f4932cfdSyangbo lu int base = reg & ~0x3; 274f4932cfdSyangbo lu 275f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 276f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 277f4932cfdSyangbo lu return ret; 278f4932cfdSyangbo lu } 279f4932cfdSyangbo lu 280f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg) 281f4932cfdSyangbo lu { 282f4932cfdSyangbo lu u16 ret; 283f4932cfdSyangbo lu u32 value; 284f4932cfdSyangbo lu int base = reg & ~0x3; 285f4932cfdSyangbo lu 286f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 287f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 288f4932cfdSyangbo lu return ret; 289f4932cfdSyangbo lu } 290f4932cfdSyangbo lu 291f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg) 292f4932cfdSyangbo lu { 293f4932cfdSyangbo lu u8 ret; 294f4932cfdSyangbo lu u32 value; 295f4932cfdSyangbo lu int base = reg & ~0x3; 296f4932cfdSyangbo lu 297f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 298f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 299f4932cfdSyangbo lu return ret; 300f4932cfdSyangbo lu } 301f4932cfdSyangbo lu 302f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg) 303f4932cfdSyangbo lu { 304f4932cfdSyangbo lu u8 ret; 305f4932cfdSyangbo lu u32 value; 306f4932cfdSyangbo lu int base = reg & ~0x3; 307f4932cfdSyangbo lu 308f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 309f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 310f4932cfdSyangbo lu return ret; 311f4932cfdSyangbo lu } 312f4932cfdSyangbo lu 313f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg) 314f4932cfdSyangbo lu { 315f4932cfdSyangbo lu u32 value; 316f4932cfdSyangbo lu 317f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 318f4932cfdSyangbo lu iowrite32be(value, host->ioaddr + reg); 319f4932cfdSyangbo lu } 320f4932cfdSyangbo lu 321f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg) 322f4932cfdSyangbo lu { 323f4932cfdSyangbo lu u32 value; 324f4932cfdSyangbo lu 325f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 326f4932cfdSyangbo lu iowrite32(value, host->ioaddr + reg); 327f4932cfdSyangbo lu } 328f4932cfdSyangbo lu 329f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg) 330f4932cfdSyangbo lu { 331f4932cfdSyangbo lu int base = reg & ~0x3; 332f4932cfdSyangbo lu u32 value; 333f4932cfdSyangbo lu u32 ret; 334f4932cfdSyangbo lu 335f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 336f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 337f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 338f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 339f4932cfdSyangbo lu } 340f4932cfdSyangbo lu 341f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg) 342f4932cfdSyangbo lu { 343f4932cfdSyangbo lu int base = reg & ~0x3; 344f4932cfdSyangbo lu u32 value; 345f4932cfdSyangbo lu u32 ret; 346f4932cfdSyangbo lu 347f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 348f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 349f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 350f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 351f4932cfdSyangbo lu } 352f4932cfdSyangbo lu 353f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg) 354f4932cfdSyangbo lu { 355f4932cfdSyangbo lu int base = reg & ~0x3; 356f4932cfdSyangbo lu u32 value; 357f4932cfdSyangbo lu u32 ret; 358f4932cfdSyangbo lu 359f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 360f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 361f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 362f4932cfdSyangbo lu } 363f4932cfdSyangbo lu 364f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg) 365f4932cfdSyangbo lu { 366f4932cfdSyangbo lu int base = reg & ~0x3; 367f4932cfdSyangbo lu u32 value; 368f4932cfdSyangbo lu u32 ret; 369f4932cfdSyangbo lu 370f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 371f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 372f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 3737657c3a7SAlbert Herranz } 3747657c3a7SAlbert Herranz 375a4071fbbSHaijun Zhang /* 376a4071fbbSHaijun Zhang * For Abort or Suspend after Stop at Block Gap, ignore the ADMA 377a4071fbbSHaijun Zhang * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) 378a4071fbbSHaijun Zhang * and Block Gap Event(IRQSTAT[BGE]) are also set. 379a4071fbbSHaijun Zhang * For Continue, apply soft reset for data(SYSCTL[RSTD]); 380a4071fbbSHaijun Zhang * and re-issue the entire read transaction from beginning. 381a4071fbbSHaijun Zhang */ 382f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask) 383a4071fbbSHaijun Zhang { 384f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 3858605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 386a4071fbbSHaijun Zhang bool applicable; 387a4071fbbSHaijun Zhang dma_addr_t dmastart; 388a4071fbbSHaijun Zhang dma_addr_t dmanow; 389a4071fbbSHaijun Zhang 390a4071fbbSHaijun Zhang applicable = (intmask & SDHCI_INT_DATA_END) && 391a4071fbbSHaijun Zhang (intmask & SDHCI_INT_BLK_GAP) && 392f4932cfdSyangbo lu (esdhc->vendor_ver == VENDOR_V_23); 393a4071fbbSHaijun Zhang if (!applicable) 394a4071fbbSHaijun Zhang return; 395a4071fbbSHaijun Zhang 396a4071fbbSHaijun Zhang host->data->error = 0; 397a4071fbbSHaijun Zhang dmastart = sg_dma_address(host->data->sg); 398a4071fbbSHaijun Zhang dmanow = dmastart + host->data->bytes_xfered; 399a4071fbbSHaijun Zhang /* 400a4071fbbSHaijun Zhang * Force update to the next DMA block boundary. 401a4071fbbSHaijun Zhang */ 402a4071fbbSHaijun Zhang dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + 403a4071fbbSHaijun Zhang SDHCI_DEFAULT_BOUNDARY_SIZE; 404a4071fbbSHaijun Zhang host->data->bytes_xfered = dmanow - dmastart; 405a4071fbbSHaijun Zhang sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); 406a4071fbbSHaijun Zhang } 407a4071fbbSHaijun Zhang 40880872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host) 4097657c3a7SAlbert Herranz { 410f4932cfdSyangbo lu u32 value; 411f4932cfdSyangbo lu 412f4932cfdSyangbo lu value = sdhci_readl(host, ESDHC_DMA_SYSCTL); 413f4932cfdSyangbo lu value |= ESDHC_DMA_SNOOP; 414f4932cfdSyangbo lu sdhci_writel(host, value, ESDHC_DMA_SYSCTL); 4157657c3a7SAlbert Herranz return 0; 4167657c3a7SAlbert Herranz } 4177657c3a7SAlbert Herranz 41880872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 4197657c3a7SAlbert Herranz { 420e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 42119c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 4227657c3a7SAlbert Herranz 42319c3a0efSyangbo lu if (esdhc->peripheral_clock) 42419c3a0efSyangbo lu return esdhc->peripheral_clock; 42519c3a0efSyangbo lu else 426e307148fSShawn Guo return pltfm_host->clock; 4277657c3a7SAlbert Herranz } 4287657c3a7SAlbert Herranz 42980872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 4307657c3a7SAlbert Herranz { 431e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 43219c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 43319c3a0efSyangbo lu unsigned int clock; 4347657c3a7SAlbert Herranz 43519c3a0efSyangbo lu if (esdhc->peripheral_clock) 43619c3a0efSyangbo lu clock = esdhc->peripheral_clock; 43719c3a0efSyangbo lu else 43819c3a0efSyangbo lu clock = pltfm_host->clock; 43919c3a0efSyangbo lu return clock / 256 / 16; 4407657c3a7SAlbert Herranz } 4417657c3a7SAlbert Herranz 442f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 443f060bc9cSJerry Huang { 444f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 4458605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 446bd455029SJoakim Tjernlund int pre_div = 1; 447d31fc00aSDong Aisheng int div = 1; 448e87d2db2Syangbo lu u32 timeout; 449d31fc00aSDong Aisheng u32 temp; 450d31fc00aSDong Aisheng 4511650d0c7SRussell King host->mmc->actual_clock = 0; 4521650d0c7SRussell King 453d31fc00aSDong Aisheng if (clock == 0) 454373073efSRussell King return; 455d31fc00aSDong Aisheng 45677bd2f6fSYangbo Lu /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */ 457f4932cfdSyangbo lu if (esdhc->vendor_ver < VENDOR_V_23) 45877bd2f6fSYangbo Lu pre_div = 2; 45977bd2f6fSYangbo Lu 460f060bc9cSJerry Huang /* Workaround to reduce the clock frequency for p1010 esdhc */ 461f060bc9cSJerry Huang if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) { 462f060bc9cSJerry Huang if (clock > 20000000) 463f060bc9cSJerry Huang clock -= 5000000; 464f060bc9cSJerry Huang if (clock > 40000000) 465f060bc9cSJerry Huang clock -= 5000000; 466f060bc9cSJerry Huang } 467f060bc9cSJerry Huang 468d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 469e87d2db2Syangbo lu temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | 470e87d2db2Syangbo lu ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK); 471d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 472d31fc00aSDong Aisheng 473d31fc00aSDong Aisheng while (host->max_clk / pre_div / 16 > clock && pre_div < 256) 474d31fc00aSDong Aisheng pre_div *= 2; 475d31fc00aSDong Aisheng 476d31fc00aSDong Aisheng while (host->max_clk / pre_div / div > clock && div < 16) 477d31fc00aSDong Aisheng div++; 478d31fc00aSDong Aisheng 479d31fc00aSDong Aisheng dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 480e76b8559SDong Aisheng clock, host->max_clk / pre_div / div); 481bd455029SJoakim Tjernlund host->mmc->actual_clock = host->max_clk / pre_div / div; 482d31fc00aSDong Aisheng pre_div >>= 1; 483d31fc00aSDong Aisheng div--; 484d31fc00aSDong Aisheng 485d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 486d31fc00aSDong Aisheng temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 487d31fc00aSDong Aisheng | (div << ESDHC_DIVIDER_SHIFT) 488d31fc00aSDong Aisheng | (pre_div << ESDHC_PREDIV_SHIFT)); 489d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 490e87d2db2Syangbo lu 491e87d2db2Syangbo lu /* Wait max 20 ms */ 492e87d2db2Syangbo lu timeout = 20; 493e87d2db2Syangbo lu while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) { 494e87d2db2Syangbo lu if (timeout == 0) { 495e87d2db2Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 496e87d2db2Syangbo lu mmc_hostname(host->mmc)); 497e87d2db2Syangbo lu return; 498e87d2db2Syangbo lu } 499e87d2db2Syangbo lu timeout--; 500d31fc00aSDong Aisheng mdelay(1); 501f060bc9cSJerry Huang } 502f060bc9cSJerry Huang 503e87d2db2Syangbo lu temp |= ESDHC_CLOCK_SDCLKEN; 504e87d2db2Syangbo lu sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 505e87d2db2Syangbo lu } 506e87d2db2Syangbo lu 5072317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) 50866b50a00SOded Gabbay { 50966b50a00SOded Gabbay u32 ctrl; 51066b50a00SOded Gabbay 511f4932cfdSyangbo lu ctrl = sdhci_readl(host, ESDHC_PROCTL); 512f4932cfdSyangbo lu ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); 51366b50a00SOded Gabbay switch (width) { 51466b50a00SOded Gabbay case MMC_BUS_WIDTH_8: 515f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_8BITBUS; 51666b50a00SOded Gabbay break; 51766b50a00SOded Gabbay 51866b50a00SOded Gabbay case MMC_BUS_WIDTH_4: 519f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_4BITBUS; 52066b50a00SOded Gabbay break; 52166b50a00SOded Gabbay 52266b50a00SOded Gabbay default: 52366b50a00SOded Gabbay break; 52466b50a00SOded Gabbay } 52566b50a00SOded Gabbay 526f4932cfdSyangbo lu sdhci_writel(host, ctrl, ESDHC_PROCTL); 52766b50a00SOded Gabbay } 52866b50a00SOded Gabbay 52919c3a0efSyangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable) 53019c3a0efSyangbo lu { 53119c3a0efSyangbo lu u32 val; 53219c3a0efSyangbo lu ktime_t timeout; 53319c3a0efSyangbo lu 53419c3a0efSyangbo lu val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 53519c3a0efSyangbo lu 53619c3a0efSyangbo lu if (enable) 53719c3a0efSyangbo lu val |= ESDHC_CLOCK_SDCLKEN; 53819c3a0efSyangbo lu else 53919c3a0efSyangbo lu val &= ~ESDHC_CLOCK_SDCLKEN; 54019c3a0efSyangbo lu 54119c3a0efSyangbo lu sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL); 54219c3a0efSyangbo lu 54319c3a0efSyangbo lu /* Wait max 20 ms */ 54419c3a0efSyangbo lu timeout = ktime_add_ms(ktime_get(), 20); 54519c3a0efSyangbo lu val = ESDHC_CLOCK_STABLE; 54619c3a0efSyangbo lu while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) { 54719c3a0efSyangbo lu if (ktime_after(ktime_get(), timeout)) { 54819c3a0efSyangbo lu pr_err("%s: Internal clock never stabilised.\n", 54919c3a0efSyangbo lu mmc_hostname(host->mmc)); 55019c3a0efSyangbo lu break; 55119c3a0efSyangbo lu } 55219c3a0efSyangbo lu udelay(10); 55319c3a0efSyangbo lu } 55419c3a0efSyangbo lu } 55519c3a0efSyangbo lu 556304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask) 557304f0a98SAlessio Igor Bogani { 558304f0a98SAlessio Igor Bogani sdhci_reset(host, mask); 559304f0a98SAlessio Igor Bogani 560304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); 561304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); 562304f0a98SAlessio Igor Bogani } 563304f0a98SAlessio Igor Bogani 564ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific 565ea35645aSyangbo lu * configuration and status registers for the device. There is a 566ea35645aSyangbo lu * SDHC IO VSEL control register on SCFG for some platforms. It's 567ea35645aSyangbo lu * used to support SDHC IO voltage switching. 568ea35645aSyangbo lu */ 569ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = { 570ea35645aSyangbo lu { .compatible = "fsl,t1040-scfg", }, 571ea35645aSyangbo lu { .compatible = "fsl,ls1012a-scfg", }, 572ea35645aSyangbo lu { .compatible = "fsl,ls1046a-scfg", }, 573ea35645aSyangbo lu {} 574ea35645aSyangbo lu }; 575ea35645aSyangbo lu 576ea35645aSyangbo lu /* SDHC IO VSEL control register definition */ 577ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR 0x408 578ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN 0x80000000 579ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL 0x60000000 580ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS 0x00000001 581ea35645aSyangbo lu 582ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc, 583ea35645aSyangbo lu struct mmc_ios *ios) 584ea35645aSyangbo lu { 585ea35645aSyangbo lu struct sdhci_host *host = mmc_priv(mmc); 586ea35645aSyangbo lu struct device_node *scfg_node; 587ea35645aSyangbo lu void __iomem *scfg_base = NULL; 588ea35645aSyangbo lu u32 sdhciovselcr; 589ea35645aSyangbo lu u32 val; 590ea35645aSyangbo lu 591ea35645aSyangbo lu /* 592ea35645aSyangbo lu * Signal Voltage Switching is only applicable for Host Controllers 593ea35645aSyangbo lu * v3.00 and above. 594ea35645aSyangbo lu */ 595ea35645aSyangbo lu if (host->version < SDHCI_SPEC_300) 596ea35645aSyangbo lu return 0; 597ea35645aSyangbo lu 598ea35645aSyangbo lu val = sdhci_readl(host, ESDHC_PROCTL); 599ea35645aSyangbo lu 600ea35645aSyangbo lu switch (ios->signal_voltage) { 601ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_330: 602ea35645aSyangbo lu val &= ~ESDHC_VOLT_SEL; 603ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 604ea35645aSyangbo lu return 0; 605ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_180: 606ea35645aSyangbo lu scfg_node = of_find_matching_node(NULL, scfg_device_ids); 607ea35645aSyangbo lu if (scfg_node) 608ea35645aSyangbo lu scfg_base = of_iomap(scfg_node, 0); 609ea35645aSyangbo lu if (scfg_base) { 610ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 611ea35645aSyangbo lu SDHCIOVSELCR_VSELVAL; 612ea35645aSyangbo lu iowrite32be(sdhciovselcr, 613ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 614ea35645aSyangbo lu 615ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 616ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 617ea35645aSyangbo lu mdelay(5); 618ea35645aSyangbo lu 619ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 620ea35645aSyangbo lu SDHCIOVSELCR_SDHC_VS; 621ea35645aSyangbo lu iowrite32be(sdhciovselcr, 622ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 623ea35645aSyangbo lu iounmap(scfg_base); 624ea35645aSyangbo lu } else { 625ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 626ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 627ea35645aSyangbo lu } 628ea35645aSyangbo lu return 0; 629ea35645aSyangbo lu default: 630ea35645aSyangbo lu return 0; 631ea35645aSyangbo lu } 632ea35645aSyangbo lu } 633ea35645aSyangbo lu 634ba49cbd0Syangbo lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) 635ba49cbd0Syangbo lu { 636ba49cbd0Syangbo lu struct sdhci_host *host = mmc_priv(mmc); 637ba49cbd0Syangbo lu u32 val; 638ba49cbd0Syangbo lu 639ba49cbd0Syangbo lu /* Use tuning block for tuning procedure */ 640ba49cbd0Syangbo lu esdhc_clock_enable(host, false); 641ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 642ba49cbd0Syangbo lu val |= ESDHC_FLUSH_ASYNC_FIFO; 643ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 644ba49cbd0Syangbo lu 645ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_TBCTL); 646ba49cbd0Syangbo lu val |= ESDHC_TB_EN; 647ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_TBCTL); 648ba49cbd0Syangbo lu esdhc_clock_enable(host, true); 649ba49cbd0Syangbo lu 650ba49cbd0Syangbo lu return sdhci_execute_tuning(mmc, opcode); 651ba49cbd0Syangbo lu } 652ba49cbd0Syangbo lu 6539e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP 654723f7924SRussell King static u32 esdhc_proctl; 655723f7924SRussell King static int esdhc_of_suspend(struct device *dev) 656723f7924SRussell King { 657723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 658723f7924SRussell King 659f4932cfdSyangbo lu esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); 660723f7924SRussell King 661d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 662d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 663d38dcad4SAdrian Hunter 664723f7924SRussell King return sdhci_suspend_host(host); 665723f7924SRussell King } 666723f7924SRussell King 66706732b84SUlf Hansson static int esdhc_of_resume(struct device *dev) 668723f7924SRussell King { 669723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 670723f7924SRussell King int ret = sdhci_resume_host(host); 671723f7924SRussell King 672723f7924SRussell King if (ret == 0) { 673723f7924SRussell King /* Isn't this already done by sdhci_resume_host() ? --rmk */ 674723f7924SRussell King esdhc_of_enable_dma(host); 675f4932cfdSyangbo lu sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 676723f7924SRussell King } 677723f7924SRussell King return ret; 678723f7924SRussell King } 679723f7924SRussell King #endif 680723f7924SRussell King 6819e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops, 6829e48b336SUlf Hansson esdhc_of_suspend, 6839e48b336SUlf Hansson esdhc_of_resume); 6849e48b336SUlf Hansson 685f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = { 686f4932cfdSyangbo lu .read_l = esdhc_be_readl, 687f4932cfdSyangbo lu .read_w = esdhc_be_readw, 688f4932cfdSyangbo lu .read_b = esdhc_be_readb, 689f4932cfdSyangbo lu .write_l = esdhc_be_writel, 690f4932cfdSyangbo lu .write_w = esdhc_be_writew, 691f4932cfdSyangbo lu .write_b = esdhc_be_writeb, 692f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 693f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 694f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 695f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 696f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 697f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 698f4932cfdSyangbo lu .reset = esdhc_reset, 699f4932cfdSyangbo lu .set_uhs_signaling = sdhci_set_uhs_signaling, 700f4932cfdSyangbo lu }; 701f4932cfdSyangbo lu 702f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = { 703f4932cfdSyangbo lu .read_l = esdhc_le_readl, 704f4932cfdSyangbo lu .read_w = esdhc_le_readw, 705f4932cfdSyangbo lu .read_b = esdhc_le_readb, 706f4932cfdSyangbo lu .write_l = esdhc_le_writel, 707f4932cfdSyangbo lu .write_w = esdhc_le_writew, 708f4932cfdSyangbo lu .write_b = esdhc_le_writeb, 709f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 710f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 711f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 712f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 713f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 714f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 715f4932cfdSyangbo lu .reset = esdhc_reset, 716f4932cfdSyangbo lu .set_uhs_signaling = sdhci_set_uhs_signaling, 717f4932cfdSyangbo lu }; 718f4932cfdSyangbo lu 719f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { 720e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 721e9acc77dSyangbo lu #ifdef CONFIG_PPC 722e9acc77dSyangbo lu SDHCI_QUIRK_BROKEN_CARD_DETECTION | 723e9acc77dSyangbo lu #endif 724e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 725e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 726f4932cfdSyangbo lu .ops = &sdhci_esdhc_be_ops, 7277657c3a7SAlbert Herranz }; 72838576af1SShawn Guo 729f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { 730e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 731e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 732e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 733f4932cfdSyangbo lu .ops = &sdhci_esdhc_le_ops, 734f4932cfdSyangbo lu }; 735f4932cfdSyangbo lu 736151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = { 737151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "1.0", }, 738151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "2.0", }, 739151ede40Syangbo lu { }, 740151ede40Syangbo lu }; 741151ede40Syangbo lu 742f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) 743f4932cfdSyangbo lu { 744f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host; 745f4932cfdSyangbo lu struct sdhci_esdhc *esdhc; 74619c3a0efSyangbo lu struct device_node *np; 74719c3a0efSyangbo lu struct clk *clk; 74819c3a0efSyangbo lu u32 val; 749f4932cfdSyangbo lu u16 host_ver; 750f4932cfdSyangbo lu 751f4932cfdSyangbo lu pltfm_host = sdhci_priv(host); 7528605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 753f4932cfdSyangbo lu 754f4932cfdSyangbo lu host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); 755f4932cfdSyangbo lu esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> 756f4932cfdSyangbo lu SDHCI_VENDOR_VER_SHIFT; 757f4932cfdSyangbo lu esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; 758151ede40Syangbo lu if (soc_device_match(soc_incorrect_hostver)) 759151ede40Syangbo lu esdhc->quirk_incorrect_hostver = true; 760151ede40Syangbo lu else 761151ede40Syangbo lu esdhc->quirk_incorrect_hostver = false; 76219c3a0efSyangbo lu 76319c3a0efSyangbo lu np = pdev->dev.of_node; 76419c3a0efSyangbo lu clk = of_clk_get(np, 0); 76519c3a0efSyangbo lu if (!IS_ERR(clk)) { 76619c3a0efSyangbo lu /* 76719c3a0efSyangbo lu * esdhc->peripheral_clock would be assigned with a value 76819c3a0efSyangbo lu * which is eSDHC base clock when use periperal clock. 76919c3a0efSyangbo lu * For ls1046a, the clock value got by common clk API is 77019c3a0efSyangbo lu * peripheral clock while the eSDHC base clock is 1/2 77119c3a0efSyangbo lu * peripheral clock. 77219c3a0efSyangbo lu */ 77319c3a0efSyangbo lu if (of_device_is_compatible(np, "fsl,ls1046a-esdhc")) 77419c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk) / 2; 77519c3a0efSyangbo lu else 77619c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk); 77719c3a0efSyangbo lu 77819c3a0efSyangbo lu clk_put(clk); 77919c3a0efSyangbo lu } 78019c3a0efSyangbo lu 78119c3a0efSyangbo lu if (esdhc->peripheral_clock) { 78219c3a0efSyangbo lu esdhc_clock_enable(host, false); 78319c3a0efSyangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 78419c3a0efSyangbo lu val |= ESDHC_PERIPHERAL_CLK_SEL; 78519c3a0efSyangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 78619c3a0efSyangbo lu esdhc_clock_enable(host, true); 78719c3a0efSyangbo lu } 788f4932cfdSyangbo lu } 789f4932cfdSyangbo lu 790c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 79138576af1SShawn Guo { 79266b50a00SOded Gabbay struct sdhci_host *host; 793dcaff04dSOded Gabbay struct device_node *np; 7941ef5e49eSyangbo lu struct sdhci_pltfm_host *pltfm_host; 7951ef5e49eSyangbo lu struct sdhci_esdhc *esdhc; 79666b50a00SOded Gabbay int ret; 79766b50a00SOded Gabbay 798f4932cfdSyangbo lu np = pdev->dev.of_node; 799f4932cfdSyangbo lu 800150d4240SJulia Lawall if (of_property_read_bool(np, "little-endian")) 8018605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, 8028605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 803f4932cfdSyangbo lu else 8048605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, 8058605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 806f4932cfdSyangbo lu 80766b50a00SOded Gabbay if (IS_ERR(host)) 80866b50a00SOded Gabbay return PTR_ERR(host); 80966b50a00SOded Gabbay 810ea35645aSyangbo lu host->mmc_host_ops.start_signal_voltage_switch = 811ea35645aSyangbo lu esdhc_signal_voltage_switch; 812ba49cbd0Syangbo lu host->mmc_host_ops.execute_tuning = esdhc_execute_tuning; 813*6b236f37Syangbo lu host->tuning_delay = 1; 814ea35645aSyangbo lu 815f4932cfdSyangbo lu esdhc_init(pdev, host); 816f4932cfdSyangbo lu 81766b50a00SOded Gabbay sdhci_get_of_property(pdev); 81866b50a00SOded Gabbay 8191ef5e49eSyangbo lu pltfm_host = sdhci_priv(host); 8208605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 8211ef5e49eSyangbo lu if (esdhc->vendor_ver == VENDOR_V_22) 8221ef5e49eSyangbo lu host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 8231ef5e49eSyangbo lu 8241ef5e49eSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) 8251ef5e49eSyangbo lu host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 8261ef5e49eSyangbo lu 82774fd5e30SYangbo Lu if (of_device_is_compatible(np, "fsl,p5040-esdhc") || 82874fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p5020-esdhc") || 82974fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p4080-esdhc") || 83074fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p1020-esdhc") || 831e9acc77dSyangbo lu of_device_is_compatible(np, "fsl,t1040-esdhc")) 83274fd5e30SYangbo Lu host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 83374fd5e30SYangbo Lu 834a22950c8Syangbo lu if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) 835a22950c8Syangbo lu host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 836a22950c8Syangbo lu 837dcaff04dSOded Gabbay if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { 838dcaff04dSOded Gabbay /* 839dcaff04dSOded Gabbay * Freescale messed up with P2020 as it has a non-standard 840dcaff04dSOded Gabbay * host control register 841dcaff04dSOded Gabbay */ 842dcaff04dSOded Gabbay host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; 843dcaff04dSOded Gabbay } 844dcaff04dSOded Gabbay 84566b50a00SOded Gabbay /* call to generic mmc_of_parse to support additional capabilities */ 846f0991408SUlf Hansson ret = mmc_of_parse(host->mmc); 847f0991408SUlf Hansson if (ret) 848f0991408SUlf Hansson goto err; 849f0991408SUlf Hansson 850490104acSHaijun Zhang mmc_of_parse_voltage(np, &host->ocr_mask); 85166b50a00SOded Gabbay 85266b50a00SOded Gabbay ret = sdhci_add_host(host); 85366b50a00SOded Gabbay if (ret) 854f0991408SUlf Hansson goto err; 85566b50a00SOded Gabbay 856f0991408SUlf Hansson return 0; 857f0991408SUlf Hansson err: 858f0991408SUlf Hansson sdhci_pltfm_free(pdev); 85966b50a00SOded Gabbay return ret; 86038576af1SShawn Guo } 86138576af1SShawn Guo 86238576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = { 86338576af1SShawn Guo { .compatible = "fsl,mpc8379-esdhc" }, 86438576af1SShawn Guo { .compatible = "fsl,mpc8536-esdhc" }, 86538576af1SShawn Guo { .compatible = "fsl,esdhc" }, 86638576af1SShawn Guo { } 86738576af1SShawn Guo }; 86838576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); 86938576af1SShawn Guo 87038576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 87138576af1SShawn Guo .driver = { 87238576af1SShawn Guo .name = "sdhci-esdhc", 87338576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 8749e48b336SUlf Hansson .pm = &esdhc_of_dev_pm_ops, 87538576af1SShawn Guo }, 87638576af1SShawn Guo .probe = sdhci_esdhc_probe, 877caebcae9SKevin Hao .remove = sdhci_pltfm_unregister, 87838576af1SShawn Guo }; 87938576af1SShawn Guo 880d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 88138576af1SShawn Guo 88238576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 88338576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 88438576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 88538576af1SShawn Guo MODULE_LICENSE("GPL v2"); 886