blob: 4dc0028086a34357ef5b5578678ce7ca4a1ccdd6 [file] [log] [blame]
Wolfram Sang95f25ef2010-10-15 12:21:04 +02001/*
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 Sang0c6d49c2011-02-26 14:44:39 +010018#include <linux/gpio.h>
Shawn Guo66506f72011-08-15 10:28:18 +080019#include <linux/module.h>
Richard Zhue1498602011-03-25 09:18:27 -040020#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020021#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080022#include <linux/mmc/mmc.h>
23#include <linux/mmc/sdio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080024#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/of_gpio.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010027#include <mach/esdhc.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020028#include "sdhci-pltfm.h"
29#include "sdhci-esdhc.h"
30
Tony Lin0d588642011-08-11 16:45:59 -040031#define SDHCI_CTRL_D3CD 0x08
Richard Zhu58ac8172011-03-21 13:22:16 +080032/* VENDOR SPEC register */
33#define SDHCI_VENDOR_SPEC 0xC0
34#define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
35
Richard Zhu58ac8172011-03-21 13:22:16 +080036/*
37 * The CMDTYPE of the CMD register (offset 0xE) should be set to
38 * "11" when the STOP CMD12 is issued on imx53 to abort one
39 * open ended multi-blk IO. Otherwise the TC INT wouldn't
40 * be generated.
41 * In exact block transfer, the controller doesn't complete the
42 * operations automatically as required at the end of the
43 * transfer and remains on hold if the abort command is not sent.
44 * As a result, the TC flag is not asserted and SW received timeout
45 * exeception. Bit1 of Vendor Spec registor is used to fix it.
46 */
47#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040048
Shawn Guo57ed3312011-06-30 09:24:26 +080049enum imx_esdhc_type {
50 IMX25_ESDHC,
51 IMX35_ESDHC,
52 IMX51_ESDHC,
53 IMX53_ESDHC,
54};
55
Richard Zhue1498602011-03-25 09:18:27 -040056struct pltfm_imx_data {
57 int flags;
58 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080059 enum imx_esdhc_type devtype;
Shawn Guo842afc02011-07-06 22:57:48 +080060 struct esdhc_platform_data boarddata;
Richard Zhue1498602011-03-25 09:18:27 -040061};
62
Shawn Guo57ed3312011-06-30 09:24:26 +080063static struct platform_device_id imx_esdhc_devtype[] = {
64 {
65 .name = "sdhci-esdhc-imx25",
66 .driver_data = IMX25_ESDHC,
67 }, {
68 .name = "sdhci-esdhc-imx35",
69 .driver_data = IMX35_ESDHC,
70 }, {
71 .name = "sdhci-esdhc-imx51",
72 .driver_data = IMX51_ESDHC,
73 }, {
74 .name = "sdhci-esdhc-imx53",
75 .driver_data = IMX53_ESDHC,
76 }, {
77 /* sentinel */
78 }
79};
80MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
81
Shawn Guoabfafc22011-06-30 15:44:44 +080082static const struct of_device_id imx_esdhc_dt_ids[] = {
83 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
84 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
85 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
86 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
87 { /* sentinel */ }
88};
89MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
90
Shawn Guo57ed3312011-06-30 09:24:26 +080091static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
92{
93 return data->devtype == IMX25_ESDHC;
94}
95
96static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
97{
98 return data->devtype == IMX35_ESDHC;
99}
100
101static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
102{
103 return data->devtype == IMX51_ESDHC;
104}
105
106static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
107{
108 return data->devtype == IMX53_ESDHC;
109}
110
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200111static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
112{
113 void __iomem *base = host->ioaddr + (reg & ~0x3);
114 u32 shift = (reg & 0x3) * 8;
115
116 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
117}
118
Wolfram Sang7e29c302011-02-26 14:44:41 +0100119static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
120{
Shawn Guo842afc02011-07-06 22:57:48 +0800121 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
122 struct pltfm_imx_data *imx_data = pltfm_host->priv;
123 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Richard Zhue1498602011-03-25 09:18:27 -0400124
Shawn Guo913413c2011-06-21 22:41:51 +0800125 /* fake CARD_PRESENT flag */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100126 u32 val = readl(host->ioaddr + reg);
127
Richard Zhue1498602011-03-25 09:18:27 -0400128 if (unlikely((reg == SDHCI_PRESENT_STATE)
Shawn Guo913413c2011-06-21 22:41:51 +0800129 && gpio_is_valid(boarddata->cd_gpio))) {
130 if (gpio_get_value(boarddata->cd_gpio))
Wolfram Sang7e29c302011-02-26 14:44:41 +0100131 /* no card, if a valid gpio says so... */
Shawn Guo803862a2011-06-21 22:41:49 +0800132 val &= ~SDHCI_CARD_PRESENT;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100133 else
134 /* ... in all other cases assume card is present */
135 val |= SDHCI_CARD_PRESENT;
136 }
137
138 return val;
139}
140
141static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
142{
Richard Zhue1498602011-03-25 09:18:27 -0400143 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
144 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800145 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Tony Lin0d588642011-08-11 16:45:59 -0400146 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400147
Tony Lin0d588642011-08-11 16:45:59 -0400148 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
149 if (boarddata->cd_type == ESDHC_CD_GPIO)
150 /*
151 * These interrupts won't work with a custom
152 * card_detect gpio (only applied to mx25/35)
153 */
154 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
155
156 if (val & SDHCI_INT_CARD_INT) {
157 /*
158 * Clear and then set D3CD bit to avoid missing the
159 * card interrupt. This is a eSDHC controller problem
160 * so we need to apply the following workaround: clear
161 * and set D3CD bit will make eSDHC re-sample the card
162 * interrupt. In case a card interrupt was lost,
163 * re-sample it by the following steps.
164 */
165 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
166 data &= ~SDHCI_CTRL_D3CD;
167 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
168 data |= SDHCI_CTRL_D3CD;
169 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
170 }
171 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100172
Richard Zhu58ac8172011-03-21 13:22:16 +0800173 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
174 && (reg == SDHCI_INT_STATUS)
175 && (val & SDHCI_INT_DATA_END))) {
176 u32 v;
177 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
178 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
179 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
180 }
181
Wolfram Sang7e29c302011-02-26 14:44:41 +0100182 writel(val, host->ioaddr + reg);
183}
184
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200185static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
186{
187 if (unlikely(reg == SDHCI_HOST_VERSION))
188 reg ^= 2;
189
190 return readw(host->ioaddr + reg);
191}
192
193static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
194{
195 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400196 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200197
198 switch (reg) {
199 case SDHCI_TRANSFER_MODE:
200 /*
201 * Postpone this write, we must do it together with a
202 * command write that is down below.
203 */
Richard Zhu58ac8172011-03-21 13:22:16 +0800204 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
205 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
206 && (host->cmd->data->blocks > 1)
207 && (host->cmd->data->flags & MMC_DATA_READ)) {
208 u32 v;
209 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
210 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
211 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
212 }
Richard Zhue1498602011-03-25 09:18:27 -0400213 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200214 return;
215 case SDHCI_COMMAND:
Richard Zhu58ac8172011-03-21 13:22:16 +0800216 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION)
217 && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
218 val |= SDHCI_CMD_ABORTCMD;
Richard Zhue1498602011-03-25 09:18:27 -0400219 writel(val << 16 | imx_data->scratchpad,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200220 host->ioaddr + SDHCI_TRANSFER_MODE);
221 return;
222 case SDHCI_BLOCK_SIZE:
223 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
224 break;
225 }
226 esdhc_clrset_le(host, 0xffff, val, reg);
227}
228
229static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
230{
231 u32 new_val;
232
233 switch (reg) {
234 case SDHCI_POWER_CONTROL:
235 /*
236 * FSL put some DMA bits here
237 * If your board has a regulator, code should be here
238 */
239 return;
240 case SDHCI_HOST_CONTROL:
Tony Lin0d588642011-08-11 16:45:59 -0400241 /* FSL messed up here, so we can just keep those three */
242 new_val = val & (SDHCI_CTRL_LED | \
243 SDHCI_CTRL_4BITBUS | \
244 SDHCI_CTRL_D3CD);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200245 /* ensure the endianess */
246 new_val |= ESDHC_HOST_CONTROL_LE;
247 /* DMA mode bits are shifted */
248 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
249
250 esdhc_clrset_le(host, 0xffff, new_val, reg);
251 return;
252 }
253 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800254
255 /*
256 * The esdhc has a design violation to SDHC spec which tells
257 * that software reset should not affect card detection circuit.
258 * But esdhc clears its SYSCTL register bits [0..2] during the
259 * software reset. This will stop those clocks that card detection
260 * circuit relies on. To work around it, we turn the clocks on back
261 * to keep card detection circuit functional.
262 */
263 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
264 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200265}
266
267static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
268{
269 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
270
271 return clk_get_rate(pltfm_host->clk);
272}
273
274static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
275{
276 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
277
278 return clk_get_rate(pltfm_host->clk) / 256 / 16;
279}
280
Shawn Guo913413c2011-06-21 22:41:51 +0800281static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
282{
Shawn Guo842afc02011-07-06 22:57:48 +0800283 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
284 struct pltfm_imx_data *imx_data = pltfm_host->priv;
285 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800286
287 switch (boarddata->wp_type) {
288 case ESDHC_WP_GPIO:
289 if (gpio_is_valid(boarddata->wp_gpio))
290 return gpio_get_value(boarddata->wp_gpio);
291 case ESDHC_WP_CONTROLLER:
292 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
293 SDHCI_WRITE_PROTECT);
294 case ESDHC_WP_NONE:
295 break;
296 }
297
298 return -ENOSYS;
299}
300
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100301static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400302 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100303 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400304 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100305 .write_w = esdhc_writew_le,
306 .write_b = esdhc_writeb_le,
307 .set_clock = esdhc_set_clock,
308 .get_max_clock = esdhc_pltfm_get_max_clock,
309 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800310 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100311};
312
Shawn Guo85d65092011-05-27 23:48:12 +0800313static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
314 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_ADMA
315 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
316 /* ADMA has issues. Might be fixable */
317 .ops = &sdhci_esdhc_ops,
318};
319
Wolfram Sang7e29c302011-02-26 14:44:41 +0100320static irqreturn_t cd_irq(int irq, void *data)
321{
322 struct sdhci_host *sdhost = (struct sdhci_host *)data;
323
324 tasklet_schedule(&sdhost->card_tasklet);
325 return IRQ_HANDLED;
326};
327
Shawn Guoabfafc22011-06-30 15:44:44 +0800328#ifdef CONFIG_OF
329static int __devinit
330sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
331 struct esdhc_platform_data *boarddata)
332{
333 struct device_node *np = pdev->dev.of_node;
334
335 if (!np)
336 return -ENODEV;
337
338 if (of_get_property(np, "fsl,card-wired", NULL))
339 boarddata->cd_type = ESDHC_CD_PERMANENT;
340
341 if (of_get_property(np, "fsl,cd-controller", NULL))
342 boarddata->cd_type = ESDHC_CD_CONTROLLER;
343
344 if (of_get_property(np, "fsl,wp-controller", NULL))
345 boarddata->wp_type = ESDHC_WP_CONTROLLER;
346
347 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
348 if (gpio_is_valid(boarddata->cd_gpio))
349 boarddata->cd_type = ESDHC_CD_GPIO;
350
351 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
352 if (gpio_is_valid(boarddata->wp_gpio))
353 boarddata->wp_type = ESDHC_WP_GPIO;
354
355 return 0;
356}
357#else
358static inline int
359sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
360 struct esdhc_platform_data *boarddata)
361{
362 return -ENODEV;
363}
364#endif
365
Shawn Guo85d65092011-05-27 23:48:12 +0800366static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200367{
Shawn Guoabfafc22011-06-30 15:44:44 +0800368 const struct of_device_id *of_id =
369 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800370 struct sdhci_pltfm_host *pltfm_host;
371 struct sdhci_host *host;
372 struct esdhc_platform_data *boarddata;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200373 struct clk *clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100374 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400375 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200376
Shawn Guo85d65092011-05-27 23:48:12 +0800377 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
378 if (IS_ERR(host))
379 return PTR_ERR(host);
380
381 pltfm_host = sdhci_priv(host);
382
383 imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800384 if (!imx_data) {
385 err = -ENOMEM;
386 goto err_imx_data;
387 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800388
Shawn Guoabfafc22011-06-30 15:44:44 +0800389 if (of_id)
390 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800391 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800392 pltfm_host->priv = imx_data;
393
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200394 clk = clk_get(mmc_dev(host->mmc), NULL);
395 if (IS_ERR(clk)) {
396 dev_err(mmc_dev(host->mmc), "clk err\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800397 err = PTR_ERR(clk);
398 goto err_clk_get;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200399 }
400 clk_enable(clk);
401 pltfm_host->clk = clk;
402
Shawn Guo57ed3312011-06-30 09:24:26 +0800403 if (!is_imx25_esdhc(imx_data))
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200404 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
405
Shawn Guo57ed3312011-06-30 09:24:26 +0800406 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100407 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200408 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100409
Shawn Guo57ed3312011-06-30 09:24:26 +0800410 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800411 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
412
Shawn Guo842afc02011-07-06 22:57:48 +0800413 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800414 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
415 if (!host->mmc->parent->platform_data) {
416 dev_err(mmc_dev(host->mmc), "no board data!\n");
417 err = -EINVAL;
418 goto no_board_data;
419 }
420 imx_data->boarddata = *((struct esdhc_platform_data *)
421 host->mmc->parent->platform_data);
422 }
Shawn Guo913413c2011-06-21 22:41:51 +0800423
424 /* write_protect */
425 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100426 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
427 if (err) {
428 dev_warn(mmc_dev(host->mmc),
Shawn Guo913413c2011-06-21 22:41:51 +0800429 "no write-protect pin available!\n");
430 boarddata->wp_gpio = -EINVAL;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100431 }
Shawn Guo913413c2011-06-21 22:41:51 +0800432 } else {
433 boarddata->wp_gpio = -EINVAL;
434 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100435
Shawn Guo913413c2011-06-21 22:41:51 +0800436 /* card_detect */
437 if (boarddata->cd_type != ESDHC_CD_GPIO)
438 boarddata->cd_gpio = -EINVAL;
439
440 switch (boarddata->cd_type) {
441 case ESDHC_CD_GPIO:
Wolfram Sang7e29c302011-02-26 14:44:41 +0100442 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
443 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800444 dev_err(mmc_dev(host->mmc),
Wolfram Sang7e29c302011-02-26 14:44:41 +0100445 "no card-detect pin available!\n");
446 goto no_card_detect_pin;
447 }
448
Wolfram Sang7e29c302011-02-26 14:44:41 +0100449 err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
450 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
451 mmc_hostname(host->mmc), host);
452 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800453 dev_err(mmc_dev(host->mmc), "request irq error\n");
Wolfram Sang7e29c302011-02-26 14:44:41 +0100454 goto no_card_detect_irq;
455 }
Shawn Guo913413c2011-06-21 22:41:51 +0800456 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100457
Shawn Guo913413c2011-06-21 22:41:51 +0800458 case ESDHC_CD_CONTROLLER:
459 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100460 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800461 break;
462
463 case ESDHC_CD_PERMANENT:
464 host->mmc->caps = MMC_CAP_NONREMOVABLE;
465 break;
466
467 case ESDHC_CD_NONE:
468 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100469 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200470
Shawn Guo85d65092011-05-27 23:48:12 +0800471 err = sdhci_add_host(host);
472 if (err)
473 goto err_add_host;
474
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200475 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100476
Shawn Guo913413c2011-06-21 22:41:51 +0800477err_add_host:
478 if (gpio_is_valid(boarddata->cd_gpio))
479 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
480no_card_detect_irq:
481 if (gpio_is_valid(boarddata->cd_gpio))
482 gpio_free(boarddata->cd_gpio);
483 if (gpio_is_valid(boarddata->wp_gpio))
484 gpio_free(boarddata->wp_gpio);
485no_card_detect_pin:
486no_board_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800487 clk_disable(pltfm_host->clk);
488 clk_put(pltfm_host->clk);
Shawn Guo913413c2011-06-21 22:41:51 +0800489err_clk_get:
490 kfree(imx_data);
Shawn Guoabfafc22011-06-30 15:44:44 +0800491err_imx_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800492 sdhci_pltfm_free(pdev);
493 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200494}
495
Shawn Guo85d65092011-05-27 23:48:12 +0800496static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200497{
Shawn Guo85d65092011-05-27 23:48:12 +0800498 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200499 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400500 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800501 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo85d65092011-05-27 23:48:12 +0800502 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
503
504 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100505
Shawn Guo913413c2011-06-21 22:41:51 +0800506 if (gpio_is_valid(boarddata->wp_gpio))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100507 gpio_free(boarddata->wp_gpio);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200508
Shawn Guo913413c2011-06-21 22:41:51 +0800509 if (gpio_is_valid(boarddata->cd_gpio)) {
510 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100511 gpio_free(boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100512 }
513
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200514 clk_disable(pltfm_host->clk);
515 clk_put(pltfm_host->clk);
Richard Zhue1498602011-03-25 09:18:27 -0400516 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800517
518 sdhci_pltfm_free(pdev);
519
520 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200521}
522
Shawn Guo85d65092011-05-27 23:48:12 +0800523static struct platform_driver sdhci_esdhc_imx_driver = {
524 .driver = {
525 .name = "sdhci-esdhc-imx",
526 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800527 .of_match_table = imx_esdhc_dt_ids,
Shawn Guo85d65092011-05-27 23:48:12 +0800528 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800529 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800530 .probe = sdhci_esdhc_imx_probe,
531 .remove = __devexit_p(sdhci_esdhc_imx_remove),
532#ifdef CONFIG_PM
533 .suspend = sdhci_pltfm_suspend,
534 .resume = sdhci_pltfm_resume,
535#endif
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200536};
Shawn Guo85d65092011-05-27 23:48:12 +0800537
538static int __init sdhci_esdhc_imx_init(void)
539{
540 return platform_driver_register(&sdhci_esdhc_imx_driver);
541}
542module_init(sdhci_esdhc_imx_init);
543
544static void __exit sdhci_esdhc_imx_exit(void)
545{
546 platform_driver_unregister(&sdhci_esdhc_imx_driver);
547}
548module_exit(sdhci_esdhc_imx_exit);
549
550MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
551MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
552MODULE_LICENSE("GPL v2");