blob: 70c724bc6fc7844a9e559e72a0a74f7d33d60e66 [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
Paul Osmialowski017210d2015-02-04 10:16:59 +010015#include <linux/spinlock.h>
Ben Dooks0d1bb412009-06-14 13:52:37 +010016#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/platform_device.h>
Arnd Bergmanncc014f32013-03-04 18:28:21 +010019#include <linux/platform_data/mmc-sdhci-s3c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Ben Dooks0d1bb412009-06-14 13:52:37 +010021#include <linux/clk.h>
22#include <linux/io.h>
Marek Szyprowski17866e12010-08-10 18:01:58 -070023#include <linux/gpio.h>
Mark Brown55156d22011-07-29 15:35:00 +010024#include <linux/module.h>
Mark Brownd5e9c022012-03-03 00:46:41 +000025#include <linux/of.h>
26#include <linux/of_gpio.h>
27#include <linux/pm.h>
Mark Brown9f4e8152012-03-31 23:31:55 -040028#include <linux/pm_runtime.h>
Ben Dooks0d1bb412009-06-14 13:52:37 +010029
30#include <linux/mmc/host.h>
31
Arnd Bergmanncc014f32013-03-04 18:28:21 +010032#include "sdhci-s3c-regs.h"
Ben Dooks0d1bb412009-06-14 13:52:37 +010033#include "sdhci.h"
34
35#define MAX_BUS_CLK (4)
36
37/**
38 * struct sdhci_s3c - S3C SDHCI instance
39 * @host: The SDHCI host created
40 * @pdev: The platform device we where created from.
41 * @ioarea: The resource created when we claimed the IO area.
42 * @pdata: The platform data for this controller.
43 * @cur_clk: The index of the current bus clock.
44 * @clk_io: The clock for the internal bus interface.
45 * @clk_bus: The clocks that are available for the SD/MMC bus clock.
46 */
47struct sdhci_s3c {
48 struct sdhci_host *host;
49 struct platform_device *pdev;
50 struct resource *ioarea;
51 struct s3c_sdhci_platdata *pdata;
Tomasz Figa3ac147f2014-01-11 22:39:05 +010052 int cur_clk;
Marek Szyprowski17866e12010-08-10 18:01:58 -070053 int ext_cd_irq;
54 int ext_cd_gpio;
Ben Dooks0d1bb412009-06-14 13:52:37 +010055
56 struct clk *clk_io;
57 struct clk *clk_bus[MAX_BUS_CLK];
Tomasz Figa6eb28bd2014-01-11 22:39:02 +010058 unsigned long clk_rates[MAX_BUS_CLK];
Russell King17710592014-04-25 12:58:55 +010059
60 bool no_divider;
Ben Dooks0d1bb412009-06-14 13:52:37 +010061};
62
Thomas Abraham3119936a2012-02-16 22:23:58 +090063/**
64 * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
65 * @sdhci_quirks: sdhci host specific quirks.
66 *
67 * Specifies platform specific configuration of sdhci controller.
68 * Note: A structure for driver specific platform data is used for future
69 * expansion of its usage.
70 */
71struct sdhci_s3c_drv_data {
72 unsigned int sdhci_quirks;
Russell King17710592014-04-25 12:58:55 +010073 bool no_divider;
Thomas Abraham3119936a2012-02-16 22:23:58 +090074};
75
Ben Dooks0d1bb412009-06-14 13:52:37 +010076static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
77{
78 return sdhci_priv(host);
79}
80
81/**
Ben Dooks0d1bb412009-06-14 13:52:37 +010082 * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
83 * @host: The SDHCI host instance.
84 *
85 * Callback to return the maximum clock rate acheivable by the controller.
86*/
87static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
88{
89 struct sdhci_s3c *ourhost = to_s3c(host);
Tomasz Figa222a13c2014-01-11 22:39:04 +010090 unsigned long rate, max = 0;
91 int src;
Ben Dooks0d1bb412009-06-14 13:52:37 +010092
Tomasz Figa222a13c2014-01-11 22:39:04 +010093 for (src = 0; src < MAX_BUS_CLK; src++) {
94 rate = ourhost->clk_rates[src];
Ben Dooks0d1bb412009-06-14 13:52:37 +010095 if (rate > max)
96 max = rate;
97 }
98
99 return max;
100}
101
Ben Dooks0d1bb412009-06-14 13:52:37 +0100102/**
103 * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
104 * @ourhost: Our SDHCI instance.
105 * @src: The source clock index.
106 * @wanted: The clock frequency wanted.
107 */
108static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
109 unsigned int src,
110 unsigned int wanted)
111{
112 unsigned long rate;
113 struct clk *clksrc = ourhost->clk_bus[src];
Tomasz Figa8880a4a2014-01-11 22:39:01 +0100114 int shift;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100115
Tomasz Figa8f4b78d2014-01-11 22:39:03 +0100116 if (IS_ERR(clksrc))
Ben Dooks0d1bb412009-06-14 13:52:37 +0100117 return UINT_MAX;
118
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900119 /*
Thomas Abraham3119936a2012-02-16 22:23:58 +0900120 * If controller uses a non-standard clock division, find the best clock
121 * speed possible with selected clock source and skip the division.
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900122 */
Russell King17710592014-04-25 12:58:55 +0100123 if (ourhost->no_divider) {
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900124 rate = clk_round_rate(clksrc, wanted);
125 return wanted - rate;
126 }
127
Tomasz Figa6eb28bd2014-01-11 22:39:02 +0100128 rate = ourhost->clk_rates[src];
Ben Dooks0d1bb412009-06-14 13:52:37 +0100129
Tomasz Figa22003002014-01-11 22:39:06 +0100130 for (shift = 0; shift <= 8; ++shift) {
Tomasz Figa8880a4a2014-01-11 22:39:01 +0100131 if ((rate >> shift) <= wanted)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100132 break;
133 }
134
Tomasz Figa22003002014-01-11 22:39:06 +0100135 if (shift > 8) {
136 dev_dbg(&ourhost->pdev->dev,
137 "clk %d: rate %ld, min rate %lu > wanted %u\n",
138 src, rate, rate / 256, wanted);
139 return UINT_MAX;
140 }
141
Ben Dooks0d1bb412009-06-14 13:52:37 +0100142 dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
Tomasz Figa8880a4a2014-01-11 22:39:01 +0100143 src, rate, wanted, rate >> shift);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100144
Tomasz Figa8880a4a2014-01-11 22:39:01 +0100145 return wanted - (rate >> shift);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100146}
147
148/**
149 * sdhci_s3c_set_clock - callback on clock change
150 * @host: The SDHCI host being changed
151 * @clock: The clock rate being requested.
152 *
153 * When the card's clock is going to be changed, look at the new frequency
154 * and find the best clock source to go with it.
155*/
156static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
157{
158 struct sdhci_s3c *ourhost = to_s3c(host);
159 unsigned int best = UINT_MAX;
160 unsigned int delta;
161 int best_src = 0;
162 int src;
163 u32 ctrl;
164
Russell King1650d0c2014-04-25 12:58:50 +0100165 host->mmc->actual_clock = 0;
166
Ben Dooks0d1bb412009-06-14 13:52:37 +0100167 /* don't bother if the clock is going off. */
Russell King17710592014-04-25 12:58:55 +0100168 if (clock == 0) {
169 sdhci_set_clock(host, clock);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100170 return;
Russell King17710592014-04-25 12:58:55 +0100171 }
Ben Dooks0d1bb412009-06-14 13:52:37 +0100172
173 for (src = 0; src < MAX_BUS_CLK; src++) {
174 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
175 if (delta < best) {
176 best = delta;
177 best_src = src;
178 }
179 }
180
181 dev_dbg(&ourhost->pdev->dev,
182 "selected source %d, clock %d, delta %d\n",
183 best_src, clock, best);
184
185 /* select the new clock source */
Ben Dooks0d1bb412009-06-14 13:52:37 +0100186 if (ourhost->cur_clk != best_src) {
187 struct clk *clk = ourhost->clk_bus[best_src];
188
Thomas Abraham0f310a052012-10-03 08:35:43 +0900189 clk_prepare_enable(clk);
Tomasz Figa3ac147f2014-01-11 22:39:05 +0100190 if (ourhost->cur_clk >= 0)
191 clk_disable_unprepare(
192 ourhost->clk_bus[ourhost->cur_clk]);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100193
194 ourhost->cur_clk = best_src;
Tomasz Figa6eb28bd2014-01-11 22:39:02 +0100195 host->max_clk = ourhost->clk_rates[best_src];
Ben Dooks0d1bb412009-06-14 13:52:37 +0100196 }
197
Tomasz Figa3ac147f2014-01-11 22:39:05 +0100198 /* turn clock off to card before changing clock source */
199 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
200
201 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
202 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
203 ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
204 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
205
Thomas Abraham6fe47172011-09-14 12:39:17 +0530206 /* reprogram default hardware configuration */
207 writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
208 host->ioaddr + S3C64XX_SDHCI_CONTROL4);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100209
Thomas Abraham6fe47172011-09-14 12:39:17 +0530210 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
211 ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
212 S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
213 S3C_SDHCI_CTRL2_ENFBCLKRX |
214 S3C_SDHCI_CTRL2_DFCNT_NONE |
215 S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
216 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100217
Thomas Abraham6fe47172011-09-14 12:39:17 +0530218 /* reconfigure the controller for new clock rate */
219 ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
220 if (clock < 25 * 1000000)
221 ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
222 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
Russell King17710592014-04-25 12:58:55 +0100223
224 sdhci_set_clock(host, clock);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100225}
226
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700227/**
228 * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
229 * @host: The SDHCI host being queried
230 *
231 * To init mmc host properly a minimal clock value is needed. For high system
232 * bus clock's values the standard formula gives values out of allowed range.
233 * The clock still can be set to lower values, if clock source other then
234 * system bus is selected.
235*/
236static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
237{
238 struct sdhci_s3c *ourhost = to_s3c(host);
Tomasz Figa222a13c2014-01-11 22:39:04 +0100239 unsigned long rate, min = ULONG_MAX;
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700240 int src;
241
242 for (src = 0; src < MAX_BUS_CLK; src++) {
Tomasz Figa222a13c2014-01-11 22:39:04 +0100243 rate = ourhost->clk_rates[src] / 256;
244 if (!rate)
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700245 continue;
Tomasz Figa222a13c2014-01-11 22:39:04 +0100246 if (rate < min)
247 min = rate;
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700248 }
Tomasz Figa222a13c2014-01-11 22:39:04 +0100249
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700250 return min;
251}
252
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900253/* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
254static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
255{
256 struct sdhci_s3c *ourhost = to_s3c(host);
Tomasz Figa222a13c2014-01-11 22:39:04 +0100257 unsigned long rate, max = 0;
258 int src;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900259
Tomasz Figa222a13c2014-01-11 22:39:04 +0100260 for (src = 0; src < MAX_BUS_CLK; src++) {
261 struct clk *clk;
262
263 clk = ourhost->clk_bus[src];
264 if (IS_ERR(clk))
265 continue;
266
267 rate = clk_round_rate(clk, ULONG_MAX);
268 if (rate > max)
269 max = rate;
270 }
271
272 return max;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900273}
274
275/* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
276static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
277{
278 struct sdhci_s3c *ourhost = to_s3c(host);
Tomasz Figa222a13c2014-01-11 22:39:04 +0100279 unsigned long rate, min = ULONG_MAX;
280 int src;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900281
Tomasz Figa222a13c2014-01-11 22:39:04 +0100282 for (src = 0; src < MAX_BUS_CLK; src++) {
283 struct clk *clk;
284
285 clk = ourhost->clk_bus[src];
286 if (IS_ERR(clk))
287 continue;
288
289 rate = clk_round_rate(clk, 0);
290 if (rate < min)
291 min = rate;
292 }
293
294 return min;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900295}
296
297/* sdhci_cmu_set_clock - callback on clock change.*/
298static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
299{
300 struct sdhci_s3c *ourhost = to_s3c(host);
Jingoo Han2ad0b242012-08-29 14:35:06 +0900301 struct device *dev = &ourhost->pdev->dev;
Thomas Abraham3119936a2012-02-16 22:23:58 +0900302 unsigned long timeout;
303 u16 clk = 0;
Mark Browncd0cfdd2014-11-04 12:26:42 +0000304 int ret;
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900305
Russell King1650d0c2014-04-25 12:58:50 +0100306 host->mmc->actual_clock = 0;
307
Jaehoon Chung7ef2a5e2013-08-02 23:08:58 +0900308 /* If the clock is going off, set to 0 at clock control register */
309 if (clock == 0) {
310 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900311 return;
Jaehoon Chung7ef2a5e2013-08-02 23:08:58 +0900312 }
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900313
314 sdhci_s3c_set_clock(host, clock);
315
Paul Osmialowski017210d2015-02-04 10:16:59 +0100316 /* Reset SD Clock Enable */
317 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
318 clk &= ~SDHCI_CLOCK_CARD_EN;
319 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
320
321 spin_unlock_irq(&host->lock);
Mark Browncd0cfdd2014-11-04 12:26:42 +0000322 ret = clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
Paul Osmialowski017210d2015-02-04 10:16:59 +0100323 spin_lock_irq(&host->lock);
Mark Browncd0cfdd2014-11-04 12:26:42 +0000324 if (ret != 0) {
325 dev_err(dev, "%s: failed to set clock rate %uHz\n",
326 mmc_hostname(host->mmc), clock);
327 return;
328 }
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900329
Thomas Abraham3119936a2012-02-16 22:23:58 +0900330 clk = SDHCI_CLOCK_INT_EN;
331 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
332
333 /* Wait max 20 ms */
334 timeout = 20;
335 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
336 & SDHCI_CLOCK_INT_STABLE)) {
337 if (timeout == 0) {
Jingoo Han2ad0b242012-08-29 14:35:06 +0900338 dev_err(dev, "%s: Internal clock never stabilised.\n",
339 mmc_hostname(host->mmc));
Thomas Abraham3119936a2012-02-16 22:23:58 +0900340 return;
341 }
342 timeout--;
343 mdelay(1);
344 }
345
346 clk |= SDHCI_CLOCK_CARD_EN;
347 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900348}
349
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900350/**
Russell King2317f562014-04-25 12:57:07 +0100351 * sdhci_s3c_set_bus_width - support 8bit buswidth
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900352 * @host: The SDHCI host being queried
353 * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
354 *
355 * We have 8-bit width support but is not a v3 controller.
Sascha Hauer7bc088d2013-01-21 19:02:27 +0800356 * So we add platform_bus_width() and support 8bit width.
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900357 */
Russell King2317f562014-04-25 12:57:07 +0100358static void sdhci_s3c_set_bus_width(struct sdhci_host *host, int width)
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900359{
360 u8 ctrl;
361
362 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
363
364 switch (width) {
365 case MMC_BUS_WIDTH_8:
366 ctrl |= SDHCI_CTRL_8BITBUS;
367 ctrl &= ~SDHCI_CTRL_4BITBUS;
368 break;
369 case MMC_BUS_WIDTH_4:
370 ctrl |= SDHCI_CTRL_4BITBUS;
371 ctrl &= ~SDHCI_CTRL_8BITBUS;
372 break;
373 default:
Girish K S49bb1e62011-08-26 14:58:18 +0530374 ctrl &= ~SDHCI_CTRL_4BITBUS;
375 ctrl &= ~SDHCI_CTRL_8BITBUS;
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900376 break;
377 }
378
379 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Jaehoon Chung548f07d2011-01-12 11:59:12 +0900380}
381
Ben Dooks0d1bb412009-06-14 13:52:37 +0100382static struct sdhci_ops sdhci_s3c_ops = {
383 .get_max_clock = sdhci_s3c_get_max_clk,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100384 .set_clock = sdhci_s3c_set_clock,
Marek Szyprowskice5f0362010-08-10 18:01:56 -0700385 .get_min_clock = sdhci_s3c_get_min_clock,
Russell King2317f562014-04-25 12:57:07 +0100386 .set_bus_width = sdhci_s3c_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100387 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100388 .set_uhs_signaling = sdhci_set_uhs_signaling,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100389};
390
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000391#ifdef CONFIG_OF
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500392static int sdhci_s3c_parse_dt(struct device *dev,
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000393 struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
394{
395 struct device_node *node = dev->of_node;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000396 u32 max_width;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000397
398 /* if the bus-width property is not specified, assume width as 1 */
399 if (of_property_read_u32(node, "bus-width", &max_width))
400 max_width = 1;
401 pdata->max_width = max_width;
402
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000403 /* get the card detection method */
Tushar Beheraab5023e2012-11-20 09:41:53 +0530404 if (of_get_property(node, "broken-cd", NULL)) {
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000405 pdata->cd_type = S3C_SDHCI_CD_NONE;
Thomas Abrahame19499a2013-03-06 17:06:16 +0530406 return 0;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000407 }
408
Tushar Beheraab5023e2012-11-20 09:41:53 +0530409 if (of_get_property(node, "non-removable", NULL)) {
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000410 pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
Thomas Abrahame19499a2013-03-06 17:06:16 +0530411 return 0;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000412 }
413
Jaehoon Chung11bc9382014-05-26 13:58:28 +0900414 if (of_get_named_gpio(node, "cd-gpios", 0))
Thomas Abrahame19499a2013-03-06 17:06:16 +0530415 return 0;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000416
Tomasz Figab96efcc2012-11-16 15:28:17 +0100417 /* assuming internal card detect that will be configured by pinctrl */
418 pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000419 return 0;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000420}
421#else
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500422static int sdhci_s3c_parse_dt(struct device *dev,
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000423 struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
424{
425 return -EINVAL;
426}
427#endif
428
429static const struct of_device_id sdhci_s3c_dt_match[];
430
Thomas Abraham3119936a2012-02-16 22:23:58 +0900431static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
432 struct platform_device *pdev)
433{
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000434#ifdef CONFIG_OF
435 if (pdev->dev.of_node) {
436 const struct of_device_id *match;
437 match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
438 return (struct sdhci_s3c_drv_data *)match->data;
439 }
440#endif
Thomas Abraham3119936a2012-02-16 22:23:58 +0900441 return (struct sdhci_s3c_drv_data *)
442 platform_get_device_id(pdev)->driver_data;
443}
444
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500445static int sdhci_s3c_probe(struct platform_device *pdev)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100446{
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900447 struct s3c_sdhci_platdata *pdata;
Thomas Abraham3119936a2012-02-16 22:23:58 +0900448 struct sdhci_s3c_drv_data *drv_data;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100449 struct device *dev = &pdev->dev;
450 struct sdhci_host *host;
451 struct sdhci_s3c *sc;
452 struct resource *res;
453 int ret, irq, ptr, clks;
454
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000455 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
Ben Dooks0d1bb412009-06-14 13:52:37 +0100456 dev_err(dev, "no device data specified\n");
457 return -ENOENT;
458 }
459
460 irq = platform_get_irq(pdev, 0);
461 if (irq < 0) {
462 dev_err(dev, "no irq specified\n");
463 return irq;
464 }
465
Ben Dooks0d1bb412009-06-14 13:52:37 +0100466 host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
467 if (IS_ERR(host)) {
468 dev_err(dev, "sdhci_alloc_host() failed\n");
469 return PTR_ERR(host);
470 }
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000471 sc = sdhci_priv(host);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100472
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900473 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
474 if (!pdata) {
475 ret = -ENOMEM;
Tomasz Figab1b8fea2012-11-25 15:40:44 -0500476 goto err_pdata_io_clk;
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900477 }
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000478
479 if (pdev->dev.of_node) {
480 ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
481 if (ret)
Tomasz Figab1b8fea2012-11-25 15:40:44 -0500482 goto err_pdata_io_clk;
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000483 } else {
484 memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
485 sc->ext_cd_gpio = -1; /* invalid gpio number */
486 }
Thomas Abraham1d4dc332012-02-16 22:23:59 +0900487
Thomas Abraham3119936a2012-02-16 22:23:58 +0900488 drv_data = sdhci_s3c_get_driver_data(pdev);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100489
490 sc->host = host;
491 sc->pdev = pdev;
492 sc->pdata = pdata;
Tomasz Figa3ac147f2014-01-11 22:39:05 +0100493 sc->cur_clk = -1;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100494
495 platform_set_drvdata(pdev, host);
496
Jingoo Han3aaf7ba2013-02-12 12:24:39 +0900497 sc->clk_io = devm_clk_get(dev, "hsmmc");
Ben Dooks0d1bb412009-06-14 13:52:37 +0100498 if (IS_ERR(sc->clk_io)) {
499 dev_err(dev, "failed to get io clock\n");
500 ret = PTR_ERR(sc->clk_io);
Tomasz Figab1b8fea2012-11-25 15:40:44 -0500501 goto err_pdata_io_clk;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100502 }
503
504 /* enable the local io clock and keep it running for the moment. */
Thomas Abraham0f310a052012-10-03 08:35:43 +0900505 clk_prepare_enable(sc->clk_io);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100506
507 for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
Rajeshwari Shinde4346b6d2011-11-03 10:52:58 +0900508 char name[14];
Ben Dooks0d1bb412009-06-14 13:52:37 +0100509
Rajeshwari Shinde4346b6d2011-11-03 10:52:58 +0900510 snprintf(name, 14, "mmc_busclk.%d", ptr);
Tomasz Figa8f4b78d2014-01-11 22:39:03 +0100511 sc->clk_bus[ptr] = devm_clk_get(dev, name);
512 if (IS_ERR(sc->clk_bus[ptr]))
Ben Dooks0d1bb412009-06-14 13:52:37 +0100513 continue;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100514
515 clks++;
Tomasz Figa6eb28bd2014-01-11 22:39:02 +0100516 sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);
517
Ben Dooks0d1bb412009-06-14 13:52:37 +0100518 dev_info(dev, "clock source %d: %s (%ld Hz)\n",
Tomasz Figa6eb28bd2014-01-11 22:39:02 +0100519 ptr, name, sc->clk_rates[ptr]);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100520 }
521
522 if (clks == 0) {
523 dev_err(dev, "failed to find any bus clocks\n");
524 ret = -ENOENT;
525 goto err_no_busclks;
526 }
527
Julia Lawall9bda6da2012-03-08 23:24:53 -0500528 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redinga3e2cd72013-01-21 11:09:11 +0100529 host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
530 if (IS_ERR(host->ioaddr)) {
531 ret = PTR_ERR(host->ioaddr);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100532 goto err_req_regs;
533 }
534
535 /* Ensure we have minimal gpio selected CMD/CLK/Detect */
536 if (pdata->cfg_gpio)
537 pdata->cfg_gpio(pdev, pdata->max_width);
538
539 host->hw_name = "samsung-hsmmc";
540 host->ops = &sdhci_s3c_ops;
541 host->quirks = 0;
Jaehoon Chung285e2442013-08-02 23:09:00 +0900542 host->quirks2 = 0;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100543 host->irq = irq;
544
545 /* Setup quirks for the controller */
Thomas Abrahamb2e75ef2010-05-26 14:42:05 -0700546 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
Marek Szyprowskia1d56462010-08-10 18:01:57 -0700547 host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
Russell King17710592014-04-25 12:58:55 +0100548 if (drv_data) {
Thomas Abraham3119936a2012-02-16 22:23:58 +0900549 host->quirks |= drv_data->sdhci_quirks;
Russell King17710592014-04-25 12:58:55 +0100550 sc->no_divider = drv_data->no_divider;
551 }
Ben Dooks0d1bb412009-06-14 13:52:37 +0100552
553#ifndef CONFIG_MMC_SDHCI_S3C_DMA
554
555 /* we currently see overruns on errors, so disable the SDMA
556 * support as well. */
557 host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
558
Ben Dooks0d1bb412009-06-14 13:52:37 +0100559#endif /* CONFIG_MMC_SDHCI_S3C_DMA */
560
561 /* It seems we do not get an DATA transfer complete on non-busy
562 * transfers, not sure if this is a problem with this specific
563 * SDHCI block, or a missing configuration that needs to be set. */
564 host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
565
Kyungmin Park732f0e32010-10-30 12:58:56 +0900566 /* This host supports the Auto CMD12 */
567 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
568
Jaehoon Chung7199e2b2011-07-12 17:30:47 +0900569 /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
570 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
571
Marek Szyprowski17866e12010-08-10 18:01:58 -0700572 if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
573 pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
574 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
575
576 if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
577 host->mmc->caps = MMC_CAP_NONREMOVABLE;
578
Thomas Abraham0d22c772012-03-31 23:29:45 -0400579 switch (pdata->max_width) {
580 case 8:
581 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
582 case 4:
583 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
584 break;
585 }
586
Sangwook Leefa1773c2011-11-07 17:05:22 +0000587 if (pdata->pm_caps)
588 host->mmc->pm_caps |= pdata->pm_caps;
589
Ben Dooks0d1bb412009-06-14 13:52:37 +0100590 host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
591 SDHCI_QUIRK_32BIT_DMA_SIZE);
592
Hyuk Lee3fe42e02010-08-10 18:01:55 -0700593 /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
594 host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
595
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900596 /*
597 * If controller does not have internal clock divider,
598 * we can use overriding functions instead of default.
599 */
Russell King17710592014-04-25 12:58:55 +0100600 if (sc->no_divider) {
Jeongbae Seo253e0a72010-10-08 17:46:21 +0900601 sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
602 sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
603 sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
604 }
605
Jeongbae Seob3824f22010-10-08 17:46:20 +0900606 /* It supports additional host capabilities if needed */
607 if (pdata->host_caps)
608 host->mmc->caps |= pdata->host_caps;
609
Jaehoon Chungc1c4b662012-02-07 15:59:01 +0900610 if (pdata->host_caps2)
611 host->mmc->caps2 |= pdata->host_caps2;
612
Mark Brown9f4e8152012-03-31 23:31:55 -0400613 pm_runtime_enable(&pdev->dev);
614 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
615 pm_runtime_use_autosuspend(&pdev->dev);
616 pm_suspend_ignore_children(&pdev->dev, 1);
617
Ulf Hanssonf8e32602014-12-18 10:41:42 +0100618 ret = mmc_of_parse(host->mmc);
619 if (ret)
620 goto err_req_regs;
Jaehoon Chung11bc9382014-05-26 13:58:28 +0900621
Ben Dooks0d1bb412009-06-14 13:52:37 +0100622 ret = sdhci_add_host(host);
623 if (ret) {
624 dev_err(dev, "sdhci_add_host() failed\n");
Julia Lawall9bda6da2012-03-08 23:24:53 -0500625 goto err_req_regs;
Ben Dooks0d1bb412009-06-14 13:52:37 +0100626 }
627
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100628#ifdef CONFIG_PM
Seungwon Jeon0aa55c22012-10-30 14:28:36 +0900629 if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
630 clk_disable_unprepare(sc->clk_io);
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000631#endif
Ben Dooks0d1bb412009-06-14 13:52:37 +0100632 return 0;
633
Ben Dooks0d1bb412009-06-14 13:52:37 +0100634 err_req_regs:
Bartlomiej Zolnierkiewicz221414d2014-08-07 18:07:07 +0200635 pm_runtime_disable(&pdev->dev);
636
Ben Dooks0d1bb412009-06-14 13:52:37 +0100637 err_no_busclks:
Thomas Abraham0f310a052012-10-03 08:35:43 +0900638 clk_disable_unprepare(sc->clk_io);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100639
Tomasz Figab1b8fea2012-11-25 15:40:44 -0500640 err_pdata_io_clk:
Ben Dooks0d1bb412009-06-14 13:52:37 +0100641 sdhci_free_host(host);
642
643 return ret;
644}
645
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500646static int sdhci_s3c_remove(struct platform_device *pdev)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100647{
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700648 struct sdhci_host *host = platform_get_drvdata(pdev);
649 struct sdhci_s3c *sc = sdhci_priv(host);
Marek Szyprowski17866e12010-08-10 18:01:58 -0700650
651 if (sc->ext_cd_irq)
652 free_irq(sc->ext_cd_irq, sc);
653
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100654#ifdef CONFIG_PM
Jaehoon Chung11bc9382014-05-26 13:58:28 +0900655 if (sc->pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
Seungwon Jeon0aa55c22012-10-30 14:28:36 +0900656 clk_prepare_enable(sc->clk_io);
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000657#endif
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700658 sdhci_remove_host(host, 1);
659
Chander Kashyap387a8cbd2012-09-14 09:08:50 +0000660 pm_runtime_dont_use_autosuspend(&pdev->dev);
Mark Brown9f4e8152012-03-31 23:31:55 -0400661 pm_runtime_disable(&pdev->dev);
662
Thomas Abraham0f310a052012-10-03 08:35:43 +0900663 clk_disable_unprepare(sc->clk_io);
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700664
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700665 sdhci_free_host(host);
Marek Szyprowski9d51a6b2010-07-20 13:24:33 -0700666
Ben Dooks0d1bb412009-06-14 13:52:37 +0100667 return 0;
668}
669
Mark Brownd5e9c022012-03-03 00:46:41 +0000670#ifdef CONFIG_PM_SLEEP
Manuel Lauss29495aa2011-11-03 11:09:45 +0100671static int sdhci_s3c_suspend(struct device *dev)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100672{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100673 struct sdhci_host *host = dev_get_drvdata(dev);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100674
Manuel Lauss29495aa2011-11-03 11:09:45 +0100675 return sdhci_suspend_host(host);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100676}
677
Manuel Lauss29495aa2011-11-03 11:09:45 +0100678static int sdhci_s3c_resume(struct device *dev)
Ben Dooks0d1bb412009-06-14 13:52:37 +0100679{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100680 struct sdhci_host *host = dev_get_drvdata(dev);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100681
Wonil Choi65d13512011-06-29 11:38:38 +0900682 return sdhci_resume_host(host);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100683}
Mark Brownd5e9c022012-03-03 00:46:41 +0000684#endif
Ben Dooks0d1bb412009-06-14 13:52:37 +0100685
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100686#ifdef CONFIG_PM
Mark Brown9f4e8152012-03-31 23:31:55 -0400687static int sdhci_s3c_runtime_suspend(struct device *dev)
688{
689 struct sdhci_host *host = dev_get_drvdata(dev);
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000690 struct sdhci_s3c *ourhost = to_s3c(host);
691 struct clk *busclk = ourhost->clk_io;
692 int ret;
Mark Brown9f4e8152012-03-31 23:31:55 -0400693
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000694 ret = sdhci_runtime_suspend_host(host);
695
Tomasz Figa3ac147f2014-01-11 22:39:05 +0100696 if (ourhost->cur_clk >= 0)
697 clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
Thomas Abraham0f310a052012-10-03 08:35:43 +0900698 clk_disable_unprepare(busclk);
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000699 return ret;
Mark Brown9f4e8152012-03-31 23:31:55 -0400700}
701
702static int sdhci_s3c_runtime_resume(struct device *dev)
703{
704 struct sdhci_host *host = dev_get_drvdata(dev);
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000705 struct sdhci_s3c *ourhost = to_s3c(host);
706 struct clk *busclk = ourhost->clk_io;
707 int ret;
Mark Brown9f4e8152012-03-31 23:31:55 -0400708
Thomas Abraham0f310a052012-10-03 08:35:43 +0900709 clk_prepare_enable(busclk);
Tomasz Figa3ac147f2014-01-11 22:39:05 +0100710 if (ourhost->cur_clk >= 0)
711 clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
Chander Kashyap2abeb5c2012-09-21 05:42:08 +0000712 ret = sdhci_runtime_resume_host(host);
713 return ret;
Mark Brown9f4e8152012-03-31 23:31:55 -0400714}
715#endif
716
Mark Brownd5e9c022012-03-03 00:46:41 +0000717#ifdef CONFIG_PM
Manuel Lauss29495aa2011-11-03 11:09:45 +0100718static const struct dev_pm_ops sdhci_s3c_pmops = {
Mark Brownd5e9c022012-03-03 00:46:41 +0000719 SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
Mark Brown9f4e8152012-03-31 23:31:55 -0400720 SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
721 NULL)
Manuel Lauss29495aa2011-11-03 11:09:45 +0100722};
723
724#define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
725
Ben Dooks0d1bb412009-06-14 13:52:37 +0100726#else
Manuel Lauss29495aa2011-11-03 11:09:45 +0100727#define SDHCI_S3C_PMOPS NULL
Ben Dooks0d1bb412009-06-14 13:52:37 +0100728#endif
729
Thomas Abraham3119936a2012-02-16 22:23:58 +0900730#if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
731static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
Russell King17710592014-04-25 12:58:55 +0100732 .no_divider = true,
Thomas Abraham3119936a2012-02-16 22:23:58 +0900733};
734#define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
735#else
736#define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
737#endif
738
Krzysztof Kozlowski4d0aa492015-05-02 00:49:22 +0900739static const struct platform_device_id sdhci_s3c_driver_ids[] = {
Thomas Abraham3119936a2012-02-16 22:23:58 +0900740 {
741 .name = "s3c-sdhci",
742 .driver_data = (kernel_ulong_t)NULL,
743 }, {
744 .name = "exynos4-sdhci",
745 .driver_data = EXYNOS4_SDHCI_DRV_DATA,
746 },
747 { }
748};
749MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
750
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000751#ifdef CONFIG_OF
752static const struct of_device_id sdhci_s3c_dt_match[] = {
753 { .compatible = "samsung,s3c6410-sdhci", },
754 { .compatible = "samsung,exynos4210-sdhci",
755 .data = (void *)EXYNOS4_SDHCI_DRV_DATA },
756 {},
757};
758MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
759#endif
760
Ben Dooks0d1bb412009-06-14 13:52:37 +0100761static struct platform_driver sdhci_s3c_driver = {
762 .probe = sdhci_s3c_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500763 .remove = sdhci_s3c_remove,
Thomas Abraham3119936a2012-02-16 22:23:58 +0900764 .id_table = sdhci_s3c_driver_ids,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100765 .driver = {
Ben Dooks0d1bb412009-06-14 13:52:37 +0100766 .name = "s3c-sdhci",
Thomas Abrahamcd1b00e2012-08-23 17:10:09 +0000767 .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
Manuel Lauss29495aa2011-11-03 11:09:45 +0100768 .pm = SDHCI_S3C_PMOPS,
Ben Dooks0d1bb412009-06-14 13:52:37 +0100769 },
770};
771
Axel Lind1f81a62011-11-26 12:55:43 +0800772module_platform_driver(sdhci_s3c_driver);
Ben Dooks0d1bb412009-06-14 13:52:37 +0100773
774MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
775MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
776MODULE_LICENSE("GPL v2");
777MODULE_ALIAS("platform:s3c-sdhci");