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_DDR BIT(19)
35 #define CON_CLKEXTFREE BIT(16)
36 #define CON_PADEN BIT(15)
37 #define CON_INIT BIT(1)
40 #define SDHCI_OMAP_CMD 0x20c
42 #define SDHCI_OMAP_PSTATE 0x0224
43 #define PSTATE_DLEV_DAT0 BIT(20)
44 #define PSTATE_DATI BIT(1)
46 #define SDHCI_OMAP_HCTL 0x228
47 #define HCTL_SDBP BIT(8)
48 #define HCTL_SDVS_SHIFT 9
49 #define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT)
50 #define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT)
51 #define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT)
52 #define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT)
54 #define SDHCI_OMAP_SYSCTL 0x22c
55 #define SYSCTL_CEN BIT(2)
56 #define SYSCTL_CLKD_SHIFT 6
57 #define SYSCTL_CLKD_MASK 0x3ff
59 #define SDHCI_OMAP_STAT 0x230
61 #define SDHCI_OMAP_IE 0x234
62 #define INT_CC_EN BIT(0)
64 #define SDHCI_OMAP_AC12 0x23c
65 #define AC12_V1V8_SIGEN BIT(19)
67 #define SDHCI_OMAP_CAPA 0x240
68 #define CAPA_VS33 BIT(24)
69 #define CAPA_VS30 BIT(25)
70 #define CAPA_VS18 BIT(26)
72 #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
74 #define SYSCTL_CLKD_MAX 0x3FF
76 #define IOV_1V8 1800000 /* 180000 uV */
77 #define IOV_3V0 3000000 /* 300000 uV */
78 #define IOV_3V3 3300000 /* 330000 uV */
80 struct sdhci_omap_data {
84 struct sdhci_omap_host {
87 struct regulator *pbias;
89 struct sdhci_host *host;
94 static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
97 return readl(host->base + offset);
100 static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
101 unsigned int offset, u32 data)
103 writel(data, host->base + offset);
106 static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
107 bool power_on, unsigned int iov)
110 struct device *dev = omap_host->dev;
112 if (IS_ERR(omap_host->pbias))
116 ret = regulator_set_voltage(omap_host->pbias, iov, iov);
118 dev_err(dev, "pbias set voltage failed\n");
122 if (omap_host->pbias_enabled)
125 ret = regulator_enable(omap_host->pbias);
127 dev_err(dev, "pbias reg enable fail\n");
131 omap_host->pbias_enabled = true;
133 if (!omap_host->pbias_enabled)
136 ret = regulator_disable(omap_host->pbias);
138 dev_err(dev, "pbias reg disable fail\n");
141 omap_host->pbias_enabled = false;
147 static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
151 struct sdhci_host *host = omap_host->host;
152 struct mmc_host *mmc = host->mmc;
154 ret = sdhci_omap_set_pbias(omap_host, false, 0);
158 if (!IS_ERR(mmc->supply.vqmmc)) {
159 ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
161 dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
166 ret = sdhci_omap_set_pbias(omap_host, true, iov);
173 static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
174 unsigned char signal_voltage)
179 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
180 reg &= ~HCTL_SDVS_MASK;
182 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
187 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
190 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
193 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
194 while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) {
195 if (WARN_ON(ktime_after(ktime_get(), timeout)))
201 static int sdhci_omap_card_busy(struct mmc_host *mmc)
205 struct sdhci_host *host = mmc_priv(mmc);
206 struct sdhci_pltfm_host *pltfm_host;
207 struct sdhci_omap_host *omap_host;
210 pltfm_host = sdhci_priv(host);
211 omap_host = sdhci_pltfm_priv(pltfm_host);
213 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
214 ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
215 reg &= ~CON_CLKEXTFREE;
216 if (ac12 & AC12_V1V8_SIGEN)
217 reg |= CON_CLKEXTFREE;
219 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
221 disable_irq(host->irq);
222 ier |= SDHCI_INT_CARD_INT;
223 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
224 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
227 * Delay is required for PSTATE to correctly reflect
228 * DLEV/CLEV values after PADEN is set.
230 usleep_range(50, 100);
231 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
232 if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
235 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
236 reg &= ~(CON_CLKEXTFREE | CON_PADEN);
237 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
239 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
240 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
241 enable_irq(host->irq);
246 static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
252 struct sdhci_host *host = mmc_priv(mmc);
253 struct sdhci_pltfm_host *pltfm_host;
254 struct sdhci_omap_host *omap_host;
257 pltfm_host = sdhci_priv(host);
258 omap_host = sdhci_pltfm_priv(pltfm_host);
259 dev = omap_host->dev;
261 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
262 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
263 if (!(reg & CAPA_VS33))
266 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
268 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
269 reg &= ~AC12_V1V8_SIGEN;
270 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
273 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
274 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
275 if (!(reg & CAPA_VS18))
278 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
280 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
281 reg |= AC12_V1V8_SIGEN;
282 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
289 ret = sdhci_omap_enable_iov(omap_host, iov);
291 dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
295 dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
299 static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
302 omap_host->power_mode = power_mode;
305 static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
310 if (omap_host->bus_mode == mode)
313 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
314 if (mode == MMC_BUSMODE_OPENDRAIN)
318 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
320 omap_host->bus_mode = mode;
323 static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
325 struct sdhci_host *host = mmc_priv(mmc);
326 struct sdhci_pltfm_host *pltfm_host;
327 struct sdhci_omap_host *omap_host;
329 pltfm_host = sdhci_priv(host);
330 omap_host = sdhci_pltfm_priv(pltfm_host);
332 sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
333 sdhci_set_ios(mmc, ios);
334 sdhci_omap_set_power_mode(omap_host, ios->power_mode);
337 static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
342 dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
343 if (dsor > SYSCTL_CLKD_MAX)
344 dsor = SYSCTL_CLKD_MAX;
349 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
353 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
355 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
358 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
362 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
364 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
367 static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
369 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
370 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
371 unsigned long clkdiv;
373 sdhci_omap_stop_clock(omap_host);
378 clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
379 clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
380 sdhci_enable_clk(host, clkdiv);
382 sdhci_omap_start_clock(omap_host);
385 static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
388 struct mmc_host *mmc = host->mmc;
390 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
393 static int sdhci_omap_enable_dma(struct sdhci_host *host)
396 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
397 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
399 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
400 reg |= CON_DMA_MASTER;
401 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
406 static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
408 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
410 return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
413 static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
415 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
416 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
419 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
420 if (width == MMC_BUS_WIDTH_8)
424 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
426 sdhci_set_bus_width(host, width);
429 static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
433 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
434 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
436 if (omap_host->power_mode == power_mode)
439 if (power_mode != MMC_POWER_ON)
442 disable_irq(host->irq);
444 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
446 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
447 sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
450 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
451 while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
452 if (WARN_ON(ktime_after(ktime_get(), timeout)))
457 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
459 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
460 sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
462 enable_irq(host->irq);
465 static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
469 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
470 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
472 sdhci_omap_stop_clock(omap_host);
474 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
475 if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
479 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
481 sdhci_set_uhs_signaling(host, timing);
482 sdhci_omap_start_clock(omap_host);
485 static struct sdhci_ops sdhci_omap_ops = {
486 .set_clock = sdhci_omap_set_clock,
487 .set_power = sdhci_omap_set_power,
488 .enable_dma = sdhci_omap_enable_dma,
489 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
490 .get_min_clock = sdhci_omap_get_min_clock,
491 .set_bus_width = sdhci_omap_set_bus_width,
492 .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
493 .reset = sdhci_reset,
494 .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
497 static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
501 struct device *dev = omap_host->dev;
502 struct regulator *vqmmc;
504 vqmmc = regulator_get(dev, "vqmmc");
506 ret = PTR_ERR(vqmmc);
510 /* voltage capabilities might be set by boot loader, clear it */
511 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
512 reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
514 if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
516 if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
519 sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
522 regulator_put(vqmmc);
527 static const struct sdhci_pltfm_data sdhci_omap_pdata = {
528 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
529 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
530 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
531 SDHCI_QUIRK_NO_HISPD_BIT |
532 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
533 .quirks2 = SDHCI_QUIRK2_NO_1_8_V |
534 SDHCI_QUIRK2_ACMD23_BROKEN |
535 SDHCI_QUIRK2_RSP_136_HAS_CRC,
536 .ops = &sdhci_omap_ops,
539 static const struct sdhci_omap_data dra7_data = {
543 static const struct of_device_id omap_sdhci_match[] = {
544 { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
547 MODULE_DEVICE_TABLE(of, omap_sdhci_match);
549 static int sdhci_omap_probe(struct platform_device *pdev)
553 struct device *dev = &pdev->dev;
554 struct sdhci_host *host;
555 struct sdhci_pltfm_host *pltfm_host;
556 struct sdhci_omap_host *omap_host;
557 struct mmc_host *mmc;
558 const struct of_device_id *match;
559 struct sdhci_omap_data *data;
561 match = of_match_device(omap_sdhci_match, dev);
565 data = (struct sdhci_omap_data *)match->data;
567 dev_err(dev, "no sdhci omap data\n");
570 offset = data->offset;
572 host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
575 dev_err(dev, "Failed sdhci_pltfm_init\n");
576 return PTR_ERR(host);
579 pltfm_host = sdhci_priv(host);
580 omap_host = sdhci_pltfm_priv(pltfm_host);
581 omap_host->host = host;
582 omap_host->base = host->ioaddr;
583 omap_host->dev = dev;
584 omap_host->power_mode = MMC_POWER_UNDEFINED;
585 host->ioaddr += offset;
588 ret = mmc_of_parse(mmc);
592 pltfm_host->clk = devm_clk_get(dev, "fck");
593 if (IS_ERR(pltfm_host->clk)) {
594 ret = PTR_ERR(pltfm_host->clk);
598 ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
600 dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
604 omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
605 if (IS_ERR(omap_host->pbias)) {
606 ret = PTR_ERR(omap_host->pbias);
609 dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
611 omap_host->pbias_enabled = false;
614 * omap_device_pm_domain has callbacks to enable the main
615 * functional clock, interface clock and also configure the
616 * SYSCONFIG register of omap devices. The callback will be invoked
617 * as part of pm_runtime_get_sync.
619 pm_runtime_enable(dev);
620 ret = pm_runtime_get_sync(dev);
622 dev_err(dev, "pm_runtime_get_sync failed\n");
623 pm_runtime_put_noidle(dev);
624 goto err_rpm_disable;
627 ret = sdhci_omap_set_capabilities(omap_host);
629 dev_err(dev, "failed to set system capabilities\n");
633 host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
634 host->mmc_host_ops.start_signal_voltage_switch =
635 sdhci_omap_start_signal_voltage_switch;
636 host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
637 host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
639 sdhci_read_caps(host);
640 host->caps |= SDHCI_CAN_DO_ADMA2;
642 ret = sdhci_add_host(host);
649 pm_runtime_put_sync(dev);
652 pm_runtime_disable(dev);
655 sdhci_pltfm_free(pdev);
659 static int sdhci_omap_remove(struct platform_device *pdev)
661 struct device *dev = &pdev->dev;
662 struct sdhci_host *host = platform_get_drvdata(pdev);
664 sdhci_remove_host(host, true);
665 pm_runtime_put_sync(dev);
666 pm_runtime_disable(dev);
667 sdhci_pltfm_free(pdev);
672 static struct platform_driver sdhci_omap_driver = {
673 .probe = sdhci_omap_probe,
674 .remove = sdhci_omap_remove,
676 .name = "sdhci-omap",
677 .of_match_table = omap_sdhci_match,
681 module_platform_driver(sdhci_omap_driver);
683 MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
684 MODULE_AUTHOR("Texas Instruments Inc.");
685 MODULE_LICENSE("GPL v2");
686 MODULE_ALIAS("platform:sdhci_omap");