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.
11 * Special thanks to Video BG4 project team.
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/ktime.h>
17 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
22 #include "sdhci-pltfm.h"
23 #include "sdhci-xenon.h"
25 static int xenon_enable_internal_clk(struct sdhci_host *host)
30 reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
31 reg |= SDHCI_CLOCK_INT_EN;
32 sdhci_writel(host, reg, SDHCI_CLOCK_CONTROL);
34 timeout = ktime_add_ms(ktime_get(), 20);
36 bool timedout = ktime_after(ktime_get(), timeout);
38 reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
39 if (reg & SDHCI_CLOCK_INT_STABLE)
42 dev_err(mmc_dev(host->mmc), "Internal clock never stabilised.\n");
45 usleep_range(900, 1100);
51 /* Set SDCLK-off-while-idle */
52 static void xenon_set_sdclk_off_idle(struct sdhci_host *host,
53 unsigned char sdhc_id, bool enable)
58 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
59 /* Get the bit shift basing on the SDHC index */
60 mask = (0x1 << (XENON_SDCLK_IDLEOFF_ENABLE_SHIFT + sdhc_id));
66 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
69 /* Enable/Disable the Auto Clock Gating function */
70 static void xenon_set_acg(struct sdhci_host *host, bool enable)
74 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
76 reg &= ~XENON_AUTO_CLKGATE_DISABLE_MASK;
78 reg |= XENON_AUTO_CLKGATE_DISABLE_MASK;
79 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
82 /* Enable this SDHC */
83 static void xenon_enable_sdhc(struct sdhci_host *host,
84 unsigned char sdhc_id)
88 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
89 reg |= (BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
90 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
92 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
94 * Force to clear BUS_TEST to
95 * skip bus_test_pre and bus_test_post
97 host->mmc->caps &= ~MMC_CAP_BUS_WIDTH_TEST;
100 /* Disable this SDHC */
101 static void xenon_disable_sdhc(struct sdhci_host *host,
102 unsigned char sdhc_id)
106 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
107 reg &= ~(BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
108 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
111 /* Enable Parallel Transfer Mode */
112 static void xenon_enable_sdhc_parallel_tran(struct sdhci_host *host,
113 unsigned char sdhc_id)
117 reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
119 sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
122 /* Mask command conflict error */
123 static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
127 reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
128 reg |= XENON_MASK_CMD_CONFLICT_ERR;
129 sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
132 static void xenon_retune_setup(struct sdhci_host *host)
134 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
135 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
138 /* Disable the Re-Tuning Request functionality */
139 reg = sdhci_readl(host, XENON_SLOT_RETUNING_REQ_CTRL);
140 reg &= ~XENON_RETUNING_COMPATIBLE;
141 sdhci_writel(host, reg, XENON_SLOT_RETUNING_REQ_CTRL);
143 /* Disable the Re-tuning Interrupt */
144 reg = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
145 reg &= ~SDHCI_INT_RETUNE;
146 sdhci_writel(host, reg, SDHCI_SIGNAL_ENABLE);
147 reg = sdhci_readl(host, SDHCI_INT_ENABLE);
148 reg &= ~SDHCI_INT_RETUNE;
149 sdhci_writel(host, reg, SDHCI_INT_ENABLE);
151 /* Force to use Tuning Mode 1 */
152 host->tuning_mode = SDHCI_TUNING_MODE_1;
153 /* Set re-tuning period */
154 host->tuning_count = 1 << (priv->tuning_count - 1);
158 * Operations inside struct sdhci_ops
160 /* Recover the Register Setting cleared during SOFTWARE_RESET_ALL */
161 static void xenon_reset_exit(struct sdhci_host *host,
162 unsigned char sdhc_id, u8 mask)
164 /* Only SOFTWARE RESET ALL will clear the register setting */
165 if (!(mask & SDHCI_RESET_ALL))
168 /* Disable tuning request and auto-retuning again */
169 xenon_retune_setup(host);
172 * The ACG should be turned off at the early init time, in order
173 * to solve a possible issues with the 1.8V regulator stabilization.
174 * The feature is enabled in later stage.
176 xenon_set_acg(host, false);
178 xenon_set_sdclk_off_idle(host, sdhc_id, false);
180 xenon_mask_cmd_conflict_err(host);
183 static void xenon_reset(struct sdhci_host *host, u8 mask)
185 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
186 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
188 sdhci_reset(host, mask);
189 xenon_reset_exit(host, priv->sdhc_id, mask);
193 * Xenon defines different values for HS200 and HS400
196 static void xenon_set_uhs_signaling(struct sdhci_host *host,
201 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
202 /* Select Bus Speed Mode for host */
203 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
204 if (timing == MMC_TIMING_MMC_HS200)
205 ctrl_2 |= XENON_CTRL_HS200;
206 else if (timing == MMC_TIMING_UHS_SDR104)
207 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
208 else if (timing == MMC_TIMING_UHS_SDR12)
209 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
210 else if (timing == MMC_TIMING_UHS_SDR25)
211 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
212 else if (timing == MMC_TIMING_UHS_SDR50)
213 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
214 else if ((timing == MMC_TIMING_UHS_DDR50) ||
215 (timing == MMC_TIMING_MMC_DDR52))
216 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
217 else if (timing == MMC_TIMING_MMC_HS400)
218 ctrl_2 |= XENON_CTRL_HS400;
219 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
222 static void xenon_set_power(struct sdhci_host *host, unsigned char mode,
225 struct mmc_host *mmc = host->mmc;
228 sdhci_set_power_noreg(host, mode, vdd);
230 if (host->pwr == pwr)
236 if (!IS_ERR(mmc->supply.vmmc))
237 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
240 static void xenon_voltage_switch(struct sdhci_host *host)
242 /* Wait for 5ms after set 1.8V signal enable bit */
243 usleep_range(5000, 5500);
246 * For some reason the controller's Host Control2 register reports
247 * the bit representing 1.8V signaling as 0 when read after it was
248 * written as 1. Subsequent read reports 1.
250 * Since this may cause some issues, do an empty read of the Host
251 * Control2 register here to circumvent this.
253 sdhci_readw(host, SDHCI_HOST_CONTROL2);
256 static unsigned int xenon_get_max_clock(struct sdhci_host *host)
258 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
261 return sdhci_pltfm_clk_get_max_clock(host);
263 return pltfm_host->clock;
266 static const struct sdhci_ops sdhci_xenon_ops = {
267 .voltage_switch = xenon_voltage_switch,
268 .set_clock = sdhci_set_clock,
269 .set_power = xenon_set_power,
270 .set_bus_width = sdhci_set_bus_width,
271 .reset = xenon_reset,
272 .set_uhs_signaling = xenon_set_uhs_signaling,
273 .get_max_clock = xenon_get_max_clock,
276 static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
277 .ops = &sdhci_xenon_ops,
278 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
279 SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
280 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
284 * Xenon Specific Operations in mmc_host_ops
286 static void xenon_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
288 struct sdhci_host *host = mmc_priv(mmc);
289 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
290 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
294 * HS400/HS200/eMMC HS doesn't have Preset Value register.
295 * However, sdhci_set_ios will read HS400/HS200 Preset register.
296 * Disable Preset Value register for HS400/HS200.
297 * eMMC HS with preset_enabled set will trigger a bug in
298 * get_preset_value().
300 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
301 (ios->timing == MMC_TIMING_MMC_HS200) ||
302 (ios->timing == MMC_TIMING_MMC_HS)) {
303 host->preset_enabled = false;
304 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
305 host->flags &= ~SDHCI_PV_ENABLED;
307 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
308 reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
309 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
311 host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
314 sdhci_set_ios(mmc, ios);
315 xenon_phy_adj(host, ios);
317 if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
318 xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
321 static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
324 struct sdhci_host *host = mmc_priv(mmc);
327 * Before SD/SDIO set signal voltage, SD bus clock should be
328 * disabled. However, sdhci_set_clock will also disable the Internal
329 * clock in mmc_set_signal_voltage().
330 * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
331 * Thus here manually enable internal clock.
333 * After switch completes, it is unnecessary to disable internal clock,
334 * since keeping internal clock active obeys SD spec.
336 xenon_enable_internal_clk(host);
338 xenon_soc_pad_ctrl(host, ios->signal_voltage);
341 * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
342 * Thus SDHCI_CTRL_VDD_180 bit might not work then.
343 * Skip the standard voltage switch to avoid any issue.
345 if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
348 return sdhci_start_signal_voltage_switch(mmc, ios);
353 * priv->init_card_type will be used in PHY timing adjustment.
355 static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
357 struct sdhci_host *host = mmc_priv(mmc);
358 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
359 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
361 /* Update card type*/
362 priv->init_card_type = card->type;
365 static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
367 struct sdhci_host *host = mmc_priv(mmc);
369 if (host->timing == MMC_TIMING_UHS_DDR50 ||
370 host->timing == MMC_TIMING_MMC_DDR52)
374 * Currently force Xenon driver back to support mode 1 only,
375 * even though Xenon might claim to support mode 2 or mode 3.
376 * It requires more time to test mode 2/mode 3 on more platforms.
378 if (host->tuning_mode != SDHCI_TUNING_MODE_1)
379 xenon_retune_setup(host);
381 return sdhci_execute_tuning(mmc, opcode);
384 static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
386 struct sdhci_host *host = mmc_priv(mmc);
387 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
388 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
390 u8 sdhc_id = priv->sdhc_id;
392 sdhci_enable_sdio_irq(mmc, enable);
396 * Set SDIO Card Inserted indication
397 * to enable detecting SDIO async irq.
399 reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
400 reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
401 sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
403 /* Clear SDIO Card Inserted indication */
404 reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
405 reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
406 sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
410 static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
412 host->mmc_host_ops.set_ios = xenon_set_ios;
413 host->mmc_host_ops.start_signal_voltage_switch =
414 xenon_start_signal_voltage_switch;
415 host->mmc_host_ops.init_card = xenon_init_card;
416 host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
417 host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
421 * Parse Xenon specific DT properties:
422 * sdhc-id: the index of current SDHC.
423 * Refer to XENON_SYS_CFG_INFO register
424 * tun-count: the interval between re-tuning
426 static int xenon_probe_params(struct platform_device *pdev)
428 struct device *dev = &pdev->dev;
429 struct sdhci_host *host = platform_get_drvdata(pdev);
430 struct mmc_host *mmc = host->mmc;
431 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
432 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
433 u32 sdhc_id, nr_sdhc;
436 /* Disable HS200 on Armada AP806 */
437 if (priv->hw_version == XENON_AP806)
438 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
441 if (!device_property_read_u32(dev, "marvell,xenon-sdhc-id", &sdhc_id)) {
442 nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
443 nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
444 if (unlikely(sdhc_id > nr_sdhc)) {
445 dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
450 priv->sdhc_id = sdhc_id;
452 tuning_count = XENON_DEF_TUNING_COUNT;
453 if (!device_property_read_u32(dev, "marvell,xenon-tun-count",
455 if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
456 dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
457 XENON_DEF_TUNING_COUNT);
458 tuning_count = XENON_DEF_TUNING_COUNT;
461 priv->tuning_count = tuning_count;
463 return xenon_phy_parse_params(dev, host);
466 static int xenon_sdhc_prepare(struct sdhci_host *host)
468 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
469 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
470 u8 sdhc_id = priv->sdhc_id;
473 xenon_enable_sdhc(host, sdhc_id);
476 xenon_set_acg(host, true);
478 /* Enable Parallel Transfer Mode */
479 xenon_enable_sdhc_parallel_tran(host, sdhc_id);
481 /* Disable SDCLK-Off-While-Idle before card init */
482 xenon_set_sdclk_off_idle(host, sdhc_id, false);
484 xenon_mask_cmd_conflict_err(host);
489 static void xenon_sdhc_unprepare(struct sdhci_host *host)
491 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
492 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
493 u8 sdhc_id = priv->sdhc_id;
496 xenon_disable_sdhc(host, sdhc_id);
499 static int xenon_probe(struct platform_device *pdev)
501 struct sdhci_pltfm_host *pltfm_host;
502 struct device *dev = &pdev->dev;
503 struct sdhci_host *host;
504 struct xenon_priv *priv;
507 host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
508 sizeof(struct xenon_priv));
510 return PTR_ERR(host);
512 pltfm_host = sdhci_priv(host);
513 priv = sdhci_pltfm_priv(pltfm_host);
515 priv->hw_version = (unsigned long)device_get_match_data(&pdev->dev);
518 * Link Xenon specific mmc_host_ops function,
519 * to replace standard ones in sdhci_ops.
521 xenon_replace_mmc_host_ops(host);
524 pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
525 if (IS_ERR(pltfm_host->clk)) {
526 err = PTR_ERR(pltfm_host->clk);
527 dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
530 err = clk_prepare_enable(pltfm_host->clk);
534 priv->axi_clk = devm_clk_get(&pdev->dev, "axi");
535 if (IS_ERR(priv->axi_clk)) {
536 err = PTR_ERR(priv->axi_clk);
537 if (err == -EPROBE_DEFER)
540 err = clk_prepare_enable(priv->axi_clk);
546 err = mmc_of_parse(host->mmc);
550 sdhci_get_property(pdev);
552 xenon_set_acg(host, false);
554 /* Xenon specific parameters parse */
555 err = xenon_probe_params(pdev);
559 err = xenon_sdhc_prepare(host);
563 pm_runtime_get_noresume(&pdev->dev);
564 pm_runtime_set_active(&pdev->dev);
565 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
566 pm_runtime_use_autosuspend(&pdev->dev);
567 pm_runtime_enable(&pdev->dev);
568 pm_suspend_ignore_children(&pdev->dev, 1);
570 err = sdhci_add_host(host);
574 pm_runtime_put_autosuspend(&pdev->dev);
579 pm_runtime_disable(&pdev->dev);
580 pm_runtime_put_noidle(&pdev->dev);
581 xenon_sdhc_unprepare(host);
583 clk_disable_unprepare(priv->axi_clk);
585 clk_disable_unprepare(pltfm_host->clk);
587 sdhci_pltfm_free(pdev);
591 static int xenon_remove(struct platform_device *pdev)
593 struct sdhci_host *host = platform_get_drvdata(pdev);
594 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
595 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
597 pm_runtime_get_sync(&pdev->dev);
598 pm_runtime_disable(&pdev->dev);
599 pm_runtime_put_noidle(&pdev->dev);
601 sdhci_remove_host(host, 0);
603 xenon_sdhc_unprepare(host);
604 clk_disable_unprepare(priv->axi_clk);
605 clk_disable_unprepare(pltfm_host->clk);
607 sdhci_pltfm_free(pdev);
612 #ifdef CONFIG_PM_SLEEP
613 static int xenon_suspend(struct device *dev)
615 struct sdhci_host *host = dev_get_drvdata(dev);
616 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
617 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
620 ret = pm_runtime_force_suspend(dev);
622 priv->restore_needed = true;
628 static int xenon_runtime_suspend(struct device *dev)
630 struct sdhci_host *host = dev_get_drvdata(dev);
631 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
632 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
635 ret = sdhci_runtime_suspend_host(host);
639 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
640 mmc_retune_needed(host->mmc);
642 clk_disable_unprepare(pltfm_host->clk);
644 * Need to update the priv->clock here, or when runtime resume
645 * back, phy don't aware the clock change and won't adjust phy
646 * which will cause cmd err
652 static int xenon_runtime_resume(struct device *dev)
654 struct sdhci_host *host = dev_get_drvdata(dev);
655 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
656 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
659 ret = clk_prepare_enable(pltfm_host->clk);
661 dev_err(dev, "can't enable mainck\n");
665 if (priv->restore_needed) {
666 ret = xenon_sdhc_prepare(host);
669 priv->restore_needed = false;
672 ret = sdhci_runtime_resume_host(host, 0);
677 clk_disable_unprepare(pltfm_host->clk);
680 #endif /* CONFIG_PM */
682 static const struct dev_pm_ops sdhci_xenon_dev_pm_ops = {
683 SET_SYSTEM_SLEEP_PM_OPS(xenon_suspend,
684 pm_runtime_force_resume)
685 SET_RUNTIME_PM_OPS(xenon_runtime_suspend,
686 xenon_runtime_resume,
690 static const struct of_device_id sdhci_xenon_dt_ids[] = {
691 { .compatible = "marvell,armada-ap806-sdhci", .data = (void *)XENON_AP806},
692 { .compatible = "marvell,armada-ap807-sdhci", .data = (void *)XENON_AP807},
693 { .compatible = "marvell,armada-cp110-sdhci", .data = (void *)XENON_CP110},
694 { .compatible = "marvell,armada-3700-sdhci", .data = (void *)XENON_A3700},
697 MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
700 static const struct acpi_device_id sdhci_xenon_acpi_ids[] = {
701 { .id = "MRVL0002", XENON_AP806},
702 { .id = "MRVL0003", XENON_AP807},
703 { .id = "MRVL0004", XENON_CP110},
706 MODULE_DEVICE_TABLE(acpi, sdhci_xenon_acpi_ids);
709 static struct platform_driver sdhci_xenon_driver = {
711 .name = "xenon-sdhci",
712 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
713 .of_match_table = sdhci_xenon_dt_ids,
714 .acpi_match_table = ACPI_PTR(sdhci_xenon_acpi_ids),
715 .pm = &sdhci_xenon_dev_pm_ops,
717 .probe = xenon_probe,
718 .remove = xenon_remove,
721 module_platform_driver(sdhci_xenon_driver);
723 MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
725 MODULE_LICENSE("GPL v2");