1 // SPDX-License-Identifier: GPL-2.0-only
3 * SDHCI Controller driver for TI's OMAP SoCs
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/delay.h>
10 #include <linux/mmc/mmc.h>
11 #include <linux/mmc/slot-gpio.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/sys_soc.h>
20 #include <linux/thermal.h>
22 #include "sdhci-pltfm.h"
24 #define SDHCI_OMAP_CON 0x12c
25 #define CON_DW8 BIT(5)
26 #define CON_DMA_MASTER BIT(20)
27 #define CON_DDR BIT(19)
28 #define CON_CLKEXTFREE BIT(16)
29 #define CON_PADEN BIT(15)
30 #define CON_CTPL BIT(11)
31 #define CON_INIT BIT(1)
34 #define SDHCI_OMAP_DLL 0x0134
35 #define DLL_SWT BIT(20)
36 #define DLL_FORCE_SR_C_SHIFT 13
37 #define DLL_FORCE_SR_C_MASK (0x7f << DLL_FORCE_SR_C_SHIFT)
38 #define DLL_FORCE_VALUE BIT(12)
39 #define DLL_CALIB BIT(1)
41 #define SDHCI_OMAP_CMD 0x20c
43 #define SDHCI_OMAP_PSTATE 0x0224
44 #define PSTATE_DLEV_DAT0 BIT(20)
45 #define PSTATE_DATI BIT(1)
47 #define SDHCI_OMAP_HCTL 0x228
48 #define HCTL_SDBP BIT(8)
49 #define HCTL_SDVS_SHIFT 9
50 #define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT)
51 #define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT)
52 #define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT)
53 #define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT)
55 #define SDHCI_OMAP_SYSCTL 0x22c
56 #define SYSCTL_CEN BIT(2)
57 #define SYSCTL_CLKD_SHIFT 6
58 #define SYSCTL_CLKD_MASK 0x3ff
60 #define SDHCI_OMAP_STAT 0x230
62 #define SDHCI_OMAP_IE 0x234
63 #define INT_CC_EN BIT(0)
65 #define SDHCI_OMAP_AC12 0x23c
66 #define AC12_V1V8_SIGEN BIT(19)
67 #define AC12_SCLK_SEL BIT(23)
69 #define SDHCI_OMAP_CAPA 0x240
70 #define CAPA_VS33 BIT(24)
71 #define CAPA_VS30 BIT(25)
72 #define CAPA_VS18 BIT(26)
74 #define SDHCI_OMAP_CAPA2 0x0244
75 #define CAPA2_TSDR50 BIT(13)
77 #define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
79 #define SYSCTL_CLKD_MAX 0x3FF
81 #define IOV_1V8 1800000 /* 180000 uV */
82 #define IOV_3V0 3000000 /* 300000 uV */
83 #define IOV_3V3 3300000 /* 330000 uV */
85 #define MAX_PHASE_DELAY 0x7C
87 /* sdhci-omap controller flags */
88 #define SDHCI_OMAP_REQUIRE_IODELAY BIT(0)
89 #define SDHCI_OMAP_SPECIAL_RESET BIT(1)
91 struct sdhci_omap_data {
96 struct sdhci_omap_host {
100 struct regulator *pbias;
102 struct sdhci_host *host;
108 struct pinctrl *pinctrl;
109 struct pinctrl_state **pinctrl_state;
113 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host);
114 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host);
116 static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
119 return readl(host->base + offset);
122 static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
123 unsigned int offset, u32 data)
125 writel(data, host->base + offset);
128 static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
129 bool power_on, unsigned int iov)
132 struct device *dev = omap_host->dev;
134 if (IS_ERR(omap_host->pbias))
138 ret = regulator_set_voltage(omap_host->pbias, iov, iov);
140 dev_err(dev, "pbias set voltage failed\n");
144 if (omap_host->pbias_enabled)
147 ret = regulator_enable(omap_host->pbias);
149 dev_err(dev, "pbias reg enable fail\n");
153 omap_host->pbias_enabled = true;
155 if (!omap_host->pbias_enabled)
158 ret = regulator_disable(omap_host->pbias);
160 dev_err(dev, "pbias reg disable fail\n");
163 omap_host->pbias_enabled = false;
169 static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
173 struct sdhci_host *host = omap_host->host;
174 struct mmc_host *mmc = host->mmc;
176 ret = sdhci_omap_set_pbias(omap_host, false, 0);
180 if (!IS_ERR(mmc->supply.vqmmc)) {
181 ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
183 dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
188 ret = sdhci_omap_set_pbias(omap_host, true, iov);
195 static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
196 unsigned char signal_voltage)
201 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
202 reg &= ~HCTL_SDVS_MASK;
204 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
209 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
212 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
215 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
217 bool timedout = ktime_after(ktime_get(), timeout);
219 if (sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)
221 if (WARN_ON(timedout))
227 static void sdhci_omap_enable_sdio_irq(struct mmc_host *mmc, int enable)
229 struct sdhci_host *host = mmc_priv(mmc);
230 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
231 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
234 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
236 reg |= (CON_CTPL | CON_CLKEXTFREE);
238 reg &= ~(CON_CTPL | CON_CLKEXTFREE);
239 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
241 sdhci_enable_sdio_irq(mmc, enable);
244 static inline void sdhci_omap_set_dll(struct sdhci_omap_host *omap_host,
250 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
251 reg |= DLL_FORCE_VALUE;
252 reg &= ~DLL_FORCE_SR_C_MASK;
253 reg |= (count << DLL_FORCE_SR_C_SHIFT);
254 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
257 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
258 for (i = 0; i < 1000; i++) {
259 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
264 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
267 static void sdhci_omap_disable_tuning(struct sdhci_omap_host *omap_host)
271 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
272 reg &= ~AC12_SCLK_SEL;
273 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
275 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
276 reg &= ~(DLL_FORCE_VALUE | DLL_SWT);
277 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
280 static int sdhci_omap_execute_tuning(struct mmc_host *mmc, u32 opcode)
282 struct sdhci_host *host = mmc_priv(mmc);
283 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
284 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
285 struct thermal_zone_device *thermal_dev;
286 struct device *dev = omap_host->dev;
287 struct mmc_ios *ios = &mmc->ios;
288 u32 start_window = 0, max_window = 0;
289 bool single_point_failure = false;
290 bool dcrc_was_enabled = false;
291 u8 cur_match, prev_match = 0;
292 u32 length = 0, max_len = 0;
299 /* clock tuning is not needed for upto 52MHz */
300 if (ios->clock <= 52000000)
303 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA2);
304 if (ios->timing == MMC_TIMING_UHS_SDR50 && !(reg & CAPA2_TSDR50))
307 thermal_dev = thermal_zone_get_zone_by_name("cpu_thermal");
308 if (IS_ERR(thermal_dev)) {
309 dev_err(dev, "Unable to get thermal zone for tuning\n");
310 return PTR_ERR(thermal_dev);
313 ret = thermal_zone_get_temp(thermal_dev, &temperature);
317 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
319 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
322 * OMAP5/DRA74X/DRA72x Errata i802:
323 * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
324 * during the tuning procedure. So disable it during the
327 if (host->ier & SDHCI_INT_DATA_CRC) {
328 host->ier &= ~SDHCI_INT_DATA_CRC;
329 dcrc_was_enabled = true;
332 omap_host->is_tuning = true;
335 * Stage 1: Search for a maximum pass window ignoring any
336 * any single point failures. If the tuning value ends up
337 * near it, move away from it in stage 2 below
339 while (phase_delay <= MAX_PHASE_DELAY) {
340 sdhci_omap_set_dll(omap_host, phase_delay);
342 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
346 } else if (single_point_failure) {
347 /* ignore single point failure */
350 start_window = phase_delay;
354 single_point_failure = prev_match;
357 if (length > max_len) {
358 max_window = start_window;
362 prev_match = cur_match;
367 dev_err(dev, "Unable to find match\n");
373 * Assign tuning value as a ratio of maximum pass window based
376 if (temperature < -20000)
377 phase_delay = min(max_window + 4 * (max_len - 1) - 24,
379 DIV_ROUND_UP(13 * max_len, 16) * 4);
380 else if (temperature < 20000)
381 phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
382 else if (temperature < 40000)
383 phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
384 else if (temperature < 70000)
385 phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
386 else if (temperature < 90000)
387 phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
388 else if (temperature < 120000)
389 phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
391 phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
394 * Stage 2: Search for a single point failure near the chosen tuning
395 * value in two steps. First in the +3 to +10 range and then in the
396 * +2 to -10 range. If found, move away from it in the appropriate
397 * direction by the appropriate amount depending on the temperature.
399 for (i = 3; i <= 10; i++) {
400 sdhci_omap_set_dll(omap_host, phase_delay + i);
402 if (mmc_send_tuning(mmc, opcode, NULL)) {
403 if (temperature < 10000)
404 phase_delay += i + 6;
405 else if (temperature < 20000)
406 phase_delay += i - 12;
407 else if (temperature < 70000)
408 phase_delay += i - 8;
410 phase_delay += i - 6;
412 goto single_failure_found;
416 for (i = 2; i >= -10; i--) {
417 sdhci_omap_set_dll(omap_host, phase_delay + i);
419 if (mmc_send_tuning(mmc, opcode, NULL)) {
420 if (temperature < 10000)
421 phase_delay += i + 12;
422 else if (temperature < 20000)
423 phase_delay += i + 8;
424 else if (temperature < 70000)
425 phase_delay += i + 8;
426 else if (temperature < 90000)
427 phase_delay += i + 10;
429 phase_delay += i + 12;
431 goto single_failure_found;
435 single_failure_found:
436 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
437 if (!(reg & AC12_SCLK_SEL)) {
442 sdhci_omap_set_dll(omap_host, phase_delay);
444 omap_host->is_tuning = false;
449 omap_host->is_tuning = false;
450 dev_err(dev, "Tuning failed\n");
451 sdhci_omap_disable_tuning(omap_host);
454 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
455 /* Reenable forbidden interrupt */
456 if (dcrc_was_enabled)
457 host->ier |= SDHCI_INT_DATA_CRC;
458 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
459 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
463 static int sdhci_omap_card_busy(struct mmc_host *mmc)
467 struct sdhci_host *host = mmc_priv(mmc);
468 struct sdhci_pltfm_host *pltfm_host;
469 struct sdhci_omap_host *omap_host;
472 pltfm_host = sdhci_priv(host);
473 omap_host = sdhci_pltfm_priv(pltfm_host);
475 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
476 ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
477 reg &= ~CON_CLKEXTFREE;
478 if (ac12 & AC12_V1V8_SIGEN)
479 reg |= CON_CLKEXTFREE;
481 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
483 disable_irq(host->irq);
484 ier |= SDHCI_INT_CARD_INT;
485 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
486 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
489 * Delay is required for PSTATE to correctly reflect
490 * DLEV/CLEV values after PADEN is set.
492 usleep_range(50, 100);
493 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
494 if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
497 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
498 reg &= ~(CON_CLKEXTFREE | CON_PADEN);
499 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
501 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
502 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
503 enable_irq(host->irq);
508 static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
514 struct sdhci_host *host = mmc_priv(mmc);
515 struct sdhci_pltfm_host *pltfm_host;
516 struct sdhci_omap_host *omap_host;
519 pltfm_host = sdhci_priv(host);
520 omap_host = sdhci_pltfm_priv(pltfm_host);
521 dev = omap_host->dev;
523 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
524 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
525 if (!(reg & CAPA_VS33))
528 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
530 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
531 reg &= ~AC12_V1V8_SIGEN;
532 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
535 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
536 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
537 if (!(reg & CAPA_VS18))
540 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
542 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
543 reg |= AC12_V1V8_SIGEN;
544 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
551 ret = sdhci_omap_enable_iov(omap_host, iov);
553 dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
557 dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
561 static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
564 struct pinctrl_state *pinctrl_state;
565 struct device *dev = omap_host->dev;
567 if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
570 if (omap_host->timing == timing)
573 sdhci_omap_stop_clock(omap_host);
575 pinctrl_state = omap_host->pinctrl_state[timing];
576 ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
578 dev_err(dev, "failed to select pinctrl state\n");
582 sdhci_omap_start_clock(omap_host);
583 omap_host->timing = timing;
586 static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
589 if (omap_host->bus_mode == MMC_POWER_OFF)
590 sdhci_omap_disable_tuning(omap_host);
591 omap_host->power_mode = power_mode;
594 static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
599 if (omap_host->bus_mode == mode)
602 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
603 if (mode == MMC_BUSMODE_OPENDRAIN)
607 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
609 omap_host->bus_mode = mode;
612 static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
614 struct sdhci_host *host = mmc_priv(mmc);
615 struct sdhci_pltfm_host *pltfm_host;
616 struct sdhci_omap_host *omap_host;
618 pltfm_host = sdhci_priv(host);
619 omap_host = sdhci_pltfm_priv(pltfm_host);
621 sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
622 sdhci_omap_set_timing(omap_host, ios->timing);
623 sdhci_set_ios(mmc, ios);
624 sdhci_omap_set_power_mode(omap_host, ios->power_mode);
627 static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
632 dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
633 if (dsor > SYSCTL_CLKD_MAX)
634 dsor = SYSCTL_CLKD_MAX;
639 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
643 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
645 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
648 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
652 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
654 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
657 static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
659 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
660 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
661 unsigned long clkdiv;
663 sdhci_omap_stop_clock(omap_host);
668 clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
669 clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
670 sdhci_enable_clk(host, clkdiv);
672 sdhci_omap_start_clock(omap_host);
675 static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
678 struct mmc_host *mmc = host->mmc;
680 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
683 static int sdhci_omap_enable_dma(struct sdhci_host *host)
686 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
687 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
689 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
690 reg &= ~CON_DMA_MASTER;
691 /* Switch to DMA slave mode when using external DMA */
692 if (!host->use_external_dma)
693 reg |= CON_DMA_MASTER;
695 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
700 static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
702 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
704 return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
707 static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
709 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
710 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
713 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
714 if (width == MMC_BUS_WIDTH_8)
718 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
720 sdhci_set_bus_width(host, width);
723 static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
727 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
728 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
730 if (omap_host->power_mode == power_mode)
733 if (power_mode != MMC_POWER_ON)
736 disable_irq(host->irq);
738 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
740 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
741 sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
744 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
746 bool timedout = ktime_after(ktime_get(), timeout);
748 if (sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)
750 if (WARN_ON(timedout))
755 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
757 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
758 sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
760 enable_irq(host->irq);
763 static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
767 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
768 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
770 sdhci_omap_stop_clock(omap_host);
772 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
773 if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
777 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
779 sdhci_set_uhs_signaling(host, timing);
780 sdhci_omap_start_clock(omap_host);
783 #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
784 static void sdhci_omap_reset(struct sdhci_host *host, u8 mask)
786 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
787 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
788 unsigned long limit = MMC_TIMEOUT_US;
791 /* Don't reset data lines during tuning operation */
792 if (omap_host->is_tuning)
793 mask &= ~SDHCI_RESET_DATA;
795 if (omap_host->flags & SDHCI_OMAP_SPECIAL_RESET) {
796 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
797 while ((!(sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask)) &&
801 while ((sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) &&
805 if (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask)
806 dev_err(mmc_dev(host->mmc),
807 "Timeout waiting on controller reset in %s\n",
812 sdhci_reset(host, mask);
815 #define CMD_ERR_MASK (SDHCI_INT_CRC | SDHCI_INT_END_BIT | SDHCI_INT_INDEX |\
817 #define CMD_MASK (CMD_ERR_MASK | SDHCI_INT_RESPONSE)
819 static u32 sdhci_omap_irq(struct sdhci_host *host, u32 intmask)
821 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
822 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
824 if (omap_host->is_tuning && host->cmd && !host->data_early &&
825 (intmask & CMD_ERR_MASK)) {
828 * Since we are not resetting data lines during tuning
829 * operation, data error or data complete interrupts
830 * might still arrive. Mark this request as a failure
831 * but still wait for the data interrupt
833 if (intmask & SDHCI_INT_TIMEOUT)
834 host->cmd->error = -ETIMEDOUT;
836 host->cmd->error = -EILSEQ;
841 * Sometimes command error interrupts and command complete
842 * interrupt will arrive together. Clear all command related
845 sdhci_writel(host, intmask & CMD_MASK, SDHCI_INT_STATUS);
846 intmask &= ~CMD_MASK;
852 static void sdhci_omap_set_timeout(struct sdhci_host *host,
853 struct mmc_command *cmd)
855 if (cmd->opcode == MMC_ERASE)
856 sdhci_set_data_timeout_irq(host, false);
858 __sdhci_set_timeout(host, cmd);
861 static struct sdhci_ops sdhci_omap_ops = {
862 .set_clock = sdhci_omap_set_clock,
863 .set_power = sdhci_omap_set_power,
864 .enable_dma = sdhci_omap_enable_dma,
865 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
866 .get_min_clock = sdhci_omap_get_min_clock,
867 .set_bus_width = sdhci_omap_set_bus_width,
868 .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
869 .reset = sdhci_omap_reset,
870 .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
871 .irq = sdhci_omap_irq,
872 .set_timeout = sdhci_omap_set_timeout,
875 static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
879 struct device *dev = omap_host->dev;
880 struct regulator *vqmmc;
882 vqmmc = regulator_get(dev, "vqmmc");
884 ret = PTR_ERR(vqmmc);
888 /* voltage capabilities might be set by boot loader, clear it */
889 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
890 reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
892 if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
894 if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
897 sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
900 regulator_put(vqmmc);
905 static const struct sdhci_pltfm_data sdhci_omap_pdata = {
906 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
907 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
908 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
909 SDHCI_QUIRK_NO_HISPD_BIT |
910 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
911 .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN |
912 SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
913 SDHCI_QUIRK2_RSP_136_HAS_CRC |
914 SDHCI_QUIRK2_DISABLE_HW_TIMEOUT,
915 .ops = &sdhci_omap_ops,
918 static const struct sdhci_omap_data k2g_data = {
922 static const struct sdhci_omap_data am335_data = {
924 .flags = SDHCI_OMAP_SPECIAL_RESET,
927 static const struct sdhci_omap_data am437_data = {
929 .flags = SDHCI_OMAP_SPECIAL_RESET,
932 static const struct sdhci_omap_data dra7_data = {
934 .flags = SDHCI_OMAP_REQUIRE_IODELAY,
937 static const struct of_device_id omap_sdhci_match[] = {
938 { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
939 { .compatible = "ti,k2g-sdhci", .data = &k2g_data },
940 { .compatible = "ti,am335-sdhci", .data = &am335_data },
941 { .compatible = "ti,am437-sdhci", .data = &am437_data },
944 MODULE_DEVICE_TABLE(of, omap_sdhci_match);
946 static struct pinctrl_state
947 *sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
948 u32 *caps, u32 capmask)
950 struct device *dev = omap_host->dev;
951 char *version = omap_host->version;
952 struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
955 if (!(*caps & capmask))
959 snprintf(str, 20, "%s-%s", mode, version);
960 pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, str);
963 if (IS_ERR(pinctrl_state))
964 pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
966 if (IS_ERR(pinctrl_state)) {
967 dev_err(dev, "no pinctrl state for %s mode", mode);
972 return pinctrl_state;
975 static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
978 struct device *dev = omap_host->dev;
979 struct sdhci_host *host = omap_host->host;
980 struct mmc_host *mmc = host->mmc;
981 u32 *caps = &mmc->caps;
982 u32 *caps2 = &mmc->caps2;
983 struct pinctrl_state *state;
984 struct pinctrl_state **pinctrl_state;
986 if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
989 pinctrl_state = devm_kcalloc(dev,
990 MMC_TIMING_MMC_HS200 + 1,
991 sizeof(*pinctrl_state),
996 omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
997 if (IS_ERR(omap_host->pinctrl)) {
998 dev_err(dev, "Cannot get pinctrl\n");
999 return PTR_ERR(omap_host->pinctrl);
1002 state = pinctrl_lookup_state(omap_host->pinctrl, "default");
1003 if (IS_ERR(state)) {
1004 dev_err(dev, "no pinctrl state for default mode\n");
1005 return PTR_ERR(state);
1007 pinctrl_state[MMC_TIMING_LEGACY] = state;
1009 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
1010 MMC_CAP_UHS_SDR104);
1012 pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
1014 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
1017 pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
1019 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
1022 pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
1024 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
1027 pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
1029 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
1032 pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
1034 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
1036 if (!IS_ERR(state)) {
1037 pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
1039 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_3_3v",
1043 pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
1046 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
1047 MMC_CAP_SD_HIGHSPEED);
1049 pinctrl_state[MMC_TIMING_SD_HS] = state;
1051 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
1052 MMC_CAP_MMC_HIGHSPEED);
1054 pinctrl_state[MMC_TIMING_MMC_HS] = state;
1056 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
1057 MMC_CAP2_HS200_1_8V_SDR);
1059 pinctrl_state[MMC_TIMING_MMC_HS200] = state;
1061 omap_host->pinctrl_state = pinctrl_state;
1066 static const struct soc_device_attribute sdhci_omap_soc_devices[] = {
1068 .machine = "DRA7[45]*",
1069 .revision = "ES1.[01]",
1076 static int sdhci_omap_probe(struct platform_device *pdev)
1080 struct device *dev = &pdev->dev;
1081 struct sdhci_host *host;
1082 struct sdhci_pltfm_host *pltfm_host;
1083 struct sdhci_omap_host *omap_host;
1084 struct mmc_host *mmc;
1085 const struct of_device_id *match;
1086 struct sdhci_omap_data *data;
1087 const struct soc_device_attribute *soc;
1088 struct resource *regs;
1090 match = of_match_device(omap_sdhci_match, dev);
1094 data = (struct sdhci_omap_data *)match->data;
1096 dev_err(dev, "no sdhci omap data\n");
1099 offset = data->offset;
1101 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1105 host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
1106 sizeof(*omap_host));
1108 dev_err(dev, "Failed sdhci_pltfm_init\n");
1109 return PTR_ERR(host);
1112 pltfm_host = sdhci_priv(host);
1113 omap_host = sdhci_pltfm_priv(pltfm_host);
1114 omap_host->host = host;
1115 omap_host->base = host->ioaddr;
1116 omap_host->dev = dev;
1117 omap_host->power_mode = MMC_POWER_UNDEFINED;
1118 omap_host->timing = MMC_TIMING_LEGACY;
1119 omap_host->flags = data->flags;
1120 host->ioaddr += offset;
1121 host->mapbase = regs->start + offset;
1124 sdhci_get_of_property(pdev);
1125 ret = mmc_of_parse(mmc);
1127 goto err_pltfm_free;
1129 soc = soc_device_match(sdhci_omap_soc_devices);
1131 omap_host->version = "rev11";
1132 if (!strcmp(dev_name(dev), "4809c000.mmc"))
1133 mmc->f_max = 96000000;
1134 if (!strcmp(dev_name(dev), "480b4000.mmc"))
1135 mmc->f_max = 48000000;
1136 if (!strcmp(dev_name(dev), "480ad000.mmc"))
1137 mmc->f_max = 48000000;
1140 if (!mmc_can_gpio_ro(mmc))
1141 mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
1143 pltfm_host->clk = devm_clk_get(dev, "fck");
1144 if (IS_ERR(pltfm_host->clk)) {
1145 ret = PTR_ERR(pltfm_host->clk);
1146 goto err_pltfm_free;
1149 ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
1151 dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
1152 goto err_pltfm_free;
1155 omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
1156 if (IS_ERR(omap_host->pbias)) {
1157 ret = PTR_ERR(omap_host->pbias);
1159 goto err_pltfm_free;
1160 dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
1162 omap_host->pbias_enabled = false;
1165 * omap_device_pm_domain has callbacks to enable the main
1166 * functional clock, interface clock and also configure the
1167 * SYSCONFIG register of omap devices. The callback will be invoked
1168 * as part of pm_runtime_get_sync.
1170 pm_runtime_enable(dev);
1171 ret = pm_runtime_get_sync(dev);
1173 dev_err(dev, "pm_runtime_get_sync failed\n");
1174 pm_runtime_put_noidle(dev);
1175 goto err_rpm_disable;
1178 ret = sdhci_omap_set_capabilities(omap_host);
1180 dev_err(dev, "failed to set system capabilities\n");
1184 host->mmc_host_ops.start_signal_voltage_switch =
1185 sdhci_omap_start_signal_voltage_switch;
1186 host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
1187 host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
1188 host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
1189 host->mmc_host_ops.enable_sdio_irq = sdhci_omap_enable_sdio_irq;
1191 /* Switch to external DMA only if there is the "dmas" property */
1192 if (of_find_property(dev->of_node, "dmas", NULL))
1193 sdhci_switch_external_dma(host, true);
1195 /* R1B responses is required to properly manage HW busy detection. */
1196 mmc->caps |= MMC_CAP_NEED_RSP_BUSY;
1198 ret = sdhci_setup_host(host);
1202 ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
1204 goto err_cleanup_host;
1206 ret = __sdhci_add_host(host);
1208 goto err_cleanup_host;
1213 sdhci_cleanup_host(host);
1216 pm_runtime_put_sync(dev);
1219 pm_runtime_disable(dev);
1222 sdhci_pltfm_free(pdev);
1226 static int sdhci_omap_remove(struct platform_device *pdev)
1228 struct device *dev = &pdev->dev;
1229 struct sdhci_host *host = platform_get_drvdata(pdev);
1231 sdhci_remove_host(host, true);
1232 pm_runtime_put_sync(dev);
1233 pm_runtime_disable(dev);
1234 sdhci_pltfm_free(pdev);
1239 static struct platform_driver sdhci_omap_driver = {
1240 .probe = sdhci_omap_probe,
1241 .remove = sdhci_omap_remove,
1243 .name = "sdhci-omap",
1244 .of_match_table = omap_sdhci_match,
1248 module_platform_driver(sdhci_omap_driver);
1250 MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
1251 MODULE_AUTHOR("Texas Instruments Inc.");
1252 MODULE_LICENSE("GPL v2");
1253 MODULE_ALIAS("platform:sdhci_omap");