[ARM] pxamci: fix printing gpio numbers in pxamci_probe
[linux-2.6.git] / drivers / mmc / host / sdhci-s3c.c
blob50997d2a63e7effcd533d9d95fea45b9ebf34efc
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>
19 #include <linux/io.h>
21 #include <linux/mmc/host.h>
23 #include <plat/sdhci.h>
24 #include <plat/regs-sdhci.h>
26 #include "sdhci.h"
28 #define MAX_BUS_CLK (4)
30 /**
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.
40 struct sdhci_s3c {
41 struct sdhci_host *host;
42 struct platform_device *pdev;
43 struct resource *ioarea;
44 struct s3c_sdhci_platdata *pdata;
45 unsigned int cur_clk;
47 struct clk *clk_io;
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);
56 /**
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;
65 return ctrl2;
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);
82 /**
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);
91 struct clk *busclk;
92 unsigned int rate, max;
93 int clk;
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];
101 if (!busclk)
102 continue;
104 rate = clk_get_rate(busclk);
105 if (rate > max)
106 max = rate;
109 return max;
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,
124 unsigned int src,
125 unsigned int wanted)
127 unsigned long rate;
128 struct clk *clksrc = ourhost->clk_bus[src];
129 int div;
131 if (!clksrc)
132 return UINT_MAX;
134 rate = clk_get_rate(clksrc);
136 for (div = 1; div < 256; div *= 2) {
137 if ((rate / div) <= wanted)
138 break;
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;
159 unsigned int delta;
160 int best_src = 0;
161 int src;
162 u32 ctrl;
164 /* don't bother if the clock is going off. */
165 if (clock == 0)
166 return;
168 for (src = 0; src < MAX_BUS_CLK; src++) {
169 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
170 if (delta < best) {
171 best = delta;
172 best_src = src;
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 */
201 struct mmc_ios ios;
203 ios.clock = clock;
205 if (ourhost->pdata->cfg_card)
206 (ourhost->pdata->cfg_card)(ourhost->pdev, host->ioaddr,
207 &ios, NULL);
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;
226 if (!pdata) {
227 dev_err(dev, "no device data specified\n");
228 return -ENOENT;
231 irq = platform_get_irq(pdev, 0);
232 if (irq < 0) {
233 dev_err(dev, "no irq specified\n");
234 return irq;
237 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 if (!res) {
239 dev_err(dev, "no memory specified\n");
240 return -ENOENT;
243 host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
244 if (IS_ERR(host)) {
245 dev_err(dev, "sdhci_alloc_host() failed\n");
246 return PTR_ERR(host);
249 sc = sdhci_priv(host);
251 sc->host = host;
252 sc->pdev = pdev;
253 sc->pdata = pdata;
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);
261 goto err_io_clk;
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++) {
268 struct clk *clk;
269 char *name = pdata->clocks[ptr];
271 if (name == NULL)
272 continue;
274 clk = clk_get(dev, name);
275 if (IS_ERR(clk)) {
276 dev_err(dev, "failed to get clock %s\n", name);
277 continue;
280 clks++;
281 sc->clk_bus[ptr] = clk;
282 clk_enable(clk);
284 dev_info(dev, "clock source %d: %s (%ld Hz)\n",
285 ptr, name, clk_get_rate(clk));
288 if (clks == 0) {
289 dev_err(dev, "failed to find any bus clocks\n");
290 ret = -ENOENT;
291 goto err_no_busclks;
294 sc->ioarea = request_mem_region(res->start, resource_size(res),
295 mmc_hostname(host->mmc));
296 if (!sc->ioarea) {
297 dev_err(dev, "failed to reserve register area\n");
298 ret = -ENXIO;
299 goto err_req_regs;
302 host->ioaddr = ioremap_nocache(res->start, resource_size(res));
303 if (!host->ioaddr) {
304 dev_err(dev, "failed to map registers\n");
305 ret = -ENXIO;
306 goto err_req_regs;
309 /* Ensure we have minimal gpio selected CMD/CLK/Detect */
310 if (pdata->cfg_gpio)
311 pdata->cfg_gpio(pdev, pdata->max_width);
313 host->hw_name = "samsung-hsmmc";
314 host->ops = &sdhci_s3c_ops;
315 host->quirks = 0;
316 host->irq = irq;
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);
346 if (ret) {
347 dev_err(dev, "sdhci_add_host() failed\n");
348 goto err_add_host;
351 return 0;
353 err_add_host:
354 release_resource(sc->ioarea);
355 kfree(sc->ioarea);
357 err_req_regs:
358 for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
359 clk_disable(sc->clk_bus[ptr]);
360 clk_put(sc->clk_bus[ptr]);
363 err_no_busclks:
364 clk_disable(sc->clk_io);
365 clk_put(sc->clk_io);
367 err_io_clk:
368 sdhci_free_host(host);
370 return ret;
373 static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
375 return 0;
378 #ifdef CONFIG_PM
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);
385 return 0;
388 static int sdhci_s3c_resume(struct platform_device *dev)
390 struct sdhci_host *host = platform_get_drvdata(dev);
392 sdhci_resume_host(host);
393 return 0;
396 #else
397 #define sdhci_s3c_suspend NULL
398 #define sdhci_s3c_resume NULL
399 #endif
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,
406 .driver = {
407 .owner = THIS_MODULE,
408 .name = "s3c-sdhci",
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");