2 * Freescale eSDHC i.MX controller driver for the platform bus.
4 * derived from the OF-version.
6 * Copyright (c) 2010 Pengutronix e.K.
7 * Author: Wolfram Sang <w.sang@pengutronix.de>
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.
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
19 #include <linux/slab.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/sdio.h>
24 #include <linux/of_device.h>
25 #include <linux/of_gpio.h>
26 #include <mach/esdhc.h>
27 #include "sdhci-pltfm.h"
28 #include "sdhci-esdhc.h"
30 /* VENDOR SPEC register */
31 #define SDHCI_VENDOR_SPEC 0xC0
32 #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
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
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.
45 #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
54 struct pltfm_imx_data
{
57 enum imx_esdhc_type devtype
;
58 struct esdhc_platform_data boarddata
;
61 static struct platform_device_id imx_esdhc_devtype
[] = {
63 .name
= "sdhci-esdhc-imx25",
64 .driver_data
= IMX25_ESDHC
,
66 .name
= "sdhci-esdhc-imx35",
67 .driver_data
= IMX35_ESDHC
,
69 .name
= "sdhci-esdhc-imx51",
70 .driver_data
= IMX51_ESDHC
,
72 .name
= "sdhci-esdhc-imx53",
73 .driver_data
= IMX53_ESDHC
,
78 MODULE_DEVICE_TABLE(platform
, imx_esdhc_devtype
);
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
], },
87 MODULE_DEVICE_TABLE(of
, imx_esdhc_dt_ids
);
89 static inline int is_imx25_esdhc(struct pltfm_imx_data
*data
)
91 return data
->devtype
== IMX25_ESDHC
;
94 static inline int is_imx35_esdhc(struct pltfm_imx_data
*data
)
96 return data
->devtype
== IMX35_ESDHC
;
99 static inline int is_imx51_esdhc(struct pltfm_imx_data
*data
)
101 return data
->devtype
== IMX51_ESDHC
;
104 static inline int is_imx53_esdhc(struct pltfm_imx_data
*data
)
106 return data
->devtype
== IMX53_ESDHC
;
109 static inline void esdhc_clrset_le(struct sdhci_host
*host
, u32 mask
, u32 val
, int reg
)
111 void __iomem
*base
= host
->ioaddr
+ (reg
& ~0x3);
112 u32 shift
= (reg
& 0x3) * 8;
114 writel(((readl(base
) & ~(mask
<< shift
)) | (val
<< shift
)), base
);
117 static u32
esdhc_readl_le(struct sdhci_host
*host
, int reg
)
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
;
123 /* fake CARD_PRESENT flag */
124 u32 val
= readl(host
->ioaddr
+ reg
);
126 if (unlikely((reg
== SDHCI_PRESENT_STATE
)
127 && gpio_is_valid(boarddata
->cd_gpio
))) {
128 if (gpio_get_value(boarddata
->cd_gpio
))
129 /* no card, if a valid gpio says so... */
130 val
&= ~SDHCI_CARD_PRESENT
;
132 /* ... in all other cases assume card is present */
133 val
|= SDHCI_CARD_PRESENT
;
139 static void esdhc_writel_le(struct sdhci_host
*host
, u32 val
, int reg
)
141 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
;
145 if (unlikely((reg
== SDHCI_INT_ENABLE
|| reg
== SDHCI_SIGNAL_ENABLE
)
146 && (boarddata
->cd_type
== ESDHC_CD_GPIO
)))
148 * these interrupts won't work with a custom card_detect gpio
150 val
&= ~(SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
);
152 if (unlikely((imx_data
->flags
& ESDHC_FLAG_MULTIBLK_NO_INT
)
153 && (reg
== SDHCI_INT_STATUS
)
154 && (val
& SDHCI_INT_DATA_END
))) {
156 v
= readl(host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
157 v
&= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK
;
158 writel(v
, host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
161 writel(val
, host
->ioaddr
+ reg
);
164 static u16
esdhc_readw_le(struct sdhci_host
*host
, int reg
)
166 if (unlikely(reg
== SDHCI_HOST_VERSION
))
169 return readw(host
->ioaddr
+ reg
);
172 static void esdhc_writew_le(struct sdhci_host
*host
, u16 val
, int reg
)
174 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
175 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
178 case SDHCI_TRANSFER_MODE
:
180 * Postpone this write, we must do it together with a
181 * command write that is down below.
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
)) {
188 v
= readl(host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
189 v
|= SDHCI_VENDOR_SPEC_SDIO_QUIRK
;
190 writel(v
, host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
192 imx_data
->scratchpad
= val
;
195 if ((host
->cmd
->opcode
== MMC_STOP_TRANSMISSION
)
196 && (imx_data
->flags
& ESDHC_FLAG_MULTIBLK_NO_INT
))
197 val
|= SDHCI_CMD_ABORTCMD
;
198 writel(val
<< 16 | imx_data
->scratchpad
,
199 host
->ioaddr
+ SDHCI_TRANSFER_MODE
);
201 case SDHCI_BLOCK_SIZE
:
202 val
&= ~SDHCI_MAKE_BLKSZ(0x7, 0);
205 esdhc_clrset_le(host
, 0xffff, val
, reg
);
208 static void esdhc_writeb_le(struct sdhci_host
*host
, u8 val
, int reg
)
213 case SDHCI_POWER_CONTROL
:
215 * FSL put some DMA bits here
216 * If your board has a regulator, code should be here
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;
227 esdhc_clrset_le(host
, 0xffff, new_val
, reg
);
230 esdhc_clrset_le(host
, 0xff, val
, reg
);
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.
240 if ((reg
== SDHCI_SOFTWARE_RESET
) && (val
& 1))
241 esdhc_clrset_le(host
, 0x7, 0x7, ESDHC_SYSTEM_CONTROL
);
244 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host
*host
)
246 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
248 return clk_get_rate(pltfm_host
->clk
);
251 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host
*host
)
253 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
255 return clk_get_rate(pltfm_host
->clk
) / 256 / 16;
258 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host
*host
)
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
;
264 switch (boarddata
->wp_type
) {
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
);
278 static struct sdhci_ops sdhci_esdhc_ops
= {
279 .read_l
= esdhc_readl_le
,
280 .read_w
= esdhc_readw_le
,
281 .write_l
= esdhc_writel_le
,
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
,
287 .get_ro
= esdhc_pltfm_get_ro
,
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
,
297 static irqreturn_t
cd_irq(int irq
, void *data
)
299 struct sdhci_host
*sdhost
= (struct sdhci_host
*)data
;
301 tasklet_schedule(&sdhost
->card_tasklet
);
307 sdhci_esdhc_imx_probe_dt(struct platform_device
*pdev
,
308 struct esdhc_platform_data
*boarddata
)
310 struct device_node
*np
= pdev
->dev
.of_node
;
315 if (of_get_property(np
, "fsl,card-wired", NULL
))
316 boarddata
->cd_type
= ESDHC_CD_PERMANENT
;
318 if (of_get_property(np
, "fsl,cd-controller", NULL
))
319 boarddata
->cd_type
= ESDHC_CD_CONTROLLER
;
321 if (of_get_property(np
, "fsl,wp-controller", NULL
))
322 boarddata
->wp_type
= ESDHC_WP_CONTROLLER
;
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
;
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
;
336 sdhci_esdhc_imx_probe_dt(struct platform_device
*pdev
,
337 struct esdhc_platform_data
*boarddata
)
343 static int __devinit
sdhci_esdhc_imx_probe(struct platform_device
*pdev
)
345 const struct of_device_id
*of_id
=
346 of_match_device(imx_esdhc_dt_ids
, &pdev
->dev
);
347 struct sdhci_pltfm_host
*pltfm_host
;
348 struct sdhci_host
*host
;
349 struct esdhc_platform_data
*boarddata
;
352 struct pltfm_imx_data
*imx_data
;
354 host
= sdhci_pltfm_init(pdev
, &sdhci_esdhc_imx_pdata
);
356 return PTR_ERR(host
);
358 pltfm_host
= sdhci_priv(host
);
360 imx_data
= kzalloc(sizeof(struct pltfm_imx_data
), GFP_KERNEL
);
367 pdev
->id_entry
= of_id
->data
;
368 imx_data
->devtype
= pdev
->id_entry
->driver_data
;
369 pltfm_host
->priv
= imx_data
;
371 clk
= clk_get(mmc_dev(host
->mmc
), NULL
);
373 dev_err(mmc_dev(host
->mmc
), "clk err\n");
378 pltfm_host
->clk
= clk
;
380 if (!is_imx25_esdhc(imx_data
))
381 host
->quirks
|= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
;
383 if (is_imx25_esdhc(imx_data
) || is_imx35_esdhc(imx_data
))
384 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
385 host
->quirks
|= SDHCI_QUIRK_NO_MULTIBLOCK
;
387 if (is_imx53_esdhc(imx_data
))
388 imx_data
->flags
|= ESDHC_FLAG_MULTIBLK_NO_INT
;
390 boarddata
= &imx_data
->boarddata
;
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");
397 imx_data
->boarddata
= *((struct esdhc_platform_data
*)
398 host
->mmc
->parent
->platform_data
);
402 if (boarddata
->wp_type
== ESDHC_WP_GPIO
) {
403 err
= gpio_request_one(boarddata
->wp_gpio
, GPIOF_IN
, "ESDHC_WP");
405 dev_warn(mmc_dev(host
->mmc
),
406 "no write-protect pin available!\n");
407 boarddata
->wp_gpio
= -EINVAL
;
410 boarddata
->wp_gpio
= -EINVAL
;
414 if (boarddata
->cd_type
!= ESDHC_CD_GPIO
)
415 boarddata
->cd_gpio
= -EINVAL
;
417 switch (boarddata
->cd_type
) {
419 err
= gpio_request_one(boarddata
->cd_gpio
, GPIOF_IN
, "ESDHC_CD");
421 dev_err(mmc_dev(host
->mmc
),
422 "no card-detect pin available!\n");
423 goto no_card_detect_pin
;
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
);
430 dev_err(mmc_dev(host
->mmc
), "request irq error\n");
431 goto no_card_detect_irq
;
435 case ESDHC_CD_CONTROLLER
:
436 /* we have a working card_detect back */
437 host
->quirks
&= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION
;
440 case ESDHC_CD_PERMANENT
:
441 host
->mmc
->caps
= MMC_CAP_NONREMOVABLE
;
448 err
= sdhci_add_host(host
);
455 if (gpio_is_valid(boarddata
->cd_gpio
))
456 free_irq(gpio_to_irq(boarddata
->cd_gpio
), host
);
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
);
464 clk_disable(pltfm_host
->clk
);
465 clk_put(pltfm_host
->clk
);
469 sdhci_pltfm_free(pdev
);
473 static int __devexit
sdhci_esdhc_imx_remove(struct platform_device
*pdev
)
475 struct sdhci_host
*host
= platform_get_drvdata(pdev
);
476 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
477 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
478 struct esdhc_platform_data
*boarddata
= &imx_data
->boarddata
;
479 int dead
= (readl(host
->ioaddr
+ SDHCI_INT_STATUS
) == 0xffffffff);
481 sdhci_remove_host(host
, dead
);
483 if (gpio_is_valid(boarddata
->wp_gpio
))
484 gpio_free(boarddata
->wp_gpio
);
486 if (gpio_is_valid(boarddata
->cd_gpio
)) {
487 free_irq(gpio_to_irq(boarddata
->cd_gpio
), host
);
488 gpio_free(boarddata
->cd_gpio
);
491 clk_disable(pltfm_host
->clk
);
492 clk_put(pltfm_host
->clk
);
495 sdhci_pltfm_free(pdev
);
500 static struct platform_driver sdhci_esdhc_imx_driver
= {
502 .name
= "sdhci-esdhc-imx",
503 .owner
= THIS_MODULE
,
504 .of_match_table
= imx_esdhc_dt_ids
,
506 .id_table
= imx_esdhc_devtype
,
507 .probe
= sdhci_esdhc_imx_probe
,
508 .remove
= __devexit_p(sdhci_esdhc_imx_remove
),
510 .suspend
= sdhci_pltfm_suspend
,
511 .resume
= sdhci_pltfm_resume
,
515 static int __init
sdhci_esdhc_imx_init(void)
517 return platform_driver_register(&sdhci_esdhc_imx_driver
);
519 module_init(sdhci_esdhc_imx_init
);
521 static void __exit
sdhci_esdhc_imx_exit(void)
523 platform_driver_unregister(&sdhci_esdhc_imx_driver
);
525 module_exit(sdhci_esdhc_imx_exit
);
527 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
528 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
529 MODULE_LICENSE("GPL v2");