xref: /linux/drivers/phy/rockchip/phy-rockchip-naneng-combphy.c (revision 6fcb22ef50b47959e17012be57dcad0fdca8d237)
17160820dSYifeng Zhao // SPDX-License-Identifier: GPL-2.0
27160820dSYifeng Zhao /*
37160820dSYifeng Zhao  * Rockchip PIPE USB3.0 PCIE SATA Combo Phy driver
47160820dSYifeng Zhao  *
57160820dSYifeng Zhao  * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
67160820dSYifeng Zhao  */
77160820dSYifeng Zhao 
87160820dSYifeng Zhao #include <dt-bindings/phy/phy.h>
97160820dSYifeng Zhao #include <linux/clk.h>
107160820dSYifeng Zhao #include <linux/mfd/syscon.h>
117559e757SRob Herring #include <linux/of.h>
127160820dSYifeng Zhao #include <linux/phy/phy.h>
137559e757SRob Herring #include <linux/platform_device.h>
147160820dSYifeng Zhao #include <linux/regmap.h>
157160820dSYifeng Zhao #include <linux/reset.h>
167160820dSYifeng Zhao #include <linux/units.h>
177160820dSYifeng Zhao 
187160820dSYifeng Zhao #define BIT_WRITEABLE_SHIFT		16
197160820dSYifeng Zhao #define REF_CLOCK_24MHz			(24 * HZ_PER_MHZ)
207160820dSYifeng Zhao #define REF_CLOCK_25MHz			(25 * HZ_PER_MHZ)
217160820dSYifeng Zhao #define REF_CLOCK_100MHz		(100 * HZ_PER_MHZ)
227160820dSYifeng Zhao 
237160820dSYifeng Zhao /* COMBO PHY REG */
247160820dSYifeng Zhao #define PHYREG6				0x14
257160820dSYifeng Zhao #define PHYREG6_PLL_DIV_MASK		GENMASK(7, 6)
267160820dSYifeng Zhao #define PHYREG6_PLL_DIV_SHIFT		6
277160820dSYifeng Zhao #define PHYREG6_PLL_DIV_2		1
287160820dSYifeng Zhao 
297160820dSYifeng Zhao #define PHYREG7				0x18
307160820dSYifeng Zhao #define PHYREG7_TX_RTERM_MASK		GENMASK(7, 4)
317160820dSYifeng Zhao #define PHYREG7_TX_RTERM_SHIFT		4
327160820dSYifeng Zhao #define PHYREG7_TX_RTERM_50OHM		8
337160820dSYifeng Zhao #define PHYREG7_RX_RTERM_MASK		GENMASK(3, 0)
347160820dSYifeng Zhao #define PHYREG7_RX_RTERM_SHIFT		0
357160820dSYifeng Zhao #define PHYREG7_RX_RTERM_44OHM		15
367160820dSYifeng Zhao 
377160820dSYifeng Zhao #define PHYREG8				0x1C
387160820dSYifeng Zhao #define PHYREG8_SSC_EN			BIT(4)
397160820dSYifeng Zhao 
407160820dSYifeng Zhao #define PHYREG11			0x28
417160820dSYifeng Zhao #define PHYREG11_SU_TRIM_0_7		0xF0
427160820dSYifeng Zhao 
437160820dSYifeng Zhao #define PHYREG12			0x2C
447160820dSYifeng Zhao #define PHYREG12_PLL_LPF_ADJ_VALUE	4
457160820dSYifeng Zhao 
467160820dSYifeng Zhao #define PHYREG13			0x30
477160820dSYifeng Zhao #define PHYREG13_RESISTER_MASK		GENMASK(5, 4)
487160820dSYifeng Zhao #define PHYREG13_RESISTER_SHIFT		0x4
497160820dSYifeng Zhao #define PHYREG13_RESISTER_HIGH_Z	3
507160820dSYifeng Zhao #define PHYREG13_CKRCV_AMP0		BIT(7)
517160820dSYifeng Zhao 
527160820dSYifeng Zhao #define PHYREG14			0x34
537160820dSYifeng Zhao #define PHYREG14_CKRCV_AMP1		BIT(0)
547160820dSYifeng Zhao 
557160820dSYifeng Zhao #define PHYREG15			0x38
567160820dSYifeng Zhao #define PHYREG15_CTLE_EN		BIT(0)
577160820dSYifeng Zhao #define PHYREG15_SSC_CNT_MASK		GENMASK(7, 6)
587160820dSYifeng Zhao #define PHYREG15_SSC_CNT_SHIFT		6
597160820dSYifeng Zhao #define PHYREG15_SSC_CNT_VALUE		1
607160820dSYifeng Zhao 
617160820dSYifeng Zhao #define PHYREG16			0x3C
627160820dSYifeng Zhao #define PHYREG16_SSC_CNT_VALUE		0x5f
637160820dSYifeng Zhao 
647160820dSYifeng Zhao #define PHYREG18			0x44
657160820dSYifeng Zhao #define PHYREG18_PLL_LOOP		0x32
667160820dSYifeng Zhao 
67a03c4427SLucas Tanure #define PHYREG27			0x6C
68a03c4427SLucas Tanure #define PHYREG27_RX_TRIM_RK3588		0x4C
69a03c4427SLucas Tanure 
707160820dSYifeng Zhao #define PHYREG32			0x7C
717160820dSYifeng Zhao #define PHYREG32_SSC_MASK		GENMASK(7, 4)
727160820dSYifeng Zhao #define PHYREG32_SSC_DIR_SHIFT		4
737160820dSYifeng Zhao #define PHYREG32_SSC_UPWARD		0
747160820dSYifeng Zhao #define PHYREG32_SSC_DOWNWARD		1
757160820dSYifeng Zhao #define PHYREG32_SSC_OFFSET_SHIFT	6
767160820dSYifeng Zhao #define PHYREG32_SSC_OFFSET_500PPM	1
777160820dSYifeng Zhao 
787160820dSYifeng Zhao #define PHYREG33			0x80
797160820dSYifeng Zhao #define PHYREG33_PLL_KVCO_MASK		GENMASK(4, 2)
807160820dSYifeng Zhao #define PHYREG33_PLL_KVCO_SHIFT		2
817160820dSYifeng Zhao #define PHYREG33_PLL_KVCO_VALUE		2
827160820dSYifeng Zhao 
837160820dSYifeng Zhao struct rockchip_combphy_priv;
847160820dSYifeng Zhao 
857160820dSYifeng Zhao struct combphy_reg {
867160820dSYifeng Zhao 	u16 offset;
877160820dSYifeng Zhao 	u16 bitend;
887160820dSYifeng Zhao 	u16 bitstart;
897160820dSYifeng Zhao 	u16 disable;
907160820dSYifeng Zhao 	u16 enable;
917160820dSYifeng Zhao };
927160820dSYifeng Zhao 
937160820dSYifeng Zhao struct rockchip_combphy_grfcfg {
947160820dSYifeng Zhao 	struct combphy_reg pcie_mode_set;
957160820dSYifeng Zhao 	struct combphy_reg usb_mode_set;
967160820dSYifeng Zhao 	struct combphy_reg sgmii_mode_set;
977160820dSYifeng Zhao 	struct combphy_reg qsgmii_mode_set;
987160820dSYifeng Zhao 	struct combphy_reg pipe_rxterm_set;
997160820dSYifeng Zhao 	struct combphy_reg pipe_txelec_set;
1007160820dSYifeng Zhao 	struct combphy_reg pipe_txcomp_set;
1017160820dSYifeng Zhao 	struct combphy_reg pipe_clk_25m;
1027160820dSYifeng Zhao 	struct combphy_reg pipe_clk_100m;
1037160820dSYifeng Zhao 	struct combphy_reg pipe_phymode_sel;
1047160820dSYifeng Zhao 	struct combphy_reg pipe_rate_sel;
1057160820dSYifeng Zhao 	struct combphy_reg pipe_rxterm_sel;
1067160820dSYifeng Zhao 	struct combphy_reg pipe_txelec_sel;
1077160820dSYifeng Zhao 	struct combphy_reg pipe_txcomp_sel;
1087160820dSYifeng Zhao 	struct combphy_reg pipe_clk_ext;
1097160820dSYifeng Zhao 	struct combphy_reg pipe_sel_usb;
1107160820dSYifeng Zhao 	struct combphy_reg pipe_sel_qsgmii;
1117160820dSYifeng Zhao 	struct combphy_reg pipe_phy_status;
1127160820dSYifeng Zhao 	struct combphy_reg con0_for_pcie;
1137160820dSYifeng Zhao 	struct combphy_reg con1_for_pcie;
1147160820dSYifeng Zhao 	struct combphy_reg con2_for_pcie;
1157160820dSYifeng Zhao 	struct combphy_reg con3_for_pcie;
1167160820dSYifeng Zhao 	struct combphy_reg con0_for_sata;
1177160820dSYifeng Zhao 	struct combphy_reg con1_for_sata;
1187160820dSYifeng Zhao 	struct combphy_reg con2_for_sata;
1197160820dSYifeng Zhao 	struct combphy_reg con3_for_sata;
1207160820dSYifeng Zhao 	struct combphy_reg pipe_con0_for_sata;
121a03c4427SLucas Tanure 	struct combphy_reg pipe_con1_for_sata;
1227160820dSYifeng Zhao 	struct combphy_reg pipe_xpcs_phy_ready;
123a03c4427SLucas Tanure 	struct combphy_reg pipe_pcie1l0_sel;
124a03c4427SLucas Tanure 	struct combphy_reg pipe_pcie1l1_sel;
1257160820dSYifeng Zhao };
1267160820dSYifeng Zhao 
1277160820dSYifeng Zhao struct rockchip_combphy_cfg {
128d16d4002SSebastian Reichel 	unsigned int num_phys;
129d16d4002SSebastian Reichel 	unsigned int phy_ids[3];
1307160820dSYifeng Zhao 	const struct rockchip_combphy_grfcfg *grfcfg;
1317160820dSYifeng Zhao 	int (*combphy_cfg)(struct rockchip_combphy_priv *priv);
1327160820dSYifeng Zhao };
1337160820dSYifeng Zhao 
1347160820dSYifeng Zhao struct rockchip_combphy_priv {
1357160820dSYifeng Zhao 	u8 type;
136d16d4002SSebastian Reichel 	int id;
1377160820dSYifeng Zhao 	void __iomem *mmio;
1387160820dSYifeng Zhao 	int num_clks;
1397160820dSYifeng Zhao 	struct clk_bulk_data *clks;
1407160820dSYifeng Zhao 	struct device *dev;
1417160820dSYifeng Zhao 	struct regmap *pipe_grf;
1427160820dSYifeng Zhao 	struct regmap *phy_grf;
1437160820dSYifeng Zhao 	struct phy *phy;
1447160820dSYifeng Zhao 	struct reset_control *phy_rst;
1457160820dSYifeng Zhao 	const struct rockchip_combphy_cfg *cfg;
1467160820dSYifeng Zhao 	bool enable_ssc;
1477160820dSYifeng Zhao 	bool ext_refclk;
1487160820dSYifeng Zhao 	struct clk *refclk;
1497160820dSYifeng Zhao };
1507160820dSYifeng Zhao 
rockchip_combphy_updatel(struct rockchip_combphy_priv * priv,int mask,int val,int reg)1517160820dSYifeng Zhao static void rockchip_combphy_updatel(struct rockchip_combphy_priv *priv,
1527160820dSYifeng Zhao 				     int mask, int val, int reg)
1537160820dSYifeng Zhao {
1547160820dSYifeng Zhao 	unsigned int temp;
1557160820dSYifeng Zhao 
1567160820dSYifeng Zhao 	temp = readl(priv->mmio + reg);
1577160820dSYifeng Zhao 	temp = (temp & ~(mask)) | val;
1587160820dSYifeng Zhao 	writel(temp, priv->mmio + reg);
1597160820dSYifeng Zhao }
1607160820dSYifeng Zhao 
rockchip_combphy_param_write(struct regmap * base,const struct combphy_reg * reg,bool en)1617160820dSYifeng Zhao static int rockchip_combphy_param_write(struct regmap *base,
1627160820dSYifeng Zhao 					const struct combphy_reg *reg, bool en)
1637160820dSYifeng Zhao {
1647160820dSYifeng Zhao 	u32 val, mask, tmp;
1657160820dSYifeng Zhao 
1667160820dSYifeng Zhao 	tmp = en ? reg->enable : reg->disable;
1677160820dSYifeng Zhao 	mask = GENMASK(reg->bitend, reg->bitstart);
1687160820dSYifeng Zhao 	val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
1697160820dSYifeng Zhao 
1707160820dSYifeng Zhao 	return regmap_write(base, reg->offset, val);
1717160820dSYifeng Zhao }
1727160820dSYifeng Zhao 
rockchip_combphy_is_ready(struct rockchip_combphy_priv * priv)1737160820dSYifeng Zhao static u32 rockchip_combphy_is_ready(struct rockchip_combphy_priv *priv)
1747160820dSYifeng Zhao {
1757160820dSYifeng Zhao 	const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
1767160820dSYifeng Zhao 	u32 mask, val;
1777160820dSYifeng Zhao 
1787160820dSYifeng Zhao 	mask = GENMASK(cfg->pipe_phy_status.bitend,
1797160820dSYifeng Zhao 		       cfg->pipe_phy_status.bitstart);
1807160820dSYifeng Zhao 
1817160820dSYifeng Zhao 	regmap_read(priv->phy_grf, cfg->pipe_phy_status.offset, &val);
1827160820dSYifeng Zhao 	val = (val & mask) >> cfg->pipe_phy_status.bitstart;
1837160820dSYifeng Zhao 
1847160820dSYifeng Zhao 	return val;
1857160820dSYifeng Zhao }
1867160820dSYifeng Zhao 
rockchip_combphy_init(struct phy * phy)1877160820dSYifeng Zhao static int rockchip_combphy_init(struct phy *phy)
1887160820dSYifeng Zhao {
1897160820dSYifeng Zhao 	struct rockchip_combphy_priv *priv = phy_get_drvdata(phy);
1907160820dSYifeng Zhao 	const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
1917160820dSYifeng Zhao 	u32 val;
1927160820dSYifeng Zhao 	int ret;
1937160820dSYifeng Zhao 
1947160820dSYifeng Zhao 	ret = clk_bulk_prepare_enable(priv->num_clks, priv->clks);
1957160820dSYifeng Zhao 	if (ret) {
1967160820dSYifeng Zhao 		dev_err(priv->dev, "failed to enable clks\n");
1977160820dSYifeng Zhao 		return ret;
1987160820dSYifeng Zhao 	}
1997160820dSYifeng Zhao 
2007160820dSYifeng Zhao 	switch (priv->type) {
2017160820dSYifeng Zhao 	case PHY_TYPE_PCIE:
2027160820dSYifeng Zhao 	case PHY_TYPE_USB3:
2037160820dSYifeng Zhao 	case PHY_TYPE_SATA:
2047160820dSYifeng Zhao 	case PHY_TYPE_SGMII:
2057160820dSYifeng Zhao 	case PHY_TYPE_QSGMII:
2067160820dSYifeng Zhao 		if (priv->cfg->combphy_cfg)
2077160820dSYifeng Zhao 			ret = priv->cfg->combphy_cfg(priv);
2087160820dSYifeng Zhao 		break;
2097160820dSYifeng Zhao 	default:
2107160820dSYifeng Zhao 		dev_err(priv->dev, "incompatible PHY type\n");
2117160820dSYifeng Zhao 		ret = -EINVAL;
2127160820dSYifeng Zhao 		break;
2137160820dSYifeng Zhao 	}
2147160820dSYifeng Zhao 
2157160820dSYifeng Zhao 	if (ret) {
2167160820dSYifeng Zhao 		dev_err(priv->dev, "failed to init phy for phy type %x\n", priv->type);
2177160820dSYifeng Zhao 		goto err_clk;
2187160820dSYifeng Zhao 	}
2197160820dSYifeng Zhao 
2207160820dSYifeng Zhao 	ret = reset_control_deassert(priv->phy_rst);
2217160820dSYifeng Zhao 	if (ret)
2227160820dSYifeng Zhao 		goto err_clk;
2237160820dSYifeng Zhao 
2247160820dSYifeng Zhao 	if (priv->type == PHY_TYPE_USB3) {
2257160820dSYifeng Zhao 		ret = readx_poll_timeout_atomic(rockchip_combphy_is_ready,
2267160820dSYifeng Zhao 						priv, val,
2277160820dSYifeng Zhao 						val == cfg->pipe_phy_status.enable,
2287160820dSYifeng Zhao 						10, 1000);
2297160820dSYifeng Zhao 		if (ret)
2307160820dSYifeng Zhao 			dev_warn(priv->dev, "wait phy status ready timeout\n");
2317160820dSYifeng Zhao 	}
2327160820dSYifeng Zhao 
2337160820dSYifeng Zhao 	return 0;
2347160820dSYifeng Zhao 
2357160820dSYifeng Zhao err_clk:
2367160820dSYifeng Zhao 	clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
2377160820dSYifeng Zhao 
2387160820dSYifeng Zhao 	return ret;
2397160820dSYifeng Zhao }
2407160820dSYifeng Zhao 
rockchip_combphy_exit(struct phy * phy)2417160820dSYifeng Zhao static int rockchip_combphy_exit(struct phy *phy)
2427160820dSYifeng Zhao {
2437160820dSYifeng Zhao 	struct rockchip_combphy_priv *priv = phy_get_drvdata(phy);
2447160820dSYifeng Zhao 
2457160820dSYifeng Zhao 	clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
2467160820dSYifeng Zhao 	reset_control_assert(priv->phy_rst);
2477160820dSYifeng Zhao 
2487160820dSYifeng Zhao 	return 0;
2497160820dSYifeng Zhao }
2507160820dSYifeng Zhao 
2519b6bfad9SRick Wertenbroek static const struct phy_ops rockchip_combphy_ops = {
2527160820dSYifeng Zhao 	.init = rockchip_combphy_init,
2537160820dSYifeng Zhao 	.exit = rockchip_combphy_exit,
2547160820dSYifeng Zhao 	.owner = THIS_MODULE,
2557160820dSYifeng Zhao };
2567160820dSYifeng Zhao 
rockchip_combphy_xlate(struct device * dev,const struct of_phandle_args * args)25700ca8a15SKrzysztof Kozlowski static struct phy *rockchip_combphy_xlate(struct device *dev, const struct of_phandle_args *args)
2587160820dSYifeng Zhao {
2597160820dSYifeng Zhao 	struct rockchip_combphy_priv *priv = dev_get_drvdata(dev);
2607160820dSYifeng Zhao 
2617160820dSYifeng Zhao 	if (args->args_count != 1) {
2627160820dSYifeng Zhao 		dev_err(dev, "invalid number of arguments\n");
2637160820dSYifeng Zhao 		return ERR_PTR(-EINVAL);
2647160820dSYifeng Zhao 	}
2657160820dSYifeng Zhao 
2667160820dSYifeng Zhao 	if (priv->type != PHY_NONE && priv->type != args->args[0])
2677160820dSYifeng Zhao 		dev_warn(dev, "phy type select %d overwriting type %d\n",
2687160820dSYifeng Zhao 			 args->args[0], priv->type);
2697160820dSYifeng Zhao 
2707160820dSYifeng Zhao 	priv->type = args->args[0];
2717160820dSYifeng Zhao 
2727160820dSYifeng Zhao 	return priv->phy;
2737160820dSYifeng Zhao }
2747160820dSYifeng Zhao 
rockchip_combphy_parse_dt(struct device * dev,struct rockchip_combphy_priv * priv)2757160820dSYifeng Zhao static int rockchip_combphy_parse_dt(struct device *dev, struct rockchip_combphy_priv *priv)
2767160820dSYifeng Zhao {
2777160820dSYifeng Zhao 	int i;
2787160820dSYifeng Zhao 
2797160820dSYifeng Zhao 	priv->num_clks = devm_clk_bulk_get_all(dev, &priv->clks);
2807160820dSYifeng Zhao 	if (priv->num_clks < 1)
2817160820dSYifeng Zhao 		return -EINVAL;
2827160820dSYifeng Zhao 
2837160820dSYifeng Zhao 	priv->refclk = NULL;
2847160820dSYifeng Zhao 	for (i = 0; i < priv->num_clks; i++) {
2857160820dSYifeng Zhao 		if (!strncmp(priv->clks[i].id, "ref", 3)) {
2867160820dSYifeng Zhao 			priv->refclk = priv->clks[i].clk;
2877160820dSYifeng Zhao 			break;
2887160820dSYifeng Zhao 		}
2897160820dSYifeng Zhao 	}
2907160820dSYifeng Zhao 
2917160820dSYifeng Zhao 	if (!priv->refclk) {
2927160820dSYifeng Zhao 		dev_err(dev, "no refclk found\n");
2937160820dSYifeng Zhao 		return -EINVAL;
2947160820dSYifeng Zhao 	}
2957160820dSYifeng Zhao 
2967160820dSYifeng Zhao 	priv->pipe_grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,pipe-grf");
2977160820dSYifeng Zhao 	if (IS_ERR(priv->pipe_grf)) {
2987160820dSYifeng Zhao 		dev_err(dev, "failed to find peri_ctrl pipe-grf regmap\n");
2997160820dSYifeng Zhao 		return PTR_ERR(priv->pipe_grf);
3007160820dSYifeng Zhao 	}
3017160820dSYifeng Zhao 
3027160820dSYifeng Zhao 	priv->phy_grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,pipe-phy-grf");
3037160820dSYifeng Zhao 	if (IS_ERR(priv->phy_grf)) {
3047160820dSYifeng Zhao 		dev_err(dev, "failed to find peri_ctrl pipe-phy-grf regmap\n");
3057160820dSYifeng Zhao 		return PTR_ERR(priv->phy_grf);
3067160820dSYifeng Zhao 	}
3077160820dSYifeng Zhao 
3087160820dSYifeng Zhao 	priv->enable_ssc = device_property_present(dev, "rockchip,enable-ssc");
3097160820dSYifeng Zhao 
3107160820dSYifeng Zhao 	priv->ext_refclk = device_property_present(dev, "rockchip,ext-refclk");
3117160820dSYifeng Zhao 
312*fbcbffbaSChukun Pan 	priv->phy_rst = devm_reset_control_get(dev, "phy");
3137160820dSYifeng Zhao 	if (IS_ERR(priv->phy_rst))
3147160820dSYifeng Zhao 		return dev_err_probe(dev, PTR_ERR(priv->phy_rst), "failed to get phy reset\n");
3157160820dSYifeng Zhao 
3167160820dSYifeng Zhao 	return 0;
3177160820dSYifeng Zhao }
3187160820dSYifeng Zhao 
rockchip_combphy_probe(struct platform_device * pdev)3197160820dSYifeng Zhao static int rockchip_combphy_probe(struct platform_device *pdev)
3207160820dSYifeng Zhao {
3217160820dSYifeng Zhao 	struct phy_provider *phy_provider;
3227160820dSYifeng Zhao 	struct device *dev = &pdev->dev;
3237160820dSYifeng Zhao 	struct rockchip_combphy_priv *priv;
3247160820dSYifeng Zhao 	const struct rockchip_combphy_cfg *phy_cfg;
3257160820dSYifeng Zhao 	struct resource *res;
326d16d4002SSebastian Reichel 	int ret, id;
3277160820dSYifeng Zhao 
3287160820dSYifeng Zhao 	phy_cfg = of_device_get_match_data(dev);
3297160820dSYifeng Zhao 	if (!phy_cfg) {
3307160820dSYifeng Zhao 		dev_err(dev, "no OF match data provided\n");
3317160820dSYifeng Zhao 		return -EINVAL;
3327160820dSYifeng Zhao 	}
3337160820dSYifeng Zhao 
3347160820dSYifeng Zhao 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3357160820dSYifeng Zhao 	if (!priv)
3367160820dSYifeng Zhao 		return -ENOMEM;
3377160820dSYifeng Zhao 
3387160820dSYifeng Zhao 	priv->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
3397160820dSYifeng Zhao 	if (IS_ERR(priv->mmio)) {
3407160820dSYifeng Zhao 		ret = PTR_ERR(priv->mmio);
3417160820dSYifeng Zhao 		return ret;
3427160820dSYifeng Zhao 	}
3437160820dSYifeng Zhao 
344d16d4002SSebastian Reichel 	/* find the phy-id from the io address */
345d16d4002SSebastian Reichel 	priv->id = -ENODEV;
346d16d4002SSebastian Reichel 	for (id = 0; id < phy_cfg->num_phys; id++) {
347d16d4002SSebastian Reichel 		if (res->start == phy_cfg->phy_ids[id]) {
348d16d4002SSebastian Reichel 			priv->id = id;
349d16d4002SSebastian Reichel 			break;
350d16d4002SSebastian Reichel 		}
351d16d4002SSebastian Reichel 	}
352d16d4002SSebastian Reichel 
3537160820dSYifeng Zhao 	priv->dev = dev;
3547160820dSYifeng Zhao 	priv->type = PHY_NONE;
3557160820dSYifeng Zhao 	priv->cfg = phy_cfg;
3567160820dSYifeng Zhao 
3577160820dSYifeng Zhao 	ret = rockchip_combphy_parse_dt(dev, priv);
3587160820dSYifeng Zhao 	if (ret)
3597160820dSYifeng Zhao 		return ret;
3607160820dSYifeng Zhao 
3617160820dSYifeng Zhao 	ret = reset_control_assert(priv->phy_rst);
3627160820dSYifeng Zhao 	if (ret) {
3637160820dSYifeng Zhao 		dev_err(dev, "failed to reset phy\n");
3647160820dSYifeng Zhao 		return ret;
3657160820dSYifeng Zhao 	}
3667160820dSYifeng Zhao 
3679b6bfad9SRick Wertenbroek 	priv->phy = devm_phy_create(dev, NULL, &rockchip_combphy_ops);
3687160820dSYifeng Zhao 	if (IS_ERR(priv->phy)) {
3697160820dSYifeng Zhao 		dev_err(dev, "failed to create combphy\n");
3707160820dSYifeng Zhao 		return PTR_ERR(priv->phy);
3717160820dSYifeng Zhao 	}
3727160820dSYifeng Zhao 
3737160820dSYifeng Zhao 	dev_set_drvdata(dev, priv);
3747160820dSYifeng Zhao 	phy_set_drvdata(priv->phy, priv);
3757160820dSYifeng Zhao 
3767160820dSYifeng Zhao 	phy_provider = devm_of_phy_provider_register(dev, rockchip_combphy_xlate);
3777160820dSYifeng Zhao 
3787160820dSYifeng Zhao 	return PTR_ERR_OR_ZERO(phy_provider);
3797160820dSYifeng Zhao }
3807160820dSYifeng Zhao 
rk3568_combphy_cfg(struct rockchip_combphy_priv * priv)3817160820dSYifeng Zhao static int rk3568_combphy_cfg(struct rockchip_combphy_priv *priv)
3827160820dSYifeng Zhao {
3837160820dSYifeng Zhao 	const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
3847160820dSYifeng Zhao 	unsigned long rate;
3857160820dSYifeng Zhao 	u32 val;
3867160820dSYifeng Zhao 
3877160820dSYifeng Zhao 	switch (priv->type) {
3887160820dSYifeng Zhao 	case PHY_TYPE_PCIE:
3897160820dSYifeng Zhao 		/* Set SSC downward spread spectrum. */
3907160820dSYifeng Zhao 		rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK,
3917160820dSYifeng Zhao 					 PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT,
3927160820dSYifeng Zhao 					 PHYREG32);
3937160820dSYifeng Zhao 
3947160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_pcie, true);
3957160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_pcie, true);
3967160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_pcie, true);
3977160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_pcie, true);
3987160820dSYifeng Zhao 		break;
3997160820dSYifeng Zhao 
4007160820dSYifeng Zhao 	case PHY_TYPE_USB3:
4017160820dSYifeng Zhao 		/* Set SSC downward spread spectrum. */
4027160820dSYifeng Zhao 		rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK,
4037160820dSYifeng Zhao 					 PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT,
4047160820dSYifeng Zhao 					 PHYREG32);
4057160820dSYifeng Zhao 
4067160820dSYifeng Zhao 		/* Enable adaptive CTLE for USB3.0 Rx. */
4077160820dSYifeng Zhao 		val = readl(priv->mmio + PHYREG15);
4087160820dSYifeng Zhao 		val |= PHYREG15_CTLE_EN;
4097160820dSYifeng Zhao 		writel(val, priv->mmio + PHYREG15);
4107160820dSYifeng Zhao 
4117160820dSYifeng Zhao 		/* Set PLL KVCO fine tuning signals. */
4127160820dSYifeng Zhao 		rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
4137160820dSYifeng Zhao 					 PHYREG33_PLL_KVCO_VALUE << PHYREG33_PLL_KVCO_SHIFT,
4147160820dSYifeng Zhao 					 PHYREG33);
4157160820dSYifeng Zhao 
4167160820dSYifeng Zhao 		/* Enable controlling random jitter. */
4177160820dSYifeng Zhao 		writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
4187160820dSYifeng Zhao 
4197160820dSYifeng Zhao 		/* Set PLL input clock divider 1/2. */
4207160820dSYifeng Zhao 		rockchip_combphy_updatel(priv, PHYREG6_PLL_DIV_MASK,
4217160820dSYifeng Zhao 					 PHYREG6_PLL_DIV_2 << PHYREG6_PLL_DIV_SHIFT,
4227160820dSYifeng Zhao 					 PHYREG6);
4237160820dSYifeng Zhao 
4247160820dSYifeng Zhao 		writel(PHYREG18_PLL_LOOP, priv->mmio + PHYREG18);
4257160820dSYifeng Zhao 		writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
4267160820dSYifeng Zhao 
4277160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_sel_usb, true);
4287160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txcomp_sel, false);
4297160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txelec_sel, false);
4307160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->usb_mode_set, true);
4317160820dSYifeng Zhao 		break;
4327160820dSYifeng Zhao 
4337160820dSYifeng Zhao 	case PHY_TYPE_SATA:
4347160820dSYifeng Zhao 		/* Enable adaptive CTLE for SATA Rx. */
4357160820dSYifeng Zhao 		val = readl(priv->mmio + PHYREG15);
4367160820dSYifeng Zhao 		val |= PHYREG15_CTLE_EN;
4377160820dSYifeng Zhao 		writel(val, priv->mmio + PHYREG15);
4387160820dSYifeng Zhao 		/*
4397160820dSYifeng Zhao 		 * Set tx_rterm=50ohm and rx_rterm=44ohm for SATA.
4407160820dSYifeng Zhao 		 * 0: 60ohm, 8: 50ohm 15: 44ohm (by step abort 1ohm)
4417160820dSYifeng Zhao 		 */
4427160820dSYifeng Zhao 		val = PHYREG7_TX_RTERM_50OHM << PHYREG7_TX_RTERM_SHIFT;
4437160820dSYifeng Zhao 		val |= PHYREG7_RX_RTERM_44OHM << PHYREG7_RX_RTERM_SHIFT;
4447160820dSYifeng Zhao 		writel(val, priv->mmio + PHYREG7);
4457160820dSYifeng Zhao 
4467160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_sata, true);
4477160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_sata, true);
4487160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_sata, true);
4497160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_sata, true);
4507160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_con0_for_sata, true);
4517160820dSYifeng Zhao 		break;
4527160820dSYifeng Zhao 
4537160820dSYifeng Zhao 	case PHY_TYPE_SGMII:
4547160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_xpcs_phy_ready, true);
4557160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_phymode_sel, true);
4567160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_sel_qsgmii, true);
4577160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->sgmii_mode_set, true);
4587160820dSYifeng Zhao 		break;
4597160820dSYifeng Zhao 
4607160820dSYifeng Zhao 	case PHY_TYPE_QSGMII:
4617160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_xpcs_phy_ready, true);
4627160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_phymode_sel, true);
4637160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_rate_sel, true);
4647160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_sel_qsgmii, true);
4657160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->qsgmii_mode_set, true);
4667160820dSYifeng Zhao 		break;
4677160820dSYifeng Zhao 
4687160820dSYifeng Zhao 	default:
4697160820dSYifeng Zhao 		dev_err(priv->dev, "incompatible PHY type\n");
4707160820dSYifeng Zhao 		return -EINVAL;
4717160820dSYifeng Zhao 	}
4727160820dSYifeng Zhao 
4737160820dSYifeng Zhao 	rate = clk_get_rate(priv->refclk);
4747160820dSYifeng Zhao 
4757160820dSYifeng Zhao 	switch (rate) {
4767160820dSYifeng Zhao 	case REF_CLOCK_24MHz:
4777160820dSYifeng Zhao 		if (priv->type == PHY_TYPE_USB3 || priv->type == PHY_TYPE_SATA) {
4787160820dSYifeng Zhao 			/* Set ssc_cnt[9:0]=0101111101 & 31.5KHz. */
4797160820dSYifeng Zhao 			val = PHYREG15_SSC_CNT_VALUE << PHYREG15_SSC_CNT_SHIFT;
4807160820dSYifeng Zhao 			rockchip_combphy_updatel(priv, PHYREG15_SSC_CNT_MASK,
4817160820dSYifeng Zhao 						 val, PHYREG15);
4827160820dSYifeng Zhao 
4837160820dSYifeng Zhao 			writel(PHYREG16_SSC_CNT_VALUE, priv->mmio + PHYREG16);
4847160820dSYifeng Zhao 		}
4857160820dSYifeng Zhao 		break;
4867160820dSYifeng Zhao 
4877160820dSYifeng Zhao 	case REF_CLOCK_25MHz:
4887160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_25m, true);
4897160820dSYifeng Zhao 		break;
4907160820dSYifeng Zhao 
4917160820dSYifeng Zhao 	case REF_CLOCK_100MHz:
4927160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_100m, true);
4937160820dSYifeng Zhao 		if (priv->type == PHY_TYPE_PCIE) {
4947160820dSYifeng Zhao 			/* PLL KVCO  fine tuning. */
4957160820dSYifeng Zhao 			val = PHYREG33_PLL_KVCO_VALUE << PHYREG33_PLL_KVCO_SHIFT;
4967160820dSYifeng Zhao 			rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
4977160820dSYifeng Zhao 						 val, PHYREG33);
4987160820dSYifeng Zhao 
4997160820dSYifeng Zhao 			/* Enable controlling random jitter. */
5007160820dSYifeng Zhao 			writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
5017160820dSYifeng Zhao 
5027160820dSYifeng Zhao 			val = PHYREG6_PLL_DIV_2 << PHYREG6_PLL_DIV_SHIFT;
5037160820dSYifeng Zhao 			rockchip_combphy_updatel(priv, PHYREG6_PLL_DIV_MASK,
5047160820dSYifeng Zhao 						 val, PHYREG6);
5057160820dSYifeng Zhao 
5067160820dSYifeng Zhao 			writel(PHYREG18_PLL_LOOP, priv->mmio + PHYREG18);
5077160820dSYifeng Zhao 			writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
5087160820dSYifeng Zhao 		} else if (priv->type == PHY_TYPE_SATA) {
5097160820dSYifeng Zhao 			/* downward spread spectrum +500ppm */
5107160820dSYifeng Zhao 			val = PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT;
5117160820dSYifeng Zhao 			val |= PHYREG32_SSC_OFFSET_500PPM << PHYREG32_SSC_OFFSET_SHIFT;
5127160820dSYifeng Zhao 			rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK, val, PHYREG32);
5137160820dSYifeng Zhao 		}
5147160820dSYifeng Zhao 		break;
5157160820dSYifeng Zhao 
5167160820dSYifeng Zhao 	default:
5177160820dSYifeng Zhao 		dev_err(priv->dev, "unsupported rate: %lu\n", rate);
5187160820dSYifeng Zhao 		return -EINVAL;
5197160820dSYifeng Zhao 	}
5207160820dSYifeng Zhao 
5217160820dSYifeng Zhao 	if (priv->ext_refclk) {
5227160820dSYifeng Zhao 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_ext, true);
5237160820dSYifeng Zhao 		if (priv->type == PHY_TYPE_PCIE && rate == REF_CLOCK_100MHz) {
5247160820dSYifeng Zhao 			val = PHYREG13_RESISTER_HIGH_Z << PHYREG13_RESISTER_SHIFT;
5257160820dSYifeng Zhao 			val |= PHYREG13_CKRCV_AMP0;
5267160820dSYifeng Zhao 			rockchip_combphy_updatel(priv, PHYREG13_RESISTER_MASK, val, PHYREG13);
5277160820dSYifeng Zhao 
5287160820dSYifeng Zhao 			val = readl(priv->mmio + PHYREG14);
5297160820dSYifeng Zhao 			val |= PHYREG14_CKRCV_AMP1;
5307160820dSYifeng Zhao 			writel(val, priv->mmio + PHYREG14);
5317160820dSYifeng Zhao 		}
5327160820dSYifeng Zhao 	}
5337160820dSYifeng Zhao 
5347160820dSYifeng Zhao 	if (priv->enable_ssc) {
5357160820dSYifeng Zhao 		val = readl(priv->mmio + PHYREG8);
5367160820dSYifeng Zhao 		val |= PHYREG8_SSC_EN;
5377160820dSYifeng Zhao 		writel(val, priv->mmio + PHYREG8);
5387160820dSYifeng Zhao 	}
5397160820dSYifeng Zhao 
5407160820dSYifeng Zhao 	return 0;
5417160820dSYifeng Zhao }
5427160820dSYifeng Zhao 
5437160820dSYifeng Zhao static const struct rockchip_combphy_grfcfg rk3568_combphy_grfcfgs = {
5447160820dSYifeng Zhao 	/* pipe-phy-grf */
5457160820dSYifeng Zhao 	.pcie_mode_set		= { 0x0000, 5, 0, 0x00, 0x11 },
5467160820dSYifeng Zhao 	.usb_mode_set		= { 0x0000, 5, 0, 0x00, 0x04 },
5477160820dSYifeng Zhao 	.sgmii_mode_set		= { 0x0000, 5, 0, 0x00, 0x01 },
5487160820dSYifeng Zhao 	.qsgmii_mode_set	= { 0x0000, 5, 0, 0x00, 0x21 },
5497160820dSYifeng Zhao 	.pipe_rxterm_set	= { 0x0000, 12, 12, 0x00, 0x01 },
5507160820dSYifeng Zhao 	.pipe_txelec_set	= { 0x0004, 1, 1, 0x00, 0x01 },
5517160820dSYifeng Zhao 	.pipe_txcomp_set	= { 0x0004, 4, 4, 0x00, 0x01 },
5527160820dSYifeng Zhao 	.pipe_clk_25m		= { 0x0004, 14, 13, 0x00, 0x01 },
5537160820dSYifeng Zhao 	.pipe_clk_100m		= { 0x0004, 14, 13, 0x00, 0x02 },
5547160820dSYifeng Zhao 	.pipe_phymode_sel	= { 0x0008, 1, 1, 0x00, 0x01 },
5557160820dSYifeng Zhao 	.pipe_rate_sel		= { 0x0008, 2, 2, 0x00, 0x01 },
5567160820dSYifeng Zhao 	.pipe_rxterm_sel	= { 0x0008, 8, 8, 0x00, 0x01 },
5577160820dSYifeng Zhao 	.pipe_txelec_sel	= { 0x0008, 12, 12, 0x00, 0x01 },
5587160820dSYifeng Zhao 	.pipe_txcomp_sel	= { 0x0008, 15, 15, 0x00, 0x01 },
5597160820dSYifeng Zhao 	.pipe_clk_ext		= { 0x000c, 9, 8, 0x02, 0x01 },
5607160820dSYifeng Zhao 	.pipe_sel_usb		= { 0x000c, 14, 13, 0x00, 0x01 },
5617160820dSYifeng Zhao 	.pipe_sel_qsgmii	= { 0x000c, 15, 13, 0x00, 0x07 },
5627160820dSYifeng Zhao 	.pipe_phy_status	= { 0x0034, 6, 6, 0x01, 0x00 },
5637160820dSYifeng Zhao 	.con0_for_pcie		= { 0x0000, 15, 0, 0x00, 0x1000 },
5647160820dSYifeng Zhao 	.con1_for_pcie		= { 0x0004, 15, 0, 0x00, 0x0000 },
5657160820dSYifeng Zhao 	.con2_for_pcie		= { 0x0008, 15, 0, 0x00, 0x0101 },
5667160820dSYifeng Zhao 	.con3_for_pcie		= { 0x000c, 15, 0, 0x00, 0x0200 },
5677160820dSYifeng Zhao 	.con0_for_sata		= { 0x0000, 15, 0, 0x00, 0x0119 },
5687160820dSYifeng Zhao 	.con1_for_sata		= { 0x0004, 15, 0, 0x00, 0x0040 },
5697160820dSYifeng Zhao 	.con2_for_sata		= { 0x0008, 15, 0, 0x00, 0x80c3 },
5707160820dSYifeng Zhao 	.con3_for_sata		= { 0x000c, 15, 0, 0x00, 0x4407 },
5717160820dSYifeng Zhao 	/* pipe-grf */
5727160820dSYifeng Zhao 	.pipe_con0_for_sata	= { 0x0000, 15, 0, 0x00, 0x2220 },
5737160820dSYifeng Zhao 	.pipe_xpcs_phy_ready	= { 0x0040, 2, 2, 0x00, 0x01 },
5747160820dSYifeng Zhao };
5757160820dSYifeng Zhao 
5767160820dSYifeng Zhao static const struct rockchip_combphy_cfg rk3568_combphy_cfgs = {
577d16d4002SSebastian Reichel 	.num_phys = 3,
578d16d4002SSebastian Reichel 	.phy_ids = {
579d16d4002SSebastian Reichel 		0xfe820000,
580d16d4002SSebastian Reichel 		0xfe830000,
581d16d4002SSebastian Reichel 		0xfe840000,
582d16d4002SSebastian Reichel 	},
5837160820dSYifeng Zhao 	.grfcfg		= &rk3568_combphy_grfcfgs,
5847160820dSYifeng Zhao 	.combphy_cfg	= rk3568_combphy_cfg,
5857160820dSYifeng Zhao };
5867160820dSYifeng Zhao 
rk3588_combphy_cfg(struct rockchip_combphy_priv * priv)587a03c4427SLucas Tanure static int rk3588_combphy_cfg(struct rockchip_combphy_priv *priv)
588a03c4427SLucas Tanure {
589a03c4427SLucas Tanure 	const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
590a03c4427SLucas Tanure 	unsigned long rate;
591a03c4427SLucas Tanure 	u32 val;
592a03c4427SLucas Tanure 
593a03c4427SLucas Tanure 	switch (priv->type) {
594a03c4427SLucas Tanure 	case PHY_TYPE_PCIE:
595a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_pcie, true);
596a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_pcie, true);
597a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_pcie, true);
598a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_pcie, true);
599d16d4002SSebastian Reichel 		switch (priv->id) {
600d16d4002SSebastian Reichel 		case 1:
601a03c4427SLucas Tanure 			rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_pcie1l0_sel, true);
602d16d4002SSebastian Reichel 			break;
603d16d4002SSebastian Reichel 		case 2:
604a03c4427SLucas Tanure 			rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_pcie1l1_sel, true);
605a03c4427SLucas Tanure 			break;
606d16d4002SSebastian Reichel 		}
607d16d4002SSebastian Reichel 		break;
608a03c4427SLucas Tanure 	case PHY_TYPE_USB3:
609a03c4427SLucas Tanure 		/* Set SSC downward spread spectrum */
610a03c4427SLucas Tanure 		rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK,
611a03c4427SLucas Tanure 					 PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT,
612a03c4427SLucas Tanure 					 PHYREG32);
613a03c4427SLucas Tanure 
614a03c4427SLucas Tanure 		/* Enable adaptive CTLE for USB3.0 Rx. */
615a03c4427SLucas Tanure 		val = readl(priv->mmio + PHYREG15);
616a03c4427SLucas Tanure 		val |= PHYREG15_CTLE_EN;
617a03c4427SLucas Tanure 		writel(val, priv->mmio + PHYREG15);
618a03c4427SLucas Tanure 
619a03c4427SLucas Tanure 		/* Set PLL KVCO fine tuning signals. */
620a03c4427SLucas Tanure 		rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
621a03c4427SLucas Tanure 					 PHYREG33_PLL_KVCO_VALUE << PHYREG33_PLL_KVCO_SHIFT,
622a03c4427SLucas Tanure 					 PHYREG33);
623a03c4427SLucas Tanure 
624a03c4427SLucas Tanure 		/* Enable controlling random jitter. */
625a03c4427SLucas Tanure 		writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
626a03c4427SLucas Tanure 
627a03c4427SLucas Tanure 		/* Set PLL input clock divider 1/2. */
628a03c4427SLucas Tanure 		rockchip_combphy_updatel(priv, PHYREG6_PLL_DIV_MASK,
629a03c4427SLucas Tanure 					 PHYREG6_PLL_DIV_2 << PHYREG6_PLL_DIV_SHIFT,
630a03c4427SLucas Tanure 					 PHYREG6);
631a03c4427SLucas Tanure 
632a03c4427SLucas Tanure 		writel(PHYREG18_PLL_LOOP, priv->mmio + PHYREG18);
633a03c4427SLucas Tanure 		writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
634a03c4427SLucas Tanure 
635a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txcomp_sel, false);
636a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txelec_sel, false);
637a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->usb_mode_set, true);
638a03c4427SLucas Tanure 		break;
639a03c4427SLucas Tanure 	case PHY_TYPE_SATA:
640a03c4427SLucas Tanure 		/* Enable adaptive CTLE for SATA Rx. */
641a03c4427SLucas Tanure 		val = readl(priv->mmio + PHYREG15);
642a03c4427SLucas Tanure 		val |= PHYREG15_CTLE_EN;
643a03c4427SLucas Tanure 		writel(val, priv->mmio + PHYREG15);
644a03c4427SLucas Tanure 		/*
645a03c4427SLucas Tanure 		 * Set tx_rterm=50ohm and rx_rterm=44ohm for SATA.
646a03c4427SLucas Tanure 		 * 0: 60ohm, 8: 50ohm 15: 44ohm (by step abort 1ohm)
647a03c4427SLucas Tanure 		 */
648a03c4427SLucas Tanure 		val = PHYREG7_TX_RTERM_50OHM << PHYREG7_TX_RTERM_SHIFT;
649a03c4427SLucas Tanure 		val |= PHYREG7_RX_RTERM_44OHM << PHYREG7_RX_RTERM_SHIFT;
650a03c4427SLucas Tanure 		writel(val, priv->mmio + PHYREG7);
651a03c4427SLucas Tanure 
652a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_sata, true);
653a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_sata, true);
654a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_sata, true);
655a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_sata, true);
656a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_con0_for_sata, true);
657a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_con1_for_sata, true);
658a03c4427SLucas Tanure 		break;
659a03c4427SLucas Tanure 	case PHY_TYPE_SGMII:
660a03c4427SLucas Tanure 	case PHY_TYPE_QSGMII:
661a03c4427SLucas Tanure 	default:
662a03c4427SLucas Tanure 		dev_err(priv->dev, "incompatible PHY type\n");
663a03c4427SLucas Tanure 		return -EINVAL;
664a03c4427SLucas Tanure 	}
665a03c4427SLucas Tanure 
666a03c4427SLucas Tanure 	rate = clk_get_rate(priv->refclk);
667a03c4427SLucas Tanure 
668a03c4427SLucas Tanure 	switch (rate) {
669a03c4427SLucas Tanure 	case REF_CLOCK_24MHz:
670a03c4427SLucas Tanure 		if (priv->type == PHY_TYPE_USB3 || priv->type == PHY_TYPE_SATA) {
671a03c4427SLucas Tanure 			/* Set ssc_cnt[9:0]=0101111101 & 31.5KHz. */
672a03c4427SLucas Tanure 			val = PHYREG15_SSC_CNT_VALUE << PHYREG15_SSC_CNT_SHIFT;
673a03c4427SLucas Tanure 			rockchip_combphy_updatel(priv, PHYREG15_SSC_CNT_MASK,
674a03c4427SLucas Tanure 						 val, PHYREG15);
675a03c4427SLucas Tanure 
676a03c4427SLucas Tanure 			writel(PHYREG16_SSC_CNT_VALUE, priv->mmio + PHYREG16);
677a03c4427SLucas Tanure 		}
678a03c4427SLucas Tanure 		break;
679a03c4427SLucas Tanure 
680a03c4427SLucas Tanure 	case REF_CLOCK_25MHz:
681a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_25m, true);
682a03c4427SLucas Tanure 		break;
683a03c4427SLucas Tanure 	case REF_CLOCK_100MHz:
684a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_100m, true);
685a03c4427SLucas Tanure 		if (priv->type == PHY_TYPE_PCIE) {
686a03c4427SLucas Tanure 			/* PLL KVCO fine tuning. */
687a03c4427SLucas Tanure 			val = 4 << PHYREG33_PLL_KVCO_SHIFT;
688a03c4427SLucas Tanure 			rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
689a03c4427SLucas Tanure 						 val, PHYREG33);
690a03c4427SLucas Tanure 
691a03c4427SLucas Tanure 			/* Enable controlling random jitter. */
692a03c4427SLucas Tanure 			writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
693a03c4427SLucas Tanure 
694a03c4427SLucas Tanure 			/* Set up rx_trim: PLL LPF C1 85pf R1 1.25kohm */
695a03c4427SLucas Tanure 			writel(PHYREG27_RX_TRIM_RK3588, priv->mmio + PHYREG27);
696a03c4427SLucas Tanure 
697a03c4427SLucas Tanure 			/* Set up su_trim:  */
698a03c4427SLucas Tanure 			writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
699a03c4427SLucas Tanure 		} else if (priv->type == PHY_TYPE_SATA) {
700a03c4427SLucas Tanure 			/* downward spread spectrum +500ppm */
701a03c4427SLucas Tanure 			val = PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT;
702a03c4427SLucas Tanure 			val |= PHYREG32_SSC_OFFSET_500PPM << PHYREG32_SSC_OFFSET_SHIFT;
703a03c4427SLucas Tanure 			rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK, val, PHYREG32);
704a03c4427SLucas Tanure 		}
705a03c4427SLucas Tanure 		break;
706a03c4427SLucas Tanure 	default:
707a03c4427SLucas Tanure 		dev_err(priv->dev, "Unsupported rate: %lu\n", rate);
708a03c4427SLucas Tanure 		return -EINVAL;
709a03c4427SLucas Tanure 	}
710a03c4427SLucas Tanure 
711a03c4427SLucas Tanure 	if (priv->ext_refclk) {
712a03c4427SLucas Tanure 		rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_ext, true);
713a03c4427SLucas Tanure 		if (priv->type == PHY_TYPE_PCIE && rate == REF_CLOCK_100MHz) {
714a03c4427SLucas Tanure 			val = PHYREG13_RESISTER_HIGH_Z << PHYREG13_RESISTER_SHIFT;
715a03c4427SLucas Tanure 			val |= PHYREG13_CKRCV_AMP0;
716a03c4427SLucas Tanure 			rockchip_combphy_updatel(priv, PHYREG13_RESISTER_MASK, val, PHYREG13);
717a03c4427SLucas Tanure 
718a03c4427SLucas Tanure 			val = readl(priv->mmio + PHYREG14);
719a03c4427SLucas Tanure 			val |= PHYREG14_CKRCV_AMP1;
720a03c4427SLucas Tanure 			writel(val, priv->mmio + PHYREG14);
721a03c4427SLucas Tanure 		}
722a03c4427SLucas Tanure 	}
723a03c4427SLucas Tanure 
724a03c4427SLucas Tanure 	if (priv->enable_ssc) {
725a03c4427SLucas Tanure 		val = readl(priv->mmio + PHYREG8);
726a03c4427SLucas Tanure 		val |= PHYREG8_SSC_EN;
727a03c4427SLucas Tanure 		writel(val, priv->mmio + PHYREG8);
728a03c4427SLucas Tanure 	}
729a03c4427SLucas Tanure 
730a03c4427SLucas Tanure 	return 0;
731a03c4427SLucas Tanure }
732a03c4427SLucas Tanure 
733a03c4427SLucas Tanure static const struct rockchip_combphy_grfcfg rk3588_combphy_grfcfgs = {
734a03c4427SLucas Tanure 	/* pipe-phy-grf */
735a03c4427SLucas Tanure 	.pcie_mode_set		= { 0x0000, 5, 0, 0x00, 0x11 },
736a03c4427SLucas Tanure 	.usb_mode_set		= { 0x0000, 5, 0, 0x00, 0x04 },
737a03c4427SLucas Tanure 	.pipe_rxterm_set	= { 0x0000, 12, 12, 0x00, 0x01 },
738a03c4427SLucas Tanure 	.pipe_txelec_set	= { 0x0004, 1, 1, 0x00, 0x01 },
739a03c4427SLucas Tanure 	.pipe_txcomp_set	= { 0x0004, 4, 4, 0x00, 0x01 },
740a03c4427SLucas Tanure 	.pipe_clk_25m		= { 0x0004, 14, 13, 0x00, 0x01 },
741a03c4427SLucas Tanure 	.pipe_clk_100m		= { 0x0004, 14, 13, 0x00, 0x02 },
742a03c4427SLucas Tanure 	.pipe_rxterm_sel	= { 0x0008, 8, 8, 0x00, 0x01 },
743a03c4427SLucas Tanure 	.pipe_txelec_sel	= { 0x0008, 12, 12, 0x00, 0x01 },
744a03c4427SLucas Tanure 	.pipe_txcomp_sel	= { 0x0008, 15, 15, 0x00, 0x01 },
745a03c4427SLucas Tanure 	.pipe_clk_ext		= { 0x000c, 9, 8, 0x02, 0x01 },
746a03c4427SLucas Tanure 	.pipe_phy_status	= { 0x0034, 6, 6, 0x01, 0x00 },
747a03c4427SLucas Tanure 	.con0_for_pcie		= { 0x0000, 15, 0, 0x00, 0x1000 },
748a03c4427SLucas Tanure 	.con1_for_pcie		= { 0x0004, 15, 0, 0x00, 0x0000 },
749a03c4427SLucas Tanure 	.con2_for_pcie		= { 0x0008, 15, 0, 0x00, 0x0101 },
750a03c4427SLucas Tanure 	.con3_for_pcie		= { 0x000c, 15, 0, 0x00, 0x0200 },
751a03c4427SLucas Tanure 	.con0_for_sata		= { 0x0000, 15, 0, 0x00, 0x0129 },
752a03c4427SLucas Tanure 	.con1_for_sata		= { 0x0004, 15, 0, 0x00, 0x0000 },
753a03c4427SLucas Tanure 	.con2_for_sata		= { 0x0008, 15, 0, 0x00, 0x80c1 },
754a03c4427SLucas Tanure 	.con3_for_sata		= { 0x000c, 15, 0, 0x00, 0x0407 },
755a03c4427SLucas Tanure 	/* pipe-grf */
756a03c4427SLucas Tanure 	.pipe_con0_for_sata	= { 0x0000, 11, 5, 0x00, 0x22 },
757a03c4427SLucas Tanure 	.pipe_con1_for_sata	= { 0x0000, 2, 0, 0x00, 0x2 },
758a03c4427SLucas Tanure 	.pipe_pcie1l0_sel	= { 0x0100, 0, 0, 0x01, 0x0 },
759a03c4427SLucas Tanure 	.pipe_pcie1l1_sel	= { 0x0100, 1, 1, 0x01, 0x0 },
760a03c4427SLucas Tanure };
761a03c4427SLucas Tanure 
762a03c4427SLucas Tanure static const struct rockchip_combphy_cfg rk3588_combphy_cfgs = {
763d16d4002SSebastian Reichel 	.num_phys = 3,
764d16d4002SSebastian Reichel 	.phy_ids = {
765d16d4002SSebastian Reichel 		0xfee00000,
766d16d4002SSebastian Reichel 		0xfee10000,
767d16d4002SSebastian Reichel 		0xfee20000,
768d16d4002SSebastian Reichel 	},
769a03c4427SLucas Tanure 	.grfcfg		= &rk3588_combphy_grfcfgs,
770a03c4427SLucas Tanure 	.combphy_cfg	= rk3588_combphy_cfg,
771a03c4427SLucas Tanure };
772a03c4427SLucas Tanure 
7737160820dSYifeng Zhao static const struct of_device_id rockchip_combphy_of_match[] = {
7747160820dSYifeng Zhao 	{
7757160820dSYifeng Zhao 		.compatible = "rockchip,rk3568-naneng-combphy",
7767160820dSYifeng Zhao 		.data = &rk3568_combphy_cfgs,
7777160820dSYifeng Zhao 	},
778a03c4427SLucas Tanure 	{
779a03c4427SLucas Tanure 		.compatible = "rockchip,rk3588-naneng-combphy",
780a03c4427SLucas Tanure 		.data = &rk3588_combphy_cfgs,
781a03c4427SLucas Tanure 	},
7827160820dSYifeng Zhao 	{ },
7837160820dSYifeng Zhao };
7847160820dSYifeng Zhao MODULE_DEVICE_TABLE(of, rockchip_combphy_of_match);
7857160820dSYifeng Zhao 
7867160820dSYifeng Zhao static struct platform_driver rockchip_combphy_driver = {
7877160820dSYifeng Zhao 	.probe	= rockchip_combphy_probe,
7887160820dSYifeng Zhao 	.driver = {
7897160820dSYifeng Zhao 		.name = "rockchip-naneng-combphy",
7907160820dSYifeng Zhao 		.of_match_table = rockchip_combphy_of_match,
7917160820dSYifeng Zhao 	},
7927160820dSYifeng Zhao };
7937160820dSYifeng Zhao module_platform_driver(rockchip_combphy_driver);
7947160820dSYifeng Zhao 
7957160820dSYifeng Zhao MODULE_DESCRIPTION("Rockchip NANENG COMBPHY driver");
7967160820dSYifeng Zhao MODULE_LICENSE("GPL v2");
797