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_bus_mode(struct sdhci_omap_host
*omap_host
,
252 if (omap_host
->bus_mode
== mode
)
255 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_CON
);
256 if (mode
== MMC_BUSMODE_OPENDRAIN
)
260 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CON
, reg
);
262 omap_host
->bus_mode
= mode
;
265 static void sdhci_omap_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
267 struct sdhci_host
*host
= mmc_priv(mmc
);
268 struct sdhci_pltfm_host
*pltfm_host
;
269 struct sdhci_omap_host
*omap_host
;
271 pltfm_host
= sdhci_priv(host
);
272 omap_host
= sdhci_pltfm_priv(pltfm_host
);
274 sdhci_omap_set_bus_mode(omap_host
, ios
->bus_mode
);
275 sdhci_set_ios(mmc
, ios
);
278 static u16
sdhci_omap_calc_divisor(struct sdhci_pltfm_host
*host
,
283 dsor
= DIV_ROUND_UP(clk_get_rate(host
->clk
), clock
);
284 if (dsor
> SYSCTL_CLKD_MAX
)
285 dsor
= SYSCTL_CLKD_MAX
;
290 static void sdhci_omap_start_clock(struct sdhci_omap_host
*omap_host
)
294 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_SYSCTL
);
296 sdhci_omap_writel(omap_host
, SDHCI_OMAP_SYSCTL
, reg
);
299 static void sdhci_omap_stop_clock(struct sdhci_omap_host
*omap_host
)
303 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_SYSCTL
);
305 sdhci_omap_writel(omap_host
, SDHCI_OMAP_SYSCTL
, reg
);
308 static void sdhci_omap_set_clock(struct sdhci_host
*host
, unsigned int clock
)
310 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
311 struct sdhci_omap_host
*omap_host
= sdhci_pltfm_priv(pltfm_host
);
312 unsigned long clkdiv
;
314 sdhci_omap_stop_clock(omap_host
);
319 clkdiv
= sdhci_omap_calc_divisor(pltfm_host
, clock
);
320 clkdiv
= (clkdiv
& SYSCTL_CLKD_MASK
) << SYSCTL_CLKD_SHIFT
;
321 sdhci_enable_clk(host
, clkdiv
);
323 sdhci_omap_start_clock(omap_host
);
326 static void sdhci_omap_set_power(struct sdhci_host
*host
, unsigned char mode
,
329 struct mmc_host
*mmc
= host
->mmc
;
331 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, vdd
);
334 static int sdhci_omap_enable_dma(struct sdhci_host
*host
)
337 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
338 struct sdhci_omap_host
*omap_host
= sdhci_pltfm_priv(pltfm_host
);
340 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_CON
);
341 reg
|= CON_DMA_MASTER
;
342 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CON
, reg
);
347 static unsigned int sdhci_omap_get_min_clock(struct sdhci_host
*host
)
349 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
351 return clk_get_rate(pltfm_host
->clk
) / SYSCTL_CLKD_MAX
;
354 static void sdhci_omap_set_bus_width(struct sdhci_host
*host
, int width
)
356 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
357 struct sdhci_omap_host
*omap_host
= sdhci_pltfm_priv(pltfm_host
);
360 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_CON
);
361 if (width
== MMC_BUS_WIDTH_8
)
365 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CON
, reg
);
367 sdhci_set_bus_width(host
, width
);
370 static void sdhci_omap_init_74_clocks(struct sdhci_host
*host
, u8 power_mode
)
374 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
375 struct sdhci_omap_host
*omap_host
= sdhci_pltfm_priv(pltfm_host
);
377 if (omap_host
->power_mode
== power_mode
)
380 if (power_mode
!= MMC_POWER_ON
)
383 disable_irq(host
->irq
);
385 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_CON
);
387 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CON
, reg
);
388 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CMD
, 0x0);
391 timeout
= ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT
);
392 while (!(sdhci_omap_readl(omap_host
, SDHCI_OMAP_STAT
) & INT_CC_EN
)) {
393 if (WARN_ON(ktime_after(ktime_get(), timeout
)))
398 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_CON
);
400 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CON
, reg
);
401 sdhci_omap_writel(omap_host
, SDHCI_OMAP_STAT
, INT_CC_EN
);
403 enable_irq(host
->irq
);
405 omap_host
->power_mode
= power_mode
;
408 static struct sdhci_ops sdhci_omap_ops
= {
409 .set_clock
= sdhci_omap_set_clock
,
410 .set_power
= sdhci_omap_set_power
,
411 .enable_dma
= sdhci_omap_enable_dma
,
412 .get_max_clock
= sdhci_pltfm_clk_get_max_clock
,
413 .get_min_clock
= sdhci_omap_get_min_clock
,
414 .set_bus_width
= sdhci_omap_set_bus_width
,
415 .platform_send_init_74_clocks
= sdhci_omap_init_74_clocks
,
416 .reset
= sdhci_reset
,
417 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
420 static int sdhci_omap_set_capabilities(struct sdhci_omap_host
*omap_host
)
424 struct device
*dev
= omap_host
->dev
;
425 struct regulator
*vqmmc
;
427 vqmmc
= regulator_get(dev
, "vqmmc");
429 ret
= PTR_ERR(vqmmc
);
433 /* voltage capabilities might be set by boot loader, clear it */
434 reg
= sdhci_omap_readl(omap_host
, SDHCI_OMAP_CAPA
);
435 reg
&= ~(CAPA_VS18
| CAPA_VS30
| CAPA_VS33
);
437 if (regulator_is_supported_voltage(vqmmc
, IOV_3V3
, IOV_3V3
))
439 if (regulator_is_supported_voltage(vqmmc
, IOV_1V8
, IOV_1V8
))
442 sdhci_omap_writel(omap_host
, SDHCI_OMAP_CAPA
, reg
);
445 regulator_put(vqmmc
);
450 static const struct sdhci_pltfm_data sdhci_omap_pdata
= {
451 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
|
452 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
|
453 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN
|
454 SDHCI_QUIRK_NO_HISPD_BIT
|
455 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
,
456 .quirks2
= SDHCI_QUIRK2_NO_1_8_V
|
457 SDHCI_QUIRK2_ACMD23_BROKEN
|
458 SDHCI_QUIRK2_RSP_136_HAS_CRC
,
459 .ops
= &sdhci_omap_ops
,
462 static const struct sdhci_omap_data dra7_data
= {
466 static const struct of_device_id omap_sdhci_match
[] = {
467 { .compatible
= "ti,dra7-sdhci", .data
= &dra7_data
},
470 MODULE_DEVICE_TABLE(of
, omap_sdhci_match
);
472 static int sdhci_omap_probe(struct platform_device
*pdev
)
476 struct device
*dev
= &pdev
->dev
;
477 struct sdhci_host
*host
;
478 struct sdhci_pltfm_host
*pltfm_host
;
479 struct sdhci_omap_host
*omap_host
;
480 struct mmc_host
*mmc
;
481 const struct of_device_id
*match
;
482 struct sdhci_omap_data
*data
;
484 match
= of_match_device(omap_sdhci_match
, dev
);
488 data
= (struct sdhci_omap_data
*)match
->data
;
490 dev_err(dev
, "no sdhci omap data\n");
493 offset
= data
->offset
;
495 host
= sdhci_pltfm_init(pdev
, &sdhci_omap_pdata
,
498 dev_err(dev
, "Failed sdhci_pltfm_init\n");
499 return PTR_ERR(host
);
502 pltfm_host
= sdhci_priv(host
);
503 omap_host
= sdhci_pltfm_priv(pltfm_host
);
504 omap_host
->host
= host
;
505 omap_host
->base
= host
->ioaddr
;
506 omap_host
->dev
= dev
;
507 host
->ioaddr
+= offset
;
510 ret
= mmc_of_parse(mmc
);
514 pltfm_host
->clk
= devm_clk_get(dev
, "fck");
515 if (IS_ERR(pltfm_host
->clk
)) {
516 ret
= PTR_ERR(pltfm_host
->clk
);
520 ret
= clk_set_rate(pltfm_host
->clk
, mmc
->f_max
);
522 dev_err(dev
, "failed to set clock to %d\n", mmc
->f_max
);
526 omap_host
->pbias
= devm_regulator_get_optional(dev
, "pbias");
527 if (IS_ERR(omap_host
->pbias
)) {
528 ret
= PTR_ERR(omap_host
->pbias
);
531 dev_dbg(dev
, "unable to get pbias regulator %d\n", ret
);
533 omap_host
->pbias_enabled
= false;
536 * omap_device_pm_domain has callbacks to enable the main
537 * functional clock, interface clock and also configure the
538 * SYSCONFIG register of omap devices. The callback will be invoked
539 * as part of pm_runtime_get_sync.
541 pm_runtime_enable(dev
);
542 ret
= pm_runtime_get_sync(dev
);
544 dev_err(dev
, "pm_runtime_get_sync failed\n");
545 pm_runtime_put_noidle(dev
);
546 goto err_rpm_disable
;
549 ret
= sdhci_omap_set_capabilities(omap_host
);
551 dev_err(dev
, "failed to set system capabilities\n");
555 host
->mmc_host_ops
.get_ro
= mmc_gpio_get_ro
;
556 host
->mmc_host_ops
.start_signal_voltage_switch
=
557 sdhci_omap_start_signal_voltage_switch
;
558 host
->mmc_host_ops
.set_ios
= sdhci_omap_set_ios
;
560 sdhci_read_caps(host
);
561 host
->caps
|= SDHCI_CAN_DO_ADMA2
;
563 ret
= sdhci_add_host(host
);
570 pm_runtime_put_sync(dev
);
573 pm_runtime_disable(dev
);
576 sdhci_pltfm_free(pdev
);
580 static int sdhci_omap_remove(struct platform_device
*pdev
)
582 struct device
*dev
= &pdev
->dev
;
583 struct sdhci_host
*host
= platform_get_drvdata(pdev
);
585 sdhci_remove_host(host
, true);
586 pm_runtime_put_sync(dev
);
587 pm_runtime_disable(dev
);
588 sdhci_pltfm_free(pdev
);
593 static struct platform_driver sdhci_omap_driver
= {
594 .probe
= sdhci_omap_probe
,
595 .remove
= sdhci_omap_remove
,
597 .name
= "sdhci-omap",
598 .of_match_table
= omap_sdhci_match
,
602 module_platform_driver(sdhci_omap_driver
);
604 MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
605 MODULE_AUTHOR("Texas Instruments Inc.");
606 MODULE_LICENSE("GPL v2");
607 MODULE_ALIAS("platform:sdhci_omap");