162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * StarFive JH7110 DPHY RX driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2023 StarFive Technology Co., Ltd. 662306a36Sopenharmony_ci * Author: Jack Zhu <jack.zhu@starfivetech.com> 762306a36Sopenharmony_ci * Author: Changhuang Liang <changhuang.liang@starfivetech.com> 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/bitfield.h> 1162306a36Sopenharmony_ci#include <linux/bitops.h> 1262306a36Sopenharmony_ci#include <linux/clk.h> 1362306a36Sopenharmony_ci#include <linux/io.h> 1462306a36Sopenharmony_ci#include <linux/module.h> 1562306a36Sopenharmony_ci#include <linux/of.h> 1662306a36Sopenharmony_ci#include <linux/phy/phy.h> 1762306a36Sopenharmony_ci#include <linux/platform_device.h> 1862306a36Sopenharmony_ci#include <linux/pm_runtime.h> 1962306a36Sopenharmony_ci#include <linux/reset.h> 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#define STF_DPHY_APBCFGSAIF_SYSCFG(x) (x) 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#define STF_DPHY_ENABLE_CLK BIT(6) 2462306a36Sopenharmony_ci#define STF_DPHY_ENABLE_CLK1 BIT(7) 2562306a36Sopenharmony_ci#define STF_DPHY_ENABLE_LAN0 BIT(8) 2662306a36Sopenharmony_ci#define STF_DPHY_ENABLE_LAN1 BIT(9) 2762306a36Sopenharmony_ci#define STF_DPHY_ENABLE_LAN2 BIT(10) 2862306a36Sopenharmony_ci#define STF_DPHY_ENABLE_LAN3 BIT(11) 2962306a36Sopenharmony_ci#define STF_DPHY_LANE_SWAP_CLK GENMASK(22, 20) 3062306a36Sopenharmony_ci#define STF_DPHY_LANE_SWAP_CLK1 GENMASK(25, 23) 3162306a36Sopenharmony_ci#define STF_DPHY_LANE_SWAP_LAN0 GENMASK(28, 26) 3262306a36Sopenharmony_ci#define STF_DPHY_LANE_SWAP_LAN1 GENMASK(31, 29) 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#define STF_DPHY_LANE_SWAP_LAN2 GENMASK(2, 0) 3562306a36Sopenharmony_ci#define STF_DPHY_LANE_SWAP_LAN3 GENMASK(5, 3) 3662306a36Sopenharmony_ci#define STF_DPHY_PLL_CLK_SEL GENMASK(21, 12) 3762306a36Sopenharmony_ci#define STF_DPHY_PRECOUNTER_IN_CLK GENMASK(29, 22) 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci#define STF_DPHY_PRECOUNTER_IN_CLK1 GENMASK(7, 0) 4062306a36Sopenharmony_ci#define STF_DPHY_PRECOUNTER_IN_LAN0 GENMASK(15, 8) 4162306a36Sopenharmony_ci#define STF_DPHY_PRECOUNTER_IN_LAN1 GENMASK(23, 16) 4262306a36Sopenharmony_ci#define STF_DPHY_PRECOUNTER_IN_LAN2 GENMASK(31, 24) 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#define STF_DPHY_PRECOUNTER_IN_LAN3 GENMASK(7, 0) 4562306a36Sopenharmony_ci#define STF_DPHY_RX_1C2C_SEL BIT(8) 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci#define STF_MAP_LANES_NUM 6 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistruct regval { 5062306a36Sopenharmony_ci u32 addr; 5162306a36Sopenharmony_ci u32 val; 5262306a36Sopenharmony_ci}; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistruct stf_dphy_info { 5562306a36Sopenharmony_ci /** 5662306a36Sopenharmony_ci * @maps: 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * Physical lanes and logic lanes mapping table. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * The default order is: 6162306a36Sopenharmony_ci * [clk lane0, data lane 0, data lane 1, data lane 2, date lane 3, clk lane 1] 6262306a36Sopenharmony_ci */ 6362306a36Sopenharmony_ci u8 maps[STF_MAP_LANES_NUM]; 6462306a36Sopenharmony_ci}; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistruct stf_dphy { 6762306a36Sopenharmony_ci struct device *dev; 6862306a36Sopenharmony_ci void __iomem *regs; 6962306a36Sopenharmony_ci struct clk *cfg_clk; 7062306a36Sopenharmony_ci struct clk *ref_clk; 7162306a36Sopenharmony_ci struct clk *tx_clk; 7262306a36Sopenharmony_ci struct reset_control *rstc; 7362306a36Sopenharmony_ci struct regulator *mipi_0p9; 7462306a36Sopenharmony_ci struct phy *phy; 7562306a36Sopenharmony_ci const struct stf_dphy_info *info; 7662306a36Sopenharmony_ci}; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cistatic int stf_dphy_configure(struct phy *phy, union phy_configure_opts *opts) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci struct stf_dphy *dphy = phy_get_drvdata(phy); 8162306a36Sopenharmony_ci const struct stf_dphy_info *info = dphy->info; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci writel(FIELD_PREP(STF_DPHY_ENABLE_CLK, 1) | 8462306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_ENABLE_CLK1, 1) | 8562306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_ENABLE_LAN0, 1) | 8662306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_ENABLE_LAN1, 1) | 8762306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_ENABLE_LAN2, 1) | 8862306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_ENABLE_LAN3, 1) | 8962306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_LANE_SWAP_CLK, info->maps[0]) | 9062306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_LANE_SWAP_CLK1, info->maps[5]) | 9162306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_LANE_SWAP_LAN0, info->maps[1]) | 9262306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_LANE_SWAP_LAN1, info->maps[2]), 9362306a36Sopenharmony_ci dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(188)); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci writel(FIELD_PREP(STF_DPHY_LANE_SWAP_LAN2, info->maps[3]) | 9662306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_LANE_SWAP_LAN3, info->maps[4]) | 9762306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_PRECOUNTER_IN_CLK, 8), 9862306a36Sopenharmony_ci dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(192)); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci writel(FIELD_PREP(STF_DPHY_PRECOUNTER_IN_CLK1, 8) | 10162306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN0, 7) | 10262306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN1, 7) | 10362306a36Sopenharmony_ci FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN2, 7), 10462306a36Sopenharmony_ci dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(196)); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci writel(FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN3, 7), 10762306a36Sopenharmony_ci dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(200)); 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci return 0; 11062306a36Sopenharmony_ci} 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_cistatic int stf_dphy_power_on(struct phy *phy) 11362306a36Sopenharmony_ci{ 11462306a36Sopenharmony_ci struct stf_dphy *dphy = phy_get_drvdata(phy); 11562306a36Sopenharmony_ci int ret; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci ret = pm_runtime_resume_and_get(dphy->dev); 11862306a36Sopenharmony_ci if (ret < 0) 11962306a36Sopenharmony_ci return ret; 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci ret = regulator_enable(dphy->mipi_0p9); 12262306a36Sopenharmony_ci if (ret) { 12362306a36Sopenharmony_ci pm_runtime_put(dphy->dev); 12462306a36Sopenharmony_ci return ret; 12562306a36Sopenharmony_ci } 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci clk_set_rate(dphy->cfg_clk, 99000000); 12862306a36Sopenharmony_ci clk_set_rate(dphy->ref_clk, 49500000); 12962306a36Sopenharmony_ci clk_set_rate(dphy->tx_clk, 19800000); 13062306a36Sopenharmony_ci reset_control_deassert(dphy->rstc); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci return 0; 13362306a36Sopenharmony_ci} 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_cistatic int stf_dphy_power_off(struct phy *phy) 13662306a36Sopenharmony_ci{ 13762306a36Sopenharmony_ci struct stf_dphy *dphy = phy_get_drvdata(phy); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci reset_control_assert(dphy->rstc); 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci regulator_disable(dphy->mipi_0p9); 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci pm_runtime_put_sync(dphy->dev); 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci return 0; 14662306a36Sopenharmony_ci} 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_cistatic const struct phy_ops stf_dphy_ops = { 14962306a36Sopenharmony_ci .configure = stf_dphy_configure, 15062306a36Sopenharmony_ci .power_on = stf_dphy_power_on, 15162306a36Sopenharmony_ci .power_off = stf_dphy_power_off, 15262306a36Sopenharmony_ci}; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_cistatic int stf_dphy_probe(struct platform_device *pdev) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci struct phy_provider *phy_provider; 15762306a36Sopenharmony_ci struct stf_dphy *dphy; 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL); 16062306a36Sopenharmony_ci if (!dphy) 16162306a36Sopenharmony_ci return -ENOMEM; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci dphy->info = of_device_get_match_data(&pdev->dev); 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci dev_set_drvdata(&pdev->dev, dphy); 16662306a36Sopenharmony_ci dphy->dev = &pdev->dev; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci dphy->regs = devm_platform_ioremap_resource(pdev, 0); 16962306a36Sopenharmony_ci if (IS_ERR(dphy->regs)) 17062306a36Sopenharmony_ci return PTR_ERR(dphy->regs); 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci dphy->cfg_clk = devm_clk_get(&pdev->dev, "cfg"); 17362306a36Sopenharmony_ci if (IS_ERR(dphy->cfg_clk)) 17462306a36Sopenharmony_ci return PTR_ERR(dphy->cfg_clk); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci dphy->ref_clk = devm_clk_get(&pdev->dev, "ref"); 17762306a36Sopenharmony_ci if (IS_ERR(dphy->ref_clk)) 17862306a36Sopenharmony_ci return PTR_ERR(dphy->ref_clk); 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci dphy->tx_clk = devm_clk_get(&pdev->dev, "tx"); 18162306a36Sopenharmony_ci if (IS_ERR(dphy->tx_clk)) 18262306a36Sopenharmony_ci return PTR_ERR(dphy->tx_clk); 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci dphy->rstc = devm_reset_control_array_get_exclusive(&pdev->dev); 18562306a36Sopenharmony_ci if (IS_ERR(dphy->rstc)) 18662306a36Sopenharmony_ci return PTR_ERR(dphy->rstc); 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci dphy->mipi_0p9 = devm_regulator_get(&pdev->dev, "mipi_0p9"); 18962306a36Sopenharmony_ci if (IS_ERR(dphy->mipi_0p9)) 19062306a36Sopenharmony_ci return PTR_ERR(dphy->mipi_0p9); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci dphy->phy = devm_phy_create(&pdev->dev, NULL, &stf_dphy_ops); 19362306a36Sopenharmony_ci if (IS_ERR(dphy->phy)) { 19462306a36Sopenharmony_ci dev_err(&pdev->dev, "Failed to create PHY\n"); 19562306a36Sopenharmony_ci return PTR_ERR(dphy->phy); 19662306a36Sopenharmony_ci } 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci pm_runtime_enable(&pdev->dev); 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci phy_set_drvdata(dphy->phy, dphy); 20162306a36Sopenharmony_ci phy_provider = devm_of_phy_provider_register(&pdev->dev, 20262306a36Sopenharmony_ci of_phy_simple_xlate); 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci return PTR_ERR_OR_ZERO(phy_provider); 20562306a36Sopenharmony_ci} 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_cistatic const struct stf_dphy_info starfive_dphy_info = { 20862306a36Sopenharmony_ci .maps = {4, 0, 1, 2, 3, 5}, 20962306a36Sopenharmony_ci}; 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_cistatic const struct of_device_id stf_dphy_dt_ids[] = { 21262306a36Sopenharmony_ci { 21362306a36Sopenharmony_ci .compatible = "starfive,jh7110-dphy-rx", 21462306a36Sopenharmony_ci .data = &starfive_dphy_info, 21562306a36Sopenharmony_ci }, 21662306a36Sopenharmony_ci { /* sentinel */ }, 21762306a36Sopenharmony_ci}; 21862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, stf_dphy_dt_ids); 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_cistatic struct platform_driver stf_dphy_driver = { 22162306a36Sopenharmony_ci .probe = stf_dphy_probe, 22262306a36Sopenharmony_ci .driver = { 22362306a36Sopenharmony_ci .name = "starfive-dphy-rx", 22462306a36Sopenharmony_ci .of_match_table = stf_dphy_dt_ids, 22562306a36Sopenharmony_ci }, 22662306a36Sopenharmony_ci}; 22762306a36Sopenharmony_cimodule_platform_driver(stf_dphy_driver); 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ciMODULE_AUTHOR("Jack Zhu <jack.zhu@starfivetech.com>"); 23062306a36Sopenharmony_ciMODULE_AUTHOR("Changhuang Liang <changhuang.liang@starfivetech.com>"); 23162306a36Sopenharmony_ciMODULE_DESCRIPTION("StarFive JH7110 DPHY RX driver"); 23262306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 233