blob: 2ea3e6b8bd6a8c3a8825e76c82336f50ee7af537 [file] [log] [blame]
Ben Dooks0d1bb412009-06-14 13:52:37 +01001/* 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Ben Dooks0d1bb412009-06-14 13:52:37 +010019#include <linux/clk.h>
20#include <linux/io.h>
Marek Szyprowski17866e142010-08-10 18:01:58 -070021#include <linux/gpio.h>
Mark Brown55156d22011-07-29 15:35:00 +010022#include <linux/module.h>
Mark Brownd5e9c022012-03-03 00:46:41 +000023#include <linux/of.h>
24#include <linux/of_gpio.h>
25#include <linux/pm.h>
Ben Dooks0d1bb412009-06-14 13:52:37 +010026
27#include <linux/mmc/host.h>
28
29#include <plat/sdhci.h>
30#include <plat/regs-sdhci.h>
31
32#include "sdhci.h"
33
34#define MAX_BUS_CLK (4)
35
36/**
37 * struct sdhci_s3c - S3C SDHCI instance
38 * @host: The SDHCI host created
39 * @pdev: The platform device we where created from.
40 * @ioarea: The resource created when we claimed the IO area.
41 * @pdata: The platform data for this controller.
42 * @cur_clk: The index of the current bus clock.
43 * @clk_io: The clock for the internal bus interface.
44 * @clk_bus: The clocks that are available for the SD/MMC bus clock.
45 */
46struct sdhci_s3c {
47 struct sdhci_host *host;
48 struct platform_device *pdev;
49 struct resource *ioarea;
50 struct s3c_sdhci_platdata *pdata;
51 unsigned int cur_clk;
Marek Szyprowski17866e142010-08-10 18:01:58 -070052 int ext_cd_irq;
53 int ext_cd_gpio;
Ben Dooks0d1bb412009-06-14 13:52:37 +010054
55 struct clk *clk_io;
56 struct clk *clk_bus[MAX_BUS_CLK];
57};
58
Thomas Abraham3119936a2012-02-16 22:23:58 +090059/**
60 * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
61 * @sdhci_quirks: sdhci host specific quirks.
62 *
63 * Specifies platform specific configuration of sdhci controller.
64 * Note: A structure for driver specific platform data is used for future
65 * expansion of its usage.
66 */
67struct sdhci_s3c_drv_data {
68 unsigned int sdhci_quirks;
69};
70
Ben Dooks0d1bb412009-06-14 13:52:37 +010071static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
72{
73 return sdhci_priv(host);
74}
75
76/**
77 * get_curclk - convert ctrl2 register to clock source number
78 * @ctrl2: Control2 register value.
79 */
80static u32 get_curclk(u32 ctrl2)
81{
82 ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
83 ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
84
85 return ctrl2;
86}
87
88static void sdhci_s3c_check_sclk(struct sdhci_host *host)
89{
90 struct sdhci_s3c *ourhost = to_s3c(host);
91 u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
92
93 if (get_curclk(tmp) != ourhost->cur_clk) {
94 dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
95
96 tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
97 tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
Jingoo Han7003fec2011-12-14 13:25:46 +090098 writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
Ben Dooks0d1bb412009-06-14 13:52:37 +010099 }
100}
101
102/**
103 * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
104 * @host: The SDHCI host instance.
105 *
106 * Callback to return the maximum clock rate acheivable by the controller.
107*/
108static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
109{
110 struct sdhci_s3c *ourhost = to_s3c(host);
111 struct clk *busclk;
112 unsigned int rate, max;
113 int clk;
114
115 /* note, a reset will reset the clock source */
116
117 sdhci_s3c_check_sclk(host);
118
119 for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
120 busclk = ourhost->clk_bus[clk];
121 if (!busclk)
122 continue;
123
124 rate = clk_get_rate(busclk);
125 if (rate > max)
126 max = rate;
127 }
128
129 return max;
130}
131
Ben Dooks0d1bb412009-06-14 13:52:37 +0100132/**
133 * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
134 * @ourhost: Our SDHCI instance.
135 * @src: The source clock index.
136 * @wanted: The clock frequency wanted.
137 */
138static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
139 unsigned int src,
140 unsigned int wanted)
141{
142 unsigned long rate;
143 struct clk *clksrc = ourhost->clk_bus[src];
144 int div;
145
146 if (!clksrc)
147 return UINT_MAX;
148
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900149 /*
Thomas Abraham3119936a2012-02-16 22:23:58 +0900150 * If controller uses a non-standard clock division, find the best clock
151 * speed possible with selected clock source and skip the division.
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900152 */
Thomas Abraham3119936a2012-02-16 22:23:58 +0900153 if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900154 rate = clk_round_rate(clksrc, wanted);
155 return wanted - rate;
156 }
157
Ben Dooks0d1bb412009-06-14 13:52:37 +0100158 rate = clk_get_rate(clksrc);
159
160 for (div = 1; div < 256; div *= 2) {
161 if ((rate / div) <= wanted)
162 break;
163 }
164
165 dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
166 src, rate, wanted, rate / div);
167
168 return (wanted - (rate / div));
169}
170
171/**
172 * sdhci_s3c_set_clock - callback on clock change
173 * @host: The SDHCI host being changed
174 * @clock: The clock rate being requested.
175 *
176 * When the card's clock is going to be changed, look at the new frequency
177 * and find the best clock source to go with it.
178*/
179static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
180{
181 struct sdhci_s3c *ourhost = to_s3c(host);
182 unsigned int best = UINT_MAX;
183 unsigned int delta;
184 int best_src = 0;
185 int src;
186 u32 ctrl;
187
188 /* don't bother if the clock is going off. */
189 if (clock == 0)
190 return;
191
192 for (src = 0; src < MAX_BUS_CLK; src++) {
193 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
194 if (delta < best) {
195 best = delta;
196 best_src = src;
197 }
198 }
199
200 dev_dbg(&ourhost->pdev->dev,
201 "selected source %d, clock %d, delta %d\n",
202 best_src, clock, best);
203
204 /* select the new clock source */
205
206 if (ourhost->cur_clk != best_src) {
207 struct clk *clk = ourhost->clk_bus[best_src];
208
209 /* turn clock off to card before changing clock source */
210 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
211
212 ourhost->cur_clk = best_src;
213 host->max_clk = clk_get_rate(clk);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100214
215 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
216 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
217 ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
218 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
219 }
220
Thomas Abraham6fe47172011-09-14 12:39:17 +0530221 /* reprogram default hardware configuration */
222 writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
223 host->ioaddr + S3C64XX_SDHCI_CONTROL4);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100224
Thomas Abraham6fe47172011-09-14 12:39:17 +0530225 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
226 ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
227 S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
228 S3C_SDHCI_CTRL2_ENFBCLKRX |
229 S3C_SDHCI_CTRL2_DFCNT_NONE |
230 S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
231 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100232
Thomas Abraham6fe47172011-09-14 12:39:17 +0530233 /* reconfigure the controller for new clock rate */
234 ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
235 if (clock < 25 * 1000000)
236 ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
237 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100238}
239
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700240/**
241 * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
242 * @host: The SDHCI host being queried
243 *
244 * To init mmc host properly a minimal clock value is needed. For high system
245 * bus clock's values the standard formula gives values out of allowed range.
246 * The clock still can be set to lower values, if clock source other then
247 * system bus is selected.
248*/
249static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
250{
251 struct sdhci_s3c *ourhost = to_s3c(host);
252 unsigned int delta, min = UINT_MAX;
253 int src;
254
255 for (src = 0; src < MAX_BUS_CLK; src++) {
256 delta = sdhci_s3c_consider_clock(ourhost, src, 0);
257 if (delta == UINT_MAX)
258 continue;
259 /* delta is a negative value in this case */
260 if (-delta < min)
261 min = -delta;
262 }
263 return min;
264}
265
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900266/* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
267static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
268{
269 struct sdhci_s3c *ourhost = to_s3c(host);
270
271 return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
272}
273
274/* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
275static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
276{
277 struct sdhci_s3c *ourhost = to_s3c(host);
278
279 /*
280 * initial clock can be in the frequency range of
281 * 100KHz-400KHz, so we set it as max value.
282 */
283 return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
284}
285
286/* sdhci_cmu_set_clock - callback on clock change.*/
287static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
288{
289 struct sdhci_s3c *ourhost = to_s3c(host);
Thomas Abraham3119936a2012-02-16 22:23:58 +0900290 unsigned long timeout;
291 u16 clk = 0;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900292
293 /* don't bother if the clock is going off */
294 if (clock == 0)
295 return;
296
297 sdhci_s3c_set_clock(host, clock);
298
299 clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
300
301 host->clock = clock;
Thomas Abraham3119936a2012-02-16 22:23:58 +0900302
303 clk = SDHCI_CLOCK_INT_EN;
304 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
305
306 /* Wait max 20 ms */
307 timeout = 20;
308 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
309 & SDHCI_CLOCK_INT_STABLE)) {
310 if (timeout == 0) {
311 printk(KERN_ERR "%s: Internal clock never "
312 "stabilised.\n", mmc_hostname(host->mmc));
313 return;
314 }
315 timeout--;
316 mdelay(1);
317 }
318
319 clk |= SDHCI_CLOCK_CARD_EN;
320 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900321}
322
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900323/**
324 * sdhci_s3c_platform_8bit_width - support 8bit buswidth
325 * @host: The SDHCI host being queried
326 * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
327 *
328 * We have 8-bit width support but is not a v3 controller.
329 * So we add platform_8bit_width() and support 8bit width.
330 */
331static int sdhci_s3c_platform_8bit_width(struct sdhci_host *host, int width)
332{
333 u8 ctrl;
334
335 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
336
337 switch (width) {
338 case MMC_BUS_WIDTH_8:
339 ctrl |= SDHCI_CTRL_8BITBUS;
340 ctrl &= ~SDHCI_CTRL_4BITBUS;
341 break;
342 case MMC_BUS_WIDTH_4:
343 ctrl |= SDHCI_CTRL_4BITBUS;
344 ctrl &= ~SDHCI_CTRL_8BITBUS;
345 break;
346 default:
Girish K S49bb1e62011-08-26 14:58:18 +0530347 ctrl &= ~SDHCI_CTRL_4BITBUS;
348 ctrl &= ~SDHCI_CTRL_8BITBUS;
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900349 break;
350 }
351
352 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
353
354 return 0;
355}
356
Ben Dooks0d1bb412009-06-14 13:52:37 +0100357static struct sdhci_ops sdhci_s3c_ops = {
358 .get_max_clock = sdhci_s3c_get_max_clk,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100359 .set_clock = sdhci_s3c_set_clock,
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700360 .get_min_clock = sdhci_s3c_get_min_clock,
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900361 .platform_8bit_width = sdhci_s3c_platform_8bit_width,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100362};
363
Marek Szyprowski17866e142010-08-10 18:01:58 -0700364static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
365{
366 struct sdhci_host *host = platform_get_drvdata(dev);
Marek Szyprowski06fe5772010-09-20 15:03:42 +0200367 unsigned long flags;
368
Marek Szyprowski17866e142010-08-10 18:01:58 -0700369 if (host) {
Marek Szyprowski06fe5772010-09-20 15:03:42 +0200370 spin_lock_irqsave(&host->lock, flags);
Marek Szyprowski17866e142010-08-10 18:01:58 -0700371 if (state) {
372 dev_dbg(&dev->dev, "card inserted.\n");
373 host->flags &= ~SDHCI_DEVICE_DEAD;
374 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
375 } else {
376 dev_dbg(&dev->dev, "card removed.\n");
377 host->flags |= SDHCI_DEVICE_DEAD;
378 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
379 }
Kyungmin Parkf5228862010-08-19 14:13:37 -0700380 tasklet_schedule(&host->card_tasklet);
Marek Szyprowski06fe5772010-09-20 15:03:42 +0200381 spin_unlock_irqrestore(&host->lock, flags);
Marek Szyprowski17866e142010-08-10 18:01:58 -0700382 }
383}
384
385static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
386{
387 struct sdhci_s3c *sc = dev_id;
388 int status = gpio_get_value(sc->ext_cd_gpio);
389 if (sc->pdata->ext_cd_gpio_invert)
390 status = !status;
391 sdhci_s3c_notify_change(sc->pdev, status);
392 return IRQ_HANDLED;
393}
394
395static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
396{
397 struct s3c_sdhci_platdata *pdata = sc->pdata;
398 struct device *dev = &sc->pdev->dev;
399
400 if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
401 sc->ext_cd_gpio = pdata->ext_cd_gpio;
402 sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
403 if (sc->ext_cd_irq &&
404 request_threaded_irq(sc->ext_cd_irq, NULL,
405 sdhci_s3c_gpio_card_detect_thread,
406 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
407 dev_name(dev), sc) == 0) {
408 int status = gpio_get_value(sc->ext_cd_gpio);
409 if (pdata->ext_cd_gpio_invert)
410 status = !status;
411 sdhci_s3c_notify_change(sc->pdev, status);
412 } else {
413 dev_warn(dev, "cannot request irq for card detect\n");
414 sc->ext_cd_irq = 0;
415 }
416 } else {
417 dev_err(dev, "cannot request gpio for card detect\n");
418 }
419}
420
Thomas Abraham3119936a2012-02-16 22:23:58 +0900421static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
422 struct platform_device *pdev)
423{
424 return (struct sdhci_s3c_drv_data *)
425 platform_get_device_id(pdev)->driver_data;
426}
427
Ben Dooks0d1bb412009-06-14 13:52:37 +0100428static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
429{
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900430 struct s3c_sdhci_platdata *pdata;
Thomas Abraham3119936a2012-02-16 22:23:58 +0900431 struct sdhci_s3c_drv_data *drv_data;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100432 struct device *dev = &pdev->dev;
433 struct sdhci_host *host;
434 struct sdhci_s3c *sc;
435 struct resource *res;
436 int ret, irq, ptr, clks;
437
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900438 if (!pdev->dev.platform_data) {
Ben Dooks0d1bb412009-06-14 13:52:37 +0100439 dev_err(dev, "no device data specified\n");
440 return -ENOENT;
441 }
442
443 irq = platform_get_irq(pdev, 0);
444 if (irq < 0) {
445 dev_err(dev, "no irq specified\n");
446 return irq;
447 }
448
Ben Dooks0d1bb412009-06-14 13:52:37 +0100449 host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
450 if (IS_ERR(host)) {
451 dev_err(dev, "sdhci_alloc_host() failed\n");
452 return PTR_ERR(host);
453 }
454
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900455 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
456 if (!pdata) {
457 ret = -ENOMEM;
458 goto err_io_clk;
459 }
460 memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
461
Thomas Abraham3119936a2012-02-16 22:23:58 +0900462 drv_data = sdhci_s3c_get_driver_data(pdev);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100463 sc = sdhci_priv(host);
464
465 sc->host = host;
466 sc->pdev = pdev;
467 sc->pdata = pdata;
Marek Szyprowski17866e142010-08-10 18:01:58 -0700468 sc->ext_cd_gpio = -1; /* invalid gpio number */
Ben Dooks0d1bb412009-06-14 13:52:37 +0100469
470 platform_set_drvdata(pdev, host);
471
472 sc->clk_io = clk_get(dev, "hsmmc");
473 if (IS_ERR(sc->clk_io)) {
474 dev_err(dev, "failed to get io clock\n");
475 ret = PTR_ERR(sc->clk_io);
476 goto err_io_clk;
477 }
478
479 /* enable the local io clock and keep it running for the moment. */
480 clk_enable(sc->clk_io);
481
482 for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
483 struct clk *clk;
Rajeshwari Shinde4346b6d2011-11-03 10:52:58 +0900484 char name[14];
Ben Dooks0d1bb412009-06-14 13:52:37 +0100485
Rajeshwari Shinde4346b6d2011-11-03 10:52:58 +0900486 snprintf(name, 14, "mmc_busclk.%d", ptr);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100487 clk = clk_get(dev, name);
488 if (IS_ERR(clk)) {
Ben Dooks0d1bb412009-06-14 13:52:37 +0100489 continue;
490 }
491
492 clks++;
493 sc->clk_bus[ptr] = clk;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900494
495 /*
496 * save current clock index to know which clock bus
497 * is used later in overriding functions.
498 */
499 sc->cur_clk = ptr;
500
Ben Dooks0d1bb412009-06-14 13:52:37 +0100501 clk_enable(clk);
502
503 dev_info(dev, "clock source %d: %s (%ld Hz)\n",
504 ptr, name, clk_get_rate(clk));
505 }
506
507 if (clks == 0) {
508 dev_err(dev, "failed to find any bus clocks\n");
509 ret = -ENOENT;
510 goto err_no_busclks;
511 }
512
Julia Lawall9bda6da2012-03-08 23:24:53 -0500513 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
514 host->ioaddr = devm_request_and_ioremap(&pdev->dev, res);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100515 if (!host->ioaddr) {
516 dev_err(dev, "failed to map registers\n");
517 ret = -ENXIO;
518 goto err_req_regs;
519 }
520
521 /* Ensure we have minimal gpio selected CMD/CLK/Detect */
522 if (pdata->cfg_gpio)
523 pdata->cfg_gpio(pdev, pdata->max_width);
524
525 host->hw_name = "samsung-hsmmc";
526 host->ops = &sdhci_s3c_ops;
527 host->quirks = 0;
528 host->irq = irq;
529
530 /* Setup quirks for the controller */
Thomas Abrahamb2e75ef2010-05-26 14:42:05 -0700531 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
Marek Szyprowskia1d56462010-08-10 18:01:57 -0700532 host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
Thomas Abraham3119936a2012-02-16 22:23:58 +0900533 if (drv_data)
534 host->quirks |= drv_data->sdhci_quirks;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100535
536#ifndef CONFIG_MMC_SDHCI_S3C_DMA
537
538 /* we currently see overruns on errors, so disable the SDMA
539 * support as well. */
540 host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
541
Ben Dooks0d1bb412009-06-14 13:52:37 +0100542#endif /* CONFIG_MMC_SDHCI_S3C_DMA */
543
544 /* It seems we do not get an DATA transfer complete on non-busy
545 * transfers, not sure if this is a problem with this specific
546 * SDHCI block, or a missing configuration that needs to be set. */
547 host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
548
Kyungmin Park732f0e32010-10-30 12:58:56 +0900549 /* This host supports the Auto CMD12 */
550 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
551
Jaehoon Chung7199e2b2011-07-12 17:30:47 +0900552 /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
553 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
554
Marek Szyprowski17866e142010-08-10 18:01:58 -0700555 if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
556 pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
557 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
558
559 if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
560 host->mmc->caps = MMC_CAP_NONREMOVABLE;
561
Thomas Abraham0d22c772012-03-31 23:29:45 -0400562 switch (pdata->max_width) {
563 case 8:
564 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
565 case 4:
566 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
567 break;
568 }
569
Sangwook Leefa1773c2011-11-07 17:05:22 +0000570 if (pdata->pm_caps)
571 host->mmc->pm_caps |= pdata->pm_caps;
572
Ben Dooks0d1bb412009-06-14 13:52:37 +0100573 host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
574 SDHCI_QUIRK_32BIT_DMA_SIZE);
575
Hyuk Lee3fe42e02010-08-10 18:01:55 -0700576 /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
577 host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
578
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900579 /*
580 * If controller does not have internal clock divider,
581 * we can use overriding functions instead of default.
582 */
Thomas Abraham3119936a2012-02-16 22:23:58 +0900583 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900584 sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
585 sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
586 sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
587 }
588
Jeongbae Seob3824f22010-10-08 17:46:20 +0900589 /* It supports additional host capabilities if needed */
590 if (pdata->host_caps)
591 host->mmc->caps |= pdata->host_caps;
592
Jaehoon Chungc1c4b662012-02-07 15:59:01 +0900593 if (pdata->host_caps2)
594 host->mmc->caps2 |= pdata->host_caps2;
595
Ben Dooks0d1bb412009-06-14 13:52:37 +0100596 ret = sdhci_add_host(host);
597 if (ret) {
598 dev_err(dev, "sdhci_add_host() failed\n");
Julia Lawall9bda6da2012-03-08 23:24:53 -0500599 goto err_req_regs;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100600 }
601
Marek Szyprowski17866e142010-08-10 18:01:58 -0700602 /* The following two methods of card detection might call
603 sdhci_s3c_notify_change() immediately, so they can be called
604 only after sdhci_add_host(). Setup errors are ignored. */
605 if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
606 pdata->ext_cd_init(&sdhci_s3c_notify_change);
607 if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
608 gpio_is_valid(pdata->ext_cd_gpio))
609 sdhci_s3c_setup_card_detect_gpio(sc);
610
Ben Dooks0d1bb412009-06-14 13:52:37 +0100611 return 0;
612
Ben Dooks0d1bb412009-06-14 13:52:37 +0100613 err_req_regs:
614 for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
Jaehoon Chung326adda2011-10-12 13:14:29 +0900615 if (sc->clk_bus[ptr]) {
616 clk_disable(sc->clk_bus[ptr]);
617 clk_put(sc->clk_bus[ptr]);
618 }
Ben Dooks0d1bb412009-06-14 13:52:37 +0100619 }
620
621 err_no_busclks:
622 clk_disable(sc->clk_io);
623 clk_put(sc->clk_io);
624
625 err_io_clk:
626 sdhci_free_host(host);
627
628 return ret;
629}
630
631static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
632{
Marek Szyprowski17866e142010-08-10 18:01:58 -0700633 struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700634 struct sdhci_host *host = platform_get_drvdata(pdev);
635 struct sdhci_s3c *sc = sdhci_priv(host);
636 int ptr;
637
Marek Szyprowski17866e142010-08-10 18:01:58 -0700638 if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
639 pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
640
641 if (sc->ext_cd_irq)
642 free_irq(sc->ext_cd_irq, sc);
643
644 if (gpio_is_valid(sc->ext_cd_gpio))
645 gpio_free(sc->ext_cd_gpio);
646
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700647 sdhci_remove_host(host, 1);
648
649 for (ptr = 0; ptr < 3; ptr++) {
Marek Szyprowski9320f7c2010-09-23 16:22:05 +0200650 if (sc->clk_bus[ptr]) {
651 clk_disable(sc->clk_bus[ptr]);
652 clk_put(sc->clk_bus[ptr]);
653 }
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700654 }
655 clk_disable(sc->clk_io);
656 clk_put(sc->clk_io);
657
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700658 sdhci_free_host(host);
659 platform_set_drvdata(pdev, NULL);
660
Ben Dooks0d1bb412009-06-14 13:52:37 +0100661 return 0;
662}
663
Mark Brownd5e9c022012-03-03 00:46:41 +0000664#ifdef CONFIG_PM_SLEEP
Manuel Lauss29495aa2011-11-03 11:09:45 +0100665static int sdhci_s3c_suspend(struct device *dev)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100666{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100667 struct sdhci_host *host = dev_get_drvdata(dev);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100668
Manuel Lauss29495aa2011-11-03 11:09:45 +0100669 return sdhci_suspend_host(host);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100670}
671
Manuel Lauss29495aa2011-11-03 11:09:45 +0100672static int sdhci_s3c_resume(struct device *dev)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100673{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100674 struct sdhci_host *host = dev_get_drvdata(dev);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100675
Wonil Choi65d13512011-06-29 11:38:38 +0900676 return sdhci_resume_host(host);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100677}
Mark Brownd5e9c022012-03-03 00:46:41 +0000678#endif
Ben Dooks0d1bb412009-06-14 13:52:37 +0100679
Mark Brownd5e9c022012-03-03 00:46:41 +0000680#ifdef CONFIG_PM
Manuel Lauss29495aa2011-11-03 11:09:45 +0100681static const struct dev_pm_ops sdhci_s3c_pmops = {
Mark Brownd5e9c022012-03-03 00:46:41 +0000682 SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
Manuel Lauss29495aa2011-11-03 11:09:45 +0100683};
684
685#define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
686
Ben Dooks0d1bb412009-06-14 13:52:37 +0100687#else
Manuel Lauss29495aa2011-11-03 11:09:45 +0100688#define SDHCI_S3C_PMOPS NULL
Ben Dooks0d1bb412009-06-14 13:52:37 +0100689#endif
690
Thomas Abraham3119936a2012-02-16 22:23:58 +0900691#if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
692static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
693 .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
694};
695#define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
696#else
697#define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
698#endif
699
700static struct platform_device_id sdhci_s3c_driver_ids[] = {
701 {
702 .name = "s3c-sdhci",
703 .driver_data = (kernel_ulong_t)NULL,
704 }, {
705 .name = "exynos4-sdhci",
706 .driver_data = EXYNOS4_SDHCI_DRV_DATA,
707 },
708 { }
709};
710MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
711
Ben Dooks0d1bb412009-06-14 13:52:37 +0100712static struct platform_driver sdhci_s3c_driver = {
713 .probe = sdhci_s3c_probe,
714 .remove = __devexit_p(sdhci_s3c_remove),
Thomas Abraham3119936a2012-02-16 22:23:58 +0900715 .id_table = sdhci_s3c_driver_ids,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100716 .driver = {
717 .owner = THIS_MODULE,
718 .name = "s3c-sdhci",
Manuel Lauss29495aa2011-11-03 11:09:45 +0100719 .pm = SDHCI_S3C_PMOPS,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100720 },
721};
722
Axel Lind1f81a62011-11-26 12:55:43 +0800723module_platform_driver(sdhci_s3c_driver);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100724
725MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
726MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
727MODULE_LICENSE("GPL v2");
728MODULE_ALIAS("platform:s3c-sdhci");