195f25efeSWolfram Sang /* 295f25efeSWolfram Sang * Freescale eSDHC i.MX controller driver for the platform bus. 395f25efeSWolfram Sang * 495f25efeSWolfram Sang * derived from the OF-version. 595f25efeSWolfram Sang * 695f25efeSWolfram Sang * Copyright (c) 2010 Pengutronix e.K. 795f25efeSWolfram Sang * Author: Wolfram Sang <w.sang@pengutronix.de> 895f25efeSWolfram Sang * 995f25efeSWolfram Sang * This program is free software; you can redistribute it and/or modify 1095f25efeSWolfram Sang * it under the terms of the GNU General Public License as published by 1195f25efeSWolfram Sang * the Free Software Foundation; either version 2 of the License. 1295f25efeSWolfram Sang */ 1395f25efeSWolfram Sang 1495f25efeSWolfram Sang #include <linux/io.h> 1595f25efeSWolfram Sang #include <linux/delay.h> 1695f25efeSWolfram Sang #include <linux/err.h> 1795f25efeSWolfram Sang #include <linux/clk.h> 180c6d49ceSWolfram Sang #include <linux/gpio.h> 1966506f76SShawn Guo #include <linux/module.h> 20e149860dSRichard Zhu #include <linux/slab.h> 2195f25efeSWolfram Sang #include <linux/mmc/host.h> 2258ac8177SRichard Zhu #include <linux/mmc/mmc.h> 2358ac8177SRichard Zhu #include <linux/mmc/sdio.h> 24abfafc2dSShawn Guo #include <linux/of.h> 25abfafc2dSShawn Guo #include <linux/of_device.h> 26abfafc2dSShawn Guo #include <linux/of_gpio.h> 270c6d49ceSWolfram Sang #include <mach/esdhc.h> 2895f25efeSWolfram Sang #include "sdhci-pltfm.h" 2995f25efeSWolfram Sang #include "sdhci-esdhc.h" 3095f25efeSWolfram Sang 310d58864bSTony Lin #define SDHCI_CTRL_D3CD 0x08 3258ac8177SRichard Zhu /* VENDOR SPEC register */ 3358ac8177SRichard Zhu #define SDHCI_VENDOR_SPEC 0xC0 3458ac8177SRichard Zhu #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002 35*95a2482aSShawn Guo #define SDHCI_MIX_CTRL 0x48 3658ac8177SRichard Zhu 3758ac8177SRichard Zhu /* 3897e4ba6aSRichard Zhu * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC: 3997e4ba6aSRichard Zhu * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design, 4097e4ba6aSRichard Zhu * but bit28 is used as the INT DMA ERR in fsl eSDHC design. 4197e4ba6aSRichard Zhu * Define this macro DMA error INT for fsl eSDHC 4297e4ba6aSRichard Zhu */ 4397e4ba6aSRichard Zhu #define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000 4497e4ba6aSRichard Zhu 4597e4ba6aSRichard Zhu /* 4658ac8177SRichard Zhu * The CMDTYPE of the CMD register (offset 0xE) should be set to 4758ac8177SRichard Zhu * "11" when the STOP CMD12 is issued on imx53 to abort one 4858ac8177SRichard Zhu * open ended multi-blk IO. Otherwise the TC INT wouldn't 4958ac8177SRichard Zhu * be generated. 5058ac8177SRichard Zhu * In exact block transfer, the controller doesn't complete the 5158ac8177SRichard Zhu * operations automatically as required at the end of the 5258ac8177SRichard Zhu * transfer and remains on hold if the abort command is not sent. 5358ac8177SRichard Zhu * As a result, the TC flag is not asserted and SW received timeout 5458ac8177SRichard Zhu * exeception. Bit1 of Vendor Spec registor is used to fix it. 5558ac8177SRichard Zhu */ 5658ac8177SRichard Zhu #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1) 57e149860dSRichard Zhu 5857ed3314SShawn Guo enum imx_esdhc_type { 5957ed3314SShawn Guo IMX25_ESDHC, 6057ed3314SShawn Guo IMX35_ESDHC, 6157ed3314SShawn Guo IMX51_ESDHC, 6257ed3314SShawn Guo IMX53_ESDHC, 63*95a2482aSShawn Guo IMX6Q_USDHC, 6457ed3314SShawn Guo }; 6557ed3314SShawn Guo 66e149860dSRichard Zhu struct pltfm_imx_data { 67e149860dSRichard Zhu int flags; 68e149860dSRichard Zhu u32 scratchpad; 6957ed3314SShawn Guo enum imx_esdhc_type devtype; 70842afc02SShawn Guo struct esdhc_platform_data boarddata; 71e149860dSRichard Zhu }; 72e149860dSRichard Zhu 7357ed3314SShawn Guo static struct platform_device_id imx_esdhc_devtype[] = { 7457ed3314SShawn Guo { 7557ed3314SShawn Guo .name = "sdhci-esdhc-imx25", 7657ed3314SShawn Guo .driver_data = IMX25_ESDHC, 7757ed3314SShawn Guo }, { 7857ed3314SShawn Guo .name = "sdhci-esdhc-imx35", 7957ed3314SShawn Guo .driver_data = IMX35_ESDHC, 8057ed3314SShawn Guo }, { 8157ed3314SShawn Guo .name = "sdhci-esdhc-imx51", 8257ed3314SShawn Guo .driver_data = IMX51_ESDHC, 8357ed3314SShawn Guo }, { 8457ed3314SShawn Guo .name = "sdhci-esdhc-imx53", 8557ed3314SShawn Guo .driver_data = IMX53_ESDHC, 8657ed3314SShawn Guo }, { 87*95a2482aSShawn Guo .name = "sdhci-usdhc-imx6q", 88*95a2482aSShawn Guo .driver_data = IMX6Q_USDHC, 89*95a2482aSShawn Guo }, { 9057ed3314SShawn Guo /* sentinel */ 9157ed3314SShawn Guo } 9257ed3314SShawn Guo }; 9357ed3314SShawn Guo MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype); 9457ed3314SShawn Guo 95abfafc2dSShawn Guo static const struct of_device_id imx_esdhc_dt_ids[] = { 96abfafc2dSShawn Guo { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], }, 97abfafc2dSShawn Guo { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], }, 98abfafc2dSShawn Guo { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], }, 99abfafc2dSShawn Guo { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], }, 100*95a2482aSShawn Guo { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], }, 101abfafc2dSShawn Guo { /* sentinel */ } 102abfafc2dSShawn Guo }; 103abfafc2dSShawn Guo MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids); 104abfafc2dSShawn Guo 10557ed3314SShawn Guo static inline int is_imx25_esdhc(struct pltfm_imx_data *data) 10657ed3314SShawn Guo { 10757ed3314SShawn Guo return data->devtype == IMX25_ESDHC; 10857ed3314SShawn Guo } 10957ed3314SShawn Guo 11057ed3314SShawn Guo static inline int is_imx35_esdhc(struct pltfm_imx_data *data) 11157ed3314SShawn Guo { 11257ed3314SShawn Guo return data->devtype == IMX35_ESDHC; 11357ed3314SShawn Guo } 11457ed3314SShawn Guo 11557ed3314SShawn Guo static inline int is_imx51_esdhc(struct pltfm_imx_data *data) 11657ed3314SShawn Guo { 11757ed3314SShawn Guo return data->devtype == IMX51_ESDHC; 11857ed3314SShawn Guo } 11957ed3314SShawn Guo 12057ed3314SShawn Guo static inline int is_imx53_esdhc(struct pltfm_imx_data *data) 12157ed3314SShawn Guo { 12257ed3314SShawn Guo return data->devtype == IMX53_ESDHC; 12357ed3314SShawn Guo } 12457ed3314SShawn Guo 125*95a2482aSShawn Guo static inline int is_imx6q_usdhc(struct pltfm_imx_data *data) 126*95a2482aSShawn Guo { 127*95a2482aSShawn Guo return data->devtype == IMX6Q_USDHC; 128*95a2482aSShawn Guo } 129*95a2482aSShawn Guo 13095f25efeSWolfram Sang static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg) 13195f25efeSWolfram Sang { 13295f25efeSWolfram Sang void __iomem *base = host->ioaddr + (reg & ~0x3); 13395f25efeSWolfram Sang u32 shift = (reg & 0x3) * 8; 13495f25efeSWolfram Sang 13595f25efeSWolfram Sang writel(((readl(base) & ~(mask << shift)) | (val << shift)), base); 13695f25efeSWolfram Sang } 13795f25efeSWolfram Sang 1387e29c306SWolfram Sang static u32 esdhc_readl_le(struct sdhci_host *host, int reg) 1397e29c306SWolfram Sang { 140842afc02SShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 141842afc02SShawn Guo struct pltfm_imx_data *imx_data = pltfm_host->priv; 142842afc02SShawn Guo struct esdhc_platform_data *boarddata = &imx_data->boarddata; 1437e29c306SWolfram Sang 144913413c3SShawn Guo /* fake CARD_PRESENT flag */ 145913413c3SShawn Guo u32 val = readl(host->ioaddr + reg); 146913413c3SShawn Guo 147913413c3SShawn Guo if (unlikely((reg == SDHCI_PRESENT_STATE) 148913413c3SShawn Guo && gpio_is_valid(boarddata->cd_gpio))) { 149913413c3SShawn Guo if (gpio_get_value(boarddata->cd_gpio)) 1507e29c306SWolfram Sang /* no card, if a valid gpio says so... */ 151803862a6SShawn Guo val &= ~SDHCI_CARD_PRESENT; 1527e29c306SWolfram Sang else 1537e29c306SWolfram Sang /* ... in all other cases assume card is present */ 1547e29c306SWolfram Sang val |= SDHCI_CARD_PRESENT; 1557e29c306SWolfram Sang } 1567e29c306SWolfram Sang 15797e4ba6aSRichard Zhu if (unlikely(reg == SDHCI_CAPABILITIES)) { 15897e4ba6aSRichard Zhu /* In FSL esdhc IC module, only bit20 is used to indicate the 15997e4ba6aSRichard Zhu * ADMA2 capability of esdhc, but this bit is messed up on 16097e4ba6aSRichard Zhu * some SOCs (e.g. on MX25, MX35 this bit is set, but they 16197e4ba6aSRichard Zhu * don't actually support ADMA2). So set the BROKEN_ADMA 16297e4ba6aSRichard Zhu * uirk on MX25/35 platforms. 16397e4ba6aSRichard Zhu */ 16497e4ba6aSRichard Zhu 16597e4ba6aSRichard Zhu if (val & SDHCI_CAN_DO_ADMA1) { 16697e4ba6aSRichard Zhu val &= ~SDHCI_CAN_DO_ADMA1; 16797e4ba6aSRichard Zhu val |= SDHCI_CAN_DO_ADMA2; 16897e4ba6aSRichard Zhu } 16997e4ba6aSRichard Zhu } 17097e4ba6aSRichard Zhu 17197e4ba6aSRichard Zhu if (unlikely(reg == SDHCI_INT_STATUS)) { 17297e4ba6aSRichard Zhu if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) { 17397e4ba6aSRichard Zhu val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR; 17497e4ba6aSRichard Zhu val |= SDHCI_INT_ADMA_ERROR; 17597e4ba6aSRichard Zhu } 17697e4ba6aSRichard Zhu } 17797e4ba6aSRichard Zhu 1787e29c306SWolfram Sang return val; 1797e29c306SWolfram Sang } 1807e29c306SWolfram Sang 1817e29c306SWolfram Sang static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg) 1827e29c306SWolfram Sang { 183e149860dSRichard Zhu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 184e149860dSRichard Zhu struct pltfm_imx_data *imx_data = pltfm_host->priv; 185842afc02SShawn Guo struct esdhc_platform_data *boarddata = &imx_data->boarddata; 1860d58864bSTony Lin u32 data; 187e149860dSRichard Zhu 1880d58864bSTony Lin if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) { 1890d58864bSTony Lin if (boarddata->cd_type == ESDHC_CD_GPIO) 1907e29c306SWolfram Sang /* 1910d58864bSTony Lin * These interrupts won't work with a custom 1920d58864bSTony Lin * card_detect gpio (only applied to mx25/35) 1937e29c306SWolfram Sang */ 1947e29c306SWolfram Sang val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT); 1957e29c306SWolfram Sang 1960d58864bSTony Lin if (val & SDHCI_INT_CARD_INT) { 1970d58864bSTony Lin /* 1980d58864bSTony Lin * Clear and then set D3CD bit to avoid missing the 1990d58864bSTony Lin * card interrupt. This is a eSDHC controller problem 2000d58864bSTony Lin * so we need to apply the following workaround: clear 2010d58864bSTony Lin * and set D3CD bit will make eSDHC re-sample the card 2020d58864bSTony Lin * interrupt. In case a card interrupt was lost, 2030d58864bSTony Lin * re-sample it by the following steps. 2040d58864bSTony Lin */ 2050d58864bSTony Lin data = readl(host->ioaddr + SDHCI_HOST_CONTROL); 2060d58864bSTony Lin data &= ~SDHCI_CTRL_D3CD; 2070d58864bSTony Lin writel(data, host->ioaddr + SDHCI_HOST_CONTROL); 2080d58864bSTony Lin data |= SDHCI_CTRL_D3CD; 2090d58864bSTony Lin writel(data, host->ioaddr + SDHCI_HOST_CONTROL); 2100d58864bSTony Lin } 2110d58864bSTony Lin } 2120d58864bSTony Lin 21358ac8177SRichard Zhu if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT) 21458ac8177SRichard Zhu && (reg == SDHCI_INT_STATUS) 21558ac8177SRichard Zhu && (val & SDHCI_INT_DATA_END))) { 21658ac8177SRichard Zhu u32 v; 21758ac8177SRichard Zhu v = readl(host->ioaddr + SDHCI_VENDOR_SPEC); 21858ac8177SRichard Zhu v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK; 21958ac8177SRichard Zhu writel(v, host->ioaddr + SDHCI_VENDOR_SPEC); 22058ac8177SRichard Zhu } 22158ac8177SRichard Zhu 22297e4ba6aSRichard Zhu if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) { 22397e4ba6aSRichard Zhu if (val & SDHCI_INT_ADMA_ERROR) { 22497e4ba6aSRichard Zhu val &= ~SDHCI_INT_ADMA_ERROR; 22597e4ba6aSRichard Zhu val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR; 22697e4ba6aSRichard Zhu } 22797e4ba6aSRichard Zhu } 22897e4ba6aSRichard Zhu 2297e29c306SWolfram Sang writel(val, host->ioaddr + reg); 2307e29c306SWolfram Sang } 2317e29c306SWolfram Sang 23295f25efeSWolfram Sang static u16 esdhc_readw_le(struct sdhci_host *host, int reg) 23395f25efeSWolfram Sang { 234*95a2482aSShawn Guo if (unlikely(reg == SDHCI_HOST_VERSION)) { 235*95a2482aSShawn Guo u16 val = readw(host->ioaddr + (reg ^ 2)); 236*95a2482aSShawn Guo /* 237*95a2482aSShawn Guo * uSDHC supports SDHCI v3.0, but it's encoded as value 238*95a2482aSShawn Guo * 0x3 in host controller version register, which violates 239*95a2482aSShawn Guo * SDHCI_SPEC_300 definition. Work it around here. 240*95a2482aSShawn Guo */ 241*95a2482aSShawn Guo if ((val & SDHCI_SPEC_VER_MASK) == 3) 242*95a2482aSShawn Guo return --val; 243*95a2482aSShawn Guo } 24495f25efeSWolfram Sang 24595f25efeSWolfram Sang return readw(host->ioaddr + reg); 24695f25efeSWolfram Sang } 24795f25efeSWolfram Sang 24895f25efeSWolfram Sang static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg) 24995f25efeSWolfram Sang { 25095f25efeSWolfram Sang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 251e149860dSRichard Zhu struct pltfm_imx_data *imx_data = pltfm_host->priv; 25295f25efeSWolfram Sang 25395f25efeSWolfram Sang switch (reg) { 25495f25efeSWolfram Sang case SDHCI_TRANSFER_MODE: 25595f25efeSWolfram Sang /* 25695f25efeSWolfram Sang * Postpone this write, we must do it together with a 25795f25efeSWolfram Sang * command write that is down below. 25895f25efeSWolfram Sang */ 25958ac8177SRichard Zhu if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT) 26058ac8177SRichard Zhu && (host->cmd->opcode == SD_IO_RW_EXTENDED) 26158ac8177SRichard Zhu && (host->cmd->data->blocks > 1) 26258ac8177SRichard Zhu && (host->cmd->data->flags & MMC_DATA_READ)) { 26358ac8177SRichard Zhu u32 v; 26458ac8177SRichard Zhu v = readl(host->ioaddr + SDHCI_VENDOR_SPEC); 26558ac8177SRichard Zhu v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK; 26658ac8177SRichard Zhu writel(v, host->ioaddr + SDHCI_VENDOR_SPEC); 26758ac8177SRichard Zhu } 268e149860dSRichard Zhu imx_data->scratchpad = val; 26995f25efeSWolfram Sang return; 27095f25efeSWolfram Sang case SDHCI_COMMAND: 27158ac8177SRichard Zhu if ((host->cmd->opcode == MMC_STOP_TRANSMISSION) 27258ac8177SRichard Zhu && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)) 27358ac8177SRichard Zhu val |= SDHCI_CMD_ABORTCMD; 274*95a2482aSShawn Guo 275*95a2482aSShawn Guo if (is_imx6q_usdhc(imx_data)) { 276*95a2482aSShawn Guo u32 m = readl(host->ioaddr + SDHCI_MIX_CTRL); 277*95a2482aSShawn Guo m = imx_data->scratchpad | (m & 0xffff0000); 278*95a2482aSShawn Guo writel(m, host->ioaddr + SDHCI_MIX_CTRL); 279*95a2482aSShawn Guo writel(val << 16, 280*95a2482aSShawn Guo host->ioaddr + SDHCI_TRANSFER_MODE); 281*95a2482aSShawn Guo } else { 282e149860dSRichard Zhu writel(val << 16 | imx_data->scratchpad, 28395f25efeSWolfram Sang host->ioaddr + SDHCI_TRANSFER_MODE); 284*95a2482aSShawn Guo } 28595f25efeSWolfram Sang return; 28695f25efeSWolfram Sang case SDHCI_BLOCK_SIZE: 28795f25efeSWolfram Sang val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); 28895f25efeSWolfram Sang break; 28995f25efeSWolfram Sang } 29095f25efeSWolfram Sang esdhc_clrset_le(host, 0xffff, val, reg); 29195f25efeSWolfram Sang } 29295f25efeSWolfram Sang 29395f25efeSWolfram Sang static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg) 29495f25efeSWolfram Sang { 29595f25efeSWolfram Sang u32 new_val; 29695f25efeSWolfram Sang 29795f25efeSWolfram Sang switch (reg) { 29895f25efeSWolfram Sang case SDHCI_POWER_CONTROL: 29995f25efeSWolfram Sang /* 30095f25efeSWolfram Sang * FSL put some DMA bits here 30195f25efeSWolfram Sang * If your board has a regulator, code should be here 30295f25efeSWolfram Sang */ 30395f25efeSWolfram Sang return; 30495f25efeSWolfram Sang case SDHCI_HOST_CONTROL: 3050d58864bSTony Lin /* FSL messed up here, so we can just keep those three */ 3060d58864bSTony Lin new_val = val & (SDHCI_CTRL_LED | \ 3070d58864bSTony Lin SDHCI_CTRL_4BITBUS | \ 3080d58864bSTony Lin SDHCI_CTRL_D3CD); 30995f25efeSWolfram Sang /* ensure the endianess */ 31095f25efeSWolfram Sang new_val |= ESDHC_HOST_CONTROL_LE; 31195f25efeSWolfram Sang /* DMA mode bits are shifted */ 31295f25efeSWolfram Sang new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5; 31395f25efeSWolfram Sang 31495f25efeSWolfram Sang esdhc_clrset_le(host, 0xffff, new_val, reg); 31595f25efeSWolfram Sang return; 31695f25efeSWolfram Sang } 31795f25efeSWolfram Sang esdhc_clrset_le(host, 0xff, val, reg); 318913413c3SShawn Guo 319913413c3SShawn Guo /* 320913413c3SShawn Guo * The esdhc has a design violation to SDHC spec which tells 321913413c3SShawn Guo * that software reset should not affect card detection circuit. 322913413c3SShawn Guo * But esdhc clears its SYSCTL register bits [0..2] during the 323913413c3SShawn Guo * software reset. This will stop those clocks that card detection 324913413c3SShawn Guo * circuit relies on. To work around it, we turn the clocks on back 325913413c3SShawn Guo * to keep card detection circuit functional. 326913413c3SShawn Guo */ 327913413c3SShawn Guo if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) 328913413c3SShawn Guo esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL); 32995f25efeSWolfram Sang } 33095f25efeSWolfram Sang 33195f25efeSWolfram Sang static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host) 33295f25efeSWolfram Sang { 33395f25efeSWolfram Sang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 33495f25efeSWolfram Sang 33595f25efeSWolfram Sang return clk_get_rate(pltfm_host->clk); 33695f25efeSWolfram Sang } 33795f25efeSWolfram Sang 33895f25efeSWolfram Sang static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host) 33995f25efeSWolfram Sang { 34095f25efeSWolfram Sang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 34195f25efeSWolfram Sang 34295f25efeSWolfram Sang return clk_get_rate(pltfm_host->clk) / 256 / 16; 34395f25efeSWolfram Sang } 34495f25efeSWolfram Sang 345913413c3SShawn Guo static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host) 346913413c3SShawn Guo { 347842afc02SShawn Guo struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 348842afc02SShawn Guo struct pltfm_imx_data *imx_data = pltfm_host->priv; 349842afc02SShawn Guo struct esdhc_platform_data *boarddata = &imx_data->boarddata; 350913413c3SShawn Guo 351913413c3SShawn Guo switch (boarddata->wp_type) { 352913413c3SShawn Guo case ESDHC_WP_GPIO: 353913413c3SShawn Guo if (gpio_is_valid(boarddata->wp_gpio)) 354913413c3SShawn Guo return gpio_get_value(boarddata->wp_gpio); 355913413c3SShawn Guo case ESDHC_WP_CONTROLLER: 356913413c3SShawn Guo return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) & 357913413c3SShawn Guo SDHCI_WRITE_PROTECT); 358913413c3SShawn Guo case ESDHC_WP_NONE: 359913413c3SShawn Guo break; 360913413c3SShawn Guo } 361913413c3SShawn Guo 362913413c3SShawn Guo return -ENOSYS; 363913413c3SShawn Guo } 364913413c3SShawn Guo 3650c6d49ceSWolfram Sang static struct sdhci_ops sdhci_esdhc_ops = { 366e149860dSRichard Zhu .read_l = esdhc_readl_le, 3670c6d49ceSWolfram Sang .read_w = esdhc_readw_le, 368e149860dSRichard Zhu .write_l = esdhc_writel_le, 3690c6d49ceSWolfram Sang .write_w = esdhc_writew_le, 3700c6d49ceSWolfram Sang .write_b = esdhc_writeb_le, 3710c6d49ceSWolfram Sang .set_clock = esdhc_set_clock, 3720c6d49ceSWolfram Sang .get_max_clock = esdhc_pltfm_get_max_clock, 3730c6d49ceSWolfram Sang .get_min_clock = esdhc_pltfm_get_min_clock, 374913413c3SShawn Guo .get_ro = esdhc_pltfm_get_ro, 3750c6d49ceSWolfram Sang }; 3760c6d49ceSWolfram Sang 37785d6509dSShawn Guo static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = { 37897e4ba6aSRichard Zhu .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT 37997e4ba6aSRichard Zhu | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC 38097e4ba6aSRichard Zhu | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC 38185d6509dSShawn Guo | SDHCI_QUIRK_BROKEN_CARD_DETECTION, 38285d6509dSShawn Guo .ops = &sdhci_esdhc_ops, 38385d6509dSShawn Guo }; 38485d6509dSShawn Guo 3857e29c306SWolfram Sang static irqreturn_t cd_irq(int irq, void *data) 3867e29c306SWolfram Sang { 3877e29c306SWolfram Sang struct sdhci_host *sdhost = (struct sdhci_host *)data; 3887e29c306SWolfram Sang 3897e29c306SWolfram Sang tasklet_schedule(&sdhost->card_tasklet); 3907e29c306SWolfram Sang return IRQ_HANDLED; 3917e29c306SWolfram Sang }; 3927e29c306SWolfram Sang 393abfafc2dSShawn Guo #ifdef CONFIG_OF 394abfafc2dSShawn Guo static int __devinit 395abfafc2dSShawn Guo sdhci_esdhc_imx_probe_dt(struct platform_device *pdev, 396abfafc2dSShawn Guo struct esdhc_platform_data *boarddata) 397abfafc2dSShawn Guo { 398abfafc2dSShawn Guo struct device_node *np = pdev->dev.of_node; 399abfafc2dSShawn Guo 400abfafc2dSShawn Guo if (!np) 401abfafc2dSShawn Guo return -ENODEV; 402abfafc2dSShawn Guo 403abfafc2dSShawn Guo if (of_get_property(np, "fsl,card-wired", NULL)) 404abfafc2dSShawn Guo boarddata->cd_type = ESDHC_CD_PERMANENT; 405abfafc2dSShawn Guo 406abfafc2dSShawn Guo if (of_get_property(np, "fsl,cd-controller", NULL)) 407abfafc2dSShawn Guo boarddata->cd_type = ESDHC_CD_CONTROLLER; 408abfafc2dSShawn Guo 409abfafc2dSShawn Guo if (of_get_property(np, "fsl,wp-controller", NULL)) 410abfafc2dSShawn Guo boarddata->wp_type = ESDHC_WP_CONTROLLER; 411abfafc2dSShawn Guo 412abfafc2dSShawn Guo boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0); 413abfafc2dSShawn Guo if (gpio_is_valid(boarddata->cd_gpio)) 414abfafc2dSShawn Guo boarddata->cd_type = ESDHC_CD_GPIO; 415abfafc2dSShawn Guo 416abfafc2dSShawn Guo boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0); 417abfafc2dSShawn Guo if (gpio_is_valid(boarddata->wp_gpio)) 418abfafc2dSShawn Guo boarddata->wp_type = ESDHC_WP_GPIO; 419abfafc2dSShawn Guo 420abfafc2dSShawn Guo return 0; 421abfafc2dSShawn Guo } 422abfafc2dSShawn Guo #else 423abfafc2dSShawn Guo static inline int 424abfafc2dSShawn Guo sdhci_esdhc_imx_probe_dt(struct platform_device *pdev, 425abfafc2dSShawn Guo struct esdhc_platform_data *boarddata) 426abfafc2dSShawn Guo { 427abfafc2dSShawn Guo return -ENODEV; 428abfafc2dSShawn Guo } 429abfafc2dSShawn Guo #endif 430abfafc2dSShawn Guo 43185d6509dSShawn Guo static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev) 43295f25efeSWolfram Sang { 433abfafc2dSShawn Guo const struct of_device_id *of_id = 434abfafc2dSShawn Guo of_match_device(imx_esdhc_dt_ids, &pdev->dev); 43585d6509dSShawn Guo struct sdhci_pltfm_host *pltfm_host; 43685d6509dSShawn Guo struct sdhci_host *host; 43785d6509dSShawn Guo struct esdhc_platform_data *boarddata; 43895f25efeSWolfram Sang struct clk *clk; 4390c6d49ceSWolfram Sang int err; 440e149860dSRichard Zhu struct pltfm_imx_data *imx_data; 44195f25efeSWolfram Sang 44285d6509dSShawn Guo host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata); 44385d6509dSShawn Guo if (IS_ERR(host)) 44485d6509dSShawn Guo return PTR_ERR(host); 44585d6509dSShawn Guo 44685d6509dSShawn Guo pltfm_host = sdhci_priv(host); 44785d6509dSShawn Guo 44885d6509dSShawn Guo imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL); 449abfafc2dSShawn Guo if (!imx_data) { 450abfafc2dSShawn Guo err = -ENOMEM; 451abfafc2dSShawn Guo goto err_imx_data; 452abfafc2dSShawn Guo } 45357ed3314SShawn Guo 454abfafc2dSShawn Guo if (of_id) 455abfafc2dSShawn Guo pdev->id_entry = of_id->data; 45657ed3314SShawn Guo imx_data->devtype = pdev->id_entry->driver_data; 45785d6509dSShawn Guo pltfm_host->priv = imx_data; 45885d6509dSShawn Guo 45995f25efeSWolfram Sang clk = clk_get(mmc_dev(host->mmc), NULL); 46095f25efeSWolfram Sang if (IS_ERR(clk)) { 46195f25efeSWolfram Sang dev_err(mmc_dev(host->mmc), "clk err\n"); 46285d6509dSShawn Guo err = PTR_ERR(clk); 46385d6509dSShawn Guo goto err_clk_get; 46495f25efeSWolfram Sang } 46595f25efeSWolfram Sang clk_enable(clk); 46695f25efeSWolfram Sang pltfm_host->clk = clk; 46795f25efeSWolfram Sang 46857ed3314SShawn Guo if (!is_imx25_esdhc(imx_data)) 46937865fe9SEric Bénard host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 47037865fe9SEric Bénard 47157ed3314SShawn Guo if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data)) 4720c6d49ceSWolfram Sang /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */ 47397e4ba6aSRichard Zhu host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK 47497e4ba6aSRichard Zhu | SDHCI_QUIRK_BROKEN_ADMA; 4750c6d49ceSWolfram Sang 47657ed3314SShawn Guo if (is_imx53_esdhc(imx_data)) 47758ac8177SRichard Zhu imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT; 47858ac8177SRichard Zhu 479abfafc2dSShawn Guo boarddata = &imx_data->boarddata; 480abfafc2dSShawn Guo if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) { 481842afc02SShawn Guo if (!host->mmc->parent->platform_data) { 482913413c3SShawn Guo dev_err(mmc_dev(host->mmc), "no board data!\n"); 483913413c3SShawn Guo err = -EINVAL; 484913413c3SShawn Guo goto no_board_data; 485913413c3SShawn Guo } 486842afc02SShawn Guo imx_data->boarddata = *((struct esdhc_platform_data *) 487842afc02SShawn Guo host->mmc->parent->platform_data); 488abfafc2dSShawn Guo } 489913413c3SShawn Guo 490913413c3SShawn Guo /* write_protect */ 491913413c3SShawn Guo if (boarddata->wp_type == ESDHC_WP_GPIO) { 4920c6d49ceSWolfram Sang err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP"); 4930c6d49ceSWolfram Sang if (err) { 4940c6d49ceSWolfram Sang dev_warn(mmc_dev(host->mmc), 4950c6d49ceSWolfram Sang "no write-protect pin available!\n"); 496913413c3SShawn Guo boarddata->wp_gpio = -EINVAL; 497913413c3SShawn Guo } 498913413c3SShawn Guo } else { 499913413c3SShawn Guo boarddata->wp_gpio = -EINVAL; 5000c6d49ceSWolfram Sang } 5017e29c306SWolfram Sang 502913413c3SShawn Guo /* card_detect */ 503913413c3SShawn Guo if (boarddata->cd_type != ESDHC_CD_GPIO) 504913413c3SShawn Guo boarddata->cd_gpio = -EINVAL; 505913413c3SShawn Guo 506913413c3SShawn Guo switch (boarddata->cd_type) { 507913413c3SShawn Guo case ESDHC_CD_GPIO: 5087e29c306SWolfram Sang err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD"); 5097e29c306SWolfram Sang if (err) { 510913413c3SShawn Guo dev_err(mmc_dev(host->mmc), 5117e29c306SWolfram Sang "no card-detect pin available!\n"); 5127e29c306SWolfram Sang goto no_card_detect_pin; 5130c6d49ceSWolfram Sang } 51416a790bcSEric Bénard 5157e29c306SWolfram Sang err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq, 5167e29c306SWolfram Sang IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 5177e29c306SWolfram Sang mmc_hostname(host->mmc), host); 5187e29c306SWolfram Sang if (err) { 519913413c3SShawn Guo dev_err(mmc_dev(host->mmc), "request irq error\n"); 5207e29c306SWolfram Sang goto no_card_detect_irq; 5217e29c306SWolfram Sang } 522913413c3SShawn Guo /* fall through */ 5237e29c306SWolfram Sang 524913413c3SShawn Guo case ESDHC_CD_CONTROLLER: 525913413c3SShawn Guo /* we have a working card_detect back */ 5267e29c306SWolfram Sang host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 527913413c3SShawn Guo break; 528913413c3SShawn Guo 529913413c3SShawn Guo case ESDHC_CD_PERMANENT: 530913413c3SShawn Guo host->mmc->caps = MMC_CAP_NONREMOVABLE; 531913413c3SShawn Guo break; 532913413c3SShawn Guo 533913413c3SShawn Guo case ESDHC_CD_NONE: 534913413c3SShawn Guo break; 5357e29c306SWolfram Sang } 5367e29c306SWolfram Sang 53785d6509dSShawn Guo err = sdhci_add_host(host); 53885d6509dSShawn Guo if (err) 53985d6509dSShawn Guo goto err_add_host; 54085d6509dSShawn Guo 5417e29c306SWolfram Sang return 0; 5427e29c306SWolfram Sang 54385d6509dSShawn Guo err_add_host: 544913413c3SShawn Guo if (gpio_is_valid(boarddata->cd_gpio)) 545913413c3SShawn Guo free_irq(gpio_to_irq(boarddata->cd_gpio), host); 546913413c3SShawn Guo no_card_detect_irq: 547913413c3SShawn Guo if (gpio_is_valid(boarddata->cd_gpio)) 548913413c3SShawn Guo gpio_free(boarddata->cd_gpio); 549913413c3SShawn Guo if (gpio_is_valid(boarddata->wp_gpio)) 550913413c3SShawn Guo gpio_free(boarddata->wp_gpio); 551913413c3SShawn Guo no_card_detect_pin: 552913413c3SShawn Guo no_board_data: 55385d6509dSShawn Guo clk_disable(pltfm_host->clk); 55485d6509dSShawn Guo clk_put(pltfm_host->clk); 55585d6509dSShawn Guo err_clk_get: 556913413c3SShawn Guo kfree(imx_data); 557abfafc2dSShawn Guo err_imx_data: 55885d6509dSShawn Guo sdhci_pltfm_free(pdev); 55985d6509dSShawn Guo return err; 56095f25efeSWolfram Sang } 56195f25efeSWolfram Sang 56285d6509dSShawn Guo static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev) 56395f25efeSWolfram Sang { 56485d6509dSShawn Guo struct sdhci_host *host = platform_get_drvdata(pdev); 56595f25efeSWolfram Sang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 566e149860dSRichard Zhu struct pltfm_imx_data *imx_data = pltfm_host->priv; 567842afc02SShawn Guo struct esdhc_platform_data *boarddata = &imx_data->boarddata; 56885d6509dSShawn Guo int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff); 56985d6509dSShawn Guo 57085d6509dSShawn Guo sdhci_remove_host(host, dead); 5710c6d49ceSWolfram Sang 572913413c3SShawn Guo if (gpio_is_valid(boarddata->wp_gpio)) 5730c6d49ceSWolfram Sang gpio_free(boarddata->wp_gpio); 57495f25efeSWolfram Sang 575913413c3SShawn Guo if (gpio_is_valid(boarddata->cd_gpio)) { 5767e29c306SWolfram Sang free_irq(gpio_to_irq(boarddata->cd_gpio), host); 577913413c3SShawn Guo gpio_free(boarddata->cd_gpio); 5787e29c306SWolfram Sang } 5797e29c306SWolfram Sang 58095f25efeSWolfram Sang clk_disable(pltfm_host->clk); 58195f25efeSWolfram Sang clk_put(pltfm_host->clk); 582e149860dSRichard Zhu kfree(imx_data); 58385d6509dSShawn Guo 58485d6509dSShawn Guo sdhci_pltfm_free(pdev); 58585d6509dSShawn Guo 58685d6509dSShawn Guo return 0; 58795f25efeSWolfram Sang } 58895f25efeSWolfram Sang 58985d6509dSShawn Guo static struct platform_driver sdhci_esdhc_imx_driver = { 59085d6509dSShawn Guo .driver = { 59185d6509dSShawn Guo .name = "sdhci-esdhc-imx", 59285d6509dSShawn Guo .owner = THIS_MODULE, 593abfafc2dSShawn Guo .of_match_table = imx_esdhc_dt_ids, 59485d6509dSShawn Guo }, 59557ed3314SShawn Guo .id_table = imx_esdhc_devtype, 59685d6509dSShawn Guo .probe = sdhci_esdhc_imx_probe, 59785d6509dSShawn Guo .remove = __devexit_p(sdhci_esdhc_imx_remove), 59885d6509dSShawn Guo #ifdef CONFIG_PM 59985d6509dSShawn Guo .suspend = sdhci_pltfm_suspend, 60085d6509dSShawn Guo .resume = sdhci_pltfm_resume, 60185d6509dSShawn Guo #endif 60295f25efeSWolfram Sang }; 60385d6509dSShawn Guo 60485d6509dSShawn Guo static int __init sdhci_esdhc_imx_init(void) 60585d6509dSShawn Guo { 60685d6509dSShawn Guo return platform_driver_register(&sdhci_esdhc_imx_driver); 60785d6509dSShawn Guo } 60885d6509dSShawn Guo module_init(sdhci_esdhc_imx_init); 60985d6509dSShawn Guo 61085d6509dSShawn Guo static void __exit sdhci_esdhc_imx_exit(void) 61185d6509dSShawn Guo { 61285d6509dSShawn Guo platform_driver_unregister(&sdhci_esdhc_imx_driver); 61385d6509dSShawn Guo } 61485d6509dSShawn Guo module_exit(sdhci_esdhc_imx_exit); 61585d6509dSShawn Guo 61685d6509dSShawn Guo MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC"); 61785d6509dSShawn Guo MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); 61885d6509dSShawn Guo MODULE_LICENSE("GPL v2"); 619