blob: ecb04bdedfa7a3f06b3422d75532e935b21834eb [file] [log] [blame]
Minda Chen1dd049f2025-03-06 14:20:27 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * StarFive JH7110 PCIe 2.0 PHY driver
4 *
5 * Copyright (C) 2024 StarFive Technology Co., Ltd.
6 * Author: Minda Chen <minda.chen@starfivetech.com>
7 */
8#include <asm/io.h>
9#include <dm.h>
10#include <dm/device_compat.h>
11#include <errno.h>
12#include <generic-phy.h>
13#include <regmap.h>
14#include <soc.h>
15#include <syscon.h>
16#include <linux/bitops.h>
17#include <linux/err.h>
18
19#include "phy-jh7110-usb-syscon.h"
20
21#define PCIE_KVCO_LEVEL_OFF 0x28
22#define PCIE_USB3_PHY_PLL_CTL_OFF 0x7c
23#define PCIE_USB3_PHY_SS_MODE BIT(4)
24#define PCIE_KVCO_TUNE_SIGNAL_OFF 0x80
25#define PHY_KVCO_FINE_TUNE_LEVEL 0x91
26#define PHY_KVCO_FINE_TUNE_SIGNALS 0xc
27
28#define PCIE_USB3_PHY_MODE 0x1
29#define PCIE_BUS_WIDTH 0x2
30#define PCIE_USB3_PHY_ENABLE 0x1
31#define PCIE_USB3_PHY_SPLIT 0x1
32
33struct jh7110_pcie_phy {
34 struct phy *phy;
35 struct regmap *stg_syscon;
36 struct regmap *sys_syscon;
37 void __iomem *regs;
38 struct regmap_field *phy_mode;
39 struct regmap_field *bus_width;
40 struct regmap_field *usb3_phy_en;
41 struct regmap_field *usb_split;
42 enum phy_mode mode;
43};
44
45static int phy_pcie_mode_set(struct jh7110_pcie_phy *data, bool usb_mode)
46{
47 unsigned int phy_mode, width, usb3_phy, ss_mode, split;
48
49 /* default is PCIe mode */
50 if (!data->stg_syscon || !data->sys_syscon) {
51 if (usb_mode) {
52 dev_err(data->phy->dev, "doesn't support USB3 mode\n");
53 return -EINVAL;
54 }
55 return 0;
56 }
57
58 if (usb_mode) {
59 phy_mode = PCIE_USB3_PHY_MODE;
60 width = 0;
61 usb3_phy = PCIE_USB3_PHY_ENABLE;
62 ss_mode = PCIE_USB3_PHY_SS_MODE;
63 split = 0;
64 } else {
65 phy_mode = 0;
66 width = PCIE_BUS_WIDTH;
67 usb3_phy = 0;
68 ss_mode = 0;
69 split = PCIE_USB3_PHY_SPLIT;
70 }
71
72 regmap_field_write(data->phy_mode, phy_mode);
73 regmap_field_write(data->bus_width, width);
74 regmap_field_write(data->usb3_phy_en, usb3_phy);
75 clrsetbits_le32(data->regs + PCIE_USB3_PHY_PLL_CTL_OFF,
76 PCIE_USB3_PHY_SS_MODE, ss_mode);
77 regmap_field_write(data->usb_split, split);
78
79 return 0;
80}
81
82static void phy_kvco_gain_set(struct jh7110_pcie_phy *phy)
83{
84 /* PCIe Multi-PHY PLL KVCO Gain fine tune settings: */
85 writel(PHY_KVCO_FINE_TUNE_LEVEL, phy->regs + PCIE_KVCO_LEVEL_OFF);
86 writel(PHY_KVCO_FINE_TUNE_SIGNALS, phy->regs + PCIE_KVCO_TUNE_SIGNAL_OFF);
87}
88
89static int jh7110_pcie_phy_set_mode(struct phy *phy,
90 enum phy_mode mode, int submode)
91{
92 struct udevice *dev = phy->dev;
93 struct jh7110_pcie_phy *pcie_phy = dev_get_priv(dev);
94 int ret;
95
96 if (mode == pcie_phy->mode)
97 return 0;
98
99 switch (mode) {
100 case PHY_MODE_USB_HOST:
101 case PHY_MODE_USB_DEVICE:
102 case PHY_MODE_USB_OTG:
103 ret = phy_pcie_mode_set(pcie_phy, 1);
104 if (ret)
105 return ret;
106 break;
107 case PHY_MODE_PCIE:
108 phy_pcie_mode_set(pcie_phy, 0);
109 break;
110 default:
111 return -EINVAL;
112 }
113
114 dev_dbg(phy->dev, "Changing PHY mode to %d\n", mode);
115 pcie_phy->mode = mode;
116
117 return 0;
118}
119
120static const struct phy_ops jh7110_pcie_phy_ops = {
121 .set_mode = jh7110_pcie_phy_set_mode,
122};
123
124static int phy_stg_regfield_init(struct udevice *dev, int mode, int usb3)
125{
126 struct jh7110_pcie_phy *phy = dev_get_priv(dev);
127 struct reg_field phy_mode = REG_FIELD(mode, 20, 21);
128 struct reg_field bus_width = REG_FIELD(usb3, 2, 3);
129 struct reg_field usb3_phy_en = REG_FIELD(usb3, 4, 4);
130
131 phy->phy_mode = devm_regmap_field_alloc(dev, phy->stg_syscon, phy_mode);
132 if (IS_ERR(phy->phy_mode)) {
133 dev_err(dev, "PHY mode reg field init failed\n");
134 return PTR_ERR(phy->phy_mode);
135 }
136
137 phy->bus_width = devm_regmap_field_alloc(dev, phy->stg_syscon, bus_width);
138 if (IS_ERR(phy->bus_width)) {
139 dev_err(dev, "PHY bus width reg field init failed\n");
140 return PTR_ERR(phy->bus_width);
141 }
142
143 phy->usb3_phy_en = devm_regmap_field_alloc(dev, phy->stg_syscon, usb3_phy_en);
144 if (IS_ERR(phy->usb3_phy_en)) {
145 dev_err(dev, "USB3 PHY enable field init failed\n");
146 return PTR_ERR(phy->bus_width);
147 }
148
149 return 0;
150}
151
152static int phy_sys_regfield_init(struct udevice *dev, int split)
153{
154 struct jh7110_pcie_phy *phy = dev_get_priv(dev);
155 struct reg_field usb_split = REG_FIELD(split, USB_PDRSTN_SPLIT_BIT, USB_PDRSTN_SPLIT_BIT);
156
157 phy->usb_split = devm_regmap_field_alloc(dev, phy->sys_syscon, usb_split);
158 if (IS_ERR(phy->usb_split)) {
159 dev_err(dev, "USB split field init failed\n");
160 return PTR_ERR(phy->usb_split);
161 }
162
163 return 0;
164}
165
166static int starfive_pcie_phy_get_syscon(struct udevice *dev)
167{
168 struct jh7110_pcie_phy *phy = dev_get_priv(dev);
169 struct ofnode_phandle_args sys_phandle, stg_phandle;
170 int ret;
171
172 /* get corresponding syscon phandle */
173 ret = dev_read_phandle_with_args(dev, "starfive,sys-syscon", NULL, 0, 0,
174 &sys_phandle);
175
176 if (ret < 0) {
177 dev_err(dev, "Can't get sys cfg phandle: %d\n", ret);
178 return ret;
179 }
180
181 ret = dev_read_phandle_with_args(dev, "starfive,stg-syscon", NULL, 2, 0,
182 &stg_phandle);
183
184 if (ret < 0) {
185 dev_err(dev, "Can't get stg cfg phandle: %d\n", ret);
186 return ret;
187 }
188
189 phy->sys_syscon = syscon_node_to_regmap(sys_phandle.node);
190 /* get syscon register offset */
191 if (!IS_ERR(phy->sys_syscon)) {
192 ret = phy_sys_regfield_init(dev, SYSCON_USB_PDRSTN_REG_OFFSET);
193 if (ret)
194 return ret;
195 } else {
196 phy->sys_syscon = NULL;
197 }
198
199 phy->stg_syscon = syscon_node_to_regmap(stg_phandle.node);
200 if (!IS_ERR(phy->stg_syscon))
201 return phy_stg_regfield_init(dev, stg_phandle.args[0],
202 stg_phandle.args[1]);
203 else
204 phy->stg_syscon = NULL;
205
206 return 0;
207}
208
209int jh7110_pcie_phy_probe(struct udevice *dev)
210{
211 struct jh7110_pcie_phy *phy = dev_get_priv(dev);
212 int rc;
213
214 phy->regs = dev_read_addr_ptr(dev);
215 if (!phy->regs)
216 return -EINVAL;
217
218 rc = starfive_pcie_phy_get_syscon(dev);
219 if (rc)
220 return rc;
221
222 phy_kvco_gain_set(phy);
223
224 return 0;
225}
226
227static const struct udevice_id jh7110_pcie_phy[] = {
228 { .compatible = "starfive,jh7110-pcie-phy"},
229 {},
230};
231
232U_BOOT_DRIVER(jh7110_pcie_phy) = {
233 .name = "jh7110_pcie_phy",
234 .id = UCLASS_PHY,
235 .of_match = jh7110_pcie_phy,
236 .probe = jh7110_pcie_phy_probe,
237 .ops = &jh7110_pcie_phy_ops,
238 .priv_auto = sizeof(struct jh7110_pcie_phy),
239};