Skip to content

Commit fd097f4

Browse files
mindachen1987vinodkoul
authored andcommitted
phy: starfive: Add JH7110 PCIE 2.0 PHY driver
Add Starfive JH7110 SoC PCIe 2.0 PHY driver support. PCIe 2.0 PHY default connect to PCIe controller. PCIe PHY can connect to USB 3.0 controller. Signed-off-by: Minda Chen <[email protected]> Reviewed-by: Roger Quadros <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]>
1 parent 16d3a71 commit fd097f4

File tree

4 files changed

+218
-1
lines changed

4 files changed

+218
-1
lines changed

MAINTAINERS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20335,10 +20335,12 @@ S: Supported
2033520335
F: Documentation/devicetree/bindings/watchdog/starfive*
2033620336
F: drivers/watchdog/starfive-wdt.c
2033720337

20338-
STARFIVE JH71X0 USB PHY DRIVER
20338+
STARFIVE JH71X0 PCIE AND USB PHY DRIVER
2033920339
M: Minda Chen <[email protected]>
2034020340
S: Supported
20341+
F: Documentation/devicetree/bindings/phy/starfive,jh7110-pcie-phy.yaml
2034120342
F: Documentation/devicetree/bindings/phy/starfive,jh7110-usb-phy.yaml
20343+
F: drivers/phy/starfive/phy-jh7110-pcie.c
2034220344
F: drivers/phy/starfive/phy-jh7110-usb.c
2034320345

2034420346
STATIC BRANCH/CALL

drivers/phy/starfive/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
# Phy drivers for StarFive platforms
44
#
55

6+
config PHY_STARFIVE_JH7110_PCIE
7+
tristate "Starfive JH7110 PCIE 2.0/USB 3.0 PHY support"
8+
depends on HAS_IOMEM
9+
select GENERIC_PHY
10+
help
11+
Enable this to support the StarFive PCIe 2.0 PHY,
12+
or used as USB 3.0 PHY.
13+
If M is selected, the module will be called
14+
phy-jh7110-pcie.ko.
15+
616
config PHY_STARFIVE_JH7110_USB
717
tristate "Starfive JH7110 USB 2.0 PHY support"
818
depends on USB_SUPPORT

drivers/phy/starfive/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
obj-$(CONFIG_PHY_STARFIVE_JH7110_PCIE) += phy-jh7110-pcie.o
23
obj-$(CONFIG_PHY_STARFIVE_JH7110_USB) += phy-jh7110-usb.o
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* StarFive JH7110 PCIe 2.0 PHY driver
4+
*
5+
* Copyright (C) 2023 StarFive Technology Co., Ltd.
6+
* Author: Minda Chen <[email protected]>
7+
*/
8+
9+
#include <linux/bits.h>
10+
#include <linux/clk.h>
11+
#include <linux/err.h>
12+
#include <linux/io.h>
13+
#include <linux/module.h>
14+
#include <linux/mfd/syscon.h>
15+
#include <linux/phy/phy.h>
16+
#include <linux/platform_device.h>
17+
#include <linux/regmap.h>
18+
19+
#define PCIE_KVCO_LEVEL_OFF 0x28
20+
#define PCIE_USB3_PHY_PLL_CTL_OFF 0x7c
21+
#define PCIE_KVCO_TUNE_SIGNAL_OFF 0x80
22+
#define PCIE_USB3_PHY_ENABLE BIT(4)
23+
#define PHY_KVCO_FINE_TUNE_LEVEL 0x91
24+
#define PHY_KVCO_FINE_TUNE_SIGNALS 0xc
25+
26+
#define USB_PDRSTN_SPLIT BIT(17)
27+
28+
#define PCIE_PHY_MODE BIT(20)
29+
#define PCIE_PHY_MODE_MASK GENMASK(21, 20)
30+
#define PCIE_USB3_BUS_WIDTH_MASK GENMASK(3, 2)
31+
#define PCIE_USB3_BUS_WIDTH BIT(3)
32+
#define PCIE_USB3_RATE_MASK GENMASK(6, 5)
33+
#define PCIE_USB3_RX_STANDBY_MASK BIT(7)
34+
#define PCIE_USB3_PHY_ENABLE BIT(4)
35+
36+
struct jh7110_pcie_phy {
37+
struct phy *phy;
38+
struct regmap *stg_syscon;
39+
struct regmap *sys_syscon;
40+
void __iomem *regs;
41+
u32 sys_phy_connect;
42+
u32 stg_pcie_mode;
43+
u32 stg_pcie_usb;
44+
enum phy_mode mode;
45+
};
46+
47+
static int phy_usb3_mode_set(struct jh7110_pcie_phy *data)
48+
{
49+
if (!data->stg_syscon || !data->sys_syscon) {
50+
dev_err(&data->phy->dev, "doesn't support usb3 mode\n");
51+
return -EINVAL;
52+
}
53+
54+
regmap_update_bits(data->stg_syscon, data->stg_pcie_mode,
55+
PCIE_PHY_MODE_MASK, PCIE_PHY_MODE);
56+
regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
57+
PCIE_USB3_BUS_WIDTH_MASK, 0);
58+
regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
59+
PCIE_USB3_PHY_ENABLE, PCIE_USB3_PHY_ENABLE);
60+
61+
/* Connect usb 3.0 phy mode */
62+
regmap_update_bits(data->sys_syscon, data->sys_phy_connect,
63+
USB_PDRSTN_SPLIT, 0);
64+
65+
/* Configuare spread-spectrum mode: down-spread-spectrum */
66+
writel(PCIE_USB3_PHY_ENABLE, data->regs + PCIE_USB3_PHY_PLL_CTL_OFF);
67+
68+
return 0;
69+
}
70+
71+
static void phy_pcie_mode_set(struct jh7110_pcie_phy *data)
72+
{
73+
u32 val;
74+
75+
/* default is PCIe mode */
76+
if (!data->stg_syscon || !data->sys_syscon)
77+
return;
78+
79+
regmap_update_bits(data->stg_syscon, data->stg_pcie_mode,
80+
PCIE_PHY_MODE_MASK, 0);
81+
regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
82+
PCIE_USB3_BUS_WIDTH_MASK,
83+
PCIE_USB3_BUS_WIDTH);
84+
regmap_update_bits(data->stg_syscon, data->stg_pcie_usb,
85+
PCIE_USB3_PHY_ENABLE, 0);
86+
87+
regmap_update_bits(data->sys_syscon, data->sys_phy_connect,
88+
USB_PDRSTN_SPLIT, 0);
89+
90+
val = readl(data->regs + PCIE_USB3_PHY_PLL_CTL_OFF);
91+
val &= ~PCIE_USB3_PHY_ENABLE;
92+
writel(val, data->regs + PCIE_USB3_PHY_PLL_CTL_OFF);
93+
}
94+
95+
static void phy_kvco_gain_set(struct jh7110_pcie_phy *phy)
96+
{
97+
/* PCIe Multi-PHY PLL KVCO Gain fine tune settings: */
98+
writel(PHY_KVCO_FINE_TUNE_LEVEL, phy->regs + PCIE_KVCO_LEVEL_OFF);
99+
writel(PHY_KVCO_FINE_TUNE_SIGNALS, phy->regs + PCIE_KVCO_TUNE_SIGNAL_OFF);
100+
}
101+
102+
static int jh7110_pcie_phy_set_mode(struct phy *_phy,
103+
enum phy_mode mode, int submode)
104+
{
105+
struct jh7110_pcie_phy *phy = phy_get_drvdata(_phy);
106+
int ret;
107+
108+
if (mode == phy->mode)
109+
return 0;
110+
111+
switch (mode) {
112+
case PHY_MODE_USB_HOST:
113+
case PHY_MODE_USB_DEVICE:
114+
case PHY_MODE_USB_OTG:
115+
ret = phy_usb3_mode_set(phy);
116+
if (ret)
117+
return ret;
118+
break;
119+
case PHY_MODE_PCIE:
120+
phy_pcie_mode_set(phy);
121+
break;
122+
default:
123+
return -EINVAL;
124+
}
125+
126+
dev_dbg(&_phy->dev, "Changing phy mode to %d\n", mode);
127+
phy->mode = mode;
128+
129+
return 0;
130+
}
131+
132+
static const struct phy_ops jh7110_pcie_phy_ops = {
133+
.set_mode = jh7110_pcie_phy_set_mode,
134+
.owner = THIS_MODULE,
135+
};
136+
137+
static int jh7110_pcie_phy_probe(struct platform_device *pdev)
138+
{
139+
struct jh7110_pcie_phy *phy;
140+
struct device *dev = &pdev->dev;
141+
struct phy_provider *phy_provider;
142+
u32 args[2];
143+
144+
phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
145+
if (!phy)
146+
return -ENOMEM;
147+
148+
phy->regs = devm_platform_ioremap_resource(pdev, 0);
149+
if (IS_ERR(phy->regs))
150+
return PTR_ERR(phy->regs);
151+
152+
phy->phy = devm_phy_create(dev, NULL, &jh7110_pcie_phy_ops);
153+
if (IS_ERR(phy->phy))
154+
return dev_err_probe(dev, PTR_ERR(phy->regs),
155+
"Failed to map phy base\n");
156+
157+
phy->sys_syscon =
158+
syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
159+
"starfive,sys-syscon",
160+
1, args);
161+
162+
if (!IS_ERR_OR_NULL(phy->sys_syscon))
163+
phy->sys_phy_connect = args[0];
164+
else
165+
phy->sys_syscon = NULL;
166+
167+
phy->stg_syscon =
168+
syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
169+
"starfive,stg-syscon",
170+
2, args);
171+
172+
if (!IS_ERR_OR_NULL(phy->stg_syscon)) {
173+
phy->stg_pcie_mode = args[0];
174+
phy->stg_pcie_usb = args[1];
175+
} else {
176+
phy->stg_syscon = NULL;
177+
}
178+
179+
phy_kvco_gain_set(phy);
180+
181+
phy_set_drvdata(phy->phy, phy);
182+
phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
183+
184+
return PTR_ERR_OR_ZERO(phy_provider);
185+
}
186+
187+
static const struct of_device_id jh7110_pcie_phy_of_match[] = {
188+
{ .compatible = "starfive,jh7110-pcie-phy" },
189+
{ /* sentinel */ },
190+
};
191+
MODULE_DEVICE_TABLE(of, jh7110_pcie_phy_of_match);
192+
193+
static struct platform_driver jh7110_pcie_phy_driver = {
194+
.probe = jh7110_pcie_phy_probe,
195+
.driver = {
196+
.of_match_table = jh7110_pcie_phy_of_match,
197+
.name = "jh7110-pcie-phy",
198+
}
199+
};
200+
module_platform_driver(jh7110_pcie_phy_driver);
201+
202+
MODULE_DESCRIPTION("StarFive JH7110 PCIe 2.0 PHY driver");
203+
MODULE_AUTHOR("Minda Chen <[email protected]>");
204+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)