blob: 8635a960d6e417a2097e1e5560bd57df883e39df [file] [log] [blame]
Yanhong Wang1d6c3412023-06-15 17:36:42 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Motorcomm 8531 PHY driver.
4 *
5 * Copyright (C) 2023 StarFive Technology Co., Ltd.
6 */
7
8#include <config.h>
9#include <common.h>
10#include <malloc.h>
11#include <phy.h>
12#include <linux/bitfield.h>
13
Nicolas Frattarolif08686a2023-08-05 12:35:01 +020014#define PHY_ID_YT8511 0x0000010a
Yanhong Wang1d6c3412023-06-15 17:36:42 +080015#define PHY_ID_YT8531 0x4f51e91b
16#define PHY_ID_MASK GENMASK(31, 0)
17
18/* Extended Register's Address Offset Register */
19#define YTPHY_PAGE_SELECT 0x1E
20
21/* Extended Register's Data Register */
22#define YTPHY_PAGE_DATA 0x1F
23
24#define YTPHY_SYNCE_CFG_REG 0xA012
25
26#define YTPHY_DTS_OUTPUT_CLK_DIS 0
27#define YTPHY_DTS_OUTPUT_CLK_25M 25000000
28#define YTPHY_DTS_OUTPUT_CLK_125M 125000000
29
Nicolas Frattarolif08686a2023-08-05 12:35:01 +020030#define YT8511_EXT_CLK_GATE 0x0c
31#define YT8511_EXT_DELAY_DRIVE 0x0d
32#define YT8511_EXT_SLEEP_CTRL 0x27
33
34/* 2b00 25m from pll
35 * 2b01 25m from xtl *default*
36 * 2b10 62.m from pll
37 * 2b11 125m from pll
38 */
39#define YT8511_CLK_125M (BIT(2) | BIT(1))
40#define YT8511_PLLON_SLP BIT(14)
41
42/* RX Delay enabled = 1.8ns 1000T, 8ns 10/100T */
43#define YT8511_DELAY_RX BIT(0)
44
45/* TX Gig-E Delay is bits 7:4, default 0x5
46 * TX Fast-E Delay is bits 15:12, default 0xf
47 * Delay = 150ps * N - 250ps
48 * On = 2000ps, off = 50ps
49 */
50#define YT8511_DELAY_GE_TX_EN (0xf << 4)
51#define YT8511_DELAY_GE_TX_DIS (0x2 << 4)
52#define YT8511_DELAY_FE_TX_EN (0xf << 12)
53#define YT8511_DELAY_FE_TX_DIS (0x2 << 12)
54
Yanhong Wang1d6c3412023-06-15 17:36:42 +080055#define YT8531_SCR_SYNCE_ENABLE BIT(6)
56/* 1b0 output 25m clock *default*
57 * 1b1 output 125m clock
58 */
59#define YT8531_SCR_CLK_FRE_SEL_125M BIT(4)
60#define YT8531_SCR_CLK_SRC_MASK GENMASK(3, 1)
61#define YT8531_SCR_CLK_SRC_PLL_125M 0
62#define YT8531_SCR_CLK_SRC_UTP_RX 1
63#define YT8531_SCR_CLK_SRC_SDS_RX 2
64#define YT8531_SCR_CLK_SRC_CLOCK_FROM_DIGITAL 3
65#define YT8531_SCR_CLK_SRC_REF_25M 4
66#define YT8531_SCR_CLK_SRC_SSC_25M 5
67
68/* 1b0 use original tx_clk_rgmii *default*
69 * 1b1 use inverted tx_clk_rgmii.
70 */
71#define YT8531_RC1R_TX_CLK_SEL_INVERTED BIT(14)
72#define YT8531_RC1R_RX_DELAY_MASK GENMASK(13, 10)
73#define YT8531_RC1R_FE_TX_DELAY_MASK GENMASK(7, 4)
74#define YT8531_RC1R_GE_TX_DELAY_MASK GENMASK(3, 0)
75#define YT8531_RC1R_RGMII_0_000_NS 0
76#define YT8531_RC1R_RGMII_0_150_NS 1
77#define YT8531_RC1R_RGMII_0_300_NS 2
78#define YT8531_RC1R_RGMII_0_450_NS 3
79#define YT8531_RC1R_RGMII_0_600_NS 4
80#define YT8531_RC1R_RGMII_0_750_NS 5
81#define YT8531_RC1R_RGMII_0_900_NS 6
82#define YT8531_RC1R_RGMII_1_050_NS 7
83#define YT8531_RC1R_RGMII_1_200_NS 8
84#define YT8531_RC1R_RGMII_1_350_NS 9
85#define YT8531_RC1R_RGMII_1_500_NS 10
86#define YT8531_RC1R_RGMII_1_650_NS 11
87#define YT8531_RC1R_RGMII_1_800_NS 12
88#define YT8531_RC1R_RGMII_1_950_NS 13
89#define YT8531_RC1R_RGMII_2_100_NS 14
90#define YT8531_RC1R_RGMII_2_250_NS 15
91
92/* Phy gmii clock gating Register */
93#define YT8531_CLOCK_GATING_REG 0xC
94#define YT8531_CGR_RX_CLK_EN BIT(12)
95
96/* Specific Status Register */
97#define YTPHY_SPECIFIC_STATUS_REG 0x11
98#define YTPHY_DUPLEX_MASK BIT(13)
99#define YTPHY_DUPLEX_SHIFT 13
100#define YTPHY_SPEED_MODE_MASK GENMASK(15, 14)
101#define YTPHY_SPEED_MODE_SHIFT 14
102
103#define YT8531_EXTREG_SLEEP_CONTROL1_REG 0x27
104#define YT8531_ESC1R_SLEEP_SW BIT(15)
105#define YT8531_ESC1R_PLLON_SLP BIT(14)
106
107#define YT8531_RGMII_CONFIG1_REG 0xA003
108
109#define YT8531_CHIP_CONFIG_REG 0xA001
110#define YT8531_CCR_SW_RST BIT(15)
111/* 1b0 disable 1.9ns rxc clock delay *default*
112 * 1b1 enable 1.9ns rxc clock delay
113 */
114#define YT8531_CCR_RXC_DLY_EN BIT(8)
115#define YT8531_CCR_RXC_DLY_1_900_NS 1900
116
117/* bits in struct ytphy_plat_priv->flag */
118#define TX_CLK_ADJ_ENABLED BIT(0)
119#define AUTO_SLEEP_DISABLED BIT(1)
120#define KEEP_PLL_ENABLED BIT(2)
121#define TX_CLK_10_INVERTED BIT(3)
122#define TX_CLK_100_INVERTED BIT(4)
123#define TX_CLK_1000_INVERTED BIT(5)
124
125struct ytphy_plat_priv {
126 u32 rx_delay_ps;
127 u32 tx_delay_ps;
128 u32 clk_out_frequency;
129 u32 flag;
130};
131
132/**
133 * struct ytphy_cfg_reg_map - map a config value to a register value
134 * @cfg: value in device configuration
135 * @reg: value in the register
136 */
137struct ytphy_cfg_reg_map {
138 u32 cfg;
139 u32 reg;
140};
141
142static const struct ytphy_cfg_reg_map ytphy_rgmii_delays[] = {
143 /* for tx delay / rx delay with YT8531_CCR_RXC_DLY_EN is not set. */
144 { 0, YT8531_RC1R_RGMII_0_000_NS },
145 { 150, YT8531_RC1R_RGMII_0_150_NS },
146 { 300, YT8531_RC1R_RGMII_0_300_NS },
147 { 450, YT8531_RC1R_RGMII_0_450_NS },
148 { 600, YT8531_RC1R_RGMII_0_600_NS },
149 { 750, YT8531_RC1R_RGMII_0_750_NS },
150 { 900, YT8531_RC1R_RGMII_0_900_NS },
151 { 1050, YT8531_RC1R_RGMII_1_050_NS },
152 { 1200, YT8531_RC1R_RGMII_1_200_NS },
153 { 1350, YT8531_RC1R_RGMII_1_350_NS },
154 { 1500, YT8531_RC1R_RGMII_1_500_NS },
155 { 1650, YT8531_RC1R_RGMII_1_650_NS },
156 { 1800, YT8531_RC1R_RGMII_1_800_NS },
157 { 1950, YT8531_RC1R_RGMII_1_950_NS }, /* default tx/rx delay */
158 { 2100, YT8531_RC1R_RGMII_2_100_NS },
159 { 2250, YT8531_RC1R_RGMII_2_250_NS },
160
161 /* only for rx delay with YT8531_CCR_RXC_DLY_EN is set. */
162 { 0 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_000_NS },
163 { 150 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_150_NS },
164 { 300 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_300_NS },
165 { 450 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_450_NS },
166 { 600 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_600_NS },
167 { 750 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_750_NS },
168 { 900 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_900_NS },
169 { 1050 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_050_NS },
170 { 1200 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_200_NS },
171 { 1350 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_350_NS },
172 { 1500 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_500_NS },
173 { 1650 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_650_NS },
174 { 1800 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_800_NS },
175 { 1950 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_950_NS },
176 { 2100 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_2_100_NS },
177 { 2250 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_2_250_NS }
178};
179
180static u32 ytphy_get_delay_reg_value(struct phy_device *phydev,
181 u32 val,
182 u16 *rxc_dly_en)
183{
184 int tb_size = ARRAY_SIZE(ytphy_rgmii_delays);
185 int tb_size_half = tb_size / 2;
186 int i;
187
188 /* when rxc_dly_en is NULL, it is get the delay for tx, only half of
189 * tb_size is valid.
190 */
191 if (!rxc_dly_en)
192 tb_size = tb_size_half;
193
194 for (i = 0; i < tb_size; i++) {
195 if (ytphy_rgmii_delays[i].cfg == val) {
196 if (rxc_dly_en && i < tb_size_half)
197 *rxc_dly_en = 0;
198 return ytphy_rgmii_delays[i].reg;
199 }
200 }
201
202 pr_warn("Unsupported value %d, using default (%u)\n",
203 val, YT8531_RC1R_RGMII_1_950_NS);
204
205 /* when rxc_dly_en is not NULL, it is get the delay for rx.
206 * The rx default in dts and ytphy_rgmii_clk_delay_config is 1950 ps,
207 * so YT8531_CCR_RXC_DLY_EN should not be set.
208 */
209 if (rxc_dly_en)
210 *rxc_dly_en = 0;
211
212 return YT8531_RC1R_RGMII_1_950_NS;
213}
214
215static int ytphy_modify_ext(struct phy_device *phydev, u16 regnum, u16 mask,
216 u16 set)
217{
218 int ret;
219
220 ret = phy_write(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_SELECT, regnum);
221 if (ret < 0)
222 return ret;
223
224 return phy_modify(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_DATA, mask, set);
225}
226
227static int ytphy_rgmii_clk_delay_config(struct phy_device *phydev)
228{
229 struct ytphy_plat_priv *priv = phydev->priv;
230 u16 rxc_dly_en = YT8531_CCR_RXC_DLY_EN;
231 u32 rx_reg, tx_reg;
232 u16 mask, val = 0;
233 int ret;
234
235 rx_reg = ytphy_get_delay_reg_value(phydev, priv->rx_delay_ps,
236 &rxc_dly_en);
237 tx_reg = ytphy_get_delay_reg_value(phydev, priv->tx_delay_ps,
238 NULL);
239
240 switch (phydev->interface) {
241 case PHY_INTERFACE_MODE_RGMII:
242 rxc_dly_en = 0;
243 break;
244 case PHY_INTERFACE_MODE_RGMII_RXID:
245 val |= FIELD_PREP(YT8531_RC1R_RX_DELAY_MASK, rx_reg);
246 break;
247 case PHY_INTERFACE_MODE_RGMII_TXID:
248 rxc_dly_en = 0;
249 val |= FIELD_PREP(YT8531_RC1R_GE_TX_DELAY_MASK, tx_reg);
250 break;
251 case PHY_INTERFACE_MODE_RGMII_ID:
252 val |= FIELD_PREP(YT8531_RC1R_RX_DELAY_MASK, rx_reg) |
253 FIELD_PREP(YT8531_RC1R_GE_TX_DELAY_MASK, tx_reg);
254 break;
255 default: /* do not support other modes */
256 return -EOPNOTSUPP;
257 }
258
259 ret = ytphy_modify_ext(phydev, YT8531_CHIP_CONFIG_REG,
260 YT8531_CCR_RXC_DLY_EN, rxc_dly_en);
261 if (ret < 0)
262 return ret;
263
264 /* Generally, it is not necessary to adjust YT8531_RC1R_FE_TX_DELAY */
265 mask = YT8531_RC1R_RX_DELAY_MASK | YT8531_RC1R_GE_TX_DELAY_MASK;
266 return ytphy_modify_ext(phydev, YT8531_RGMII_CONFIG1_REG, mask, val);
267}
268
269static int yt8531_parse_status(struct phy_device *phydev)
270{
271 int val;
272 int speed, speed_mode;
273
274 val = phy_read(phydev, MDIO_DEVAD_NONE, YTPHY_SPECIFIC_STATUS_REG);
275 if (val < 0)
276 return val;
277
278 speed_mode = (val & YTPHY_SPEED_MODE_MASK) >> YTPHY_SPEED_MODE_SHIFT;
279 switch (speed_mode) {
280 case 2:
281 speed = SPEED_1000;
282 break;
283 case 1:
284 speed = SPEED_100;
285 break;
286 default:
287 speed = SPEED_10;
288 break;
289 }
290
291 phydev->speed = speed;
292 phydev->duplex = (val & YTPHY_DUPLEX_MASK) >> YTPHY_DUPLEX_SHIFT;
293
294 return 0;
295}
296
297static int yt8531_startup(struct phy_device *phydev)
298{
299 struct ytphy_plat_priv *priv = phydev->priv;
300 u16 val = 0;
301 int ret;
302
303 ret = genphy_update_link(phydev);
304 if (ret)
305 return ret;
306
307 ret = yt8531_parse_status(phydev);
308 if (ret)
309 return ret;
310
311 if (phydev->speed < 0)
312 return -EINVAL;
313
314 if (!(priv->flag & TX_CLK_ADJ_ENABLED))
315 return 0;
316
317 switch (phydev->speed) {
318 case SPEED_1000:
319 if (priv->flag & TX_CLK_1000_INVERTED)
320 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
321 break;
322 case SPEED_100:
323 if (priv->flag & TX_CLK_100_INVERTED)
324 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
325 break;
326 case SPEED_10:
327 if (priv->flag & TX_CLK_10_INVERTED)
328 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
329 break;
330 default:
331 printf("UNKNOWN SPEED\n");
332 return -EINVAL;
333 }
334
335 ret = ytphy_modify_ext(phydev, YT8531_RGMII_CONFIG1_REG,
336 YT8531_RC1R_TX_CLK_SEL_INVERTED, val);
337 if (ret < 0)
338 pr_warn("Modify TX_CLK_SEL err:%d\n", ret);
339
340 return 0;
341}
342
343static void ytphy_dt_parse(struct phy_device *phydev)
344{
345 struct ytphy_plat_priv *priv = phydev->priv;
346
347 priv->clk_out_frequency = ofnode_read_u32_default(phydev->node,
348 "motorcomm,clk-out-frequency-hz",
349 YTPHY_DTS_OUTPUT_CLK_DIS);
350 priv->rx_delay_ps = ofnode_read_u32_default(phydev->node,
351 "rx-internal-delay-ps",
352 YT8531_RC1R_RGMII_1_950_NS);
353 priv->tx_delay_ps = ofnode_read_u32_default(phydev->node,
354 "tx-internal-delay-ps",
355 YT8531_RC1R_RGMII_1_950_NS);
356
357 if (ofnode_read_bool(phydev->node, "motorcomm,auto-sleep-disabled"))
358 priv->flag |= AUTO_SLEEP_DISABLED;
359
360 if (ofnode_read_bool(phydev->node, "motorcomm,keep-pll-enabled"))
361 priv->flag |= KEEP_PLL_ENABLED;
362
363 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-adj-enabled"))
364 priv->flag |= TX_CLK_ADJ_ENABLED;
365
366 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-10-inverted"))
367 priv->flag |= TX_CLK_10_INVERTED;
368
369 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-100-inverted"))
370 priv->flag |= TX_CLK_100_INVERTED;
371
372 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-1000-inverted"))
373 priv->flag |= TX_CLK_1000_INVERTED;
374}
375
Nicolas Frattarolif08686a2023-08-05 12:35:01 +0200376static int yt8511_config(struct phy_device *phydev)
377{
378 u32 ge, fe;
379 int ret;
380
381 ret = genphy_config_aneg(phydev);
382 if (ret < 0)
383 return ret;
384
385 switch (phydev->interface) {
386 case PHY_INTERFACE_MODE_RGMII:
387 ge = YT8511_DELAY_GE_TX_DIS;
388 fe = YT8511_DELAY_FE_TX_DIS;
389 break;
390 case PHY_INTERFACE_MODE_RGMII_RXID:
391 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_DIS;
392 fe = YT8511_DELAY_FE_TX_DIS;
393 break;
394 case PHY_INTERFACE_MODE_RGMII_TXID:
395 ge = YT8511_DELAY_GE_TX_EN;
396 fe = YT8511_DELAY_FE_TX_EN;
397 break;
398 case PHY_INTERFACE_MODE_RGMII_ID:
399 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN;
400 fe = YT8511_DELAY_FE_TX_EN;
401 break;
402 default: /* do not support other modes */
403 return -EOPNOTSUPP;
404 }
405
406 ret = ytphy_modify_ext(phydev, YT8511_EXT_CLK_GATE,
407 (YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN), ge);
408 if (ret < 0)
409 return ret;
410 /* set clock mode to 125m */
411 ret = ytphy_modify_ext(phydev, YT8511_EXT_CLK_GATE,
412 YT8511_CLK_125M, YT8511_CLK_125M);
413 if (ret < 0)
414 return ret;
415 ret = ytphy_modify_ext(phydev, YT8511_EXT_DELAY_DRIVE,
416 YT8511_DELAY_FE_TX_EN, fe);
417 if (ret < 0)
418 return ret;
419 /* sleep control, disable PLL in sleep for now */
420 ret = ytphy_modify_ext(phydev, YT8511_EXT_SLEEP_CTRL, YT8511_PLLON_SLP,
421 0);
422 if (ret < 0)
423 return ret;
424
425 return 0;
426}
427
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800428static int yt8531_config(struct phy_device *phydev)
429{
430 struct ytphy_plat_priv *priv = phydev->priv;
431 u16 mask, val;
432 int ret;
433
434 ret = genphy_config_aneg(phydev);
435 if (ret < 0)
436 return ret;
437
438 ytphy_dt_parse(phydev);
439 switch (priv->clk_out_frequency) {
440 case YTPHY_DTS_OUTPUT_CLK_DIS:
441 mask = YT8531_SCR_SYNCE_ENABLE;
442 val = 0;
443 break;
444 case YTPHY_DTS_OUTPUT_CLK_25M:
445 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
446 YT8531_SCR_CLK_FRE_SEL_125M;
447 val = YT8531_SCR_SYNCE_ENABLE |
448 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
449 YT8531_SCR_CLK_SRC_REF_25M);
450 break;
451 case YTPHY_DTS_OUTPUT_CLK_125M:
452 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
453 YT8531_SCR_CLK_FRE_SEL_125M;
454 val = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_FRE_SEL_125M |
455 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
456 YT8531_SCR_CLK_SRC_PLL_125M);
457 break;
458 default:
459 pr_warn("Freq err:%u\n", priv->clk_out_frequency);
460 return -EINVAL;
461 }
462
463 ret = ytphy_modify_ext(phydev, YTPHY_SYNCE_CFG_REG, mask,
464 val);
465 if (ret < 0)
466 return ret;
467
468 ret = ytphy_rgmii_clk_delay_config(phydev);
469 if (ret < 0)
470 return ret;
471
472 if (priv->flag & AUTO_SLEEP_DISABLED) {
473 /* disable auto sleep */
474 ret = ytphy_modify_ext(phydev,
475 YT8531_EXTREG_SLEEP_CONTROL1_REG,
476 YT8531_ESC1R_SLEEP_SW, 0);
477 if (ret < 0)
478 return ret;
479 }
480
481 if (priv->flag & KEEP_PLL_ENABLED) {
482 /* enable RXC clock when no wire plug */
483 ret = ytphy_modify_ext(phydev,
484 YT8531_CLOCK_GATING_REG,
485 YT8531_CGR_RX_CLK_EN, 0);
486 if (ret < 0)
487 return ret;
488 }
489
490 return 0;
491}
492
493static int yt8531_probe(struct phy_device *phydev)
494{
495 struct ytphy_plat_priv *priv;
496
497 priv = calloc(1, sizeof(struct ytphy_plat_priv));
498 if (!priv)
499 return -ENOMEM;
500
501 phydev->priv = priv;
502
503 return 0;
504}
505
Nicolas Frattarolif08686a2023-08-05 12:35:01 +0200506U_BOOT_PHY_DRIVER(motorcomm8511) = {
507 .name = "YT8511 Gigabit Ethernet",
508 .uid = PHY_ID_YT8511,
509 .mask = PHY_ID_MASK,
510 .features = PHY_GBIT_FEATURES,
511 .config = &yt8511_config,
512 .startup = &genphy_startup,
513 .shutdown = &genphy_shutdown,
514};
515
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800516U_BOOT_PHY_DRIVER(motorcomm8531) = {
517 .name = "YT8531 Gigabit Ethernet",
518 .uid = PHY_ID_YT8531,
519 .mask = PHY_ID_MASK,
520 .features = PHY_GBIT_FEATURES,
521 .probe = &yt8531_probe,
522 .config = &yt8531_config,
523 .startup = &yt8531_startup,
524 .shutdown = &genphy_shutdown,
525};