1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Marvell Xenon SDHC as a platform device
5 * Copyright (C) 2016 Marvell, All Rights Reserved.
7 * Author: Hu Ziji <huziji@marvell.com>
10 * Inspired by Jisheng Zhang <jszhang@marvell.com>
11 * Special thanks to Video BG4 project team.
14 #include <linux/delay.h>
15 #include <linux/ktime.h>
16 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
21 #include "sdhci-pltfm.h"
22 #include "sdhci-xenon.h"
24 static int xenon_enable_internal_clk(struct sdhci_host *host)
29 reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
30 reg |= SDHCI_CLOCK_INT_EN;
31 sdhci_writel(host, reg, SDHCI_CLOCK_CONTROL);
33 timeout = ktime_add_ms(ktime_get(), 20);
35 bool timedout = ktime_after(ktime_get(), timeout);
37 reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
38 if (reg & SDHCI_CLOCK_INT_STABLE)
41 dev_err(mmc_dev(host->mmc), "Internal clock never stabilised.\n");
44 usleep_range(900, 1100);
50 /* Set SDCLK-off-while-idle */
51 static void xenon_set_sdclk_off_idle(struct sdhci_host *host,
52 unsigned char sdhc_id, bool enable)
57 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
58 /* Get the bit shift basing on the SDHC index */
59 mask = (0x1 << (XENON_SDCLK_IDLEOFF_ENABLE_SHIFT + sdhc_id));
65 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
68 /* Enable/Disable the Auto Clock Gating function */
69 static void xenon_set_acg(struct sdhci_host *host, bool enable)
73 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
75 reg &= ~XENON_AUTO_CLKGATE_DISABLE_MASK;
77 reg |= XENON_AUTO_CLKGATE_DISABLE_MASK;
78 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
81 /* Enable this SDHC */
82 static void xenon_enable_sdhc(struct sdhci_host *host,
83 unsigned char sdhc_id)
87 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
88 reg |= (BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
89 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
91 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
93 * Force to clear BUS_TEST to
94 * skip bus_test_pre and bus_test_post
96 host->mmc->caps &= ~MMC_CAP_BUS_WIDTH_TEST;
99 /* Disable this SDHC */
100 static void xenon_disable_sdhc(struct sdhci_host *host,
101 unsigned char sdhc_id)
105 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
106 reg &= ~(BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
107 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
110 /* Enable Parallel Transfer Mode */
111 static void xenon_enable_sdhc_parallel_tran(struct sdhci_host *host,
112 unsigned char sdhc_id)
116 reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
118 sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
121 /* Mask command conflict error */
122 static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
126 reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
127 reg |= XENON_MASK_CMD_CONFLICT_ERR;
128 sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
131 static void xenon_retune_setup(struct sdhci_host *host)
133 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
134 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
137 /* Disable the Re-Tuning Request functionality */
138 reg = sdhci_readl(host, XENON_SLOT_RETUNING_REQ_CTRL);
139 reg &= ~XENON_RETUNING_COMPATIBLE;
140 sdhci_writel(host, reg, XENON_SLOT_RETUNING_REQ_CTRL);
142 /* Disable the Re-tuning Interrupt */
143 reg = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
144 reg &= ~SDHCI_INT_RETUNE;
145 sdhci_writel(host, reg, SDHCI_SIGNAL_ENABLE);
146 reg = sdhci_readl(host, SDHCI_INT_ENABLE);
147 reg &= ~SDHCI_INT_RETUNE;
148 sdhci_writel(host, reg, SDHCI_INT_ENABLE);
150 /* Force to use Tuning Mode 1 */
151 host->tuning_mode = SDHCI_TUNING_MODE_1;
152 /* Set re-tuning period */
153 host->tuning_count = 1 << (priv->tuning_count - 1);
157 * Operations inside struct sdhci_ops
159 /* Recover the Register Setting cleared during SOFTWARE_RESET_ALL */
160 static void xenon_reset_exit(struct sdhci_host *host,
161 unsigned char sdhc_id, u8 mask)
163 /* Only SOFTWARE RESET ALL will clear the register setting */
164 if (!(mask & SDHCI_RESET_ALL))
167 /* Disable tuning request and auto-retuning again */
168 xenon_retune_setup(host);
171 * The ACG should be turned off at the early init time, in order
172 * to solve a possible issues with the 1.8V regulator stabilization.
173 * The feature is enabled in later stage.
175 xenon_set_acg(host, false);
177 xenon_set_sdclk_off_idle(host, sdhc_id, false);
179 xenon_mask_cmd_conflict_err(host);
182 static void xenon_reset(struct sdhci_host *host, u8 mask)
184 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
185 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
187 sdhci_reset(host, mask);
188 xenon_reset_exit(host, priv->sdhc_id, mask);
192 * Xenon defines different values for HS200 and HS400
195 static void xenon_set_uhs_signaling(struct sdhci_host *host,
200 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
201 /* Select Bus Speed Mode for host */
202 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
203 if (timing == MMC_TIMING_MMC_HS200)
204 ctrl_2 |= XENON_CTRL_HS200;
205 else if (timing == MMC_TIMING_UHS_SDR104)
206 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
207 else if (timing == MMC_TIMING_UHS_SDR12)
208 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
209 else if (timing == MMC_TIMING_UHS_SDR25)
210 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
211 else if (timing == MMC_TIMING_UHS_SDR50)
212 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
213 else if ((timing == MMC_TIMING_UHS_DDR50) ||
214 (timing == MMC_TIMING_MMC_DDR52))
215 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
216 else if (timing == MMC_TIMING_MMC_HS400)
217 ctrl_2 |= XENON_CTRL_HS400;
218 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
221 static void xenon_set_power(struct sdhci_host *host, unsigned char mode,
224 struct mmc_host *mmc = host->mmc;
227 sdhci_set_power_noreg(host, mode, vdd);
229 if (host->pwr == pwr)
235 if (!IS_ERR(mmc->supply.vmmc))
236 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
239 static void xenon_voltage_switch(struct sdhci_host *host)
241 /* Wait for 5ms after set 1.8V signal enable bit */
242 usleep_range(5000, 5500);
245 static const struct sdhci_ops sdhci_xenon_ops = {
246 .voltage_switch = xenon_voltage_switch,
247 .set_clock = sdhci_set_clock,
248 .set_power = xenon_set_power,
249 .set_bus_width = sdhci_set_bus_width,
250 .reset = xenon_reset,
251 .set_uhs_signaling = xenon_set_uhs_signaling,
252 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
255 static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
256 .ops = &sdhci_xenon_ops,
257 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
258 SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
259 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
263 * Xenon Specific Operations in mmc_host_ops
265 static void xenon_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 = sdhci_priv(host);
269 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
273 * HS400/HS200/eMMC HS doesn't have Preset Value register.
274 * However, sdhci_set_ios will read HS400/HS200 Preset register.
275 * Disable Preset Value register for HS400/HS200.
276 * eMMC HS with preset_enabled set will trigger a bug in
277 * get_preset_value().
279 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
280 (ios->timing == MMC_TIMING_MMC_HS200) ||
281 (ios->timing == MMC_TIMING_MMC_HS)) {
282 host->preset_enabled = false;
283 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
284 host->flags &= ~SDHCI_PV_ENABLED;
286 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
287 reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
288 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
290 host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
293 sdhci_set_ios(mmc, ios);
294 xenon_phy_adj(host, ios);
296 if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
297 xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
300 static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
303 struct sdhci_host *host = mmc_priv(mmc);
306 * Before SD/SDIO set signal voltage, SD bus clock should be
307 * disabled. However, sdhci_set_clock will also disable the Internal
308 * clock in mmc_set_signal_voltage().
309 * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
310 * Thus here manually enable internal clock.
312 * After switch completes, it is unnecessary to disable internal clock,
313 * since keeping internal clock active obeys SD spec.
315 xenon_enable_internal_clk(host);
317 xenon_soc_pad_ctrl(host, ios->signal_voltage);
320 * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
321 * Thus SDHCI_CTRL_VDD_180 bit might not work then.
322 * Skip the standard voltage switch to avoid any issue.
324 if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
327 return sdhci_start_signal_voltage_switch(mmc, ios);
332 * priv->init_card_type will be used in PHY timing adjustment.
334 static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
336 struct sdhci_host *host = mmc_priv(mmc);
337 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
338 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
340 /* Update card type*/
341 priv->init_card_type = card->type;
344 static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
346 struct sdhci_host *host = mmc_priv(mmc);
348 if (host->timing == MMC_TIMING_UHS_DDR50 ||
349 host->timing == MMC_TIMING_MMC_DDR52)
353 * Currently force Xenon driver back to support mode 1 only,
354 * even though Xenon might claim to support mode 2 or mode 3.
355 * It requires more time to test mode 2/mode 3 on more platforms.
357 if (host->tuning_mode != SDHCI_TUNING_MODE_1)
358 xenon_retune_setup(host);
360 return sdhci_execute_tuning(mmc, opcode);
363 static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
365 struct sdhci_host *host = mmc_priv(mmc);
366 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
367 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
369 u8 sdhc_id = priv->sdhc_id;
371 sdhci_enable_sdio_irq(mmc, enable);
375 * Set SDIO Card Inserted indication
376 * to enable detecting SDIO async irq.
378 reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
379 reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
380 sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
382 /* Clear SDIO Card Inserted indication */
383 reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
384 reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
385 sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
389 static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
391 host->mmc_host_ops.set_ios = xenon_set_ios;
392 host->mmc_host_ops.start_signal_voltage_switch =
393 xenon_start_signal_voltage_switch;
394 host->mmc_host_ops.init_card = xenon_init_card;
395 host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
396 host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
400 * Parse Xenon specific DT properties:
401 * sdhc-id: the index of current SDHC.
402 * Refer to XENON_SYS_CFG_INFO register
403 * tun-count: the interval between re-tuning
405 static int xenon_probe_dt(struct platform_device *pdev)
407 struct device_node *np = pdev->dev.of_node;
408 struct sdhci_host *host = platform_get_drvdata(pdev);
409 struct mmc_host *mmc = host->mmc;
410 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
411 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
412 u32 sdhc_id, nr_sdhc;
415 /* Disable HS200 on Armada AP806 */
416 if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
417 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
420 if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
421 nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
422 nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
423 if (unlikely(sdhc_id > nr_sdhc)) {
424 dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
429 priv->sdhc_id = sdhc_id;
431 tuning_count = XENON_DEF_TUNING_COUNT;
432 if (!of_property_read_u32(np, "marvell,xenon-tun-count",
434 if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
435 dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
436 XENON_DEF_TUNING_COUNT);
437 tuning_count = XENON_DEF_TUNING_COUNT;
440 priv->tuning_count = tuning_count;
442 return xenon_phy_parse_dt(np, host);
445 static int xenon_sdhc_prepare(struct sdhci_host *host)
447 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
448 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
449 u8 sdhc_id = priv->sdhc_id;
452 xenon_enable_sdhc(host, sdhc_id);
455 xenon_set_acg(host, true);
457 /* Enable Parallel Transfer Mode */
458 xenon_enable_sdhc_parallel_tran(host, sdhc_id);
460 /* Disable SDCLK-Off-While-Idle before card init */
461 xenon_set_sdclk_off_idle(host, sdhc_id, false);
463 xenon_mask_cmd_conflict_err(host);
468 static void xenon_sdhc_unprepare(struct sdhci_host *host)
470 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
471 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
472 u8 sdhc_id = priv->sdhc_id;
475 xenon_disable_sdhc(host, sdhc_id);
478 static int xenon_probe(struct platform_device *pdev)
480 struct sdhci_pltfm_host *pltfm_host;
481 struct sdhci_host *host;
482 struct xenon_priv *priv;
485 host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
486 sizeof(struct xenon_priv));
488 return PTR_ERR(host);
490 pltfm_host = sdhci_priv(host);
491 priv = sdhci_pltfm_priv(pltfm_host);
494 * Link Xenon specific mmc_host_ops function,
495 * to replace standard ones in sdhci_ops.
497 xenon_replace_mmc_host_ops(host);
499 pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
500 if (IS_ERR(pltfm_host->clk)) {
501 err = PTR_ERR(pltfm_host->clk);
502 dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
505 err = clk_prepare_enable(pltfm_host->clk);
509 priv->axi_clk = devm_clk_get(&pdev->dev, "axi");
510 if (IS_ERR(priv->axi_clk)) {
511 err = PTR_ERR(priv->axi_clk);
512 if (err == -EPROBE_DEFER)
515 err = clk_prepare_enable(priv->axi_clk);
520 err = mmc_of_parse(host->mmc);
524 sdhci_get_of_property(pdev);
526 xenon_set_acg(host, false);
528 /* Xenon specific dt parse */
529 err = xenon_probe_dt(pdev);
533 err = xenon_sdhc_prepare(host);
537 pm_runtime_get_noresume(&pdev->dev);
538 pm_runtime_set_active(&pdev->dev);
539 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
540 pm_runtime_use_autosuspend(&pdev->dev);
541 pm_runtime_enable(&pdev->dev);
542 pm_suspend_ignore_children(&pdev->dev, 1);
544 err = sdhci_add_host(host);
548 pm_runtime_put_autosuspend(&pdev->dev);
553 pm_runtime_disable(&pdev->dev);
554 pm_runtime_put_noidle(&pdev->dev);
555 xenon_sdhc_unprepare(host);
557 clk_disable_unprepare(priv->axi_clk);
559 clk_disable_unprepare(pltfm_host->clk);
561 sdhci_pltfm_free(pdev);
565 static int xenon_remove(struct platform_device *pdev)
567 struct sdhci_host *host = platform_get_drvdata(pdev);
568 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
569 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
571 pm_runtime_get_sync(&pdev->dev);
572 pm_runtime_disable(&pdev->dev);
573 pm_runtime_put_noidle(&pdev->dev);
575 sdhci_remove_host(host, 0);
577 xenon_sdhc_unprepare(host);
578 clk_disable_unprepare(priv->axi_clk);
579 clk_disable_unprepare(pltfm_host->clk);
581 sdhci_pltfm_free(pdev);
586 #ifdef CONFIG_PM_SLEEP
587 static int xenon_suspend(struct device *dev)
589 struct sdhci_host *host = dev_get_drvdata(dev);
590 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
591 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
594 ret = pm_runtime_force_suspend(dev);
596 priv->restore_needed = true;
602 static int xenon_runtime_suspend(struct device *dev)
604 struct sdhci_host *host = dev_get_drvdata(dev);
605 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
606 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
609 ret = sdhci_runtime_suspend_host(host);
613 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
614 mmc_retune_needed(host->mmc);
616 clk_disable_unprepare(pltfm_host->clk);
618 * Need to update the priv->clock here, or when runtime resume
619 * back, phy don't aware the clock change and won't adjust phy
620 * which will cause cmd err
626 static int xenon_runtime_resume(struct device *dev)
628 struct sdhci_host *host = dev_get_drvdata(dev);
629 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
630 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
633 ret = clk_prepare_enable(pltfm_host->clk);
635 dev_err(dev, "can't enable mainck\n");
639 if (priv->restore_needed) {
640 ret = xenon_sdhc_prepare(host);
643 priv->restore_needed = false;
646 ret = sdhci_runtime_resume_host(host, 0);
651 clk_disable_unprepare(pltfm_host->clk);
654 #endif /* CONFIG_PM */
656 static const struct dev_pm_ops sdhci_xenon_dev_pm_ops = {
657 SET_SYSTEM_SLEEP_PM_OPS(xenon_suspend,
658 pm_runtime_force_resume)
659 SET_RUNTIME_PM_OPS(xenon_runtime_suspend,
660 xenon_runtime_resume,
664 static const struct of_device_id sdhci_xenon_dt_ids[] = {
665 { .compatible = "marvell,armada-ap806-sdhci",},
666 { .compatible = "marvell,armada-cp110-sdhci",},
667 { .compatible = "marvell,armada-3700-sdhci",},
670 MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
672 static struct platform_driver sdhci_xenon_driver = {
674 .name = "xenon-sdhci",
675 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
676 .of_match_table = sdhci_xenon_dt_ids,
677 .pm = &sdhci_xenon_dev_pm_ops,
679 .probe = xenon_probe,
680 .remove = xenon_remove,
683 module_platform_driver(sdhci_xenon_driver);
685 MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
686 MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
687 MODULE_LICENSE("GPL v2");