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> 197657c3a7SAlbert Herranz #include <linux/delay.h> 2088b47679SPaul Gortmaker #include <linux/module.h> 217657c3a7SAlbert Herranz #include <linux/mmc/host.h> 2238576af1SShawn Guo #include "sdhci-pltfm.h" 2380872e21SWolfram Sang #include "sdhci-esdhc.h" 247657c3a7SAlbert Herranz 25137ccd46SJerry Huang #define VENDOR_V_22 0x12 26a4071fbbSHaijun Zhang #define VENDOR_V_23 0x13 27f4932cfdSyangbo lu 28f4932cfdSyangbo lu struct sdhci_esdhc { 29f4932cfdSyangbo lu u8 vendor_ver; 30f4932cfdSyangbo lu u8 spec_ver; 31f4932cfdSyangbo lu }; 32f4932cfdSyangbo lu 33f4932cfdSyangbo lu /** 34f4932cfdSyangbo lu * esdhc_read*_fixup - Fixup the value read from incompatible eSDHC register 35f4932cfdSyangbo lu * to make it compatible with SD spec. 36f4932cfdSyangbo lu * 37f4932cfdSyangbo lu * @host: pointer to sdhci_host 38f4932cfdSyangbo lu * @spec_reg: SD spec register address 39f4932cfdSyangbo lu * @value: 32bit eSDHC register value on spec_reg address 40f4932cfdSyangbo lu * 41f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 42f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 43f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 44f4932cfdSyangbo lu * and SD spec. 45f4932cfdSyangbo lu * 46f4932cfdSyangbo lu * Return a fixed up register value 47f4932cfdSyangbo lu */ 48f4932cfdSyangbo lu static u32 esdhc_readl_fixup(struct sdhci_host *host, 49f4932cfdSyangbo lu int spec_reg, u32 value) 50137ccd46SJerry Huang { 51f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 52*8605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 53137ccd46SJerry Huang u32 ret; 54137ccd46SJerry Huang 55137ccd46SJerry Huang /* 56137ccd46SJerry Huang * The bit of ADMA flag in eSDHC is not compatible with standard 57137ccd46SJerry Huang * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is 58137ccd46SJerry Huang * supported by eSDHC. 59137ccd46SJerry Huang * And for many FSL eSDHC controller, the reset value of field 60f4932cfdSyangbo lu * SDHCI_CAN_DO_ADMA1 is 1, but some of them can't support ADMA, 61137ccd46SJerry Huang * only these vendor version is greater than 2.2/0x12 support ADMA. 62137ccd46SJerry Huang */ 63f4932cfdSyangbo lu if ((spec_reg == SDHCI_CAPABILITIES) && (value & SDHCI_CAN_DO_ADMA1)) { 64f4932cfdSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) { 65f4932cfdSyangbo lu ret = value | SDHCI_CAN_DO_ADMA2; 66f4932cfdSyangbo lu return ret; 67137ccd46SJerry Huang } 68f4932cfdSyangbo lu } 69f4932cfdSyangbo lu ret = value; 70137ccd46SJerry Huang return ret; 71137ccd46SJerry Huang } 72137ccd46SJerry Huang 73f4932cfdSyangbo lu static u16 esdhc_readw_fixup(struct sdhci_host *host, 74f4932cfdSyangbo lu int spec_reg, u32 value) 757657c3a7SAlbert Herranz { 767657c3a7SAlbert Herranz u16 ret; 77f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 787657c3a7SAlbert Herranz 79f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_VERSION) 80f4932cfdSyangbo lu ret = value & 0xffff; 817657c3a7SAlbert Herranz else 82f4932cfdSyangbo lu ret = (value >> shift) & 0xffff; 83e51cbc9eSXu lei return ret; 84e51cbc9eSXu lei } 85e51cbc9eSXu lei 86f4932cfdSyangbo lu static u8 esdhc_readb_fixup(struct sdhci_host *host, 87f4932cfdSyangbo lu int spec_reg, u32 value) 88e51cbc9eSXu lei { 89f4932cfdSyangbo lu u8 ret; 90f4932cfdSyangbo lu u8 dma_bits; 91f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 92f4932cfdSyangbo lu 93f4932cfdSyangbo lu ret = (value >> shift) & 0xff; 94ba8c4dc9SRoy Zang 95ba8c4dc9SRoy Zang /* 96ba8c4dc9SRoy Zang * "DMA select" locates at offset 0x28 in SD specification, but on 97ba8c4dc9SRoy Zang * P5020 or P3041, it locates at 0x29. 98ba8c4dc9SRoy Zang */ 99f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 100ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 101f4932cfdSyangbo lu dma_bits = (value >> 5) & SDHCI_CTRL_DMA_MASK; 102ba8c4dc9SRoy Zang /* fixup the result */ 103ba8c4dc9SRoy Zang ret &= ~SDHCI_CTRL_DMA_MASK; 104ba8c4dc9SRoy Zang ret |= dma_bits; 105ba8c4dc9SRoy Zang } 106f4932cfdSyangbo lu return ret; 107f4932cfdSyangbo lu } 108f4932cfdSyangbo lu 109f4932cfdSyangbo lu /** 110f4932cfdSyangbo lu * esdhc_write*_fixup - Fixup the SD spec register value so that it could be 111f4932cfdSyangbo lu * written into eSDHC register. 112f4932cfdSyangbo lu * 113f4932cfdSyangbo lu * @host: pointer to sdhci_host 114f4932cfdSyangbo lu * @spec_reg: SD spec register address 115f4932cfdSyangbo lu * @value: 8/16/32bit SD spec register value that would be written 116f4932cfdSyangbo lu * @old_value: 32bit eSDHC register value on spec_reg address 117f4932cfdSyangbo lu * 118f4932cfdSyangbo lu * In SD spec, there are 8/16/32/64 bits registers, while all of eSDHC 119f4932cfdSyangbo lu * registers are 32 bits. There are differences in register size, register 120f4932cfdSyangbo lu * address, register function, bit position and function between eSDHC spec 121f4932cfdSyangbo lu * and SD spec. 122f4932cfdSyangbo lu * 123f4932cfdSyangbo lu * Return a fixed up register value 124f4932cfdSyangbo lu */ 125f4932cfdSyangbo lu static u32 esdhc_writel_fixup(struct sdhci_host *host, 126f4932cfdSyangbo lu int spec_reg, u32 value, u32 old_value) 127f4932cfdSyangbo lu { 128f4932cfdSyangbo lu u32 ret; 129f4932cfdSyangbo lu 130f4932cfdSyangbo lu /* 131f4932cfdSyangbo lu * Enabling IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] 132f4932cfdSyangbo lu * when SYSCTL[RSTD] is set for some special operations. 133f4932cfdSyangbo lu * No any impact on other operation. 134f4932cfdSyangbo lu */ 135f4932cfdSyangbo lu if (spec_reg == SDHCI_INT_ENABLE) 136f4932cfdSyangbo lu ret = value | SDHCI_INT_BLK_GAP; 137f4932cfdSyangbo lu else 138f4932cfdSyangbo lu ret = value; 139ba8c4dc9SRoy Zang 1407657c3a7SAlbert Herranz return ret; 1417657c3a7SAlbert Herranz } 1427657c3a7SAlbert Herranz 143f4932cfdSyangbo lu static u32 esdhc_writew_fixup(struct sdhci_host *host, 144f4932cfdSyangbo lu int spec_reg, u16 value, u32 old_value) 145a4071fbbSHaijun Zhang { 146f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 147f4932cfdSyangbo lu int shift = (spec_reg & 0x2) * 8; 148f4932cfdSyangbo lu u32 ret; 149f4932cfdSyangbo lu 150f4932cfdSyangbo lu switch (spec_reg) { 151f4932cfdSyangbo lu case SDHCI_TRANSFER_MODE: 152a4071fbbSHaijun Zhang /* 153f4932cfdSyangbo lu * Postpone this write, we must do it together with a 154f4932cfdSyangbo lu * command write that is down below. Return old value. 155a4071fbbSHaijun Zhang */ 156f4932cfdSyangbo lu pltfm_host->xfer_mode_shadow = value; 157f4932cfdSyangbo lu return old_value; 158f4932cfdSyangbo lu case SDHCI_COMMAND: 159f4932cfdSyangbo lu ret = (value << 16) | pltfm_host->xfer_mode_shadow; 160f4932cfdSyangbo lu return ret; 161a4071fbbSHaijun Zhang } 162a4071fbbSHaijun Zhang 163f4932cfdSyangbo lu ret = old_value & (~(0xffff << shift)); 164f4932cfdSyangbo lu ret |= (value << shift); 165f4932cfdSyangbo lu 166f4932cfdSyangbo lu if (spec_reg == SDHCI_BLOCK_SIZE) { 1677657c3a7SAlbert Herranz /* 1687657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 1697657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 1707657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 1717657c3a7SAlbert Herranz */ 172f4932cfdSyangbo lu ret &= (~SDHCI_MAKE_BLKSZ(0x7, 0)); 1737657c3a7SAlbert Herranz } 174f4932cfdSyangbo lu return ret; 1757657c3a7SAlbert Herranz } 1767657c3a7SAlbert Herranz 177f4932cfdSyangbo lu static u32 esdhc_writeb_fixup(struct sdhci_host *host, 178f4932cfdSyangbo lu int spec_reg, u8 value, u32 old_value) 1797657c3a7SAlbert Herranz { 180f4932cfdSyangbo lu u32 ret; 181f4932cfdSyangbo lu u32 dma_bits; 182f4932cfdSyangbo lu u8 tmp; 183f4932cfdSyangbo lu int shift = (spec_reg & 0x3) * 8; 184f4932cfdSyangbo lu 185ba8c4dc9SRoy Zang /* 1869e4703dfSyangbo lu * eSDHC doesn't have a standard power control register, so we do 1879e4703dfSyangbo lu * nothing here to avoid incorrect operation. 1889e4703dfSyangbo lu */ 1899e4703dfSyangbo lu if (spec_reg == SDHCI_POWER_CONTROL) 1909e4703dfSyangbo lu return old_value; 1919e4703dfSyangbo lu /* 192ba8c4dc9SRoy Zang * "DMA select" location is offset 0x28 in SD specification, but on 193ba8c4dc9SRoy Zang * P5020 or P3041, it's located at 0x29. 194ba8c4dc9SRoy Zang */ 195f4932cfdSyangbo lu if (spec_reg == SDHCI_HOST_CONTROL) { 196dcaff04dSOded Gabbay /* 197dcaff04dSOded Gabbay * If host control register is not standard, exit 198dcaff04dSOded Gabbay * this function 199dcaff04dSOded Gabbay */ 200dcaff04dSOded Gabbay if (host->quirks2 & SDHCI_QUIRK2_BROKEN_HOST_CONTROL) 201f4932cfdSyangbo lu return old_value; 202dcaff04dSOded Gabbay 203ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 204f4932cfdSyangbo lu dma_bits = (value & SDHCI_CTRL_DMA_MASK) << 5; 205f4932cfdSyangbo lu ret = (old_value & (~(SDHCI_CTRL_DMA_MASK << 5))) | dma_bits; 206f4932cfdSyangbo lu tmp = (value & (~SDHCI_CTRL_DMA_MASK)) | 207f4932cfdSyangbo lu (old_value & SDHCI_CTRL_DMA_MASK); 208f4932cfdSyangbo lu ret = (ret & (~0xff)) | tmp; 209f4932cfdSyangbo lu 210f4932cfdSyangbo lu /* Prevent SDHCI core from writing reserved bits (e.g. HISPD) */ 211f4932cfdSyangbo lu ret &= ~ESDHC_HOST_CONTROL_RES; 212f4932cfdSyangbo lu return ret; 213ba8c4dc9SRoy Zang } 214ba8c4dc9SRoy Zang 215f4932cfdSyangbo lu ret = (old_value & (~(0xff << shift))) | (value << shift); 216f4932cfdSyangbo lu return ret; 217f4932cfdSyangbo lu } 218f4932cfdSyangbo lu 219f4932cfdSyangbo lu static u32 esdhc_be_readl(struct sdhci_host *host, int reg) 220f4932cfdSyangbo lu { 221f4932cfdSyangbo lu u32 ret; 222f4932cfdSyangbo lu u32 value; 223f4932cfdSyangbo lu 224f4932cfdSyangbo lu value = ioread32be(host->ioaddr + reg); 225f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 226f4932cfdSyangbo lu 227f4932cfdSyangbo lu return ret; 228f4932cfdSyangbo lu } 229f4932cfdSyangbo lu 230f4932cfdSyangbo lu static u32 esdhc_le_readl(struct sdhci_host *host, int reg) 231f4932cfdSyangbo lu { 232f4932cfdSyangbo lu u32 ret; 233f4932cfdSyangbo lu u32 value; 234f4932cfdSyangbo lu 235f4932cfdSyangbo lu value = ioread32(host->ioaddr + reg); 236f4932cfdSyangbo lu ret = esdhc_readl_fixup(host, reg, value); 237f4932cfdSyangbo lu 238f4932cfdSyangbo lu return ret; 239f4932cfdSyangbo lu } 240f4932cfdSyangbo lu 241f4932cfdSyangbo lu static u16 esdhc_be_readw(struct sdhci_host *host, int reg) 242f4932cfdSyangbo lu { 243f4932cfdSyangbo lu u16 ret; 244f4932cfdSyangbo lu u32 value; 245f4932cfdSyangbo lu int base = reg & ~0x3; 246f4932cfdSyangbo lu 247f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 248f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 249f4932cfdSyangbo lu return ret; 250f4932cfdSyangbo lu } 251f4932cfdSyangbo lu 252f4932cfdSyangbo lu static u16 esdhc_le_readw(struct sdhci_host *host, int reg) 253f4932cfdSyangbo lu { 254f4932cfdSyangbo lu u16 ret; 255f4932cfdSyangbo lu u32 value; 256f4932cfdSyangbo lu int base = reg & ~0x3; 257f4932cfdSyangbo lu 258f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 259f4932cfdSyangbo lu ret = esdhc_readw_fixup(host, reg, value); 260f4932cfdSyangbo lu return ret; 261f4932cfdSyangbo lu } 262f4932cfdSyangbo lu 263f4932cfdSyangbo lu static u8 esdhc_be_readb(struct sdhci_host *host, int reg) 264f4932cfdSyangbo lu { 265f4932cfdSyangbo lu u8 ret; 266f4932cfdSyangbo lu u32 value; 267f4932cfdSyangbo lu int base = reg & ~0x3; 268f4932cfdSyangbo lu 269f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 270f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 271f4932cfdSyangbo lu return ret; 272f4932cfdSyangbo lu } 273f4932cfdSyangbo lu 274f4932cfdSyangbo lu static u8 esdhc_le_readb(struct sdhci_host *host, int reg) 275f4932cfdSyangbo lu { 276f4932cfdSyangbo lu u8 ret; 277f4932cfdSyangbo lu u32 value; 278f4932cfdSyangbo lu int base = reg & ~0x3; 279f4932cfdSyangbo lu 280f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 281f4932cfdSyangbo lu ret = esdhc_readb_fixup(host, reg, value); 282f4932cfdSyangbo lu return ret; 283f4932cfdSyangbo lu } 284f4932cfdSyangbo lu 285f4932cfdSyangbo lu static void esdhc_be_writel(struct sdhci_host *host, u32 val, int reg) 286f4932cfdSyangbo lu { 287f4932cfdSyangbo lu u32 value; 288f4932cfdSyangbo lu 289f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 290f4932cfdSyangbo lu iowrite32be(value, host->ioaddr + reg); 291f4932cfdSyangbo lu } 292f4932cfdSyangbo lu 293f4932cfdSyangbo lu static void esdhc_le_writel(struct sdhci_host *host, u32 val, int reg) 294f4932cfdSyangbo lu { 295f4932cfdSyangbo lu u32 value; 296f4932cfdSyangbo lu 297f4932cfdSyangbo lu value = esdhc_writel_fixup(host, reg, val, 0); 298f4932cfdSyangbo lu iowrite32(value, host->ioaddr + reg); 299f4932cfdSyangbo lu } 300f4932cfdSyangbo lu 301f4932cfdSyangbo lu static void esdhc_be_writew(struct sdhci_host *host, u16 val, int reg) 302f4932cfdSyangbo lu { 303f4932cfdSyangbo lu int base = reg & ~0x3; 304f4932cfdSyangbo lu u32 value; 305f4932cfdSyangbo lu u32 ret; 306f4932cfdSyangbo lu 307f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 308f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 309f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 310f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 311f4932cfdSyangbo lu } 312f4932cfdSyangbo lu 313f4932cfdSyangbo lu static void esdhc_le_writew(struct sdhci_host *host, u16 val, int reg) 314f4932cfdSyangbo lu { 315f4932cfdSyangbo lu int base = reg & ~0x3; 316f4932cfdSyangbo lu u32 value; 317f4932cfdSyangbo lu u32 ret; 318f4932cfdSyangbo lu 319f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 320f4932cfdSyangbo lu ret = esdhc_writew_fixup(host, reg, val, value); 321f4932cfdSyangbo lu if (reg != SDHCI_TRANSFER_MODE) 322f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 323f4932cfdSyangbo lu } 324f4932cfdSyangbo lu 325f4932cfdSyangbo lu static void esdhc_be_writeb(struct sdhci_host *host, u8 val, int reg) 326f4932cfdSyangbo lu { 327f4932cfdSyangbo lu int base = reg & ~0x3; 328f4932cfdSyangbo lu u32 value; 329f4932cfdSyangbo lu u32 ret; 330f4932cfdSyangbo lu 331f4932cfdSyangbo lu value = ioread32be(host->ioaddr + base); 332f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 333f4932cfdSyangbo lu iowrite32be(ret, host->ioaddr + base); 334f4932cfdSyangbo lu } 335f4932cfdSyangbo lu 336f4932cfdSyangbo lu static void esdhc_le_writeb(struct sdhci_host *host, u8 val, int reg) 337f4932cfdSyangbo lu { 338f4932cfdSyangbo lu int base = reg & ~0x3; 339f4932cfdSyangbo lu u32 value; 340f4932cfdSyangbo lu u32 ret; 341f4932cfdSyangbo lu 342f4932cfdSyangbo lu value = ioread32(host->ioaddr + base); 343f4932cfdSyangbo lu ret = esdhc_writeb_fixup(host, reg, val, value); 344f4932cfdSyangbo lu iowrite32(ret, host->ioaddr + base); 3457657c3a7SAlbert Herranz } 3467657c3a7SAlbert Herranz 347a4071fbbSHaijun Zhang /* 348a4071fbbSHaijun Zhang * For Abort or Suspend after Stop at Block Gap, ignore the ADMA 349a4071fbbSHaijun Zhang * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) 350a4071fbbSHaijun Zhang * and Block Gap Event(IRQSTAT[BGE]) are also set. 351a4071fbbSHaijun Zhang * For Continue, apply soft reset for data(SYSCTL[RSTD]); 352a4071fbbSHaijun Zhang * and re-issue the entire read transaction from beginning. 353a4071fbbSHaijun Zhang */ 354f4932cfdSyangbo lu static void esdhc_of_adma_workaround(struct sdhci_host *host, u32 intmask) 355a4071fbbSHaijun Zhang { 356f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 357*8605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 358a4071fbbSHaijun Zhang bool applicable; 359a4071fbbSHaijun Zhang dma_addr_t dmastart; 360a4071fbbSHaijun Zhang dma_addr_t dmanow; 361a4071fbbSHaijun Zhang 362a4071fbbSHaijun Zhang applicable = (intmask & SDHCI_INT_DATA_END) && 363a4071fbbSHaijun Zhang (intmask & SDHCI_INT_BLK_GAP) && 364f4932cfdSyangbo lu (esdhc->vendor_ver == VENDOR_V_23); 365a4071fbbSHaijun Zhang if (!applicable) 366a4071fbbSHaijun Zhang return; 367a4071fbbSHaijun Zhang 368a4071fbbSHaijun Zhang host->data->error = 0; 369a4071fbbSHaijun Zhang dmastart = sg_dma_address(host->data->sg); 370a4071fbbSHaijun Zhang dmanow = dmastart + host->data->bytes_xfered; 371a4071fbbSHaijun Zhang /* 372a4071fbbSHaijun Zhang * Force update to the next DMA block boundary. 373a4071fbbSHaijun Zhang */ 374a4071fbbSHaijun Zhang dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + 375a4071fbbSHaijun Zhang SDHCI_DEFAULT_BOUNDARY_SIZE; 376a4071fbbSHaijun Zhang host->data->bytes_xfered = dmanow - dmastart; 377a4071fbbSHaijun Zhang sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); 378a4071fbbSHaijun Zhang } 379a4071fbbSHaijun Zhang 38080872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host) 3817657c3a7SAlbert Herranz { 382f4932cfdSyangbo lu u32 value; 383f4932cfdSyangbo lu 384f4932cfdSyangbo lu value = sdhci_readl(host, ESDHC_DMA_SYSCTL); 385f4932cfdSyangbo lu value |= ESDHC_DMA_SNOOP; 386f4932cfdSyangbo lu sdhci_writel(host, value, ESDHC_DMA_SYSCTL); 3877657c3a7SAlbert Herranz return 0; 3887657c3a7SAlbert Herranz } 3897657c3a7SAlbert Herranz 39080872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 3917657c3a7SAlbert Herranz { 392e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 3937657c3a7SAlbert Herranz 394e307148fSShawn Guo return pltfm_host->clock; 3957657c3a7SAlbert Herranz } 3967657c3a7SAlbert Herranz 39780872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 3987657c3a7SAlbert Herranz { 399e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 4007657c3a7SAlbert Herranz 401e307148fSShawn Guo return pltfm_host->clock / 256 / 16; 4027657c3a7SAlbert Herranz } 4037657c3a7SAlbert Herranz 404f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 405f060bc9cSJerry Huang { 406f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 407*8605e7aeSJisheng Zhang struct sdhci_esdhc *esdhc = sdhci_pltfm_priv(pltfm_host); 408bd455029SJoakim Tjernlund int pre_div = 1; 409d31fc00aSDong Aisheng int div = 1; 410d31fc00aSDong Aisheng u32 temp; 411d31fc00aSDong Aisheng 4121650d0c7SRussell King host->mmc->actual_clock = 0; 4131650d0c7SRussell King 414d31fc00aSDong Aisheng if (clock == 0) 415373073efSRussell King return; 416d31fc00aSDong Aisheng 41777bd2f6fSYangbo Lu /* Workaround to start pre_div at 2 for VNN < VENDOR_V_23 */ 418f4932cfdSyangbo lu if (esdhc->vendor_ver < VENDOR_V_23) 41977bd2f6fSYangbo Lu pre_div = 2; 42077bd2f6fSYangbo Lu 421f060bc9cSJerry Huang /* Workaround to reduce the clock frequency for p1010 esdhc */ 422f060bc9cSJerry Huang if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) { 423f060bc9cSJerry Huang if (clock > 20000000) 424f060bc9cSJerry Huang clock -= 5000000; 425f060bc9cSJerry Huang if (clock > 40000000) 426f060bc9cSJerry Huang clock -= 5000000; 427f060bc9cSJerry Huang } 428f060bc9cSJerry Huang 429d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 430d31fc00aSDong Aisheng temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 431d31fc00aSDong Aisheng | ESDHC_CLOCK_MASK); 432d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 433d31fc00aSDong Aisheng 434d31fc00aSDong Aisheng while (host->max_clk / pre_div / 16 > clock && pre_div < 256) 435d31fc00aSDong Aisheng pre_div *= 2; 436d31fc00aSDong Aisheng 437d31fc00aSDong Aisheng while (host->max_clk / pre_div / div > clock && div < 16) 438d31fc00aSDong Aisheng div++; 439d31fc00aSDong Aisheng 440d31fc00aSDong Aisheng dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 441e76b8559SDong Aisheng clock, host->max_clk / pre_div / div); 442bd455029SJoakim Tjernlund host->mmc->actual_clock = host->max_clk / pre_div / div; 443d31fc00aSDong Aisheng pre_div >>= 1; 444d31fc00aSDong Aisheng div--; 445d31fc00aSDong Aisheng 446d31fc00aSDong Aisheng temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 447d31fc00aSDong Aisheng temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 448d31fc00aSDong Aisheng | (div << ESDHC_DIVIDER_SHIFT) 449d31fc00aSDong Aisheng | (pre_div << ESDHC_PREDIV_SHIFT)); 450d31fc00aSDong Aisheng sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 451d31fc00aSDong Aisheng mdelay(1); 452f060bc9cSJerry Huang } 453f060bc9cSJerry Huang 4542317f56cSRussell King static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) 45566b50a00SOded Gabbay { 45666b50a00SOded Gabbay u32 ctrl; 45766b50a00SOded Gabbay 458f4932cfdSyangbo lu ctrl = sdhci_readl(host, ESDHC_PROCTL); 459f4932cfdSyangbo lu ctrl &= (~ESDHC_CTRL_BUSWIDTH_MASK); 46066b50a00SOded Gabbay switch (width) { 46166b50a00SOded Gabbay case MMC_BUS_WIDTH_8: 462f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_8BITBUS; 46366b50a00SOded Gabbay break; 46466b50a00SOded Gabbay 46566b50a00SOded Gabbay case MMC_BUS_WIDTH_4: 466f4932cfdSyangbo lu ctrl |= ESDHC_CTRL_4BITBUS; 46766b50a00SOded Gabbay break; 46866b50a00SOded Gabbay 46966b50a00SOded Gabbay default: 47066b50a00SOded Gabbay break; 47166b50a00SOded Gabbay } 47266b50a00SOded Gabbay 473f4932cfdSyangbo lu sdhci_writel(host, ctrl, ESDHC_PROCTL); 47466b50a00SOded Gabbay } 47566b50a00SOded Gabbay 476304f0a98SAlessio Igor Bogani static void esdhc_reset(struct sdhci_host *host, u8 mask) 477304f0a98SAlessio Igor Bogani { 478304f0a98SAlessio Igor Bogani sdhci_reset(host, mask); 479304f0a98SAlessio Igor Bogani 480304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); 481304f0a98SAlessio Igor Bogani sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); 482304f0a98SAlessio Igor Bogani } 483304f0a98SAlessio Igor Bogani 484723f7924SRussell King #ifdef CONFIG_PM 485723f7924SRussell King static u32 esdhc_proctl; 486723f7924SRussell King static int esdhc_of_suspend(struct device *dev) 487723f7924SRussell King { 488723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 489723f7924SRussell King 490f4932cfdSyangbo lu esdhc_proctl = sdhci_readl(host, SDHCI_HOST_CONTROL); 491723f7924SRussell King 492723f7924SRussell King return sdhci_suspend_host(host); 493723f7924SRussell King } 494723f7924SRussell King 49506732b84SUlf Hansson static int esdhc_of_resume(struct device *dev) 496723f7924SRussell King { 497723f7924SRussell King struct sdhci_host *host = dev_get_drvdata(dev); 498723f7924SRussell King int ret = sdhci_resume_host(host); 499723f7924SRussell King 500723f7924SRussell King if (ret == 0) { 501723f7924SRussell King /* Isn't this already done by sdhci_resume_host() ? --rmk */ 502723f7924SRussell King esdhc_of_enable_dma(host); 503f4932cfdSyangbo lu sdhci_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 504723f7924SRussell King } 505723f7924SRussell King return ret; 506723f7924SRussell King } 507723f7924SRussell King 508723f7924SRussell King static const struct dev_pm_ops esdhc_pmops = { 50906732b84SUlf Hansson .suspend = esdhc_of_suspend, 51006732b84SUlf Hansson .resume = esdhc_of_resume, 511723f7924SRussell King }; 512723f7924SRussell King #define ESDHC_PMOPS (&esdhc_pmops) 513723f7924SRussell King #else 514723f7924SRussell King #define ESDHC_PMOPS NULL 515723f7924SRussell King #endif 516723f7924SRussell King 517f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_be_ops = { 518f4932cfdSyangbo lu .read_l = esdhc_be_readl, 519f4932cfdSyangbo lu .read_w = esdhc_be_readw, 520f4932cfdSyangbo lu .read_b = esdhc_be_readb, 521f4932cfdSyangbo lu .write_l = esdhc_be_writel, 522f4932cfdSyangbo lu .write_w = esdhc_be_writew, 523f4932cfdSyangbo lu .write_b = esdhc_be_writeb, 524f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 525f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 526f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 527f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 528f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 529f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 530f4932cfdSyangbo lu .reset = esdhc_reset, 531f4932cfdSyangbo lu .set_uhs_signaling = sdhci_set_uhs_signaling, 532f4932cfdSyangbo lu }; 533f4932cfdSyangbo lu 534f4932cfdSyangbo lu static const struct sdhci_ops sdhci_esdhc_le_ops = { 535f4932cfdSyangbo lu .read_l = esdhc_le_readl, 536f4932cfdSyangbo lu .read_w = esdhc_le_readw, 537f4932cfdSyangbo lu .read_b = esdhc_le_readb, 538f4932cfdSyangbo lu .write_l = esdhc_le_writel, 539f4932cfdSyangbo lu .write_w = esdhc_le_writew, 540f4932cfdSyangbo lu .write_b = esdhc_le_writeb, 541f4932cfdSyangbo lu .set_clock = esdhc_of_set_clock, 542f4932cfdSyangbo lu .enable_dma = esdhc_of_enable_dma, 543f4932cfdSyangbo lu .get_max_clock = esdhc_of_get_max_clock, 544f4932cfdSyangbo lu .get_min_clock = esdhc_of_get_min_clock, 545f4932cfdSyangbo lu .adma_workaround = esdhc_of_adma_workaround, 546f4932cfdSyangbo lu .set_bus_width = esdhc_pltfm_set_bus_width, 547f4932cfdSyangbo lu .reset = esdhc_reset, 548f4932cfdSyangbo lu .set_uhs_signaling = sdhci_set_uhs_signaling, 549f4932cfdSyangbo lu }; 550f4932cfdSyangbo lu 551f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_be_pdata = { 552e307148fSShawn Guo .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION 553137ccd46SJerry Huang | SDHCI_QUIRK_NO_CARD_NO_RESET 554137ccd46SJerry Huang | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 555f4932cfdSyangbo lu .ops = &sdhci_esdhc_be_ops, 5567657c3a7SAlbert Herranz }; 55738576af1SShawn Guo 558f4932cfdSyangbo lu static const struct sdhci_pltfm_data sdhci_esdhc_le_pdata = { 559f4932cfdSyangbo lu .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION 560f4932cfdSyangbo lu | SDHCI_QUIRK_NO_CARD_NO_RESET 561f4932cfdSyangbo lu | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 562f4932cfdSyangbo lu .ops = &sdhci_esdhc_le_ops, 563f4932cfdSyangbo lu }; 564f4932cfdSyangbo lu 565f4932cfdSyangbo lu static void esdhc_init(struct platform_device *pdev, struct sdhci_host *host) 566f4932cfdSyangbo lu { 567f4932cfdSyangbo lu struct sdhci_pltfm_host *pltfm_host; 568f4932cfdSyangbo lu struct sdhci_esdhc *esdhc; 569f4932cfdSyangbo lu u16 host_ver; 570f4932cfdSyangbo lu 571f4932cfdSyangbo lu pltfm_host = sdhci_priv(host); 572*8605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 573f4932cfdSyangbo lu 574f4932cfdSyangbo lu host_ver = sdhci_readw(host, SDHCI_HOST_VERSION); 575f4932cfdSyangbo lu esdhc->vendor_ver = (host_ver & SDHCI_VENDOR_VER_MASK) >> 576f4932cfdSyangbo lu SDHCI_VENDOR_VER_SHIFT; 577f4932cfdSyangbo lu esdhc->spec_ver = host_ver & SDHCI_SPEC_VER_MASK; 578f4932cfdSyangbo lu } 579f4932cfdSyangbo lu 580c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 58138576af1SShawn Guo { 58266b50a00SOded Gabbay struct sdhci_host *host; 583dcaff04dSOded Gabbay struct device_node *np; 5841ef5e49eSyangbo lu struct sdhci_pltfm_host *pltfm_host; 5851ef5e49eSyangbo lu struct sdhci_esdhc *esdhc; 58666b50a00SOded Gabbay int ret; 58766b50a00SOded Gabbay 588f4932cfdSyangbo lu np = pdev->dev.of_node; 589f4932cfdSyangbo lu 590f4932cfdSyangbo lu if (of_get_property(np, "little-endian", NULL)) 591*8605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_le_pdata, 592*8605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 593f4932cfdSyangbo lu else 594*8605e7aeSJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_esdhc_be_pdata, 595*8605e7aeSJisheng Zhang sizeof(struct sdhci_esdhc)); 596f4932cfdSyangbo lu 59766b50a00SOded Gabbay if (IS_ERR(host)) 59866b50a00SOded Gabbay return PTR_ERR(host); 59966b50a00SOded Gabbay 600f4932cfdSyangbo lu esdhc_init(pdev, host); 601f4932cfdSyangbo lu 60266b50a00SOded Gabbay sdhci_get_of_property(pdev); 60366b50a00SOded Gabbay 6041ef5e49eSyangbo lu pltfm_host = sdhci_priv(host); 605*8605e7aeSJisheng Zhang esdhc = sdhci_pltfm_priv(pltfm_host); 6061ef5e49eSyangbo lu if (esdhc->vendor_ver == VENDOR_V_22) 6071ef5e49eSyangbo lu host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 6081ef5e49eSyangbo lu 6091ef5e49eSyangbo lu if (esdhc->vendor_ver > VENDOR_V_22) 6101ef5e49eSyangbo lu host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 6111ef5e49eSyangbo lu 61274fd5e30SYangbo Lu if (of_device_is_compatible(np, "fsl,p5040-esdhc") || 61374fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p5020-esdhc") || 61474fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p4080-esdhc") || 61574fd5e30SYangbo Lu of_device_is_compatible(np, "fsl,p1020-esdhc") || 616aaa58d0eSYangbo Lu of_device_is_compatible(np, "fsl,t1040-esdhc") || 617aaa58d0eSYangbo Lu of_device_is_compatible(np, "fsl,ls1021a-esdhc")) 61874fd5e30SYangbo Lu host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 61974fd5e30SYangbo Lu 620a22950c8Syangbo lu if (of_device_is_compatible(np, "fsl,ls1021a-esdhc")) 621a22950c8Syangbo lu host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 622a22950c8Syangbo lu 623dcaff04dSOded Gabbay if (of_device_is_compatible(np, "fsl,p2020-esdhc")) { 624dcaff04dSOded Gabbay /* 625dcaff04dSOded Gabbay * Freescale messed up with P2020 as it has a non-standard 626dcaff04dSOded Gabbay * host control register 627dcaff04dSOded Gabbay */ 628dcaff04dSOded Gabbay host->quirks2 |= SDHCI_QUIRK2_BROKEN_HOST_CONTROL; 629dcaff04dSOded Gabbay } 630dcaff04dSOded Gabbay 63166b50a00SOded Gabbay /* call to generic mmc_of_parse to support additional capabilities */ 632f0991408SUlf Hansson ret = mmc_of_parse(host->mmc); 633f0991408SUlf Hansson if (ret) 634f0991408SUlf Hansson goto err; 635f0991408SUlf Hansson 636490104acSHaijun Zhang mmc_of_parse_voltage(np, &host->ocr_mask); 63766b50a00SOded Gabbay 63866b50a00SOded Gabbay ret = sdhci_add_host(host); 63966b50a00SOded Gabbay if (ret) 640f0991408SUlf Hansson goto err; 64166b50a00SOded Gabbay 642f0991408SUlf Hansson return 0; 643f0991408SUlf Hansson err: 644f0991408SUlf Hansson sdhci_pltfm_free(pdev); 64566b50a00SOded Gabbay return ret; 64638576af1SShawn Guo } 64738576af1SShawn Guo 64838576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = { 64938576af1SShawn Guo { .compatible = "fsl,mpc8379-esdhc" }, 65038576af1SShawn Guo { .compatible = "fsl,mpc8536-esdhc" }, 65138576af1SShawn Guo { .compatible = "fsl,esdhc" }, 65238576af1SShawn Guo { } 65338576af1SShawn Guo }; 65438576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); 65538576af1SShawn Guo 65638576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 65738576af1SShawn Guo .driver = { 65838576af1SShawn Guo .name = "sdhci-esdhc", 65938576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 660723f7924SRussell King .pm = ESDHC_PMOPS, 66138576af1SShawn Guo }, 66238576af1SShawn Guo .probe = sdhci_esdhc_probe, 663caebcae9SKevin Hao .remove = sdhci_pltfm_unregister, 66438576af1SShawn Guo }; 66538576af1SShawn Guo 666d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 66738576af1SShawn Guo 66838576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 66938576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 67038576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 67138576af1SShawn Guo MODULE_LICENSE("GPL v2"); 672