Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 1 | /** |
| 2 | * SDHCI Controller driver for TI's OMAP SoCs |
| 3 | * |
| 4 | * Copyright (C) 2017 Texas Instruments |
| 5 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 of |
| 9 | * the License as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/mmc/slot-gpio.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pm_runtime.h> |
| 27 | #include <linux/regulator/consumer.h> |
| 28 | |
| 29 | #include "sdhci-pltfm.h" |
| 30 | |
| 31 | #define SDHCI_OMAP_CON 0x12c |
| 32 | #define CON_DW8 BIT(5) |
| 33 | #define CON_DMA_MASTER BIT(20) |
Kishon Vijay Abraham I | 20ea26a | 2018-02-05 18:20:15 +0530 | [diff] [blame^] | 34 | #define CON_CLKEXTFREE BIT(16) |
| 35 | #define CON_PADEN BIT(15) |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 36 | #define CON_INIT BIT(1) |
| 37 | #define CON_OD BIT(0) |
| 38 | |
| 39 | #define SDHCI_OMAP_CMD 0x20c |
| 40 | |
Kishon Vijay Abraham I | 20ea26a | 2018-02-05 18:20:15 +0530 | [diff] [blame^] | 41 | #define SDHCI_OMAP_PSTATE 0x0224 |
| 42 | #define PSTATE_DLEV_DAT0 BIT(20) |
| 43 | #define PSTATE_DATI BIT(1) |
| 44 | |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 45 | #define SDHCI_OMAP_HCTL 0x228 |
| 46 | #define HCTL_SDBP BIT(8) |
| 47 | #define HCTL_SDVS_SHIFT 9 |
| 48 | #define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT) |
| 49 | #define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT) |
| 50 | #define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT) |
| 51 | #define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT) |
| 52 | |
| 53 | #define SDHCI_OMAP_SYSCTL 0x22c |
| 54 | #define SYSCTL_CEN BIT(2) |
| 55 | #define SYSCTL_CLKD_SHIFT 6 |
| 56 | #define SYSCTL_CLKD_MASK 0x3ff |
| 57 | |
| 58 | #define SDHCI_OMAP_STAT 0x230 |
| 59 | |
| 60 | #define SDHCI_OMAP_IE 0x234 |
| 61 | #define INT_CC_EN BIT(0) |
| 62 | |
| 63 | #define SDHCI_OMAP_AC12 0x23c |
| 64 | #define AC12_V1V8_SIGEN BIT(19) |
| 65 | |
| 66 | #define SDHCI_OMAP_CAPA 0x240 |
| 67 | #define CAPA_VS33 BIT(24) |
| 68 | #define CAPA_VS30 BIT(25) |
| 69 | #define CAPA_VS18 BIT(26) |
| 70 | |
| 71 | #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */ |
| 72 | |
| 73 | #define SYSCTL_CLKD_MAX 0x3FF |
| 74 | |
| 75 | #define IOV_1V8 1800000 /* 180000 uV */ |
| 76 | #define IOV_3V0 3000000 /* 300000 uV */ |
| 77 | #define IOV_3V3 3300000 /* 330000 uV */ |
| 78 | |
| 79 | struct sdhci_omap_data { |
| 80 | u32 offset; |
| 81 | }; |
| 82 | |
| 83 | struct sdhci_omap_host { |
| 84 | void __iomem *base; |
| 85 | struct device *dev; |
| 86 | struct regulator *pbias; |
| 87 | bool pbias_enabled; |
| 88 | struct sdhci_host *host; |
| 89 | u8 bus_mode; |
| 90 | u8 power_mode; |
| 91 | }; |
| 92 | |
| 93 | static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host, |
| 94 | unsigned int offset) |
| 95 | { |
| 96 | return readl(host->base + offset); |
| 97 | } |
| 98 | |
| 99 | static inline void sdhci_omap_writel(struct sdhci_omap_host *host, |
| 100 | unsigned int offset, u32 data) |
| 101 | { |
| 102 | writel(data, host->base + offset); |
| 103 | } |
| 104 | |
| 105 | static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host, |
| 106 | bool power_on, unsigned int iov) |
| 107 | { |
| 108 | int ret; |
| 109 | struct device *dev = omap_host->dev; |
| 110 | |
| 111 | if (IS_ERR(omap_host->pbias)) |
| 112 | return 0; |
| 113 | |
| 114 | if (power_on) { |
| 115 | ret = regulator_set_voltage(omap_host->pbias, iov, iov); |
| 116 | if (ret) { |
| 117 | dev_err(dev, "pbias set voltage failed\n"); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | if (omap_host->pbias_enabled) |
| 122 | return 0; |
| 123 | |
| 124 | ret = regulator_enable(omap_host->pbias); |
| 125 | if (ret) { |
| 126 | dev_err(dev, "pbias reg enable fail\n"); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | omap_host->pbias_enabled = true; |
| 131 | } else { |
| 132 | if (!omap_host->pbias_enabled) |
| 133 | return 0; |
| 134 | |
| 135 | ret = regulator_disable(omap_host->pbias); |
| 136 | if (ret) { |
| 137 | dev_err(dev, "pbias reg disable fail\n"); |
| 138 | return ret; |
| 139 | } |
| 140 | omap_host->pbias_enabled = false; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host, |
| 147 | unsigned int iov) |
| 148 | { |
| 149 | int ret; |
| 150 | struct sdhci_host *host = omap_host->host; |
| 151 | struct mmc_host *mmc = host->mmc; |
| 152 | |
| 153 | ret = sdhci_omap_set_pbias(omap_host, false, 0); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 158 | ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov); |
| 159 | if (ret) { |
| 160 | dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n"); |
| 161 | return ret; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | ret = sdhci_omap_set_pbias(omap_host, true, iov); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host, |
| 173 | unsigned char signal_voltage) |
| 174 | { |
| 175 | u32 reg; |
| 176 | ktime_t timeout; |
| 177 | |
| 178 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL); |
| 179 | reg &= ~HCTL_SDVS_MASK; |
| 180 | |
| 181 | if (signal_voltage == MMC_SIGNAL_VOLTAGE_330) |
| 182 | reg |= HCTL_SDVS_33; |
| 183 | else |
| 184 | reg |= HCTL_SDVS_18; |
| 185 | |
| 186 | sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg); |
| 187 | |
| 188 | reg |= HCTL_SDBP; |
| 189 | sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg); |
| 190 | |
| 191 | /* wait 1ms */ |
| 192 | timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT); |
| 193 | while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) { |
| 194 | if (WARN_ON(ktime_after(ktime_get(), timeout))) |
| 195 | return; |
| 196 | usleep_range(5, 10); |
| 197 | } |
| 198 | } |
| 199 | |
Kishon Vijay Abraham I | 20ea26a | 2018-02-05 18:20:15 +0530 | [diff] [blame^] | 200 | static int sdhci_omap_card_busy(struct mmc_host *mmc) |
| 201 | { |
| 202 | u32 reg, ac12; |
| 203 | int ret = false; |
| 204 | struct sdhci_host *host = mmc_priv(mmc); |
| 205 | struct sdhci_pltfm_host *pltfm_host; |
| 206 | struct sdhci_omap_host *omap_host; |
| 207 | u32 ier = host->ier; |
| 208 | |
| 209 | pltfm_host = sdhci_priv(host); |
| 210 | omap_host = sdhci_pltfm_priv(pltfm_host); |
| 211 | |
| 212 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 213 | ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12); |
| 214 | reg &= ~CON_CLKEXTFREE; |
| 215 | if (ac12 & AC12_V1V8_SIGEN) |
| 216 | reg |= CON_CLKEXTFREE; |
| 217 | reg |= CON_PADEN; |
| 218 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 219 | |
| 220 | disable_irq(host->irq); |
| 221 | ier |= SDHCI_INT_CARD_INT; |
| 222 | sdhci_writel(host, ier, SDHCI_INT_ENABLE); |
| 223 | sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE); |
| 224 | |
| 225 | /* |
| 226 | * Delay is required for PSTATE to correctly reflect |
| 227 | * DLEV/CLEV values after PADEN is set. |
| 228 | */ |
| 229 | usleep_range(50, 100); |
| 230 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE); |
| 231 | if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0)) |
| 232 | ret = true; |
| 233 | |
| 234 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 235 | reg &= ~(CON_CLKEXTFREE | CON_PADEN); |
| 236 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 237 | |
| 238 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 239 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 240 | enable_irq(host->irq); |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 245 | static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc, |
| 246 | struct mmc_ios *ios) |
| 247 | { |
| 248 | u32 reg; |
| 249 | int ret; |
| 250 | unsigned int iov; |
| 251 | struct sdhci_host *host = mmc_priv(mmc); |
| 252 | struct sdhci_pltfm_host *pltfm_host; |
| 253 | struct sdhci_omap_host *omap_host; |
| 254 | struct device *dev; |
| 255 | |
| 256 | pltfm_host = sdhci_priv(host); |
| 257 | omap_host = sdhci_pltfm_priv(pltfm_host); |
| 258 | dev = omap_host->dev; |
| 259 | |
| 260 | if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) { |
| 261 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA); |
| 262 | if (!(reg & CAPA_VS33)) |
| 263 | return -EOPNOTSUPP; |
| 264 | |
| 265 | sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage); |
| 266 | |
| 267 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12); |
| 268 | reg &= ~AC12_V1V8_SIGEN; |
| 269 | sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg); |
| 270 | |
| 271 | iov = IOV_3V3; |
| 272 | } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) { |
| 273 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA); |
| 274 | if (!(reg & CAPA_VS18)) |
| 275 | return -EOPNOTSUPP; |
| 276 | |
| 277 | sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage); |
| 278 | |
| 279 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12); |
| 280 | reg |= AC12_V1V8_SIGEN; |
| 281 | sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg); |
| 282 | |
| 283 | iov = IOV_1V8; |
| 284 | } else { |
| 285 | return -EOPNOTSUPP; |
| 286 | } |
| 287 | |
| 288 | ret = sdhci_omap_enable_iov(omap_host, iov); |
| 289 | if (ret) { |
| 290 | dev_err(dev, "failed to switch IO voltage to %dmV\n", iov); |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | dev_dbg(dev, "IO voltage switched to %dmV\n", iov); |
| 295 | return 0; |
| 296 | } |
| 297 | |
Kishon Vijay Abraham I | 300df50 | 2018-02-05 18:20:14 +0530 | [diff] [blame] | 298 | static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host, |
| 299 | u8 power_mode) |
| 300 | { |
| 301 | omap_host->power_mode = power_mode; |
| 302 | } |
| 303 | |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 304 | static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host, |
| 305 | unsigned int mode) |
| 306 | { |
| 307 | u32 reg; |
| 308 | |
| 309 | if (omap_host->bus_mode == mode) |
| 310 | return; |
| 311 | |
| 312 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 313 | if (mode == MMC_BUSMODE_OPENDRAIN) |
| 314 | reg |= CON_OD; |
| 315 | else |
| 316 | reg &= ~CON_OD; |
| 317 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 318 | |
| 319 | omap_host->bus_mode = mode; |
| 320 | } |
| 321 | |
Colin Ian King | ddde0e7 | 2017-09-26 15:55:46 +0100 | [diff] [blame] | 322 | static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 323 | { |
| 324 | struct sdhci_host *host = mmc_priv(mmc); |
| 325 | struct sdhci_pltfm_host *pltfm_host; |
| 326 | struct sdhci_omap_host *omap_host; |
| 327 | |
| 328 | pltfm_host = sdhci_priv(host); |
| 329 | omap_host = sdhci_pltfm_priv(pltfm_host); |
| 330 | |
| 331 | sdhci_omap_set_bus_mode(omap_host, ios->bus_mode); |
| 332 | sdhci_set_ios(mmc, ios); |
Kishon Vijay Abraham I | 300df50 | 2018-02-05 18:20:14 +0530 | [diff] [blame] | 333 | sdhci_omap_set_power_mode(omap_host, ios->power_mode); |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host, |
| 337 | unsigned int clock) |
| 338 | { |
| 339 | u16 dsor; |
| 340 | |
| 341 | dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock); |
| 342 | if (dsor > SYSCTL_CLKD_MAX) |
| 343 | dsor = SYSCTL_CLKD_MAX; |
| 344 | |
| 345 | return dsor; |
| 346 | } |
| 347 | |
| 348 | static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host) |
| 349 | { |
| 350 | u32 reg; |
| 351 | |
| 352 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL); |
| 353 | reg |= SYSCTL_CEN; |
| 354 | sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg); |
| 355 | } |
| 356 | |
| 357 | static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host) |
| 358 | { |
| 359 | u32 reg; |
| 360 | |
| 361 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL); |
| 362 | reg &= ~SYSCTL_CEN; |
| 363 | sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg); |
| 364 | } |
| 365 | |
| 366 | static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock) |
| 367 | { |
| 368 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 369 | struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host); |
| 370 | unsigned long clkdiv; |
| 371 | |
| 372 | sdhci_omap_stop_clock(omap_host); |
| 373 | |
| 374 | if (!clock) |
| 375 | return; |
| 376 | |
| 377 | clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock); |
| 378 | clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT; |
| 379 | sdhci_enable_clk(host, clkdiv); |
| 380 | |
| 381 | sdhci_omap_start_clock(omap_host); |
| 382 | } |
| 383 | |
Colin Ian King | ddde0e7 | 2017-09-26 15:55:46 +0100 | [diff] [blame] | 384 | static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode, |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 385 | unsigned short vdd) |
| 386 | { |
| 387 | struct mmc_host *mmc = host->mmc; |
| 388 | |
| 389 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); |
| 390 | } |
| 391 | |
| 392 | static int sdhci_omap_enable_dma(struct sdhci_host *host) |
| 393 | { |
| 394 | u32 reg; |
| 395 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 396 | struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host); |
| 397 | |
| 398 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 399 | reg |= CON_DMA_MASTER; |
| 400 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Colin Ian King | ddde0e7 | 2017-09-26 15:55:46 +0100 | [diff] [blame] | 405 | static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host) |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 406 | { |
| 407 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 408 | |
| 409 | return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX; |
| 410 | } |
| 411 | |
| 412 | static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width) |
| 413 | { |
| 414 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 415 | struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host); |
| 416 | u32 reg; |
| 417 | |
| 418 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 419 | if (width == MMC_BUS_WIDTH_8) |
| 420 | reg |= CON_DW8; |
| 421 | else |
| 422 | reg &= ~CON_DW8; |
| 423 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 424 | |
| 425 | sdhci_set_bus_width(host, width); |
| 426 | } |
| 427 | |
| 428 | static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode) |
| 429 | { |
| 430 | u32 reg; |
| 431 | ktime_t timeout; |
| 432 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 433 | struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host); |
| 434 | |
| 435 | if (omap_host->power_mode == power_mode) |
| 436 | return; |
| 437 | |
| 438 | if (power_mode != MMC_POWER_ON) |
| 439 | return; |
| 440 | |
| 441 | disable_irq(host->irq); |
| 442 | |
| 443 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 444 | reg |= CON_INIT; |
| 445 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 446 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0); |
| 447 | |
| 448 | /* wait 1ms */ |
| 449 | timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT); |
| 450 | while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) { |
| 451 | if (WARN_ON(ktime_after(ktime_get(), timeout))) |
| 452 | return; |
| 453 | usleep_range(5, 10); |
| 454 | } |
| 455 | |
| 456 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON); |
| 457 | reg &= ~CON_INIT; |
| 458 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg); |
| 459 | sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN); |
| 460 | |
| 461 | enable_irq(host->irq); |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static struct sdhci_ops sdhci_omap_ops = { |
| 465 | .set_clock = sdhci_omap_set_clock, |
| 466 | .set_power = sdhci_omap_set_power, |
| 467 | .enable_dma = sdhci_omap_enable_dma, |
| 468 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, |
| 469 | .get_min_clock = sdhci_omap_get_min_clock, |
| 470 | .set_bus_width = sdhci_omap_set_bus_width, |
| 471 | .platform_send_init_74_clocks = sdhci_omap_init_74_clocks, |
| 472 | .reset = sdhci_reset, |
| 473 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
| 474 | }; |
| 475 | |
| 476 | static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host) |
| 477 | { |
| 478 | u32 reg; |
| 479 | int ret = 0; |
| 480 | struct device *dev = omap_host->dev; |
| 481 | struct regulator *vqmmc; |
| 482 | |
| 483 | vqmmc = regulator_get(dev, "vqmmc"); |
| 484 | if (IS_ERR(vqmmc)) { |
| 485 | ret = PTR_ERR(vqmmc); |
| 486 | goto reg_put; |
| 487 | } |
| 488 | |
| 489 | /* voltage capabilities might be set by boot loader, clear it */ |
| 490 | reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA); |
| 491 | reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33); |
| 492 | |
| 493 | if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3)) |
| 494 | reg |= CAPA_VS33; |
| 495 | if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8)) |
| 496 | reg |= CAPA_VS18; |
| 497 | |
| 498 | sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg); |
| 499 | |
| 500 | reg_put: |
| 501 | regulator_put(vqmmc); |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | static const struct sdhci_pltfm_data sdhci_omap_pdata = { |
| 507 | .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 508 | SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 509 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 510 | SDHCI_QUIRK_NO_HISPD_BIT | |
| 511 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC, |
| 512 | .quirks2 = SDHCI_QUIRK2_NO_1_8_V | |
| 513 | SDHCI_QUIRK2_ACMD23_BROKEN | |
| 514 | SDHCI_QUIRK2_RSP_136_HAS_CRC, |
| 515 | .ops = &sdhci_omap_ops, |
| 516 | }; |
| 517 | |
| 518 | static const struct sdhci_omap_data dra7_data = { |
| 519 | .offset = 0x200, |
| 520 | }; |
| 521 | |
| 522 | static const struct of_device_id omap_sdhci_match[] = { |
| 523 | { .compatible = "ti,dra7-sdhci", .data = &dra7_data }, |
| 524 | {}, |
| 525 | }; |
| 526 | MODULE_DEVICE_TABLE(of, omap_sdhci_match); |
| 527 | |
| 528 | static int sdhci_omap_probe(struct platform_device *pdev) |
| 529 | { |
| 530 | int ret; |
| 531 | u32 offset; |
| 532 | struct device *dev = &pdev->dev; |
| 533 | struct sdhci_host *host; |
| 534 | struct sdhci_pltfm_host *pltfm_host; |
| 535 | struct sdhci_omap_host *omap_host; |
| 536 | struct mmc_host *mmc; |
| 537 | const struct of_device_id *match; |
| 538 | struct sdhci_omap_data *data; |
| 539 | |
| 540 | match = of_match_device(omap_sdhci_match, dev); |
| 541 | if (!match) |
| 542 | return -EINVAL; |
| 543 | |
| 544 | data = (struct sdhci_omap_data *)match->data; |
| 545 | if (!data) { |
| 546 | dev_err(dev, "no sdhci omap data\n"); |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | offset = data->offset; |
| 550 | |
| 551 | host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata, |
| 552 | sizeof(*omap_host)); |
| 553 | if (IS_ERR(host)) { |
| 554 | dev_err(dev, "Failed sdhci_pltfm_init\n"); |
| 555 | return PTR_ERR(host); |
| 556 | } |
| 557 | |
| 558 | pltfm_host = sdhci_priv(host); |
| 559 | omap_host = sdhci_pltfm_priv(pltfm_host); |
| 560 | omap_host->host = host; |
| 561 | omap_host->base = host->ioaddr; |
| 562 | omap_host->dev = dev; |
Kishon Vijay Abraham I | 300df50 | 2018-02-05 18:20:14 +0530 | [diff] [blame] | 563 | omap_host->power_mode = MMC_POWER_UNDEFINED; |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 564 | host->ioaddr += offset; |
| 565 | |
| 566 | mmc = host->mmc; |
| 567 | ret = mmc_of_parse(mmc); |
| 568 | if (ret) |
| 569 | goto err_pltfm_free; |
| 570 | |
| 571 | pltfm_host->clk = devm_clk_get(dev, "fck"); |
| 572 | if (IS_ERR(pltfm_host->clk)) { |
| 573 | ret = PTR_ERR(pltfm_host->clk); |
| 574 | goto err_pltfm_free; |
| 575 | } |
| 576 | |
| 577 | ret = clk_set_rate(pltfm_host->clk, mmc->f_max); |
| 578 | if (ret) { |
| 579 | dev_err(dev, "failed to set clock to %d\n", mmc->f_max); |
| 580 | goto err_pltfm_free; |
| 581 | } |
| 582 | |
| 583 | omap_host->pbias = devm_regulator_get_optional(dev, "pbias"); |
| 584 | if (IS_ERR(omap_host->pbias)) { |
| 585 | ret = PTR_ERR(omap_host->pbias); |
| 586 | if (ret != -ENODEV) |
| 587 | goto err_pltfm_free; |
| 588 | dev_dbg(dev, "unable to get pbias regulator %d\n", ret); |
| 589 | } |
| 590 | omap_host->pbias_enabled = false; |
| 591 | |
| 592 | /* |
| 593 | * omap_device_pm_domain has callbacks to enable the main |
| 594 | * functional clock, interface clock and also configure the |
| 595 | * SYSCONFIG register of omap devices. The callback will be invoked |
| 596 | * as part of pm_runtime_get_sync. |
| 597 | */ |
| 598 | pm_runtime_enable(dev); |
| 599 | ret = pm_runtime_get_sync(dev); |
| 600 | if (ret < 0) { |
| 601 | dev_err(dev, "pm_runtime_get_sync failed\n"); |
| 602 | pm_runtime_put_noidle(dev); |
| 603 | goto err_rpm_disable; |
| 604 | } |
| 605 | |
| 606 | ret = sdhci_omap_set_capabilities(omap_host); |
| 607 | if (ret) { |
| 608 | dev_err(dev, "failed to set system capabilities\n"); |
| 609 | goto err_put_sync; |
| 610 | } |
| 611 | |
| 612 | host->mmc_host_ops.get_ro = mmc_gpio_get_ro; |
| 613 | host->mmc_host_ops.start_signal_voltage_switch = |
| 614 | sdhci_omap_start_signal_voltage_switch; |
| 615 | host->mmc_host_ops.set_ios = sdhci_omap_set_ios; |
Kishon Vijay Abraham I | 20ea26a | 2018-02-05 18:20:15 +0530 | [diff] [blame^] | 616 | host->mmc_host_ops.card_busy = sdhci_omap_card_busy; |
Kishon Vijay Abraham I | 7d32693 | 2017-09-06 17:15:55 +0530 | [diff] [blame] | 617 | |
| 618 | sdhci_read_caps(host); |
| 619 | host->caps |= SDHCI_CAN_DO_ADMA2; |
| 620 | |
| 621 | ret = sdhci_add_host(host); |
| 622 | if (ret) |
| 623 | goto err_put_sync; |
| 624 | |
| 625 | return 0; |
| 626 | |
| 627 | err_put_sync: |
| 628 | pm_runtime_put_sync(dev); |
| 629 | |
| 630 | err_rpm_disable: |
| 631 | pm_runtime_disable(dev); |
| 632 | |
| 633 | err_pltfm_free: |
| 634 | sdhci_pltfm_free(pdev); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int sdhci_omap_remove(struct platform_device *pdev) |
| 639 | { |
| 640 | struct device *dev = &pdev->dev; |
| 641 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 642 | |
| 643 | sdhci_remove_host(host, true); |
| 644 | pm_runtime_put_sync(dev); |
| 645 | pm_runtime_disable(dev); |
| 646 | sdhci_pltfm_free(pdev); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static struct platform_driver sdhci_omap_driver = { |
| 652 | .probe = sdhci_omap_probe, |
| 653 | .remove = sdhci_omap_remove, |
| 654 | .driver = { |
| 655 | .name = "sdhci-omap", |
| 656 | .of_match_table = omap_sdhci_match, |
| 657 | }, |
| 658 | }; |
| 659 | |
| 660 | module_platform_driver(sdhci_omap_driver); |
| 661 | |
| 662 | MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs"); |
| 663 | MODULE_AUTHOR("Texas Instruments Inc."); |
| 664 | MODULE_LICENSE("GPL v2"); |
| 665 | MODULE_ALIAS("platform:sdhci_omap"); |