1 /* linux/drivers/mmc/host/sdhci-s3c.c
3 * Copyright 2008 Openmoko Inc.
4 * Copyright 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
8 * SDHCI (HSMMC) support for Samsung SoC
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.
15 #include <linux/delay.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
21 #include <linux/mmc/host.h>
23 #include <plat/sdhci.h>
24 #include <plat/regs-sdhci.h>
28 #define MAX_BUS_CLK (4)
31 * struct sdhci_s3c - S3C SDHCI instance
32 * @host: The SDHCI host created
33 * @pdev: The platform device we where created from.
34 * @ioarea: The resource created when we claimed the IO area.
35 * @pdata: The platform data for this controller.
36 * @cur_clk: The index of the current bus clock.
37 * @clk_io: The clock for the internal bus interface.
38 * @clk_bus: The clocks that are available for the SD/MMC bus clock.
41 struct sdhci_host *host;
42 struct platform_device *pdev;
43 struct resource *ioarea;
44 struct s3c_sdhci_platdata *pdata;
48 struct clk *clk_bus[MAX_BUS_CLK];
51 static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
53 return sdhci_priv(host);
57 * get_curclk - convert ctrl2 register to clock source number
58 * @ctrl2: Control2 register value.
60 static u32 get_curclk(u32 ctrl2)
62 ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
63 ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
68 static void sdhci_s3c_check_sclk(struct sdhci_host *host)
70 struct sdhci_s3c *ourhost = to_s3c(host);
71 u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
73 if (get_curclk(tmp) != ourhost->cur_clk) {
74 dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
76 tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
77 tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
78 writel(tmp, host->ioaddr + 0x80);
83 * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
84 * @host: The SDHCI host instance.
86 * Callback to return the maximum clock rate acheivable by the controller.
88 static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
90 struct sdhci_s3c *ourhost = to_s3c(host);
92 unsigned int rate, max;
95 /* note, a reset will reset the clock source */
97 sdhci_s3c_check_sclk(host);
99 for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
100 busclk = ourhost->clk_bus[clk];
104 rate = clk_get_rate(busclk);
112 static unsigned int sdhci_s3c_get_timeout_clk(struct sdhci_host *host)
114 return sdhci_s3c_get_max_clk(host) / 1000000;
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.
123 static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
128 struct clk *clksrc = ourhost->clk_bus[src];
134 rate = clk_get_rate(clksrc);
136 for (div = 1; div < 256; div *= 2) {
137 if ((rate / div) <= wanted)
141 dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
142 src, rate, wanted, rate / div);
144 return (wanted - (rate / div));
148 * sdhci_s3c_set_clock - callback on clock change
149 * @host: The SDHCI host being changed
150 * @clock: The clock rate being requested.
152 * When the card's clock is going to be changed, look at the new frequency
153 * and find the best clock source to go with it.
155 static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
157 struct sdhci_s3c *ourhost = to_s3c(host);
158 unsigned int best = UINT_MAX;
164 /* don't bother if the clock is going off. */
168 for (src = 0; src < MAX_BUS_CLK; src++) {
169 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
176 dev_dbg(&ourhost->pdev->dev,
177 "selected source %d, clock %d, delta %d\n",
178 best_src, clock, best);
180 /* select the new clock source */
182 if (ourhost->cur_clk != best_src) {
183 struct clk *clk = ourhost->clk_bus[best_src];
185 /* turn clock off to card before changing clock source */
186 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
188 ourhost->cur_clk = best_src;
189 host->max_clk = clk_get_rate(clk);
190 host->timeout_clk = sdhci_s3c_get_timeout_clk(host);
192 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
193 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
194 ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
195 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
198 /* reconfigure the hardware for new clock rate */
205 if (ourhost->pdata->cfg_card)
206 (ourhost->pdata->cfg_card)(ourhost->pdev, host->ioaddr,
211 static struct sdhci_ops sdhci_s3c_ops = {
212 .get_max_clock = sdhci_s3c_get_max_clk,
213 .get_timeout_clock = sdhci_s3c_get_timeout_clk,
214 .set_clock = sdhci_s3c_set_clock,
217 static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
219 struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
220 struct device *dev = &pdev->dev;
221 struct sdhci_host *host;
222 struct sdhci_s3c *sc;
223 struct resource *res;
224 int ret, irq, ptr, clks;
227 dev_err(dev, "no device data specified\n");
231 irq = platform_get_irq(pdev, 0);
233 dev_err(dev, "no irq specified\n");
237 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
239 dev_err(dev, "no memory specified\n");
243 host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
245 dev_err(dev, "sdhci_alloc_host() failed\n");
246 return PTR_ERR(host);
249 sc = sdhci_priv(host);
255 platform_set_drvdata(pdev, host);
257 sc->clk_io = clk_get(dev, "hsmmc");
258 if (IS_ERR(sc->clk_io)) {
259 dev_err(dev, "failed to get io clock\n");
260 ret = PTR_ERR(sc->clk_io);
264 /* enable the local io clock and keep it running for the moment. */
265 clk_enable(sc->clk_io);
267 for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
269 char *name = pdata->clocks[ptr];
274 clk = clk_get(dev, name);
276 dev_err(dev, "failed to get clock %s\n", name);
281 sc->clk_bus[ptr] = clk;
284 dev_info(dev, "clock source %d: %s (%ld Hz)\n",
285 ptr, name, clk_get_rate(clk));
289 dev_err(dev, "failed to find any bus clocks\n");
294 sc->ioarea = request_mem_region(res->start, resource_size(res),
295 mmc_hostname(host->mmc));
297 dev_err(dev, "failed to reserve register area\n");
302 host->ioaddr = ioremap_nocache(res->start, resource_size(res));
304 dev_err(dev, "failed to map registers\n");
309 /* Ensure we have minimal gpio selected CMD/CLK/Detect */
311 pdata->cfg_gpio(pdev, pdata->max_width);
313 host->hw_name = "samsung-hsmmc";
314 host->ops = &sdhci_s3c_ops;
318 /* Setup quirks for the controller */
320 /* Currently with ADMA enabled we are getting some length
321 * interrupts that are not being dealt with, do disable
322 * ADMA until this is sorted out. */
323 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
324 host->quirks |= SDHCI_QUIRK_32BIT_ADMA_SIZE;
326 #ifndef CONFIG_MMC_SDHCI_S3C_DMA
328 /* we currently see overruns on errors, so disable the SDMA
329 * support as well. */
330 host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
332 /* PIO currently has problems with multi-block IO */
333 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
335 #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
337 /* It seems we do not get an DATA transfer complete on non-busy
338 * transfers, not sure if this is a problem with this specific
339 * SDHCI block, or a missing configuration that needs to be set. */
340 host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
342 host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
343 SDHCI_QUIRK_32BIT_DMA_SIZE);
345 ret = sdhci_add_host(host);
347 dev_err(dev, "sdhci_add_host() failed\n");
354 release_resource(sc->ioarea);
358 for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
359 clk_disable(sc->clk_bus[ptr]);
360 clk_put(sc->clk_bus[ptr]);
364 clk_disable(sc->clk_io);
368 sdhci_free_host(host);
373 static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
380 static int sdhci_s3c_suspend(struct platform_device *dev, pm_message_t pm)
382 struct sdhci_host *host = platform_get_drvdata(dev);
384 sdhci_suspend_host(host, pm);
388 static int sdhci_s3c_resume(struct platform_device *dev)
390 struct sdhci_host *host = platform_get_drvdata(dev);
392 sdhci_resume_host(host);
397 #define sdhci_s3c_suspend NULL
398 #define sdhci_s3c_resume NULL
401 static struct platform_driver sdhci_s3c_driver = {
402 .probe = sdhci_s3c_probe,
403 .remove = __devexit_p(sdhci_s3c_remove),
404 .suspend = sdhci_s3c_suspend,
405 .resume = sdhci_s3c_resume,
407 .owner = THIS_MODULE,
412 static int __init sdhci_s3c_init(void)
414 return platform_driver_register(&sdhci_s3c_driver);
417 static void __exit sdhci_s3c_exit(void)
419 platform_driver_unregister(&sdhci_s3c_driver);
422 module_init(sdhci_s3c_init);
423 module_exit(sdhci_s3c_exit);
425 MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
426 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
427 MODULE_LICENSE("GPL v2");
428 MODULE_ALIAS("platform:s3c-sdhci");