Merge branch 'for-linus' of git://ceph.newdream.net/git/ceph-client
[pandora-kernel.git] / drivers / mmc / host / sdhci-s3c.c
1 /* linux/drivers/mmc/host/sdhci-s3c.c
2  *
3  * Copyright 2008 Openmoko Inc.
4  * Copyright 2008 Simtec Electronics
5  *      Ben Dooks <ben@simtec.co.uk>
6  *      http://armlinux.simtec.co.uk/
7  *
8  * SDHCI (HSMMC) support for Samsung SoC
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/delay.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/gpio.h>
22 #include <linux/module.h>
23
24 #include <linux/mmc/host.h>
25
26 #include <plat/sdhci.h>
27 #include <plat/regs-sdhci.h>
28
29 #include "sdhci.h"
30
31 #define MAX_BUS_CLK     (4)
32
33 /**
34  * struct sdhci_s3c - S3C SDHCI instance
35  * @host: The SDHCI host created
36  * @pdev: The platform device we where created from.
37  * @ioarea: The resource created when we claimed the IO area.
38  * @pdata: The platform data for this controller.
39  * @cur_clk: The index of the current bus clock.
40  * @clk_io: The clock for the internal bus interface.
41  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
42  */
43 struct sdhci_s3c {
44         struct sdhci_host       *host;
45         struct platform_device  *pdev;
46         struct resource         *ioarea;
47         struct s3c_sdhci_platdata *pdata;
48         unsigned int            cur_clk;
49         int                     ext_cd_irq;
50         int                     ext_cd_gpio;
51
52         struct clk              *clk_io;
53         struct clk              *clk_bus[MAX_BUS_CLK];
54 };
55
56 static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
57 {
58         return sdhci_priv(host);
59 }
60
61 /**
62  * get_curclk - convert ctrl2 register to clock source number
63  * @ctrl2: Control2 register value.
64  */
65 static u32 get_curclk(u32 ctrl2)
66 {
67         ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
68         ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
69
70         return ctrl2;
71 }
72
73 static void sdhci_s3c_check_sclk(struct sdhci_host *host)
74 {
75         struct sdhci_s3c *ourhost = to_s3c(host);
76         u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
77
78         if (get_curclk(tmp) != ourhost->cur_clk) {
79                 dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
80
81                 tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
82                 tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
83                 writel(tmp, host->ioaddr + 0x80);
84         }
85 }
86
87 /**
88  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
89  * @host: The SDHCI host instance.
90  *
91  * Callback to return the maximum clock rate acheivable by the controller.
92 */
93 static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
94 {
95         struct sdhci_s3c *ourhost = to_s3c(host);
96         struct clk *busclk;
97         unsigned int rate, max;
98         int clk;
99
100         /* note, a reset will reset the clock source */
101
102         sdhci_s3c_check_sclk(host);
103
104         for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
105                 busclk = ourhost->clk_bus[clk];
106                 if (!busclk)
107                         continue;
108
109                 rate = clk_get_rate(busclk);
110                 if (rate > max)
111                         max = rate;
112         }
113
114         return max;
115 }
116
117 /**
118  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
119  * @ourhost: Our SDHCI instance.
120  * @src: The source clock index.
121  * @wanted: The clock frequency wanted.
122  */
123 static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
124                                              unsigned int src,
125                                              unsigned int wanted)
126 {
127         unsigned long rate;
128         struct clk *clksrc = ourhost->clk_bus[src];
129         int div;
130
131         if (!clksrc)
132                 return UINT_MAX;
133
134         /*
135          * Clock divider's step is different as 1 from that of host controller
136          * when 'clk_type' is S3C_SDHCI_CLK_DIV_EXTERNAL.
137          */
138         if (ourhost->pdata->clk_type) {
139                 rate = clk_round_rate(clksrc, wanted);
140                 return wanted - rate;
141         }
142
143         rate = clk_get_rate(clksrc);
144
145         for (div = 1; div < 256; div *= 2) {
146                 if ((rate / div) <= wanted)
147                         break;
148         }
149
150         dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
151                 src, rate, wanted, rate / div);
152
153         return (wanted - (rate / div));
154 }
155
156 /**
157  * sdhci_s3c_set_clock - callback on clock change
158  * @host: The SDHCI host being changed
159  * @clock: The clock rate being requested.
160  *
161  * When the card's clock is going to be changed, look at the new frequency
162  * and find the best clock source to go with it.
163 */
164 static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
165 {
166         struct sdhci_s3c *ourhost = to_s3c(host);
167         unsigned int best = UINT_MAX;
168         unsigned int delta;
169         int best_src = 0;
170         int src;
171         u32 ctrl;
172
173         /* don't bother if the clock is going off. */
174         if (clock == 0)
175                 return;
176
177         for (src = 0; src < MAX_BUS_CLK; src++) {
178                 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
179                 if (delta < best) {
180                         best = delta;
181                         best_src = src;
182                 }
183         }
184
185         dev_dbg(&ourhost->pdev->dev,
186                 "selected source %d, clock %d, delta %d\n",
187                  best_src, clock, best);
188
189         /* select the new clock source */
190
191         if (ourhost->cur_clk != best_src) {
192                 struct clk *clk = ourhost->clk_bus[best_src];
193
194                 /* turn clock off to card before changing clock source */
195                 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
196
197                 ourhost->cur_clk = best_src;
198                 host->max_clk = clk_get_rate(clk);
199
200                 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
201                 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
202                 ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
203                 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
204         }
205
206         /* reconfigure the hardware for new clock rate */
207
208         {
209                 struct mmc_ios ios;
210
211                 ios.clock = clock;
212
213                 if (ourhost->pdata->cfg_card)
214                         (ourhost->pdata->cfg_card)(ourhost->pdev, host->ioaddr,
215                                                    &ios, NULL);
216         }
217 }
218
219 /**
220  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
221  * @host: The SDHCI host being queried
222  *
223  * To init mmc host properly a minimal clock value is needed. For high system
224  * bus clock's values the standard formula gives values out of allowed range.
225  * The clock still can be set to lower values, if clock source other then
226  * system bus is selected.
227 */
228 static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
229 {
230         struct sdhci_s3c *ourhost = to_s3c(host);
231         unsigned int delta, min = UINT_MAX;
232         int src;
233
234         for (src = 0; src < MAX_BUS_CLK; src++) {
235                 delta = sdhci_s3c_consider_clock(ourhost, src, 0);
236                 if (delta == UINT_MAX)
237                         continue;
238                 /* delta is a negative value in this case */
239                 if (-delta < min)
240                         min = -delta;
241         }
242         return min;
243 }
244
245 /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
246 static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
247 {
248         struct sdhci_s3c *ourhost = to_s3c(host);
249
250         return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
251 }
252
253 /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
254 static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
255 {
256         struct sdhci_s3c *ourhost = to_s3c(host);
257
258         /*
259          * initial clock can be in the frequency range of
260          * 100KHz-400KHz, so we set it as max value.
261          */
262         return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
263 }
264
265 /* sdhci_cmu_set_clock - callback on clock change.*/
266 static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
267 {
268         struct sdhci_s3c *ourhost = to_s3c(host);
269
270         /* don't bother if the clock is going off */
271         if (clock == 0)
272                 return;
273
274         sdhci_s3c_set_clock(host, clock);
275
276         clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
277
278         host->clock = clock;
279 }
280
281 /**
282  * sdhci_s3c_platform_8bit_width - support 8bit buswidth
283  * @host: The SDHCI host being queried
284  * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
285  *
286  * We have 8-bit width support but is not a v3 controller.
287  * So we add platform_8bit_width() and support 8bit width.
288  */
289 static int sdhci_s3c_platform_8bit_width(struct sdhci_host *host, int width)
290 {
291         u8 ctrl;
292
293         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
294
295         switch (width) {
296         case MMC_BUS_WIDTH_8:
297                 ctrl |= SDHCI_CTRL_8BITBUS;
298                 ctrl &= ~SDHCI_CTRL_4BITBUS;
299                 break;
300         case MMC_BUS_WIDTH_4:
301                 ctrl |= SDHCI_CTRL_4BITBUS;
302                 ctrl &= ~SDHCI_CTRL_8BITBUS;
303                 break;
304         default:
305                 break;
306         }
307
308         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
309
310         return 0;
311 }
312
313 static struct sdhci_ops sdhci_s3c_ops = {
314         .get_max_clock          = sdhci_s3c_get_max_clk,
315         .set_clock              = sdhci_s3c_set_clock,
316         .get_min_clock          = sdhci_s3c_get_min_clock,
317         .platform_8bit_width    = sdhci_s3c_platform_8bit_width,
318 };
319
320 static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
321 {
322         struct sdhci_host *host = platform_get_drvdata(dev);
323         unsigned long flags;
324
325         if (host) {
326                 spin_lock_irqsave(&host->lock, flags);
327                 if (state) {
328                         dev_dbg(&dev->dev, "card inserted.\n");
329                         host->flags &= ~SDHCI_DEVICE_DEAD;
330                         host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
331                 } else {
332                         dev_dbg(&dev->dev, "card removed.\n");
333                         host->flags |= SDHCI_DEVICE_DEAD;
334                         host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
335                 }
336                 tasklet_schedule(&host->card_tasklet);
337                 spin_unlock_irqrestore(&host->lock, flags);
338         }
339 }
340
341 static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
342 {
343         struct sdhci_s3c *sc = dev_id;
344         int status = gpio_get_value(sc->ext_cd_gpio);
345         if (sc->pdata->ext_cd_gpio_invert)
346                 status = !status;
347         sdhci_s3c_notify_change(sc->pdev, status);
348         return IRQ_HANDLED;
349 }
350
351 static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
352 {
353         struct s3c_sdhci_platdata *pdata = sc->pdata;
354         struct device *dev = &sc->pdev->dev;
355
356         if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
357                 sc->ext_cd_gpio = pdata->ext_cd_gpio;
358                 sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
359                 if (sc->ext_cd_irq &&
360                     request_threaded_irq(sc->ext_cd_irq, NULL,
361                                          sdhci_s3c_gpio_card_detect_thread,
362                                          IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
363                                          dev_name(dev), sc) == 0) {
364                         int status = gpio_get_value(sc->ext_cd_gpio);
365                         if (pdata->ext_cd_gpio_invert)
366                                 status = !status;
367                         sdhci_s3c_notify_change(sc->pdev, status);
368                 } else {
369                         dev_warn(dev, "cannot request irq for card detect\n");
370                         sc->ext_cd_irq = 0;
371                 }
372         } else {
373                 dev_err(dev, "cannot request gpio for card detect\n");
374         }
375 }
376
377 static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
378 {
379         struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
380         struct device *dev = &pdev->dev;
381         struct sdhci_host *host;
382         struct sdhci_s3c *sc;
383         struct resource *res;
384         int ret, irq, ptr, clks;
385
386         if (!pdata) {
387                 dev_err(dev, "no device data specified\n");
388                 return -ENOENT;
389         }
390
391         irq = platform_get_irq(pdev, 0);
392         if (irq < 0) {
393                 dev_err(dev, "no irq specified\n");
394                 return irq;
395         }
396
397         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
398         if (!res) {
399                 dev_err(dev, "no memory specified\n");
400                 return -ENOENT;
401         }
402
403         host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
404         if (IS_ERR(host)) {
405                 dev_err(dev, "sdhci_alloc_host() failed\n");
406                 return PTR_ERR(host);
407         }
408
409         sc = sdhci_priv(host);
410
411         sc->host = host;
412         sc->pdev = pdev;
413         sc->pdata = pdata;
414         sc->ext_cd_gpio = -1; /* invalid gpio number */
415
416         platform_set_drvdata(pdev, host);
417
418         sc->clk_io = clk_get(dev, "hsmmc");
419         if (IS_ERR(sc->clk_io)) {
420                 dev_err(dev, "failed to get io clock\n");
421                 ret = PTR_ERR(sc->clk_io);
422                 goto err_io_clk;
423         }
424
425         /* enable the local io clock and keep it running for the moment. */
426         clk_enable(sc->clk_io);
427
428         for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
429                 struct clk *clk;
430                 char *name = pdata->clocks[ptr];
431
432                 if (name == NULL)
433                         continue;
434
435                 clk = clk_get(dev, name);
436                 if (IS_ERR(clk)) {
437                         dev_err(dev, "failed to get clock %s\n", name);
438                         continue;
439                 }
440
441                 clks++;
442                 sc->clk_bus[ptr] = clk;
443
444                 /*
445                  * save current clock index to know which clock bus
446                  * is used later in overriding functions.
447                  */
448                 sc->cur_clk = ptr;
449
450                 clk_enable(clk);
451
452                 dev_info(dev, "clock source %d: %s (%ld Hz)\n",
453                          ptr, name, clk_get_rate(clk));
454         }
455
456         if (clks == 0) {
457                 dev_err(dev, "failed to find any bus clocks\n");
458                 ret = -ENOENT;
459                 goto err_no_busclks;
460         }
461
462         sc->ioarea = request_mem_region(res->start, resource_size(res),
463                                         mmc_hostname(host->mmc));
464         if (!sc->ioarea) {
465                 dev_err(dev, "failed to reserve register area\n");
466                 ret = -ENXIO;
467                 goto err_req_regs;
468         }
469
470         host->ioaddr = ioremap_nocache(res->start, resource_size(res));
471         if (!host->ioaddr) {
472                 dev_err(dev, "failed to map registers\n");
473                 ret = -ENXIO;
474                 goto err_req_regs;
475         }
476
477         /* Ensure we have minimal gpio selected CMD/CLK/Detect */
478         if (pdata->cfg_gpio)
479                 pdata->cfg_gpio(pdev, pdata->max_width);
480
481         host->hw_name = "samsung-hsmmc";
482         host->ops = &sdhci_s3c_ops;
483         host->quirks = 0;
484         host->irq = irq;
485
486         /* Setup quirks for the controller */
487         host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
488         host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
489
490 #ifndef CONFIG_MMC_SDHCI_S3C_DMA
491
492         /* we currently see overruns on errors, so disable the SDMA
493          * support as well. */
494         host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
495
496 #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
497
498         /* It seems we do not get an DATA transfer complete on non-busy
499          * transfers, not sure if this is a problem with this specific
500          * SDHCI block, or a missing configuration that needs to be set. */
501         host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
502
503         /* This host supports the Auto CMD12 */
504         host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
505
506         /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
507         host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
508
509         if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
510             pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
511                 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
512
513         if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
514                 host->mmc->caps = MMC_CAP_NONREMOVABLE;
515
516         if (pdata->host_caps)
517                 host->mmc->caps |= pdata->host_caps;
518
519         host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
520                          SDHCI_QUIRK_32BIT_DMA_SIZE);
521
522         /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
523         host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
524
525         /*
526          * If controller does not have internal clock divider,
527          * we can use overriding functions instead of default.
528          */
529         if (pdata->clk_type) {
530                 sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
531                 sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
532                 sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
533         }
534
535         /* It supports additional host capabilities if needed */
536         if (pdata->host_caps)
537                 host->mmc->caps |= pdata->host_caps;
538
539         ret = sdhci_add_host(host);
540         if (ret) {
541                 dev_err(dev, "sdhci_add_host() failed\n");
542                 goto err_add_host;
543         }
544
545         /* The following two methods of card detection might call
546            sdhci_s3c_notify_change() immediately, so they can be called
547            only after sdhci_add_host(). Setup errors are ignored. */
548         if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
549                 pdata->ext_cd_init(&sdhci_s3c_notify_change);
550         if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
551             gpio_is_valid(pdata->ext_cd_gpio))
552                 sdhci_s3c_setup_card_detect_gpio(sc);
553
554         return 0;
555
556  err_add_host:
557         release_resource(sc->ioarea);
558         kfree(sc->ioarea);
559
560  err_req_regs:
561         for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
562                 clk_disable(sc->clk_bus[ptr]);
563                 clk_put(sc->clk_bus[ptr]);
564         }
565
566  err_no_busclks:
567         clk_disable(sc->clk_io);
568         clk_put(sc->clk_io);
569
570  err_io_clk:
571         sdhci_free_host(host);
572
573         return ret;
574 }
575
576 static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
577 {
578         struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
579         struct sdhci_host *host =  platform_get_drvdata(pdev);
580         struct sdhci_s3c *sc = sdhci_priv(host);
581         int ptr;
582
583         if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
584                 pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
585
586         if (sc->ext_cd_irq)
587                 free_irq(sc->ext_cd_irq, sc);
588
589         if (gpio_is_valid(sc->ext_cd_gpio))
590                 gpio_free(sc->ext_cd_gpio);
591
592         sdhci_remove_host(host, 1);
593
594         for (ptr = 0; ptr < 3; ptr++) {
595                 if (sc->clk_bus[ptr]) {
596                         clk_disable(sc->clk_bus[ptr]);
597                         clk_put(sc->clk_bus[ptr]);
598                 }
599         }
600         clk_disable(sc->clk_io);
601         clk_put(sc->clk_io);
602
603         iounmap(host->ioaddr);
604         release_resource(sc->ioarea);
605         kfree(sc->ioarea);
606
607         sdhci_free_host(host);
608         platform_set_drvdata(pdev, NULL);
609
610         return 0;
611 }
612
613 #ifdef CONFIG_PM
614
615 static int sdhci_s3c_suspend(struct platform_device *dev, pm_message_t pm)
616 {
617         struct sdhci_host *host = platform_get_drvdata(dev);
618
619         return sdhci_suspend_host(host, pm);
620 }
621
622 static int sdhci_s3c_resume(struct platform_device *dev)
623 {
624         struct sdhci_host *host = platform_get_drvdata(dev);
625
626         return sdhci_resume_host(host);
627 }
628
629 #else
630 #define sdhci_s3c_suspend NULL
631 #define sdhci_s3c_resume NULL
632 #endif
633
634 static struct platform_driver sdhci_s3c_driver = {
635         .probe          = sdhci_s3c_probe,
636         .remove         = __devexit_p(sdhci_s3c_remove),
637         .suspend        = sdhci_s3c_suspend,
638         .resume         = sdhci_s3c_resume,
639         .driver         = {
640                 .owner  = THIS_MODULE,
641                 .name   = "s3c-sdhci",
642         },
643 };
644
645 static int __init sdhci_s3c_init(void)
646 {
647         return platform_driver_register(&sdhci_s3c_driver);
648 }
649
650 static void __exit sdhci_s3c_exit(void)
651 {
652         platform_driver_unregister(&sdhci_s3c_driver);
653 }
654
655 module_init(sdhci_s3c_init);
656 module_exit(sdhci_s3c_exit);
657
658 MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
659 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
660 MODULE_LICENSE("GPL v2");
661 MODULE_ALIAS("platform:s3c-sdhci");