Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Freescale eSDHC i.MX controller driver for the platform bus. |
| 3 | * |
| 4 | * derived from the OF-version. |
| 5 | * |
| 6 | * Copyright (c) 2010 Pengutronix e.K. |
| 7 | * Author: Wolfram Sang <w.sang@pengutronix.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/clk.h> |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 18 | #include <linux/gpio.h> |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 19 | #include <linux/slab.h> |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 20 | #include <linux/mmc/host.h> |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 21 | #include <linux/mmc/mmc.h> |
| 22 | #include <linux/mmc/sdio.h> |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/of_gpio.h> |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 26 | #include <mach/esdhc.h> |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 27 | #include "sdhci-pltfm.h" |
| 28 | #include "sdhci-esdhc.h" |
| 29 | |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 30 | /* VENDOR SPEC register */ |
| 31 | #define SDHCI_VENDOR_SPEC 0xC0 |
| 32 | #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002 |
| 33 | |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 34 | /* |
| 35 | * The CMDTYPE of the CMD register (offset 0xE) should be set to |
| 36 | * "11" when the STOP CMD12 is issued on imx53 to abort one |
| 37 | * open ended multi-blk IO. Otherwise the TC INT wouldn't |
| 38 | * be generated. |
| 39 | * In exact block transfer, the controller doesn't complete the |
| 40 | * operations automatically as required at the end of the |
| 41 | * transfer and remains on hold if the abort command is not sent. |
| 42 | * As a result, the TC flag is not asserted and SW received timeout |
| 43 | * exeception. Bit1 of Vendor Spec registor is used to fix it. |
| 44 | */ |
| 45 | #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1) |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 46 | |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 47 | enum imx_esdhc_type { |
| 48 | IMX25_ESDHC, |
| 49 | IMX35_ESDHC, |
| 50 | IMX51_ESDHC, |
| 51 | IMX53_ESDHC, |
| 52 | }; |
| 53 | |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 54 | struct pltfm_imx_data { |
| 55 | int flags; |
| 56 | u32 scratchpad; |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 57 | enum imx_esdhc_type devtype; |
Shawn Guo | 842afc0 | 2011-07-06 22:57:48 +0800 | [diff] [blame] | 58 | struct esdhc_platform_data boarddata; |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 59 | }; |
| 60 | |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 61 | static struct platform_device_id imx_esdhc_devtype[] = { |
| 62 | { |
| 63 | .name = "sdhci-esdhc-imx25", |
| 64 | .driver_data = IMX25_ESDHC, |
| 65 | }, { |
| 66 | .name = "sdhci-esdhc-imx35", |
| 67 | .driver_data = IMX35_ESDHC, |
| 68 | }, { |
| 69 | .name = "sdhci-esdhc-imx51", |
| 70 | .driver_data = IMX51_ESDHC, |
| 71 | }, { |
| 72 | .name = "sdhci-esdhc-imx53", |
| 73 | .driver_data = IMX53_ESDHC, |
| 74 | }, { |
| 75 | /* sentinel */ |
| 76 | } |
| 77 | }; |
| 78 | MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype); |
| 79 | |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 80 | static const struct of_device_id imx_esdhc_dt_ids[] = { |
| 81 | { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], }, |
| 82 | { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], }, |
| 83 | { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], }, |
| 84 | { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], }, |
| 85 | { /* sentinel */ } |
| 86 | }; |
| 87 | MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids); |
| 88 | |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 89 | static inline int is_imx25_esdhc(struct pltfm_imx_data *data) |
| 90 | { |
| 91 | return data->devtype == IMX25_ESDHC; |
| 92 | } |
| 93 | |
| 94 | static inline int is_imx35_esdhc(struct pltfm_imx_data *data) |
| 95 | { |
| 96 | return data->devtype == IMX35_ESDHC; |
| 97 | } |
| 98 | |
| 99 | static inline int is_imx51_esdhc(struct pltfm_imx_data *data) |
| 100 | { |
| 101 | return data->devtype == IMX51_ESDHC; |
| 102 | } |
| 103 | |
| 104 | static inline int is_imx53_esdhc(struct pltfm_imx_data *data) |
| 105 | { |
| 106 | return data->devtype == IMX53_ESDHC; |
| 107 | } |
| 108 | |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 109 | static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg) |
| 110 | { |
| 111 | void __iomem *base = host->ioaddr + (reg & ~0x3); |
| 112 | u32 shift = (reg & 0x3) * 8; |
| 113 | |
| 114 | writel(((readl(base) & ~(mask << shift)) | (val << shift)), base); |
| 115 | } |
| 116 | |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 117 | static u32 esdhc_readl_le(struct sdhci_host *host, int reg) |
| 118 | { |
Shawn Guo | 842afc0 | 2011-07-06 22:57:48 +0800 | [diff] [blame] | 119 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 120 | struct pltfm_imx_data *imx_data = pltfm_host->priv; |
| 121 | struct esdhc_platform_data *boarddata = &imx_data->boarddata; |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 122 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 123 | /* fake CARD_PRESENT flag */ |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 124 | u32 val = readl(host->ioaddr + reg); |
| 125 | |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 126 | if (unlikely((reg == SDHCI_PRESENT_STATE) |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 127 | && gpio_is_valid(boarddata->cd_gpio))) { |
| 128 | if (gpio_get_value(boarddata->cd_gpio)) |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 129 | /* no card, if a valid gpio says so... */ |
Shawn Guo | 803862a | 2011-06-21 22:41:49 +0800 | [diff] [blame] | 130 | val &= ~SDHCI_CARD_PRESENT; |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 131 | else |
| 132 | /* ... in all other cases assume card is present */ |
| 133 | val |= SDHCI_CARD_PRESENT; |
| 134 | } |
| 135 | |
| 136 | return val; |
| 137 | } |
| 138 | |
| 139 | static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg) |
| 140 | { |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 141 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 142 | struct pltfm_imx_data *imx_data = pltfm_host->priv; |
Shawn Guo | 842afc0 | 2011-07-06 22:57:48 +0800 | [diff] [blame] | 143 | struct esdhc_platform_data *boarddata = &imx_data->boarddata; |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 144 | |
| 145 | if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE) |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 146 | && (boarddata->cd_type == ESDHC_CD_GPIO))) |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 147 | /* |
| 148 | * these interrupts won't work with a custom card_detect gpio |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 149 | */ |
| 150 | val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT); |
| 151 | |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 152 | if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT) |
| 153 | && (reg == SDHCI_INT_STATUS) |
| 154 | && (val & SDHCI_INT_DATA_END))) { |
| 155 | u32 v; |
| 156 | v = readl(host->ioaddr + SDHCI_VENDOR_SPEC); |
| 157 | v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK; |
| 158 | writel(v, host->ioaddr + SDHCI_VENDOR_SPEC); |
| 159 | } |
| 160 | |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 161 | writel(val, host->ioaddr + reg); |
| 162 | } |
| 163 | |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 164 | static u16 esdhc_readw_le(struct sdhci_host *host, int reg) |
| 165 | { |
| 166 | if (unlikely(reg == SDHCI_HOST_VERSION)) |
| 167 | reg ^= 2; |
| 168 | |
| 169 | return readw(host->ioaddr + reg); |
| 170 | } |
| 171 | |
| 172 | static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg) |
| 173 | { |
| 174 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 175 | struct pltfm_imx_data *imx_data = pltfm_host->priv; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 176 | |
| 177 | switch (reg) { |
| 178 | case SDHCI_TRANSFER_MODE: |
| 179 | /* |
| 180 | * Postpone this write, we must do it together with a |
| 181 | * command write that is down below. |
| 182 | */ |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 183 | if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT) |
| 184 | && (host->cmd->opcode == SD_IO_RW_EXTENDED) |
| 185 | && (host->cmd->data->blocks > 1) |
| 186 | && (host->cmd->data->flags & MMC_DATA_READ)) { |
| 187 | u32 v; |
| 188 | v = readl(host->ioaddr + SDHCI_VENDOR_SPEC); |
| 189 | v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK; |
| 190 | writel(v, host->ioaddr + SDHCI_VENDOR_SPEC); |
| 191 | } |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 192 | imx_data->scratchpad = val; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 193 | return; |
| 194 | case SDHCI_COMMAND: |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 195 | if ((host->cmd->opcode == MMC_STOP_TRANSMISSION) |
| 196 | && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)) |
| 197 | val |= SDHCI_CMD_ABORTCMD; |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 198 | writel(val << 16 | imx_data->scratchpad, |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 199 | host->ioaddr + SDHCI_TRANSFER_MODE); |
| 200 | return; |
| 201 | case SDHCI_BLOCK_SIZE: |
| 202 | val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); |
| 203 | break; |
| 204 | } |
| 205 | esdhc_clrset_le(host, 0xffff, val, reg); |
| 206 | } |
| 207 | |
| 208 | static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg) |
| 209 | { |
| 210 | u32 new_val; |
| 211 | |
| 212 | switch (reg) { |
| 213 | case SDHCI_POWER_CONTROL: |
| 214 | /* |
| 215 | * FSL put some DMA bits here |
| 216 | * If your board has a regulator, code should be here |
| 217 | */ |
| 218 | return; |
| 219 | case SDHCI_HOST_CONTROL: |
| 220 | /* FSL messed up here, so we can just keep those two */ |
| 221 | new_val = val & (SDHCI_CTRL_LED | SDHCI_CTRL_4BITBUS); |
| 222 | /* ensure the endianess */ |
| 223 | new_val |= ESDHC_HOST_CONTROL_LE; |
| 224 | /* DMA mode bits are shifted */ |
| 225 | new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5; |
| 226 | |
| 227 | esdhc_clrset_le(host, 0xffff, new_val, reg); |
| 228 | return; |
| 229 | } |
| 230 | esdhc_clrset_le(host, 0xff, val, reg); |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * The esdhc has a design violation to SDHC spec which tells |
| 234 | * that software reset should not affect card detection circuit. |
| 235 | * But esdhc clears its SYSCTL register bits [0..2] during the |
| 236 | * software reset. This will stop those clocks that card detection |
| 237 | * circuit relies on. To work around it, we turn the clocks on back |
| 238 | * to keep card detection circuit functional. |
| 239 | */ |
| 240 | if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) |
| 241 | esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL); |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host) |
| 245 | { |
| 246 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 247 | |
| 248 | return clk_get_rate(pltfm_host->clk); |
| 249 | } |
| 250 | |
| 251 | static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host) |
| 252 | { |
| 253 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 254 | |
| 255 | return clk_get_rate(pltfm_host->clk) / 256 / 16; |
| 256 | } |
| 257 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 258 | static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host) |
| 259 | { |
Shawn Guo | 842afc0 | 2011-07-06 22:57:48 +0800 | [diff] [blame] | 260 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 261 | struct pltfm_imx_data *imx_data = pltfm_host->priv; |
| 262 | struct esdhc_platform_data *boarddata = &imx_data->boarddata; |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 263 | |
| 264 | switch (boarddata->wp_type) { |
| 265 | case ESDHC_WP_GPIO: |
| 266 | if (gpio_is_valid(boarddata->wp_gpio)) |
| 267 | return gpio_get_value(boarddata->wp_gpio); |
| 268 | case ESDHC_WP_CONTROLLER: |
| 269 | return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) & |
| 270 | SDHCI_WRITE_PROTECT); |
| 271 | case ESDHC_WP_NONE: |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | return -ENOSYS; |
| 276 | } |
| 277 | |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 278 | static struct sdhci_ops sdhci_esdhc_ops = { |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 279 | .read_l = esdhc_readl_le, |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 280 | .read_w = esdhc_readw_le, |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 281 | .write_l = esdhc_writel_le, |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 282 | .write_w = esdhc_writew_le, |
| 283 | .write_b = esdhc_writeb_le, |
| 284 | .set_clock = esdhc_set_clock, |
| 285 | .get_max_clock = esdhc_pltfm_get_max_clock, |
| 286 | .get_min_clock = esdhc_pltfm_get_min_clock, |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 287 | .get_ro = esdhc_pltfm_get_ro, |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 288 | }; |
| 289 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 290 | static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = { |
| 291 | .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA |
| 292 | | SDHCI_QUIRK_BROKEN_CARD_DETECTION, |
| 293 | /* ADMA has issues. Might be fixable */ |
| 294 | .ops = &sdhci_esdhc_ops, |
| 295 | }; |
| 296 | |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 297 | static irqreturn_t cd_irq(int irq, void *data) |
| 298 | { |
| 299 | struct sdhci_host *sdhost = (struct sdhci_host *)data; |
| 300 | |
| 301 | tasklet_schedule(&sdhost->card_tasklet); |
| 302 | return IRQ_HANDLED; |
| 303 | }; |
| 304 | |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 305 | #ifdef CONFIG_OF |
| 306 | static int __devinit |
| 307 | sdhci_esdhc_imx_probe_dt(struct platform_device *pdev, |
| 308 | struct esdhc_platform_data *boarddata) |
| 309 | { |
| 310 | struct device_node *np = pdev->dev.of_node; |
| 311 | |
| 312 | if (!np) |
| 313 | return -ENODEV; |
| 314 | |
| 315 | if (of_get_property(np, "fsl,card-wired", NULL)) |
| 316 | boarddata->cd_type = ESDHC_CD_PERMANENT; |
| 317 | |
| 318 | if (of_get_property(np, "fsl,cd-controller", NULL)) |
| 319 | boarddata->cd_type = ESDHC_CD_CONTROLLER; |
| 320 | |
| 321 | if (of_get_property(np, "fsl,wp-controller", NULL)) |
| 322 | boarddata->wp_type = ESDHC_WP_CONTROLLER; |
| 323 | |
| 324 | boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0); |
| 325 | if (gpio_is_valid(boarddata->cd_gpio)) |
| 326 | boarddata->cd_type = ESDHC_CD_GPIO; |
| 327 | |
| 328 | boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0); |
| 329 | if (gpio_is_valid(boarddata->wp_gpio)) |
| 330 | boarddata->wp_type = ESDHC_WP_GPIO; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | #else |
| 335 | static inline int |
| 336 | sdhci_esdhc_imx_probe_dt(struct platform_device *pdev, |
| 337 | struct esdhc_platform_data *boarddata) |
| 338 | { |
| 339 | return -ENODEV; |
| 340 | } |
| 341 | #endif |
| 342 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 343 | static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev) |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 344 | { |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 345 | const struct of_device_id *of_id = |
| 346 | of_match_device(imx_esdhc_dt_ids, &pdev->dev); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 347 | struct sdhci_pltfm_host *pltfm_host; |
| 348 | struct sdhci_host *host; |
| 349 | struct esdhc_platform_data *boarddata; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 350 | struct clk *clk; |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 351 | int err; |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 352 | struct pltfm_imx_data *imx_data; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 353 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 354 | host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata); |
| 355 | if (IS_ERR(host)) |
| 356 | return PTR_ERR(host); |
| 357 | |
| 358 | pltfm_host = sdhci_priv(host); |
| 359 | |
| 360 | imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL); |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 361 | if (!imx_data) { |
| 362 | err = -ENOMEM; |
| 363 | goto err_imx_data; |
| 364 | } |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 365 | |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 366 | if (of_id) |
| 367 | pdev->id_entry = of_id->data; |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 368 | imx_data->devtype = pdev->id_entry->driver_data; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 369 | pltfm_host->priv = imx_data; |
| 370 | |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 371 | clk = clk_get(mmc_dev(host->mmc), NULL); |
| 372 | if (IS_ERR(clk)) { |
| 373 | dev_err(mmc_dev(host->mmc), "clk err\n"); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 374 | err = PTR_ERR(clk); |
| 375 | goto err_clk_get; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 376 | } |
| 377 | clk_enable(clk); |
| 378 | pltfm_host->clk = clk; |
| 379 | |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 380 | if (!is_imx25_esdhc(imx_data)) |
Eric BĂ©nard | 37865fe | 2010-10-23 01:57:21 +0200 | [diff] [blame] | 381 | host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; |
| 382 | |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 383 | if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data)) |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 384 | /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */ |
Eric BĂ©nard | 16a790b | 2010-10-23 01:57:22 +0200 | [diff] [blame] | 385 | host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK; |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 386 | |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 387 | if (is_imx53_esdhc(imx_data)) |
Richard Zhu | 58ac817 | 2011-03-21 13:22:16 +0800 | [diff] [blame] | 388 | imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT; |
| 389 | |
Shawn Guo | 842afc0 | 2011-07-06 22:57:48 +0800 | [diff] [blame] | 390 | boarddata = &imx_data->boarddata; |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 391 | if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) { |
| 392 | if (!host->mmc->parent->platform_data) { |
| 393 | dev_err(mmc_dev(host->mmc), "no board data!\n"); |
| 394 | err = -EINVAL; |
| 395 | goto no_board_data; |
| 396 | } |
| 397 | imx_data->boarddata = *((struct esdhc_platform_data *) |
| 398 | host->mmc->parent->platform_data); |
| 399 | } |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 400 | |
| 401 | /* write_protect */ |
| 402 | if (boarddata->wp_type == ESDHC_WP_GPIO) { |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 403 | err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP"); |
| 404 | if (err) { |
| 405 | dev_warn(mmc_dev(host->mmc), |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 406 | "no write-protect pin available!\n"); |
| 407 | boarddata->wp_gpio = -EINVAL; |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 408 | } |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 409 | } else { |
| 410 | boarddata->wp_gpio = -EINVAL; |
| 411 | } |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 412 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 413 | /* card_detect */ |
| 414 | if (boarddata->cd_type != ESDHC_CD_GPIO) |
| 415 | boarddata->cd_gpio = -EINVAL; |
| 416 | |
| 417 | switch (boarddata->cd_type) { |
| 418 | case ESDHC_CD_GPIO: |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 419 | err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD"); |
| 420 | if (err) { |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 421 | dev_err(mmc_dev(host->mmc), |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 422 | "no card-detect pin available!\n"); |
| 423 | goto no_card_detect_pin; |
| 424 | } |
| 425 | |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 426 | err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq, |
| 427 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 428 | mmc_hostname(host->mmc), host); |
| 429 | if (err) { |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 430 | dev_err(mmc_dev(host->mmc), "request irq error\n"); |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 431 | goto no_card_detect_irq; |
| 432 | } |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 433 | /* fall through */ |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 434 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 435 | case ESDHC_CD_CONTROLLER: |
| 436 | /* we have a working card_detect back */ |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 437 | host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 438 | break; |
| 439 | |
| 440 | case ESDHC_CD_PERMANENT: |
| 441 | host->mmc->caps = MMC_CAP_NONREMOVABLE; |
| 442 | break; |
| 443 | |
| 444 | case ESDHC_CD_NONE: |
| 445 | break; |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 446 | } |
Eric BĂ©nard | 16a790b | 2010-10-23 01:57:22 +0200 | [diff] [blame] | 447 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 448 | err = sdhci_add_host(host); |
| 449 | if (err) |
| 450 | goto err_add_host; |
| 451 | |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 452 | return 0; |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 453 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 454 | err_add_host: |
| 455 | if (gpio_is_valid(boarddata->cd_gpio)) |
| 456 | free_irq(gpio_to_irq(boarddata->cd_gpio), host); |
| 457 | no_card_detect_irq: |
| 458 | if (gpio_is_valid(boarddata->cd_gpio)) |
| 459 | gpio_free(boarddata->cd_gpio); |
| 460 | if (gpio_is_valid(boarddata->wp_gpio)) |
| 461 | gpio_free(boarddata->wp_gpio); |
| 462 | no_card_detect_pin: |
| 463 | no_board_data: |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 464 | clk_disable(pltfm_host->clk); |
| 465 | clk_put(pltfm_host->clk); |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 466 | err_clk_get: |
| 467 | kfree(imx_data); |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 468 | err_imx_data: |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 469 | sdhci_pltfm_free(pdev); |
| 470 | return err; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 473 | static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev) |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 474 | { |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 475 | struct sdhci_host *host = platform_get_drvdata(pdev); |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 476 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 477 | struct pltfm_imx_data *imx_data = pltfm_host->priv; |
Shawn Guo | 842afc0 | 2011-07-06 22:57:48 +0800 | [diff] [blame] | 478 | struct esdhc_platform_data *boarddata = &imx_data->boarddata; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 479 | int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff); |
| 480 | |
| 481 | sdhci_remove_host(host, dead); |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 482 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 483 | if (gpio_is_valid(boarddata->wp_gpio)) |
Wolfram Sang | 0c6d49c | 2011-02-26 14:44:39 +0100 | [diff] [blame] | 484 | gpio_free(boarddata->wp_gpio); |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 485 | |
Shawn Guo | 913413c | 2011-06-21 22:41:51 +0800 | [diff] [blame] | 486 | if (gpio_is_valid(boarddata->cd_gpio)) { |
| 487 | free_irq(gpio_to_irq(boarddata->cd_gpio), host); |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 488 | gpio_free(boarddata->cd_gpio); |
Wolfram Sang | 7e29c30 | 2011-02-26 14:44:41 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 491 | clk_disable(pltfm_host->clk); |
| 492 | clk_put(pltfm_host->clk); |
Richard Zhu | e149860 | 2011-03-25 09:18:27 -0400 | [diff] [blame] | 493 | kfree(imx_data); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 494 | |
| 495 | sdhci_pltfm_free(pdev); |
| 496 | |
| 497 | return 0; |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 500 | static struct platform_driver sdhci_esdhc_imx_driver = { |
| 501 | .driver = { |
| 502 | .name = "sdhci-esdhc-imx", |
| 503 | .owner = THIS_MODULE, |
Shawn Guo | abfafc2 | 2011-06-30 15:44:44 +0800 | [diff] [blame^] | 504 | .of_match_table = imx_esdhc_dt_ids, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 505 | }, |
Shawn Guo | 57ed331 | 2011-06-30 09:24:26 +0800 | [diff] [blame] | 506 | .id_table = imx_esdhc_devtype, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 507 | .probe = sdhci_esdhc_imx_probe, |
| 508 | .remove = __devexit_p(sdhci_esdhc_imx_remove), |
| 509 | #ifdef CONFIG_PM |
| 510 | .suspend = sdhci_pltfm_suspend, |
| 511 | .resume = sdhci_pltfm_resume, |
| 512 | #endif |
Wolfram Sang | 95f25ef | 2010-10-15 12:21:04 +0200 | [diff] [blame] | 513 | }; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 514 | |
| 515 | static int __init sdhci_esdhc_imx_init(void) |
| 516 | { |
| 517 | return platform_driver_register(&sdhci_esdhc_imx_driver); |
| 518 | } |
| 519 | module_init(sdhci_esdhc_imx_init); |
| 520 | |
| 521 | static void __exit sdhci_esdhc_imx_exit(void) |
| 522 | { |
| 523 | platform_driver_unregister(&sdhci_esdhc_imx_driver); |
| 524 | } |
| 525 | module_exit(sdhci_esdhc_imx_exit); |
| 526 | |
| 527 | MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC"); |
| 528 | MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); |
| 529 | MODULE_LICENSE("GPL v2"); |