blob: e822fd76f2727d932ff745a1e0cae3e812b9af2f [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
14#define PHY_ID_YT8531 0x4f51e91b
15#define PHY_ID_MASK GENMASK(31, 0)
16
17/* Extended Register's Address Offset Register */
18#define YTPHY_PAGE_SELECT 0x1E
19
20/* Extended Register's Data Register */
21#define YTPHY_PAGE_DATA 0x1F
22
23#define YTPHY_SYNCE_CFG_REG 0xA012
24
25#define YTPHY_DTS_OUTPUT_CLK_DIS 0
26#define YTPHY_DTS_OUTPUT_CLK_25M 25000000
27#define YTPHY_DTS_OUTPUT_CLK_125M 125000000
28
29#define YT8531_SCR_SYNCE_ENABLE BIT(6)
30/* 1b0 output 25m clock *default*
31 * 1b1 output 125m clock
32 */
33#define YT8531_SCR_CLK_FRE_SEL_125M BIT(4)
34#define YT8531_SCR_CLK_SRC_MASK GENMASK(3, 1)
35#define YT8531_SCR_CLK_SRC_PLL_125M 0
36#define YT8531_SCR_CLK_SRC_UTP_RX 1
37#define YT8531_SCR_CLK_SRC_SDS_RX 2
38#define YT8531_SCR_CLK_SRC_CLOCK_FROM_DIGITAL 3
39#define YT8531_SCR_CLK_SRC_REF_25M 4
40#define YT8531_SCR_CLK_SRC_SSC_25M 5
41
42/* 1b0 use original tx_clk_rgmii *default*
43 * 1b1 use inverted tx_clk_rgmii.
44 */
45#define YT8531_RC1R_TX_CLK_SEL_INVERTED BIT(14)
46#define YT8531_RC1R_RX_DELAY_MASK GENMASK(13, 10)
47#define YT8531_RC1R_FE_TX_DELAY_MASK GENMASK(7, 4)
48#define YT8531_RC1R_GE_TX_DELAY_MASK GENMASK(3, 0)
49#define YT8531_RC1R_RGMII_0_000_NS 0
50#define YT8531_RC1R_RGMII_0_150_NS 1
51#define YT8531_RC1R_RGMII_0_300_NS 2
52#define YT8531_RC1R_RGMII_0_450_NS 3
53#define YT8531_RC1R_RGMII_0_600_NS 4
54#define YT8531_RC1R_RGMII_0_750_NS 5
55#define YT8531_RC1R_RGMII_0_900_NS 6
56#define YT8531_RC1R_RGMII_1_050_NS 7
57#define YT8531_RC1R_RGMII_1_200_NS 8
58#define YT8531_RC1R_RGMII_1_350_NS 9
59#define YT8531_RC1R_RGMII_1_500_NS 10
60#define YT8531_RC1R_RGMII_1_650_NS 11
61#define YT8531_RC1R_RGMII_1_800_NS 12
62#define YT8531_RC1R_RGMII_1_950_NS 13
63#define YT8531_RC1R_RGMII_2_100_NS 14
64#define YT8531_RC1R_RGMII_2_250_NS 15
65
66/* Phy gmii clock gating Register */
67#define YT8531_CLOCK_GATING_REG 0xC
68#define YT8531_CGR_RX_CLK_EN BIT(12)
69
70/* Specific Status Register */
71#define YTPHY_SPECIFIC_STATUS_REG 0x11
72#define YTPHY_DUPLEX_MASK BIT(13)
73#define YTPHY_DUPLEX_SHIFT 13
74#define YTPHY_SPEED_MODE_MASK GENMASK(15, 14)
75#define YTPHY_SPEED_MODE_SHIFT 14
76
77#define YT8531_EXTREG_SLEEP_CONTROL1_REG 0x27
78#define YT8531_ESC1R_SLEEP_SW BIT(15)
79#define YT8531_ESC1R_PLLON_SLP BIT(14)
80
81#define YT8531_RGMII_CONFIG1_REG 0xA003
82
83#define YT8531_CHIP_CONFIG_REG 0xA001
84#define YT8531_CCR_SW_RST BIT(15)
85/* 1b0 disable 1.9ns rxc clock delay *default*
86 * 1b1 enable 1.9ns rxc clock delay
87 */
88#define YT8531_CCR_RXC_DLY_EN BIT(8)
89#define YT8531_CCR_RXC_DLY_1_900_NS 1900
90
91/* bits in struct ytphy_plat_priv->flag */
92#define TX_CLK_ADJ_ENABLED BIT(0)
93#define AUTO_SLEEP_DISABLED BIT(1)
94#define KEEP_PLL_ENABLED BIT(2)
95#define TX_CLK_10_INVERTED BIT(3)
96#define TX_CLK_100_INVERTED BIT(4)
97#define TX_CLK_1000_INVERTED BIT(5)
98
99struct ytphy_plat_priv {
100 u32 rx_delay_ps;
101 u32 tx_delay_ps;
102 u32 clk_out_frequency;
103 u32 flag;
104};
105
106/**
107 * struct ytphy_cfg_reg_map - map a config value to a register value
108 * @cfg: value in device configuration
109 * @reg: value in the register
110 */
111struct ytphy_cfg_reg_map {
112 u32 cfg;
113 u32 reg;
114};
115
116static const struct ytphy_cfg_reg_map ytphy_rgmii_delays[] = {
117 /* for tx delay / rx delay with YT8531_CCR_RXC_DLY_EN is not set. */
118 { 0, YT8531_RC1R_RGMII_0_000_NS },
119 { 150, YT8531_RC1R_RGMII_0_150_NS },
120 { 300, YT8531_RC1R_RGMII_0_300_NS },
121 { 450, YT8531_RC1R_RGMII_0_450_NS },
122 { 600, YT8531_RC1R_RGMII_0_600_NS },
123 { 750, YT8531_RC1R_RGMII_0_750_NS },
124 { 900, YT8531_RC1R_RGMII_0_900_NS },
125 { 1050, YT8531_RC1R_RGMII_1_050_NS },
126 { 1200, YT8531_RC1R_RGMII_1_200_NS },
127 { 1350, YT8531_RC1R_RGMII_1_350_NS },
128 { 1500, YT8531_RC1R_RGMII_1_500_NS },
129 { 1650, YT8531_RC1R_RGMII_1_650_NS },
130 { 1800, YT8531_RC1R_RGMII_1_800_NS },
131 { 1950, YT8531_RC1R_RGMII_1_950_NS }, /* default tx/rx delay */
132 { 2100, YT8531_RC1R_RGMII_2_100_NS },
133 { 2250, YT8531_RC1R_RGMII_2_250_NS },
134
135 /* only for rx delay with YT8531_CCR_RXC_DLY_EN is set. */
136 { 0 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_000_NS },
137 { 150 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_150_NS },
138 { 300 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_300_NS },
139 { 450 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_450_NS },
140 { 600 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_600_NS },
141 { 750 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_750_NS },
142 { 900 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_900_NS },
143 { 1050 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_050_NS },
144 { 1200 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_200_NS },
145 { 1350 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_350_NS },
146 { 1500 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_500_NS },
147 { 1650 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_650_NS },
148 { 1800 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_800_NS },
149 { 1950 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_950_NS },
150 { 2100 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_2_100_NS },
151 { 2250 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_2_250_NS }
152};
153
154static u32 ytphy_get_delay_reg_value(struct phy_device *phydev,
155 u32 val,
156 u16 *rxc_dly_en)
157{
158 int tb_size = ARRAY_SIZE(ytphy_rgmii_delays);
159 int tb_size_half = tb_size / 2;
160 int i;
161
162 /* when rxc_dly_en is NULL, it is get the delay for tx, only half of
163 * tb_size is valid.
164 */
165 if (!rxc_dly_en)
166 tb_size = tb_size_half;
167
168 for (i = 0; i < tb_size; i++) {
169 if (ytphy_rgmii_delays[i].cfg == val) {
170 if (rxc_dly_en && i < tb_size_half)
171 *rxc_dly_en = 0;
172 return ytphy_rgmii_delays[i].reg;
173 }
174 }
175
176 pr_warn("Unsupported value %d, using default (%u)\n",
177 val, YT8531_RC1R_RGMII_1_950_NS);
178
179 /* when rxc_dly_en is not NULL, it is get the delay for rx.
180 * The rx default in dts and ytphy_rgmii_clk_delay_config is 1950 ps,
181 * so YT8531_CCR_RXC_DLY_EN should not be set.
182 */
183 if (rxc_dly_en)
184 *rxc_dly_en = 0;
185
186 return YT8531_RC1R_RGMII_1_950_NS;
187}
188
189static int ytphy_modify_ext(struct phy_device *phydev, u16 regnum, u16 mask,
190 u16 set)
191{
192 int ret;
193
194 ret = phy_write(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_SELECT, regnum);
195 if (ret < 0)
196 return ret;
197
198 return phy_modify(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_DATA, mask, set);
199}
200
201static int ytphy_rgmii_clk_delay_config(struct phy_device *phydev)
202{
203 struct ytphy_plat_priv *priv = phydev->priv;
204 u16 rxc_dly_en = YT8531_CCR_RXC_DLY_EN;
205 u32 rx_reg, tx_reg;
206 u16 mask, val = 0;
207 int ret;
208
209 rx_reg = ytphy_get_delay_reg_value(phydev, priv->rx_delay_ps,
210 &rxc_dly_en);
211 tx_reg = ytphy_get_delay_reg_value(phydev, priv->tx_delay_ps,
212 NULL);
213
214 switch (phydev->interface) {
215 case PHY_INTERFACE_MODE_RGMII:
216 rxc_dly_en = 0;
217 break;
218 case PHY_INTERFACE_MODE_RGMII_RXID:
219 val |= FIELD_PREP(YT8531_RC1R_RX_DELAY_MASK, rx_reg);
220 break;
221 case PHY_INTERFACE_MODE_RGMII_TXID:
222 rxc_dly_en = 0;
223 val |= FIELD_PREP(YT8531_RC1R_GE_TX_DELAY_MASK, tx_reg);
224 break;
225 case PHY_INTERFACE_MODE_RGMII_ID:
226 val |= FIELD_PREP(YT8531_RC1R_RX_DELAY_MASK, rx_reg) |
227 FIELD_PREP(YT8531_RC1R_GE_TX_DELAY_MASK, tx_reg);
228 break;
229 default: /* do not support other modes */
230 return -EOPNOTSUPP;
231 }
232
233 ret = ytphy_modify_ext(phydev, YT8531_CHIP_CONFIG_REG,
234 YT8531_CCR_RXC_DLY_EN, rxc_dly_en);
235 if (ret < 0)
236 return ret;
237
238 /* Generally, it is not necessary to adjust YT8531_RC1R_FE_TX_DELAY */
239 mask = YT8531_RC1R_RX_DELAY_MASK | YT8531_RC1R_GE_TX_DELAY_MASK;
240 return ytphy_modify_ext(phydev, YT8531_RGMII_CONFIG1_REG, mask, val);
241}
242
243static int yt8531_parse_status(struct phy_device *phydev)
244{
245 int val;
246 int speed, speed_mode;
247
248 val = phy_read(phydev, MDIO_DEVAD_NONE, YTPHY_SPECIFIC_STATUS_REG);
249 if (val < 0)
250 return val;
251
252 speed_mode = (val & YTPHY_SPEED_MODE_MASK) >> YTPHY_SPEED_MODE_SHIFT;
253 switch (speed_mode) {
254 case 2:
255 speed = SPEED_1000;
256 break;
257 case 1:
258 speed = SPEED_100;
259 break;
260 default:
261 speed = SPEED_10;
262 break;
263 }
264
265 phydev->speed = speed;
266 phydev->duplex = (val & YTPHY_DUPLEX_MASK) >> YTPHY_DUPLEX_SHIFT;
267
268 return 0;
269}
270
271static int yt8531_startup(struct phy_device *phydev)
272{
273 struct ytphy_plat_priv *priv = phydev->priv;
274 u16 val = 0;
275 int ret;
276
277 ret = genphy_update_link(phydev);
278 if (ret)
279 return ret;
280
281 ret = yt8531_parse_status(phydev);
282 if (ret)
283 return ret;
284
285 if (phydev->speed < 0)
286 return -EINVAL;
287
288 if (!(priv->flag & TX_CLK_ADJ_ENABLED))
289 return 0;
290
291 switch (phydev->speed) {
292 case SPEED_1000:
293 if (priv->flag & TX_CLK_1000_INVERTED)
294 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
295 break;
296 case SPEED_100:
297 if (priv->flag & TX_CLK_100_INVERTED)
298 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
299 break;
300 case SPEED_10:
301 if (priv->flag & TX_CLK_10_INVERTED)
302 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
303 break;
304 default:
305 printf("UNKNOWN SPEED\n");
306 return -EINVAL;
307 }
308
309 ret = ytphy_modify_ext(phydev, YT8531_RGMII_CONFIG1_REG,
310 YT8531_RC1R_TX_CLK_SEL_INVERTED, val);
311 if (ret < 0)
312 pr_warn("Modify TX_CLK_SEL err:%d\n", ret);
313
314 return 0;
315}
316
317static void ytphy_dt_parse(struct phy_device *phydev)
318{
319 struct ytphy_plat_priv *priv = phydev->priv;
320
321 priv->clk_out_frequency = ofnode_read_u32_default(phydev->node,
322 "motorcomm,clk-out-frequency-hz",
323 YTPHY_DTS_OUTPUT_CLK_DIS);
324 priv->rx_delay_ps = ofnode_read_u32_default(phydev->node,
325 "rx-internal-delay-ps",
326 YT8531_RC1R_RGMII_1_950_NS);
327 priv->tx_delay_ps = ofnode_read_u32_default(phydev->node,
328 "tx-internal-delay-ps",
329 YT8531_RC1R_RGMII_1_950_NS);
330
331 if (ofnode_read_bool(phydev->node, "motorcomm,auto-sleep-disabled"))
332 priv->flag |= AUTO_SLEEP_DISABLED;
333
334 if (ofnode_read_bool(phydev->node, "motorcomm,keep-pll-enabled"))
335 priv->flag |= KEEP_PLL_ENABLED;
336
337 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-adj-enabled"))
338 priv->flag |= TX_CLK_ADJ_ENABLED;
339
340 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-10-inverted"))
341 priv->flag |= TX_CLK_10_INVERTED;
342
343 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-100-inverted"))
344 priv->flag |= TX_CLK_100_INVERTED;
345
346 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-1000-inverted"))
347 priv->flag |= TX_CLK_1000_INVERTED;
348}
349
350static int yt8531_config(struct phy_device *phydev)
351{
352 struct ytphy_plat_priv *priv = phydev->priv;
353 u16 mask, val;
354 int ret;
355
356 ret = genphy_config_aneg(phydev);
357 if (ret < 0)
358 return ret;
359
360 ytphy_dt_parse(phydev);
361 switch (priv->clk_out_frequency) {
362 case YTPHY_DTS_OUTPUT_CLK_DIS:
363 mask = YT8531_SCR_SYNCE_ENABLE;
364 val = 0;
365 break;
366 case YTPHY_DTS_OUTPUT_CLK_25M:
367 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
368 YT8531_SCR_CLK_FRE_SEL_125M;
369 val = YT8531_SCR_SYNCE_ENABLE |
370 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
371 YT8531_SCR_CLK_SRC_REF_25M);
372 break;
373 case YTPHY_DTS_OUTPUT_CLK_125M:
374 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
375 YT8531_SCR_CLK_FRE_SEL_125M;
376 val = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_FRE_SEL_125M |
377 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
378 YT8531_SCR_CLK_SRC_PLL_125M);
379 break;
380 default:
381 pr_warn("Freq err:%u\n", priv->clk_out_frequency);
382 return -EINVAL;
383 }
384
385 ret = ytphy_modify_ext(phydev, YTPHY_SYNCE_CFG_REG, mask,
386 val);
387 if (ret < 0)
388 return ret;
389
390 ret = ytphy_rgmii_clk_delay_config(phydev);
391 if (ret < 0)
392 return ret;
393
394 if (priv->flag & AUTO_SLEEP_DISABLED) {
395 /* disable auto sleep */
396 ret = ytphy_modify_ext(phydev,
397 YT8531_EXTREG_SLEEP_CONTROL1_REG,
398 YT8531_ESC1R_SLEEP_SW, 0);
399 if (ret < 0)
400 return ret;
401 }
402
403 if (priv->flag & KEEP_PLL_ENABLED) {
404 /* enable RXC clock when no wire plug */
405 ret = ytphy_modify_ext(phydev,
406 YT8531_CLOCK_GATING_REG,
407 YT8531_CGR_RX_CLK_EN, 0);
408 if (ret < 0)
409 return ret;
410 }
411
412 return 0;
413}
414
415static int yt8531_probe(struct phy_device *phydev)
416{
417 struct ytphy_plat_priv *priv;
418
419 priv = calloc(1, sizeof(struct ytphy_plat_priv));
420 if (!priv)
421 return -ENOMEM;
422
423 phydev->priv = priv;
424
425 return 0;
426}
427
428U_BOOT_PHY_DRIVER(motorcomm8531) = {
429 .name = "YT8531 Gigabit Ethernet",
430 .uid = PHY_ID_YT8531,
431 .mask = PHY_ID_MASK,
432 .features = PHY_GBIT_FEATURES,
433 .probe = &yt8531_probe,
434 .config = &yt8531_config,
435 .startup = &yt8531_startup,
436 .shutdown = &genphy_shutdown,
437};