1 From c27b091519da0c889fa452e98d44cf8fb431b961 Mon Sep 17 00:00:00 2001
2 From: Minda Chen <minda.chen@starfivetech.com>
3 Date: Thu, 18 May 2023 19:27:47 +0800
4 Subject: [PATCH 103/129] phy: starfive: Add JH7110 PCIE 2.0 PHY driver
6 Add Starfive JH7110 SoC PCIe 2.0 PHY driver support.
7 PCIe 2.0 PHY default connect to PCIe controller.
8 PCIe PHY can connect to USB 3.0 controller.
10 Signed-off-by: Minda Chen <minda.chen@starfivetech.com>
12 drivers/phy/starfive/Kconfig | 10 ++
13 drivers/phy/starfive/Makefile | 1 +
14 drivers/phy/starfive/phy-jh7110-pcie.c | 204 +++++++++++++++++++++++++
15 3 files changed, 215 insertions(+)
16 create mode 100644 drivers/phy/starfive/phy-jh7110-pcie.c
18 diff --git a/drivers/phy/starfive/Kconfig b/drivers/phy/starfive/Kconfig
19 index 2c013c390..38eb0c0c8 100644
20 --- a/drivers/phy/starfive/Kconfig
21 +++ b/drivers/phy/starfive/Kconfig
22 @@ -12,6 +12,16 @@ config PHY_STARFIVE_DPHY_RX
23 system. If M is selected, the module will be called
26 +config PHY_STARFIVE_JH7110_PCIE
27 + tristate "Starfive JH7110 PCIE 2.0/USB 3.0 PHY support"
31 + Enable this to support the StarFive PCIe 2.0 PHY,
32 + or used as USB 3.0 PHY.
33 + If M is selected, the module will be called
36 config PHY_STARFIVE_JH7110_USB
37 tristate "Starfive JH7110 USB 2.0 PHY support"
38 depends on USB_SUPPORT
39 diff --git a/drivers/phy/starfive/Makefile b/drivers/phy/starfive/Makefile
40 index 176443852..03a55aad5 100644
41 --- a/drivers/phy/starfive/Makefile
42 +++ b/drivers/phy/starfive/Makefile
44 # SPDX-License-Identifier: GPL-2.0
45 obj-$(CONFIG_PHY_STARFIVE_DPHY_RX) += phy-starfive-dphy-rx.o
46 +obj-$(CONFIG_PHY_STARFIVE_JH7110_PCIE) += phy-jh7110-pcie.o
47 obj-$(CONFIG_PHY_STARFIVE_JH7110_USB) += phy-jh7110-usb.o
48 diff --git a/drivers/phy/starfive/phy-jh7110-pcie.c b/drivers/phy/starfive/phy-jh7110-pcie.c
50 index 000000000..cbe79c1f5
52 +++ b/drivers/phy/starfive/phy-jh7110-pcie.c
54 +// SPDX-License-Identifier: GPL-2.0+
56 + * StarFive JH7110 PCIe 2.0 PHY driver
58 + * Copyright (C) 2023 StarFive Technology Co., Ltd.
59 + * Author: Minda Chen <minda.chen@starfivetech.com>
62 +#include <linux/bits.h>
63 +#include <linux/clk.h>
64 +#include <linux/err.h>
65 +#include <linux/io.h>
66 +#include <linux/module.h>
67 +#include <linux/mfd/syscon.h>
68 +#include <linux/phy/phy.h>
69 +#include <linux/platform_device.h>
70 +#include <linux/regmap.h>
72 +#define PCIE_KVCO_LEVEL_OFF 0x28
73 +#define PCIE_USB3_PHY_PLL_CTL_OFF 0x7c
74 +#define PCIE_KVCO_TUNE_SIGNAL_OFF 0x80
75 +#define PCIE_USB3_PHY_ENABLE BIT(4)
76 +#define PHY_KVCO_FINE_TUNE_LEVEL 0x91
77 +#define PHY_KVCO_FINE_TUNE_SIGNALS 0xc
79 +#define USB_PDRSTN_SPLIT BIT(17)
81 +#define PCIE_PHY_MODE BIT(20)
82 +#define PCIE_PHY_MODE_MASK GENMASK(21, 20)
83 +#define PCIE_USB3_BUS_WIDTH_MASK GENMASK(3, 2)
84 +#define PCIE_USB3_BUS_WIDTH BIT(3)
85 +#define PCIE_USB3_RATE_MASK GENMASK(6, 5)
86 +#define PCIE_USB3_RX_STANDBY_MASK BIT(7)
87 +#define PCIE_USB3_PHY_ENABLE BIT(4)
89 +struct jh7110_pcie_phy {
91 + struct regmap *stg_syscon;
92 + struct regmap *sys_syscon;
94 + u32 sys_phy_connect;
100 +static int phy_usb3_mode_set(struct jh7110_pcie_phy *data)
102 + if (!data->stg_syscon || !data->sys_syscon) {
103 + dev_err(&data->phy->dev, "doesn't support usb3 mode\n");
107 + regmap_update_bits(data->stg_syscon, data->stg_pcie_mode,
108 + PCIE_PHY_MODE_MASK, PCIE_PHY_MODE);
109 + regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
110 + PCIE_USB3_BUS_WIDTH_MASK, 0);
111 + regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
112 + PCIE_USB3_PHY_ENABLE, PCIE_USB3_PHY_ENABLE);
114 + /* Connect usb 3.0 phy mode */
115 + regmap_update_bits(data->sys_syscon, data->sys_phy_connect,
116 + USB_PDRSTN_SPLIT, 0);
118 + /* Configuare spread-spectrum mode: down-spread-spectrum */
119 + writel(PCIE_USB3_PHY_ENABLE, data->regs + PCIE_USB3_PHY_PLL_CTL_OFF);
124 +static void phy_pcie_mode_set(struct jh7110_pcie_phy *data)
128 + /* default is PCIe mode */
129 + if (!data->stg_syscon || !data->sys_syscon)
132 + regmap_update_bits(data->stg_syscon, data->stg_pcie_mode,
133 + PCIE_PHY_MODE_MASK, 0);
134 + regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
135 + PCIE_USB3_BUS_WIDTH_MASK,
136 + PCIE_USB3_BUS_WIDTH);
137 + regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
138 + PCIE_USB3_PHY_ENABLE, 0);
140 + regmap_update_bits(data->sys_syscon, data->sys_phy_connect,
141 + USB_PDRSTN_SPLIT, 0);
143 + val = readl(data->regs + PCIE_USB3_PHY_PLL_CTL_OFF);
144 + val &= ~PCIE_USB3_PHY_ENABLE;
145 + writel(val, data->regs + PCIE_USB3_PHY_PLL_CTL_OFF);
148 +static void phy_kvco_gain_set(struct jh7110_pcie_phy *phy)
150 + /* PCIe Multi-PHY PLL KVCO Gain fine tune settings: */
151 + writel(PHY_KVCO_FINE_TUNE_LEVEL, phy->regs + PCIE_KVCO_LEVEL_OFF);
152 + writel(PHY_KVCO_FINE_TUNE_SIGNALS, phy->regs + PCIE_KVCO_TUNE_SIGNAL_OFF);
155 +static int jh7110_pcie_phy_set_mode(struct phy *_phy,
156 + enum phy_mode mode, int submode)
158 + struct jh7110_pcie_phy *phy = phy_get_drvdata(_phy);
161 + if (mode == phy->mode)
165 + case PHY_MODE_USB_HOST:
166 + case PHY_MODE_USB_DEVICE:
167 + case PHY_MODE_USB_OTG:
168 + ret = phy_usb3_mode_set(phy);
172 + case PHY_MODE_PCIE:
173 + phy_pcie_mode_set(phy);
179 + dev_dbg(&_phy->dev, "Changing phy mode to %d\n", mode);
185 +static const struct phy_ops jh7110_pcie_phy_ops = {
186 + .set_mode = jh7110_pcie_phy_set_mode,
187 + .owner = THIS_MODULE,
190 +static int jh7110_pcie_phy_probe(struct platform_device *pdev)
192 + struct jh7110_pcie_phy *phy;
193 + struct device *dev = &pdev->dev;
194 + struct phy_provider *phy_provider;
197 + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
201 + phy->regs = devm_platform_ioremap_resource(pdev, 0);
202 + if (IS_ERR(phy->regs))
203 + return PTR_ERR(phy->regs);
205 + phy->phy = devm_phy_create(dev, NULL, &jh7110_pcie_phy_ops);
206 + if (IS_ERR(phy->phy))
207 + return dev_err_probe(dev, PTR_ERR(phy->regs),
208 + "Failed to map phy base\n");
211 + syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
212 + "starfive,sys-syscon",
215 + if (!IS_ERR_OR_NULL(phy->sys_syscon))
216 + phy->sys_phy_connect = args[0];
218 + phy->sys_syscon = NULL;
221 + syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
222 + "starfive,stg-syscon",
225 + if (!IS_ERR_OR_NULL(phy->stg_syscon)) {
226 + phy->stg_pcie_mode = args[0];
227 + phy->stg_pcie_usb = args[1];
229 + phy->stg_syscon = NULL;
232 + phy_kvco_gain_set(phy);
234 + phy_set_drvdata(phy->phy, phy);
235 + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
237 + return PTR_ERR_OR_ZERO(phy_provider);
240 +static const struct of_device_id jh7110_pcie_phy_of_match[] = {
241 + { .compatible = "starfive,jh7110-pcie-phy" },
242 + { /* sentinel */ },
244 +MODULE_DEVICE_TABLE(of, jh7110_pcie_phy_of_match);
246 +static struct platform_driver jh7110_pcie_phy_driver = {
247 + .probe = jh7110_pcie_phy_probe,
249 + .of_match_table = jh7110_pcie_phy_of_match,
250 + .name = "jh7110-pcie-phy",
253 +module_platform_driver(jh7110_pcie_phy_driver);
255 +MODULE_DESCRIPTION("StarFive JH7110 PCIe 2.0 PHY driver");
256 +MODULE_AUTHOR("Minda Chen <minda.chen@starfivetech.com>");
257 +MODULE_LICENSE("GPL");