Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 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 | |
Paul Osmialowski | 017210d | 2015-02-04 10:16:59 +0100 | [diff] [blame] | 15 | #include <linux/spinlock.h> |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 16 | #include <linux/delay.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/platform_device.h> |
Arnd Bergmann | cc014f3 | 2013-03-04 18:28:21 +0100 | [diff] [blame] | 19 | #include <linux/platform_data/mmc-sdhci-s3c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 21 | #include <linux/clk.h> |
| 22 | #include <linux/io.h> |
Marek Szyprowski | 17866e1 | 2010-08-10 18:01:58 -0700 | [diff] [blame] | 23 | #include <linux/gpio.h> |
Mark Brown | 55156d2 | 2011-07-29 15:35:00 +0100 | [diff] [blame] | 24 | #include <linux/module.h> |
Mark Brown | d5e9c02 | 2012-03-03 00:46:41 +0000 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_gpio.h> |
| 27 | #include <linux/pm.h> |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 29 | |
| 30 | #include <linux/mmc/host.h> |
| 31 | |
Arnd Bergmann | cc014f3 | 2013-03-04 18:28:21 +0100 | [diff] [blame] | 32 | #include "sdhci-s3c-regs.h" |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 33 | #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 | */ |
| 47 | struct sdhci_s3c { |
| 48 | struct sdhci_host *host; |
| 49 | struct platform_device *pdev; |
| 50 | struct resource *ioarea; |
| 51 | struct s3c_sdhci_platdata *pdata; |
Tomasz Figa | 3ac147f | 2014-01-11 22:39:05 +0100 | [diff] [blame] | 52 | int cur_clk; |
Marek Szyprowski | 17866e1 | 2010-08-10 18:01:58 -0700 | [diff] [blame] | 53 | int ext_cd_irq; |
| 54 | int ext_cd_gpio; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 55 | |
| 56 | struct clk *clk_io; |
| 57 | struct clk *clk_bus[MAX_BUS_CLK]; |
Tomasz Figa | 6eb28bd | 2014-01-11 22:39:02 +0100 | [diff] [blame] | 58 | unsigned long clk_rates[MAX_BUS_CLK]; |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 59 | |
| 60 | bool no_divider; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 63 | /** |
| 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 | */ |
| 71 | struct sdhci_s3c_drv_data { |
| 72 | unsigned int sdhci_quirks; |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 73 | bool no_divider; |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 76 | static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host) |
| 77 | { |
| 78 | return sdhci_priv(host); |
| 79 | } |
| 80 | |
| 81 | /** |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 82 | * 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 | */ |
| 87 | static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host) |
| 88 | { |
| 89 | struct sdhci_s3c *ourhost = to_s3c(host); |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 90 | unsigned long rate, max = 0; |
| 91 | int src; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 92 | |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 93 | for (src = 0; src < MAX_BUS_CLK; src++) { |
| 94 | rate = ourhost->clk_rates[src]; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 95 | if (rate > max) |
| 96 | max = rate; |
| 97 | } |
| 98 | |
| 99 | return max; |
| 100 | } |
| 101 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 102 | /** |
| 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 | */ |
| 108 | static 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 Figa | 8880a4a | 2014-01-11 22:39:01 +0100 | [diff] [blame] | 114 | int shift; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 115 | |
Tomasz Figa | 8f4b78d | 2014-01-11 22:39:03 +0100 | [diff] [blame] | 116 | if (IS_ERR(clksrc)) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 117 | return UINT_MAX; |
| 118 | |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 119 | /* |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 120 | * If controller uses a non-standard clock division, find the best clock |
| 121 | * speed possible with selected clock source and skip the division. |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 122 | */ |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 123 | if (ourhost->no_divider) { |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 124 | rate = clk_round_rate(clksrc, wanted); |
| 125 | return wanted - rate; |
| 126 | } |
| 127 | |
Tomasz Figa | 6eb28bd | 2014-01-11 22:39:02 +0100 | [diff] [blame] | 128 | rate = ourhost->clk_rates[src]; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 129 | |
Tomasz Figa | 2200300 | 2014-01-11 22:39:06 +0100 | [diff] [blame] | 130 | for (shift = 0; shift <= 8; ++shift) { |
Tomasz Figa | 8880a4a | 2014-01-11 22:39:01 +0100 | [diff] [blame] | 131 | if ((rate >> shift) <= wanted) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | |
Tomasz Figa | 2200300 | 2014-01-11 22:39:06 +0100 | [diff] [blame] | 135 | 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 Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 142 | dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n", |
Tomasz Figa | 8880a4a | 2014-01-11 22:39:01 +0100 | [diff] [blame] | 143 | src, rate, wanted, rate >> shift); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 144 | |
Tomasz Figa | 8880a4a | 2014-01-11 22:39:01 +0100 | [diff] [blame] | 145 | return wanted - (rate >> shift); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 146 | } |
| 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 | */ |
| 156 | static 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 King | 1650d0c | 2014-04-25 12:58:50 +0100 | [diff] [blame] | 165 | host->mmc->actual_clock = 0; |
| 166 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 167 | /* don't bother if the clock is going off. */ |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 168 | if (clock == 0) { |
| 169 | sdhci_set_clock(host, clock); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 170 | return; |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 171 | } |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 172 | |
| 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 Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 186 | if (ourhost->cur_clk != best_src) { |
| 187 | struct clk *clk = ourhost->clk_bus[best_src]; |
| 188 | |
Thomas Abraham | 0f310a05 | 2012-10-03 08:35:43 +0900 | [diff] [blame] | 189 | clk_prepare_enable(clk); |
Tomasz Figa | 3ac147f | 2014-01-11 22:39:05 +0100 | [diff] [blame] | 190 | if (ourhost->cur_clk >= 0) |
| 191 | clk_disable_unprepare( |
| 192 | ourhost->clk_bus[ourhost->cur_clk]); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 193 | |
| 194 | ourhost->cur_clk = best_src; |
Tomasz Figa | 6eb28bd | 2014-01-11 22:39:02 +0100 | [diff] [blame] | 195 | host->max_clk = ourhost->clk_rates[best_src]; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Tomasz Figa | 3ac147f | 2014-01-11 22:39:05 +0100 | [diff] [blame] | 198 | /* 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 Abraham | 6fe4717 | 2011-09-14 12:39:17 +0530 | [diff] [blame] | 206 | /* reprogram default hardware configuration */ |
| 207 | writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA, |
| 208 | host->ioaddr + S3C64XX_SDHCI_CONTROL4); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 209 | |
Thomas Abraham | 6fe4717 | 2011-09-14 12:39:17 +0530 | [diff] [blame] | 210 | 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 Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 217 | |
Thomas Abraham | 6fe4717 | 2011-09-14 12:39:17 +0530 | [diff] [blame] | 218 | /* 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 King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 223 | |
| 224 | sdhci_set_clock(host, clock); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 227 | /** |
| 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 | */ |
| 236 | static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host) |
| 237 | { |
| 238 | struct sdhci_s3c *ourhost = to_s3c(host); |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 239 | unsigned long rate, min = ULONG_MAX; |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 240 | int src; |
| 241 | |
| 242 | for (src = 0; src < MAX_BUS_CLK; src++) { |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 243 | rate = ourhost->clk_rates[src] / 256; |
| 244 | if (!rate) |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 245 | continue; |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 246 | if (rate < min) |
| 247 | min = rate; |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 248 | } |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 249 | |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 250 | return min; |
| 251 | } |
| 252 | |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 253 | /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/ |
| 254 | static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host) |
| 255 | { |
| 256 | struct sdhci_s3c *ourhost = to_s3c(host); |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 257 | unsigned long rate, max = 0; |
| 258 | int src; |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 259 | |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 260 | 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 Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */ |
| 276 | static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host) |
| 277 | { |
| 278 | struct sdhci_s3c *ourhost = to_s3c(host); |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 279 | unsigned long rate, min = ULONG_MAX; |
| 280 | int src; |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 281 | |
Tomasz Figa | 222a13c | 2014-01-11 22:39:04 +0100 | [diff] [blame] | 282 | 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 Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* sdhci_cmu_set_clock - callback on clock change.*/ |
| 298 | static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock) |
| 299 | { |
| 300 | struct sdhci_s3c *ourhost = to_s3c(host); |
Jingoo Han | 2ad0b24 | 2012-08-29 14:35:06 +0900 | [diff] [blame] | 301 | struct device *dev = &ourhost->pdev->dev; |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 302 | unsigned long timeout; |
| 303 | u16 clk = 0; |
Mark Brown | cd0cfdd | 2014-11-04 12:26:42 +0000 | [diff] [blame] | 304 | int ret; |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 305 | |
Russell King | 1650d0c | 2014-04-25 12:58:50 +0100 | [diff] [blame] | 306 | host->mmc->actual_clock = 0; |
| 307 | |
Jaehoon Chung | 7ef2a5e | 2013-08-02 23:08:58 +0900 | [diff] [blame] | 308 | /* 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 Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 311 | return; |
Jaehoon Chung | 7ef2a5e | 2013-08-02 23:08:58 +0900 | [diff] [blame] | 312 | } |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 313 | |
| 314 | sdhci_s3c_set_clock(host, clock); |
| 315 | |
Paul Osmialowski | 017210d | 2015-02-04 10:16:59 +0100 | [diff] [blame] | 316 | /* 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 Brown | cd0cfdd | 2014-11-04 12:26:42 +0000 | [diff] [blame] | 322 | ret = clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock); |
Paul Osmialowski | 017210d | 2015-02-04 10:16:59 +0100 | [diff] [blame] | 323 | spin_lock_irq(&host->lock); |
Mark Brown | cd0cfdd | 2014-11-04 12:26:42 +0000 | [diff] [blame] | 324 | 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 Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 329 | |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 330 | 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 Han | 2ad0b24 | 2012-08-29 14:35:06 +0900 | [diff] [blame] | 338 | dev_err(dev, "%s: Internal clock never stabilised.\n", |
| 339 | mmc_hostname(host->mmc)); |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | timeout--; |
| 343 | mdelay(1); |
| 344 | } |
| 345 | |
| 346 | clk |= SDHCI_CLOCK_CARD_EN; |
| 347 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 348 | } |
| 349 | |
Jaehoon Chung | 548f07d | 2011-01-12 11:59:12 +0900 | [diff] [blame] | 350 | /** |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 351 | * sdhci_s3c_set_bus_width - support 8bit buswidth |
Jaehoon Chung | 548f07d | 2011-01-12 11:59:12 +0900 | [diff] [blame] | 352 | * @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 Hauer | 7bc088d | 2013-01-21 19:02:27 +0800 | [diff] [blame] | 356 | * So we add platform_bus_width() and support 8bit width. |
Jaehoon Chung | 548f07d | 2011-01-12 11:59:12 +0900 | [diff] [blame] | 357 | */ |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 358 | static void sdhci_s3c_set_bus_width(struct sdhci_host *host, int width) |
Jaehoon Chung | 548f07d | 2011-01-12 11:59:12 +0900 | [diff] [blame] | 359 | { |
| 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 S | 49bb1e6 | 2011-08-26 14:58:18 +0530 | [diff] [blame] | 374 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 375 | ctrl &= ~SDHCI_CTRL_8BITBUS; |
Jaehoon Chung | 548f07d | 2011-01-12 11:59:12 +0900 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | |
| 379 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Jaehoon Chung | 548f07d | 2011-01-12 11:59:12 +0900 | [diff] [blame] | 380 | } |
| 381 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 382 | static struct sdhci_ops sdhci_s3c_ops = { |
| 383 | .get_max_clock = sdhci_s3c_get_max_clk, |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 384 | .set_clock = sdhci_s3c_set_clock, |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 385 | .get_min_clock = sdhci_s3c_get_min_clock, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 386 | .set_bus_width = sdhci_s3c_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 387 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 388 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 389 | }; |
| 390 | |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 391 | #ifdef CONFIG_OF |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 392 | static int sdhci_s3c_parse_dt(struct device *dev, |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 393 | struct sdhci_host *host, struct s3c_sdhci_platdata *pdata) |
| 394 | { |
| 395 | struct device_node *node = dev->of_node; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 396 | u32 max_width; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 397 | |
| 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 Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 403 | /* get the card detection method */ |
Tushar Behera | ab5023e | 2012-11-20 09:41:53 +0530 | [diff] [blame] | 404 | if (of_get_property(node, "broken-cd", NULL)) { |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 405 | pdata->cd_type = S3C_SDHCI_CD_NONE; |
Thomas Abraham | e19499a | 2013-03-06 17:06:16 +0530 | [diff] [blame] | 406 | return 0; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Tushar Behera | ab5023e | 2012-11-20 09:41:53 +0530 | [diff] [blame] | 409 | if (of_get_property(node, "non-removable", NULL)) { |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 410 | pdata->cd_type = S3C_SDHCI_CD_PERMANENT; |
Thomas Abraham | e19499a | 2013-03-06 17:06:16 +0530 | [diff] [blame] | 411 | return 0; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Jaehoon Chung | 11bc938 | 2014-05-26 13:58:28 +0900 | [diff] [blame] | 414 | if (of_get_named_gpio(node, "cd-gpios", 0)) |
Thomas Abraham | e19499a | 2013-03-06 17:06:16 +0530 | [diff] [blame] | 415 | return 0; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 416 | |
Tomasz Figa | b96efcc | 2012-11-16 15:28:17 +0100 | [diff] [blame] | 417 | /* assuming internal card detect that will be configured by pinctrl */ |
| 418 | pdata->cd_type = S3C_SDHCI_CD_INTERNAL; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 419 | return 0; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 420 | } |
| 421 | #else |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 422 | static int sdhci_s3c_parse_dt(struct device *dev, |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 423 | struct sdhci_host *host, struct s3c_sdhci_platdata *pdata) |
| 424 | { |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | static const struct of_device_id sdhci_s3c_dt_match[]; |
| 430 | |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 431 | static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data( |
| 432 | struct platform_device *pdev) |
| 433 | { |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 434 | #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 Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 441 | return (struct sdhci_s3c_drv_data *) |
| 442 | platform_get_device_id(pdev)->driver_data; |
| 443 | } |
| 444 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 445 | static int sdhci_s3c_probe(struct platform_device *pdev) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 446 | { |
Thomas Abraham | 1d4dc33 | 2012-02-16 22:23:59 +0900 | [diff] [blame] | 447 | struct s3c_sdhci_platdata *pdata; |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 448 | struct sdhci_s3c_drv_data *drv_data; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 449 | 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 Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 455 | if (!pdev->dev.platform_data && !pdev->dev.of_node) { |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 456 | 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 Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 466 | 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 Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 471 | sc = sdhci_priv(host); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 472 | |
Thomas Abraham | 1d4dc33 | 2012-02-16 22:23:59 +0900 | [diff] [blame] | 473 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 474 | if (!pdata) { |
| 475 | ret = -ENOMEM; |
Tomasz Figa | b1b8fea | 2012-11-25 15:40:44 -0500 | [diff] [blame] | 476 | goto err_pdata_io_clk; |
Thomas Abraham | 1d4dc33 | 2012-02-16 22:23:59 +0900 | [diff] [blame] | 477 | } |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 478 | |
| 479 | if (pdev->dev.of_node) { |
| 480 | ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata); |
| 481 | if (ret) |
Tomasz Figa | b1b8fea | 2012-11-25 15:40:44 -0500 | [diff] [blame] | 482 | goto err_pdata_io_clk; |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 483 | } else { |
| 484 | memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata)); |
| 485 | sc->ext_cd_gpio = -1; /* invalid gpio number */ |
| 486 | } |
Thomas Abraham | 1d4dc33 | 2012-02-16 22:23:59 +0900 | [diff] [blame] | 487 | |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 488 | drv_data = sdhci_s3c_get_driver_data(pdev); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 489 | |
| 490 | sc->host = host; |
| 491 | sc->pdev = pdev; |
| 492 | sc->pdata = pdata; |
Tomasz Figa | 3ac147f | 2014-01-11 22:39:05 +0100 | [diff] [blame] | 493 | sc->cur_clk = -1; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 494 | |
| 495 | platform_set_drvdata(pdev, host); |
| 496 | |
Jingoo Han | 3aaf7ba | 2013-02-12 12:24:39 +0900 | [diff] [blame] | 497 | sc->clk_io = devm_clk_get(dev, "hsmmc"); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 498 | if (IS_ERR(sc->clk_io)) { |
| 499 | dev_err(dev, "failed to get io clock\n"); |
| 500 | ret = PTR_ERR(sc->clk_io); |
Tomasz Figa | b1b8fea | 2012-11-25 15:40:44 -0500 | [diff] [blame] | 501 | goto err_pdata_io_clk; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* enable the local io clock and keep it running for the moment. */ |
Thomas Abraham | 0f310a05 | 2012-10-03 08:35:43 +0900 | [diff] [blame] | 505 | clk_prepare_enable(sc->clk_io); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 506 | |
| 507 | for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) { |
Rajeshwari Shinde | 4346b6d | 2011-11-03 10:52:58 +0900 | [diff] [blame] | 508 | char name[14]; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 509 | |
Rajeshwari Shinde | 4346b6d | 2011-11-03 10:52:58 +0900 | [diff] [blame] | 510 | snprintf(name, 14, "mmc_busclk.%d", ptr); |
Tomasz Figa | 8f4b78d | 2014-01-11 22:39:03 +0100 | [diff] [blame] | 511 | sc->clk_bus[ptr] = devm_clk_get(dev, name); |
| 512 | if (IS_ERR(sc->clk_bus[ptr])) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 513 | continue; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 514 | |
| 515 | clks++; |
Tomasz Figa | 6eb28bd | 2014-01-11 22:39:02 +0100 | [diff] [blame] | 516 | sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]); |
| 517 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 518 | dev_info(dev, "clock source %d: %s (%ld Hz)\n", |
Tomasz Figa | 6eb28bd | 2014-01-11 22:39:02 +0100 | [diff] [blame] | 519 | ptr, name, sc->clk_rates[ptr]); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 520 | } |
| 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 Lawall | 9bda6da | 2012-03-08 23:24:53 -0500 | [diff] [blame] | 528 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | a3e2cd7 | 2013-01-21 11:09:11 +0100 | [diff] [blame] | 529 | host->ioaddr = devm_ioremap_resource(&pdev->dev, res); |
| 530 | if (IS_ERR(host->ioaddr)) { |
| 531 | ret = PTR_ERR(host->ioaddr); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 532 | 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 Chung | 285e244 | 2013-08-02 23:09:00 +0900 | [diff] [blame] | 542 | host->quirks2 = 0; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 543 | host->irq = irq; |
| 544 | |
| 545 | /* Setup quirks for the controller */ |
Thomas Abraham | b2e75ef | 2010-05-26 14:42:05 -0700 | [diff] [blame] | 546 | host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC; |
Marek Szyprowski | a1d5646 | 2010-08-10 18:01:57 -0700 | [diff] [blame] | 547 | host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT; |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 548 | if (drv_data) { |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 549 | host->quirks |= drv_data->sdhci_quirks; |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 550 | sc->no_divider = drv_data->no_divider; |
| 551 | } |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 552 | |
| 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 Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 559 | #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 Park | 732f0e3 | 2010-10-30 12:58:56 +0900 | [diff] [blame] | 566 | /* This host supports the Auto CMD12 */ |
| 567 | host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12; |
| 568 | |
Jaehoon Chung | 7199e2b | 2011-07-12 17:30:47 +0900 | [diff] [blame] | 569 | /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */ |
| 570 | host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC; |
| 571 | |
Marek Szyprowski | 17866e1 | 2010-08-10 18:01:58 -0700 | [diff] [blame] | 572 | 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 Abraham | 0d22c77 | 2012-03-31 23:29:45 -0400 | [diff] [blame] | 579 | 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 Lee | fa1773c | 2011-11-07 17:05:22 +0000 | [diff] [blame] | 587 | if (pdata->pm_caps) |
| 588 | host->mmc->pm_caps |= pdata->pm_caps; |
| 589 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 590 | host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR | |
| 591 | SDHCI_QUIRK_32BIT_DMA_SIZE); |
| 592 | |
Hyuk Lee | 3fe42e0 | 2010-08-10 18:01:55 -0700 | [diff] [blame] | 593 | /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */ |
| 594 | host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK; |
| 595 | |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 596 | /* |
| 597 | * If controller does not have internal clock divider, |
| 598 | * we can use overriding functions instead of default. |
| 599 | */ |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 600 | if (sc->no_divider) { |
Jeongbae Seo | 253e0a7 | 2010-10-08 17:46:21 +0900 | [diff] [blame] | 601 | 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 Seo | b3824f2 | 2010-10-08 17:46:20 +0900 | [diff] [blame] | 606 | /* It supports additional host capabilities if needed */ |
| 607 | if (pdata->host_caps) |
| 608 | host->mmc->caps |= pdata->host_caps; |
| 609 | |
Jaehoon Chung | c1c4b66 | 2012-02-07 15:59:01 +0900 | [diff] [blame] | 610 | if (pdata->host_caps2) |
| 611 | host->mmc->caps2 |= pdata->host_caps2; |
| 612 | |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 613 | 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 Hansson | f8e3260 | 2014-12-18 10:41:42 +0100 | [diff] [blame] | 618 | ret = mmc_of_parse(host->mmc); |
| 619 | if (ret) |
| 620 | goto err_req_regs; |
Jaehoon Chung | 11bc938 | 2014-05-26 13:58:28 +0900 | [diff] [blame] | 621 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 622 | ret = sdhci_add_host(host); |
| 623 | if (ret) { |
| 624 | dev_err(dev, "sdhci_add_host() failed\n"); |
Julia Lawall | 9bda6da | 2012-03-08 23:24:53 -0500 | [diff] [blame] | 625 | goto err_req_regs; |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Rafael J. Wysocki | 162d6f9 | 2014-12-05 03:05:33 +0100 | [diff] [blame] | 628 | #ifdef CONFIG_PM |
Seungwon Jeon | 0aa55c2 | 2012-10-30 14:28:36 +0900 | [diff] [blame] | 629 | if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL) |
| 630 | clk_disable_unprepare(sc->clk_io); |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 631 | #endif |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 632 | return 0; |
| 633 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 634 | err_req_regs: |
Bartlomiej Zolnierkiewicz | 221414d | 2014-08-07 18:07:07 +0200 | [diff] [blame] | 635 | pm_runtime_disable(&pdev->dev); |
| 636 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 637 | err_no_busclks: |
Thomas Abraham | 0f310a05 | 2012-10-03 08:35:43 +0900 | [diff] [blame] | 638 | clk_disable_unprepare(sc->clk_io); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 639 | |
Tomasz Figa | b1b8fea | 2012-11-25 15:40:44 -0500 | [diff] [blame] | 640 | err_pdata_io_clk: |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 641 | sdhci_free_host(host); |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
Bill Pemberton | 6e0ee71 | 2012-11-19 13:26:03 -0500 | [diff] [blame] | 646 | static int sdhci_s3c_remove(struct platform_device *pdev) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 647 | { |
Marek Szyprowski | 9d51a6b | 2010-07-20 13:24:33 -0700 | [diff] [blame] | 648 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 649 | struct sdhci_s3c *sc = sdhci_priv(host); |
Marek Szyprowski | 17866e1 | 2010-08-10 18:01:58 -0700 | [diff] [blame] | 650 | |
| 651 | if (sc->ext_cd_irq) |
| 652 | free_irq(sc->ext_cd_irq, sc); |
| 653 | |
Rafael J. Wysocki | 162d6f9 | 2014-12-05 03:05:33 +0100 | [diff] [blame] | 654 | #ifdef CONFIG_PM |
Jaehoon Chung | 11bc938 | 2014-05-26 13:58:28 +0900 | [diff] [blame] | 655 | if (sc->pdata->cd_type != S3C_SDHCI_CD_INTERNAL) |
Seungwon Jeon | 0aa55c2 | 2012-10-30 14:28:36 +0900 | [diff] [blame] | 656 | clk_prepare_enable(sc->clk_io); |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 657 | #endif |
Marek Szyprowski | 9d51a6b | 2010-07-20 13:24:33 -0700 | [diff] [blame] | 658 | sdhci_remove_host(host, 1); |
| 659 | |
Chander Kashyap | 387a8cbd | 2012-09-14 09:08:50 +0000 | [diff] [blame] | 660 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 661 | pm_runtime_disable(&pdev->dev); |
| 662 | |
Thomas Abraham | 0f310a05 | 2012-10-03 08:35:43 +0900 | [diff] [blame] | 663 | clk_disable_unprepare(sc->clk_io); |
Marek Szyprowski | 9d51a6b | 2010-07-20 13:24:33 -0700 | [diff] [blame] | 664 | |
Marek Szyprowski | 9d51a6b | 2010-07-20 13:24:33 -0700 | [diff] [blame] | 665 | sdhci_free_host(host); |
Marek Szyprowski | 9d51a6b | 2010-07-20 13:24:33 -0700 | [diff] [blame] | 666 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 667 | return 0; |
| 668 | } |
| 669 | |
Mark Brown | d5e9c02 | 2012-03-03 00:46:41 +0000 | [diff] [blame] | 670 | #ifdef CONFIG_PM_SLEEP |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 671 | static int sdhci_s3c_suspend(struct device *dev) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 672 | { |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 673 | struct sdhci_host *host = dev_get_drvdata(dev); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 674 | |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 675 | return sdhci_suspend_host(host); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 678 | static int sdhci_s3c_resume(struct device *dev) |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 679 | { |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 680 | struct sdhci_host *host = dev_get_drvdata(dev); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 681 | |
Wonil Choi | 65d1351 | 2011-06-29 11:38:38 +0900 | [diff] [blame] | 682 | return sdhci_resume_host(host); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 683 | } |
Mark Brown | d5e9c02 | 2012-03-03 00:46:41 +0000 | [diff] [blame] | 684 | #endif |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 685 | |
Rafael J. Wysocki | 162d6f9 | 2014-12-05 03:05:33 +0100 | [diff] [blame] | 686 | #ifdef CONFIG_PM |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 687 | static int sdhci_s3c_runtime_suspend(struct device *dev) |
| 688 | { |
| 689 | struct sdhci_host *host = dev_get_drvdata(dev); |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 690 | struct sdhci_s3c *ourhost = to_s3c(host); |
| 691 | struct clk *busclk = ourhost->clk_io; |
| 692 | int ret; |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 693 | |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 694 | ret = sdhci_runtime_suspend_host(host); |
| 695 | |
Tomasz Figa | 3ac147f | 2014-01-11 22:39:05 +0100 | [diff] [blame] | 696 | if (ourhost->cur_clk >= 0) |
| 697 | clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]); |
Thomas Abraham | 0f310a05 | 2012-10-03 08:35:43 +0900 | [diff] [blame] | 698 | clk_disable_unprepare(busclk); |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 699 | return ret; |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static int sdhci_s3c_runtime_resume(struct device *dev) |
| 703 | { |
| 704 | struct sdhci_host *host = dev_get_drvdata(dev); |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 705 | struct sdhci_s3c *ourhost = to_s3c(host); |
| 706 | struct clk *busclk = ourhost->clk_io; |
| 707 | int ret; |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 708 | |
Thomas Abraham | 0f310a05 | 2012-10-03 08:35:43 +0900 | [diff] [blame] | 709 | clk_prepare_enable(busclk); |
Tomasz Figa | 3ac147f | 2014-01-11 22:39:05 +0100 | [diff] [blame] | 710 | if (ourhost->cur_clk >= 0) |
| 711 | clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]); |
Chander Kashyap | 2abeb5c | 2012-09-21 05:42:08 +0000 | [diff] [blame] | 712 | ret = sdhci_runtime_resume_host(host); |
| 713 | return ret; |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 714 | } |
| 715 | #endif |
| 716 | |
Mark Brown | d5e9c02 | 2012-03-03 00:46:41 +0000 | [diff] [blame] | 717 | #ifdef CONFIG_PM |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 718 | static const struct dev_pm_ops sdhci_s3c_pmops = { |
Mark Brown | d5e9c02 | 2012-03-03 00:46:41 +0000 | [diff] [blame] | 719 | SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume) |
Mark Brown | 9f4e815 | 2012-03-31 23:31:55 -0400 | [diff] [blame] | 720 | SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume, |
| 721 | NULL) |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 722 | }; |
| 723 | |
| 724 | #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops) |
| 725 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 726 | #else |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 727 | #define SDHCI_S3C_PMOPS NULL |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 728 | #endif |
| 729 | |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 730 | #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) |
| 731 | static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = { |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 732 | .no_divider = true, |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 733 | }; |
| 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 Kozlowski | 4d0aa49 | 2015-05-02 00:49:22 +0900 | [diff] [blame] | 739 | static const struct platform_device_id sdhci_s3c_driver_ids[] = { |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 740 | { |
| 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 | }; |
| 749 | MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids); |
| 750 | |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 751 | #ifdef CONFIG_OF |
| 752 | static 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 | }; |
| 758 | MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match); |
| 759 | #endif |
| 760 | |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 761 | static struct platform_driver sdhci_s3c_driver = { |
| 762 | .probe = sdhci_s3c_probe, |
Bill Pemberton | 0433c14 | 2012-11-19 13:20:26 -0500 | [diff] [blame] | 763 | .remove = sdhci_s3c_remove, |
Thomas Abraham | 3119936a | 2012-02-16 22:23:58 +0900 | [diff] [blame] | 764 | .id_table = sdhci_s3c_driver_ids, |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 765 | .driver = { |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 766 | .name = "s3c-sdhci", |
Thomas Abraham | cd1b00e | 2012-08-23 17:10:09 +0000 | [diff] [blame] | 767 | .of_match_table = of_match_ptr(sdhci_s3c_dt_match), |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 768 | .pm = SDHCI_S3C_PMOPS, |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 769 | }, |
| 770 | }; |
| 771 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 772 | module_platform_driver(sdhci_s3c_driver); |
Ben Dooks | 0d1bb41 | 2009-06-14 13:52:37 +0100 | [diff] [blame] | 773 | |
| 774 | MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue"); |
| 775 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); |
| 776 | MODULE_LICENSE("GPL v2"); |
| 777 | MODULE_ALIAS("platform:s3c-sdhci"); |