1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Marvell SOC Platform Group Xenon SDHC as a platform device
5 * Copyright (C) 2016 Marvell, All Rights Reserved.
7 * Author: Victor Gu <xigu@marvell.com>
10 * Included parts of the Linux driver version which was written by:
11 * Hu Ziji <huziji@marvell.com>
13 * Ported to from Marvell 2015.01 to mainline U-Boot 2017.01:
14 * Stefan Roese <sr@denx.de>
20 #include <linux/bitops.h>
21 #include <linux/delay.h>
22 #include <linux/libfdt.h>
25 #include <power/regulator.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 /* Register Offset of SD Host Controller SOCP self-defined register */
30 #define SDHC_SYS_CFG_INFO 0x0104
31 #define SLOT_TYPE_SDIO_SHIFT 24
32 #define SLOT_TYPE_EMMC_MASK 0xFF
33 #define SLOT_TYPE_EMMC_SHIFT 16
34 #define SLOT_TYPE_SD_SDIO_MMC_MASK 0xFF
35 #define SLOT_TYPE_SD_SDIO_MMC_SHIFT 8
36 #define NR_SUPPORTED_SLOT_MASK 0x7
38 #define SDHC_SYS_OP_CTRL 0x0108
39 #define AUTO_CLKGATE_DISABLE_MASK BIT(20)
40 #define SDCLK_IDLEOFF_ENABLE_SHIFT 8
41 #define SLOT_ENABLE_SHIFT 0
43 #define SDHC_SYS_EXT_OP_CTRL 0x010C
44 #define MASK_CMD_CONFLICT_ERROR BIT(8)
46 #define SDHC_SLOT_EMMC_CTRL 0x0130
47 #define ENABLE_DATA_STROBE_SHIFT 24
48 #define SET_EMMC_RSTN_SHIFT 16
49 #define EMMC_VCCQ_MASK 0x3
50 #define EMMC_VCCQ_1_8V 0x1
51 #define EMMC_VCCQ_1_2V 0x2
52 #define EMMC_VCCQ_3_3V 0x3
54 #define SDHC_SLOT_RETUNING_REQ_CTRL 0x0144
55 /* retuning compatible */
56 #define RETUNING_COMPATIBLE 0x1
58 /* Xenon specific Mode Select value */
59 #define XENON_SDHCI_CTRL_HS200 0x5
60 #define XENON_SDHCI_CTRL_HS400 0x6
62 #define EMMC_PHY_REG_BASE 0x170
63 #define EMMC_PHY_TIMING_ADJUST EMMC_PHY_REG_BASE
64 #define OUTPUT_QSN_PHASE_SELECT BIT(17)
65 #define SAMPL_INV_QSP_PHASE_SELECT BIT(18)
66 #define SAMPL_INV_QSP_PHASE_SELECT_SHIFT 18
67 #define EMMC_PHY_SLOW_MODE BIT(29)
68 #define PHY_INITIALIZAION BIT(31)
69 #define WAIT_CYCLE_BEFORE_USING_MASK 0xf
70 #define WAIT_CYCLE_BEFORE_USING_SHIFT 12
71 #define FC_SYNC_EN_DURATION_MASK 0xf
72 #define FC_SYNC_EN_DURATION_SHIFT 8
73 #define FC_SYNC_RST_EN_DURATION_MASK 0xf
74 #define FC_SYNC_RST_EN_DURATION_SHIFT 4
75 #define FC_SYNC_RST_DURATION_MASK 0xf
76 #define FC_SYNC_RST_DURATION_SHIFT 0
78 #define EMMC_PHY_FUNC_CONTROL (EMMC_PHY_REG_BASE + 0x4)
79 #define DQ_ASYNC_MODE BIT(4)
80 #define DQ_DDR_MODE_SHIFT 8
81 #define DQ_DDR_MODE_MASK 0xff
82 #define CMD_DDR_MODE BIT(16)
84 #define EMMC_PHY_PAD_CONTROL (EMMC_PHY_REG_BASE + 0x8)
85 #define REC_EN_SHIFT 24
86 #define REC_EN_MASK 0xf
87 #define FC_DQ_RECEN BIT(24)
88 #define FC_CMD_RECEN BIT(25)
89 #define FC_QSP_RECEN BIT(26)
90 #define FC_QSN_RECEN BIT(27)
91 #define OEN_QSN BIT(28)
92 #define AUTO_RECEN_CTRL BIT(30)
94 #define EMMC_PHY_PAD_CONTROL1 (EMMC_PHY_REG_BASE + 0xc)
95 #define EMMC5_1_FC_QSP_PD BIT(9)
96 #define EMMC5_1_FC_QSP_PU BIT(25)
97 #define EMMC5_1_FC_CMD_PD BIT(8)
98 #define EMMC5_1_FC_CMD_PU BIT(24)
99 #define EMMC5_1_FC_DQ_PD 0xff
100 #define EMMC5_1_FC_DQ_PU (0xff << 16)
102 #define SDHCI_RETUNE_EVT_INTSIG 0x00001000
104 /* Hyperion only have one slot 0 */
105 #define XENON_MMC_SLOT_ID_HYPERION 0
107 #define MMC_TIMING_LEGACY 0
108 #define MMC_TIMING_MMC_HS 1
109 #define MMC_TIMING_SD_HS 2
110 #define MMC_TIMING_UHS_SDR12 3
111 #define MMC_TIMING_UHS_SDR25 4
112 #define MMC_TIMING_UHS_SDR50 5
113 #define MMC_TIMING_UHS_SDR104 6
114 #define MMC_TIMING_UHS_DDR50 7
115 #define MMC_TIMING_MMC_DDR52 8
116 #define MMC_TIMING_MMC_HS200 9
117 #define MMC_TIMING_MMC_HS400 10
119 #define XENON_MMC_MAX_CLK 400000000
120 #define XENON_MMC_3V3_UV 3300000
121 #define XENON_MMC_1V8_UV 1800000
123 enum soc_pad_ctrl_type {
128 struct xenon_sdhci_plat {
129 struct mmc_config cfg;
133 struct xenon_sdhci_priv {
134 struct sdhci_host host;
143 struct udevice *vqmmc;
146 static int xenon_mmc_phy_init(struct sdhci_host *host)
148 struct xenon_sdhci_priv *priv = host->mmc->priv;
149 u32 clock = priv->clock;
153 /* Enable QSP PHASE SELECT */
154 var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
155 var |= SAMPL_INV_QSP_PHASE_SELECT;
156 if ((priv->timing == MMC_TIMING_UHS_SDR50) ||
157 (priv->timing == MMC_TIMING_UHS_SDR25) ||
158 (priv->timing == MMC_TIMING_UHS_SDR12) ||
159 (priv->timing == MMC_TIMING_SD_HS) ||
160 (priv->timing == MMC_TIMING_LEGACY))
161 var |= EMMC_PHY_SLOW_MODE;
162 sdhci_writel(host, var, EMMC_PHY_TIMING_ADJUST);
164 /* Poll for host MMC PHY clock init to be stable */
165 /* Wait up to 10ms */
168 var = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
169 if (var & SDHCI_CLOCK_INT_STABLE)
176 pr_err("Failed to enable MMC internal clock in time\n");
181 var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
182 var |= PHY_INITIALIZAION;
183 sdhci_writel(host, var, EMMC_PHY_TIMING_ADJUST);
186 /* Use the possibly slowest bus frequency value */
190 /* Poll for host eMMC PHY init to complete */
191 /* Wait up to 10ms */
194 var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
195 var &= PHY_INITIALIZAION;
199 /* wait for host eMMC PHY init to complete */
204 pr_err("Failed to init MMC PHY in time\n");
211 #define ARMADA_3700_SOC_PAD_1_8V 0x1
212 #define ARMADA_3700_SOC_PAD_3_3V 0x0
214 static void armada_3700_soc_pad_voltage_set(struct sdhci_host *host)
216 struct xenon_sdhci_priv *priv = host->mmc->priv;
218 if (priv->pad_type == SOC_PAD_FIXED_1_8V)
219 writel(ARMADA_3700_SOC_PAD_1_8V, priv->pad_ctrl_reg);
220 else if (priv->pad_type == SOC_PAD_SD)
221 writel(ARMADA_3700_SOC_PAD_3_3V, priv->pad_ctrl_reg);
224 static int xenon_mmc_start_signal_voltage_switch(struct sdhci_host *host)
226 struct xenon_sdhci_priv *priv = host->mmc->priv;
231 /* If there is no vqmmc regulator, return */
235 if (priv->pad_type == SOC_PAD_FIXED_1_8V) {
237 ret = regulator_set_value(priv->vqmmc,
239 } else if (priv->pad_type == SOC_PAD_SD) {
240 /* Get voltage info */
241 voltage = sdhci_readb(host, SDHCI_POWER_CONTROL);
242 voltage &= ~SDHCI_POWER_ON;
244 if (voltage == SDHCI_POWER_330) {
246 ret = regulator_set_value(priv->vqmmc,
250 ret = regulator_set_value(priv->vqmmc,
255 /* Set VCCQ, eMMC mode: 1.8V; SD/SDIO mode: 3.3V */
256 ctrl = sdhci_readl(host, SDHC_SLOT_EMMC_CTRL);
257 if (IS_SD(host->mmc))
258 ctrl |= EMMC_VCCQ_3_3V;
260 ctrl |= EMMC_VCCQ_1_8V;
261 sdhci_writel(host, ctrl, SDHC_SLOT_EMMC_CTRL);
264 printf("Signal voltage switch fail\n");
269 static void xenon_mmc_phy_set(struct sdhci_host *host)
271 struct xenon_sdhci_priv *priv = host->mmc->priv;
274 /* Setup pad, set bit[30], bit[28] and bits[26:24] */
275 var = sdhci_readl(host, EMMC_PHY_PAD_CONTROL);
276 var |= AUTO_RECEN_CTRL | OEN_QSN | FC_QSP_RECEN |
277 FC_CMD_RECEN | FC_DQ_RECEN;
278 sdhci_writel(host, var, EMMC_PHY_PAD_CONTROL);
280 /* Set CMD and DQ Pull Up */
281 var = sdhci_readl(host, EMMC_PHY_PAD_CONTROL1);
282 var |= (EMMC5_1_FC_CMD_PU | EMMC5_1_FC_DQ_PU);
283 var &= ~(EMMC5_1_FC_CMD_PD | EMMC5_1_FC_DQ_PD);
284 sdhci_writel(host, var, EMMC_PHY_PAD_CONTROL1);
287 * If timing belongs to high speed, set bit[17] of
288 * EMMC_PHY_TIMING_ADJUST register
290 if ((priv->timing == MMC_TIMING_MMC_HS400) ||
291 (priv->timing == MMC_TIMING_MMC_HS200) ||
292 (priv->timing == MMC_TIMING_UHS_SDR50) ||
293 (priv->timing == MMC_TIMING_UHS_SDR104) ||
294 (priv->timing == MMC_TIMING_UHS_DDR50) ||
295 (priv->timing == MMC_TIMING_UHS_SDR25) ||
296 (priv->timing == MMC_TIMING_MMC_DDR52)) {
297 var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
298 var |= OUTPUT_QSN_PHASE_SELECT;
299 sdhci_writel(host, var, EMMC_PHY_TIMING_ADJUST);
303 * When setting EMMC_PHY_FUNC_CONTROL register,
304 * SD clock should be disabled
306 var = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
307 var &= ~SDHCI_CLOCK_CARD_EN;
308 sdhci_writew(host, var, SDHCI_CLOCK_CONTROL);
310 var = sdhci_readl(host, EMMC_PHY_FUNC_CONTROL);
311 if (host->mmc->ddr_mode) {
312 var |= (DQ_DDR_MODE_MASK << DQ_DDR_MODE_SHIFT) | CMD_DDR_MODE;
314 var &= ~((DQ_DDR_MODE_MASK << DQ_DDR_MODE_SHIFT) |
317 sdhci_writel(host, var, EMMC_PHY_FUNC_CONTROL);
319 /* Enable bus clock */
320 var = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
321 var |= SDHCI_CLOCK_CARD_EN;
322 sdhci_writew(host, var, SDHCI_CLOCK_CONTROL);
324 xenon_mmc_phy_init(host);
327 /* Enable/Disable the Auto Clock Gating function of this slot */
328 static void xenon_mmc_set_acg(struct sdhci_host *host, bool enable)
332 var = sdhci_readl(host, SDHC_SYS_OP_CTRL);
334 var &= ~AUTO_CLKGATE_DISABLE_MASK;
336 var |= AUTO_CLKGATE_DISABLE_MASK;
338 sdhci_writel(host, var, SDHC_SYS_OP_CTRL);
341 #define SLOT_MASK(slot) BIT(slot)
343 /* Enable specific slot */
344 static void xenon_mmc_enable_slot(struct sdhci_host *host, u8 slot)
348 var = sdhci_readl(host, SDHC_SYS_OP_CTRL);
349 var |= SLOT_MASK(slot) << SLOT_ENABLE_SHIFT;
350 sdhci_writel(host, var, SDHC_SYS_OP_CTRL);
353 /* Enable Parallel Transfer Mode */
354 static void xenon_mmc_enable_parallel_tran(struct sdhci_host *host, u8 slot)
358 var = sdhci_readl(host, SDHC_SYS_EXT_OP_CTRL);
359 var |= SLOT_MASK(slot);
360 sdhci_writel(host, var, SDHC_SYS_EXT_OP_CTRL);
363 static void xenon_mmc_disable_tuning(struct sdhci_host *host, u8 slot)
367 /* Clear the Re-Tuning Request functionality */
368 var = sdhci_readl(host, SDHC_SLOT_RETUNING_REQ_CTRL);
369 var &= ~RETUNING_COMPATIBLE;
370 sdhci_writel(host, var, SDHC_SLOT_RETUNING_REQ_CTRL);
372 /* Clear the Re-tuning Event Signal Enable */
373 var = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
374 var &= ~SDHCI_RETUNE_EVT_INTSIG;
375 sdhci_writel(host, var, SDHCI_SIGNAL_ENABLE);
378 /* Mask command conflict error */
379 static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
383 reg = sdhci_readl(host, SDHC_SYS_EXT_OP_CTRL);
384 reg |= MASK_CMD_CONFLICT_ERROR;
385 sdhci_writel(host, reg, SDHC_SYS_EXT_OP_CTRL);
388 /* Platform specific function for post set_ios configuration */
389 static int xenon_sdhci_set_ios_post(struct sdhci_host *host)
391 struct xenon_sdhci_priv *priv = host->mmc->priv;
392 uint speed = host->mmc->tran_speed;
396 * Signal Voltage Switching is only applicable for Host Controllers
399 if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
400 xenon_mmc_start_signal_voltage_switch(host);
402 if ((sdhci_readb(host, SDHCI_POWER_CONTROL) & ~SDHCI_POWER_ON) ==
406 /* Set timing variable according to the configured speed */
407 if (IS_SD(host->mmc)) {
410 if (host->mmc->ddr_mode)
411 priv->timing = MMC_TIMING_UHS_DDR50;
412 else if (speed <= 25000000)
413 priv->timing = MMC_TIMING_UHS_SDR25;
415 priv->timing = MMC_TIMING_UHS_SDR50;
417 if (speed <= 25000000)
418 priv->timing = MMC_TIMING_LEGACY;
420 priv->timing = MMC_TIMING_SD_HS;
424 if (host->mmc->ddr_mode)
425 priv->timing = MMC_TIMING_MMC_DDR52;
426 else if (speed <= 26000000)
427 priv->timing = MMC_TIMING_LEGACY;
429 priv->timing = MMC_TIMING_MMC_HS;
432 /* Re-init the PHY */
433 xenon_mmc_phy_set(host);
438 /* Install a driver specific handler for post set_ios configuration */
439 static const struct sdhci_ops xenon_sdhci_ops = {
440 .set_ios_post = xenon_sdhci_set_ios_post
443 static int xenon_sdhci_probe(struct udevice *dev)
445 struct xenon_sdhci_plat *plat = dev_get_plat(dev);
446 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
447 struct xenon_sdhci_priv *priv = dev_get_priv(dev);
448 struct sdhci_host *host = dev_get_priv(dev);
451 host->mmc = &plat->mmc;
452 host->mmc->priv = host;
453 host->mmc->dev = dev;
454 upriv->mmc = host->mmc;
457 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_32BIT_DMA_ADDR;
459 /* Set default timing */
460 priv->timing = MMC_TIMING_LEGACY;
462 /* Get the vqmmc regulator if there is */
463 device_get_supply_regulator(dev, "vqmmc-supply", &priv->vqmmc);
464 /* Set the initial voltage value to 3.3V if there is regulator */
466 ret = regulator_set_value(priv->vqmmc,
469 printf("Failed to set VQMMC regulator to 3.3V\n");
474 /* Disable auto clock gating during init */
475 xenon_mmc_set_acg(host, false);
478 xenon_mmc_enable_slot(host, XENON_MMC_SLOT_ID_HYPERION);
481 * Set default power on SoC PHY PAD register (currently only
482 * available on the Armada 3700)
484 if (priv->pad_ctrl_reg)
485 armada_3700_soc_pad_voltage_set(host);
487 host->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_DDR_52MHz;
489 ret = mmc_of_parse(dev, &plat->cfg);
493 host->ops = &xenon_sdhci_ops;
495 host->max_clk = XENON_MMC_MAX_CLK;
496 ret = sdhci_setup_cfg(&plat->cfg, host, XENON_MMC_MAX_CLK, 0);
500 ret = sdhci_probe(dev);
504 /* Enable parallel transfer */
505 xenon_mmc_enable_parallel_tran(host, XENON_MMC_SLOT_ID_HYPERION);
507 /* Disable tuning functionality of this slot */
508 xenon_mmc_disable_tuning(host, XENON_MMC_SLOT_ID_HYPERION);
510 /* Enable auto clock gating after init */
511 xenon_mmc_set_acg(host, true);
513 xenon_mask_cmd_conflict_err(host);
518 static int xenon_sdhci_of_to_plat(struct udevice *dev)
520 struct sdhci_host *host = dev_get_priv(dev);
521 struct xenon_sdhci_priv *priv = dev_get_priv(dev);
524 host->name = dev->name;
525 host->ioaddr = dev_read_addr_ptr(dev);
527 if (device_is_compatible(dev, "marvell,armada-3700-sdhci"))
528 priv->pad_ctrl_reg = (void *)devfdt_get_addr_index(dev, 1);
530 name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "marvell,pad-type",
533 if (0 == strncmp(name, "sd", 2)) {
534 priv->pad_type = SOC_PAD_SD;
535 } else if (0 == strncmp(name, "fixed-1-8v", 10)) {
536 priv->pad_type = SOC_PAD_FIXED_1_8V;
538 printf("Unsupported SOC PHY PAD ctrl type %s\n", name);
546 static int xenon_sdhci_bind(struct udevice *dev)
548 struct xenon_sdhci_plat *plat = dev_get_plat(dev);
550 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
553 static const struct udevice_id xenon_sdhci_ids[] = {
554 { .compatible = "marvell,armada-8k-sdhci",},
555 { .compatible = "marvell,armada-3700-sdhci",},
559 U_BOOT_DRIVER(xenon_sdhci_drv) = {
560 .name = "xenon_sdhci",
562 .of_match = xenon_sdhci_ids,
563 .of_to_plat = xenon_sdhci_of_to_plat,
565 .bind = xenon_sdhci_bind,
566 .probe = xenon_sdhci_probe,
567 .priv_auto = sizeof(struct xenon_sdhci_priv),
568 .plat_auto = sizeof(struct xenon_sdhci_plat),