xref: /rk3399_rockchip-uboot/drivers/mmc/xenon_sdhci.c (revision 0e00a84cdedf7a1949486746225b35984b351eca)
1b6acb5f1SStefan Roese /*
2b6acb5f1SStefan Roese  * Driver for Marvell SOC Platform Group Xenon SDHC as a platform device
3b6acb5f1SStefan Roese  *
4b6acb5f1SStefan Roese  * Copyright (C) 2016 Marvell, All Rights Reserved.
5b6acb5f1SStefan Roese  *
6b6acb5f1SStefan Roese  * Author:	Victor Gu <xigu@marvell.com>
7b6acb5f1SStefan Roese  * Date:	2016-8-24
8b6acb5f1SStefan Roese  *
9b6acb5f1SStefan Roese  * Included parts of the Linux driver version which was written by:
10b6acb5f1SStefan Roese  * Hu Ziji <huziji@marvell.com>
11b6acb5f1SStefan Roese  *
12b6acb5f1SStefan Roese  * Ported to from Marvell 2015.01 to mainline U-Boot 2017.01:
13b6acb5f1SStefan Roese  * Stefan Roese <sr@denx.de>
14b6acb5f1SStefan Roese  *
15b6acb5f1SStefan Roese  * SPDX-License-Identifier:	GPL-2.0
16b6acb5f1SStefan Roese  */
17b6acb5f1SStefan Roese 
18b6acb5f1SStefan Roese #include <common.h>
19b6acb5f1SStefan Roese #include <dm.h>
20b6acb5f1SStefan Roese #include <fdtdec.h>
21*0e00a84cSMasahiro Yamada #include <linux/libfdt.h>
22b6acb5f1SStefan Roese #include <malloc.h>
23b6acb5f1SStefan Roese #include <sdhci.h>
24b6acb5f1SStefan Roese 
25b6acb5f1SStefan Roese DECLARE_GLOBAL_DATA_PTR;
26b6acb5f1SStefan Roese 
27b6acb5f1SStefan Roese /* Register Offset of SD Host Controller SOCP self-defined register */
28b6acb5f1SStefan Roese #define SDHC_SYS_CFG_INFO			0x0104
29b6acb5f1SStefan Roese #define SLOT_TYPE_SDIO_SHIFT			24
30b6acb5f1SStefan Roese #define SLOT_TYPE_EMMC_MASK			0xFF
31b6acb5f1SStefan Roese #define SLOT_TYPE_EMMC_SHIFT			16
32b6acb5f1SStefan Roese #define SLOT_TYPE_SD_SDIO_MMC_MASK		0xFF
33b6acb5f1SStefan Roese #define SLOT_TYPE_SD_SDIO_MMC_SHIFT		8
34b6acb5f1SStefan Roese #define NR_SUPPORTED_SLOT_MASK			0x7
35b6acb5f1SStefan Roese 
36b6acb5f1SStefan Roese #define SDHC_SYS_OP_CTRL			0x0108
37b6acb5f1SStefan Roese #define AUTO_CLKGATE_DISABLE_MASK		BIT(20)
38b6acb5f1SStefan Roese #define SDCLK_IDLEOFF_ENABLE_SHIFT		8
39b6acb5f1SStefan Roese #define SLOT_ENABLE_SHIFT			0
40b6acb5f1SStefan Roese 
41b6acb5f1SStefan Roese #define SDHC_SYS_EXT_OP_CTRL			0x010C
42b6acb5f1SStefan Roese #define MASK_CMD_CONFLICT_ERROR			BIT(8)
43b6acb5f1SStefan Roese 
44b6acb5f1SStefan Roese #define SDHC_SLOT_RETUNING_REQ_CTRL		0x0144
45b6acb5f1SStefan Roese /* retuning compatible */
46b6acb5f1SStefan Roese #define RETUNING_COMPATIBLE			0x1
47b6acb5f1SStefan Roese 
48b6acb5f1SStefan Roese /* Xenon specific Mode Select value */
49b6acb5f1SStefan Roese #define XENON_SDHCI_CTRL_HS200			0x5
50b6acb5f1SStefan Roese #define XENON_SDHCI_CTRL_HS400			0x6
51b6acb5f1SStefan Roese 
52b6acb5f1SStefan Roese #define EMMC_PHY_REG_BASE			0x170
53b6acb5f1SStefan Roese #define EMMC_PHY_TIMING_ADJUST			EMMC_PHY_REG_BASE
54b6acb5f1SStefan Roese #define OUTPUT_QSN_PHASE_SELECT			BIT(17)
55b6acb5f1SStefan Roese #define SAMPL_INV_QSP_PHASE_SELECT		BIT(18)
56b6acb5f1SStefan Roese #define SAMPL_INV_QSP_PHASE_SELECT_SHIFT	18
57b6acb5f1SStefan Roese #define EMMC_PHY_SLOW_MODE			BIT(29)
58b6acb5f1SStefan Roese #define PHY_INITIALIZAION			BIT(31)
59b6acb5f1SStefan Roese #define WAIT_CYCLE_BEFORE_USING_MASK		0xf
60b6acb5f1SStefan Roese #define WAIT_CYCLE_BEFORE_USING_SHIFT		12
61b6acb5f1SStefan Roese #define FC_SYNC_EN_DURATION_MASK		0xf
62b6acb5f1SStefan Roese #define FC_SYNC_EN_DURATION_SHIFT		8
63b6acb5f1SStefan Roese #define FC_SYNC_RST_EN_DURATION_MASK		0xf
64b6acb5f1SStefan Roese #define FC_SYNC_RST_EN_DURATION_SHIFT		4
65b6acb5f1SStefan Roese #define FC_SYNC_RST_DURATION_MASK		0xf
66b6acb5f1SStefan Roese #define FC_SYNC_RST_DURATION_SHIFT		0
67b6acb5f1SStefan Roese 
68b6acb5f1SStefan Roese #define EMMC_PHY_FUNC_CONTROL			(EMMC_PHY_REG_BASE + 0x4)
69b6acb5f1SStefan Roese #define DQ_ASYNC_MODE				BIT(4)
70b6acb5f1SStefan Roese #define DQ_DDR_MODE_SHIFT			8
71b6acb5f1SStefan Roese #define DQ_DDR_MODE_MASK			0xff
72b6acb5f1SStefan Roese #define CMD_DDR_MODE				BIT(16)
73b6acb5f1SStefan Roese 
74b6acb5f1SStefan Roese #define EMMC_PHY_PAD_CONTROL			(EMMC_PHY_REG_BASE + 0x8)
75b6acb5f1SStefan Roese #define REC_EN_SHIFT				24
76b6acb5f1SStefan Roese #define REC_EN_MASK				0xf
77b6acb5f1SStefan Roese #define FC_DQ_RECEN				BIT(24)
78b6acb5f1SStefan Roese #define FC_CMD_RECEN				BIT(25)
79b6acb5f1SStefan Roese #define FC_QSP_RECEN				BIT(26)
80b6acb5f1SStefan Roese #define FC_QSN_RECEN				BIT(27)
81b6acb5f1SStefan Roese #define OEN_QSN					BIT(28)
82b6acb5f1SStefan Roese #define AUTO_RECEN_CTRL				BIT(30)
83b6acb5f1SStefan Roese 
84b6acb5f1SStefan Roese #define EMMC_PHY_PAD_CONTROL1			(EMMC_PHY_REG_BASE + 0xc)
85b6acb5f1SStefan Roese #define EMMC5_1_FC_QSP_PD			BIT(9)
86b6acb5f1SStefan Roese #define EMMC5_1_FC_QSP_PU			BIT(25)
87b6acb5f1SStefan Roese #define EMMC5_1_FC_CMD_PD			BIT(8)
88b6acb5f1SStefan Roese #define EMMC5_1_FC_CMD_PU			BIT(24)
89b6acb5f1SStefan Roese #define EMMC5_1_FC_DQ_PD			0xff
90b6acb5f1SStefan Roese #define EMMC5_1_FC_DQ_PU			(0xff << 16)
91b6acb5f1SStefan Roese 
92b6acb5f1SStefan Roese #define SDHCI_RETUNE_EVT_INTSIG			0x00001000
93b6acb5f1SStefan Roese 
94b6acb5f1SStefan Roese /* Hyperion only have one slot 0 */
95b6acb5f1SStefan Roese #define XENON_MMC_SLOT_ID_HYPERION		0
96b6acb5f1SStefan Roese 
97b6acb5f1SStefan Roese #define XENON_MMC_MAX_CLK	400000000
98b6acb5f1SStefan Roese 
99b6acb5f1SStefan Roese enum soc_pad_ctrl_type {
100b6acb5f1SStefan Roese 	SOC_PAD_SD,
101b6acb5f1SStefan Roese 	SOC_PAD_FIXED_1_8V,
102b6acb5f1SStefan Roese };
103b6acb5f1SStefan Roese 
104b6acb5f1SStefan Roese struct xenon_sdhci_plat {
105b6acb5f1SStefan Roese 	struct mmc_config cfg;
106b6acb5f1SStefan Roese 	struct mmc mmc;
107b6acb5f1SStefan Roese };
108b6acb5f1SStefan Roese 
109b6acb5f1SStefan Roese struct xenon_sdhci_priv {
110b6acb5f1SStefan Roese 	struct sdhci_host host;
111b6acb5f1SStefan Roese 
112b6acb5f1SStefan Roese 	u8 timing;
113b6acb5f1SStefan Roese 
114b6acb5f1SStefan Roese 	unsigned int clock;
115b6acb5f1SStefan Roese 
116b6acb5f1SStefan Roese 	void *pad_ctrl_reg;
117b6acb5f1SStefan Roese 	int pad_type;
118b6acb5f1SStefan Roese };
119b6acb5f1SStefan Roese 
xenon_mmc_phy_init(struct sdhci_host * host)120b6acb5f1SStefan Roese static int xenon_mmc_phy_init(struct sdhci_host *host)
121b6acb5f1SStefan Roese {
122b6acb5f1SStefan Roese 	struct xenon_sdhci_priv *priv = host->mmc->priv;
123b6acb5f1SStefan Roese 	u32 clock = priv->clock;
124b6acb5f1SStefan Roese 	u32 time;
125b6acb5f1SStefan Roese 	u32 var;
126b6acb5f1SStefan Roese 
127b6acb5f1SStefan Roese 	/* Enable QSP PHASE SELECT */
128b6acb5f1SStefan Roese 	var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
129b6acb5f1SStefan Roese 	var |= SAMPL_INV_QSP_PHASE_SELECT;
130b6acb5f1SStefan Roese 	if ((priv->timing == MMC_TIMING_UHS_SDR50) ||
131b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_UHS_SDR25) ||
132b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_UHS_SDR12) ||
133b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_SD_HS) ||
134b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_LEGACY))
135b6acb5f1SStefan Roese 		var |= EMMC_PHY_SLOW_MODE;
136b6acb5f1SStefan Roese 	sdhci_writel(host, var, EMMC_PHY_TIMING_ADJUST);
137b6acb5f1SStefan Roese 
138b6acb5f1SStefan Roese 	/* Poll for host MMC PHY clock init to be stable */
139b6acb5f1SStefan Roese 	/* Wait up to 10ms */
140b6acb5f1SStefan Roese 	time = 100;
141b6acb5f1SStefan Roese 	while (time--) {
142b6acb5f1SStefan Roese 		var = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
143b6acb5f1SStefan Roese 		if (var & SDHCI_CLOCK_INT_STABLE)
144b6acb5f1SStefan Roese 			break;
145b6acb5f1SStefan Roese 
146b6acb5f1SStefan Roese 		udelay(100);
147b6acb5f1SStefan Roese 	}
148b6acb5f1SStefan Roese 
149b6acb5f1SStefan Roese 	if (time <= 0) {
15090aa625cSMasahiro Yamada 		pr_err("Failed to enable MMC internal clock in time\n");
151b6acb5f1SStefan Roese 		return -ETIMEDOUT;
152b6acb5f1SStefan Roese 	}
153b6acb5f1SStefan Roese 
154b6acb5f1SStefan Roese 	/* Init PHY */
155b6acb5f1SStefan Roese 	var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
156b6acb5f1SStefan Roese 	var |= PHY_INITIALIZAION;
157b6acb5f1SStefan Roese 	sdhci_writel(host, var, EMMC_PHY_TIMING_ADJUST);
158b6acb5f1SStefan Roese 
159b6acb5f1SStefan Roese 	if (clock == 0) {
160b6acb5f1SStefan Roese 		/* Use the possibly slowest bus frequency value */
161b6acb5f1SStefan Roese 		clock = 100000;
162b6acb5f1SStefan Roese 	}
163b6acb5f1SStefan Roese 
164b6acb5f1SStefan Roese 	/* Poll for host eMMC PHY init to complete */
165b6acb5f1SStefan Roese 	/* Wait up to 10ms */
166b6acb5f1SStefan Roese 	time = 100;
167b6acb5f1SStefan Roese 	while (time--) {
168b6acb5f1SStefan Roese 		var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
169b6acb5f1SStefan Roese 		var &= PHY_INITIALIZAION;
170b6acb5f1SStefan Roese 		if (!var)
171b6acb5f1SStefan Roese 			break;
172b6acb5f1SStefan Roese 
173b6acb5f1SStefan Roese 		/* wait for host eMMC PHY init to complete */
174b6acb5f1SStefan Roese 		udelay(100);
175b6acb5f1SStefan Roese 	}
176b6acb5f1SStefan Roese 
177b6acb5f1SStefan Roese 	if (time <= 0) {
17890aa625cSMasahiro Yamada 		pr_err("Failed to init MMC PHY in time\n");
179b6acb5f1SStefan Roese 		return -ETIMEDOUT;
180b6acb5f1SStefan Roese 	}
181b6acb5f1SStefan Roese 
182b6acb5f1SStefan Roese 	return 0;
183b6acb5f1SStefan Roese }
184b6acb5f1SStefan Roese 
185b6acb5f1SStefan Roese #define ARMADA_3700_SOC_PAD_1_8V	0x1
186b6acb5f1SStefan Roese #define ARMADA_3700_SOC_PAD_3_3V	0x0
187b6acb5f1SStefan Roese 
armada_3700_soc_pad_voltage_set(struct sdhci_host * host)188b6acb5f1SStefan Roese static void armada_3700_soc_pad_voltage_set(struct sdhci_host *host)
189b6acb5f1SStefan Roese {
190b6acb5f1SStefan Roese 	struct xenon_sdhci_priv *priv = host->mmc->priv;
191b6acb5f1SStefan Roese 
192b6acb5f1SStefan Roese 	if (priv->pad_type == SOC_PAD_FIXED_1_8V)
193b6acb5f1SStefan Roese 		writel(ARMADA_3700_SOC_PAD_1_8V, priv->pad_ctrl_reg);
194b6acb5f1SStefan Roese 	else if (priv->pad_type == SOC_PAD_SD)
195b6acb5f1SStefan Roese 		writel(ARMADA_3700_SOC_PAD_3_3V, priv->pad_ctrl_reg);
196b6acb5f1SStefan Roese }
197b6acb5f1SStefan Roese 
xenon_mmc_phy_set(struct sdhci_host * host)198b6acb5f1SStefan Roese static void xenon_mmc_phy_set(struct sdhci_host *host)
199b6acb5f1SStefan Roese {
200b6acb5f1SStefan Roese 	struct xenon_sdhci_priv *priv = host->mmc->priv;
201b6acb5f1SStefan Roese 	u32 var;
202b6acb5f1SStefan Roese 
203b6acb5f1SStefan Roese 	/* Setup pad, set bit[30], bit[28] and bits[26:24] */
204b6acb5f1SStefan Roese 	var = sdhci_readl(host, EMMC_PHY_PAD_CONTROL);
205b6acb5f1SStefan Roese 	var |= AUTO_RECEN_CTRL | OEN_QSN | FC_QSP_RECEN |
206b6acb5f1SStefan Roese 		FC_CMD_RECEN | FC_DQ_RECEN;
207b6acb5f1SStefan Roese 	sdhci_writel(host, var, EMMC_PHY_PAD_CONTROL);
208b6acb5f1SStefan Roese 
209b6acb5f1SStefan Roese 	/* Set CMD and DQ Pull Up */
210b6acb5f1SStefan Roese 	var = sdhci_readl(host, EMMC_PHY_PAD_CONTROL1);
211b6acb5f1SStefan Roese 	var |= (EMMC5_1_FC_CMD_PU | EMMC5_1_FC_DQ_PU);
212b6acb5f1SStefan Roese 	var &= ~(EMMC5_1_FC_CMD_PD | EMMC5_1_FC_DQ_PD);
213b6acb5f1SStefan Roese 	sdhci_writel(host, var, EMMC_PHY_PAD_CONTROL1);
214b6acb5f1SStefan Roese 
215b6acb5f1SStefan Roese 	/*
216b6acb5f1SStefan Roese 	 * If timing belongs to high speed, set bit[17] of
217b6acb5f1SStefan Roese 	 * EMMC_PHY_TIMING_ADJUST register
218b6acb5f1SStefan Roese 	 */
219b6acb5f1SStefan Roese 	if ((priv->timing == MMC_TIMING_MMC_HS400) ||
220b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_MMC_HS200) ||
221b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_UHS_SDR50) ||
222b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_UHS_SDR104) ||
223b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_UHS_DDR50) ||
224b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_UHS_SDR25) ||
225b6acb5f1SStefan Roese 	    (priv->timing == MMC_TIMING_MMC_DDR52)) {
226b6acb5f1SStefan Roese 		var = sdhci_readl(host, EMMC_PHY_TIMING_ADJUST);
227b6acb5f1SStefan Roese 		var |= OUTPUT_QSN_PHASE_SELECT;
228b6acb5f1SStefan Roese 		sdhci_writel(host, var, EMMC_PHY_TIMING_ADJUST);
229b6acb5f1SStefan Roese 	}
230b6acb5f1SStefan Roese 
231b6acb5f1SStefan Roese 	/*
232b6acb5f1SStefan Roese 	 * When setting EMMC_PHY_FUNC_CONTROL register,
233b6acb5f1SStefan Roese 	 * SD clock should be disabled
234b6acb5f1SStefan Roese 	 */
235b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
236b6acb5f1SStefan Roese 	var &= ~SDHCI_CLOCK_CARD_EN;
237b6acb5f1SStefan Roese 	sdhci_writew(host, var, SDHCI_CLOCK_CONTROL);
238b6acb5f1SStefan Roese 
239b6acb5f1SStefan Roese 	var = sdhci_readl(host, EMMC_PHY_FUNC_CONTROL);
240caa21a21SZiyuan Xu 	if (mmc_card_ddr(host->mmc)) {
241b6acb5f1SStefan Roese 		var |= (DQ_DDR_MODE_MASK << DQ_DDR_MODE_SHIFT) | CMD_DDR_MODE;
242b6acb5f1SStefan Roese 	} else {
243b6acb5f1SStefan Roese 		var &= ~((DQ_DDR_MODE_MASK << DQ_DDR_MODE_SHIFT) |
244b6acb5f1SStefan Roese 			 CMD_DDR_MODE);
245b6acb5f1SStefan Roese 	}
246b6acb5f1SStefan Roese 	sdhci_writel(host, var, EMMC_PHY_FUNC_CONTROL);
247b6acb5f1SStefan Roese 
248b6acb5f1SStefan Roese 	/* Enable bus clock */
249b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
250b6acb5f1SStefan Roese 	var |= SDHCI_CLOCK_CARD_EN;
251b6acb5f1SStefan Roese 	sdhci_writew(host, var, SDHCI_CLOCK_CONTROL);
252b6acb5f1SStefan Roese 
253b6acb5f1SStefan Roese 	xenon_mmc_phy_init(host);
254b6acb5f1SStefan Roese }
255b6acb5f1SStefan Roese 
256b6acb5f1SStefan Roese /* Enable/Disable the Auto Clock Gating function of this slot */
xenon_mmc_set_acg(struct sdhci_host * host,bool enable)257b6acb5f1SStefan Roese static void xenon_mmc_set_acg(struct sdhci_host *host, bool enable)
258b6acb5f1SStefan Roese {
259b6acb5f1SStefan Roese 	u32 var;
260b6acb5f1SStefan Roese 
261b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHC_SYS_OP_CTRL);
262b6acb5f1SStefan Roese 	if (enable)
263b6acb5f1SStefan Roese 		var &= ~AUTO_CLKGATE_DISABLE_MASK;
264b6acb5f1SStefan Roese 	else
265b6acb5f1SStefan Roese 		var |= AUTO_CLKGATE_DISABLE_MASK;
266b6acb5f1SStefan Roese 
267b6acb5f1SStefan Roese 	sdhci_writel(host, var, SDHC_SYS_OP_CTRL);
268b6acb5f1SStefan Roese }
269b6acb5f1SStefan Roese 
270b6acb5f1SStefan Roese #define SLOT_MASK(slot)		BIT(slot)
271b6acb5f1SStefan Roese 
272b6acb5f1SStefan Roese /* Enable specific slot */
xenon_mmc_enable_slot(struct sdhci_host * host,u8 slot)273b6acb5f1SStefan Roese static void xenon_mmc_enable_slot(struct sdhci_host *host, u8 slot)
274b6acb5f1SStefan Roese {
275b6acb5f1SStefan Roese 	u32 var;
276b6acb5f1SStefan Roese 
277b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHC_SYS_OP_CTRL);
278b6acb5f1SStefan Roese 	var |= SLOT_MASK(slot) << SLOT_ENABLE_SHIFT;
279b6acb5f1SStefan Roese 	sdhci_writel(host, var, SDHC_SYS_OP_CTRL);
280b6acb5f1SStefan Roese }
281b6acb5f1SStefan Roese 
282b6acb5f1SStefan Roese /* Enable Parallel Transfer Mode */
xenon_mmc_enable_parallel_tran(struct sdhci_host * host,u8 slot)283b6acb5f1SStefan Roese static void xenon_mmc_enable_parallel_tran(struct sdhci_host *host, u8 slot)
284b6acb5f1SStefan Roese {
285b6acb5f1SStefan Roese 	u32 var;
286b6acb5f1SStefan Roese 
287b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHC_SYS_EXT_OP_CTRL);
288b6acb5f1SStefan Roese 	var |= SLOT_MASK(slot);
289b6acb5f1SStefan Roese 	sdhci_writel(host, var, SDHC_SYS_EXT_OP_CTRL);
290b6acb5f1SStefan Roese }
291b6acb5f1SStefan Roese 
xenon_mmc_disable_tuning(struct sdhci_host * host,u8 slot)292b6acb5f1SStefan Roese static void xenon_mmc_disable_tuning(struct sdhci_host *host, u8 slot)
293b6acb5f1SStefan Roese {
294b6acb5f1SStefan Roese 	u32 var;
295b6acb5f1SStefan Roese 
296b6acb5f1SStefan Roese 	/* Clear the Re-Tuning Request functionality */
297b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHC_SLOT_RETUNING_REQ_CTRL);
298b6acb5f1SStefan Roese 	var &= ~RETUNING_COMPATIBLE;
299b6acb5f1SStefan Roese 	sdhci_writel(host, var, SDHC_SLOT_RETUNING_REQ_CTRL);
300b6acb5f1SStefan Roese 
301b6acb5f1SStefan Roese 	/* Clear the Re-tuning Event Signal Enable */
302b6acb5f1SStefan Roese 	var = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
303b6acb5f1SStefan Roese 	var &= ~SDHCI_RETUNE_EVT_INTSIG;
304b6acb5f1SStefan Roese 	sdhci_writel(host, var, SDHCI_SIGNAL_ENABLE);
305b6acb5f1SStefan Roese }
306b6acb5f1SStefan Roese 
307b6acb5f1SStefan Roese /* Mask command conflict error */
xenon_mask_cmd_conflict_err(struct sdhci_host * host)308b6acb5f1SStefan Roese static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
309b6acb5f1SStefan Roese {
310b6acb5f1SStefan Roese 	u32  reg;
311b6acb5f1SStefan Roese 
312b6acb5f1SStefan Roese 	reg = sdhci_readl(host, SDHC_SYS_EXT_OP_CTRL);
313b6acb5f1SStefan Roese 	reg |= MASK_CMD_CONFLICT_ERROR;
314b6acb5f1SStefan Roese 	sdhci_writel(host, reg, SDHC_SYS_EXT_OP_CTRL);
315b6acb5f1SStefan Roese }
316b6acb5f1SStefan Roese 
317b6acb5f1SStefan Roese /* Platform specific function for post set_ios configuration */
xenon_sdhci_set_ios_post(struct sdhci_host * host)318b6acb5f1SStefan Roese static void xenon_sdhci_set_ios_post(struct sdhci_host *host)
319b6acb5f1SStefan Roese {
320b6acb5f1SStefan Roese 	struct xenon_sdhci_priv *priv = host->mmc->priv;
3213e3ff0acSZiyuan Xu 	uint speed = host->mmc->clock;
322b6acb5f1SStefan Roese 	int pwr_18v = 0;
323b6acb5f1SStefan Roese 
324b6acb5f1SStefan Roese 	if ((sdhci_readb(host, SDHCI_POWER_CONTROL) & ~SDHCI_POWER_ON) ==
325b6acb5f1SStefan Roese 	    SDHCI_POWER_180)
326b6acb5f1SStefan Roese 		pwr_18v = 1;
327b6acb5f1SStefan Roese 
328b6acb5f1SStefan Roese 	/* Set timing variable according to the configured speed */
329b6acb5f1SStefan Roese 	if (IS_SD(host->mmc)) {
330b6acb5f1SStefan Roese 		/* SD/SDIO */
331b6acb5f1SStefan Roese 		if (pwr_18v) {
332caa21a21SZiyuan Xu 			if (mmc_card_ddr(host->mmc))
333b6acb5f1SStefan Roese 				priv->timing = MMC_TIMING_UHS_DDR50;
334b6acb5f1SStefan Roese 			else if (speed <= 25000000)
335b6acb5f1SStefan Roese 				priv->timing = MMC_TIMING_UHS_SDR25;
336b6acb5f1SStefan Roese 			else
337b6acb5f1SStefan Roese 				priv->timing = MMC_TIMING_UHS_SDR50;
338b6acb5f1SStefan Roese 		} else {
339b6acb5f1SStefan Roese 			if (speed <= 25000000)
340b6acb5f1SStefan Roese 				priv->timing = MMC_TIMING_LEGACY;
341b6acb5f1SStefan Roese 			else
342b6acb5f1SStefan Roese 				priv->timing = MMC_TIMING_SD_HS;
343b6acb5f1SStefan Roese 		}
344b6acb5f1SStefan Roese 	} else {
345b6acb5f1SStefan Roese 		/* eMMC */
346caa21a21SZiyuan Xu 		if (mmc_card_ddr(host->mmc))
347b6acb5f1SStefan Roese 			priv->timing = MMC_TIMING_MMC_DDR52;
348b6acb5f1SStefan Roese 		else if (speed <= 26000000)
349b6acb5f1SStefan Roese 			priv->timing = MMC_TIMING_LEGACY;
350b6acb5f1SStefan Roese 		else
351b6acb5f1SStefan Roese 			priv->timing = MMC_TIMING_MMC_HS;
352b6acb5f1SStefan Roese 	}
353b6acb5f1SStefan Roese 
354b6acb5f1SStefan Roese 	/* Re-init the PHY */
355b6acb5f1SStefan Roese 	xenon_mmc_phy_set(host);
356b6acb5f1SStefan Roese }
357b6acb5f1SStefan Roese 
358b6acb5f1SStefan Roese /* Install a driver specific handler for post set_ios configuration */
359b6acb5f1SStefan Roese static const struct sdhci_ops xenon_sdhci_ops = {
360b6acb5f1SStefan Roese 	.set_ios_post = xenon_sdhci_set_ios_post
361b6acb5f1SStefan Roese };
362b6acb5f1SStefan Roese 
xenon_sdhci_probe(struct udevice * dev)363b6acb5f1SStefan Roese static int xenon_sdhci_probe(struct udevice *dev)
364b6acb5f1SStefan Roese {
365b6acb5f1SStefan Roese 	struct xenon_sdhci_plat *plat = dev_get_platdata(dev);
366b6acb5f1SStefan Roese 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
367b6acb5f1SStefan Roese 	struct xenon_sdhci_priv *priv = dev_get_priv(dev);
368b6acb5f1SStefan Roese 	struct sdhci_host *host = dev_get_priv(dev);
369b6acb5f1SStefan Roese 	int ret;
370b6acb5f1SStefan Roese 
371b6acb5f1SStefan Roese 	host->mmc = &plat->mmc;
372b6acb5f1SStefan Roese 	host->mmc->priv = host;
373b6acb5f1SStefan Roese 	host->mmc->dev = dev;
374b6acb5f1SStefan Roese 	upriv->mmc = host->mmc;
375b6acb5f1SStefan Roese 
376b6acb5f1SStefan Roese 	/* Set quirks */
377b6acb5f1SStefan Roese 	host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_32BIT_DMA_ADDR;
378b6acb5f1SStefan Roese 
379b6acb5f1SStefan Roese 	/* Set default timing */
380b6acb5f1SStefan Roese 	priv->timing = MMC_TIMING_LEGACY;
381b6acb5f1SStefan Roese 
382b6acb5f1SStefan Roese 	/* Disable auto clock gating during init */
383b6acb5f1SStefan Roese 	xenon_mmc_set_acg(host, false);
384b6acb5f1SStefan Roese 
385b6acb5f1SStefan Roese 	/* Enable slot */
386b6acb5f1SStefan Roese 	xenon_mmc_enable_slot(host, XENON_MMC_SLOT_ID_HYPERION);
387b6acb5f1SStefan Roese 
388b6acb5f1SStefan Roese 	/*
389b6acb5f1SStefan Roese 	 * Set default power on SoC PHY PAD register (currently only
390b6acb5f1SStefan Roese 	 * available on the Armada 3700)
391b6acb5f1SStefan Roese 	 */
392b6acb5f1SStefan Roese 	if (priv->pad_ctrl_reg)
393b6acb5f1SStefan Roese 		armada_3700_soc_pad_voltage_set(host);
394b6acb5f1SStefan Roese 
395b6acb5f1SStefan Roese 	host->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_DDR_52MHz;
396e160f7d4SSimon Glass 	switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width",
397e160f7d4SSimon Glass 		1)) {
398b6acb5f1SStefan Roese 	case 8:
399b6acb5f1SStefan Roese 		host->host_caps |= MMC_MODE_8BIT;
400b6acb5f1SStefan Roese 		break;
401b6acb5f1SStefan Roese 	case 4:
402b6acb5f1SStefan Roese 		host->host_caps |= MMC_MODE_4BIT;
403b6acb5f1SStefan Roese 		break;
404b6acb5f1SStefan Roese 	case 1:
405b6acb5f1SStefan Roese 		break;
406b6acb5f1SStefan Roese 	default:
407b6acb5f1SStefan Roese 		printf("Invalid \"bus-width\" value\n");
408b6acb5f1SStefan Roese 		return -EINVAL;
409b6acb5f1SStefan Roese 	}
410b6acb5f1SStefan Roese 
411b6acb5f1SStefan Roese 	host->ops = &xenon_sdhci_ops;
412b6acb5f1SStefan Roese 
413de0359c2SStefan Roese 	host->max_clk = XENON_MMC_MAX_CLK;
414de0359c2SStefan Roese 	ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
415b6acb5f1SStefan Roese 	if (ret)
416b6acb5f1SStefan Roese 		return ret;
417b6acb5f1SStefan Roese 
418b6acb5f1SStefan Roese 	ret = sdhci_probe(dev);
419b6acb5f1SStefan Roese 	if (ret)
420b6acb5f1SStefan Roese 		return ret;
421b6acb5f1SStefan Roese 
422b6acb5f1SStefan Roese 	/* Enable parallel transfer */
423b6acb5f1SStefan Roese 	xenon_mmc_enable_parallel_tran(host, XENON_MMC_SLOT_ID_HYPERION);
424b6acb5f1SStefan Roese 
425b6acb5f1SStefan Roese 	/* Disable tuning functionality of this slot */
426b6acb5f1SStefan Roese 	xenon_mmc_disable_tuning(host, XENON_MMC_SLOT_ID_HYPERION);
427b6acb5f1SStefan Roese 
428b6acb5f1SStefan Roese 	/* Enable auto clock gating after init */
429b6acb5f1SStefan Roese 	xenon_mmc_set_acg(host, true);
430b6acb5f1SStefan Roese 
431b6acb5f1SStefan Roese 	xenon_mask_cmd_conflict_err(host);
432b6acb5f1SStefan Roese 
433b6acb5f1SStefan Roese 	return ret;
434b6acb5f1SStefan Roese }
435b6acb5f1SStefan Roese 
xenon_sdhci_ofdata_to_platdata(struct udevice * dev)436b6acb5f1SStefan Roese static int xenon_sdhci_ofdata_to_platdata(struct udevice *dev)
437b6acb5f1SStefan Roese {
438b6acb5f1SStefan Roese 	struct sdhci_host *host = dev_get_priv(dev);
439b6acb5f1SStefan Roese 	struct xenon_sdhci_priv *priv = dev_get_priv(dev);
440b6acb5f1SStefan Roese 	const char *name;
441b6acb5f1SStefan Roese 
442b6acb5f1SStefan Roese 	host->name = dev->name;
443a821c4afSSimon Glass 	host->ioaddr = (void *)devfdt_get_addr(dev);
444b6acb5f1SStefan Roese 
445911f3aefSSimon Glass 	if (device_is_compatible(dev, "marvell,armada-3700-sdhci"))
446a821c4afSSimon Glass 		priv->pad_ctrl_reg = (void *)devfdt_get_addr_index(dev, 1);
447b6acb5f1SStefan Roese 
448e160f7d4SSimon Glass 	name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "marvell,pad-type",
449b6acb5f1SStefan Roese 			   NULL);
450b6acb5f1SStefan Roese 	if (name) {
451b6acb5f1SStefan Roese 		if (0 == strncmp(name, "sd", 2)) {
452b6acb5f1SStefan Roese 			priv->pad_type = SOC_PAD_SD;
453b6acb5f1SStefan Roese 		} else if (0 == strncmp(name, "fixed-1-8v", 10)) {
454b6acb5f1SStefan Roese 			priv->pad_type = SOC_PAD_FIXED_1_8V;
455b6acb5f1SStefan Roese 		} else {
456b6acb5f1SStefan Roese 			printf("Unsupported SOC PHY PAD ctrl type %s\n", name);
457b6acb5f1SStefan Roese 			return -EINVAL;
458b6acb5f1SStefan Roese 		}
459b6acb5f1SStefan Roese 	}
460b6acb5f1SStefan Roese 
461b6acb5f1SStefan Roese 	return 0;
462b6acb5f1SStefan Roese }
463b6acb5f1SStefan Roese 
xenon_sdhci_bind(struct udevice * dev)464b6acb5f1SStefan Roese static int xenon_sdhci_bind(struct udevice *dev)
465b6acb5f1SStefan Roese {
466b6acb5f1SStefan Roese 	struct xenon_sdhci_plat *plat = dev_get_platdata(dev);
467b6acb5f1SStefan Roese 
468b6acb5f1SStefan Roese 	return sdhci_bind(dev, &plat->mmc, &plat->cfg);
469b6acb5f1SStefan Roese }
470b6acb5f1SStefan Roese 
471b6acb5f1SStefan Roese static const struct udevice_id xenon_sdhci_ids[] = {
472b6acb5f1SStefan Roese 	{ .compatible = "marvell,armada-8k-sdhci",},
473b6acb5f1SStefan Roese 	{ .compatible = "marvell,armada-3700-sdhci",},
474b6acb5f1SStefan Roese 	{ }
475b6acb5f1SStefan Roese };
476b6acb5f1SStefan Roese 
477b6acb5f1SStefan Roese U_BOOT_DRIVER(xenon_sdhci_drv) = {
478b6acb5f1SStefan Roese 	.name		= "xenon_sdhci",
479b6acb5f1SStefan Roese 	.id		= UCLASS_MMC,
480b6acb5f1SStefan Roese 	.of_match	= xenon_sdhci_ids,
481b6acb5f1SStefan Roese 	.ofdata_to_platdata = xenon_sdhci_ofdata_to_platdata,
482b6acb5f1SStefan Roese 	.ops		= &sdhci_ops,
483b6acb5f1SStefan Roese 	.bind		= xenon_sdhci_bind,
484b6acb5f1SStefan Roese 	.probe		= xenon_sdhci_probe,
485b6acb5f1SStefan Roese 	.priv_auto_alloc_size = sizeof(struct xenon_sdhci_priv),
486b6acb5f1SStefan Roese 	.platdata_auto_alloc_size = sizeof(struct xenon_sdhci_plat),
487b6acb5f1SStefan Roese };
488