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