blob: 9ebfb4b482f5983870169b1b4f0662b1fd4c7e24 [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>
Richard Zhue1498602011-03-25 09:18:27 -040019#include <linux/slab.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020020#include <linux/mmc/host.h>
Richard Zhu58ac8172011-03-21 13:22:16 +080021#include <linux/mmc/mmc.h>
22#include <linux/mmc/sdio.h>
Shawn Guoabfafc22011-06-30 15:44:44 +080023#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/of_gpio.h>
Wolfram Sang0c6d49c2011-02-26 14:44:39 +010026#include <mach/esdhc.h>
Wolfram Sang95f25ef2010-10-15 12:21:04 +020027#include "sdhci-pltfm.h"
28#include "sdhci-esdhc.h"
29
Richard Zhu58ac8172011-03-21 13:22:16 +080030/* VENDOR SPEC register */
31#define SDHCI_VENDOR_SPEC 0xC0
32#define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
33
Richard Zhu58ac8172011-03-21 13:22:16 +080034/*
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 Zhue1498602011-03-25 09:18:27 -040046
Shawn Guo57ed3312011-06-30 09:24:26 +080047enum imx_esdhc_type {
48 IMX25_ESDHC,
49 IMX35_ESDHC,
50 IMX51_ESDHC,
51 IMX53_ESDHC,
52};
53
Richard Zhue1498602011-03-25 09:18:27 -040054struct pltfm_imx_data {
55 int flags;
56 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080057 enum imx_esdhc_type devtype;
Shawn Guo842afc02011-07-06 22:57:48 +080058 struct esdhc_platform_data boarddata;
Richard Zhue1498602011-03-25 09:18:27 -040059};
60
Shawn Guo57ed3312011-06-30 09:24:26 +080061static 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};
78MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
79
Shawn Guoabfafc22011-06-30 15:44:44 +080080static 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};
87MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
88
Shawn Guo57ed3312011-06-30 09:24:26 +080089static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
90{
91 return data->devtype == IMX25_ESDHC;
92}
93
94static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
95{
96 return data->devtype == IMX35_ESDHC;
97}
98
99static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
100{
101 return data->devtype == IMX51_ESDHC;
102}
103
104static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
105{
106 return data->devtype == IMX53_ESDHC;
107}
108
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200109static 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 Sang7e29c302011-02-26 14:44:41 +0100117static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
118{
Shawn Guo842afc02011-07-06 22:57:48 +0800119 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 Zhue1498602011-03-25 09:18:27 -0400122
Shawn Guo913413c2011-06-21 22:41:51 +0800123 /* fake CARD_PRESENT flag */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100124 u32 val = readl(host->ioaddr + reg);
125
Richard Zhue1498602011-03-25 09:18:27 -0400126 if (unlikely((reg == SDHCI_PRESENT_STATE)
Shawn Guo913413c2011-06-21 22:41:51 +0800127 && gpio_is_valid(boarddata->cd_gpio))) {
128 if (gpio_get_value(boarddata->cd_gpio))
Wolfram Sang7e29c302011-02-26 14:44:41 +0100129 /* no card, if a valid gpio says so... */
Shawn Guo803862a2011-06-21 22:41:49 +0800130 val &= ~SDHCI_CARD_PRESENT;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100131 else
132 /* ... in all other cases assume card is present */
133 val |= SDHCI_CARD_PRESENT;
134 }
135
136 return val;
137}
138
139static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
140{
Richard Zhue1498602011-03-25 09:18:27 -0400141 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
142 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800143 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Richard Zhue1498602011-03-25 09:18:27 -0400144
145 if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)
Shawn Guo913413c2011-06-21 22:41:51 +0800146 && (boarddata->cd_type == ESDHC_CD_GPIO)))
Wolfram Sang7e29c302011-02-26 14:44:41 +0100147 /*
148 * these interrupts won't work with a custom card_detect gpio
Wolfram Sang7e29c302011-02-26 14:44:41 +0100149 */
150 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
151
Richard Zhu58ac8172011-03-21 13:22:16 +0800152 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 Sang7e29c302011-02-26 14:44:41 +0100161 writel(val, host->ioaddr + reg);
162}
163
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200164static 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
172static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
173{
174 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400175 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200176
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 Zhu58ac8172011-03-21 13:22:16 +0800183 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 Zhue1498602011-03-25 09:18:27 -0400192 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200193 return;
194 case SDHCI_COMMAND:
Richard Zhu58ac8172011-03-21 13:22:16 +0800195 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION)
196 && (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
197 val |= SDHCI_CMD_ABORTCMD;
Richard Zhue1498602011-03-25 09:18:27 -0400198 writel(val << 16 | imx_data->scratchpad,
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200199 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
208static 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 Guo913413c2011-06-21 22:41:51 +0800231
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 Sang95f25ef2010-10-15 12:21:04 +0200242}
243
244static 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
251static 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 Guo913413c2011-06-21 22:41:51 +0800258static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
259{
Shawn Guo842afc02011-07-06 22:57:48 +0800260 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 Guo913413c2011-06-21 22:41:51 +0800263
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 Sang0c6d49c2011-02-26 14:44:39 +0100278static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400279 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100280 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400281 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100282 .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 Guo913413c2011-06-21 22:41:51 +0800287 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100288};
289
Shawn Guo85d65092011-05-27 23:48:12 +0800290static 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 Sang7e29c302011-02-26 14:44:41 +0100297static 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 Guoabfafc22011-06-30 15:44:44 +0800305#ifdef CONFIG_OF
306static int __devinit
307sdhci_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
335static inline int
336sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
337 struct esdhc_platform_data *boarddata)
338{
339 return -ENODEV;
340}
341#endif
342
Shawn Guo85d65092011-05-27 23:48:12 +0800343static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200344{
Shawn Guoabfafc22011-06-30 15:44:44 +0800345 const struct of_device_id *of_id =
346 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800347 struct sdhci_pltfm_host *pltfm_host;
348 struct sdhci_host *host;
349 struct esdhc_platform_data *boarddata;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200350 struct clk *clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100351 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400352 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200353
Shawn Guo85d65092011-05-27 23:48:12 +0800354 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 Guoabfafc22011-06-30 15:44:44 +0800361 if (!imx_data) {
362 err = -ENOMEM;
363 goto err_imx_data;
364 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800365
Shawn Guoabfafc22011-06-30 15:44:44 +0800366 if (of_id)
367 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800368 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800369 pltfm_host->priv = imx_data;
370
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200371 clk = clk_get(mmc_dev(host->mmc), NULL);
372 if (IS_ERR(clk)) {
373 dev_err(mmc_dev(host->mmc), "clk err\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800374 err = PTR_ERR(clk);
375 goto err_clk_get;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200376 }
377 clk_enable(clk);
378 pltfm_host->clk = clk;
379
Shawn Guo57ed3312011-06-30 09:24:26 +0800380 if (!is_imx25_esdhc(imx_data))
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200381 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
382
Shawn Guo57ed3312011-06-30 09:24:26 +0800383 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100384 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200385 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100386
Shawn Guo57ed3312011-06-30 09:24:26 +0800387 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800388 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
389
Shawn Guo842afc02011-07-06 22:57:48 +0800390 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800391 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 Guo913413c2011-06-21 22:41:51 +0800400
401 /* write_protect */
402 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100403 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
404 if (err) {
405 dev_warn(mmc_dev(host->mmc),
Shawn Guo913413c2011-06-21 22:41:51 +0800406 "no write-protect pin available!\n");
407 boarddata->wp_gpio = -EINVAL;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100408 }
Shawn Guo913413c2011-06-21 22:41:51 +0800409 } else {
410 boarddata->wp_gpio = -EINVAL;
411 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100412
Shawn Guo913413c2011-06-21 22:41:51 +0800413 /* 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 Sang7e29c302011-02-26 14:44:41 +0100419 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
420 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800421 dev_err(mmc_dev(host->mmc),
Wolfram Sang7e29c302011-02-26 14:44:41 +0100422 "no card-detect pin available!\n");
423 goto no_card_detect_pin;
424 }
425
Wolfram Sang7e29c302011-02-26 14:44:41 +0100426 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 Guo913413c2011-06-21 22:41:51 +0800430 dev_err(mmc_dev(host->mmc), "request irq error\n");
Wolfram Sang7e29c302011-02-26 14:44:41 +0100431 goto no_card_detect_irq;
432 }
Shawn Guo913413c2011-06-21 22:41:51 +0800433 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100434
Shawn Guo913413c2011-06-21 22:41:51 +0800435 case ESDHC_CD_CONTROLLER:
436 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100437 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800438 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 Sang0c6d49c2011-02-26 14:44:39 +0100446 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200447
Shawn Guo85d65092011-05-27 23:48:12 +0800448 err = sdhci_add_host(host);
449 if (err)
450 goto err_add_host;
451
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200452 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100453
Shawn Guo913413c2011-06-21 22:41:51 +0800454err_add_host:
455 if (gpio_is_valid(boarddata->cd_gpio))
456 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
457no_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);
462no_card_detect_pin:
463no_board_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800464 clk_disable(pltfm_host->clk);
465 clk_put(pltfm_host->clk);
Shawn Guo913413c2011-06-21 22:41:51 +0800466err_clk_get:
467 kfree(imx_data);
Shawn Guoabfafc22011-06-30 15:44:44 +0800468err_imx_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800469 sdhci_pltfm_free(pdev);
470 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200471}
472
Shawn Guo85d65092011-05-27 23:48:12 +0800473static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200474{
Shawn Guo85d65092011-05-27 23:48:12 +0800475 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200476 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400477 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800478 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo85d65092011-05-27 23:48:12 +0800479 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
480
481 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100482
Shawn Guo913413c2011-06-21 22:41:51 +0800483 if (gpio_is_valid(boarddata->wp_gpio))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100484 gpio_free(boarddata->wp_gpio);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200485
Shawn Guo913413c2011-06-21 22:41:51 +0800486 if (gpio_is_valid(boarddata->cd_gpio)) {
487 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100488 gpio_free(boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100489 }
490
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200491 clk_disable(pltfm_host->clk);
492 clk_put(pltfm_host->clk);
Richard Zhue1498602011-03-25 09:18:27 -0400493 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800494
495 sdhci_pltfm_free(pdev);
496
497 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200498}
499
Shawn Guo85d65092011-05-27 23:48:12 +0800500static struct platform_driver sdhci_esdhc_imx_driver = {
501 .driver = {
502 .name = "sdhci-esdhc-imx",
503 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800504 .of_match_table = imx_esdhc_dt_ids,
Shawn Guo85d65092011-05-27 23:48:12 +0800505 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800506 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800507 .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 Sang95f25ef2010-10-15 12:21:04 +0200513};
Shawn Guo85d65092011-05-27 23:48:12 +0800514
515static int __init sdhci_esdhc_imx_init(void)
516{
517 return platform_driver_register(&sdhci_esdhc_imx_driver);
518}
519module_init(sdhci_esdhc_imx_init);
520
521static void __exit sdhci_esdhc_imx_exit(void)
522{
523 platform_driver_unregister(&sdhci_esdhc_imx_driver);
524}
525module_exit(sdhci_esdhc_imx_exit);
526
527MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
528MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
529MODULE_LICENSE("GPL v2");