blob: 0be4e2013632f97ce4dddc489212c2ed6f7c4985 [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
Shawn Guof750ba92011-11-10 16:39:32 +080035#define SDHCI_WTMK_LVL 0x44
Shawn Guo95a24822011-09-19 17:32:21 +080036#define SDHCI_MIX_CTRL 0x48
Richard Zhu58ac8172011-03-21 13:22:16 +080037
Richard Zhu58ac8172011-03-21 13:22:16 +080038/*
Richard Zhu97e4ba62011-08-11 16:51:46 -040039 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
40 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
41 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
42 * Define this macro DMA error INT for fsl eSDHC
43 */
44#define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
45
46/*
Richard Zhu58ac8172011-03-21 13:22:16 +080047 * The CMDTYPE of the CMD register (offset 0xE) should be set to
48 * "11" when the STOP CMD12 is issued on imx53 to abort one
49 * open ended multi-blk IO. Otherwise the TC INT wouldn't
50 * be generated.
51 * In exact block transfer, the controller doesn't complete the
52 * operations automatically as required at the end of the
53 * transfer and remains on hold if the abort command is not sent.
54 * As a result, the TC flag is not asserted and SW received timeout
55 * exeception. Bit1 of Vendor Spec registor is used to fix it.
56 */
57#define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
Richard Zhue1498602011-03-25 09:18:27 -040058
Shawn Guo57ed3312011-06-30 09:24:26 +080059enum imx_esdhc_type {
60 IMX25_ESDHC,
61 IMX35_ESDHC,
62 IMX51_ESDHC,
63 IMX53_ESDHC,
Shawn Guo95a24822011-09-19 17:32:21 +080064 IMX6Q_USDHC,
Shawn Guo57ed3312011-06-30 09:24:26 +080065};
66
Richard Zhue1498602011-03-25 09:18:27 -040067struct pltfm_imx_data {
68 int flags;
69 u32 scratchpad;
Shawn Guo57ed3312011-06-30 09:24:26 +080070 enum imx_esdhc_type devtype;
Shawn Guo842afc02011-07-06 22:57:48 +080071 struct esdhc_platform_data boarddata;
Richard Zhue1498602011-03-25 09:18:27 -040072};
73
Shawn Guo57ed3312011-06-30 09:24:26 +080074static struct platform_device_id imx_esdhc_devtype[] = {
75 {
76 .name = "sdhci-esdhc-imx25",
77 .driver_data = IMX25_ESDHC,
78 }, {
79 .name = "sdhci-esdhc-imx35",
80 .driver_data = IMX35_ESDHC,
81 }, {
82 .name = "sdhci-esdhc-imx51",
83 .driver_data = IMX51_ESDHC,
84 }, {
85 .name = "sdhci-esdhc-imx53",
86 .driver_data = IMX53_ESDHC,
87 }, {
Shawn Guo95a24822011-09-19 17:32:21 +080088 .name = "sdhci-usdhc-imx6q",
89 .driver_data = IMX6Q_USDHC,
90 }, {
Shawn Guo57ed3312011-06-30 09:24:26 +080091 /* sentinel */
92 }
93};
94MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
95
Shawn Guoabfafc22011-06-30 15:44:44 +080096static const struct of_device_id imx_esdhc_dt_ids[] = {
97 { .compatible = "fsl,imx25-esdhc", .data = &imx_esdhc_devtype[IMX25_ESDHC], },
98 { .compatible = "fsl,imx35-esdhc", .data = &imx_esdhc_devtype[IMX35_ESDHC], },
99 { .compatible = "fsl,imx51-esdhc", .data = &imx_esdhc_devtype[IMX51_ESDHC], },
100 { .compatible = "fsl,imx53-esdhc", .data = &imx_esdhc_devtype[IMX53_ESDHC], },
Shawn Guo95a24822011-09-19 17:32:21 +0800101 { .compatible = "fsl,imx6q-usdhc", .data = &imx_esdhc_devtype[IMX6Q_USDHC], },
Shawn Guoabfafc22011-06-30 15:44:44 +0800102 { /* sentinel */ }
103};
104MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
105
Shawn Guo57ed3312011-06-30 09:24:26 +0800106static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
107{
108 return data->devtype == IMX25_ESDHC;
109}
110
111static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
112{
113 return data->devtype == IMX35_ESDHC;
114}
115
116static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
117{
118 return data->devtype == IMX51_ESDHC;
119}
120
121static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
122{
123 return data->devtype == IMX53_ESDHC;
124}
125
Shawn Guo95a24822011-09-19 17:32:21 +0800126static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
127{
128 return data->devtype == IMX6Q_USDHC;
129}
130
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200131static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
132{
133 void __iomem *base = host->ioaddr + (reg & ~0x3);
134 u32 shift = (reg & 0x3) * 8;
135
136 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
137}
138
Wolfram Sang7e29c302011-02-26 14:44:41 +0100139static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
140{
Shawn Guo842afc02011-07-06 22:57:48 +0800141 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
142 struct pltfm_imx_data *imx_data = pltfm_host->priv;
143 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Richard Zhue1498602011-03-25 09:18:27 -0400144
Shawn Guo913413c2011-06-21 22:41:51 +0800145 /* fake CARD_PRESENT flag */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100146 u32 val = readl(host->ioaddr + reg);
147
Richard Zhue1498602011-03-25 09:18:27 -0400148 if (unlikely((reg == SDHCI_PRESENT_STATE)
Shawn Guo913413c2011-06-21 22:41:51 +0800149 && gpio_is_valid(boarddata->cd_gpio))) {
150 if (gpio_get_value(boarddata->cd_gpio))
Wolfram Sang7e29c302011-02-26 14:44:41 +0100151 /* no card, if a valid gpio says so... */
Shawn Guo803862a2011-06-21 22:41:49 +0800152 val &= ~SDHCI_CARD_PRESENT;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100153 else
154 /* ... in all other cases assume card is present */
155 val |= SDHCI_CARD_PRESENT;
156 }
157
Richard Zhu97e4ba62011-08-11 16:51:46 -0400158 if (unlikely(reg == SDHCI_CAPABILITIES)) {
159 /* In FSL esdhc IC module, only bit20 is used to indicate the
160 * ADMA2 capability of esdhc, but this bit is messed up on
161 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
162 * don't actually support ADMA2). So set the BROKEN_ADMA
163 * uirk on MX25/35 platforms.
164 */
165
166 if (val & SDHCI_CAN_DO_ADMA1) {
167 val &= ~SDHCI_CAN_DO_ADMA1;
168 val |= SDHCI_CAN_DO_ADMA2;
169 }
170 }
171
172 if (unlikely(reg == SDHCI_INT_STATUS)) {
173 if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) {
174 val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR;
175 val |= SDHCI_INT_ADMA_ERROR;
176 }
177 }
178
Wolfram Sang7e29c302011-02-26 14:44:41 +0100179 return val;
180}
181
182static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
183{
Richard Zhue1498602011-03-25 09:18:27 -0400184 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
185 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800186 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Tony Lin0d588642011-08-11 16:45:59 -0400187 u32 data;
Richard Zhue1498602011-03-25 09:18:27 -0400188
Tony Lin0d588642011-08-11 16:45:59 -0400189 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
190 if (boarddata->cd_type == ESDHC_CD_GPIO)
191 /*
192 * These interrupts won't work with a custom
193 * card_detect gpio (only applied to mx25/35)
194 */
195 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
196
197 if (val & SDHCI_INT_CARD_INT) {
198 /*
199 * Clear and then set D3CD bit to avoid missing the
200 * card interrupt. This is a eSDHC controller problem
201 * so we need to apply the following workaround: clear
202 * and set D3CD bit will make eSDHC re-sample the card
203 * interrupt. In case a card interrupt was lost,
204 * re-sample it by the following steps.
205 */
206 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
207 data &= ~SDHCI_CTRL_D3CD;
208 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
209 data |= SDHCI_CTRL_D3CD;
210 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
211 }
212 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100213
Richard Zhu58ac8172011-03-21 13:22:16 +0800214 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
215 && (reg == SDHCI_INT_STATUS)
216 && (val & SDHCI_INT_DATA_END))) {
217 u32 v;
218 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
219 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
220 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
221 }
222
Richard Zhu97e4ba62011-08-11 16:51:46 -0400223 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
224 if (val & SDHCI_INT_ADMA_ERROR) {
225 val &= ~SDHCI_INT_ADMA_ERROR;
226 val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR;
227 }
228 }
229
Wolfram Sang7e29c302011-02-26 14:44:41 +0100230 writel(val, host->ioaddr + reg);
231}
232
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200233static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
234{
Shawn Guo95a24822011-09-19 17:32:21 +0800235 if (unlikely(reg == SDHCI_HOST_VERSION)) {
236 u16 val = readw(host->ioaddr + (reg ^ 2));
237 /*
238 * uSDHC supports SDHCI v3.0, but it's encoded as value
239 * 0x3 in host controller version register, which violates
240 * SDHCI_SPEC_300 definition. Work it around here.
241 */
242 if ((val & SDHCI_SPEC_VER_MASK) == 3)
243 return --val;
244 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200245
246 return readw(host->ioaddr + reg);
247}
248
249static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
250{
251 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400252 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200253
254 switch (reg) {
255 case SDHCI_TRANSFER_MODE:
256 /*
257 * Postpone this write, we must do it together with a
258 * command write that is down below.
259 */
Richard Zhu58ac8172011-03-21 13:22:16 +0800260 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
261 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
262 && (host->cmd->data->blocks > 1)
263 && (host->cmd->data->flags & MMC_DATA_READ)) {
264 u32 v;
265 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
266 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
267 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
268 }
Richard Zhue1498602011-03-25 09:18:27 -0400269 imx_data->scratchpad = val;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200270 return;
271 case SDHCI_COMMAND:
Sascha Hauer5b6b0ad2012-02-17 11:51:49 +0100272 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
273 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
274 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
Richard Zhu58ac8172011-03-21 13:22:16 +0800275 val |= SDHCI_CMD_ABORTCMD;
Shawn Guo95a24822011-09-19 17:32:21 +0800276
277 if (is_imx6q_usdhc(imx_data)) {
278 u32 m = readl(host->ioaddr + SDHCI_MIX_CTRL);
279 m = imx_data->scratchpad | (m & 0xffff0000);
280 writel(m, host->ioaddr + SDHCI_MIX_CTRL);
281 writel(val << 16,
282 host->ioaddr + SDHCI_TRANSFER_MODE);
283 } else {
284 writel(val << 16 | imx_data->scratchpad,
285 host->ioaddr + SDHCI_TRANSFER_MODE);
286 }
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200287 return;
288 case SDHCI_BLOCK_SIZE:
289 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
290 break;
291 }
292 esdhc_clrset_le(host, 0xffff, val, reg);
293}
294
295static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
296{
297 u32 new_val;
298
299 switch (reg) {
300 case SDHCI_POWER_CONTROL:
301 /*
302 * FSL put some DMA bits here
303 * If your board has a regulator, code should be here
304 */
305 return;
306 case SDHCI_HOST_CONTROL:
Tony Lin0d588642011-08-11 16:45:59 -0400307 /* FSL messed up here, so we can just keep those three */
308 new_val = val & (SDHCI_CTRL_LED | \
309 SDHCI_CTRL_4BITBUS | \
310 SDHCI_CTRL_D3CD);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200311 /* ensure the endianess */
312 new_val |= ESDHC_HOST_CONTROL_LE;
313 /* DMA mode bits are shifted */
314 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
315
316 esdhc_clrset_le(host, 0xffff, new_val, reg);
317 return;
318 }
319 esdhc_clrset_le(host, 0xff, val, reg);
Shawn Guo913413c2011-06-21 22:41:51 +0800320
321 /*
322 * The esdhc has a design violation to SDHC spec which tells
323 * that software reset should not affect card detection circuit.
324 * But esdhc clears its SYSCTL register bits [0..2] during the
325 * software reset. This will stop those clocks that card detection
326 * circuit relies on. To work around it, we turn the clocks on back
327 * to keep card detection circuit functional.
328 */
329 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1))
330 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200331}
332
333static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
334{
335 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
336
337 return clk_get_rate(pltfm_host->clk);
338}
339
340static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
341{
342 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
343
344 return clk_get_rate(pltfm_host->clk) / 256 / 16;
345}
346
Shawn Guo913413c2011-06-21 22:41:51 +0800347static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
348{
Shawn Guo842afc02011-07-06 22:57:48 +0800349 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
350 struct pltfm_imx_data *imx_data = pltfm_host->priv;
351 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo913413c2011-06-21 22:41:51 +0800352
353 switch (boarddata->wp_type) {
354 case ESDHC_WP_GPIO:
355 if (gpio_is_valid(boarddata->wp_gpio))
356 return gpio_get_value(boarddata->wp_gpio);
357 case ESDHC_WP_CONTROLLER:
358 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
359 SDHCI_WRITE_PROTECT);
360 case ESDHC_WP_NONE:
361 break;
362 }
363
364 return -ENOSYS;
365}
366
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100367static struct sdhci_ops sdhci_esdhc_ops = {
Richard Zhue1498602011-03-25 09:18:27 -0400368 .read_l = esdhc_readl_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100369 .read_w = esdhc_readw_le,
Richard Zhue1498602011-03-25 09:18:27 -0400370 .write_l = esdhc_writel_le,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100371 .write_w = esdhc_writew_le,
372 .write_b = esdhc_writeb_le,
373 .set_clock = esdhc_set_clock,
374 .get_max_clock = esdhc_pltfm_get_max_clock,
375 .get_min_clock = esdhc_pltfm_get_min_clock,
Shawn Guo913413c2011-06-21 22:41:51 +0800376 .get_ro = esdhc_pltfm_get_ro,
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100377};
378
Shawn Guo85d65092011-05-27 23:48:12 +0800379static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
Richard Zhu97e4ba62011-08-11 16:51:46 -0400380 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
381 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
382 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
Shawn Guo85d65092011-05-27 23:48:12 +0800383 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Shawn Guo85d65092011-05-27 23:48:12 +0800384 .ops = &sdhci_esdhc_ops,
385};
386
Wolfram Sang7e29c302011-02-26 14:44:41 +0100387static irqreturn_t cd_irq(int irq, void *data)
388{
389 struct sdhci_host *sdhost = (struct sdhci_host *)data;
390
391 tasklet_schedule(&sdhost->card_tasklet);
392 return IRQ_HANDLED;
393};
394
Shawn Guoabfafc22011-06-30 15:44:44 +0800395#ifdef CONFIG_OF
396static int __devinit
397sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
398 struct esdhc_platform_data *boarddata)
399{
400 struct device_node *np = pdev->dev.of_node;
401
402 if (!np)
403 return -ENODEV;
404
405 if (of_get_property(np, "fsl,card-wired", NULL))
406 boarddata->cd_type = ESDHC_CD_PERMANENT;
407
408 if (of_get_property(np, "fsl,cd-controller", NULL))
409 boarddata->cd_type = ESDHC_CD_CONTROLLER;
410
411 if (of_get_property(np, "fsl,wp-controller", NULL))
412 boarddata->wp_type = ESDHC_WP_CONTROLLER;
413
414 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
415 if (gpio_is_valid(boarddata->cd_gpio))
416 boarddata->cd_type = ESDHC_CD_GPIO;
417
418 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
419 if (gpio_is_valid(boarddata->wp_gpio))
420 boarddata->wp_type = ESDHC_WP_GPIO;
421
422 return 0;
423}
424#else
425static inline int
426sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
427 struct esdhc_platform_data *boarddata)
428{
429 return -ENODEV;
430}
431#endif
432
Shawn Guo85d65092011-05-27 23:48:12 +0800433static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200434{
Shawn Guoabfafc22011-06-30 15:44:44 +0800435 const struct of_device_id *of_id =
436 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
Shawn Guo85d65092011-05-27 23:48:12 +0800437 struct sdhci_pltfm_host *pltfm_host;
438 struct sdhci_host *host;
439 struct esdhc_platform_data *boarddata;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200440 struct clk *clk;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100441 int err;
Richard Zhue1498602011-03-25 09:18:27 -0400442 struct pltfm_imx_data *imx_data;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200443
Shawn Guo85d65092011-05-27 23:48:12 +0800444 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata);
445 if (IS_ERR(host))
446 return PTR_ERR(host);
447
448 pltfm_host = sdhci_priv(host);
449
450 imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
Shawn Guoabfafc22011-06-30 15:44:44 +0800451 if (!imx_data) {
452 err = -ENOMEM;
453 goto err_imx_data;
454 }
Shawn Guo57ed3312011-06-30 09:24:26 +0800455
Shawn Guoabfafc22011-06-30 15:44:44 +0800456 if (of_id)
457 pdev->id_entry = of_id->data;
Shawn Guo57ed3312011-06-30 09:24:26 +0800458 imx_data->devtype = pdev->id_entry->driver_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800459 pltfm_host->priv = imx_data;
460
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200461 clk = clk_get(mmc_dev(host->mmc), NULL);
462 if (IS_ERR(clk)) {
463 dev_err(mmc_dev(host->mmc), "clk err\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800464 err = PTR_ERR(clk);
465 goto err_clk_get;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200466 }
467 clk_enable(clk);
468 pltfm_host->clk = clk;
469
Shawn Guo57ed3312011-06-30 09:24:26 +0800470 if (!is_imx25_esdhc(imx_data))
Eric BĂ©nard37865fe2010-10-23 01:57:21 +0200471 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
472
Shawn Guo57ed3312011-06-30 09:24:26 +0800473 if (is_imx25_esdhc(imx_data) || is_imx35_esdhc(imx_data))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100474 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
Richard Zhu97e4ba62011-08-11 16:51:46 -0400475 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
476 | SDHCI_QUIRK_BROKEN_ADMA;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100477
Shawn Guo57ed3312011-06-30 09:24:26 +0800478 if (is_imx53_esdhc(imx_data))
Richard Zhu58ac8172011-03-21 13:22:16 +0800479 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
480
Shawn Guof750ba92011-11-10 16:39:32 +0800481 /*
482 * The imx6q ROM code will change the default watermark level setting
483 * to something insane. Change it back here.
484 */
485 if (is_imx6q_usdhc(imx_data))
486 writel(0x08100810, host->ioaddr + SDHCI_WTMK_LVL);
487
Shawn Guo842afc02011-07-06 22:57:48 +0800488 boarddata = &imx_data->boarddata;
Shawn Guoabfafc22011-06-30 15:44:44 +0800489 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
490 if (!host->mmc->parent->platform_data) {
491 dev_err(mmc_dev(host->mmc), "no board data!\n");
492 err = -EINVAL;
493 goto no_board_data;
494 }
495 imx_data->boarddata = *((struct esdhc_platform_data *)
496 host->mmc->parent->platform_data);
497 }
Shawn Guo913413c2011-06-21 22:41:51 +0800498
499 /* write_protect */
500 if (boarddata->wp_type == ESDHC_WP_GPIO) {
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100501 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
502 if (err) {
503 dev_warn(mmc_dev(host->mmc),
Shawn Guo913413c2011-06-21 22:41:51 +0800504 "no write-protect pin available!\n");
505 boarddata->wp_gpio = -EINVAL;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100506 }
Shawn Guo913413c2011-06-21 22:41:51 +0800507 } else {
508 boarddata->wp_gpio = -EINVAL;
509 }
Wolfram Sang7e29c302011-02-26 14:44:41 +0100510
Shawn Guo913413c2011-06-21 22:41:51 +0800511 /* card_detect */
512 if (boarddata->cd_type != ESDHC_CD_GPIO)
513 boarddata->cd_gpio = -EINVAL;
514
515 switch (boarddata->cd_type) {
516 case ESDHC_CD_GPIO:
Wolfram Sang7e29c302011-02-26 14:44:41 +0100517 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
518 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800519 dev_err(mmc_dev(host->mmc),
Wolfram Sang7e29c302011-02-26 14:44:41 +0100520 "no card-detect pin available!\n");
521 goto no_card_detect_pin;
522 }
523
Wolfram Sang7e29c302011-02-26 14:44:41 +0100524 err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
525 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
526 mmc_hostname(host->mmc), host);
527 if (err) {
Shawn Guo913413c2011-06-21 22:41:51 +0800528 dev_err(mmc_dev(host->mmc), "request irq error\n");
Wolfram Sang7e29c302011-02-26 14:44:41 +0100529 goto no_card_detect_irq;
530 }
Shawn Guo913413c2011-06-21 22:41:51 +0800531 /* fall through */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100532
Shawn Guo913413c2011-06-21 22:41:51 +0800533 case ESDHC_CD_CONTROLLER:
534 /* we have a working card_detect back */
Wolfram Sang7e29c302011-02-26 14:44:41 +0100535 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
Shawn Guo913413c2011-06-21 22:41:51 +0800536 break;
537
538 case ESDHC_CD_PERMANENT:
539 host->mmc->caps = MMC_CAP_NONREMOVABLE;
540 break;
541
542 case ESDHC_CD_NONE:
543 break;
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100544 }
Eric BĂ©nard16a790b2010-10-23 01:57:22 +0200545
Shawn Guo85d65092011-05-27 23:48:12 +0800546 err = sdhci_add_host(host);
547 if (err)
548 goto err_add_host;
549
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200550 return 0;
Wolfram Sang7e29c302011-02-26 14:44:41 +0100551
Shawn Guo913413c2011-06-21 22:41:51 +0800552err_add_host:
553 if (gpio_is_valid(boarddata->cd_gpio))
554 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
555no_card_detect_irq:
556 if (gpio_is_valid(boarddata->cd_gpio))
557 gpio_free(boarddata->cd_gpio);
558 if (gpio_is_valid(boarddata->wp_gpio))
559 gpio_free(boarddata->wp_gpio);
560no_card_detect_pin:
561no_board_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800562 clk_disable(pltfm_host->clk);
563 clk_put(pltfm_host->clk);
Shawn Guo913413c2011-06-21 22:41:51 +0800564err_clk_get:
565 kfree(imx_data);
Shawn Guoabfafc22011-06-30 15:44:44 +0800566err_imx_data:
Shawn Guo85d65092011-05-27 23:48:12 +0800567 sdhci_pltfm_free(pdev);
568 return err;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200569}
570
Shawn Guo85d65092011-05-27 23:48:12 +0800571static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200572{
Shawn Guo85d65092011-05-27 23:48:12 +0800573 struct sdhci_host *host = platform_get_drvdata(pdev);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200574 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Richard Zhue1498602011-03-25 09:18:27 -0400575 struct pltfm_imx_data *imx_data = pltfm_host->priv;
Shawn Guo842afc02011-07-06 22:57:48 +0800576 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
Shawn Guo85d65092011-05-27 23:48:12 +0800577 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
578
579 sdhci_remove_host(host, dead);
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100580
Shawn Guo913413c2011-06-21 22:41:51 +0800581 if (gpio_is_valid(boarddata->wp_gpio))
Wolfram Sang0c6d49c2011-02-26 14:44:39 +0100582 gpio_free(boarddata->wp_gpio);
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200583
Shawn Guo913413c2011-06-21 22:41:51 +0800584 if (gpio_is_valid(boarddata->cd_gpio)) {
585 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100586 gpio_free(boarddata->cd_gpio);
Wolfram Sang7e29c302011-02-26 14:44:41 +0100587 }
588
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200589 clk_disable(pltfm_host->clk);
590 clk_put(pltfm_host->clk);
Richard Zhue1498602011-03-25 09:18:27 -0400591 kfree(imx_data);
Shawn Guo85d65092011-05-27 23:48:12 +0800592
593 sdhci_pltfm_free(pdev);
594
595 return 0;
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200596}
597
Shawn Guo85d65092011-05-27 23:48:12 +0800598static struct platform_driver sdhci_esdhc_imx_driver = {
599 .driver = {
600 .name = "sdhci-esdhc-imx",
601 .owner = THIS_MODULE,
Shawn Guoabfafc22011-06-30 15:44:44 +0800602 .of_match_table = imx_esdhc_dt_ids,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100603 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800604 },
Shawn Guo57ed3312011-06-30 09:24:26 +0800605 .id_table = imx_esdhc_devtype,
Shawn Guo85d65092011-05-27 23:48:12 +0800606 .probe = sdhci_esdhc_imx_probe,
607 .remove = __devexit_p(sdhci_esdhc_imx_remove),
Wolfram Sang95f25ef2010-10-15 12:21:04 +0200608};
Shawn Guo85d65092011-05-27 23:48:12 +0800609
Axel Lind1f81a62011-11-26 12:55:43 +0800610module_platform_driver(sdhci_esdhc_imx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800611
612MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
613MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
614MODULE_LICENSE("GPL v2");