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 32*67fdfbdfSyinbo.zhu #define MMC_TIMING_NUM (MMC_TIMING_MMC_HS400 + 1) 33*67fdfbdfSyinbo.zhu 34*67fdfbdfSyinbo.zhu struct esdhc_clk_fixup { 35*67fdfbdfSyinbo.zhu const unsigned int sd_dflt_max_clk; 36*67fdfbdfSyinbo.zhu const unsigned int max_clk[MMC_TIMING_NUM]; 37*67fdfbdfSyinbo.zhu }; 38*67fdfbdfSyinbo.zhu 39*67fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1021a_esdhc_clk = { 40*67fdfbdfSyinbo.zhu .sd_dflt_max_clk = 25000000, 41*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS] = 46500000, 42*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_SD_HS] = 46500000, 43*67fdfbdfSyinbo.zhu }; 44*67fdfbdfSyinbo.zhu 45*67fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1046a_esdhc_clk = { 46*67fdfbdfSyinbo.zhu .sd_dflt_max_clk = 25000000, 47*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_UHS_SDR104] = 167000000, 48*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS200] = 167000000, 49*67fdfbdfSyinbo.zhu }; 50*67fdfbdfSyinbo.zhu 51*67fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup ls1012a_esdhc_clk = { 52*67fdfbdfSyinbo.zhu .sd_dflt_max_clk = 25000000, 53*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_UHS_SDR104] = 125000000, 54*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS200] = 125000000, 55*67fdfbdfSyinbo.zhu }; 56*67fdfbdfSyinbo.zhu 57*67fdfbdfSyinbo.zhu static const struct esdhc_clk_fixup p1010_esdhc_clk = { 58*67fdfbdfSyinbo.zhu .sd_dflt_max_clk = 20000000, 59*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_LEGACY] = 20000000, 60*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_MMC_HS] = 42000000, 61*67fdfbdfSyinbo.zhu .max_clk[MMC_TIMING_SD_HS] = 40000000, 62*67fdfbdfSyinbo.zhu }; 63*67fdfbdfSyinbo.zhu 64*67fdfbdfSyinbo.zhu static const struct of_device_id sdhci_esdhc_of_match[] = { 65*67fdfbdfSyinbo.zhu { .compatible = "fsl,ls1021a-esdhc", .data = &ls1021a_esdhc_clk}, 66*67fdfbdfSyinbo.zhu { .compatible = "fsl,ls1046a-esdhc", .data = &ls1046a_esdhc_clk}, 67*67fdfbdfSyinbo.zhu { .compatible = "fsl,ls1012a-esdhc", .data = &ls1012a_esdhc_clk}, 68*67fdfbdfSyinbo.zhu { .compatible = "fsl,p1010-esdhc", .data = &p1010_esdhc_clk}, 69*67fdfbdfSyinbo.zhu { .compatible = "fsl,mpc8379-esdhc" }, 70*67fdfbdfSyinbo.zhu { .compatible = "fsl,mpc8536-esdhc" }, 71*67fdfbdfSyinbo.zhu { .compatible = "fsl,esdhc" }, 72*67fdfbdfSyinbo.zhu { } 73*67fdfbdfSyinbo.zhu }; 74*67fdfbdfSyinbo.zhu MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); 75*67fdfbdfSyinbo.zhu 76f4932cfdSyangbo lu struct sdhci_esdhc { 77f4932cfdSyangbo lu u8 vendor_ver; 78f4932cfdSyangbo lu u8 spec_ver; 79151ede40Syangbo lu bool quirk_incorrect_hostver; 8019c3a0efSyangbo lu unsigned int peripheral_clock; 81*67fdfbdfSyinbo.zhu const struct esdhc_clk_fixup *clk_fixup; 82f4932cfdSyangbo lu }; 83f4932cfdSyangbo lu 84f4932cfdSyangbo lu /** 85f4932cfdSyangbo lu * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register 86f4932cfdSyangbo lu * to make it compatible with SD spec. 87f4932cfdSyangbo lu * 88f4932cfdSyangbo lu * @host: pointer to sdhci_host 89f4932cfdSyangbo lu * @spec_reg: SD spec register address 90f4932cfdSyangbo lu * @value: 32bit eSDHC register value on spec_reg address 91f4932cfdSyangbo lu * 92f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 93f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 94f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 95f4932cfdSyangbo lu * and SD spec. 96f4932cfdSyangbo lu * 97f4932cfdSyangbo lu * Return a fixed up register value 98f4932cfdSyangbo lu */ 99f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host, 100f4932cfdSyangbo lu int spec_reg, u32 value) 101137ccd46SJerry Huang { 102f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1038605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 104137ccd46SJerry Huang u32 ret; 105137ccd46SJerry Huang 106137ccd46SJerry Huang /* 107137ccd46SJerry Huang * The bit of ADMA flag in eSDHC is not compatible with standard 108137ccd46SJerry Huang * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is 109137ccd46SJerry Huang * supported by eSDHC. 110137ccd46SJerry Huang * And for many FSL eSDHC controller, the reset value of field 111f4932cfdSyangbo lu * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA, 112137ccd46SJerry Huang * only these vendor version is greater than 2.2/0x12 support ADMA. 113137ccd46SJerry Huang */ 114f4932cfdSyangbo lu if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) { 115f4932cfdSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) { 116f4932cfdSyangbo lu ret = value | SDHCI_CAN_DO_ADMA2; 117f4932cfdSyangbo lu return ret; 118137ccd46SJerry Huang } 119f4932cfdSyangbo lu } 120b0921d5cSMichael Walle /* 121b0921d5cSMichael Walle * The DAT[3:0] line signal levels and the CMD line signal level are 122b0921d5cSMichael Walle * not compatible with standard SDHC register. The line signal levels 123b0921d5cSMichael Walle * DAT[7:0] are at bits 31:24 and the command line signal level is at 124b0921d5cSMichael Walle * bit 23. All other bits are the same as in the standard SDHC 125b0921d5cSMichael Walle * register. 126b0921d5cSMichael Walle */ 127b0921d5cSMichael Walle if (spec_reg == SDHCI_PRESENT_STATE) { 128b0921d5cSMichael Walle ret = value & 0x000fffff; 129b0921d5cSMichael Walle ret |= (value >> 4) & SDHCI_DATA_LVL_MASK; 130b0921d5cSMichael Walle ret |= (value << 1) & SDHCI_CMD_LVL; 131b0921d5cSMichael Walle return ret; 132b0921d5cSMichael Walle } 133b0921d5cSMichael Walle 1342f3110ccSyangbo lu /* 1352f3110ccSyangbo lu * DTS properties of mmc host are used to enable each speed mode 1362f3110ccSyangbo lu * according to soc and board capability. So clean up 1372f3110ccSyangbo lu * SDR50/SDR104/DDR50 support bits here. 1382f3110ccSyangbo lu */ 1392f3110ccSyangbo lu if (spec_reg == SDHCI_CAPABILITIES_1) { 1402f3110ccSyangbo lu ret = value & ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 | 1412f3110ccSyangbo lu SDHCI_SUPPORT_DDR50); 1422f3110ccSyangbo lu return ret; 1432f3110ccSyangbo lu } 1442f3110ccSyangbo lu 145f4932cfdSyangbo lu ret = value; 146137ccd46SJerry Huang return ret; 147137ccd46SJerry Huang } 148137ccd46SJerry Huang 149f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host, 150f4932cfdSyangbo lu int spec_reg, u32 value) 1517657c3a7SAlbert Herranz { 152151ede40Syangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 153151ede40Syangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 1547657c3a7SAlbert Herranz u16 ret; 155f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 1567657c3a7SAlbert Herranz 157f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_VERSION) 158f4932cfdSyangbo lu ret = value & 0xffff; 1597657c3a7SAlbert Herranz else 160f4932cfdSyangbo lu ret = (value >> shift) & 0xffff; 161151ede40Syangbo lu /* Workaround for T4240-R1.0-R2.0 eSDHC which has incorrect 162151ede40Syangbo lu * vendor version and spec version information. 163151ede40Syangbo lu */ 164151ede40Syangbo lu if ((spec_reg == SDHCI_HOST_VERSION) && 165151ede40Syangbo lu (esdhc->quirk_incorrect_hostver)) 166151ede40Syangbo lu ret = (VENDOR_V_23 << SDHCI_VENDOR_VER_SHIFT) | SDHCI_SPEC_200; 167e51cbc9eSXu lei return ret; 168e51cbc9eSXu lei } 169e51cbc9eSXu lei 170f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host, 171f4932cfdSyangbo lu int spec_reg, u32 value) 172e51cbc9eSXu lei { 173f4932cfdSyangbo lu u8 ret; 174f4932cfdSyangbo lu u8 dma_bits; 175f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 176f4932cfdSyangbo lu 177f4932cfdSyangbo lu ret = (value >> shift) & 0xff; 178ba8c4dc9SRoy Zang 179ba8c4dc9SRoy Zang /* 180ba8c4dc9SRoy Zang * "DMA select" locates at offset 0x28 in SD specification, but on 181ba8c4dc9SRoy Zang * P5020 or P3041, it locates at 0x29. 182ba8c4dc9SRoy Zang */ 183f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 184ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 185f4932cfdSyangbo lu dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK; 186ba8c4dc9SRoy Zang /* fixup the result */ 187ba8c4dc9SRoy Zang ret &= ~SDHCI_CTRL_DMA_MASK; 188ba8c4dc9SRoy Zang ret |= dma_bits; 189ba8c4dc9SRoy Zang } 190f4932cfdSyangbo lu return ret; 191f4932cfdSyangbo lu } 192f4932cfdSyangbo lu 193f4932cfdSyangbo lu /** 194f4932cfdSyangbo lu * esdhc_write*_fixup - Fixup the SD spec register value so that it could be 195f4932cfdSyangbo lu * written into eSDHC register. 196f4932cfdSyangbo lu * 197f4932cfdSyangbo lu * @host: pointer to sdhci_host 198f4932cfdSyangbo lu * @spec_reg: SD spec register address 199f4932cfdSyangbo lu * @value: 8/16/32bit SD spec register value that would be written 200f4932cfdSyangbo lu * @old_value: 32bit eSDHC register value on spec_reg address 201f4932cfdSyangbo lu * 202f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 203f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 204f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 205f4932cfdSyangbo lu * and SD spec. 206f4932cfdSyangbo lu * 207f4932cfdSyangbo lu * Return a fixed up register value 208f4932cfdSyangbo lu */ 209f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host, 210f4932cfdSyangbo lu int spec_reg, u32 value, u32 old_value) 211f4932cfdSyangbo lu { 212f4932cfdSyangbo lu u32 ret; 213f4932cfdSyangbo lu 214f4932cfdSyangbo lu /* 215f4932cfdSyangbo lu * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] 216f4932cfdSyangbo lu * when SYSCTL[RSTD] is set for some special operations. 217f4932cfdSyangbo lu * No any impact on other operation. 218f4932cfdSyangbo lu */ 219f4932cfdSyangbo lu if (spec_reg == SDHCI_INT_ENABLE) 220f4932cfdSyangbo lu ret = value | SDHCI_INT_BLK_GAP; 221f4932cfdSyangbo lu else 222f4932cfdSyangbo lu ret = value; 223ba8c4dc9SRoy Zang 2247657c3a7SAlbert Herranz return ret; 2257657c3a7SAlbert Herranz } 2267657c3a7SAlbert Herranz 227f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host, 228f4932cfdSyangbo lu int spec_reg, u16 value, u32 old_value) 229a4071fbbSHaijun Zhang { 230f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 231f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 232f4932cfdSyangbo lu u32 ret; 233f4932cfdSyangbo lu 234f4932cfdSyangbo lu switch (spec_reg) { 235f4932cfdSyangbo lu case SDHCI_TRANSFER_MODE: 236a4071fbbSHaijun Zhang /* 237f4932cfdSyangbo lu * Postpone this write, we must do it together with a 238f4932cfdSyangbo lu * command write that is down below. Return old value. 239a4071fbbSHaijun Zhang */ 240f4932cfdSyangbo lu pltfm_host->xfer_mode_shadow = value; 241f4932cfdSyangbo lu return old_value; 242f4932cfdSyangbo lu case SDHCI_COMMAND: 243f4932cfdSyangbo lu ret = (value << 16) | pltfm_host->xfer_mode_shadow; 244f4932cfdSyangbo lu return ret; 245a4071fbbSHaijun Zhang } 246a4071fbbSHaijun Zhang 247f4932cfdSyangbo lu ret = old_value & (~(0xffff << shift)); 248f4932cfdSyangbo lu ret |= (value << shift); 249f4932cfdSyangbo lu 250f4932cfdSyangbo lu if (spec_reg == SDHCI_BLOCK_SIZE) { 2517657c3a7SAlbert Herranz /* 2527657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 2537657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 2547657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 2557657c3a7SAlbert Herranz */ 256f4932cfdSyangbo lu ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0)); 2577657c3a7SAlbert Herranz } 258f4932cfdSyangbo lu return ret; 2597657c3a7SAlbert Herranz } 2607657c3a7SAlbert Herranz 261f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host, 262f4932cfdSyangbo lu int spec_reg, u8 value, u32 old_value) 2637657c3a7SAlbert Herranz { 264f4932cfdSyangbo lu u32 ret; 265f4932cfdSyangbo lu u32 dma_bits; 266f4932cfdSyangbo lu u8 tmp; 267f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 268f4932cfdSyangbo lu 269ba8c4dc9SRoy Zang /* 2709e4703dfSyangbo lu * eSDHC doesn't have a standard power control register, so we do 2719e4703dfSyangbo lu * nothing here to avoid incorrect operation. 2729e4703dfSyangbo lu */ 2739e4703dfSyangbo lu if (spec_reg == SDHCI_POWER_CONTROL) 2749e4703dfSyangbo lu return old_value; 2759e4703dfSyangbo lu /* 276ba8c4dc9SRoy Zang * "DMA select" location is offset 0x28 in SD specification, but on 277ba8c4dc9SRoy Zang * P5020 or P3041, it's located at 0x29. 278ba8c4dc9SRoy Zang */ 279f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 280dcaff04dSOded Gabbay /* 281dcaff04dSOded Gabbay * If host control register is not standard, exit 282dcaff04dSOded Gabbay * this function 283dcaff04dSOded Gabbay */ 284dcaff04dSOded Gabbay if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL) 285f4932cfdSyangbo lu return old_value; 286dcaff04dSOded Gabbay 287ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 288f4932cfdSyangbo lu dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5; 289f4932cfdSyangbo lu ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits; 290f4932cfdSyangbo lu tmp = (value & (~SDHCI_CTRL_DMA_MASK)) | 291f4932cfdSyangbo lu (old_value & SDHCI_CTRL_DMA_MASK); 292f4932cfdSyangbo lu ret = (ret & (~0xff)) | tmp; 293f4932cfdSyangbo lu 294f4932cfdSyangbo lu /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */ 295f4932cfdSyangbo lu ret &= ~ESDHC_HOST_CONTROL_RES; 296f4932cfdSyangbo lu return ret; 297ba8c4dc9SRoy Zang } 298ba8c4dc9SRoy Zang 299f4932cfdSyangbo lu ret = (old_value & (~(0xff << shift))) | (value << shift); 300f4932cfdSyangbo lu return ret; 301f4932cfdSyangbo lu } 302f4932cfdSyangbo lu 303f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg) 304f4932cfdSyangbo lu { 305f4932cfdSyangbo lu u32 ret; 306f4932cfdSyangbo lu u32 value; 307f4932cfdSyangbo lu 3082f3110ccSyangbo lu if (reg == SDHCI_CAPABILITIES_1) 3092f3110ccSyangbo lu value = ioread32be(host->ioaddr + ESDHC_CAPABILITIES_1); 3102f3110ccSyangbo lu else 311f4932cfdSyangbo lu value = ioread32be(host->ioaddr + reg); 3122f3110ccSyangbo lu 313f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 314f4932cfdSyangbo lu 315f4932cfdSyangbo lu return ret; 316f4932cfdSyangbo lu } 317f4932cfdSyangbo lu 318f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg) 319f4932cfdSyangbo lu { 320f4932cfdSyangbo lu u32 ret; 321f4932cfdSyangbo lu u32 value; 322f4932cfdSyangbo lu 3232f3110ccSyangbo lu if (reg == SDHCI_CAPABILITIES_1) 3242f3110ccSyangbo lu value = ioread32(host->ioaddr + ESDHC_CAPABILITIES_1); 3252f3110ccSyangbo lu else 326f4932cfdSyangbo lu value = ioread32(host->ioaddr + reg); 3272f3110ccSyangbo lu 328f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 329f4932cfdSyangbo lu 330f4932cfdSyangbo lu return ret; 331f4932cfdSyangbo lu } 332f4932cfdSyangbo lu 333f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg) 334f4932cfdSyangbo lu { 335f4932cfdSyangbo lu u16 ret; 336f4932cfdSyangbo lu u32 value; 337f4932cfdSyangbo lu int base = reg & ~0x3; 338f4932cfdSyangbo lu 339f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 340f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 341f4932cfdSyangbo lu return ret; 342f4932cfdSyangbo lu } 343f4932cfdSyangbo lu 344f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg) 345f4932cfdSyangbo lu { 346f4932cfdSyangbo lu u16 ret; 347f4932cfdSyangbo lu u32 value; 348f4932cfdSyangbo lu int base = reg & ~0x3; 349f4932cfdSyangbo lu 350f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 351f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 352f4932cfdSyangbo lu return ret; 353f4932cfdSyangbo lu } 354f4932cfdSyangbo lu 355f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg) 356f4932cfdSyangbo lu { 357f4932cfdSyangbo lu u8 ret; 358f4932cfdSyangbo lu u32 value; 359f4932cfdSyangbo lu int base = reg & ~0x3; 360f4932cfdSyangbo lu 361f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 362f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 363f4932cfdSyangbo lu return ret; 364f4932cfdSyangbo lu } 365f4932cfdSyangbo lu 366f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg) 367f4932cfdSyangbo lu { 368f4932cfdSyangbo lu u8 ret; 369f4932cfdSyangbo lu u32 value; 370f4932cfdSyangbo lu int base = reg & ~0x3; 371f4932cfdSyangbo lu 372f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 373f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 374f4932cfdSyangbo lu return ret; 375f4932cfdSyangbo lu } 376f4932cfdSyangbo lu 377f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg) 378f4932cfdSyangbo lu { 379f4932cfdSyangbo lu u32 value; 380f4932cfdSyangbo lu 381f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 382f4932cfdSyangbo lu iowrite32be(value, host->ioaddr + reg); 383f4932cfdSyangbo lu } 384f4932cfdSyangbo lu 385f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg) 386f4932cfdSyangbo lu { 387f4932cfdSyangbo lu u32 value; 388f4932cfdSyangbo lu 389f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 390f4932cfdSyangbo lu iowrite32(value, host->ioaddr + reg); 391f4932cfdSyangbo lu } 392f4932cfdSyangbo lu 393f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg) 394f4932cfdSyangbo lu { 395f4932cfdSyangbo lu int base = reg & ~0x3; 396f4932cfdSyangbo lu u32 value; 397f4932cfdSyangbo lu u32 ret; 398f4932cfdSyangbo lu 399f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 400f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 401f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 402f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 403f4932cfdSyangbo lu } 404f4932cfdSyangbo lu 405f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg) 406f4932cfdSyangbo lu { 407f4932cfdSyangbo lu int base = reg & ~0x3; 408f4932cfdSyangbo lu u32 value; 409f4932cfdSyangbo lu u32 ret; 410f4932cfdSyangbo lu 411f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 412f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 413f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 414f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 415f4932cfdSyangbo lu } 416f4932cfdSyangbo lu 417f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg) 418f4932cfdSyangbo lu { 419f4932cfdSyangbo lu int base = reg & ~0x3; 420f4932cfdSyangbo lu u32 value; 421f4932cfdSyangbo lu u32 ret; 422f4932cfdSyangbo lu 423f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 424f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 425f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 426f4932cfdSyangbo lu } 427f4932cfdSyangbo lu 428f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg) 429f4932cfdSyangbo lu { 430f4932cfdSyangbo lu int base = reg & ~0x3; 431f4932cfdSyangbo lu u32 value; 432f4932cfdSyangbo lu u32 ret; 433f4932cfdSyangbo lu 434f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 435f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 436f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 4377657c3a7SAlbert Herranz } 4387657c3a7SAlbert Herranz 439a4071fbbSHaijun Zhang /* 440a4071fbbSHaijun Zhang * For Abort or Suspend after Stop at Block Gap, ignore the ADMA 441a4071fbbSHaijun Zhang * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) 442a4071fbbSHaijun Zhang * and Block Gap Event(IRQSTAT[BGE]) are also set. 443a4071fbbSHaijun Zhang * For Continue, apply soft reset for data(SYSCTL[RSTD]); 444a4071fbbSHaijun Zhang * and re-issue the entire read transaction from beginning. 445a4071fbbSHaijun Zhang */ 446f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask) 447a4071fbbSHaijun Zhang { 448f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 4498605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 450a4071fbbSHaijun Zhang bool applicable; 451a4071fbbSHaijun Zhang dma_addr_t dmastart; 452a4071fbbSHaijun Zhang dma_addr_t dmanow; 453a4071fbbSHaijun Zhang 454a4071fbbSHaijun Zhang applicable = (intmask & SDHCI_INT_DATA_END) && 455a4071fbbSHaijun Zhang (intmask & SDHCI_INT_BLK_GAP) && 456f4932cfdSyangbo lu (esdhc->vendor_ver == VENDOR_V_23); 457a4071fbbSHaijun Zhang if (!applicable) 458a4071fbbSHaijun Zhang return; 459a4071fbbSHaijun Zhang 460a4071fbbSHaijun Zhang host->data->error = 0; 461a4071fbbSHaijun Zhang dmastart = sg_dma_address(host->data->sg); 462a4071fbbSHaijun Zhang dmanow = dmastart + host->data->bytes_xfered; 463a4071fbbSHaijun Zhang /* 464a4071fbbSHaijun Zhang * Force update to the next DMA block boundary. 465a4071fbbSHaijun Zhang */ 466a4071fbbSHaijun Zhang dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + 467a4071fbbSHaijun Zhang SDHCI_DEFAULT_BOUNDARY_SIZE; 468a4071fbbSHaijun Zhang host->data->bytes_xfered = dmanow - dmastart; 469a4071fbbSHaijun Zhang sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); 470a4071fbbSHaijun Zhang } 471a4071fbbSHaijun Zhang 47280872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host) 4737657c3a7SAlbert Herranz { 474f4932cfdSyangbo lu u32 value; 475f4932cfdSyangbo lu 476f4932cfdSyangbo lu value = sdhci_readl(host, ESDHC_DMA_SYSCTL); 477f4932cfdSyangbo lu value |= ESDHC_DMA_SNOOP; 478f4932cfdSyangbo lu sdhci_writel(host, value, ESDHC_DMA_SYSCTL); 4797657c3a7SAlbert Herranz return 0; 4807657c3a7SAlbert Herranz } 4817657c3a7SAlbert Herranz 48280872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 4837657c3a7SAlbert Herranz { 484e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 48519c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 4867657c3a7SAlbert Herranz 48719c3a0efSyangbo lu if (esdhc->peripheral_clock) 48819c3a0efSyangbo lu return esdhc->peripheral_clock; 48919c3a0efSyangbo lu else 490e307148fSShawn Guo return pltfm_host->clock; 4917657c3a7SAlbert Herranz } 4927657c3a7SAlbert Herranz 49380872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 4947657c3a7SAlbert Herranz { 495e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 49619c3a0efSyangbo lu struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 49719c3a0efSyangbo lu unsigned int clock; 4987657c3a7SAlbert Herranz 49919c3a0efSyangbo lu if (esdhc->peripheral_clock) 50019c3a0efSyangbo lu clock = esdhc->peripheral_clock; 50119c3a0efSyangbo lu else 50219c3a0efSyangbo lu clock = pltfm_host->clock; 50319c3a0efSyangbo lu return clock / 256 / 16; 5047657c3a7SAlbert Herranz } 5057657c3a7SAlbert Herranz 506dd3f6983Syangbo lu static void esdhc_clock_enable(struct sdhci_host *host, bool enable) 507dd3f6983Syangbo lu { 508dd3f6983Syangbo lu u32 val; 509dd3f6983Syangbo lu ktime_t timeout; 510dd3f6983Syangbo lu 511dd3f6983Syangbo lu val = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 512dd3f6983Syangbo lu 513dd3f6983Syangbo lu if (enable) 514dd3f6983Syangbo lu val |= ESDHC_CLOCK_SDCLKEN; 515dd3f6983Syangbo lu else 516dd3f6983Syangbo lu val &= ~ESDHC_CLOCK_SDCLKEN; 517dd3f6983Syangbo lu 518dd3f6983Syangbo lu sdhci_writel(host, val, ESDHC_SYSTEM_CONTROL); 519dd3f6983Syangbo lu 520dd3f6983Syangbo lu /* Wait max 20 ms */ 521dd3f6983Syangbo lu timeout = ktime_add_ms(ktime_get(), 20); 522dd3f6983Syangbo lu val = ESDHC_CLOCK_STABLE; 523dd3f6983Syangbo lu while (!(sdhci_readl(host, ESDHC_PRSSTAT) & val)) { 524dd3f6983Syangbo lu if (ktime_after(ktime_get(), timeout)) { 525dd3f6983Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 526dd3f6983Syangbo lu mmc_hostname(host->mmc)); 527dd3f6983Syangbo lu break; 528dd3f6983Syangbo lu } 529dd3f6983Syangbo lu udelay(10); 530dd3f6983Syangbo lu } 531dd3f6983Syangbo lu } 532dd3f6983Syangbo lu 533f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 534f060bc9cSJerry Huang { 535f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 5368605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 537bd455029SJoakim Tjernlund int pre_div = 1; 538d31fc00aSDong Aisheng int div = 1; 539e145ac45Syangbo lu ktime_t timeout; 540*67fdfbdfSyinbo.zhu long fixup = 0; 541d31fc00aSDong Aisheng u32 temp; 542d31fc00aSDong Aisheng 5431650d0c7SRussell King host->mmc->actual_clock = 0; 5441650d0c7SRussell King 545dd3f6983Syangbo lu if (clock == 0) { 546dd3f6983Syangbo lu esdhc_clock_enable(host, false); 547373073efSRussell King return; 548dd3f6983Syangbo lu } 549d31fc00aSDong Aisheng 55077bd2f6fSYangbo Lu /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */ 551f4932cfdSyangbo lu if (esdhc->vendor_ver < VENDOR_V_23) 55277bd2f6fSYangbo Lu pre_div = 2; 55377bd2f6fSYangbo Lu 554*67fdfbdfSyinbo.zhu if (host->mmc->card && mmc_card_sd(host->mmc->card) && 555*67fdfbdfSyinbo.zhu esdhc->clk_fixup && host->mmc->ios.timing == MMC_TIMING_LEGACY) 556*67fdfbdfSyinbo.zhu fixup = esdhc->clk_fixup->sd_dflt_max_clk; 557*67fdfbdfSyinbo.zhu else if (esdhc->clk_fixup) 558*67fdfbdfSyinbo.zhu fixup = esdhc->clk_fixup->max_clk[host->mmc->ios.timing]; 559a627f025Syangbo lu 560*67fdfbdfSyinbo.zhu if (fixup && clock > fixup) 561*67fdfbdfSyinbo.zhu clock = fixup; 562f060bc9cSJerry Huang 563d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 564e87d2db2Syangbo lu temp &= ~(ESDHC_CLOCK_SDCLKEN | ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | 565e87d2db2Syangbo lu ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK); 566d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 567d31fc00aSDong Aisheng 568d31fc00aSDong Aisheng while (host->max_clk / pre_div / 16 > clock && pre_div < 256) 569d31fc00aSDong Aisheng pre_div *= 2; 570d31fc00aSDong Aisheng 571d31fc00aSDong Aisheng while (host->max_clk / pre_div / div > clock && div < 16) 572d31fc00aSDong Aisheng div++; 573d31fc00aSDong Aisheng 574d31fc00aSDong Aisheng dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 575e76b8559SDong Aisheng clock, host->max_clk / pre_div / div); 576bd455029SJoakim Tjernlund host->mmc->actual_clock = host->max_clk / pre_div / div; 577d31fc00aSDong Aisheng pre_div >>= 1; 578d31fc00aSDong Aisheng div--; 579d31fc00aSDong Aisheng 580d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 581d31fc00aSDong Aisheng temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 582d31fc00aSDong Aisheng | (div << ESDHC_DIVIDER_SHIFT) 583d31fc00aSDong Aisheng | (pre_div << ESDHC_PREDIV_SHIFT)); 584d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 585e87d2db2Syangbo lu 586e87d2db2Syangbo lu /* Wait max 20 ms */ 587e145ac45Syangbo lu timeout = ktime_add_ms(ktime_get(), 20); 588e87d2db2Syangbo lu while (!(sdhci_readl(host, ESDHC_PRSSTAT) & ESDHC_CLOCK_STABLE)) { 589e145ac45Syangbo lu if (ktime_after(ktime_get(), timeout)) { 590e87d2db2Syangbo lu pr_err("%s: Internal clock never stabilised.\n", 591e87d2db2Syangbo lu mmc_hostname(host->mmc)); 592e87d2db2Syangbo lu return; 593e87d2db2Syangbo lu } 594e145ac45Syangbo lu udelay(10); 595f060bc9cSJerry Huang } 596f060bc9cSJerry Huang 597e87d2db2Syangbo lu temp |= ESDHC_CLOCK_SDCLKEN; 598e87d2db2Syangbo lu sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 599e87d2db2Syangbo lu } 600e87d2db2Syangbo lu 6012317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) 60266b50a00SOded Gabbay { 60366b50a00SOded Gabbay u32 ctrl; 60466b50a00SOded Gabbay 605f4932cfdSyangbo lu ctrl = sdhci_readl(host, ESDHC_PROCTL); 606f4932cfdSyangbo lu ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); 60766b50a00SOded Gabbay switch (width) { 60866b50a00SOded Gabbay case MMC_BUS_WIDTH_8: 609f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_8BITBUS; 61066b50a00SOded Gabbay break; 61166b50a00SOded Gabbay 61266b50a00SOded Gabbay case MMC_BUS_WIDTH_4: 613f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_4BITBUS; 61466b50a00SOded Gabbay break; 61566b50a00SOded Gabbay 61666b50a00SOded Gabbay default: 61766b50a00SOded Gabbay break; 61866b50a00SOded Gabbay } 61966b50a00SOded Gabbay 620f4932cfdSyangbo lu sdhci_writel(host, ctrl, ESDHC_PROCTL); 62166b50a00SOded Gabbay } 62266b50a00SOded Gabbay 623304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask) 624304f0a98SAlessio Igor Bogani { 625f2bc6000Syinbo.zhu u32 val; 626f2bc6000Syinbo.zhu 627304f0a98SAlessio Igor Bogani sdhci_reset(host, mask); 628304f0a98SAlessio Igor Bogani 629304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); 630304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); 631f2bc6000Syinbo.zhu 632f2bc6000Syinbo.zhu if (mask & SDHCI_RESET_ALL) { 633f2bc6000Syinbo.zhu val = sdhci_readl(host, ESDHC_TBCTL); 634f2bc6000Syinbo.zhu val &= ~ESDHC_TB_EN; 635f2bc6000Syinbo.zhu sdhci_writel(host, val, ESDHC_TBCTL); 636f2bc6000Syinbo.zhu } 637304f0a98SAlessio Igor Bogani } 638304f0a98SAlessio Igor Bogani 639ea35645aSyangbo lu /* The SCFG, Supplemental Configuration Unit, provides SoC specific 640ea35645aSyangbo lu * configuration and status registers for the device. There is a 641ea35645aSyangbo lu * SDHC IO VSEL control register on SCFG for some platforms. It's 642ea35645aSyangbo lu * used to support SDHC IO voltage switching. 643ea35645aSyangbo lu */ 644ea35645aSyangbo lu static const struct of_device_id scfg_device_ids[] = { 645ea35645aSyangbo lu { .compatible = "fsl,t1040-scfg", }, 646ea35645aSyangbo lu { .compatible = "fsl,ls1012a-scfg", }, 647ea35645aSyangbo lu { .compatible = "fsl,ls1046a-scfg", }, 648ea35645aSyangbo lu {} 649ea35645aSyangbo lu }; 650ea35645aSyangbo lu 651ea35645aSyangbo lu /* SDHC IO VSEL control register definition */ 652ea35645aSyangbo lu #define SCFG_SDHCIOVSELCR 0x408 653ea35645aSyangbo lu #define SDHCIOVSELCR_TGLEN 0x80000000 654ea35645aSyangbo lu #define SDHCIOVSELCR_VSELVAL 0x60000000 655ea35645aSyangbo lu #define SDHCIOVSELCR_SDHC_VS 0x00000001 656ea35645aSyangbo lu 657ea35645aSyangbo lu static int esdhc_signal_voltage_switch(struct mmc_host *mmc, 658ea35645aSyangbo lu struct mmc_ios *ios) 659ea35645aSyangbo lu { 660ea35645aSyangbo lu struct sdhci_host *host = mmc_priv(mmc); 661ea35645aSyangbo lu struct device_node *scfg_node; 662ea35645aSyangbo lu void __iomem *scfg_base = NULL; 663ea35645aSyangbo lu u32 sdhciovselcr; 664ea35645aSyangbo lu u32 val; 665ea35645aSyangbo lu 666ea35645aSyangbo lu /* 667ea35645aSyangbo lu * Signal Voltage Switching is only applicable for Host Controllers 668ea35645aSyangbo lu * v3.00 and above. 669ea35645aSyangbo lu */ 670ea35645aSyangbo lu if (host->version < SDHCI_SPEC_300) 671ea35645aSyangbo lu return 0; 672ea35645aSyangbo lu 673ea35645aSyangbo lu val = sdhci_readl(host, ESDHC_PROCTL); 674ea35645aSyangbo lu 675ea35645aSyangbo lu switch (ios->signal_voltage) { 676ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_330: 677ea35645aSyangbo lu val &= ~ESDHC_VOLT_SEL; 678ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 679ea35645aSyangbo lu return 0; 680ea35645aSyangbo lu case MMC_SIGNAL_VOLTAGE_180: 681ea35645aSyangbo lu scfg_node = of_find_matching_node(NULL, scfg_device_ids); 682ea35645aSyangbo lu if (scfg_node) 683ea35645aSyangbo lu scfg_base = of_iomap(scfg_node, 0); 684ea35645aSyangbo lu if (scfg_base) { 685ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 686ea35645aSyangbo lu SDHCIOVSELCR_VSELVAL; 687ea35645aSyangbo lu iowrite32be(sdhciovselcr, 688ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 689ea35645aSyangbo lu 690ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 691ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 692ea35645aSyangbo lu mdelay(5); 693ea35645aSyangbo lu 694ea35645aSyangbo lu sdhciovselcr = SDHCIOVSELCR_TGLEN | 695ea35645aSyangbo lu SDHCIOVSELCR_SDHC_VS; 696ea35645aSyangbo lu iowrite32be(sdhciovselcr, 697ea35645aSyangbo lu scfg_base + SCFG_SDHCIOVSELCR); 698ea35645aSyangbo lu iounmap(scfg_base); 699ea35645aSyangbo lu } else { 700ea35645aSyangbo lu val |= ESDHC_VOLT_SEL; 701ea35645aSyangbo lu sdhci_writel(host, val, ESDHC_PROCTL); 702ea35645aSyangbo lu } 703ea35645aSyangbo lu return 0; 704ea35645aSyangbo lu default: 705ea35645aSyangbo lu return 0; 706ea35645aSyangbo lu } 707ea35645aSyangbo lu } 708ea35645aSyangbo lu 709ba49cbd0Syangbo lu static int esdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) 710ba49cbd0Syangbo lu { 711ba49cbd0Syangbo lu struct sdhci_host *host = mmc_priv(mmc); 712ba49cbd0Syangbo lu u32 val; 713ba49cbd0Syangbo lu 714ba49cbd0Syangbo lu /* Use tuning block for tuning procedure */ 715ba49cbd0Syangbo lu esdhc_clock_enable(host, false); 716ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 717ba49cbd0Syangbo lu val |= ESDHC_FLUSH_ASYNC_FIFO; 718ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 719ba49cbd0Syangbo lu 720ba49cbd0Syangbo lu val = sdhci_readl(host, ESDHC_TBCTL); 721ba49cbd0Syangbo lu val |= ESDHC_TB_EN; 722ba49cbd0Syangbo lu sdhci_writel(host, val, ESDHC_TBCTL); 723ba49cbd0Syangbo lu esdhc_clock_enable(host, true); 724ba49cbd0Syangbo lu 725ba49cbd0Syangbo lu return sdhci_execute_tuning(mmc, opcode); 726ba49cbd0Syangbo lu } 727ba49cbd0Syangbo lu 7289e48b336SUlf Hansson #ifdef CONFIG_PM_SLEEP 729723f7924SRussell King static u32 esdhc_proctl; 730723f7924SRussell King static int esdhc_of_suspend(struct device *dev) 731723f7924SRussell King { 732723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 733723f7924SRussell King 734f4932cfdSyangbo lu esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); 735723f7924SRussell King 736d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 737d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 738d38dcad4SAdrian Hunter 739723f7924SRussell King return sdhci_suspend_host(host); 740723f7924SRussell King } 741723f7924SRussell King 74206732b84SUlf Hansson static int esdhc_of_resume(struct device *dev) 743723f7924SRussell King { 744723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 745723f7924SRussell King int ret = sdhci_resume_host(host); 746723f7924SRussell King 747723f7924SRussell King if (ret == 0) { 748723f7924SRussell King /* Isn't this already done by sdhci_resume_host() ? --rmk */ 749723f7924SRussell King esdhc_of_enable_dma(host); 750f4932cfdSyangbo lu sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 751723f7924SRussell King } 752723f7924SRussell King return ret; 753723f7924SRussell King } 754723f7924SRussell King #endif 755723f7924SRussell King 7569e48b336SUlf Hansson static SIMPLE_DEV_PM_OPS(esdhc_of_dev_pm_ops, 7579e48b336SUlf Hansson esdhc_of_suspend, 7589e48b336SUlf Hansson esdhc_of_resume); 7599e48b336SUlf Hansson 760f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = { 761f4932cfdSyangbo lu .read_l = esdhc_be_readl, 762f4932cfdSyangbo lu .read_w = esdhc_be_readw, 763f4932cfdSyangbo lu .read_b = esdhc_be_readb, 764f4932cfdSyangbo lu .write_l = esdhc_be_writel, 765f4932cfdSyangbo lu .write_w = esdhc_be_writew, 766f4932cfdSyangbo lu .write_b = esdhc_be_writeb, 767f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 768f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 769f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 770f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 771f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 772f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 773f4932cfdSyangbo lu .reset = esdhc_reset, 774f4932cfdSyangbo lu .set_uhs_signaling = sdhci_set_uhs_signaling, 775f4932cfdSyangbo lu }; 776f4932cfdSyangbo lu 777f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = { 778f4932cfdSyangbo lu .read_l = esdhc_le_readl, 779f4932cfdSyangbo lu .read_w = esdhc_le_readw, 780f4932cfdSyangbo lu .read_b = esdhc_le_readb, 781f4932cfdSyangbo lu .write_l = esdhc_le_writel, 782f4932cfdSyangbo lu .write_w = esdhc_le_writew, 783f4932cfdSyangbo lu .write_b = esdhc_le_writeb, 784f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 785f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 786f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 787f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 788f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 789f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 790f4932cfdSyangbo lu .reset = esdhc_reset, 791f4932cfdSyangbo lu .set_uhs_signaling = sdhci_set_uhs_signaling, 792f4932cfdSyangbo lu }; 793f4932cfdSyangbo lu 794f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { 795e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 796e9acc77dSyangbo lu #ifdef CONFIG_PPC 797e9acc77dSyangbo lu SDHCI_QUIRK_BROKEN_CARD_DETECTION | 798e9acc77dSyangbo lu #endif 799e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 800e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 801f4932cfdSyangbo lu .ops = &sdhci_esdhc_be_ops, 8027657c3a7SAlbert Herranz }; 80338576af1SShawn Guo 804f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { 805e9acc77dSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | 806e9acc77dSyangbo lu SDHCI_QUIRK_NO_CARD_NO_RESET | 807e9acc77dSyangbo lu SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 808f4932cfdSyangbo lu .ops = &sdhci_esdhc_le_ops, 809f4932cfdSyangbo lu }; 810f4932cfdSyangbo lu 811151ede40Syangbo lu static struct soc_device_attribute soc_incorrect_hostver[] = { 812151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "1.0", }, 813151ede40Syangbo lu { .family = "QorIQ T4240", .revision = "2.0", }, 814151ede40Syangbo lu { }, 815151ede40Syangbo lu }; 816151ede40Syangbo lu 817f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) 818f4932cfdSyangbo lu { 819*67fdfbdfSyinbo.zhu const struct of_device_id *match; 820f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host; 821f4932cfdSyangbo lu struct sdhci_esdhc *esdhc; 82219c3a0efSyangbo lu struct device_node *np; 82319c3a0efSyangbo lu struct clk *clk; 82419c3a0efSyangbo lu u32 val; 825f4932cfdSyangbo lu u16 host_ver; 826f4932cfdSyangbo lu 827f4932cfdSyangbo lu pltfm_host = sdhci_priv(host); 8288605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 829f4932cfdSyangbo lu 830f4932cfdSyangbo lu host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); 831f4932cfdSyangbo lu esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> 832f4932cfdSyangbo lu SDHCI_VENDOR_VER_SHIFT; 833f4932cfdSyangbo lu esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; 834151ede40Syangbo lu if (soc_device_match(soc_incorrect_hostver)) 835151ede40Syangbo lu esdhc->quirk_incorrect_hostver = true; 836151ede40Syangbo lu else 837151ede40Syangbo lu esdhc->quirk_incorrect_hostver = false; 83819c3a0efSyangbo lu 839*67fdfbdfSyinbo.zhu match = of_match_node(sdhci_esdhc_of_match, pdev->dev.of_node); 840*67fdfbdfSyinbo.zhu if (match) 841*67fdfbdfSyinbo.zhu esdhc->clk_fixup = match->data; 84219c3a0efSyangbo lu np = pdev->dev.of_node; 84319c3a0efSyangbo lu clk = of_clk_get(np, 0); 84419c3a0efSyangbo lu if (!IS_ERR(clk)) { 84519c3a0efSyangbo lu /* 84619c3a0efSyangbo lu * esdhc->peripheral_clock would be assigned with a value 84719c3a0efSyangbo lu * which is eSDHC base clock when use periperal clock. 84819c3a0efSyangbo lu * For ls1046a, the clock value got by common clk API is 84919c3a0efSyangbo lu * peripheral clock while the eSDHC base clock is 1/2 85019c3a0efSyangbo lu * peripheral clock. 85119c3a0efSyangbo lu */ 85219c3a0efSyangbo lu if (of_device_is_compatible(np, "fsl,ls1046a-esdhc")) 85319c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk) / 2; 85419c3a0efSyangbo lu else 85519c3a0efSyangbo lu esdhc->peripheral_clock = clk_get_rate(clk); 85619c3a0efSyangbo lu 85719c3a0efSyangbo lu clk_put(clk); 85819c3a0efSyangbo lu } 85919c3a0efSyangbo lu 86019c3a0efSyangbo lu if (esdhc->peripheral_clock) { 86119c3a0efSyangbo lu esdhc_clock_enable(host, false); 86219c3a0efSyangbo lu val = sdhci_readl(host, ESDHC_DMA_SYSCTL); 86319c3a0efSyangbo lu val |= ESDHC_PERIPHERAL_CLK_SEL; 86419c3a0efSyangbo lu sdhci_writel(host, val, ESDHC_DMA_SYSCTL); 86519c3a0efSyangbo lu esdhc_clock_enable(host, true); 86619c3a0efSyangbo lu } 867f4932cfdSyangbo lu } 868f4932cfdSyangbo lu 869c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 87038576af1SShawn Guo { 87166b50a00SOded Gabbay struct sdhci_host *host; 872dcaff04dSOded Gabbay struct device_node *np; 8731ef5e49eSyangbo lu struct sdhci_pltfm_host *pltfm_host; 8741ef5e49eSyangbo lu struct sdhci_esdhc *esdhc; 87566b50a00SOded Gabbay int ret; 87666b50a00SOded Gabbay 877f4932cfdSyangbo lu np = pdev->dev.of_node; 878f4932cfdSyangbo lu 879150d4240SJulia Lawall if (of_property_read_bool(np, "little-endian")) 8808605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, 8818605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 882f4932cfdSyangbo lu else 8838605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, 8848605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 885f4932cfdSyangbo lu 88666b50a00SOded Gabbay if (IS_ERR(host)) 88766b50a00SOded Gabbay return PTR_ERR(host); 88866b50a00SOded Gabbay 889ea35645aSyangbo lu host->mmc_host_ops.start_signal_voltage_switch = 890ea35645aSyangbo lu esdhc_signal_voltage_switch; 891ba49cbd0Syangbo lu host->mmc_host_ops.execute_tuning = esdhc_execute_tuning; 8926b236f37Syangbo lu host->tuning_delay = 1; 893ea35645aSyangbo lu 894f4932cfdSyangbo lu esdhc_init(pdev, host); 895f4932cfdSyangbo lu 89666b50a00SOded Gabbay sdhci_get_of_property(pdev); 89766b50a00SOded Gabbay 8981ef5e49eSyangbo lu pltfm_host = sdhci_priv(host); 8998605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 9001ef5e49eSyangbo lu if (esdhc->vendor_ver == VENDOR_V_22) 9011ef5e49eSyangbo lu host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 9021ef5e49eSyangbo lu 9031ef5e49eSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) 9041ef5e49eSyangbo lu host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 9051ef5e49eSyangbo lu 90674fd5e30SYangbo Lu if (of_device_is_compatible(np, "fsl,p5040-esdhc") || 90774fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p5020-esdhc") || 90874fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p4080-esdhc") || 90974fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p1020-esdhc") || 910e9acc77dSyangbo lu of_device_is_compatible(np, "fsl,t1040-esdhc")) 91174fd5e30SYangbo Lu host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 91274fd5e30SYangbo Lu 913a22950c8Syangbo lu if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) 914a22950c8Syangbo lu host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 915a22950c8Syangbo lu 916dcaff04dSOded Gabbay if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { 917dcaff04dSOded Gabbay /* 918dcaff04dSOded Gabbay * Freescale messed up with P2020 as it has a non-standard 919dcaff04dSOded Gabbay * host control register 920dcaff04dSOded Gabbay */ 921dcaff04dSOded Gabbay host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; 922dcaff04dSOded Gabbay } 923dcaff04dSOded Gabbay 92466b50a00SOded Gabbay /* call to generic mmc_of_parse to support additional capabilities */ 925f0991408SUlf Hansson ret = mmc_of_parse(host->mmc); 926f0991408SUlf Hansson if (ret) 927f0991408SUlf Hansson goto err; 928f0991408SUlf Hansson 929490104acSHaijun Zhang mmc_of_parse_voltage(np, &host->ocr_mask); 93066b50a00SOded Gabbay 93166b50a00SOded Gabbay ret = sdhci_add_host(host); 93266b50a00SOded Gabbay if (ret) 933f0991408SUlf Hansson goto err; 93466b50a00SOded Gabbay 935f0991408SUlf Hansson return 0; 936f0991408SUlf Hansson err: 937f0991408SUlf Hansson sdhci_pltfm_free(pdev); 93866b50a00SOded Gabbay return ret; 93938576af1SShawn Guo } 94038576af1SShawn Guo 94138576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 94238576af1SShawn Guo .driver = { 94338576af1SShawn Guo .name = "sdhci-esdhc", 94438576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 9459e48b336SUlf Hansson .pm = &esdhc_of_dev_pm_ops, 94638576af1SShawn Guo }, 94738576af1SShawn Guo .probe = sdhci_esdhc_probe, 948caebcae9SKevin Hao .remove = sdhci_pltfm_unregister, 94938576af1SShawn Guo }; 95038576af1SShawn Guo 951d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 95238576af1SShawn Guo 95338576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 95438576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 95538576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 95638576af1SShawn Guo MODULE_LICENSE("GPL v2"); 957