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 167657c3a7SAlbert Herranz #include <linux/io.h> 17f060bc9cSJerry Huang #include <linux/of.h> 187657c3a7SAlbert Herranz #include <linux/delay.h> 1988b47679SPaul Gortmaker #include <linux/module.h> 207657c3a7SAlbert Herranz #include <linux/mmc/host.h> 2138576af1SShawn Guo #include "sdhci-pltfm.h" 2280872e21SWolfram Sang #include "sdhci-esdhc.h" 237657c3a7SAlbert Herranz 24137ccd46SJerry Huang #define VENDOR_V_22 0x12 25a4071fbbSHaijun Zhang #define VENDOR_V_23 0x13 26137ccd46SJerry Huang static u32 esdhc_readl(struct sdhci_host *host, int reg) 27137ccd46SJerry Huang { 28137ccd46SJerry Huang u32 ret; 29137ccd46SJerry Huang 30137ccd46SJerry Huang ret = in_be32(host->ioaddr + reg); 31137ccd46SJerry Huang /* 32137ccd46SJerry Huang * The bit of ADMA flag in eSDHC is not compatible with standard 33137ccd46SJerry Huang * SDHC register, so set fake flag SDHCI_CAN_DO_ADMA2 when ADMA is 34137ccd46SJerry Huang * supported by eSDHC. 35137ccd46SJerry Huang * And for many FSL eSDHC controller, the reset value of field 36137ccd46SJerry Huang * SDHCI_CAN_DO_ADMA1 is one, but some of them can't support ADMA, 37137ccd46SJerry Huang * only these vendor version is greater than 2.2/0x12 support ADMA. 38137ccd46SJerry Huang * For FSL eSDHC, must aligned 4-byte, so use 0xFC to read the 39137ccd46SJerry Huang * the verdor version number, oxFE is SDHCI_HOST_VERSION. 40137ccd46SJerry Huang */ 41137ccd46SJerry Huang if ((reg == SDHCI_CAPABILITIES) && (ret & SDHCI_CAN_DO_ADMA1)) { 42137ccd46SJerry Huang u32 tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); 43137ccd46SJerry Huang tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; 44137ccd46SJerry Huang if (tmp > VENDOR_V_22) 45137ccd46SJerry Huang ret |= SDHCI_CAN_DO_ADMA2; 46137ccd46SJerry Huang } 47137ccd46SJerry Huang 48137ccd46SJerry Huang return ret; 49137ccd46SJerry Huang } 50137ccd46SJerry Huang 517657c3a7SAlbert Herranz static u16 esdhc_readw(struct sdhci_host *host, int reg) 527657c3a7SAlbert Herranz { 537657c3a7SAlbert Herranz u16 ret; 54e51cbc9eSXu lei int base = reg & ~0x3; 55e51cbc9eSXu lei int shift = (reg & 0x2) * 8; 567657c3a7SAlbert Herranz 577657c3a7SAlbert Herranz if (unlikely(reg == SDHCI_HOST_VERSION)) 58e51cbc9eSXu lei ret = in_be32(host->ioaddr + base) & 0xffff; 597657c3a7SAlbert Herranz else 60e51cbc9eSXu lei ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff; 61e51cbc9eSXu lei return ret; 62e51cbc9eSXu lei } 63e51cbc9eSXu lei 64e51cbc9eSXu lei static u8 esdhc_readb(struct sdhci_host *host, int reg) 65e51cbc9eSXu lei { 66e51cbc9eSXu lei int base = reg & ~0x3; 67e51cbc9eSXu lei int shift = (reg & 0x3) * 8; 68e51cbc9eSXu lei u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff; 69ba8c4dc9SRoy Zang 70ba8c4dc9SRoy Zang /* 71ba8c4dc9SRoy Zang * "DMA select" locates at offset 0x28 in SD specification, but on 72ba8c4dc9SRoy Zang * P5020 or P3041, it locates at 0x29. 73ba8c4dc9SRoy Zang */ 74ba8c4dc9SRoy Zang if (reg == SDHCI_HOST_CONTROL) { 75ba8c4dc9SRoy Zang u32 dma_bits; 76ba8c4dc9SRoy Zang 77ba8c4dc9SRoy Zang dma_bits = in_be32(host->ioaddr + reg); 78ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 79ba8c4dc9SRoy Zang dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK; 80ba8c4dc9SRoy Zang 81ba8c4dc9SRoy Zang /* fixup the result */ 82ba8c4dc9SRoy Zang ret &= ~SDHCI_CTRL_DMA_MASK; 83ba8c4dc9SRoy Zang ret |= dma_bits; 84ba8c4dc9SRoy Zang } 85ba8c4dc9SRoy Zang 867657c3a7SAlbert Herranz return ret; 877657c3a7SAlbert Herranz } 887657c3a7SAlbert Herranz 89a4071fbbSHaijun Zhang static void esdhc_writel(struct sdhci_host *host, u32 val, int reg) 90a4071fbbSHaijun Zhang { 91a4071fbbSHaijun Zhang /* 92a4071fbbSHaijun Zhang * Enable IRQSTATEN[BGESEN] is just to set IRQSTAT[BGE] 93a4071fbbSHaijun Zhang * when SYSCTL[RSTD]) is set for some special operations. 94a4071fbbSHaijun Zhang * No any impact other operation. 95a4071fbbSHaijun Zhang */ 96a4071fbbSHaijun Zhang if (reg == SDHCI_INT_ENABLE) 97a4071fbbSHaijun Zhang val |= SDHCI_INT_BLK_GAP; 98a4071fbbSHaijun Zhang sdhci_be32bs_writel(host, val, reg); 99a4071fbbSHaijun Zhang } 100a4071fbbSHaijun Zhang 1017657c3a7SAlbert Herranz static void esdhc_writew(struct sdhci_host *host, u16 val, int reg) 1027657c3a7SAlbert Herranz { 1037657c3a7SAlbert Herranz if (reg == SDHCI_BLOCK_SIZE) { 1047657c3a7SAlbert Herranz /* 1057657c3a7SAlbert Herranz * Two last DMA bits are reserved, and first one is used for 1067657c3a7SAlbert Herranz * non-standard blksz of 4096 bytes that we don't support 1077657c3a7SAlbert Herranz * yet. So clear the DMA boundary bits. 1087657c3a7SAlbert Herranz */ 1097657c3a7SAlbert Herranz val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); 1107657c3a7SAlbert Herranz } 1117657c3a7SAlbert Herranz sdhci_be32bs_writew(host, val, reg); 1127657c3a7SAlbert Herranz } 1137657c3a7SAlbert Herranz 1147657c3a7SAlbert Herranz static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg) 1157657c3a7SAlbert Herranz { 116ba8c4dc9SRoy Zang /* 117ba8c4dc9SRoy Zang * "DMA select" location is offset 0x28 in SD specification, but on 118ba8c4dc9SRoy Zang * P5020 or P3041, it's located at 0x29. 119ba8c4dc9SRoy Zang */ 120ba8c4dc9SRoy Zang if (reg == SDHCI_HOST_CONTROL) { 121ba8c4dc9SRoy Zang u32 dma_bits; 122ba8c4dc9SRoy Zang 123ba8c4dc9SRoy Zang /* DMA select is 22,23 bits in Protocol Control Register */ 124ba8c4dc9SRoy Zang dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5; 125ba8c4dc9SRoy Zang clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5, 126ba8c4dc9SRoy Zang dma_bits); 127ba8c4dc9SRoy Zang val &= ~SDHCI_CTRL_DMA_MASK; 128ba8c4dc9SRoy Zang val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK; 129ba8c4dc9SRoy Zang } 130ba8c4dc9SRoy Zang 1317657c3a7SAlbert Herranz /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */ 1327657c3a7SAlbert Herranz if (reg == SDHCI_HOST_CONTROL) 1337657c3a7SAlbert Herranz val &= ~ESDHC_HOST_CONTROL_RES; 1347657c3a7SAlbert Herranz sdhci_be32bs_writeb(host, val, reg); 1357657c3a7SAlbert Herranz } 1367657c3a7SAlbert Herranz 137a4071fbbSHaijun Zhang /* 138a4071fbbSHaijun Zhang * For Abort or Suspend after Stop at Block Gap, ignore the ADMA 139a4071fbbSHaijun Zhang * error(IRQSTAT[ADMAE]) if both Transfer Complete(IRQSTAT[TC]) 140a4071fbbSHaijun Zhang * and Block Gap Event(IRQSTAT[BGE]) are also set. 141a4071fbbSHaijun Zhang * For Continue, apply soft reset for data(SYSCTL[RSTD]); 142a4071fbbSHaijun Zhang * and re-issue the entire read transaction from beginning. 143a4071fbbSHaijun Zhang */ 144a4071fbbSHaijun Zhang static void esdhci_of_adma_workaround(struct sdhci_host *host, u32 intmask) 145a4071fbbSHaijun Zhang { 146a4071fbbSHaijun Zhang u32 tmp; 147a4071fbbSHaijun Zhang bool applicable; 148a4071fbbSHaijun Zhang dma_addr_t dmastart; 149a4071fbbSHaijun Zhang dma_addr_t dmanow; 150a4071fbbSHaijun Zhang 151a4071fbbSHaijun Zhang tmp = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); 152a4071fbbSHaijun Zhang tmp = (tmp & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; 153a4071fbbSHaijun Zhang 154a4071fbbSHaijun Zhang applicable = (intmask & SDHCI_INT_DATA_END) && 155a4071fbbSHaijun Zhang (intmask & SDHCI_INT_BLK_GAP) && 156a4071fbbSHaijun Zhang (tmp == VENDOR_V_23); 157a4071fbbSHaijun Zhang if (!applicable) 158a4071fbbSHaijun Zhang return; 159a4071fbbSHaijun Zhang 160a4071fbbSHaijun Zhang host->data->error = 0; 161a4071fbbSHaijun Zhang dmastart = sg_dma_address(host->data->sg); 162a4071fbbSHaijun Zhang dmanow = dmastart + host->data->bytes_xfered; 163a4071fbbSHaijun Zhang /* 164a4071fbbSHaijun Zhang * Force update to the next DMA block boundary. 165a4071fbbSHaijun Zhang */ 166a4071fbbSHaijun Zhang dmanow = (dmanow & ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + 167a4071fbbSHaijun Zhang SDHCI_DEFAULT_BOUNDARY_SIZE; 168a4071fbbSHaijun Zhang host->data->bytes_xfered = dmanow - dmastart; 169a4071fbbSHaijun Zhang sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); 170a4071fbbSHaijun Zhang } 171a4071fbbSHaijun Zhang 17280872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host) 1737657c3a7SAlbert Herranz { 1747657c3a7SAlbert Herranz setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP); 1757657c3a7SAlbert Herranz return 0; 1767657c3a7SAlbert Herranz } 1777657c3a7SAlbert Herranz 17880872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host) 1797657c3a7SAlbert Herranz { 180e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1817657c3a7SAlbert Herranz 182e307148fSShawn Guo return pltfm_host->clock; 1837657c3a7SAlbert Herranz } 1847657c3a7SAlbert Herranz 18580872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host) 1867657c3a7SAlbert Herranz { 187e307148fSShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1887657c3a7SAlbert Herranz 189e307148fSShawn Guo return pltfm_host->clock / 256 / 16; 1907657c3a7SAlbert Herranz } 1917657c3a7SAlbert Herranz 192f060bc9cSJerry Huang static void esdhc_of_set_clock(struct sdhci_host *host, unsigned int clock) 193f060bc9cSJerry Huang { 194f060bc9cSJerry Huang /* Workaround to reduce the clock frequency for p1010 esdhc */ 195f060bc9cSJerry Huang if (of_find_compatible_node(NULL, NULL, "fsl,p1010-esdhc")) { 196f060bc9cSJerry Huang if (clock > 20000000) 197f060bc9cSJerry Huang clock -= 5000000; 198f060bc9cSJerry Huang if (clock > 40000000) 199f060bc9cSJerry Huang clock -= 5000000; 200f060bc9cSJerry Huang } 201f060bc9cSJerry Huang 202f060bc9cSJerry Huang /* Set the clock */ 203*8ba9580aSLucas Stach esdhc_set_clock(host, clock, host->max_clk); 204f060bc9cSJerry Huang } 205f060bc9cSJerry Huang 206192b5372SJerry Huang #ifdef CONFIG_PM 207192b5372SJerry Huang static u32 esdhc_proctl; 208192b5372SJerry Huang static void esdhc_of_suspend(struct sdhci_host *host) 209192b5372SJerry Huang { 210192b5372SJerry Huang esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL); 211192b5372SJerry Huang } 212192b5372SJerry Huang 213192b5372SJerry Huang static void esdhc_of_resume(struct sdhci_host *host) 214192b5372SJerry Huang { 215192b5372SJerry Huang esdhc_of_enable_dma(host); 216192b5372SJerry Huang sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL); 217192b5372SJerry Huang } 218192b5372SJerry Huang #endif 219192b5372SJerry Huang 22063ef5d8cSJerry Huang static void esdhc_of_platform_init(struct sdhci_host *host) 22163ef5d8cSJerry Huang { 22263ef5d8cSJerry Huang u32 vvn; 22363ef5d8cSJerry Huang 22463ef5d8cSJerry Huang vvn = in_be32(host->ioaddr + SDHCI_SLOT_INT_STATUS); 22563ef5d8cSJerry Huang vvn = (vvn & SDHCI_VENDOR_VER_MASK) >> SDHCI_VENDOR_VER_SHIFT; 22663ef5d8cSJerry Huang if (vvn == VENDOR_V_22) 22763ef5d8cSJerry Huang host->quirks2 |= SDHCI_QUIRK2_HOST_NO_CMD23; 2283cf38833SJerry Huang 2293cf38833SJerry Huang if (vvn > VENDOR_V_22) 2303cf38833SJerry Huang host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 23163ef5d8cSJerry Huang } 23263ef5d8cSJerry Huang 233c915568dSLars-Peter Clausen static const struct sdhci_ops sdhci_esdhc_ops = { 234137ccd46SJerry Huang .read_l = esdhc_readl, 235dc297c92SMatt Fleming .read_w = esdhc_readw, 236e51cbc9eSXu lei .read_b = esdhc_readb, 237a4071fbbSHaijun Zhang .write_l = esdhc_writel, 238dc297c92SMatt Fleming .write_w = esdhc_writew, 239dc297c92SMatt Fleming .write_b = esdhc_writeb, 240f060bc9cSJerry Huang .set_clock = esdhc_of_set_clock, 24180872e21SWolfram Sang .enable_dma = esdhc_of_enable_dma, 24280872e21SWolfram Sang .get_max_clock = esdhc_of_get_max_clock, 24380872e21SWolfram Sang .get_min_clock = esdhc_of_get_min_clock, 24463ef5d8cSJerry Huang .platform_init = esdhc_of_platform_init, 245192b5372SJerry Huang #ifdef CONFIG_PM 246192b5372SJerry Huang .platform_suspend = esdhc_of_suspend, 247192b5372SJerry Huang .platform_resume = esdhc_of_resume, 248192b5372SJerry Huang #endif 249a4071fbbSHaijun Zhang .adma_workaround = esdhci_of_adma_workaround, 250e307148fSShawn Guo }; 251e307148fSShawn Guo 2521db5eebfSLars-Peter Clausen static const struct sdhci_pltfm_data sdhci_esdhc_pdata = { 253137ccd46SJerry Huang /* 254137ccd46SJerry Huang * card detection could be handled via GPIO 255137ccd46SJerry Huang * eSDHC cannot support End Attribute in NOP ADMA descriptor 256137ccd46SJerry Huang */ 257e307148fSShawn Guo .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION 258137ccd46SJerry Huang | SDHCI_QUIRK_NO_CARD_NO_RESET 259137ccd46SJerry Huang | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 260e307148fSShawn Guo .ops = &sdhci_esdhc_ops, 2617657c3a7SAlbert Herranz }; 26238576af1SShawn Guo 263c3be1efdSBill Pemberton static int sdhci_esdhc_probe(struct platform_device *pdev) 26438576af1SShawn Guo { 2650e748234SChristian Daudt return sdhci_pltfm_register(pdev, &sdhci_esdhc_pdata, 0); 26638576af1SShawn Guo } 26738576af1SShawn Guo 2686e0ee714SBill Pemberton static int sdhci_esdhc_remove(struct platform_device *pdev) 26938576af1SShawn Guo { 27038576af1SShawn Guo return sdhci_pltfm_unregister(pdev); 27138576af1SShawn Guo } 27238576af1SShawn Guo 27338576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = { 27438576af1SShawn Guo { .compatible = "fsl,mpc8379-esdhc" }, 27538576af1SShawn Guo { .compatible = "fsl,mpc8536-esdhc" }, 27638576af1SShawn Guo { .compatible = "fsl,esdhc" }, 27738576af1SShawn Guo { } 27838576af1SShawn Guo }; 27938576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match); 28038576af1SShawn Guo 28138576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = { 28238576af1SShawn Guo .driver = { 28338576af1SShawn Guo .name = "sdhci-esdhc", 28438576af1SShawn Guo .owner = THIS_MODULE, 28538576af1SShawn Guo .of_match_table = sdhci_esdhc_of_match, 28629495aa0SManuel Lauss .pm = SDHCI_PLTFM_PMOPS, 28738576af1SShawn Guo }, 28838576af1SShawn Guo .probe = sdhci_esdhc_probe, 2890433c143SBill Pemberton .remove = sdhci_esdhc_remove, 29038576af1SShawn Guo }; 29138576af1SShawn Guo 292d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver); 29338576af1SShawn Guo 29438576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC"); 29538576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " 29638576af1SShawn Guo "Anton Vorontsov <avorontsov@ru.mvista.com>"); 29738576af1SShawn Guo MODULE_LICENSE("GPL v2"); 298