blob: 78da5fe79700ea2c7b36b7dbbaaf25df06744131 [file] [log] [blame]
Jagan Teki0d2d0bf2023-02-17 17:28:41 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Rockchip USB3.0/PCIe Gen2/SATA/SGMII combphy driver
4 *
5 * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <dm/lists.h>
12#include <dt-bindings/phy/phy.h>
13#include <generic-phy.h>
14#include <syscon.h>
15#include <asm/io.h>
16#include <asm/arch-rockchip/clock.h>
17#include <regmap.h>
18#include <reset-uclass.h>
19#include <dm/device_compat.h>
20
21#define BIT_WRITEABLE_SHIFT 16
22
23struct rockchip_combphy_priv;
24
25struct combphy_reg {
26 u16 offset;
27 u16 bitend;
28 u16 bitstart;
29 u16 disable;
30 u16 enable;
31};
32
33struct rockchip_combphy_grfcfg {
34 struct combphy_reg pcie_mode_set;
35 struct combphy_reg usb_mode_set;
36 struct combphy_reg sgmii_mode_set;
37 struct combphy_reg qsgmii_mode_set;
38 struct combphy_reg pipe_rxterm_set;
39 struct combphy_reg pipe_txelec_set;
40 struct combphy_reg pipe_txcomp_set;
41 struct combphy_reg pipe_clk_25m;
42 struct combphy_reg pipe_clk_100m;
43 struct combphy_reg pipe_phymode_sel;
44 struct combphy_reg pipe_rate_sel;
45 struct combphy_reg pipe_rxterm_sel;
46 struct combphy_reg pipe_txelec_sel;
47 struct combphy_reg pipe_txcomp_sel;
48 struct combphy_reg pipe_clk_ext;
49 struct combphy_reg pipe_sel_usb;
50 struct combphy_reg pipe_sel_qsgmii;
51 struct combphy_reg pipe_phy_status;
52 struct combphy_reg con0_for_pcie;
53 struct combphy_reg con1_for_pcie;
54 struct combphy_reg con2_for_pcie;
55 struct combphy_reg con3_for_pcie;
56 struct combphy_reg con0_for_sata;
57 struct combphy_reg con1_for_sata;
58 struct combphy_reg con2_for_sata;
59 struct combphy_reg con3_for_sata;
60 struct combphy_reg pipe_con0_for_sata;
61 struct combphy_reg pipe_sgmii_mac_sel;
62 struct combphy_reg pipe_xpcs_phy_ready;
63 struct combphy_reg u3otg0_port_en;
64 struct combphy_reg u3otg1_port_en;
65};
66
67struct rockchip_combphy_cfg {
68 const struct rockchip_combphy_grfcfg *grfcfg;
69 int (*combphy_cfg)(struct rockchip_combphy_priv *priv);
70};
71
72struct rockchip_combphy_priv {
73 u32 mode;
74 void __iomem *mmio;
75 struct udevice *dev;
76 struct regmap *pipe_grf;
77 struct regmap *phy_grf;
78 struct phy *phy;
79 struct reset_ctl phy_rst;
80 struct clk ref_clk;
81 const struct rockchip_combphy_cfg *cfg;
82};
83
84static int param_write(struct regmap *base,
85 const struct combphy_reg *reg, bool en)
86{
87 u32 val, mask, tmp;
88
89 tmp = en ? reg->enable : reg->disable;
90 mask = GENMASK(reg->bitend, reg->bitstart);
91 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
92
93 return regmap_write(base, reg->offset, val);
94}
95
96static int rockchip_combphy_pcie_init(struct rockchip_combphy_priv *priv)
97{
98 int ret = 0;
99
100 if (priv->cfg->combphy_cfg) {
101 ret = priv->cfg->combphy_cfg(priv);
102 if (ret) {
103 dev_err(priv->dev, "failed to init phy for pcie\n");
104 return ret;
105 }
106 }
107
108 return ret;
109}
110
111static int rockchip_combphy_usb3_init(struct rockchip_combphy_priv *priv)
112{
113 int ret = 0;
114
115 if (priv->cfg->combphy_cfg) {
116 ret = priv->cfg->combphy_cfg(priv);
117 if (ret) {
118 dev_err(priv->dev, "failed to init phy for usb3\n");
119 return ret;
120 }
121 }
122
123 return ret;
124}
125
126static int rockchip_combphy_sata_init(struct rockchip_combphy_priv *priv)
127{
128 int ret = 0;
129
130 if (priv->cfg->combphy_cfg) {
131 ret = priv->cfg->combphy_cfg(priv);
132 if (ret) {
133 dev_err(priv->dev, "failed to init phy for sata\n");
134 return ret;
135 }
136 }
137
138 return ret;
139}
140
141static int rockchip_combphy_sgmii_init(struct rockchip_combphy_priv *priv)
142{
143 int ret = 0;
144
145 if (priv->cfg->combphy_cfg) {
146 ret = priv->cfg->combphy_cfg(priv);
147 if (ret) {
148 dev_err(priv->dev, "failed to init phy for sgmii\n");
149 return ret;
150 }
151 }
152
153 return ret;
154}
155
156static int rockchip_combphy_set_mode(struct rockchip_combphy_priv *priv)
157{
158 switch (priv->mode) {
159 case PHY_TYPE_PCIE:
160 rockchip_combphy_pcie_init(priv);
161 break;
162 case PHY_TYPE_USB3:
163 rockchip_combphy_usb3_init(priv);
164 break;
165 case PHY_TYPE_SATA:
166 rockchip_combphy_sata_init(priv);
167 break;
168 case PHY_TYPE_SGMII:
169 case PHY_TYPE_QSGMII:
170 return rockchip_combphy_sgmii_init(priv);
171 default:
172 dev_err(priv->dev, "incompatible PHY type\n");
173 return -EINVAL;
174 }
175
176 return 0;
177}
178
179static int rockchip_combphy_init(struct phy *phy)
180{
181 struct rockchip_combphy_priv *priv = dev_get_priv(phy->dev);
182 int ret;
183
184 ret = clk_enable(&priv->ref_clk);
185 if (ret < 0 && ret != -ENOSYS)
186 return ret;
187
188 ret = rockchip_combphy_set_mode(priv);
189 if (ret)
190 goto err_clk;
191
192 reset_deassert(&priv->phy_rst);
193
194 return 0;
195
196err_clk:
197 clk_disable(&priv->ref_clk);
198
199 return ret;
200}
201
202static int rockchip_combphy_exit(struct phy *phy)
203{
204 struct rockchip_combphy_priv *priv = dev_get_priv(phy->dev);
205
206 clk_disable(&priv->ref_clk);
207 reset_assert(&priv->phy_rst);
208
209 return 0;
210}
211
212static int rockchip_combphy_xlate(struct phy *phy, struct ofnode_phandle_args *args)
213{
214 struct rockchip_combphy_priv *priv = dev_get_priv(phy->dev);
215
216 if (args->args_count != 1) {
217 pr_err("invalid number of arguments\n");
218 return -EINVAL;
219 }
220
221 priv->mode = args->args[0];
222
223 return 0;
224}
225
226static const struct phy_ops rochchip_combphy_ops = {
227 .init = rockchip_combphy_init,
228 .exit = rockchip_combphy_exit,
229 .of_xlate = rockchip_combphy_xlate,
230};
231
232static int rockchip_combphy_parse_dt(struct udevice *dev,
233 struct rockchip_combphy_priv *priv)
234{
235 struct udevice *syscon;
236 int ret;
237
238 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pipe-grf", &syscon);
239 if (ret) {
240 dev_err(dev, "failed to find peri_ctrl pipe-grf regmap");
241 return ret;
242 }
243 priv->pipe_grf = syscon_get_regmap(syscon);
244
245 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "rockchip,pipe-phy-grf", &syscon);
246 if (ret) {
247 dev_err(dev, "failed to find peri_ctrl pipe-phy-grf regmap\n");
248 return ret;
249 }
250 priv->phy_grf = syscon_get_regmap(syscon);
251
252 ret = clk_get_by_index(dev, 0, &priv->ref_clk);
253 if (ret) {
254 dev_err(dev, "failed to find ref clock\n");
255 return PTR_ERR(&priv->ref_clk);
256 }
257
258 ret = reset_get_by_index(dev, 0, &priv->phy_rst);
259 if (ret) {
260 dev_err(dev, "no phy reset control specified\n");
261 return ret;
262 }
263
264 return 0;
265}
266
267static int rockchip_combphy_probe(struct udevice *udev)
268{
269 struct rockchip_combphy_priv *priv = dev_get_priv(udev);
270 const struct rockchip_combphy_cfg *phy_cfg;
271
272 priv->mmio = (void __iomem *)dev_read_addr(udev);
273 if (IS_ERR(priv->mmio))
274 return PTR_ERR(priv->mmio);
275
276 phy_cfg = (const struct rockchip_combphy_cfg *)dev_get_driver_data(udev);
277 if (!phy_cfg) {
278 dev_err(udev, "No OF match data provided\n");
279 return -EINVAL;
280 }
281
282 priv->dev = udev;
283 priv->mode = PHY_TYPE_SATA;
284 priv->cfg = phy_cfg;
285
286 return rockchip_combphy_parse_dt(udev, priv);
287}
288
289static int rk3568_combphy_cfg(struct rockchip_combphy_priv *priv)
290{
291 const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
292 u32 val;
293
294 switch (priv->mode) {
295 case PHY_TYPE_PCIE:
296 /* Set SSC downward spread spectrum */
297 val = readl(priv->mmio + (0x1f << 2));
298 val &= ~GENMASK(5, 4);
299 val |= 0x01 << 4;
300 writel(val, priv->mmio + 0x7c);
301
302 param_write(priv->phy_grf, &cfg->con0_for_pcie, true);
303 param_write(priv->phy_grf, &cfg->con1_for_pcie, true);
304 param_write(priv->phy_grf, &cfg->con2_for_pcie, true);
305 param_write(priv->phy_grf, &cfg->con3_for_pcie, true);
306 break;
307 case PHY_TYPE_USB3:
308 /* Set SSC downward spread spectrum */
309 val = readl(priv->mmio + (0x1f << 2));
310 val &= ~GENMASK(5, 4);
311 val |= 0x01 << 4;
312 writel(val, priv->mmio + 0x7c);
313
314 /* Enable adaptive CTLE for USB3.0 Rx */
315 val = readl(priv->mmio + (0x0e << 2));
316 val &= ~GENMASK(0, 0);
317 val |= 0x01;
318 writel(val, priv->mmio + (0x0e << 2));
319
320 /* Set PLL KVCO fine tuning signals */
321 val = readl(priv->mmio + (0x20 << 2));
322 val &= ~(0x7 << 2);
323 val |= 0x2 << 2;
324 writel(val, priv->mmio + (0x20 << 2));
325
326 /* Set PLL LPF R1 to su_trim[10:7]=1001 */
327 writel(0x4, priv->mmio + (0xb << 2));
328
329 /* Set PLL input clock divider 1/2 */
330 val = readl(priv->mmio + (0x5 << 2));
331 val &= ~(0x3 << 6);
332 val |= 0x1 << 6;
333 writel(val, priv->mmio + (0x5 << 2));
334
335 /* Set PLL loop divider */
336 writel(0x32, priv->mmio + (0x11 << 2));
337
338 /* Set PLL KVCO to min and set PLL charge pump current to max */
339 writel(0xf0, priv->mmio + (0xa << 2));
340
341 param_write(priv->phy_grf, &cfg->pipe_sel_usb, true);
342 param_write(priv->phy_grf, &cfg->pipe_txcomp_sel, false);
343 param_write(priv->phy_grf, &cfg->pipe_txelec_sel, false);
344 param_write(priv->phy_grf, &cfg->usb_mode_set, true);
345 break;
346 case PHY_TYPE_SATA:
347 writel(0x41, priv->mmio + 0x38);
348 writel(0x8F, priv->mmio + 0x18);
349 param_write(priv->phy_grf, &cfg->con0_for_sata, true);
350 param_write(priv->phy_grf, &cfg->con1_for_sata, true);
351 param_write(priv->phy_grf, &cfg->con2_for_sata, true);
352 param_write(priv->phy_grf, &cfg->con3_for_sata, true);
353 param_write(priv->pipe_grf, &cfg->pipe_con0_for_sata, true);
354 break;
355 case PHY_TYPE_SGMII:
356 param_write(priv->pipe_grf, &cfg->pipe_xpcs_phy_ready, true);
357 param_write(priv->phy_grf, &cfg->pipe_phymode_sel, true);
358 param_write(priv->phy_grf, &cfg->pipe_sel_qsgmii, true);
359 param_write(priv->phy_grf, &cfg->sgmii_mode_set, true);
360 break;
361 case PHY_TYPE_QSGMII:
362 param_write(priv->pipe_grf, &cfg->pipe_xpcs_phy_ready, true);
363 param_write(priv->phy_grf, &cfg->pipe_phymode_sel, true);
364 param_write(priv->phy_grf, &cfg->pipe_rate_sel, true);
365 param_write(priv->phy_grf, &cfg->pipe_sel_qsgmii, true);
366 param_write(priv->phy_grf, &cfg->qsgmii_mode_set, true);
367 break;
368 default:
369 pr_err("%s, phy-type %d\n", __func__, priv->mode);
370 return -EINVAL;
371 }
372
373 /* The default ref clock is 25Mhz */
374 param_write(priv->phy_grf, &cfg->pipe_clk_25m, true);
375
376 if (dev_read_bool(priv->dev, "rockchip,enable-ssc")) {
377 val = readl(priv->mmio + (0x7 << 2));
378 val |= BIT(4);
379 writel(val, priv->mmio + (0x7 << 2));
380 }
381
382 return 0;
383}
384
385static const struct rockchip_combphy_grfcfg rk3568_combphy_grfcfgs = {
386 /* pipe-phy-grf */
387 .pcie_mode_set = { 0x0000, 5, 0, 0x00, 0x11 },
388 .usb_mode_set = { 0x0000, 5, 0, 0x00, 0x04 },
389 .sgmii_mode_set = { 0x0000, 5, 0, 0x00, 0x01 },
390 .qsgmii_mode_set = { 0x0000, 5, 0, 0x00, 0x21 },
391 .pipe_rxterm_set = { 0x0000, 12, 12, 0x00, 0x01 },
392 .pipe_txelec_set = { 0x0004, 1, 1, 0x00, 0x01 },
393 .pipe_txcomp_set = { 0x0004, 4, 4, 0x00, 0x01 },
394 .pipe_clk_25m = { 0x0004, 14, 13, 0x00, 0x01 },
395 .pipe_clk_100m = { 0x0004, 14, 13, 0x00, 0x02 },
396 .pipe_phymode_sel = { 0x0008, 1, 1, 0x00, 0x01 },
397 .pipe_rate_sel = { 0x0008, 2, 2, 0x00, 0x01 },
398 .pipe_rxterm_sel = { 0x0008, 8, 8, 0x00, 0x01 },
399 .pipe_txelec_sel = { 0x0008, 12, 12, 0x00, 0x01 },
400 .pipe_txcomp_sel = { 0x0008, 15, 15, 0x00, 0x01 },
401 .pipe_clk_ext = { 0x000c, 9, 8, 0x02, 0x01 },
402 .pipe_sel_usb = { 0x000c, 14, 13, 0x00, 0x01 },
403 .pipe_sel_qsgmii = { 0x000c, 15, 13, 0x00, 0x07 },
404 .pipe_phy_status = { 0x0034, 6, 6, 0x01, 0x00 },
405 .con0_for_pcie = { 0x0000, 15, 0, 0x00, 0x1000 },
406 .con1_for_pcie = { 0x0004, 15, 0, 0x00, 0x0000 },
407 .con2_for_pcie = { 0x0008, 15, 0, 0x00, 0x0101 },
408 .con3_for_pcie = { 0x000c, 15, 0, 0x00, 0x0200 },
409 .con0_for_sata = { 0x0000, 15, 0, 0x00, 0x0119 },
410 .con1_for_sata = { 0x0004, 15, 0, 0x00, 0x0040 },
411 .con2_for_sata = { 0x0008, 15, 0, 0x00, 0x80c3 },
412 .con3_for_sata = { 0x000c, 15, 0, 0x00, 0x4407 },
413 /* pipe-grf */
414 .pipe_con0_for_sata = { 0x0000, 15, 0, 0x00, 0x2220 },
415 .pipe_sgmii_mac_sel = { 0x0040, 1, 1, 0x00, 0x01 },
416 .pipe_xpcs_phy_ready = { 0x0040, 2, 2, 0x00, 0x01 },
417 .u3otg0_port_en = { 0x0104, 15, 0, 0x0181, 0x1100 },
418 .u3otg1_port_en = { 0x0144, 15, 0, 0x0181, 0x1100 },
419};
420
421static const struct rockchip_combphy_cfg rk3568_combphy_cfgs = {
422 .grfcfg = &rk3568_combphy_grfcfgs,
423 .combphy_cfg = rk3568_combphy_cfg,
424};
425
426static const struct udevice_id rockchip_combphy_ids[] = {
427 {
428 .compatible = "rockchip,rk3568-naneng-combphy",
429 .data = (ulong)&rk3568_combphy_cfgs
430 },
431 { }
432};
433
434U_BOOT_DRIVER(rockchip_naneng_combphy) = {
435 .name = "naneng-combphy",
436 .id = UCLASS_PHY,
437 .of_match = rockchip_combphy_ids,
438 .ops = &rochchip_combphy_ops,
439 .probe = rockchip_combphy_probe,
440 .priv_auto = sizeof(struct rockchip_combphy_priv),
441};