2 * SDHCI Controller driver for TI's OMAP SoCs
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
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.
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.
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/>.
20 #include <linux/delay.h>
21 #include <linux/mmc/slot-gpio.h>
22 #include <linux/module.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>
29 #include "sdhci-pltfm.h"
31 #define SDHCI_OMAP_CON 0x12c
32 #define CON_DW8 BIT(5)
33 #define CON_DMA_MASTER BIT(20)
34 #define CON_INIT BIT(1)
37 #define SDHCI_OMAP_CMD 0x20c
39 #define SDHCI_OMAP_HCTL 0x228
40 #define HCTL_SDBP BIT(8)
41 #define HCTL_SDVS_SHIFT 9
42 #define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT)
43 #define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT)
44 #define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT)
45 #define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT)
47 #define SDHCI_OMAP_SYSCTL 0x22c
48 #define SYSCTL_CEN BIT(2)
49 #define SYSCTL_CLKD_SHIFT 6
50 #define SYSCTL_CLKD_MASK 0x3ff
52 #define SDHCI_OMAP_STAT 0x230
54 #define SDHCI_OMAP_IE 0x234
55 #define INT_CC_EN BIT(0)
57 #define SDHCI_OMAP_AC12 0x23c
58 #define AC12_V1V8_SIGEN BIT(19)
60 #define SDHCI_OMAP_CAPA 0x240
61 #define CAPA_VS33 BIT(24)
62 #define CAPA_VS30 BIT(25)
63 #define CAPA_VS18 BIT(26)
65 #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
67 #define SYSCTL_CLKD_MAX 0x3FF
69 #define IOV_1V8 1800000 /* 180000 uV */
70 #define IOV_3V0 3000000 /* 300000 uV */
71 #define IOV_3V3 3300000 /* 330000 uV */
73 struct sdhci_omap_data {
77 struct sdhci_omap_host {
80 struct regulator *pbias;
82 struct sdhci_host *host;
87 static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
90 return readl(host->base + offset);
93 static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
94 unsigned int offset, u32 data)
96 writel(data, host->base + offset);
99 static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
100 bool power_on, unsigned int iov)
103 struct device *dev = omap_host->dev;
105 if (IS_ERR(omap_host->pbias))
109 ret = regulator_set_voltage(omap_host->pbias, iov, iov);
111 dev_err(dev, "pbias set voltage failed\n");
115 if (omap_host->pbias_enabled)
118 ret = regulator_enable(omap_host->pbias);
120 dev_err(dev, "pbias reg enable fail\n");
124 omap_host->pbias_enabled = true;
126 if (!omap_host->pbias_enabled)
129 ret = regulator_disable(omap_host->pbias);
131 dev_err(dev, "pbias reg disable fail\n");
134 omap_host->pbias_enabled = false;
140 static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
144 struct sdhci_host *host = omap_host->host;
145 struct mmc_host *mmc = host->mmc;
147 ret = sdhci_omap_set_pbias(omap_host, false, 0);
151 if (!IS_ERR(mmc->supply.vqmmc)) {
152 ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
154 dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
159 ret = sdhci_omap_set_pbias(omap_host, true, iov);
166 static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
167 unsigned char signal_voltage)
172 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
173 reg &= ~HCTL_SDVS_MASK;
175 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
180 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
183 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
186 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
187 while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) {
188 if (WARN_ON(ktime_after(ktime_get(), timeout)))
194 static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
200 struct sdhci_host *host = mmc_priv(mmc);
201 struct sdhci_pltfm_host *pltfm_host;
202 struct sdhci_omap_host *omap_host;
205 pltfm_host = sdhci_priv(host);
206 omap_host = sdhci_pltfm_priv(pltfm_host);
207 dev = omap_host->dev;
209 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
210 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
211 if (!(reg & CAPA_VS33))
214 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
216 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
217 reg &= ~AC12_V1V8_SIGEN;
218 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
221 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
222 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
223 if (!(reg & CAPA_VS18))
226 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
228 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
229 reg |= AC12_V1V8_SIGEN;
230 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
237 ret = sdhci_omap_enable_iov(omap_host, iov);
239 dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
243 dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
247 static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
250 omap_host->power_mode = power_mode;
253 static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
258 if (omap_host->bus_mode == mode)
261 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
262 if (mode == MMC_BUSMODE_OPENDRAIN)
266 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
268 omap_host->bus_mode = mode;
271 static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
273 struct sdhci_host *host = mmc_priv(mmc);
274 struct sdhci_pltfm_host *pltfm_host;
275 struct sdhci_omap_host *omap_host;
277 pltfm_host = sdhci_priv(host);
278 omap_host = sdhci_pltfm_priv(pltfm_host);
280 sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
281 sdhci_set_ios(mmc, ios);
282 sdhci_omap_set_power_mode(omap_host, ios->power_mode);
285 static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
290 dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
291 if (dsor > SYSCTL_CLKD_MAX)
292 dsor = SYSCTL_CLKD_MAX;
297 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
301 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
303 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
306 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
310 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
312 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
315 static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
317 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
318 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
319 unsigned long clkdiv;
321 sdhci_omap_stop_clock(omap_host);
326 clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
327 clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
328 sdhci_enable_clk(host, clkdiv);
330 sdhci_omap_start_clock(omap_host);
333 static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
336 struct mmc_host *mmc = host->mmc;
338 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
341 static int sdhci_omap_enable_dma(struct sdhci_host *host)
344 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
345 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
347 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
348 reg |= CON_DMA_MASTER;
349 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
354 static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
356 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
358 return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
361 static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
363 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
364 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
367 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
368 if (width == MMC_BUS_WIDTH_8)
372 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
374 sdhci_set_bus_width(host, width);
377 static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
381 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
382 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
384 if (omap_host->power_mode == power_mode)
387 if (power_mode != MMC_POWER_ON)
390 disable_irq(host->irq);
392 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
394 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
395 sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
398 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
399 while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
400 if (WARN_ON(ktime_after(ktime_get(), timeout)))
405 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
407 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
408 sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
410 enable_irq(host->irq);
413 static struct sdhci_ops sdhci_omap_ops = {
414 .set_clock = sdhci_omap_set_clock,
415 .set_power = sdhci_omap_set_power,
416 .enable_dma = sdhci_omap_enable_dma,
417 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
418 .get_min_clock = sdhci_omap_get_min_clock,
419 .set_bus_width = sdhci_omap_set_bus_width,
420 .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
421 .reset = sdhci_reset,
422 .set_uhs_signaling = sdhci_set_uhs_signaling,
425 static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
429 struct device *dev = omap_host->dev;
430 struct regulator *vqmmc;
432 vqmmc = regulator_get(dev, "vqmmc");
434 ret = PTR_ERR(vqmmc);
438 /* voltage capabilities might be set by boot loader, clear it */
439 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
440 reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
442 if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
444 if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
447 sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
450 regulator_put(vqmmc);
455 static const struct sdhci_pltfm_data sdhci_omap_pdata = {
456 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
457 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
458 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
459 SDHCI_QUIRK_NO_HISPD_BIT |
460 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
461 .quirks2 = SDHCI_QUIRK2_NO_1_8_V |
462 SDHCI_QUIRK2_ACMD23_BROKEN |
463 SDHCI_QUIRK2_RSP_136_HAS_CRC,
464 .ops = &sdhci_omap_ops,
467 static const struct sdhci_omap_data dra7_data = {
471 static const struct of_device_id omap_sdhci_match[] = {
472 { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
475 MODULE_DEVICE_TABLE(of, omap_sdhci_match);
477 static int sdhci_omap_probe(struct platform_device *pdev)
481 struct device *dev = &pdev->dev;
482 struct sdhci_host *host;
483 struct sdhci_pltfm_host *pltfm_host;
484 struct sdhci_omap_host *omap_host;
485 struct mmc_host *mmc;
486 const struct of_device_id *match;
487 struct sdhci_omap_data *data;
489 match = of_match_device(omap_sdhci_match, dev);
493 data = (struct sdhci_omap_data *)match->data;
495 dev_err(dev, "no sdhci omap data\n");
498 offset = data->offset;
500 host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
503 dev_err(dev, "Failed sdhci_pltfm_init\n");
504 return PTR_ERR(host);
507 pltfm_host = sdhci_priv(host);
508 omap_host = sdhci_pltfm_priv(pltfm_host);
509 omap_host->host = host;
510 omap_host->base = host->ioaddr;
511 omap_host->dev = dev;
512 omap_host->power_mode = MMC_POWER_UNDEFINED;
513 host->ioaddr += offset;
516 ret = mmc_of_parse(mmc);
520 pltfm_host->clk = devm_clk_get(dev, "fck");
521 if (IS_ERR(pltfm_host->clk)) {
522 ret = PTR_ERR(pltfm_host->clk);
526 ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
528 dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
532 omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
533 if (IS_ERR(omap_host->pbias)) {
534 ret = PTR_ERR(omap_host->pbias);
537 dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
539 omap_host->pbias_enabled = false;
542 * omap_device_pm_domain has callbacks to enable the main
543 * functional clock, interface clock and also configure the
544 * SYSCONFIG register of omap devices. The callback will be invoked
545 * as part of pm_runtime_get_sync.
547 pm_runtime_enable(dev);
548 ret = pm_runtime_get_sync(dev);
550 dev_err(dev, "pm_runtime_get_sync failed\n");
551 pm_runtime_put_noidle(dev);
552 goto err_rpm_disable;
555 ret = sdhci_omap_set_capabilities(omap_host);
557 dev_err(dev, "failed to set system capabilities\n");
561 host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
562 host->mmc_host_ops.start_signal_voltage_switch =
563 sdhci_omap_start_signal_voltage_switch;
564 host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
566 sdhci_read_caps(host);
567 host->caps |= SDHCI_CAN_DO_ADMA2;
569 ret = sdhci_add_host(host);
576 pm_runtime_put_sync(dev);
579 pm_runtime_disable(dev);
582 sdhci_pltfm_free(pdev);
586 static int sdhci_omap_remove(struct platform_device *pdev)
588 struct device *dev = &pdev->dev;
589 struct sdhci_host *host = platform_get_drvdata(pdev);
591 sdhci_remove_host(host, true);
592 pm_runtime_put_sync(dev);
593 pm_runtime_disable(dev);
594 sdhci_pltfm_free(pdev);
599 static struct platform_driver sdhci_omap_driver = {
600 .probe = sdhci_omap_probe,
601 .remove = sdhci_omap_remove,
603 .name = "sdhci-omap",
604 .of_match_table = omap_sdhci_match,
608 module_platform_driver(sdhci_omap_driver);
610 MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
611 MODULE_AUTHOR("Texas Instruments Inc.");
612 MODULE_LICENSE("GPL v2");
613 MODULE_ALIAS("platform:sdhci_omap");