xref: /linux/drivers/mmc/host/sdhci-xenon-phy.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
206c8b667SHu Ziji /*
306c8b667SHu Ziji  * PHY support for Xenon SDHC
406c8b667SHu Ziji  *
506c8b667SHu Ziji  * Copyright (C) 2016 Marvell, All Rights Reserved.
606c8b667SHu Ziji  *
706c8b667SHu Ziji  * Author:	Hu Ziji <huziji@marvell.com>
806c8b667SHu Ziji  * Date:	2016-8-24
906c8b667SHu Ziji  */
1006c8b667SHu Ziji 
1106c8b667SHu Ziji #include <linux/slab.h>
1206c8b667SHu Ziji #include <linux/delay.h>
1306c8b667SHu Ziji #include <linux/ktime.h>
148e9f25a2SElad Nachman #include <linux/iopoll.h>
1506c8b667SHu Ziji #include <linux/of_address.h>
1606c8b667SHu Ziji 
1706c8b667SHu Ziji #include "sdhci-pltfm.h"
1806c8b667SHu Ziji #include "sdhci-xenon.h"
1906c8b667SHu Ziji 
2006c8b667SHu Ziji /* Register base for eMMC PHY 5.0 Version */
2106c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_REG_BASE		0x0160
2206c8b667SHu Ziji /* Register base for eMMC PHY 5.1 Version */
2306c8b667SHu Ziji #define XENON_EMMC_PHY_REG_BASE			0x0170
2406c8b667SHu Ziji 
2506c8b667SHu Ziji #define XENON_EMMC_PHY_TIMING_ADJUST		XENON_EMMC_PHY_REG_BASE
2606c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_TIMING_ADJUST	XENON_EMMC_5_0_PHY_REG_BASE
2706c8b667SHu Ziji #define XENON_TIMING_ADJUST_SLOW_MODE		BIT(29)
2806c8b667SHu Ziji #define XENON_TIMING_ADJUST_SDIO_MODE		BIT(28)
2906c8b667SHu Ziji #define XENON_SAMPL_INV_QSP_PHASE_SELECT	BIT(18)
3006c8b667SHu Ziji #define XENON_SAMPL_INV_QSP_PHASE_SELECT_SHIFT	18
3106c8b667SHu Ziji #define XENON_PHY_INITIALIZAION			BIT(31)
3206c8b667SHu Ziji #define XENON_WAIT_CYCLE_BEFORE_USING_MASK	0xF
3306c8b667SHu Ziji #define XENON_WAIT_CYCLE_BEFORE_USING_SHIFT	12
3406c8b667SHu Ziji #define XENON_FC_SYNC_EN_DURATION_MASK		0xF
3506c8b667SHu Ziji #define XENON_FC_SYNC_EN_DURATION_SHIFT		8
3606c8b667SHu Ziji #define XENON_FC_SYNC_RST_EN_DURATION_MASK	0xF
3706c8b667SHu Ziji #define XENON_FC_SYNC_RST_EN_DURATION_SHIFT	4
3806c8b667SHu Ziji #define XENON_FC_SYNC_RST_DURATION_MASK		0xF
3906c8b667SHu Ziji #define XENON_FC_SYNC_RST_DURATION_SHIFT	0
4006c8b667SHu Ziji 
4106c8b667SHu Ziji #define XENON_EMMC_PHY_FUNC_CONTROL		(XENON_EMMC_PHY_REG_BASE + 0x4)
4206c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_FUNC_CONTROL		\
4306c8b667SHu Ziji 	(XENON_EMMC_5_0_PHY_REG_BASE + 0x4)
4406c8b667SHu Ziji #define XENON_ASYNC_DDRMODE_MASK		BIT(23)
4506c8b667SHu Ziji #define XENON_ASYNC_DDRMODE_SHIFT		23
4606c8b667SHu Ziji #define XENON_CMD_DDR_MODE			BIT(16)
4706c8b667SHu Ziji #define XENON_DQ_DDR_MODE_SHIFT			8
4806c8b667SHu Ziji #define XENON_DQ_DDR_MODE_MASK			0xFF
4906c8b667SHu Ziji #define XENON_DQ_ASYNC_MODE			BIT(4)
5006c8b667SHu Ziji 
5106c8b667SHu Ziji #define XENON_EMMC_PHY_PAD_CONTROL		(XENON_EMMC_PHY_REG_BASE + 0x8)
5206c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_PAD_CONTROL		\
5306c8b667SHu Ziji 	(XENON_EMMC_5_0_PHY_REG_BASE + 0x8)
5406c8b667SHu Ziji #define XENON_REC_EN_SHIFT			24
5506c8b667SHu Ziji #define XENON_REC_EN_MASK			0xF
5606c8b667SHu Ziji #define XENON_FC_DQ_RECEN			BIT(24)
5706c8b667SHu Ziji #define XENON_FC_CMD_RECEN			BIT(25)
5806c8b667SHu Ziji #define XENON_FC_QSP_RECEN			BIT(26)
5906c8b667SHu Ziji #define XENON_FC_QSN_RECEN			BIT(27)
6006c8b667SHu Ziji #define XENON_OEN_QSN				BIT(28)
6106c8b667SHu Ziji #define XENON_AUTO_RECEN_CTRL			BIT(30)
6206c8b667SHu Ziji #define XENON_FC_ALL_CMOS_RECEIVER		0xF000
6306c8b667SHu Ziji 
6406c8b667SHu Ziji #define XENON_EMMC5_FC_QSP_PD			BIT(18)
6506c8b667SHu Ziji #define XENON_EMMC5_FC_QSP_PU			BIT(22)
6606c8b667SHu Ziji #define XENON_EMMC5_FC_CMD_PD			BIT(17)
6706c8b667SHu Ziji #define XENON_EMMC5_FC_CMD_PU			BIT(21)
6806c8b667SHu Ziji #define XENON_EMMC5_FC_DQ_PD			BIT(16)
6906c8b667SHu Ziji #define XENON_EMMC5_FC_DQ_PU			BIT(20)
7006c8b667SHu Ziji 
7106c8b667SHu Ziji #define XENON_EMMC_PHY_PAD_CONTROL1		(XENON_EMMC_PHY_REG_BASE + 0xC)
7206c8b667SHu Ziji #define XENON_EMMC5_1_FC_QSP_PD			BIT(9)
7306c8b667SHu Ziji #define XENON_EMMC5_1_FC_QSP_PU			BIT(25)
7406c8b667SHu Ziji #define XENON_EMMC5_1_FC_CMD_PD			BIT(8)
7506c8b667SHu Ziji #define XENON_EMMC5_1_FC_CMD_PU			BIT(24)
7606c8b667SHu Ziji #define XENON_EMMC5_1_FC_DQ_PD			0xFF
7706c8b667SHu Ziji #define XENON_EMMC5_1_FC_DQ_PU			(0xFF << 16)
7806c8b667SHu Ziji 
7906c8b667SHu Ziji #define XENON_EMMC_PHY_PAD_CONTROL2		(XENON_EMMC_PHY_REG_BASE + 0x10)
8006c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_PAD_CONTROL2		\
8106c8b667SHu Ziji 	(XENON_EMMC_5_0_PHY_REG_BASE + 0xC)
8206c8b667SHu Ziji #define XENON_ZNR_MASK				0x1F
8306c8b667SHu Ziji #define XENON_ZNR_SHIFT				8
8406c8b667SHu Ziji #define XENON_ZPR_MASK				0x1F
8506c8b667SHu Ziji /* Preferred ZNR and ZPR value vary between different boards.
8606c8b667SHu Ziji  * The specific ZNR and ZPR value should be defined here
8706c8b667SHu Ziji  * according to board actual timing.
8806c8b667SHu Ziji  */
8906c8b667SHu Ziji #define XENON_ZNR_DEF_VALUE			0xF
9006c8b667SHu Ziji #define XENON_ZPR_DEF_VALUE			0xF
9106c8b667SHu Ziji 
9206c8b667SHu Ziji #define XENON_EMMC_PHY_DLL_CONTROL		(XENON_EMMC_PHY_REG_BASE + 0x14)
9306c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_DLL_CONTROL		\
9406c8b667SHu Ziji 	(XENON_EMMC_5_0_PHY_REG_BASE + 0x10)
9506c8b667SHu Ziji #define XENON_DLL_ENABLE			BIT(31)
9606c8b667SHu Ziji #define XENON_DLL_UPDATE_STROBE_5_0		BIT(30)
9706c8b667SHu Ziji #define XENON_DLL_REFCLK_SEL			BIT(30)
9806c8b667SHu Ziji #define XENON_DLL_UPDATE			BIT(23)
9906c8b667SHu Ziji #define XENON_DLL_PHSEL1_SHIFT			24
10006c8b667SHu Ziji #define XENON_DLL_PHSEL0_SHIFT			16
10106c8b667SHu Ziji #define XENON_DLL_PHASE_MASK			0x3F
10206c8b667SHu Ziji #define XENON_DLL_PHASE_90_DEGREE		0x1F
10306c8b667SHu Ziji #define XENON_DLL_FAST_LOCK			BIT(5)
10406c8b667SHu Ziji #define XENON_DLL_GAIN2X			BIT(3)
10506c8b667SHu Ziji #define XENON_DLL_BYPASS_EN			BIT(0)
10606c8b667SHu Ziji 
10706c8b667SHu Ziji #define XENON_EMMC_5_0_PHY_LOGIC_TIMING_ADJUST	\
10806c8b667SHu Ziji 	(XENON_EMMC_5_0_PHY_REG_BASE + 0x14)
109a04b9b47SHu Ziji #define XENON_EMMC_5_0_PHY_LOGIC_TIMING_VALUE	0x5A54
11006c8b667SHu Ziji #define XENON_EMMC_PHY_LOGIC_TIMING_ADJUST	(XENON_EMMC_PHY_REG_BASE + 0x18)
11106c8b667SHu Ziji #define XENON_LOGIC_TIMING_VALUE		0x00AA8977
11206c8b667SHu Ziji 
113*09e23823SElad Nachman #define XENON_MAX_PHY_TIMEOUT_LOOPS		100
114*09e23823SElad Nachman 
11506c8b667SHu Ziji /*
11606c8b667SHu Ziji  * List offset of PHY registers and some special register values
11706c8b667SHu Ziji  * in eMMC PHY 5.0 or eMMC PHY 5.1
11806c8b667SHu Ziji  */
11906c8b667SHu Ziji struct xenon_emmc_phy_regs {
12006c8b667SHu Ziji 	/* Offset of Timing Adjust register */
12106c8b667SHu Ziji 	u16 timing_adj;
12206c8b667SHu Ziji 	/* Offset of Func Control register */
12306c8b667SHu Ziji 	u16 func_ctrl;
12406c8b667SHu Ziji 	/* Offset of Pad Control register */
12506c8b667SHu Ziji 	u16 pad_ctrl;
12606c8b667SHu Ziji 	/* Offset of Pad Control register 2 */
12706c8b667SHu Ziji 	u16 pad_ctrl2;
12806c8b667SHu Ziji 	/* Offset of DLL Control register */
12906c8b667SHu Ziji 	u16 dll_ctrl;
13006c8b667SHu Ziji 	/* Offset of Logic Timing Adjust register */
13106c8b667SHu Ziji 	u16 logic_timing_adj;
13206c8b667SHu Ziji 	/* DLL Update Enable bit */
13306c8b667SHu Ziji 	u32 dll_update;
134a04b9b47SHu Ziji 	/* value in Logic Timing Adjustment register */
135a04b9b47SHu Ziji 	u32 logic_timing_val;
13606c8b667SHu Ziji };
13706c8b667SHu Ziji 
13806c8b667SHu Ziji static const char * const phy_types[] = {
13906c8b667SHu Ziji 	"emmc 5.0 phy",
14006c8b667SHu Ziji 	"emmc 5.1 phy"
14106c8b667SHu Ziji };
14206c8b667SHu Ziji 
14306c8b667SHu Ziji enum xenon_phy_type_enum {
14406c8b667SHu Ziji 	EMMC_5_0_PHY,
14506c8b667SHu Ziji 	EMMC_5_1_PHY,
14606c8b667SHu Ziji 	NR_PHY_TYPES
14706c8b667SHu Ziji };
14806c8b667SHu Ziji 
149298269c6SHu Ziji enum soc_pad_ctrl_type {
150298269c6SHu Ziji 	SOC_PAD_SD,
151298269c6SHu Ziji 	SOC_PAD_FIXED_1_8V,
152298269c6SHu Ziji };
153298269c6SHu Ziji 
154298269c6SHu Ziji struct soc_pad_ctrl {
155298269c6SHu Ziji 	/* Register address of SoC PHY PAD ctrl */
156298269c6SHu Ziji 	void __iomem	*reg;
157298269c6SHu Ziji 	/* SoC PHY PAD ctrl type */
158298269c6SHu Ziji 	enum soc_pad_ctrl_type pad_type;
159298269c6SHu Ziji 	/* SoC specific operation to set SoC PHY PAD */
160298269c6SHu Ziji 	void (*set_soc_pad)(struct sdhci_host *host,
161298269c6SHu Ziji 			    unsigned char signal_voltage);
162298269c6SHu Ziji };
163298269c6SHu Ziji 
16406c8b667SHu Ziji static struct xenon_emmc_phy_regs xenon_emmc_5_0_phy_regs = {
16506c8b667SHu Ziji 	.timing_adj	= XENON_EMMC_5_0_PHY_TIMING_ADJUST,
16606c8b667SHu Ziji 	.func_ctrl	= XENON_EMMC_5_0_PHY_FUNC_CONTROL,
16706c8b667SHu Ziji 	.pad_ctrl	= XENON_EMMC_5_0_PHY_PAD_CONTROL,
16806c8b667SHu Ziji 	.pad_ctrl2	= XENON_EMMC_5_0_PHY_PAD_CONTROL2,
16906c8b667SHu Ziji 	.dll_ctrl	= XENON_EMMC_5_0_PHY_DLL_CONTROL,
17006c8b667SHu Ziji 	.logic_timing_adj = XENON_EMMC_5_0_PHY_LOGIC_TIMING_ADJUST,
17106c8b667SHu Ziji 	.dll_update	= XENON_DLL_UPDATE_STROBE_5_0,
172a04b9b47SHu Ziji 	.logic_timing_val = XENON_EMMC_5_0_PHY_LOGIC_TIMING_VALUE,
17306c8b667SHu Ziji };
17406c8b667SHu Ziji 
17506c8b667SHu Ziji static struct xenon_emmc_phy_regs xenon_emmc_5_1_phy_regs = {
17606c8b667SHu Ziji 	.timing_adj	= XENON_EMMC_PHY_TIMING_ADJUST,
17706c8b667SHu Ziji 	.func_ctrl	= XENON_EMMC_PHY_FUNC_CONTROL,
17806c8b667SHu Ziji 	.pad_ctrl	= XENON_EMMC_PHY_PAD_CONTROL,
17906c8b667SHu Ziji 	.pad_ctrl2	= XENON_EMMC_PHY_PAD_CONTROL2,
18006c8b667SHu Ziji 	.dll_ctrl	= XENON_EMMC_PHY_DLL_CONTROL,
18106c8b667SHu Ziji 	.logic_timing_adj = XENON_EMMC_PHY_LOGIC_TIMING_ADJUST,
18206c8b667SHu Ziji 	.dll_update	= XENON_DLL_UPDATE,
183a04b9b47SHu Ziji 	.logic_timing_val = XENON_LOGIC_TIMING_VALUE,
18406c8b667SHu Ziji };
18506c8b667SHu Ziji 
18606c8b667SHu Ziji /*
18706c8b667SHu Ziji  * eMMC PHY configuration and operations
18806c8b667SHu Ziji  */
18906c8b667SHu Ziji struct xenon_emmc_phy_params {
19006c8b667SHu Ziji 	bool	slow_mode;
19106c8b667SHu Ziji 
19206c8b667SHu Ziji 	u8	znr;
19306c8b667SHu Ziji 	u8	zpr;
19406c8b667SHu Ziji 
19506c8b667SHu Ziji 	/* Nr of consecutive Sampling Points of a Valid Sampling Window */
19606c8b667SHu Ziji 	u8	nr_tun_times;
19706c8b667SHu Ziji 	/* Divider for calculating Tuning Step */
19806c8b667SHu Ziji 	u8	tun_step_divider;
199298269c6SHu Ziji 
200298269c6SHu Ziji 	struct soc_pad_ctrl pad_ctrl;
20106c8b667SHu Ziji };
20206c8b667SHu Ziji 
xenon_alloc_emmc_phy(struct sdhci_host * host)20306c8b667SHu Ziji static int xenon_alloc_emmc_phy(struct sdhci_host *host)
20406c8b667SHu Ziji {
20506c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
20606c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
20706c8b667SHu Ziji 	struct xenon_emmc_phy_params *params;
20806c8b667SHu Ziji 
20906c8b667SHu Ziji 	params = devm_kzalloc(mmc_dev(host->mmc), sizeof(*params), GFP_KERNEL);
21006c8b667SHu Ziji 	if (!params)
21106c8b667SHu Ziji 		return -ENOMEM;
21206c8b667SHu Ziji 
21306c8b667SHu Ziji 	priv->phy_params = params;
21406c8b667SHu Ziji 	if (priv->phy_type == EMMC_5_0_PHY)
21506c8b667SHu Ziji 		priv->emmc_phy_regs = &xenon_emmc_5_0_phy_regs;
21606c8b667SHu Ziji 	else
21706c8b667SHu Ziji 		priv->emmc_phy_regs = &xenon_emmc_5_1_phy_regs;
21806c8b667SHu Ziji 
21906c8b667SHu Ziji 	return 0;
22006c8b667SHu Ziji }
22106c8b667SHu Ziji 
xenon_check_stability_internal_clk(struct sdhci_host * host)2228e9f25a2SElad Nachman static int xenon_check_stability_internal_clk(struct sdhci_host *host)
2238e9f25a2SElad Nachman {
2248e9f25a2SElad Nachman 	u32 reg;
2258e9f25a2SElad Nachman 	int err;
2268e9f25a2SElad Nachman 
2278e9f25a2SElad Nachman 	err = read_poll_timeout(sdhci_readw, reg, reg & SDHCI_CLOCK_INT_STABLE,
2288e9f25a2SElad Nachman 				1100, 20000, false, host, SDHCI_CLOCK_CONTROL);
2298e9f25a2SElad Nachman 	if (err)
2308e9f25a2SElad Nachman 		dev_err(mmc_dev(host->mmc), "phy_init: Internal clock never stabilized.\n");
2318e9f25a2SElad Nachman 
2328e9f25a2SElad Nachman 	return err;
2338e9f25a2SElad Nachman }
2348e9f25a2SElad Nachman 
23506c8b667SHu Ziji /*
23606c8b667SHu Ziji  * eMMC 5.0/5.1 PHY init/re-init.
23706c8b667SHu Ziji  * eMMC PHY init should be executed after:
23806c8b667SHu Ziji  * 1. SDCLK frequency changes.
23906c8b667SHu Ziji  * 2. SDCLK is stopped and re-enabled.
24006c8b667SHu Ziji  * 3. config in emmc_phy_regs->timing_adj and emmc_phy_regs->func_ctrl
24106c8b667SHu Ziji  * are changed
24206c8b667SHu Ziji  */
xenon_emmc_phy_init(struct sdhci_host * host)24306c8b667SHu Ziji static int xenon_emmc_phy_init(struct sdhci_host *host)
24406c8b667SHu Ziji {
24506c8b667SHu Ziji 	u32 reg;
24606c8b667SHu Ziji 	u32 wait, clock;
24706c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
24806c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
24906c8b667SHu Ziji 	struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs;
25006c8b667SHu Ziji 
2518e9f25a2SElad Nachman 	int ret = xenon_check_stability_internal_clk(host);
2528e9f25a2SElad Nachman 
2538e9f25a2SElad Nachman 	if (ret)
2548e9f25a2SElad Nachman 		return ret;
2558e9f25a2SElad Nachman 
25606c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->timing_adj);
25706c8b667SHu Ziji 	reg |= XENON_PHY_INITIALIZAION;
25806c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->timing_adj);
25906c8b667SHu Ziji 
26006c8b667SHu Ziji 	/* Add duration of FC_SYNC_RST */
26106c8b667SHu Ziji 	wait = ((reg >> XENON_FC_SYNC_RST_DURATION_SHIFT) &
26206c8b667SHu Ziji 			XENON_FC_SYNC_RST_DURATION_MASK);
26306c8b667SHu Ziji 	/* Add interval between FC_SYNC_EN and FC_SYNC_RST */
26406c8b667SHu Ziji 	wait += ((reg >> XENON_FC_SYNC_RST_EN_DURATION_SHIFT) &
26506c8b667SHu Ziji 			XENON_FC_SYNC_RST_EN_DURATION_MASK);
26606c8b667SHu Ziji 	/* Add duration of asserting FC_SYNC_EN */
26706c8b667SHu Ziji 	wait += ((reg >> XENON_FC_SYNC_EN_DURATION_SHIFT) &
26806c8b667SHu Ziji 			XENON_FC_SYNC_EN_DURATION_MASK);
26906c8b667SHu Ziji 	/* Add duration of waiting for PHY */
27006c8b667SHu Ziji 	wait += ((reg >> XENON_WAIT_CYCLE_BEFORE_USING_SHIFT) &
27106c8b667SHu Ziji 			XENON_WAIT_CYCLE_BEFORE_USING_MASK);
27206c8b667SHu Ziji 	/* 4 additional bus clock and 4 AXI bus clock are required */
27306c8b667SHu Ziji 	wait += 8;
27406c8b667SHu Ziji 	wait <<= 20;
27506c8b667SHu Ziji 
27606c8b667SHu Ziji 	clock = host->clock;
27706c8b667SHu Ziji 	if (!clock)
27806c8b667SHu Ziji 		/* Use the possibly slowest bus frequency value */
27906c8b667SHu Ziji 		clock = XENON_LOWEST_SDCLK_FREQ;
28006c8b667SHu Ziji 	/* get the wait time */
28106c8b667SHu Ziji 	wait /= clock;
28206c8b667SHu Ziji 	wait++;
28306c8b667SHu Ziji 
284*09e23823SElad Nachman 	/*
285*09e23823SElad Nachman 	 * AC5X spec says bit must be polled until zero.
286*09e23823SElad Nachman 	 * We see cases in which timeout can take longer
287*09e23823SElad Nachman 	 * than the standard calculation on AC5X, which is
288*09e23823SElad Nachman 	 * expected following the spec comment above.
289*09e23823SElad Nachman 	 * According to the spec, we must wait as long as
290*09e23823SElad Nachman 	 * it takes for that bit to toggle on AC5X.
291*09e23823SElad Nachman 	 * Cap that with 100 delay loops so we won't get
292*09e23823SElad Nachman 	 * stuck here forever:
293*09e23823SElad Nachman 	 */
294*09e23823SElad Nachman 
295*09e23823SElad Nachman 	ret = read_poll_timeout(sdhci_readl, reg,
296*09e23823SElad Nachman 				!(reg & XENON_PHY_INITIALIZAION),
297*09e23823SElad Nachman 				wait, XENON_MAX_PHY_TIMEOUT_LOOPS * wait,
298*09e23823SElad Nachman 				false, host, phy_regs->timing_adj);
299*09e23823SElad Nachman 	if (ret)
30006c8b667SHu Ziji 		dev_err(mmc_dev(host->mmc), "eMMC PHY init cannot complete after %d us\n",
301*09e23823SElad Nachman 			wait * XENON_MAX_PHY_TIMEOUT_LOOPS);
30206c8b667SHu Ziji 
303*09e23823SElad Nachman 	return ret;
30406c8b667SHu Ziji }
30506c8b667SHu Ziji 
306298269c6SHu Ziji #define ARMADA_3700_SOC_PAD_1_8V	0x1
307298269c6SHu Ziji #define ARMADA_3700_SOC_PAD_3_3V	0x0
308298269c6SHu Ziji 
armada_3700_soc_pad_voltage_set(struct sdhci_host * host,unsigned char signal_voltage)309298269c6SHu Ziji static void armada_3700_soc_pad_voltage_set(struct sdhci_host *host,
310298269c6SHu Ziji 					    unsigned char signal_voltage)
311298269c6SHu Ziji {
312298269c6SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
313298269c6SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
314298269c6SHu Ziji 	struct xenon_emmc_phy_params *params = priv->phy_params;
315298269c6SHu Ziji 
316298269c6SHu Ziji 	if (params->pad_ctrl.pad_type == SOC_PAD_FIXED_1_8V) {
317298269c6SHu Ziji 		writel(ARMADA_3700_SOC_PAD_1_8V, params->pad_ctrl.reg);
318298269c6SHu Ziji 	} else if (params->pad_ctrl.pad_type == SOC_PAD_SD) {
319298269c6SHu Ziji 		if (signal_voltage == MMC_SIGNAL_VOLTAGE_180)
320298269c6SHu Ziji 			writel(ARMADA_3700_SOC_PAD_1_8V, params->pad_ctrl.reg);
321298269c6SHu Ziji 		else if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
322298269c6SHu Ziji 			writel(ARMADA_3700_SOC_PAD_3_3V, params->pad_ctrl.reg);
323298269c6SHu Ziji 	}
324298269c6SHu Ziji }
325298269c6SHu Ziji 
326298269c6SHu Ziji /*
327298269c6SHu Ziji  * Set SoC PHY voltage PAD control register,
328298269c6SHu Ziji  * according to the operation voltage on PAD.
329298269c6SHu Ziji  * The detailed operation depends on SoC implementation.
330298269c6SHu Ziji  */
xenon_emmc_phy_set_soc_pad(struct sdhci_host * host,unsigned char signal_voltage)331298269c6SHu Ziji static void xenon_emmc_phy_set_soc_pad(struct sdhci_host *host,
332298269c6SHu Ziji 				       unsigned char signal_voltage)
333298269c6SHu Ziji {
334298269c6SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
335298269c6SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
336298269c6SHu Ziji 	struct xenon_emmc_phy_params *params = priv->phy_params;
337298269c6SHu Ziji 
338298269c6SHu Ziji 	if (!params->pad_ctrl.reg)
339298269c6SHu Ziji 		return;
340298269c6SHu Ziji 
341298269c6SHu Ziji 	if (params->pad_ctrl.set_soc_pad)
342298269c6SHu Ziji 		params->pad_ctrl.set_soc_pad(host, signal_voltage);
343298269c6SHu Ziji }
344298269c6SHu Ziji 
34506c8b667SHu Ziji /*
34606c8b667SHu Ziji  * Enable eMMC PHY HW DLL
34706c8b667SHu Ziji  * DLL should be enabled and stable before HS200/SDR104 tuning,
34806c8b667SHu Ziji  * and before HS400 data strobe setting.
34906c8b667SHu Ziji  */
xenon_emmc_phy_enable_dll(struct sdhci_host * host)35006c8b667SHu Ziji static int xenon_emmc_phy_enable_dll(struct sdhci_host *host)
35106c8b667SHu Ziji {
35206c8b667SHu Ziji 	u32 reg;
35306c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
35406c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
35506c8b667SHu Ziji 	struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs;
35606c8b667SHu Ziji 	ktime_t timeout;
35706c8b667SHu Ziji 
35806c8b667SHu Ziji 	if (WARN_ON(host->clock <= MMC_HIGH_52_MAX_DTR))
35906c8b667SHu Ziji 		return -EINVAL;
36006c8b667SHu Ziji 
36106c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->dll_ctrl);
36206c8b667SHu Ziji 	if (reg & XENON_DLL_ENABLE)
36306c8b667SHu Ziji 		return 0;
36406c8b667SHu Ziji 
36506c8b667SHu Ziji 	/* Enable DLL */
36606c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->dll_ctrl);
36706c8b667SHu Ziji 	reg |= (XENON_DLL_ENABLE | XENON_DLL_FAST_LOCK);
36806c8b667SHu Ziji 
36906c8b667SHu Ziji 	/*
37006c8b667SHu Ziji 	 * Set Phase as 90 degree, which is most common value.
37106c8b667SHu Ziji 	 * Might set another value if necessary.
37206c8b667SHu Ziji 	 * The granularity is 1 degree.
37306c8b667SHu Ziji 	 */
37406c8b667SHu Ziji 	reg &= ~((XENON_DLL_PHASE_MASK << XENON_DLL_PHSEL0_SHIFT) |
37506c8b667SHu Ziji 		 (XENON_DLL_PHASE_MASK << XENON_DLL_PHSEL1_SHIFT));
37606c8b667SHu Ziji 	reg |= ((XENON_DLL_PHASE_90_DEGREE << XENON_DLL_PHSEL0_SHIFT) |
37706c8b667SHu Ziji 		(XENON_DLL_PHASE_90_DEGREE << XENON_DLL_PHSEL1_SHIFT));
37806c8b667SHu Ziji 
37906c8b667SHu Ziji 	reg &= ~XENON_DLL_BYPASS_EN;
38006c8b667SHu Ziji 	reg |= phy_regs->dll_update;
38106c8b667SHu Ziji 	if (priv->phy_type == EMMC_5_1_PHY)
38206c8b667SHu Ziji 		reg &= ~XENON_DLL_REFCLK_SEL;
38306c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->dll_ctrl);
38406c8b667SHu Ziji 
38506c8b667SHu Ziji 	/* Wait max 32 ms */
38606c8b667SHu Ziji 	timeout = ktime_add_ms(ktime_get(), 32);
3870e6e7c2fSAdrian Hunter 	while (1) {
3880e6e7c2fSAdrian Hunter 		bool timedout = ktime_after(ktime_get(), timeout);
3890e6e7c2fSAdrian Hunter 
3900e6e7c2fSAdrian Hunter 		if (sdhci_readw(host, XENON_SLOT_EXT_PRESENT_STATE) &
3910e6e7c2fSAdrian Hunter 		    XENON_DLL_LOCK_STATE)
3920e6e7c2fSAdrian Hunter 			break;
3930e6e7c2fSAdrian Hunter 		if (timedout) {
39406c8b667SHu Ziji 			dev_err(mmc_dev(host->mmc), "Wait for DLL Lock time-out\n");
39506c8b667SHu Ziji 			return -ETIMEDOUT;
39606c8b667SHu Ziji 		}
39706c8b667SHu Ziji 		udelay(100);
39806c8b667SHu Ziji 	}
39906c8b667SHu Ziji 	return 0;
40006c8b667SHu Ziji }
40106c8b667SHu Ziji 
40206c8b667SHu Ziji /*
40306c8b667SHu Ziji  * Config to eMMC PHY to prepare for tuning.
40406c8b667SHu Ziji  * Enable HW DLL and set the TUNING_STEP
40506c8b667SHu Ziji  */
xenon_emmc_phy_config_tuning(struct sdhci_host * host)40606c8b667SHu Ziji static int xenon_emmc_phy_config_tuning(struct sdhci_host *host)
40706c8b667SHu Ziji {
40806c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
40906c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
41006c8b667SHu Ziji 	struct xenon_emmc_phy_params *params = priv->phy_params;
41106c8b667SHu Ziji 	u32 reg, tuning_step;
41206c8b667SHu Ziji 	int ret;
41306c8b667SHu Ziji 
41406c8b667SHu Ziji 	if (host->clock <= MMC_HIGH_52_MAX_DTR)
41506c8b667SHu Ziji 		return -EINVAL;
41606c8b667SHu Ziji 
41706c8b667SHu Ziji 	ret = xenon_emmc_phy_enable_dll(host);
41806c8b667SHu Ziji 	if (ret)
41906c8b667SHu Ziji 		return ret;
42006c8b667SHu Ziji 
42106c8b667SHu Ziji 	/* Achieve TUNING_STEP with HW DLL help */
42206c8b667SHu Ziji 	reg = sdhci_readl(host, XENON_SLOT_DLL_CUR_DLY_VAL);
42306c8b667SHu Ziji 	tuning_step = reg / params->tun_step_divider;
42406c8b667SHu Ziji 	if (unlikely(tuning_step > XENON_TUNING_STEP_MASK)) {
42506c8b667SHu Ziji 		dev_warn(mmc_dev(host->mmc),
42606c8b667SHu Ziji 			 "HS200 TUNING_STEP %d is larger than MAX value\n",
42706c8b667SHu Ziji 			 tuning_step);
42806c8b667SHu Ziji 		tuning_step = XENON_TUNING_STEP_MASK;
42906c8b667SHu Ziji 	}
43006c8b667SHu Ziji 
43106c8b667SHu Ziji 	/* Set TUNING_STEP for later tuning */
43206c8b667SHu Ziji 	reg = sdhci_readl(host, XENON_SLOT_OP_STATUS_CTRL);
43306c8b667SHu Ziji 	reg &= ~(XENON_TUN_CONSECUTIVE_TIMES_MASK <<
43406c8b667SHu Ziji 		 XENON_TUN_CONSECUTIVE_TIMES_SHIFT);
43506c8b667SHu Ziji 	reg |= (params->nr_tun_times << XENON_TUN_CONSECUTIVE_TIMES_SHIFT);
43606c8b667SHu Ziji 	reg &= ~(XENON_TUNING_STEP_MASK << XENON_TUNING_STEP_SHIFT);
43706c8b667SHu Ziji 	reg |= (tuning_step << XENON_TUNING_STEP_SHIFT);
43806c8b667SHu Ziji 	sdhci_writel(host, reg, XENON_SLOT_OP_STATUS_CTRL);
43906c8b667SHu Ziji 
44006c8b667SHu Ziji 	return 0;
44106c8b667SHu Ziji }
44206c8b667SHu Ziji 
xenon_emmc_phy_disable_strobe(struct sdhci_host * host)443aab6e25aSHu Ziji static void xenon_emmc_phy_disable_strobe(struct sdhci_host *host)
44406c8b667SHu Ziji {
445aab6e25aSHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
446aab6e25aSHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
44706c8b667SHu Ziji 	u32 reg;
44806c8b667SHu Ziji 
449aab6e25aSHu Ziji 	/* Disable both SDHC Data Strobe and Enhanced Strobe */
45006c8b667SHu Ziji 	reg = sdhci_readl(host, XENON_SLOT_EMMC_CTRL);
451aab6e25aSHu Ziji 	reg &= ~(XENON_ENABLE_DATA_STROBE | XENON_ENABLE_RESP_STROBE);
45206c8b667SHu Ziji 	sdhci_writel(host, reg, XENON_SLOT_EMMC_CTRL);
453aab6e25aSHu Ziji 
454aab6e25aSHu Ziji 	/* Clear Strobe line Pull down or Pull up */
455aab6e25aSHu Ziji 	if (priv->phy_type == EMMC_5_0_PHY) {
456aab6e25aSHu Ziji 		reg = sdhci_readl(host, XENON_EMMC_5_0_PHY_PAD_CONTROL);
457aab6e25aSHu Ziji 		reg &= ~(XENON_EMMC5_FC_QSP_PD | XENON_EMMC5_FC_QSP_PU);
458aab6e25aSHu Ziji 		sdhci_writel(host, reg, XENON_EMMC_5_0_PHY_PAD_CONTROL);
459aab6e25aSHu Ziji 	} else {
460aab6e25aSHu Ziji 		reg = sdhci_readl(host, XENON_EMMC_PHY_PAD_CONTROL1);
461aab6e25aSHu Ziji 		reg &= ~(XENON_EMMC5_1_FC_QSP_PD | XENON_EMMC5_1_FC_QSP_PU);
462aab6e25aSHu Ziji 		sdhci_writel(host, reg, XENON_EMMC_PHY_PAD_CONTROL1);
463aab6e25aSHu Ziji 	}
46406c8b667SHu Ziji }
46506c8b667SHu Ziji 
466aab6e25aSHu Ziji /* Set HS400 Data Strobe and Enhanced Strobe */
xenon_emmc_phy_strobe_delay_adj(struct sdhci_host * host)46706c8b667SHu Ziji static void xenon_emmc_phy_strobe_delay_adj(struct sdhci_host *host)
46806c8b667SHu Ziji {
46906c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
47006c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
47106c8b667SHu Ziji 	u32 reg;
47206c8b667SHu Ziji 
47306c8b667SHu Ziji 	if (WARN_ON(host->timing != MMC_TIMING_MMC_HS400))
47406c8b667SHu Ziji 		return;
47506c8b667SHu Ziji 
47606c8b667SHu Ziji 	if (host->clock <= MMC_HIGH_52_MAX_DTR)
47706c8b667SHu Ziji 		return;
47806c8b667SHu Ziji 
47906c8b667SHu Ziji 	dev_dbg(mmc_dev(host->mmc), "starts HS400 strobe delay adjustment\n");
48006c8b667SHu Ziji 
48106c8b667SHu Ziji 	xenon_emmc_phy_enable_dll(host);
48206c8b667SHu Ziji 
48306c8b667SHu Ziji 	/* Enable SDHC Data Strobe */
48406c8b667SHu Ziji 	reg = sdhci_readl(host, XENON_SLOT_EMMC_CTRL);
48506c8b667SHu Ziji 	reg |= XENON_ENABLE_DATA_STROBE;
486aab6e25aSHu Ziji 	/*
487aab6e25aSHu Ziji 	 * Enable SDHC Enhanced Strobe if supported
488aab6e25aSHu Ziji 	 * Xenon Enhanced Strobe should be enabled only when
489aab6e25aSHu Ziji 	 * 1. card is in HS400 mode and
490aab6e25aSHu Ziji 	 * 2. SDCLK is higher than 52MHz
491aab6e25aSHu Ziji 	 * 3. DLL is enabled
492aab6e25aSHu Ziji 	 */
493aab6e25aSHu Ziji 	if (host->mmc->ios.enhanced_strobe)
494aab6e25aSHu Ziji 		reg |= XENON_ENABLE_RESP_STROBE;
49506c8b667SHu Ziji 	sdhci_writel(host, reg, XENON_SLOT_EMMC_CTRL);
49606c8b667SHu Ziji 
49706c8b667SHu Ziji 	/* Set Data Strobe Pull down */
49806c8b667SHu Ziji 	if (priv->phy_type == EMMC_5_0_PHY) {
49906c8b667SHu Ziji 		reg = sdhci_readl(host, XENON_EMMC_5_0_PHY_PAD_CONTROL);
50006c8b667SHu Ziji 		reg |= XENON_EMMC5_FC_QSP_PD;
50106c8b667SHu Ziji 		reg &= ~XENON_EMMC5_FC_QSP_PU;
50206c8b667SHu Ziji 		sdhci_writel(host, reg, XENON_EMMC_5_0_PHY_PAD_CONTROL);
50306c8b667SHu Ziji 	} else {
50406c8b667SHu Ziji 		reg = sdhci_readl(host, XENON_EMMC_PHY_PAD_CONTROL1);
50506c8b667SHu Ziji 		reg |= XENON_EMMC5_1_FC_QSP_PD;
50606c8b667SHu Ziji 		reg &= ~XENON_EMMC5_1_FC_QSP_PU;
50706c8b667SHu Ziji 		sdhci_writel(host, reg, XENON_EMMC_PHY_PAD_CONTROL1);
50806c8b667SHu Ziji 	}
50906c8b667SHu Ziji }
51006c8b667SHu Ziji 
51106c8b667SHu Ziji /*
51206c8b667SHu Ziji  * If eMMC PHY Slow Mode is required in lower speed mode (SDCLK < 55MHz)
51306c8b667SHu Ziji  * in SDR mode, enable Slow Mode to bypass eMMC PHY.
51406c8b667SHu Ziji  * SDIO slower SDR mode also requires Slow Mode.
51506c8b667SHu Ziji  *
51606c8b667SHu Ziji  * If Slow Mode is enabled, return true.
51706c8b667SHu Ziji  * Otherwise, return false.
51806c8b667SHu Ziji  */
xenon_emmc_phy_slow_mode(struct sdhci_host * host,unsigned char timing)51906c8b667SHu Ziji static bool xenon_emmc_phy_slow_mode(struct sdhci_host *host,
52006c8b667SHu Ziji 				     unsigned char timing)
52106c8b667SHu Ziji {
52206c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
52306c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
52406c8b667SHu Ziji 	struct xenon_emmc_phy_params *params = priv->phy_params;
52506c8b667SHu Ziji 	struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs;
52606c8b667SHu Ziji 	u32 reg;
52706c8b667SHu Ziji 	int ret;
52806c8b667SHu Ziji 
52906c8b667SHu Ziji 	if (host->clock > MMC_HIGH_52_MAX_DTR)
53006c8b667SHu Ziji 		return false;
53106c8b667SHu Ziji 
53206c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->timing_adj);
53306c8b667SHu Ziji 	/* When in slower SDR mode, enable Slow Mode for SDIO
53406c8b667SHu Ziji 	 * or when Slow Mode flag is set
53506c8b667SHu Ziji 	 */
53606c8b667SHu Ziji 	switch (timing) {
53706c8b667SHu Ziji 	case MMC_TIMING_LEGACY:
53806c8b667SHu Ziji 		/*
53906c8b667SHu Ziji 		 * If Slow Mode is required, enable Slow Mode by default
54006c8b667SHu Ziji 		 * in early init phase to avoid any potential issue.
54106c8b667SHu Ziji 		 */
54206c8b667SHu Ziji 		if (params->slow_mode) {
54306c8b667SHu Ziji 			reg |= XENON_TIMING_ADJUST_SLOW_MODE;
54406c8b667SHu Ziji 			ret = true;
54506c8b667SHu Ziji 		} else {
54606c8b667SHu Ziji 			reg &= ~XENON_TIMING_ADJUST_SLOW_MODE;
54706c8b667SHu Ziji 			ret = false;
54806c8b667SHu Ziji 		}
54906c8b667SHu Ziji 		break;
55006c8b667SHu Ziji 	case MMC_TIMING_UHS_SDR25:
55106c8b667SHu Ziji 	case MMC_TIMING_UHS_SDR12:
55206c8b667SHu Ziji 	case MMC_TIMING_SD_HS:
55306c8b667SHu Ziji 	case MMC_TIMING_MMC_HS:
55406c8b667SHu Ziji 		if ((priv->init_card_type == MMC_TYPE_SDIO) ||
55506c8b667SHu Ziji 		    params->slow_mode) {
55606c8b667SHu Ziji 			reg |= XENON_TIMING_ADJUST_SLOW_MODE;
55706c8b667SHu Ziji 			ret = true;
55806c8b667SHu Ziji 			break;
55906c8b667SHu Ziji 		}
560df561f66SGustavo A. R. Silva 		fallthrough;
56106c8b667SHu Ziji 	default:
56206c8b667SHu Ziji 		reg &= ~XENON_TIMING_ADJUST_SLOW_MODE;
56306c8b667SHu Ziji 		ret = false;
56406c8b667SHu Ziji 	}
56506c8b667SHu Ziji 
56606c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->timing_adj);
56706c8b667SHu Ziji 	return ret;
56806c8b667SHu Ziji }
56906c8b667SHu Ziji 
57006c8b667SHu Ziji /*
57106c8b667SHu Ziji  * Set-up eMMC 5.0/5.1 PHY.
57206c8b667SHu Ziji  * Specific configuration depends on the current speed mode in use.
57306c8b667SHu Ziji  */
xenon_emmc_phy_set(struct sdhci_host * host,unsigned char timing)57406c8b667SHu Ziji static void xenon_emmc_phy_set(struct sdhci_host *host,
57506c8b667SHu Ziji 			       unsigned char timing)
57606c8b667SHu Ziji {
57706c8b667SHu Ziji 	u32 reg;
57806c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
57906c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
58006c8b667SHu Ziji 	struct xenon_emmc_phy_params *params = priv->phy_params;
58106c8b667SHu Ziji 	struct xenon_emmc_phy_regs *phy_regs = priv->emmc_phy_regs;
58206c8b667SHu Ziji 
58306c8b667SHu Ziji 	dev_dbg(mmc_dev(host->mmc), "eMMC PHY setting starts\n");
58406c8b667SHu Ziji 
58506c8b667SHu Ziji 	/* Setup pad, set bit[28] and bits[26:24] */
58606c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->pad_ctrl);
58706c8b667SHu Ziji 	reg |= (XENON_FC_DQ_RECEN | XENON_FC_CMD_RECEN |
58806c8b667SHu Ziji 		XENON_FC_QSP_RECEN | XENON_OEN_QSN);
58906c8b667SHu Ziji 	/* All FC_XX_RECEIVCE should be set as CMOS Type */
59006c8b667SHu Ziji 	reg |= XENON_FC_ALL_CMOS_RECEIVER;
59106c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->pad_ctrl);
59206c8b667SHu Ziji 
59306c8b667SHu Ziji 	/* Set CMD and DQ Pull Up */
59406c8b667SHu Ziji 	if (priv->phy_type == EMMC_5_0_PHY) {
59506c8b667SHu Ziji 		reg = sdhci_readl(host, XENON_EMMC_5_0_PHY_PAD_CONTROL);
59606c8b667SHu Ziji 		reg |= (XENON_EMMC5_FC_CMD_PU | XENON_EMMC5_FC_DQ_PU);
59706c8b667SHu Ziji 		reg &= ~(XENON_EMMC5_FC_CMD_PD | XENON_EMMC5_FC_DQ_PD);
59806c8b667SHu Ziji 		sdhci_writel(host, reg, XENON_EMMC_5_0_PHY_PAD_CONTROL);
59906c8b667SHu Ziji 	} else {
60006c8b667SHu Ziji 		reg = sdhci_readl(host, XENON_EMMC_PHY_PAD_CONTROL1);
60106c8b667SHu Ziji 		reg |= (XENON_EMMC5_1_FC_CMD_PU | XENON_EMMC5_1_FC_DQ_PU);
60206c8b667SHu Ziji 		reg &= ~(XENON_EMMC5_1_FC_CMD_PD | XENON_EMMC5_1_FC_DQ_PD);
60306c8b667SHu Ziji 		sdhci_writel(host, reg, XENON_EMMC_PHY_PAD_CONTROL1);
60406c8b667SHu Ziji 	}
60506c8b667SHu Ziji 
60606c8b667SHu Ziji 	if (timing == MMC_TIMING_LEGACY) {
60706c8b667SHu Ziji 		xenon_emmc_phy_slow_mode(host, timing);
60806c8b667SHu Ziji 		goto phy_init;
60906c8b667SHu Ziji 	}
61006c8b667SHu Ziji 
61106c8b667SHu Ziji 	/*
61206c8b667SHu Ziji 	 * If SDIO card, set SDIO Mode
61306c8b667SHu Ziji 	 * Otherwise, clear SDIO Mode
61406c8b667SHu Ziji 	 */
61506c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->timing_adj);
61606c8b667SHu Ziji 	if (priv->init_card_type == MMC_TYPE_SDIO)
61706c8b667SHu Ziji 		reg |= XENON_TIMING_ADJUST_SDIO_MODE;
61806c8b667SHu Ziji 	else
61906c8b667SHu Ziji 		reg &= ~XENON_TIMING_ADJUST_SDIO_MODE;
62006c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->timing_adj);
62106c8b667SHu Ziji 
62206c8b667SHu Ziji 	if (xenon_emmc_phy_slow_mode(host, timing))
62306c8b667SHu Ziji 		goto phy_init;
62406c8b667SHu Ziji 
62506c8b667SHu Ziji 	/*
62606c8b667SHu Ziji 	 * Set preferred ZNR and ZPR value
62706c8b667SHu Ziji 	 * The ZNR and ZPR value vary between different boards.
62806c8b667SHu Ziji 	 * Define them both in sdhci-xenon-emmc-phy.h.
62906c8b667SHu Ziji 	 */
63006c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->pad_ctrl2);
63106c8b667SHu Ziji 	reg &= ~((XENON_ZNR_MASK << XENON_ZNR_SHIFT) | XENON_ZPR_MASK);
63206c8b667SHu Ziji 	reg |= ((params->znr << XENON_ZNR_SHIFT) | params->zpr);
63306c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->pad_ctrl2);
63406c8b667SHu Ziji 
63506c8b667SHu Ziji 	/*
63606c8b667SHu Ziji 	 * When setting EMMC_PHY_FUNC_CONTROL register,
63706c8b667SHu Ziji 	 * SD clock should be disabled
63806c8b667SHu Ziji 	 */
63906c8b667SHu Ziji 	reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
64006c8b667SHu Ziji 	reg &= ~SDHCI_CLOCK_CARD_EN;
64106c8b667SHu Ziji 	sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL);
64206c8b667SHu Ziji 
64306c8b667SHu Ziji 	reg = sdhci_readl(host, phy_regs->func_ctrl);
64406c8b667SHu Ziji 	switch (timing) {
64506c8b667SHu Ziji 	case MMC_TIMING_MMC_HS400:
64606c8b667SHu Ziji 		reg |= (XENON_DQ_DDR_MODE_MASK << XENON_DQ_DDR_MODE_SHIFT) |
64706c8b667SHu Ziji 		       XENON_CMD_DDR_MODE;
64806c8b667SHu Ziji 		reg &= ~XENON_DQ_ASYNC_MODE;
64906c8b667SHu Ziji 		break;
65006c8b667SHu Ziji 	case MMC_TIMING_UHS_DDR50:
65106c8b667SHu Ziji 	case MMC_TIMING_MMC_DDR52:
65206c8b667SHu Ziji 		reg |= (XENON_DQ_DDR_MODE_MASK << XENON_DQ_DDR_MODE_SHIFT) |
65306c8b667SHu Ziji 		       XENON_CMD_DDR_MODE | XENON_DQ_ASYNC_MODE;
65406c8b667SHu Ziji 		break;
65506c8b667SHu Ziji 	default:
65606c8b667SHu Ziji 		reg &= ~((XENON_DQ_DDR_MODE_MASK << XENON_DQ_DDR_MODE_SHIFT) |
65706c8b667SHu Ziji 			 XENON_CMD_DDR_MODE);
65806c8b667SHu Ziji 		reg |= XENON_DQ_ASYNC_MODE;
65906c8b667SHu Ziji 	}
66006c8b667SHu Ziji 	sdhci_writel(host, reg, phy_regs->func_ctrl);
66106c8b667SHu Ziji 
66206c8b667SHu Ziji 	/* Enable bus clock */
66306c8b667SHu Ziji 	reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
66406c8b667SHu Ziji 	reg |= SDHCI_CLOCK_CARD_EN;
66506c8b667SHu Ziji 	sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL);
66606c8b667SHu Ziji 
66706c8b667SHu Ziji 	if (timing == MMC_TIMING_MMC_HS400)
66806c8b667SHu Ziji 		/* Hardware team recommend a value for HS400 */
669a04b9b47SHu Ziji 		sdhci_writel(host, phy_regs->logic_timing_val,
67006c8b667SHu Ziji 			     phy_regs->logic_timing_adj);
67106c8b667SHu Ziji 	else
672aab6e25aSHu Ziji 		xenon_emmc_phy_disable_strobe(host);
67306c8b667SHu Ziji 
67406c8b667SHu Ziji phy_init:
67506c8b667SHu Ziji 	xenon_emmc_phy_init(host);
67606c8b667SHu Ziji 
67706c8b667SHu Ziji 	dev_dbg(mmc_dev(host->mmc), "eMMC PHY setting completes\n");
67806c8b667SHu Ziji }
67906c8b667SHu Ziji 
get_dt_pad_ctrl_data(struct sdhci_host * host,struct device_node * np,struct xenon_emmc_phy_params * params)680298269c6SHu Ziji static int get_dt_pad_ctrl_data(struct sdhci_host *host,
681298269c6SHu Ziji 				struct device_node *np,
682298269c6SHu Ziji 				struct xenon_emmc_phy_params *params)
683298269c6SHu Ziji {
684f75fda37SMarcin Wojtas 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
685f75fda37SMarcin Wojtas 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
686298269c6SHu Ziji 	int ret = 0;
687298269c6SHu Ziji 	const char *name;
688298269c6SHu Ziji 	struct resource iomem;
689298269c6SHu Ziji 
690f75fda37SMarcin Wojtas 	if (priv->hw_version == XENON_A3700)
691298269c6SHu Ziji 		params->pad_ctrl.set_soc_pad = armada_3700_soc_pad_voltage_set;
692298269c6SHu Ziji 	else
693298269c6SHu Ziji 		return 0;
694298269c6SHu Ziji 
695298269c6SHu Ziji 	if (of_address_to_resource(np, 1, &iomem)) {
6961ff537bdSRob Herring 		dev_err(mmc_dev(host->mmc), "Unable to find SoC PAD ctrl register address for %pOFn\n",
6971ff537bdSRob Herring 			np);
698298269c6SHu Ziji 		return -EINVAL;
699298269c6SHu Ziji 	}
700298269c6SHu Ziji 
701298269c6SHu Ziji 	params->pad_ctrl.reg = devm_ioremap_resource(mmc_dev(host->mmc),
702298269c6SHu Ziji 						     &iomem);
703e6e267b0SWei Yongjun 	if (IS_ERR(params->pad_ctrl.reg))
704298269c6SHu Ziji 		return PTR_ERR(params->pad_ctrl.reg);
705298269c6SHu Ziji 
706298269c6SHu Ziji 	ret = of_property_read_string(np, "marvell,pad-type", &name);
707298269c6SHu Ziji 	if (ret) {
708298269c6SHu Ziji 		dev_err(mmc_dev(host->mmc), "Unable to determine SoC PHY PAD ctrl type\n");
709298269c6SHu Ziji 		return ret;
710298269c6SHu Ziji 	}
711298269c6SHu Ziji 	if (!strcmp(name, "sd")) {
712298269c6SHu Ziji 		params->pad_ctrl.pad_type = SOC_PAD_SD;
713298269c6SHu Ziji 	} else if (!strcmp(name, "fixed-1-8v")) {
714298269c6SHu Ziji 		params->pad_ctrl.pad_type = SOC_PAD_FIXED_1_8V;
715298269c6SHu Ziji 	} else {
716298269c6SHu Ziji 		dev_err(mmc_dev(host->mmc), "Unsupported SoC PHY PAD ctrl type %s\n",
717298269c6SHu Ziji 			name);
718298269c6SHu Ziji 		return -EINVAL;
719298269c6SHu Ziji 	}
720298269c6SHu Ziji 
721298269c6SHu Ziji 	return ret;
722298269c6SHu Ziji }
723298269c6SHu Ziji 
xenon_emmc_phy_parse_params(struct sdhci_host * host,struct device * dev,struct xenon_emmc_phy_params * params)724f29bf660SMarcin Wojtas static int xenon_emmc_phy_parse_params(struct sdhci_host *host,
725f29bf660SMarcin Wojtas 				       struct device *dev,
72606c8b667SHu Ziji 				       struct xenon_emmc_phy_params *params)
72706c8b667SHu Ziji {
72806c8b667SHu Ziji 	u32 value;
72906c8b667SHu Ziji 
73006c8b667SHu Ziji 	params->slow_mode = false;
731f29bf660SMarcin Wojtas 	if (device_property_read_bool(dev, "marvell,xenon-phy-slow-mode"))
73206c8b667SHu Ziji 		params->slow_mode = true;
73306c8b667SHu Ziji 
73406c8b667SHu Ziji 	params->znr = XENON_ZNR_DEF_VALUE;
735f29bf660SMarcin Wojtas 	if (!device_property_read_u32(dev, "marvell,xenon-phy-znr", &value))
73606c8b667SHu Ziji 		params->znr = value & XENON_ZNR_MASK;
73706c8b667SHu Ziji 
73806c8b667SHu Ziji 	params->zpr = XENON_ZPR_DEF_VALUE;
739f29bf660SMarcin Wojtas 	if (!device_property_read_u32(dev, "marvell,xenon-phy-zpr", &value))
74006c8b667SHu Ziji 		params->zpr = value & XENON_ZPR_MASK;
74106c8b667SHu Ziji 
74206c8b667SHu Ziji 	params->nr_tun_times = XENON_TUN_CONSECUTIVE_TIMES;
743f29bf660SMarcin Wojtas 	if (!device_property_read_u32(dev, "marvell,xenon-phy-nr-success-tun",
74406c8b667SHu Ziji 				      &value))
74506c8b667SHu Ziji 		params->nr_tun_times = value & XENON_TUN_CONSECUTIVE_TIMES_MASK;
74606c8b667SHu Ziji 
74706c8b667SHu Ziji 	params->tun_step_divider = XENON_TUNING_STEP_DIVIDER;
748f29bf660SMarcin Wojtas 	if (!device_property_read_u32(dev, "marvell,xenon-phy-tun-step-divider",
74906c8b667SHu Ziji 				      &value))
75006c8b667SHu Ziji 		params->tun_step_divider = value & 0xFF;
75106c8b667SHu Ziji 
752f29bf660SMarcin Wojtas 	if (dev->of_node)
753f29bf660SMarcin Wojtas 		return get_dt_pad_ctrl_data(host, dev->of_node, params);
754f29bf660SMarcin Wojtas 	return 0;
755298269c6SHu Ziji }
756298269c6SHu Ziji 
757298269c6SHu Ziji /* Set SoC PHY Voltage PAD */
xenon_soc_pad_ctrl(struct sdhci_host * host,unsigned char signal_voltage)758298269c6SHu Ziji void xenon_soc_pad_ctrl(struct sdhci_host *host,
759298269c6SHu Ziji 			unsigned char signal_voltage)
760298269c6SHu Ziji {
761298269c6SHu Ziji 	xenon_emmc_phy_set_soc_pad(host, signal_voltage);
76206c8b667SHu Ziji }
76306c8b667SHu Ziji 
76406c8b667SHu Ziji /*
76506c8b667SHu Ziji  * Setting PHY when card is working in High Speed Mode.
766aab6e25aSHu Ziji  * HS400 set Data Strobe and Enhanced Strobe if it is supported.
76706c8b667SHu Ziji  * HS200/SDR104 set tuning config to prepare for tuning.
76806c8b667SHu Ziji  */
xenon_hs_delay_adj(struct sdhci_host * host)76906c8b667SHu Ziji static int xenon_hs_delay_adj(struct sdhci_host *host)
77006c8b667SHu Ziji {
77106c8b667SHu Ziji 	int ret = 0;
77206c8b667SHu Ziji 
77306c8b667SHu Ziji 	if (WARN_ON(host->clock <= XENON_DEFAULT_SDCLK_FREQ))
77406c8b667SHu Ziji 		return -EINVAL;
77506c8b667SHu Ziji 
77606c8b667SHu Ziji 	switch (host->timing) {
77706c8b667SHu Ziji 	case MMC_TIMING_MMC_HS400:
77806c8b667SHu Ziji 		xenon_emmc_phy_strobe_delay_adj(host);
77906c8b667SHu Ziji 		return 0;
78006c8b667SHu Ziji 	case MMC_TIMING_MMC_HS200:
78106c8b667SHu Ziji 	case MMC_TIMING_UHS_SDR104:
78206c8b667SHu Ziji 		return xenon_emmc_phy_config_tuning(host);
78306c8b667SHu Ziji 	case MMC_TIMING_MMC_DDR52:
78406c8b667SHu Ziji 	case MMC_TIMING_UHS_DDR50:
78506c8b667SHu Ziji 		/*
78606c8b667SHu Ziji 		 * DDR Mode requires driver to scan Sampling Fixed Delay Line,
78706c8b667SHu Ziji 		 * to find out a perfect operation sampling point.
78806c8b667SHu Ziji 		 * It is hard to implement such a scan in host driver
78906c8b667SHu Ziji 		 * since initiating commands by host driver is not safe.
79006c8b667SHu Ziji 		 * Thus so far just keep PHY Sampling Fixed Delay in
79106c8b667SHu Ziji 		 * default value of DDR mode.
79206c8b667SHu Ziji 		 *
79306c8b667SHu Ziji 		 * If any timing issue occurs in DDR mode on Marvell products,
79406c8b667SHu Ziji 		 * please contact maintainer for internal support in Marvell.
79506c8b667SHu Ziji 		 */
79606c8b667SHu Ziji 		dev_warn_once(mmc_dev(host->mmc), "Timing issue might occur in DDR mode\n");
79706c8b667SHu Ziji 		return 0;
79806c8b667SHu Ziji 	}
79906c8b667SHu Ziji 
80006c8b667SHu Ziji 	return ret;
80106c8b667SHu Ziji }
80206c8b667SHu Ziji 
80306c8b667SHu Ziji /*
80406c8b667SHu Ziji  * Adjust PHY setting.
80506c8b667SHu Ziji  * PHY setting should be adjusted when SDCLK frequency, Bus Width
80606c8b667SHu Ziji  * or Speed Mode is changed.
80706c8b667SHu Ziji  * Additional config are required when card is working in High Speed mode,
80806c8b667SHu Ziji  * after leaving Legacy Mode.
80906c8b667SHu Ziji  */
xenon_phy_adj(struct sdhci_host * host,struct mmc_ios * ios)81006c8b667SHu Ziji int xenon_phy_adj(struct sdhci_host *host, struct mmc_ios *ios)
81106c8b667SHu Ziji {
81206c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
81306c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
81406c8b667SHu Ziji 	int ret = 0;
81506c8b667SHu Ziji 
81606c8b667SHu Ziji 	if (!host->clock) {
81706c8b667SHu Ziji 		priv->clock = 0;
81806c8b667SHu Ziji 		return 0;
81906c8b667SHu Ziji 	}
82006c8b667SHu Ziji 
82106c8b667SHu Ziji 	/*
82206c8b667SHu Ziji 	 * The timing, frequency or bus width is changed,
82306c8b667SHu Ziji 	 * better to set eMMC PHY based on current setting
82406c8b667SHu Ziji 	 * and adjust Xenon SDHC delay.
82506c8b667SHu Ziji 	 */
82606c8b667SHu Ziji 	if ((host->clock == priv->clock) &&
82706c8b667SHu Ziji 	    (ios->bus_width == priv->bus_width) &&
82806c8b667SHu Ziji 	    (ios->timing == priv->timing))
82906c8b667SHu Ziji 		return 0;
83006c8b667SHu Ziji 
83106c8b667SHu Ziji 	xenon_emmc_phy_set(host, ios->timing);
83206c8b667SHu Ziji 
83306c8b667SHu Ziji 	/* Update the record */
83406c8b667SHu Ziji 	priv->bus_width = ios->bus_width;
83506c8b667SHu Ziji 
83606c8b667SHu Ziji 	priv->timing = ios->timing;
83706c8b667SHu Ziji 	priv->clock = host->clock;
83806c8b667SHu Ziji 
83906c8b667SHu Ziji 	/* Legacy mode is a special case */
84006c8b667SHu Ziji 	if (ios->timing == MMC_TIMING_LEGACY)
84106c8b667SHu Ziji 		return 0;
84206c8b667SHu Ziji 
84306c8b667SHu Ziji 	if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
84406c8b667SHu Ziji 		ret = xenon_hs_delay_adj(host);
84506c8b667SHu Ziji 	return ret;
84606c8b667SHu Ziji }
84706c8b667SHu Ziji 
xenon_add_phy(struct device * dev,struct sdhci_host * host,const char * phy_name)848f29bf660SMarcin Wojtas static int xenon_add_phy(struct device *dev, struct sdhci_host *host,
84906c8b667SHu Ziji 			 const char *phy_name)
85006c8b667SHu Ziji {
85106c8b667SHu Ziji 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
85206c8b667SHu Ziji 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
853e1cb88adSXie Yisheng 	int ret;
85406c8b667SHu Ziji 
855e1cb88adSXie Yisheng 	priv->phy_type = match_string(phy_types, NR_PHY_TYPES, phy_name);
856e1cb88adSXie Yisheng 	if (priv->phy_type < 0) {
85706c8b667SHu Ziji 		dev_err(mmc_dev(host->mmc),
85806c8b667SHu Ziji 			"Unable to determine PHY name %s. Use default eMMC 5.1 PHY\n",
85906c8b667SHu Ziji 			phy_name);
86006c8b667SHu Ziji 		priv->phy_type = EMMC_5_1_PHY;
86106c8b667SHu Ziji 	}
86206c8b667SHu Ziji 
86306c8b667SHu Ziji 	ret = xenon_alloc_emmc_phy(host);
86406c8b667SHu Ziji 	if (ret)
86506c8b667SHu Ziji 		return ret;
86606c8b667SHu Ziji 
867f29bf660SMarcin Wojtas 	return xenon_emmc_phy_parse_params(host, dev, priv->phy_params);
86806c8b667SHu Ziji }
86906c8b667SHu Ziji 
xenon_phy_parse_params(struct device * dev,struct sdhci_host * host)870f29bf660SMarcin Wojtas int xenon_phy_parse_params(struct device *dev, struct sdhci_host *host)
87106c8b667SHu Ziji {
87206c8b667SHu Ziji 	const char *phy_type = NULL;
87306c8b667SHu Ziji 
874f29bf660SMarcin Wojtas 	if (!device_property_read_string(dev, "marvell,xenon-phy-type", &phy_type))
875f29bf660SMarcin Wojtas 		return xenon_add_phy(dev, host, phy_type);
87606c8b667SHu Ziji 
877f29bf660SMarcin Wojtas 	return xenon_add_phy(dev, host, "emmc 5.1 phy");
87806c8b667SHu Ziji }
879