18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Broadcom SATA3 AHCI Controller PHY Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Broadcom
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/delay.h>
98c2ecf20Sopenharmony_ci#include <linux/device.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define SATA_PCB_BANK_OFFSET				0x23c
208c2ecf20Sopenharmony_ci#define SATA_PCB_REG_OFFSET(ofs)			((ofs) * 4)
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define MAX_PORTS					2
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Register offset between PHYs in PCB space */
258c2ecf20Sopenharmony_ci#define SATA_PCB_REG_28NM_SPACE_SIZE			0x1000
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* The older SATA PHY registers duplicated per port registers within the map,
288c2ecf20Sopenharmony_ci * rather than having a separate map per port.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#define SATA_PCB_REG_40NM_SPACE_SIZE			0x10
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Register offset between PHYs in PHY control space */
338c2ecf20Sopenharmony_ci#define SATA_PHY_CTRL_REG_28NM_SPACE_SIZE		0x8
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cienum brcm_sata_phy_version {
368c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_STB_16NM,
378c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_STB_28NM,
388c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_STB_40NM,
398c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_IPROC_NS2,
408c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_IPROC_NSP,
418c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_IPROC_SR,
428c2ecf20Sopenharmony_ci	BRCM_SATA_PHY_DSL_28NM,
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cienum brcm_sata_phy_rxaeq_mode {
468c2ecf20Sopenharmony_ci	RXAEQ_MODE_OFF = 0,
478c2ecf20Sopenharmony_ci	RXAEQ_MODE_AUTO,
488c2ecf20Sopenharmony_ci	RXAEQ_MODE_MANUAL,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic enum brcm_sata_phy_rxaeq_mode rxaeq_to_val(const char *m)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	if (!strcmp(m, "auto"))
548c2ecf20Sopenharmony_ci		return RXAEQ_MODE_AUTO;
558c2ecf20Sopenharmony_ci	else if (!strcmp(m, "manual"))
568c2ecf20Sopenharmony_ci		return RXAEQ_MODE_MANUAL;
578c2ecf20Sopenharmony_ci	else
588c2ecf20Sopenharmony_ci		return RXAEQ_MODE_OFF;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistruct brcm_sata_port {
628c2ecf20Sopenharmony_ci	int portnum;
638c2ecf20Sopenharmony_ci	struct phy *phy;
648c2ecf20Sopenharmony_ci	struct brcm_sata_phy *phy_priv;
658c2ecf20Sopenharmony_ci	bool ssc_en;
668c2ecf20Sopenharmony_ci	enum brcm_sata_phy_rxaeq_mode rxaeq_mode;
678c2ecf20Sopenharmony_ci	u32 rxaeq_val;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct brcm_sata_phy {
718c2ecf20Sopenharmony_ci	struct device *dev;
728c2ecf20Sopenharmony_ci	void __iomem *phy_base;
738c2ecf20Sopenharmony_ci	void __iomem *ctrl_base;
748c2ecf20Sopenharmony_ci	enum brcm_sata_phy_version version;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	struct brcm_sata_port phys[MAX_PORTS];
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cienum sata_phy_regs {
808c2ecf20Sopenharmony_ci	BLOCK0_REG_BANK				= 0x000,
818c2ecf20Sopenharmony_ci	BLOCK0_XGXSSTATUS			= 0x81,
828c2ecf20Sopenharmony_ci	BLOCK0_XGXSSTATUS_PLL_LOCK		= BIT(12),
838c2ecf20Sopenharmony_ci	BLOCK0_SPARE				= 0x8d,
848c2ecf20Sopenharmony_ci	BLOCK0_SPARE_OOB_CLK_SEL_MASK		= 0x3,
858c2ecf20Sopenharmony_ci	BLOCK0_SPARE_OOB_CLK_SEL_REFBY2		= 0x1,
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	PLL_REG_BANK_0				= 0x050,
888c2ecf20Sopenharmony_ci	PLL_REG_BANK_0_PLLCONTROL_0		= 0x81,
898c2ecf20Sopenharmony_ci	PLLCONTROL_0_FREQ_DET_RESTART		= BIT(13),
908c2ecf20Sopenharmony_ci	PLLCONTROL_0_FREQ_MONITOR		= BIT(12),
918c2ecf20Sopenharmony_ci	PLLCONTROL_0_SEQ_START			= BIT(15),
928c2ecf20Sopenharmony_ci	PLL_CAP_CHARGE_TIME			= 0x83,
938c2ecf20Sopenharmony_ci	PLL_VCO_CAL_THRESH			= 0x84,
948c2ecf20Sopenharmony_ci	PLL_CAP_CONTROL				= 0x85,
958c2ecf20Sopenharmony_ci	PLL_FREQ_DET_TIME			= 0x86,
968c2ecf20Sopenharmony_ci	PLL_ACTRL2				= 0x8b,
978c2ecf20Sopenharmony_ci	PLL_ACTRL2_SELDIV_MASK			= 0x1f,
988c2ecf20Sopenharmony_ci	PLL_ACTRL2_SELDIV_SHIFT			= 9,
998c2ecf20Sopenharmony_ci	PLL_ACTRL6				= 0x86,
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	PLL1_REG_BANK				= 0x060,
1028c2ecf20Sopenharmony_ci	PLL1_ACTRL2				= 0x82,
1038c2ecf20Sopenharmony_ci	PLL1_ACTRL3				= 0x83,
1048c2ecf20Sopenharmony_ci	PLL1_ACTRL4				= 0x84,
1058c2ecf20Sopenharmony_ci	PLL1_ACTRL5				= 0x85,
1068c2ecf20Sopenharmony_ci	PLL1_ACTRL6				= 0x86,
1078c2ecf20Sopenharmony_ci	PLL1_ACTRL7				= 0x87,
1088c2ecf20Sopenharmony_ci	PLL1_ACTRL8				= 0x88,
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	TX_REG_BANK				= 0x070,
1118c2ecf20Sopenharmony_ci	TX_ACTRL0				= 0x80,
1128c2ecf20Sopenharmony_ci	TX_ACTRL0_TXPOL_FLIP			= BIT(6),
1138c2ecf20Sopenharmony_ci	TX_ACTRL5				= 0x85,
1148c2ecf20Sopenharmony_ci	TX_ACTRL5_SSC_EN			= BIT(11),
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	AEQRX_REG_BANK_0			= 0xd0,
1178c2ecf20Sopenharmony_ci	AEQ_CONTROL1				= 0x81,
1188c2ecf20Sopenharmony_ci	AEQ_CONTROL1_ENABLE			= BIT(2),
1198c2ecf20Sopenharmony_ci	AEQ_CONTROL1_FREEZE			= BIT(3),
1208c2ecf20Sopenharmony_ci	AEQ_FRC_EQ				= 0x83,
1218c2ecf20Sopenharmony_ci	AEQ_FRC_EQ_FORCE			= BIT(0),
1228c2ecf20Sopenharmony_ci	AEQ_FRC_EQ_FORCE_VAL			= BIT(1),
1238c2ecf20Sopenharmony_ci	AEQ_RFZ_FRC_VAL				= BIT(8),
1248c2ecf20Sopenharmony_ci	AEQRX_REG_BANK_1			= 0xe0,
1258c2ecf20Sopenharmony_ci	AEQRX_SLCAL0_CTRL0			= 0x82,
1268c2ecf20Sopenharmony_ci	AEQRX_SLCAL1_CTRL0			= 0x86,
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	OOB_REG_BANK				= 0x150,
1298c2ecf20Sopenharmony_ci	OOB1_REG_BANK				= 0x160,
1308c2ecf20Sopenharmony_ci	OOB_CTRL1				= 0x80,
1318c2ecf20Sopenharmony_ci	OOB_CTRL1_BURST_MAX_MASK		= 0xf,
1328c2ecf20Sopenharmony_ci	OOB_CTRL1_BURST_MAX_SHIFT		= 12,
1338c2ecf20Sopenharmony_ci	OOB_CTRL1_BURST_MIN_MASK		= 0xf,
1348c2ecf20Sopenharmony_ci	OOB_CTRL1_BURST_MIN_SHIFT		= 8,
1358c2ecf20Sopenharmony_ci	OOB_CTRL1_WAKE_IDLE_MAX_MASK		= 0xf,
1368c2ecf20Sopenharmony_ci	OOB_CTRL1_WAKE_IDLE_MAX_SHIFT		= 4,
1378c2ecf20Sopenharmony_ci	OOB_CTRL1_WAKE_IDLE_MIN_MASK		= 0xf,
1388c2ecf20Sopenharmony_ci	OOB_CTRL1_WAKE_IDLE_MIN_SHIFT		= 0,
1398c2ecf20Sopenharmony_ci	OOB_CTRL2				= 0x81,
1408c2ecf20Sopenharmony_ci	OOB_CTRL2_SEL_ENA_SHIFT			= 15,
1418c2ecf20Sopenharmony_ci	OOB_CTRL2_SEL_ENA_RC_SHIFT		= 14,
1428c2ecf20Sopenharmony_ci	OOB_CTRL2_RESET_IDLE_MAX_MASK		= 0x3f,
1438c2ecf20Sopenharmony_ci	OOB_CTRL2_RESET_IDLE_MAX_SHIFT		= 8,
1448c2ecf20Sopenharmony_ci	OOB_CTRL2_BURST_CNT_MASK		= 0x3,
1458c2ecf20Sopenharmony_ci	OOB_CTRL2_BURST_CNT_SHIFT		= 6,
1468c2ecf20Sopenharmony_ci	OOB_CTRL2_RESET_IDLE_MIN_MASK		= 0x3f,
1478c2ecf20Sopenharmony_ci	OOB_CTRL2_RESET_IDLE_MIN_SHIFT		= 0,
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	TXPMD_REG_BANK				= 0x1a0,
1508c2ecf20Sopenharmony_ci	TXPMD_CONTROL1				= 0x81,
1518c2ecf20Sopenharmony_ci	TXPMD_CONTROL1_TX_SSC_EN_FRC		= BIT(0),
1528c2ecf20Sopenharmony_ci	TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL	= BIT(1),
1538c2ecf20Sopenharmony_ci	TXPMD_TX_FREQ_CTRL_CONTROL1		= 0x82,
1548c2ecf20Sopenharmony_ci	TXPMD_TX_FREQ_CTRL_CONTROL2		= 0x83,
1558c2ecf20Sopenharmony_ci	TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK	= 0x3ff,
1568c2ecf20Sopenharmony_ci	TXPMD_TX_FREQ_CTRL_CONTROL3		= 0x84,
1578c2ecf20Sopenharmony_ci	TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK	= 0x3ff,
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	RXPMD_REG_BANK				= 0x1c0,
1608c2ecf20Sopenharmony_ci	RXPMD_RX_CDR_CONTROL1			= 0x81,
1618c2ecf20Sopenharmony_ci	RXPMD_RX_PPM_VAL_MASK			= 0x1ff,
1628c2ecf20Sopenharmony_ci	RXPMD_RXPMD_EN_FRC			= BIT(12),
1638c2ecf20Sopenharmony_ci	RXPMD_RXPMD_EN_FRC_VAL			= BIT(13),
1648c2ecf20Sopenharmony_ci	RXPMD_RX_CDR_CDR_PROP_BW		= 0x82,
1658c2ecf20Sopenharmony_ci	RXPMD_G_CDR_PROP_BW_MASK		= 0x7,
1668c2ecf20Sopenharmony_ci	RXPMD_G1_CDR_PROP_BW_SHIFT		= 0,
1678c2ecf20Sopenharmony_ci	RXPMD_G2_CDR_PROP_BW_SHIFT		= 3,
1688c2ecf20Sopenharmony_ci	RXPMD_G3_CDR_PROB_BW_SHIFT		= 6,
1698c2ecf20Sopenharmony_ci	RXPMD_RX_CDR_CDR_ACQ_INTEG_BW		= 0x83,
1708c2ecf20Sopenharmony_ci	RXPMD_G_CDR_ACQ_INT_BW_MASK		= 0x7,
1718c2ecf20Sopenharmony_ci	RXPMD_G1_CDR_ACQ_INT_BW_SHIFT		= 0,
1728c2ecf20Sopenharmony_ci	RXPMD_G2_CDR_ACQ_INT_BW_SHIFT		= 3,
1738c2ecf20Sopenharmony_ci	RXPMD_G3_CDR_ACQ_INT_BW_SHIFT		= 6,
1748c2ecf20Sopenharmony_ci	RXPMD_RX_CDR_CDR_LOCK_INTEG_BW		= 0x84,
1758c2ecf20Sopenharmony_ci	RXPMD_G_CDR_LOCK_INT_BW_MASK		= 0x7,
1768c2ecf20Sopenharmony_ci	RXPMD_G1_CDR_LOCK_INT_BW_SHIFT		= 0,
1778c2ecf20Sopenharmony_ci	RXPMD_G2_CDR_LOCK_INT_BW_SHIFT		= 3,
1788c2ecf20Sopenharmony_ci	RXPMD_G3_CDR_LOCK_INT_BW_SHIFT		= 6,
1798c2ecf20Sopenharmony_ci	RXPMD_RX_FREQ_MON_CONTROL1		= 0x87,
1808c2ecf20Sopenharmony_ci	RXPMD_MON_CORRECT_EN			= BIT(8),
1818c2ecf20Sopenharmony_ci	RXPMD_MON_MARGIN_VAL_MASK		= 0xff,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cienum sata_phy_ctrl_regs {
1858c2ecf20Sopenharmony_ci	PHY_CTRL_1				= 0x0,
1868c2ecf20Sopenharmony_ci	PHY_CTRL_1_RESET			= BIT(0),
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic inline void __iomem *brcm_sata_ctrl_base(struct brcm_sata_port *port)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct brcm_sata_phy *priv = port->phy_priv;
1928c2ecf20Sopenharmony_ci	u32 size = 0;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	switch (priv->version) {
1958c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_IPROC_NS2:
1968c2ecf20Sopenharmony_ci		size = SATA_PHY_CTRL_REG_28NM_SPACE_SIZE;
1978c2ecf20Sopenharmony_ci		break;
1988c2ecf20Sopenharmony_ci	default:
1998c2ecf20Sopenharmony_ci		dev_err(priv->dev, "invalid phy version\n");
2008c2ecf20Sopenharmony_ci		break;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return priv->ctrl_base + (port->portnum * size);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic void brcm_sata_phy_wr(struct brcm_sata_port *port, u32 bank,
2078c2ecf20Sopenharmony_ci			     u32 ofs, u32 msk, u32 value)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct brcm_sata_phy *priv = port->phy_priv;
2108c2ecf20Sopenharmony_ci	void __iomem *pcb_base = priv->phy_base;
2118c2ecf20Sopenharmony_ci	u32 tmp;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (priv->version == BRCM_SATA_PHY_STB_40NM)
2148c2ecf20Sopenharmony_ci		bank += (port->portnum * SATA_PCB_REG_40NM_SPACE_SIZE);
2158c2ecf20Sopenharmony_ci	else
2168c2ecf20Sopenharmony_ci		pcb_base += (port->portnum * SATA_PCB_REG_28NM_SPACE_SIZE);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	writel(bank, pcb_base + SATA_PCB_BANK_OFFSET);
2198c2ecf20Sopenharmony_ci	tmp = readl(pcb_base + SATA_PCB_REG_OFFSET(ofs));
2208c2ecf20Sopenharmony_ci	tmp = (tmp & msk) | value;
2218c2ecf20Sopenharmony_ci	writel(tmp, pcb_base + SATA_PCB_REG_OFFSET(ofs));
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic u32 brcm_sata_phy_rd(struct brcm_sata_port *port, u32 bank, u32 ofs)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	struct brcm_sata_phy *priv = port->phy_priv;
2278c2ecf20Sopenharmony_ci	void __iomem *pcb_base = priv->phy_base;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (priv->version == BRCM_SATA_PHY_STB_40NM)
2308c2ecf20Sopenharmony_ci		bank += (port->portnum * SATA_PCB_REG_40NM_SPACE_SIZE);
2318c2ecf20Sopenharmony_ci	else
2328c2ecf20Sopenharmony_ci		pcb_base += (port->portnum * SATA_PCB_REG_28NM_SPACE_SIZE);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	writel(bank, pcb_base + SATA_PCB_BANK_OFFSET);
2358c2ecf20Sopenharmony_ci	return readl(pcb_base + SATA_PCB_REG_OFFSET(ofs));
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/* These defaults were characterized by H/W group */
2398c2ecf20Sopenharmony_ci#define STB_FMIN_VAL_DEFAULT	0x3df
2408c2ecf20Sopenharmony_ci#define STB_FMAX_VAL_DEFAULT	0x3df
2418c2ecf20Sopenharmony_ci#define STB_FMAX_VAL_SSC	0x83
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic void brcm_stb_sata_ssc_init(struct brcm_sata_port *port)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	struct brcm_sata_phy *priv = port->phy_priv;
2468c2ecf20Sopenharmony_ci	u32 tmp;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	/* override the TX spread spectrum setting */
2498c2ecf20Sopenharmony_ci	tmp = TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL | TXPMD_CONTROL1_TX_SSC_EN_FRC;
2508c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, TXPMD_REG_BANK, TXPMD_CONTROL1, ~tmp, tmp);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	/* set fixed min freq */
2538c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL2,
2548c2ecf20Sopenharmony_ci			 ~TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK,
2558c2ecf20Sopenharmony_ci			 STB_FMIN_VAL_DEFAULT);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/* set fixed max freq depending on SSC config */
2588c2ecf20Sopenharmony_ci	if (port->ssc_en) {
2598c2ecf20Sopenharmony_ci		dev_info(priv->dev, "enabling SSC on port%d\n", port->portnum);
2608c2ecf20Sopenharmony_ci		tmp = STB_FMAX_VAL_SSC;
2618c2ecf20Sopenharmony_ci	} else {
2628c2ecf20Sopenharmony_ci		tmp = STB_FMAX_VAL_DEFAULT;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL3,
2668c2ecf20Sopenharmony_ci			  ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK, tmp);
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci#define AEQ_FRC_EQ_VAL_SHIFT	2
2708c2ecf20Sopenharmony_ci#define AEQ_FRC_EQ_VAL_MASK	0x3f
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic int brcm_stb_sata_rxaeq_init(struct brcm_sata_port *port)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	u32 tmp = 0, reg = 0;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	switch (port->rxaeq_mode) {
2778c2ecf20Sopenharmony_ci	case RXAEQ_MODE_OFF:
2788c2ecf20Sopenharmony_ci		return 0;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	case RXAEQ_MODE_AUTO:
2818c2ecf20Sopenharmony_ci		reg = AEQ_CONTROL1;
2828c2ecf20Sopenharmony_ci		tmp = AEQ_CONTROL1_ENABLE | AEQ_CONTROL1_FREEZE;
2838c2ecf20Sopenharmony_ci		break;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	case RXAEQ_MODE_MANUAL:
2868c2ecf20Sopenharmony_ci		reg = AEQ_FRC_EQ;
2878c2ecf20Sopenharmony_ci		tmp = AEQ_FRC_EQ_FORCE | AEQ_FRC_EQ_FORCE_VAL;
2888c2ecf20Sopenharmony_ci		if (port->rxaeq_val > AEQ_FRC_EQ_VAL_MASK)
2898c2ecf20Sopenharmony_ci			return -EINVAL;
2908c2ecf20Sopenharmony_ci		tmp |= port->rxaeq_val << AEQ_FRC_EQ_VAL_SHIFT;
2918c2ecf20Sopenharmony_ci		break;
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, AEQRX_REG_BANK_0, reg, ~tmp, tmp);
2958c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, AEQRX_REG_BANK_1, reg, ~tmp, tmp);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return 0;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic int brcm_stb_sata_init(struct brcm_sata_port *port)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	brcm_stb_sata_ssc_init(port);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return brcm_stb_sata_rxaeq_init(port);
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic int brcm_stb_sata_16nm_ssc_init(struct brcm_sata_port *port)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	u32 tmp, value;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/* Reduce CP tail current to 1/16th of its default value */
3128c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL6, 0, 0x141);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* Turn off CP tail current boost */
3158c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL8, 0, 0xc006);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* Set a specific AEQ equalizer value */
3188c2ecf20Sopenharmony_ci	tmp = AEQ_FRC_EQ_FORCE_VAL | AEQ_FRC_EQ_FORCE;
3198c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, AEQRX_REG_BANK_0, AEQ_FRC_EQ,
3208c2ecf20Sopenharmony_ci			 ~(tmp | AEQ_RFZ_FRC_VAL |
3218c2ecf20Sopenharmony_ci			   AEQ_FRC_EQ_VAL_MASK << AEQ_FRC_EQ_VAL_SHIFT),
3228c2ecf20Sopenharmony_ci			 tmp | 32 << AEQ_FRC_EQ_VAL_SHIFT);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* Set RX PPM val center frequency */
3258c2ecf20Sopenharmony_ci	if (port->ssc_en)
3268c2ecf20Sopenharmony_ci		value = 0x52;
3278c2ecf20Sopenharmony_ci	else
3288c2ecf20Sopenharmony_ci		value = 0;
3298c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, RXPMD_REG_BANK, RXPMD_RX_CDR_CONTROL1,
3308c2ecf20Sopenharmony_ci			 ~RXPMD_RX_PPM_VAL_MASK, value);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* Set proportional loop bandwith Gen1/2/3 */
3338c2ecf20Sopenharmony_ci	tmp = RXPMD_G_CDR_PROP_BW_MASK << RXPMD_G1_CDR_PROP_BW_SHIFT |
3348c2ecf20Sopenharmony_ci	      RXPMD_G_CDR_PROP_BW_MASK << RXPMD_G2_CDR_PROP_BW_SHIFT |
3358c2ecf20Sopenharmony_ci	      RXPMD_G_CDR_PROP_BW_MASK << RXPMD_G3_CDR_PROB_BW_SHIFT;
3368c2ecf20Sopenharmony_ci	if (port->ssc_en)
3378c2ecf20Sopenharmony_ci		value = 2 << RXPMD_G1_CDR_PROP_BW_SHIFT |
3388c2ecf20Sopenharmony_ci			2 << RXPMD_G2_CDR_PROP_BW_SHIFT |
3398c2ecf20Sopenharmony_ci			2 << RXPMD_G3_CDR_PROB_BW_SHIFT;
3408c2ecf20Sopenharmony_ci	else
3418c2ecf20Sopenharmony_ci		value = 1 << RXPMD_G1_CDR_PROP_BW_SHIFT |
3428c2ecf20Sopenharmony_ci			1 << RXPMD_G2_CDR_PROP_BW_SHIFT |
3438c2ecf20Sopenharmony_ci			1 << RXPMD_G3_CDR_PROB_BW_SHIFT;
3448c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, RXPMD_REG_BANK, RXPMD_RX_CDR_CDR_PROP_BW, ~tmp,
3458c2ecf20Sopenharmony_ci			 value);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	/* Set CDR integral loop acquisition bandwidth for Gen1/2/3 */
3488c2ecf20Sopenharmony_ci	tmp = RXPMD_G_CDR_ACQ_INT_BW_MASK << RXPMD_G1_CDR_ACQ_INT_BW_SHIFT |
3498c2ecf20Sopenharmony_ci	      RXPMD_G_CDR_ACQ_INT_BW_MASK << RXPMD_G2_CDR_ACQ_INT_BW_SHIFT |
3508c2ecf20Sopenharmony_ci	      RXPMD_G_CDR_ACQ_INT_BW_MASK << RXPMD_G3_CDR_ACQ_INT_BW_SHIFT;
3518c2ecf20Sopenharmony_ci	if (port->ssc_en)
3528c2ecf20Sopenharmony_ci		value = 1 << RXPMD_G1_CDR_ACQ_INT_BW_SHIFT |
3538c2ecf20Sopenharmony_ci			1 << RXPMD_G2_CDR_ACQ_INT_BW_SHIFT |
3548c2ecf20Sopenharmony_ci			1 << RXPMD_G3_CDR_ACQ_INT_BW_SHIFT;
3558c2ecf20Sopenharmony_ci	else
3568c2ecf20Sopenharmony_ci		value = 0;
3578c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, RXPMD_REG_BANK, RXPMD_RX_CDR_CDR_ACQ_INTEG_BW,
3588c2ecf20Sopenharmony_ci			 ~tmp, value);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	/* Set CDR integral loop locking bandwidth to 1 for Gen 1/2/3 */
3618c2ecf20Sopenharmony_ci	tmp = RXPMD_G_CDR_LOCK_INT_BW_MASK << RXPMD_G1_CDR_LOCK_INT_BW_SHIFT |
3628c2ecf20Sopenharmony_ci	      RXPMD_G_CDR_LOCK_INT_BW_MASK << RXPMD_G2_CDR_LOCK_INT_BW_SHIFT |
3638c2ecf20Sopenharmony_ci	      RXPMD_G_CDR_LOCK_INT_BW_MASK << RXPMD_G3_CDR_LOCK_INT_BW_SHIFT;
3648c2ecf20Sopenharmony_ci	if (port->ssc_en)
3658c2ecf20Sopenharmony_ci		value = 1 << RXPMD_G1_CDR_LOCK_INT_BW_SHIFT |
3668c2ecf20Sopenharmony_ci			1 << RXPMD_G2_CDR_LOCK_INT_BW_SHIFT |
3678c2ecf20Sopenharmony_ci			1 << RXPMD_G3_CDR_LOCK_INT_BW_SHIFT;
3688c2ecf20Sopenharmony_ci	else
3698c2ecf20Sopenharmony_ci		value = 0;
3708c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, RXPMD_REG_BANK, RXPMD_RX_CDR_CDR_LOCK_INTEG_BW,
3718c2ecf20Sopenharmony_ci			 ~tmp, value);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/* Set no guard band and clamp CDR */
3748c2ecf20Sopenharmony_ci	tmp = RXPMD_MON_CORRECT_EN | RXPMD_MON_MARGIN_VAL_MASK;
3758c2ecf20Sopenharmony_ci	if (port->ssc_en)
3768c2ecf20Sopenharmony_ci		value = 0x51;
3778c2ecf20Sopenharmony_ci	else
3788c2ecf20Sopenharmony_ci		value = 0;
3798c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, RXPMD_REG_BANK, RXPMD_RX_FREQ_MON_CONTROL1,
3808c2ecf20Sopenharmony_ci			 ~tmp, RXPMD_MON_CORRECT_EN | value);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* Turn on/off SSC */
3838c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, TX_REG_BANK, TX_ACTRL5, ~TX_ACTRL5_SSC_EN,
3848c2ecf20Sopenharmony_ci			 port->ssc_en ? TX_ACTRL5_SSC_EN : 0);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	return 0;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic int brcm_stb_sata_16nm_init(struct brcm_sata_port *port)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	return brcm_stb_sata_16nm_ssc_init(port);
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/* NS2 SATA PLL1 defaults were characterized by H/W group */
3958c2ecf20Sopenharmony_ci#define NS2_PLL1_ACTRL2_MAGIC	0x1df8
3968c2ecf20Sopenharmony_ci#define NS2_PLL1_ACTRL3_MAGIC	0x2b00
3978c2ecf20Sopenharmony_ci#define NS2_PLL1_ACTRL4_MAGIC	0x8824
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic int brcm_ns2_sata_init(struct brcm_sata_port *port)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	int try;
4028c2ecf20Sopenharmony_ci	unsigned int val;
4038c2ecf20Sopenharmony_ci	void __iomem *ctrl_base = brcm_sata_ctrl_base(port);
4048c2ecf20Sopenharmony_ci	struct device *dev = port->phy_priv->dev;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	/* Configure OOB control */
4078c2ecf20Sopenharmony_ci	val = 0x0;
4088c2ecf20Sopenharmony_ci	val |= (0xc << OOB_CTRL1_BURST_MAX_SHIFT);
4098c2ecf20Sopenharmony_ci	val |= (0x4 << OOB_CTRL1_BURST_MIN_SHIFT);
4108c2ecf20Sopenharmony_ci	val |= (0x9 << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT);
4118c2ecf20Sopenharmony_ci	val |= (0x3 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT);
4128c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, OOB_REG_BANK, OOB_CTRL1, 0x0, val);
4138c2ecf20Sopenharmony_ci	val = 0x0;
4148c2ecf20Sopenharmony_ci	val |= (0x1b << OOB_CTRL2_RESET_IDLE_MAX_SHIFT);
4158c2ecf20Sopenharmony_ci	val |= (0x2 << OOB_CTRL2_BURST_CNT_SHIFT);
4168c2ecf20Sopenharmony_ci	val |= (0x9 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT);
4178c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, OOB_REG_BANK, OOB_CTRL2, 0x0, val);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/* Configure PHY PLL register bank 1 */
4208c2ecf20Sopenharmony_ci	val = NS2_PLL1_ACTRL2_MAGIC;
4218c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL2, 0x0, val);
4228c2ecf20Sopenharmony_ci	val = NS2_PLL1_ACTRL3_MAGIC;
4238c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL3, 0x0, val);
4248c2ecf20Sopenharmony_ci	val = NS2_PLL1_ACTRL4_MAGIC;
4258c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL4, 0x0, val);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* Configure PHY BLOCK0 register bank */
4288c2ecf20Sopenharmony_ci	/* Set oob_clk_sel to refclk/2 */
4298c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, BLOCK0_REG_BANK, BLOCK0_SPARE,
4308c2ecf20Sopenharmony_ci			 ~BLOCK0_SPARE_OOB_CLK_SEL_MASK,
4318c2ecf20Sopenharmony_ci			 BLOCK0_SPARE_OOB_CLK_SEL_REFBY2);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* Strobe PHY reset using PHY control register */
4348c2ecf20Sopenharmony_ci	writel(PHY_CTRL_1_RESET, ctrl_base + PHY_CTRL_1);
4358c2ecf20Sopenharmony_ci	mdelay(1);
4368c2ecf20Sopenharmony_ci	writel(0x0, ctrl_base + PHY_CTRL_1);
4378c2ecf20Sopenharmony_ci	mdelay(1);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	/* Wait for PHY PLL lock by polling pll_lock bit */
4408c2ecf20Sopenharmony_ci	try = 50;
4418c2ecf20Sopenharmony_ci	while (try) {
4428c2ecf20Sopenharmony_ci		val = brcm_sata_phy_rd(port, BLOCK0_REG_BANK,
4438c2ecf20Sopenharmony_ci					BLOCK0_XGXSSTATUS);
4448c2ecf20Sopenharmony_ci		if (val & BLOCK0_XGXSSTATUS_PLL_LOCK)
4458c2ecf20Sopenharmony_ci			break;
4468c2ecf20Sopenharmony_ci		msleep(20);
4478c2ecf20Sopenharmony_ci		try--;
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci	if (!try) {
4508c2ecf20Sopenharmony_ci		/* PLL did not lock; give up */
4518c2ecf20Sopenharmony_ci		dev_err(dev, "port%d PLL did not lock\n", port->portnum);
4528c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	dev_dbg(dev, "port%d initialized\n", port->portnum);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	return 0;
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic int brcm_nsp_sata_init(struct brcm_sata_port *port)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	struct device *dev = port->phy_priv->dev;
4638c2ecf20Sopenharmony_ci	unsigned int oob_bank;
4648c2ecf20Sopenharmony_ci	unsigned int val, try;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	/* Configure OOB control */
4678c2ecf20Sopenharmony_ci	if (port->portnum == 0)
4688c2ecf20Sopenharmony_ci		oob_bank = OOB_REG_BANK;
4698c2ecf20Sopenharmony_ci	else if (port->portnum == 1)
4708c2ecf20Sopenharmony_ci		oob_bank = OOB1_REG_BANK;
4718c2ecf20Sopenharmony_ci	else
4728c2ecf20Sopenharmony_ci		return -EINVAL;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	val = 0x0;
4758c2ecf20Sopenharmony_ci	val |= (0x0f << OOB_CTRL1_BURST_MAX_SHIFT);
4768c2ecf20Sopenharmony_ci	val |= (0x06 << OOB_CTRL1_BURST_MIN_SHIFT);
4778c2ecf20Sopenharmony_ci	val |= (0x0f << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT);
4788c2ecf20Sopenharmony_ci	val |= (0x06 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT);
4798c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, oob_bank, OOB_CTRL1, 0x0, val);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	val = 0x0;
4828c2ecf20Sopenharmony_ci	val |= (0x2e << OOB_CTRL2_RESET_IDLE_MAX_SHIFT);
4838c2ecf20Sopenharmony_ci	val |= (0x02 << OOB_CTRL2_BURST_CNT_SHIFT);
4848c2ecf20Sopenharmony_ci	val |= (0x16 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT);
4858c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, oob_bank, OOB_CTRL2, 0x0, val);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_ACTRL2,
4898c2ecf20Sopenharmony_ci		~(PLL_ACTRL2_SELDIV_MASK << PLL_ACTRL2_SELDIV_SHIFT),
4908c2ecf20Sopenharmony_ci		0x0c << PLL_ACTRL2_SELDIV_SHIFT);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_CAP_CONTROL,
4938c2ecf20Sopenharmony_ci						0xff0, 0x4f0);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	val = PLLCONTROL_0_FREQ_DET_RESTART | PLLCONTROL_0_FREQ_MONITOR;
4968c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0,
4978c2ecf20Sopenharmony_ci								~val, val);
4988c2ecf20Sopenharmony_ci	val = PLLCONTROL_0_SEQ_START;
4998c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0,
5008c2ecf20Sopenharmony_ci								~val, 0);
5018c2ecf20Sopenharmony_ci	mdelay(10);
5028c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0,
5038c2ecf20Sopenharmony_ci								~val, val);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	/* Wait for pll_seq_done bit */
5068c2ecf20Sopenharmony_ci	try = 50;
5078c2ecf20Sopenharmony_ci	while (--try) {
5088c2ecf20Sopenharmony_ci		val = brcm_sata_phy_rd(port, BLOCK0_REG_BANK,
5098c2ecf20Sopenharmony_ci					BLOCK0_XGXSSTATUS);
5108c2ecf20Sopenharmony_ci		if (val & BLOCK0_XGXSSTATUS_PLL_LOCK)
5118c2ecf20Sopenharmony_ci			break;
5128c2ecf20Sopenharmony_ci		msleep(20);
5138c2ecf20Sopenharmony_ci	}
5148c2ecf20Sopenharmony_ci	if (!try) {
5158c2ecf20Sopenharmony_ci		/* PLL did not lock; give up */
5168c2ecf20Sopenharmony_ci		dev_err(dev, "port%d PLL did not lock\n", port->portnum);
5178c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	dev_dbg(dev, "port%d initialized\n", port->portnum);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	return 0;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/* SR PHY PLL0 registers */
5268c2ecf20Sopenharmony_ci#define SR_PLL0_ACTRL6_MAGIC			0xa
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci/* SR PHY PLL1 registers */
5298c2ecf20Sopenharmony_ci#define SR_PLL1_ACTRL2_MAGIC			0x32
5308c2ecf20Sopenharmony_ci#define SR_PLL1_ACTRL3_MAGIC			0x2
5318c2ecf20Sopenharmony_ci#define SR_PLL1_ACTRL4_MAGIC			0x3e8
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic int brcm_sr_sata_init(struct brcm_sata_port *port)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	struct device *dev = port->phy_priv->dev;
5368c2ecf20Sopenharmony_ci	unsigned int val, try;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	/* Configure PHY PLL register bank 1 */
5398c2ecf20Sopenharmony_ci	val = SR_PLL1_ACTRL2_MAGIC;
5408c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL2, 0x0, val);
5418c2ecf20Sopenharmony_ci	val = SR_PLL1_ACTRL3_MAGIC;
5428c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL3, 0x0, val);
5438c2ecf20Sopenharmony_ci	val = SR_PLL1_ACTRL4_MAGIC;
5448c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL4, 0x0, val);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	/* Configure PHY PLL register bank 0 */
5478c2ecf20Sopenharmony_ci	val = SR_PLL0_ACTRL6_MAGIC;
5488c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_ACTRL6, 0x0, val);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	/* Wait for PHY PLL lock by polling pll_lock bit */
5518c2ecf20Sopenharmony_ci	try = 50;
5528c2ecf20Sopenharmony_ci	do {
5538c2ecf20Sopenharmony_ci		val = brcm_sata_phy_rd(port, BLOCK0_REG_BANK,
5548c2ecf20Sopenharmony_ci					BLOCK0_XGXSSTATUS);
5558c2ecf20Sopenharmony_ci		if (val & BLOCK0_XGXSSTATUS_PLL_LOCK)
5568c2ecf20Sopenharmony_ci			break;
5578c2ecf20Sopenharmony_ci		msleep(20);
5588c2ecf20Sopenharmony_ci		try--;
5598c2ecf20Sopenharmony_ci	} while (try);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if ((val & BLOCK0_XGXSSTATUS_PLL_LOCK) == 0) {
5628c2ecf20Sopenharmony_ci		/* PLL did not lock; give up */
5638c2ecf20Sopenharmony_ci		dev_err(dev, "port%d PLL did not lock\n", port->portnum);
5648c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/* Invert Tx polarity */
5688c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, TX_REG_BANK, TX_ACTRL0,
5698c2ecf20Sopenharmony_ci			 ~TX_ACTRL0_TXPOL_FLIP, TX_ACTRL0_TXPOL_FLIP);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	/* Configure OOB control to handle 100MHz reference clock */
5728c2ecf20Sopenharmony_ci	val = ((0xc << OOB_CTRL1_BURST_MAX_SHIFT) |
5738c2ecf20Sopenharmony_ci		(0x4 << OOB_CTRL1_BURST_MIN_SHIFT) |
5748c2ecf20Sopenharmony_ci		(0x8 << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT) |
5758c2ecf20Sopenharmony_ci		(0x3 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT));
5768c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, OOB_REG_BANK, OOB_CTRL1, 0x0, val);
5778c2ecf20Sopenharmony_ci	val = ((0x1b << OOB_CTRL2_RESET_IDLE_MAX_SHIFT) |
5788c2ecf20Sopenharmony_ci		(0x2 << OOB_CTRL2_BURST_CNT_SHIFT) |
5798c2ecf20Sopenharmony_ci		(0x9 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT));
5808c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, OOB_REG_BANK, OOB_CTRL2, 0x0, val);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return 0;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic int brcm_dsl_sata_init(struct brcm_sata_port *port)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	struct device *dev = port->phy_priv->dev;
5888c2ecf20Sopenharmony_ci	unsigned int try;
5898c2ecf20Sopenharmony_ci	u32 tmp;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL7, 0, 0x873);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL1_REG_BANK, PLL1_ACTRL6, 0, 0xc000);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0,
5968c2ecf20Sopenharmony_ci			 0, 0x3089);
5978c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0,
6008c2ecf20Sopenharmony_ci			 0, 0x3088);
6018c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, AEQRX_REG_BANK_1, AEQRX_SLCAL0_CTRL0,
6048c2ecf20Sopenharmony_ci			 0, 0x3000);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, AEQRX_REG_BANK_1, AEQRX_SLCAL1_CTRL0,
6078c2ecf20Sopenharmony_ci			 0, 0x3000);
6088c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_CAP_CHARGE_TIME, 0, 0x32);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_VCO_CAL_THRESH, 0, 0xa);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, PLL_REG_BANK_0, PLL_FREQ_DET_TIME, 0, 0x64);
6158c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* Acquire PLL lock */
6188c2ecf20Sopenharmony_ci	try = 50;
6198c2ecf20Sopenharmony_ci	while (try) {
6208c2ecf20Sopenharmony_ci		tmp = brcm_sata_phy_rd(port, BLOCK0_REG_BANK,
6218c2ecf20Sopenharmony_ci				       BLOCK0_XGXSSTATUS);
6228c2ecf20Sopenharmony_ci		if (tmp & BLOCK0_XGXSSTATUS_PLL_LOCK)
6238c2ecf20Sopenharmony_ci			break;
6248c2ecf20Sopenharmony_ci		msleep(20);
6258c2ecf20Sopenharmony_ci		try--;
6268c2ecf20Sopenharmony_ci	};
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (!try) {
6298c2ecf20Sopenharmony_ci		/* PLL did not lock; give up */
6308c2ecf20Sopenharmony_ci		dev_err(dev, "port%d PLL did not lock\n", port->portnum);
6318c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	dev_dbg(dev, "port%d initialized\n", port->portnum);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	return 0;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic int brcm_sata_phy_init(struct phy *phy)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	int rc;
6428c2ecf20Sopenharmony_ci	struct brcm_sata_port *port = phy_get_drvdata(phy);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	switch (port->phy_priv->version) {
6458c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_STB_16NM:
6468c2ecf20Sopenharmony_ci		rc = brcm_stb_sata_16nm_init(port);
6478c2ecf20Sopenharmony_ci		break;
6488c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_STB_28NM:
6498c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_STB_40NM:
6508c2ecf20Sopenharmony_ci		rc = brcm_stb_sata_init(port);
6518c2ecf20Sopenharmony_ci		break;
6528c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_IPROC_NS2:
6538c2ecf20Sopenharmony_ci		rc = brcm_ns2_sata_init(port);
6548c2ecf20Sopenharmony_ci		break;
6558c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_IPROC_NSP:
6568c2ecf20Sopenharmony_ci		rc = brcm_nsp_sata_init(port);
6578c2ecf20Sopenharmony_ci		break;
6588c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_IPROC_SR:
6598c2ecf20Sopenharmony_ci		rc = brcm_sr_sata_init(port);
6608c2ecf20Sopenharmony_ci		break;
6618c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_DSL_28NM:
6628c2ecf20Sopenharmony_ci		rc = brcm_dsl_sata_init(port);
6638c2ecf20Sopenharmony_ci		break;
6648c2ecf20Sopenharmony_ci	default:
6658c2ecf20Sopenharmony_ci		rc = -ENODEV;
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	return rc;
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cistatic void brcm_stb_sata_calibrate(struct brcm_sata_port *port)
6728c2ecf20Sopenharmony_ci{
6738c2ecf20Sopenharmony_ci	u32 tmp = BIT(8);
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	brcm_sata_phy_wr(port, RXPMD_REG_BANK, RXPMD_RX_FREQ_MON_CONTROL1,
6768c2ecf20Sopenharmony_ci			 ~tmp, tmp);
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic int brcm_sata_phy_calibrate(struct phy *phy)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct brcm_sata_port *port = phy_get_drvdata(phy);
6828c2ecf20Sopenharmony_ci	int rc = -EOPNOTSUPP;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	switch (port->phy_priv->version) {
6858c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_STB_28NM:
6868c2ecf20Sopenharmony_ci	case BRCM_SATA_PHY_STB_40NM:
6878c2ecf20Sopenharmony_ci		brcm_stb_sata_calibrate(port);
6888c2ecf20Sopenharmony_ci		rc = 0;
6898c2ecf20Sopenharmony_ci		break;
6908c2ecf20Sopenharmony_ci	default:
6918c2ecf20Sopenharmony_ci		break;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	return rc;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic const struct phy_ops phy_ops = {
6988c2ecf20Sopenharmony_ci	.init		= brcm_sata_phy_init,
6998c2ecf20Sopenharmony_ci	.calibrate	= brcm_sata_phy_calibrate,
7008c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
7018c2ecf20Sopenharmony_ci};
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic const struct of_device_id brcm_sata_phy_of_match[] = {
7048c2ecf20Sopenharmony_ci	{ .compatible	= "brcm,bcm7216-sata-phy",
7058c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_STB_16NM },
7068c2ecf20Sopenharmony_ci	{ .compatible	= "brcm,bcm7445-sata-phy",
7078c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_STB_28NM },
7088c2ecf20Sopenharmony_ci	{ .compatible	= "brcm,bcm7425-sata-phy",
7098c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_STB_40NM },
7108c2ecf20Sopenharmony_ci	{ .compatible	= "brcm,iproc-ns2-sata-phy",
7118c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_IPROC_NS2 },
7128c2ecf20Sopenharmony_ci	{ .compatible = "brcm,iproc-nsp-sata-phy",
7138c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_IPROC_NSP },
7148c2ecf20Sopenharmony_ci	{ .compatible	= "brcm,iproc-sr-sata-phy",
7158c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_IPROC_SR },
7168c2ecf20Sopenharmony_ci	{ .compatible	= "brcm,bcm63138-sata-phy",
7178c2ecf20Sopenharmony_ci	  .data = (void *)BRCM_SATA_PHY_DSL_28NM },
7188c2ecf20Sopenharmony_ci	{},
7198c2ecf20Sopenharmony_ci};
7208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, brcm_sata_phy_of_match);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic int brcm_sata_phy_probe(struct platform_device *pdev)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	const char *rxaeq_mode;
7258c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
7268c2ecf20Sopenharmony_ci	struct device_node *dn = dev->of_node, *child;
7278c2ecf20Sopenharmony_ci	const struct of_device_id *of_id;
7288c2ecf20Sopenharmony_ci	struct brcm_sata_phy *priv;
7298c2ecf20Sopenharmony_ci	struct resource *res;
7308c2ecf20Sopenharmony_ci	struct phy_provider *provider;
7318c2ecf20Sopenharmony_ci	int ret, count = 0;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	if (of_get_child_count(dn) == 0)
7348c2ecf20Sopenharmony_ci		return -ENODEV;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
7378c2ecf20Sopenharmony_ci	if (!priv)
7388c2ecf20Sopenharmony_ci		return -ENOMEM;
7398c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, priv);
7408c2ecf20Sopenharmony_ci	priv->dev = dev;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
7438c2ecf20Sopenharmony_ci	priv->phy_base = devm_ioremap_resource(dev, res);
7448c2ecf20Sopenharmony_ci	if (IS_ERR(priv->phy_base))
7458c2ecf20Sopenharmony_ci		return PTR_ERR(priv->phy_base);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	of_id = of_match_node(brcm_sata_phy_of_match, dn);
7488c2ecf20Sopenharmony_ci	if (of_id)
7498c2ecf20Sopenharmony_ci		priv->version = (enum brcm_sata_phy_version)of_id->data;
7508c2ecf20Sopenharmony_ci	else
7518c2ecf20Sopenharmony_ci		priv->version = BRCM_SATA_PHY_STB_28NM;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	if (priv->version == BRCM_SATA_PHY_IPROC_NS2) {
7548c2ecf20Sopenharmony_ci		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
7558c2ecf20Sopenharmony_ci						   "phy-ctrl");
7568c2ecf20Sopenharmony_ci		priv->ctrl_base = devm_ioremap_resource(dev, res);
7578c2ecf20Sopenharmony_ci		if (IS_ERR(priv->ctrl_base))
7588c2ecf20Sopenharmony_ci			return PTR_ERR(priv->ctrl_base);
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	for_each_available_child_of_node(dn, child) {
7628c2ecf20Sopenharmony_ci		unsigned int id;
7638c2ecf20Sopenharmony_ci		struct brcm_sata_port *port;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci		if (of_property_read_u32(child, "reg", &id)) {
7668c2ecf20Sopenharmony_ci			dev_err(dev, "missing reg property in node %pOFn\n",
7678c2ecf20Sopenharmony_ci					child);
7688c2ecf20Sopenharmony_ci			ret = -EINVAL;
7698c2ecf20Sopenharmony_ci			goto put_child;
7708c2ecf20Sopenharmony_ci		}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci		if (id >= MAX_PORTS) {
7738c2ecf20Sopenharmony_ci			dev_err(dev, "invalid reg: %u\n", id);
7748c2ecf20Sopenharmony_ci			ret = -EINVAL;
7758c2ecf20Sopenharmony_ci			goto put_child;
7768c2ecf20Sopenharmony_ci		}
7778c2ecf20Sopenharmony_ci		if (priv->phys[id].phy) {
7788c2ecf20Sopenharmony_ci			dev_err(dev, "already registered port %u\n", id);
7798c2ecf20Sopenharmony_ci			ret = -EINVAL;
7808c2ecf20Sopenharmony_ci			goto put_child;
7818c2ecf20Sopenharmony_ci		}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci		port = &priv->phys[id];
7848c2ecf20Sopenharmony_ci		port->portnum = id;
7858c2ecf20Sopenharmony_ci		port->phy_priv = priv;
7868c2ecf20Sopenharmony_ci		port->phy = devm_phy_create(dev, child, &phy_ops);
7878c2ecf20Sopenharmony_ci		port->rxaeq_mode = RXAEQ_MODE_OFF;
7888c2ecf20Sopenharmony_ci		if (!of_property_read_string(child, "brcm,rxaeq-mode",
7898c2ecf20Sopenharmony_ci					     &rxaeq_mode))
7908c2ecf20Sopenharmony_ci			port->rxaeq_mode = rxaeq_to_val(rxaeq_mode);
7918c2ecf20Sopenharmony_ci		if (port->rxaeq_mode == RXAEQ_MODE_MANUAL)
7928c2ecf20Sopenharmony_ci			of_property_read_u32(child, "brcm,rxaeq-value",
7938c2ecf20Sopenharmony_ci					     &port->rxaeq_val);
7948c2ecf20Sopenharmony_ci		port->ssc_en = of_property_read_bool(child, "brcm,enable-ssc");
7958c2ecf20Sopenharmony_ci		if (IS_ERR(port->phy)) {
7968c2ecf20Sopenharmony_ci			dev_err(dev, "failed to create PHY\n");
7978c2ecf20Sopenharmony_ci			ret = PTR_ERR(port->phy);
7988c2ecf20Sopenharmony_ci			goto put_child;
7998c2ecf20Sopenharmony_ci		}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci		phy_set_drvdata(port->phy, port);
8028c2ecf20Sopenharmony_ci		count++;
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
8068c2ecf20Sopenharmony_ci	if (IS_ERR(provider)) {
8078c2ecf20Sopenharmony_ci		dev_err(dev, "could not register PHY provider\n");
8088c2ecf20Sopenharmony_ci		return PTR_ERR(provider);
8098c2ecf20Sopenharmony_ci	}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	dev_info(dev, "registered %d port(s)\n", count);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	return 0;
8148c2ecf20Sopenharmony_ciput_child:
8158c2ecf20Sopenharmony_ci	of_node_put(child);
8168c2ecf20Sopenharmony_ci	return ret;
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_cistatic struct platform_driver brcm_sata_phy_driver = {
8208c2ecf20Sopenharmony_ci	.probe	= brcm_sata_phy_probe,
8218c2ecf20Sopenharmony_ci	.driver	= {
8228c2ecf20Sopenharmony_ci		.of_match_table	= brcm_sata_phy_of_match,
8238c2ecf20Sopenharmony_ci		.name		= "brcm-sata-phy",
8248c2ecf20Sopenharmony_ci	}
8258c2ecf20Sopenharmony_ci};
8268c2ecf20Sopenharmony_cimodule_platform_driver(brcm_sata_phy_driver);
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Broadcom SATA PHY driver");
8298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
8308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Marc Carino");
8318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Brian Norris");
8328c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:phy-brcm-sata");
833