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/module.h>
20 #include <linux/slab.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/mmc.h>
23 #include <linux/mmc/sdio.h>
25 #include <linux/of_device.h>
26 #include <linux/of_gpio.h>
27 #include <mach/esdhc.h>
28 #include "sdhci-pltfm.h"
29 #include "sdhci-esdhc.h"
31 #define SDHCI_CTRL_D3CD 0x08
32 /* VENDOR SPEC register */
33 #define SDHCI_VENDOR_SPEC 0xC0
34 #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
37 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
38 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
39 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
40 * Define this macro DMA error INT for fsl eSDHC
42 #define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
45 * The CMDTYPE of the CMD register (offset 0xE) should be set to
46 * "11" when the STOP CMD12 is issued on imx53 to abort one
47 * open ended multi-blk IO. Otherwise the TC INT wouldn't
49 * In exact block transfer, the controller doesn't complete the
50 * operations automatically as required at the end of the
51 * transfer and remains on hold if the abort command is not sent.
52 * As a result, the TC flag is not asserted and SW received timeout
53 * exeception. Bit1 of Vendor Spec registor is used to fix it.
55 #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
64 struct pltfm_imx_data
{
67 enum imx_esdhc_type devtype
;
68 struct esdhc_platform_data boarddata
;
71 static struct platform_device_id imx_esdhc_devtype
[] = {
73 .name
= "sdhci-esdhc-imx25",
74 .driver_data
= IMX25_ESDHC
,
76 .name
= "sdhci-esdhc-imx35",
77 .driver_data
= IMX35_ESDHC
,
79 .name
= "sdhci-esdhc-imx51",
80 .driver_data
= IMX51_ESDHC
,
82 .name
= "sdhci-esdhc-imx53",
83 .driver_data
= IMX53_ESDHC
,
88 MODULE_DEVICE_TABLE(platform
, imx_esdhc_devtype
);
90 static const struct of_device_id imx_esdhc_dt_ids
[] = {
91 { .compatible
= "fsl,imx25-esdhc", .data
= &imx_esdhc_devtype
[IMX25_ESDHC
], },
92 { .compatible
= "fsl,imx35-esdhc", .data
= &imx_esdhc_devtype
[IMX35_ESDHC
], },
93 { .compatible
= "fsl,imx51-esdhc", .data
= &imx_esdhc_devtype
[IMX51_ESDHC
], },
94 { .compatible
= "fsl,imx53-esdhc", .data
= &imx_esdhc_devtype
[IMX53_ESDHC
], },
97 MODULE_DEVICE_TABLE(of
, imx_esdhc_dt_ids
);
99 static inline int is_imx25_esdhc(struct pltfm_imx_data
*data
)
101 return data
->devtype
== IMX25_ESDHC
;
104 static inline int is_imx35_esdhc(struct pltfm_imx_data
*data
)
106 return data
->devtype
== IMX35_ESDHC
;
109 static inline int is_imx51_esdhc(struct pltfm_imx_data
*data
)
111 return data
->devtype
== IMX51_ESDHC
;
114 static inline int is_imx53_esdhc(struct pltfm_imx_data
*data
)
116 return data
->devtype
== IMX53_ESDHC
;
119 static inline void esdhc_clrset_le(struct sdhci_host
*host
, u32 mask
, u32 val
, int reg
)
121 void __iomem
*base
= host
->ioaddr
+ (reg
& ~0x3);
122 u32 shift
= (reg
& 0x3) * 8;
124 writel(((readl(base
) & ~(mask
<< shift
)) | (val
<< shift
)), base
);
127 static u32
esdhc_readl_le(struct sdhci_host
*host
, int reg
)
129 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
130 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
131 struct esdhc_platform_data
*boarddata
= &imx_data
->boarddata
;
133 /* fake CARD_PRESENT flag */
134 u32 val
= readl(host
->ioaddr
+ reg
);
136 if (unlikely((reg
== SDHCI_PRESENT_STATE
)
137 && gpio_is_valid(boarddata
->cd_gpio
))) {
138 if (gpio_get_value(boarddata
->cd_gpio
))
139 /* no card, if a valid gpio says so... */
140 val
&= ~SDHCI_CARD_PRESENT
;
142 /* ... in all other cases assume card is present */
143 val
|= SDHCI_CARD_PRESENT
;
146 if (unlikely(reg
== SDHCI_CAPABILITIES
)) {
147 /* In FSL esdhc IC module, only bit20 is used to indicate the
148 * ADMA2 capability of esdhc, but this bit is messed up on
149 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
150 * don't actually support ADMA2). So set the BROKEN_ADMA
151 * uirk on MX25/35 platforms.
154 if (val
& SDHCI_CAN_DO_ADMA1
) {
155 val
&= ~SDHCI_CAN_DO_ADMA1
;
156 val
|= SDHCI_CAN_DO_ADMA2
;
160 if (unlikely(reg
== SDHCI_INT_STATUS
)) {
161 if (val
& SDHCI_INT_VENDOR_SPEC_DMA_ERR
) {
162 val
&= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR
;
163 val
|= SDHCI_INT_ADMA_ERROR
;
170 static void esdhc_writel_le(struct sdhci_host
*host
, u32 val
, int reg
)
172 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
173 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
174 struct esdhc_platform_data
*boarddata
= &imx_data
->boarddata
;
177 if (unlikely(reg
== SDHCI_INT_ENABLE
|| reg
== SDHCI_SIGNAL_ENABLE
)) {
178 if (boarddata
->cd_type
== ESDHC_CD_GPIO
)
180 * These interrupts won't work with a custom
181 * card_detect gpio (only applied to mx25/35)
183 val
&= ~(SDHCI_INT_CARD_REMOVE
| SDHCI_INT_CARD_INSERT
);
185 if (val
& SDHCI_INT_CARD_INT
) {
187 * Clear and then set D3CD bit to avoid missing the
188 * card interrupt. This is a eSDHC controller problem
189 * so we need to apply the following workaround: clear
190 * and set D3CD bit will make eSDHC re-sample the card
191 * interrupt. In case a card interrupt was lost,
192 * re-sample it by the following steps.
194 data
= readl(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
195 data
&= ~SDHCI_CTRL_D3CD
;
196 writel(data
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
197 data
|= SDHCI_CTRL_D3CD
;
198 writel(data
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
202 if (unlikely((imx_data
->flags
& ESDHC_FLAG_MULTIBLK_NO_INT
)
203 && (reg
== SDHCI_INT_STATUS
)
204 && (val
& SDHCI_INT_DATA_END
))) {
206 v
= readl(host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
207 v
&= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK
;
208 writel(v
, host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
211 if (unlikely(reg
== SDHCI_INT_ENABLE
|| reg
== SDHCI_SIGNAL_ENABLE
)) {
212 if (val
& SDHCI_INT_ADMA_ERROR
) {
213 val
&= ~SDHCI_INT_ADMA_ERROR
;
214 val
|= SDHCI_INT_VENDOR_SPEC_DMA_ERR
;
218 writel(val
, host
->ioaddr
+ reg
);
221 static u16
esdhc_readw_le(struct sdhci_host
*host
, int reg
)
223 if (unlikely(reg
== SDHCI_HOST_VERSION
))
226 return readw(host
->ioaddr
+ reg
);
229 static void esdhc_writew_le(struct sdhci_host
*host
, u16 val
, int reg
)
231 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
232 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
235 case SDHCI_TRANSFER_MODE
:
237 * Postpone this write, we must do it together with a
238 * command write that is down below.
240 if ((imx_data
->flags
& ESDHC_FLAG_MULTIBLK_NO_INT
)
241 && (host
->cmd
->opcode
== SD_IO_RW_EXTENDED
)
242 && (host
->cmd
->data
->blocks
> 1)
243 && (host
->cmd
->data
->flags
& MMC_DATA_READ
)) {
245 v
= readl(host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
246 v
|= SDHCI_VENDOR_SPEC_SDIO_QUIRK
;
247 writel(v
, host
->ioaddr
+ SDHCI_VENDOR_SPEC
);
249 imx_data
->scratchpad
= val
;
252 if ((host
->cmd
->opcode
== MMC_STOP_TRANSMISSION
)
253 && (imx_data
->flags
& ESDHC_FLAG_MULTIBLK_NO_INT
))
254 val
|= SDHCI_CMD_ABORTCMD
;
255 writel(val
<< 16 | imx_data
->scratchpad
,
256 host
->ioaddr
+ SDHCI_TRANSFER_MODE
);
258 case SDHCI_BLOCK_SIZE
:
259 val
&= ~SDHCI_MAKE_BLKSZ(0x7, 0);
262 esdhc_clrset_le(host
, 0xffff, val
, reg
);
265 static void esdhc_writeb_le(struct sdhci_host
*host
, u8 val
, int reg
)
270 case SDHCI_POWER_CONTROL
:
272 * FSL put some DMA bits here
273 * If your board has a regulator, code should be here
276 case SDHCI_HOST_CONTROL
:
277 /* FSL messed up here, so we can just keep those three */
278 new_val
= val
& (SDHCI_CTRL_LED
| \
279 SDHCI_CTRL_4BITBUS
| \
281 /* ensure the endianess */
282 new_val
|= ESDHC_HOST_CONTROL_LE
;
283 /* DMA mode bits are shifted */
284 new_val
|= (val
& SDHCI_CTRL_DMA_MASK
) << 5;
286 esdhc_clrset_le(host
, 0xffff, new_val
, reg
);
289 esdhc_clrset_le(host
, 0xff, val
, reg
);
292 * The esdhc has a design violation to SDHC spec which tells
293 * that software reset should not affect card detection circuit.
294 * But esdhc clears its SYSCTL register bits [0..2] during the
295 * software reset. This will stop those clocks that card detection
296 * circuit relies on. To work around it, we turn the clocks on back
297 * to keep card detection circuit functional.
299 if ((reg
== SDHCI_SOFTWARE_RESET
) && (val
& 1))
300 esdhc_clrset_le(host
, 0x7, 0x7, ESDHC_SYSTEM_CONTROL
);
303 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host
*host
)
305 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
307 return clk_get_rate(pltfm_host
->clk
);
310 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host
*host
)
312 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
314 return clk_get_rate(pltfm_host
->clk
) / 256 / 16;
317 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host
*host
)
319 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
320 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
321 struct esdhc_platform_data
*boarddata
= &imx_data
->boarddata
;
323 switch (boarddata
->wp_type
) {
325 if (gpio_is_valid(boarddata
->wp_gpio
))
326 return gpio_get_value(boarddata
->wp_gpio
);
327 case ESDHC_WP_CONTROLLER
:
328 return !(readl(host
->ioaddr
+ SDHCI_PRESENT_STATE
) &
329 SDHCI_WRITE_PROTECT
);
337 static struct sdhci_ops sdhci_esdhc_ops
= {
338 .read_l
= esdhc_readl_le
,
339 .read_w
= esdhc_readw_le
,
340 .write_l
= esdhc_writel_le
,
341 .write_w
= esdhc_writew_le
,
342 .write_b
= esdhc_writeb_le
,
343 .set_clock
= esdhc_set_clock
,
344 .get_max_clock
= esdhc_pltfm_get_max_clock
,
345 .get_min_clock
= esdhc_pltfm_get_min_clock
,
346 .get_ro
= esdhc_pltfm_get_ro
,
349 static struct sdhci_pltfm_data sdhci_esdhc_imx_pdata
= {
350 .quirks
= ESDHC_DEFAULT_QUIRKS
| SDHCI_QUIRK_NO_HISPD_BIT
351 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
352 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
353 | SDHCI_QUIRK_BROKEN_CARD_DETECTION
,
354 .ops
= &sdhci_esdhc_ops
,
357 static irqreturn_t
cd_irq(int irq
, void *data
)
359 struct sdhci_host
*sdhost
= (struct sdhci_host
*)data
;
361 tasklet_schedule(&sdhost
->card_tasklet
);
367 sdhci_esdhc_imx_probe_dt(struct platform_device
*pdev
,
368 struct esdhc_platform_data
*boarddata
)
370 struct device_node
*np
= pdev
->dev
.of_node
;
375 if (of_get_property(np
, "fsl,card-wired", NULL
))
376 boarddata
->cd_type
= ESDHC_CD_PERMANENT
;
378 if (of_get_property(np
, "fsl,cd-controller", NULL
))
379 boarddata
->cd_type
= ESDHC_CD_CONTROLLER
;
381 if (of_get_property(np
, "fsl,wp-controller", NULL
))
382 boarddata
->wp_type
= ESDHC_WP_CONTROLLER
;
384 boarddata
->cd_gpio
= of_get_named_gpio(np
, "cd-gpios", 0);
385 if (gpio_is_valid(boarddata
->cd_gpio
))
386 boarddata
->cd_type
= ESDHC_CD_GPIO
;
388 boarddata
->wp_gpio
= of_get_named_gpio(np
, "wp-gpios", 0);
389 if (gpio_is_valid(boarddata
->wp_gpio
))
390 boarddata
->wp_type
= ESDHC_WP_GPIO
;
396 sdhci_esdhc_imx_probe_dt(struct platform_device
*pdev
,
397 struct esdhc_platform_data
*boarddata
)
403 static int __devinit
sdhci_esdhc_imx_probe(struct platform_device
*pdev
)
405 const struct of_device_id
*of_id
=
406 of_match_device(imx_esdhc_dt_ids
, &pdev
->dev
);
407 struct sdhci_pltfm_host
*pltfm_host
;
408 struct sdhci_host
*host
;
409 struct esdhc_platform_data
*boarddata
;
412 struct pltfm_imx_data
*imx_data
;
414 host
= sdhci_pltfm_init(pdev
, &sdhci_esdhc_imx_pdata
);
416 return PTR_ERR(host
);
418 pltfm_host
= sdhci_priv(host
);
420 imx_data
= kzalloc(sizeof(struct pltfm_imx_data
), GFP_KERNEL
);
427 pdev
->id_entry
= of_id
->data
;
428 imx_data
->devtype
= pdev
->id_entry
->driver_data
;
429 pltfm_host
->priv
= imx_data
;
431 clk
= clk_get(mmc_dev(host
->mmc
), NULL
);
433 dev_err(mmc_dev(host
->mmc
), "clk err\n");
438 pltfm_host
->clk
= clk
;
440 if (!is_imx25_esdhc(imx_data
))
441 host
->quirks
|= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
;
443 if (is_imx25_esdhc(imx_data
) || is_imx35_esdhc(imx_data
))
444 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
445 host
->quirks
|= SDHCI_QUIRK_NO_MULTIBLOCK
446 | SDHCI_QUIRK_BROKEN_ADMA
;
448 if (is_imx53_esdhc(imx_data
))
449 imx_data
->flags
|= ESDHC_FLAG_MULTIBLK_NO_INT
;
451 boarddata
= &imx_data
->boarddata
;
452 if (sdhci_esdhc_imx_probe_dt(pdev
, boarddata
) < 0) {
453 if (!host
->mmc
->parent
->platform_data
) {
454 dev_err(mmc_dev(host
->mmc
), "no board data!\n");
458 imx_data
->boarddata
= *((struct esdhc_platform_data
*)
459 host
->mmc
->parent
->platform_data
);
463 if (boarddata
->wp_type
== ESDHC_WP_GPIO
) {
464 err
= gpio_request_one(boarddata
->wp_gpio
, GPIOF_IN
, "ESDHC_WP");
466 dev_warn(mmc_dev(host
->mmc
),
467 "no write-protect pin available!\n");
468 boarddata
->wp_gpio
= -EINVAL
;
471 boarddata
->wp_gpio
= -EINVAL
;
475 if (boarddata
->cd_type
!= ESDHC_CD_GPIO
)
476 boarddata
->cd_gpio
= -EINVAL
;
478 switch (boarddata
->cd_type
) {
480 err
= gpio_request_one(boarddata
->cd_gpio
, GPIOF_IN
, "ESDHC_CD");
482 dev_err(mmc_dev(host
->mmc
),
483 "no card-detect pin available!\n");
484 goto no_card_detect_pin
;
487 err
= request_irq(gpio_to_irq(boarddata
->cd_gpio
), cd_irq
,
488 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
489 mmc_hostname(host
->mmc
), host
);
491 dev_err(mmc_dev(host
->mmc
), "request irq error\n");
492 goto no_card_detect_irq
;
496 case ESDHC_CD_CONTROLLER
:
497 /* we have a working card_detect back */
498 host
->quirks
&= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION
;
501 case ESDHC_CD_PERMANENT
:
502 host
->mmc
->caps
= MMC_CAP_NONREMOVABLE
;
509 err
= sdhci_add_host(host
);
516 if (gpio_is_valid(boarddata
->cd_gpio
))
517 free_irq(gpio_to_irq(boarddata
->cd_gpio
), host
);
519 if (gpio_is_valid(boarddata
->cd_gpio
))
520 gpio_free(boarddata
->cd_gpio
);
521 if (gpio_is_valid(boarddata
->wp_gpio
))
522 gpio_free(boarddata
->wp_gpio
);
525 clk_disable(pltfm_host
->clk
);
526 clk_put(pltfm_host
->clk
);
530 sdhci_pltfm_free(pdev
);
534 static int __devexit
sdhci_esdhc_imx_remove(struct platform_device
*pdev
)
536 struct sdhci_host
*host
= platform_get_drvdata(pdev
);
537 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
538 struct pltfm_imx_data
*imx_data
= pltfm_host
->priv
;
539 struct esdhc_platform_data
*boarddata
= &imx_data
->boarddata
;
540 int dead
= (readl(host
->ioaddr
+ SDHCI_INT_STATUS
) == 0xffffffff);
542 sdhci_remove_host(host
, dead
);
544 if (gpio_is_valid(boarddata
->wp_gpio
))
545 gpio_free(boarddata
->wp_gpio
);
547 if (gpio_is_valid(boarddata
->cd_gpio
)) {
548 free_irq(gpio_to_irq(boarddata
->cd_gpio
), host
);
549 gpio_free(boarddata
->cd_gpio
);
552 clk_disable(pltfm_host
->clk
);
553 clk_put(pltfm_host
->clk
);
556 sdhci_pltfm_free(pdev
);
561 static struct platform_driver sdhci_esdhc_imx_driver
= {
563 .name
= "sdhci-esdhc-imx",
564 .owner
= THIS_MODULE
,
565 .of_match_table
= imx_esdhc_dt_ids
,
567 .id_table
= imx_esdhc_devtype
,
568 .probe
= sdhci_esdhc_imx_probe
,
569 .remove
= __devexit_p(sdhci_esdhc_imx_remove
),
571 .suspend
= sdhci_pltfm_suspend
,
572 .resume
= sdhci_pltfm_resume
,
576 static int __init
sdhci_esdhc_imx_init(void)
578 return platform_driver_register(&sdhci_esdhc_imx_driver
);
580 module_init(sdhci_esdhc_imx_init
);
582 static void __exit
sdhci_esdhc_imx_exit(void)
584 platform_driver_unregister(&sdhci_esdhc_imx_driver
);
586 module_exit(sdhci_esdhc_imx_exit
);
588 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
589 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
590 MODULE_LICENSE("GPL v2");