mmc: sdhci-esdhc-imx: add device tree probe support
[pandora-kernel.git] / drivers / mmc / host / sdhci-esdhc-imx.c
1 /*
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>
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>
23 #include <linux/of.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"
29
30 /* VENDOR SPEC register */
31 #define SDHCI_VENDOR_SPEC               0xC0
32 #define  SDHCI_VENDOR_SPEC_SDIO_QUIRK   0x00000002
33
34 /*
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)
46
47 enum imx_esdhc_type {
48         IMX25_ESDHC,
49         IMX35_ESDHC,
50         IMX51_ESDHC,
51         IMX53_ESDHC,
52 };
53
54 struct pltfm_imx_data {
55         int flags;
56         u32 scratchpad;
57         enum imx_esdhc_type devtype;
58         struct esdhc_platform_data boarddata;
59 };
60
61 static 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 };
78 MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
79
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], },
85         { /* sentinel */ }
86 };
87 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
88
89 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
90 {
91         return data->devtype == IMX25_ESDHC;
92 }
93
94 static inline int is_imx35_esdhc(struct pltfm_imx_data *data)
95 {
96         return data->devtype == IMX35_ESDHC;
97 }
98
99 static inline int is_imx51_esdhc(struct pltfm_imx_data *data)
100 {
101         return data->devtype == IMX51_ESDHC;
102 }
103
104 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
105 {
106         return data->devtype == IMX53_ESDHC;
107 }
108
109 static 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
117 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
118 {
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;
122
123         /* fake CARD_PRESENT flag */
124         u32 val = readl(host->ioaddr + reg);
125
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;
131                 else
132                         /* ... in all other cases assume card is present */
133                         val |= SDHCI_CARD_PRESENT;
134         }
135
136         return val;
137 }
138
139 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
140 {
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;
144
145         if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)
146                         && (boarddata->cd_type == ESDHC_CD_GPIO)))
147                 /*
148                  * these interrupts won't work with a custom card_detect gpio
149                  */
150                 val &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
151
152         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
161         writel(val, host->ioaddr + reg);
162 }
163
164 static 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
172 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
173 {
174         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
175         struct pltfm_imx_data *imx_data = pltfm_host->priv;
176
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                  */
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)) {
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                 }
192                 imx_data->scratchpad = val;
193                 return;
194         case SDHCI_COMMAND:
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);
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
208 static 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);
231
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);
242 }
243
244 static 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
251 static 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
258 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
259 {
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;
263
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
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,
288 };
289
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,
295 };
296
297 static 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
305 #ifdef CONFIG_OF
306 static int __devinit
307 sdhci_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
335 static inline int
336 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
337                          struct esdhc_platform_data *boarddata)
338 {
339         return -ENODEV;
340 }
341 #endif
342
343 static int __devinit sdhci_esdhc_imx_probe(struct platform_device *pdev)
344 {
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;
350         struct clk *clk;
351         int err;
352         struct pltfm_imx_data *imx_data;
353
354         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);
361         if (!imx_data) {
362                 err = -ENOMEM;
363                 goto err_imx_data;
364         }
365
366         if (of_id)
367                 pdev->id_entry = of_id->data;
368         imx_data->devtype = pdev->id_entry->driver_data;
369         pltfm_host->priv = imx_data;
370
371         clk = clk_get(mmc_dev(host->mmc), NULL);
372         if (IS_ERR(clk)) {
373                 dev_err(mmc_dev(host->mmc), "clk err\n");
374                 err = PTR_ERR(clk);
375                 goto err_clk_get;
376         }
377         clk_enable(clk);
378         pltfm_host->clk = clk;
379
380         if (!is_imx25_esdhc(imx_data))
381                 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
382
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;
386
387         if (is_imx53_esdhc(imx_data))
388                 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
389
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");
394                         err = -EINVAL;
395                         goto no_board_data;
396                 }
397                 imx_data->boarddata = *((struct esdhc_platform_data *)
398                                         host->mmc->parent->platform_data);
399         }
400
401         /* write_protect */
402         if (boarddata->wp_type == ESDHC_WP_GPIO) {
403                 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
404                 if (err) {
405                         dev_warn(mmc_dev(host->mmc),
406                                  "no write-protect pin available!\n");
407                         boarddata->wp_gpio = -EINVAL;
408                 }
409         } else {
410                 boarddata->wp_gpio = -EINVAL;
411         }
412
413         /* 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:
419                 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
420                 if (err) {
421                         dev_err(mmc_dev(host->mmc),
422                                 "no card-detect pin available!\n");
423                         goto no_card_detect_pin;
424                 }
425
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);
429                 if (err) {
430                         dev_err(mmc_dev(host->mmc), "request irq error\n");
431                         goto no_card_detect_irq;
432                 }
433                 /* fall through */
434
435         case ESDHC_CD_CONTROLLER:
436                 /* we have a working card_detect back */
437                 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
438                 break;
439
440         case ESDHC_CD_PERMANENT:
441                 host->mmc->caps = MMC_CAP_NONREMOVABLE;
442                 break;
443
444         case ESDHC_CD_NONE:
445                 break;
446         }
447
448         err = sdhci_add_host(host);
449         if (err)
450                 goto err_add_host;
451
452         return 0;
453
454 err_add_host:
455         if (gpio_is_valid(boarddata->cd_gpio))
456                 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
457 no_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);
462 no_card_detect_pin:
463 no_board_data:
464         clk_disable(pltfm_host->clk);
465         clk_put(pltfm_host->clk);
466 err_clk_get:
467         kfree(imx_data);
468 err_imx_data:
469         sdhci_pltfm_free(pdev);
470         return err;
471 }
472
473 static int __devexit sdhci_esdhc_imx_remove(struct platform_device *pdev)
474 {
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);
480
481         sdhci_remove_host(host, dead);
482
483         if (gpio_is_valid(boarddata->wp_gpio))
484                 gpio_free(boarddata->wp_gpio);
485
486         if (gpio_is_valid(boarddata->cd_gpio)) {
487                 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
488                 gpio_free(boarddata->cd_gpio);
489         }
490
491         clk_disable(pltfm_host->clk);
492         clk_put(pltfm_host->clk);
493         kfree(imx_data);
494
495         sdhci_pltfm_free(pdev);
496
497         return 0;
498 }
499
500 static struct platform_driver sdhci_esdhc_imx_driver = {
501         .driver         = {
502                 .name   = "sdhci-esdhc-imx",
503                 .owner  = THIS_MODULE,
504                 .of_match_table = imx_esdhc_dt_ids,
505         },
506         .id_table       = imx_esdhc_devtype,
507         .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
513 };
514
515 static int __init sdhci_esdhc_imx_init(void)
516 {
517         return platform_driver_register(&sdhci_esdhc_imx_driver);
518 }
519 module_init(sdhci_esdhc_imx_init);
520
521 static void __exit sdhci_esdhc_imx_exit(void)
522 {
523         platform_driver_unregister(&sdhci_esdhc_imx_driver);
524 }
525 module_exit(sdhci_esdhc_imx_exit);
526
527 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
528 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
529 MODULE_LICENSE("GPL v2");