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 16*66b50a00SOded 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 27137ccd46SJerry Huang static u32 esdhc_readl(struct sdhci_host *host, int reg) 28137ccd46SJerry Huang { 29137ccd46SJerry Huang u32 ret; 30137ccd46SJerry Huang 31137ccd46SJerry Huang ret = in_be32(host->ioaddr + reg); 32137ccd46SJerry Huang /* 33137ccd46SJerry Huang * The bit of ADMA flag in eSDHC is not compatible with standard 34137ccd46SJerry Huang * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is 35137ccd46SJerry Huang * supported by eSDHC. 36137ccd46SJerry Huang * And for many FSL eSDHC controller, the reset value of field 37137ccd46SJerry Huang * SDHCI_CAN_DO_ADMA1 is one, but some of them can't support ADMA, 38137ccd46SJerry Huang * only these vendor version is greater than 2.2/0x12 support ADMA. 39137ccd46SJerry Huang * For FSL eSDHC, must aligned 4-byte, so use 0xFC to read the 40137ccd46SJerry Huang * the verdor version number, oxFE is SDHCI_HOST_VERSION. 41137ccd46SJerry Huang */ 42137ccd46SJerry Huang if ((reg == SDHCI_CAPABILITIES) && (ret & SDHCI_CAN_DO_ADMA1)) { 43137ccd46SJerry Huang u32 tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); 44137ccd46SJerry Huang tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; 45137ccd46SJerry Huang if (tmp > VENDOR_V_22) 46137ccd46SJerry Huang ret |= SDHCI_CAN_DO_ADMA2; 47137ccd46SJerry Huang } 48137ccd46SJerry Huang 49137ccd46SJerry Huang return ret; 50137ccd46SJerry Huang } 51137ccd46SJerry Huang 527657c3a7SAlbert Herranz static u16 esdhc_readw(struct sdhci_host *host, int reg) 537657c3a7SAlbert Herranz { 547657c3a7SAlbert Herranz u16 ret; 55e51cbc9eSXu lei int base = reg & ~0x3; 56e51cbc9eSXu lei int shift = (reg & 0x2) * 8; 577657c3a7SAlbert Herranz 587657c3a7SAlbert Herranz if (unlikely(reg == SDHCI_HOST_VERSION)) 59e51cbc9eSXu lei ret = in_be32(host->ioaddr + base) & 0xffff; 607657c3a7SAlbert Herranz else 61e51cbc9eSXu lei ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff; 62e51cbc9eSXu lei return ret; 63e51cbc9eSXu lei } 64e51cbc9eSXu lei 65e51cbc9eSXu lei static u8 esdhc_readb(struct sdhci_host *host, int reg) 66e51cbc9eSXu lei { 67e51cbc9eSXu lei int base = reg & ~0x3; 68e51cbc9eSXu lei int shift = (reg & 0x3) * 8; 69e51cbc9eSXu lei u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff; 70ba8c4dc9SRoy Zang 71ba8c4dc9SRoy Zang /* 72ba8c4dc9SRoy Zang * "DMA select" locates at offset 0x28 in SD specification, but on 73ba8c4dc9SRoy Zang * P5020 or P3041, it locates at 0x29. 74ba8c4dc9SRoy Zang */ 75ba8c4dc9SRoy Zang if (reg == SDHCI_HOST_CONTROL) { 76ba8c4dc9SRoy Zang u32 dma_bits; 77ba8c4dc9SRoy Zang 78ba8c4dc9SRoy Zang dma_bits = in_be32(host->ioaddr + reg); 79ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 80ba8c4dc9SRoy Zang dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK; 81ba8c4dc9SRoy Zang 82ba8c4dc9SRoy Zang /* fixup the result */ 83ba8c4dc9SRoy Zang ret &= ~SDHCI_CTRL_DMA_MASK; 84ba8c4dc9SRoy Zang ret |= dma_bits; 85ba8c4dc9SRoy Zang } 86ba8c4dc9SRoy Zang 877657c3a7SAlbert Herranz return ret; 887657c3a7SAlbert Herranz } 897657c3a7SAlbert Herranz 90a4071fbbSHaijun Zhang static void esdhc_writel(struct sdhci_host *host, u32 val, int reg) 91a4071fbbSHaijun Zhang { 92a4071fbbSHaijun Zhang /* 93a4071fbbSHaijun Zhang * Enable IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] 94a4071fbbSHaijun Zhang * when SYSCTL[RSTD]) is set for some special operations. 95a4071fbbSHaijun Zhang * No any impact other operation. 96a4071fbbSHaijun Zhang */ 97a4071fbbSHaijun Zhang if (reg == SDHCI_INT_ENABLE) 98a4071fbbSHaijun Zhang val |= SDHCI_INT_BLK_GAP; 99a4071fbbSHaijun Zhang sdhci_be32bs_writel(host, val, reg); 100a4071fbbSHaijun Zhang } 101a4071fbbSHaijun Zhang 1027657c3a7SAlbert Herranz static void esdhc_writew(struct sdhci_host *host, u16 val, int reg) 1037657c3a7SAlbert Herranz { 1047657c3a7SAlbert Herranz if (reg == SDHCI_BLOCK_SIZE) { 1057657c3a7SAlbert Herranz /* 1067657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 1077657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 1087657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 1097657c3a7SAlbert Herranz */ 1107657c3a7SAlbert Herranz val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); 1117657c3a7SAlbert Herranz } 1127657c3a7SAlbert Herranz sdhci_be32bs_writew(host, val, reg); 1137657c3a7SAlbert Herranz } 1147657c3a7SAlbert Herranz 1157657c3a7SAlbert Herranz static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg) 1167657c3a7SAlbert Herranz { 117ba8c4dc9SRoy Zang /* 118ba8c4dc9SRoy Zang * "DMA select" location is offset 0x28 in SD specification, but on 119ba8c4dc9SRoy Zang * P5020 or P3041, it's located at 0x29. 120ba8c4dc9SRoy Zang */ 121ba8c4dc9SRoy Zang if (reg == SDHCI_HOST_CONTROL) { 122ba8c4dc9SRoy Zang u32 dma_bits; 123ba8c4dc9SRoy Zang 124ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 125ba8c4dc9SRoy Zang dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5; 126ba8c4dc9SRoy Zang clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5, 127ba8c4dc9SRoy Zang dma_bits); 128ba8c4dc9SRoy Zang val &= ~SDHCI_CTRL_DMA_MASK; 129ba8c4dc9SRoy Zang val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK; 130ba8c4dc9SRoy Zang } 131ba8c4dc9SRoy Zang 1327657c3a7SAlbert Herranz /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */ 1337657c3a7SAlbert Herranz if (reg == SDHCI_HOST_CONTROL) 1347657c3a7SAlbert Herranz val &= ~ESDHC_HOST_CONTROL_RES; 1357657c3a7SAlbert Herranz sdhci_be32bs_writeb(host, val, reg); 1367657c3a7SAlbert Herranz } 1377657c3a7SAlbert Herranz 138a4071fbbSHaijun Zhang /* 139a4071fbbSHaijun Zhang * For Abort or Suspend after Stop at Block Gap, ignore the ADMA 140a4071fbbSHaijun Zhang * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) 141a4071fbbSHaijun Zhang * and Block Gap Event(IRQSTAT[BGE]) are also set. 142a4071fbbSHaijun Zhang * For Continue, apply soft reset for data(SYSCTL[RSTD]); 143a4071fbbSHaijun Zhang * and re-issue the entire read transaction from beginning. 144a4071fbbSHaijun Zhang */ 145a4071fbbSHaijun Zhang static void esdhci_of_adma_workaround(struct sdhci_host *host, u32 intmask) 146a4071fbbSHaijun Zhang { 147a4071fbbSHaijun Zhang u32 tmp; 148a4071fbbSHaijun Zhang bool applicable; 149a4071fbbSHaijun Zhang dma_addr_t dmastart; 150a4071fbbSHaijun Zhang dma_addr_t dmanow; 151a4071fbbSHaijun Zhang 152a4071fbbSHaijun Zhang tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); 153a4071fbbSHaijun Zhang tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; 154a4071fbbSHaijun Zhang 155a4071fbbSHaijun Zhang applicable = (intmask & SDHCI_INT_DATA_END) && 156a4071fbbSHaijun Zhang (intmask & SDHCI_INT_BLK_GAP) && 157a4071fbbSHaijun Zhang (tmp == VENDOR_V_23); 158a4071fbbSHaijun Zhang if (!applicable) 159a4071fbbSHaijun Zhang return; 160a4071fbbSHaijun Zhang 161a4071fbbSHaijun Zhang host->data->error = 0; 162a4071fbbSHaijun Zhang dmastart = sg_dma_address(host->data->sg); 163a4071fbbSHaijun Zhang dmanow = dmastart + host->data->bytes_xfered; 164a4071fbbSHaijun Zhang /* 165a4071fbbSHaijun Zhang * Force update to the next DMA block boundary. 166a4071fbbSHaijun Zhang */ 167a4071fbbSHaijun Zhang dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + 168a4071fbbSHaijun Zhang SDHCI_DEFAULT_BOUNDARY_SIZE; 169a4071fbbSHaijun Zhang host->data->bytes_xfered = dmanow - dmastart; 170a4071fbbSHaijun Zhang sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); 171a4071fbbSHaijun Zhang } 172a4071fbbSHaijun Zhang 17380872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host) 1747657c3a7SAlbert Herranz { 1757657c3a7SAlbert Herranz setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP); 1767657c3a7SAlbert Herranz return 0; 1777657c3a7SAlbert Herranz } 1787657c3a7SAlbert Herranz 17980872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 1807657c3a7SAlbert Herranz { 181e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1827657c3a7SAlbert Herranz 183e307148fSShawn Guo return pltfm_host->clock; 1847657c3a7SAlbert Herranz } 1857657c3a7SAlbert Herranz 18680872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 1877657c3a7SAlbert Herranz { 188e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1897657c3a7SAlbert Herranz 190e307148fSShawn Guo return pltfm_host->clock / 256 / 16; 1917657c3a7SAlbert Herranz } 1927657c3a7SAlbert Herranz 193f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 194f060bc9cSJerry Huang { 195f060bc9cSJerry Huang /* Workaround to reduce the clock frequency for p1010 esdhc */ 196f060bc9cSJerry Huang if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) { 197f060bc9cSJerry Huang if (clock > 20000000) 198f060bc9cSJerry Huang clock -= 5000000; 199f060bc9cSJerry Huang if (clock > 40000000) 200f060bc9cSJerry Huang clock -= 5000000; 201f060bc9cSJerry Huang } 202f060bc9cSJerry Huang 203f060bc9cSJerry Huang /* Set the clock */ 2048ba9580aSLucas Stach esdhc_set_clock(host, clock, host->max_clk); 205f060bc9cSJerry Huang } 206f060bc9cSJerry Huang 207192b5372SJerry Huang #ifdef CONFIG_PM 208192b5372SJerry Huang static u32 esdhc_proctl; 209192b5372SJerry Huang static void esdhc_of_suspend(struct sdhci_host *host) 210192b5372SJerry Huang { 211192b5372SJerry Huang esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL); 212192b5372SJerry Huang } 213192b5372SJerry Huang 214192b5372SJerry Huang static void esdhc_of_resume(struct sdhci_host *host) 215192b5372SJerry Huang { 216192b5372SJerry Huang esdhc_of_enable_dma(host); 217192b5372SJerry Huang sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 218192b5372SJerry Huang } 219192b5372SJerry Huang #endif 220192b5372SJerry Huang 22163ef5d8cSJerry Huang static void esdhc_of_platform_init(struct sdhci_host *host) 22263ef5d8cSJerry Huang { 22363ef5d8cSJerry Huang u32 vvn; 22463ef5d8cSJerry Huang 22563ef5d8cSJerry Huang vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); 22663ef5d8cSJerry Huang vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; 22763ef5d8cSJerry Huang if (vvn == VENDOR_V_22) 22863ef5d8cSJerry Huang host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 2293cf38833SJerry Huang 2303cf38833SJerry Huang if (vvn > VENDOR_V_22) 2313cf38833SJerry Huang host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 23263ef5d8cSJerry Huang } 23363ef5d8cSJerry Huang 234*66b50a00SOded Gabbay static int esdhc_pltfm_bus_width(struct sdhci_host *host, int width) 235*66b50a00SOded Gabbay { 236*66b50a00SOded Gabbay u32 ctrl; 237*66b50a00SOded Gabbay 238*66b50a00SOded Gabbay switch (width) { 239*66b50a00SOded Gabbay case MMC_BUS_WIDTH_8: 240*66b50a00SOded Gabbay ctrl = ESDHC_CTRL_8BITBUS; 241*66b50a00SOded Gabbay break; 242*66b50a00SOded Gabbay 243*66b50a00SOded Gabbay case MMC_BUS_WIDTH_4: 244*66b50a00SOded Gabbay ctrl = ESDHC_CTRL_4BITBUS; 245*66b50a00SOded Gabbay break; 246*66b50a00SOded Gabbay 247*66b50a00SOded Gabbay default: 248*66b50a00SOded Gabbay ctrl = 0; 249*66b50a00SOded Gabbay break; 250*66b50a00SOded Gabbay } 251*66b50a00SOded Gabbay 252*66b50a00SOded Gabbay clrsetbits_be32(host->ioaddr + SDHCI_HOST_CONTROL, 253*66b50a00SOded Gabbay ESDHC_CTRL_BUSWIDTH_MASK, ctrl); 254*66b50a00SOded Gabbay 255*66b50a00SOded Gabbay return 0; 256*66b50a00SOded Gabbay } 257*66b50a00SOded Gabbay 258c915568dSLars-Peter Clausen static const struct sdhci_ops sdhci_esdhc_ops = { 259137ccd46SJerry Huang .read_l = esdhc_readl, 260dc297c92SMatt Fleming .read_w = esdhc_readw, 261e51cbc9eSXu lei .read_b = esdhc_readb, 262a4071fbbSHaijun Zhang .write_l = esdhc_writel, 263dc297c92SMatt Fleming .write_w = esdhc_writew, 264dc297c92SMatt Fleming .write_b = esdhc_writeb, 265f060bc9cSJerry Huang .set_clock = esdhc_of_set_clock, 26680872e21SWolfram Sang .enable_dma = esdhc_of_enable_dma, 26780872e21SWolfram Sang .get_max_clock = esdhc_of_get_max_clock, 26880872e21SWolfram Sang .get_min_clock = esdhc_of_get_min_clock, 26963ef5d8cSJerry Huang .platform_init = esdhc_of_platform_init, 270192b5372SJerry Huang #ifdef CONFIG_PM 271192b5372SJerry Huang .platform_suspend = esdhc_of_suspend, 272192b5372SJerry Huang .platform_resume = esdhc_of_resume, 273192b5372SJerry Huang #endif 274a4071fbbSHaijun Zhang .adma_workaround = esdhci_of_adma_workaround, 275*66b50a00SOded Gabbay .platform_bus_width = esdhc_pltfm_bus_width, 276e307148fSShawn Guo }; 277e307148fSShawn Guo 2781db5eebfSLars-Peter Clausen static const struct sdhci_pltfm_data sdhci_esdhc_pdata = { 279137ccd46SJerry Huang /* 280137ccd46SJerry Huang * card detection could be handled via GPIO 281137ccd46SJerry Huang * eSDHC cannot support End Attribute in NOP ADMA descriptor 282137ccd46SJerry Huang */ 283e307148fSShawn Guo .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION 284137ccd46SJerry Huang | SDHCI_QUIRK_NO_CARD_NO_RESET 285137ccd46SJerry Huang | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 286e307148fSShawn Guo .ops = &sdhci_esdhc_ops, 2877657c3a7SAlbert Herranz }; 28838576af1SShawn Guo 289c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 29038576af1SShawn Guo { 291*66b50a00SOded Gabbay struct sdhci_host *host; 292*66b50a00SOded Gabbay int ret; 293*66b50a00SOded Gabbay 294*66b50a00SOded Gabbay host = sdhci_pltfm_init(pdev, &sdhci_esdhc_pdata, 0); 295*66b50a00SOded Gabbay if (IS_ERR(host)) 296*66b50a00SOded Gabbay return PTR_ERR(host); 297*66b50a00SOded Gabbay 298*66b50a00SOded Gabbay sdhci_get_of_property(pdev); 299*66b50a00SOded Gabbay 300*66b50a00SOded Gabbay /* call to generic mmc_of_parse to support additional capabilities */ 301*66b50a00SOded Gabbay mmc_of_parse(host->mmc); 302*66b50a00SOded Gabbay 303*66b50a00SOded Gabbay ret = sdhci_add_host(host); 304*66b50a00SOded Gabbay if (ret) 305*66b50a00SOded Gabbay sdhci_pltfm_free(pdev); 306*66b50a00SOded Gabbay 307*66b50a00SOded Gabbay return ret; 30838576af1SShawn Guo } 30938576af1SShawn Guo 3106e0ee714SBill Pemberton static int sdhci_esdhc_remove(struct platform_device *pdev) 31138576af1SShawn Guo { 31238576af1SShawn Guo return sdhci_pltfm_unregister(pdev); 31338576af1SShawn Guo } 31438576af1SShawn Guo 31538576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = { 31638576af1SShawn Guo { .compatible = "fsl,mpc8379-esdhc" }, 31738576af1SShawn Guo { .compatible = "fsl,mpc8536-esdhc" }, 31838576af1SShawn Guo { .compatible = "fsl,esdhc" }, 31938576af1SShawn Guo { } 32038576af1SShawn Guo }; 32138576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); 32238576af1SShawn Guo 32338576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 32438576af1SShawn Guo .driver = { 32538576af1SShawn Guo .name = "sdhci-esdhc", 32638576af1SShawn Guo .owner = THIS_MODULE, 32738576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 32829495aa0SManuel Lauss .pm = SDHCI_PLTFM_PMOPS, 32938576af1SShawn Guo }, 33038576af1SShawn Guo .probe = sdhci_esdhc_probe, 3310433c143SBill Pemberton .remove = sdhci_esdhc_remove, 33238576af1SShawn Guo }; 33338576af1SShawn Guo 334d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 33538576af1SShawn Guo 33638576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 33738576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 33838576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 33938576af1SShawn Guo MODULE_LICENSE("GPL v2"); 340