blob: a96430cec43dcdd80e51c65f7c3f9e8d572c5552 [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>
Yanhong Wang1d6c3412023-06-15 17:36:42 +08009#include <malloc.h>
10#include <phy.h>
11#include <linux/bitfield.h>
12
Nicolas Frattarolif08686a2023-08-05 12:35:01 +020013#define PHY_ID_YT8511 0x0000010a
Yanhong Wang1d6c3412023-06-15 17:36:42 +080014#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
Lukasz Tekieli3282aee2024-01-28 20:22:47 +010025#define YT8531_PAD_DRIVE_STRENGTH_CFG_REG 0xA010
26#define YT8531_RGMII_RXC_DS_MASK GENMASK(15, 13)
27#define YT8531_RGMII_RXD_DS_HI_MASK BIT(12) /* Bit 2 of rxd_ds */
28#define YT8531_RGMII_RXD_DS_LOW_MASK GENMASK(5, 4) /* Bit 1/0 of rxd_ds */
29#define YT8531_RGMII_RX_DS_DEFAULT 0x3
30
Yanhong Wang1d6c3412023-06-15 17:36:42 +080031#define YTPHY_DTS_OUTPUT_CLK_DIS 0
32#define YTPHY_DTS_OUTPUT_CLK_25M 25000000
33#define YTPHY_DTS_OUTPUT_CLK_125M 125000000
34
Nicolas Frattarolif08686a2023-08-05 12:35:01 +020035#define YT8511_EXT_CLK_GATE 0x0c
36#define YT8511_EXT_DELAY_DRIVE 0x0d
37#define YT8511_EXT_SLEEP_CTRL 0x27
38
39/* 2b00 25m from pll
40 * 2b01 25m from xtl *default*
41 * 2b10 62.m from pll
42 * 2b11 125m from pll
43 */
44#define YT8511_CLK_125M (BIT(2) | BIT(1))
45#define YT8511_PLLON_SLP BIT(14)
46
47/* RX Delay enabled = 1.8ns 1000T, 8ns 10/100T */
48#define YT8511_DELAY_RX BIT(0)
49
50/* TX Gig-E Delay is bits 7:4, default 0x5
51 * TX Fast-E Delay is bits 15:12, default 0xf
52 * Delay = 150ps * N - 250ps
53 * On = 2000ps, off = 50ps
54 */
55#define YT8511_DELAY_GE_TX_EN (0xf << 4)
56#define YT8511_DELAY_GE_TX_DIS (0x2 << 4)
57#define YT8511_DELAY_FE_TX_EN (0xf << 12)
58#define YT8511_DELAY_FE_TX_DIS (0x2 << 12)
59
Yanhong Wang1d6c3412023-06-15 17:36:42 +080060#define YT8531_SCR_SYNCE_ENABLE BIT(6)
61/* 1b0 output 25m clock *default*
62 * 1b1 output 125m clock
63 */
64#define YT8531_SCR_CLK_FRE_SEL_125M BIT(4)
65#define YT8531_SCR_CLK_SRC_MASK GENMASK(3, 1)
66#define YT8531_SCR_CLK_SRC_PLL_125M 0
67#define YT8531_SCR_CLK_SRC_UTP_RX 1
68#define YT8531_SCR_CLK_SRC_SDS_RX 2
69#define YT8531_SCR_CLK_SRC_CLOCK_FROM_DIGITAL 3
70#define YT8531_SCR_CLK_SRC_REF_25M 4
71#define YT8531_SCR_CLK_SRC_SSC_25M 5
72
73/* 1b0 use original tx_clk_rgmii *default*
74 * 1b1 use inverted tx_clk_rgmii.
75 */
76#define YT8531_RC1R_TX_CLK_SEL_INVERTED BIT(14)
77#define YT8531_RC1R_RX_DELAY_MASK GENMASK(13, 10)
78#define YT8531_RC1R_FE_TX_DELAY_MASK GENMASK(7, 4)
79#define YT8531_RC1R_GE_TX_DELAY_MASK GENMASK(3, 0)
80#define YT8531_RC1R_RGMII_0_000_NS 0
81#define YT8531_RC1R_RGMII_0_150_NS 1
82#define YT8531_RC1R_RGMII_0_300_NS 2
83#define YT8531_RC1R_RGMII_0_450_NS 3
84#define YT8531_RC1R_RGMII_0_600_NS 4
85#define YT8531_RC1R_RGMII_0_750_NS 5
86#define YT8531_RC1R_RGMII_0_900_NS 6
87#define YT8531_RC1R_RGMII_1_050_NS 7
88#define YT8531_RC1R_RGMII_1_200_NS 8
89#define YT8531_RC1R_RGMII_1_350_NS 9
90#define YT8531_RC1R_RGMII_1_500_NS 10
91#define YT8531_RC1R_RGMII_1_650_NS 11
92#define YT8531_RC1R_RGMII_1_800_NS 12
93#define YT8531_RC1R_RGMII_1_950_NS 13
94#define YT8531_RC1R_RGMII_2_100_NS 14
95#define YT8531_RC1R_RGMII_2_250_NS 15
96
97/* Phy gmii clock gating Register */
98#define YT8531_CLOCK_GATING_REG 0xC
99#define YT8531_CGR_RX_CLK_EN BIT(12)
100
101/* Specific Status Register */
102#define YTPHY_SPECIFIC_STATUS_REG 0x11
103#define YTPHY_DUPLEX_MASK BIT(13)
104#define YTPHY_DUPLEX_SHIFT 13
105#define YTPHY_SPEED_MODE_MASK GENMASK(15, 14)
106#define YTPHY_SPEED_MODE_SHIFT 14
107
108#define YT8531_EXTREG_SLEEP_CONTROL1_REG 0x27
109#define YT8531_ESC1R_SLEEP_SW BIT(15)
110#define YT8531_ESC1R_PLLON_SLP BIT(14)
111
112#define YT8531_RGMII_CONFIG1_REG 0xA003
113
114#define YT8531_CHIP_CONFIG_REG 0xA001
115#define YT8531_CCR_SW_RST BIT(15)
116/* 1b0 disable 1.9ns rxc clock delay *default*
117 * 1b1 enable 1.9ns rxc clock delay
118 */
119#define YT8531_CCR_RXC_DLY_EN BIT(8)
120#define YT8531_CCR_RXC_DLY_1_900_NS 1900
121
Lukasz Tekieli3282aee2024-01-28 20:22:47 +0100122#define YT8531_CCR_CFG_LDO_MASK GENMASK(5, 4)
123#define YT8531_CCR_CFG_LDO_3V3 0x0
124#define YT8531_CCR_CFG_LDO_1V8 0x2
125
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800126/* bits in struct ytphy_plat_priv->flag */
127#define TX_CLK_ADJ_ENABLED BIT(0)
128#define AUTO_SLEEP_DISABLED BIT(1)
129#define KEEP_PLL_ENABLED BIT(2)
130#define TX_CLK_10_INVERTED BIT(3)
131#define TX_CLK_100_INVERTED BIT(4)
132#define TX_CLK_1000_INVERTED BIT(5)
133
134struct ytphy_plat_priv {
135 u32 rx_delay_ps;
136 u32 tx_delay_ps;
137 u32 clk_out_frequency;
138 u32 flag;
139};
140
141/**
142 * struct ytphy_cfg_reg_map - map a config value to a register value
143 * @cfg: value in device configuration
144 * @reg: value in the register
145 */
146struct ytphy_cfg_reg_map {
147 u32 cfg;
148 u32 reg;
149};
150
151static const struct ytphy_cfg_reg_map ytphy_rgmii_delays[] = {
152 /* for tx delay / rx delay with YT8531_CCR_RXC_DLY_EN is not set. */
153 { 0, YT8531_RC1R_RGMII_0_000_NS },
154 { 150, YT8531_RC1R_RGMII_0_150_NS },
155 { 300, YT8531_RC1R_RGMII_0_300_NS },
156 { 450, YT8531_RC1R_RGMII_0_450_NS },
157 { 600, YT8531_RC1R_RGMII_0_600_NS },
158 { 750, YT8531_RC1R_RGMII_0_750_NS },
159 { 900, YT8531_RC1R_RGMII_0_900_NS },
160 { 1050, YT8531_RC1R_RGMII_1_050_NS },
161 { 1200, YT8531_RC1R_RGMII_1_200_NS },
162 { 1350, YT8531_RC1R_RGMII_1_350_NS },
163 { 1500, YT8531_RC1R_RGMII_1_500_NS },
164 { 1650, YT8531_RC1R_RGMII_1_650_NS },
165 { 1800, YT8531_RC1R_RGMII_1_800_NS },
166 { 1950, YT8531_RC1R_RGMII_1_950_NS }, /* default tx/rx delay */
167 { 2100, YT8531_RC1R_RGMII_2_100_NS },
168 { 2250, YT8531_RC1R_RGMII_2_250_NS },
169
170 /* only for rx delay with YT8531_CCR_RXC_DLY_EN is set. */
171 { 0 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_000_NS },
172 { 150 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_150_NS },
173 { 300 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_300_NS },
174 { 450 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_450_NS },
175 { 600 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_600_NS },
176 { 750 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_750_NS },
177 { 900 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_0_900_NS },
178 { 1050 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_050_NS },
179 { 1200 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_200_NS },
180 { 1350 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_350_NS },
181 { 1500 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_500_NS },
182 { 1650 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_650_NS },
183 { 1800 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_800_NS },
184 { 1950 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_1_950_NS },
185 { 2100 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_2_100_NS },
186 { 2250 + YT8531_CCR_RXC_DLY_1_900_NS, YT8531_RC1R_RGMII_2_250_NS }
187};
188
189static u32 ytphy_get_delay_reg_value(struct phy_device *phydev,
190 u32 val,
191 u16 *rxc_dly_en)
192{
193 int tb_size = ARRAY_SIZE(ytphy_rgmii_delays);
194 int tb_size_half = tb_size / 2;
195 int i;
196
197 /* when rxc_dly_en is NULL, it is get the delay for tx, only half of
198 * tb_size is valid.
199 */
200 if (!rxc_dly_en)
201 tb_size = tb_size_half;
202
203 for (i = 0; i < tb_size; i++) {
204 if (ytphy_rgmii_delays[i].cfg == val) {
205 if (rxc_dly_en && i < tb_size_half)
206 *rxc_dly_en = 0;
207 return ytphy_rgmii_delays[i].reg;
208 }
209 }
210
211 pr_warn("Unsupported value %d, using default (%u)\n",
212 val, YT8531_RC1R_RGMII_1_950_NS);
213
214 /* when rxc_dly_en is not NULL, it is get the delay for rx.
215 * The rx default in dts and ytphy_rgmii_clk_delay_config is 1950 ps,
216 * so YT8531_CCR_RXC_DLY_EN should not be set.
217 */
218 if (rxc_dly_en)
219 *rxc_dly_en = 0;
220
221 return YT8531_RC1R_RGMII_1_950_NS;
222}
223
224static int ytphy_modify_ext(struct phy_device *phydev, u16 regnum, u16 mask,
225 u16 set)
226{
227 int ret;
228
229 ret = phy_write(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_SELECT, regnum);
230 if (ret < 0)
231 return ret;
232
233 return phy_modify(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_DATA, mask, set);
234}
235
Lukasz Tekieli3282aee2024-01-28 20:22:47 +0100236static int ytphy_read_ext(struct phy_device *phydev, u16 regnum)
237{
238 int ret;
239
240 ret = phy_write(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_SELECT, regnum);
241 if (ret < 0)
242 return ret;
243
244 return phy_read(phydev, MDIO_DEVAD_NONE, YTPHY_PAGE_DATA);
245}
246
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800247static int ytphy_rgmii_clk_delay_config(struct phy_device *phydev)
248{
249 struct ytphy_plat_priv *priv = phydev->priv;
250 u16 rxc_dly_en = YT8531_CCR_RXC_DLY_EN;
251 u32 rx_reg, tx_reg;
252 u16 mask, val = 0;
253 int ret;
254
255 rx_reg = ytphy_get_delay_reg_value(phydev, priv->rx_delay_ps,
256 &rxc_dly_en);
257 tx_reg = ytphy_get_delay_reg_value(phydev, priv->tx_delay_ps,
258 NULL);
259
260 switch (phydev->interface) {
261 case PHY_INTERFACE_MODE_RGMII:
262 rxc_dly_en = 0;
263 break;
264 case PHY_INTERFACE_MODE_RGMII_RXID:
265 val |= FIELD_PREP(YT8531_RC1R_RX_DELAY_MASK, rx_reg);
266 break;
267 case PHY_INTERFACE_MODE_RGMII_TXID:
268 rxc_dly_en = 0;
269 val |= FIELD_PREP(YT8531_RC1R_GE_TX_DELAY_MASK, tx_reg);
270 break;
271 case PHY_INTERFACE_MODE_RGMII_ID:
272 val |= FIELD_PREP(YT8531_RC1R_RX_DELAY_MASK, rx_reg) |
273 FIELD_PREP(YT8531_RC1R_GE_TX_DELAY_MASK, tx_reg);
274 break;
275 default: /* do not support other modes */
276 return -EOPNOTSUPP;
277 }
278
279 ret = ytphy_modify_ext(phydev, YT8531_CHIP_CONFIG_REG,
280 YT8531_CCR_RXC_DLY_EN, rxc_dly_en);
281 if (ret < 0)
282 return ret;
283
284 /* Generally, it is not necessary to adjust YT8531_RC1R_FE_TX_DELAY */
285 mask = YT8531_RC1R_RX_DELAY_MASK | YT8531_RC1R_GE_TX_DELAY_MASK;
286 return ytphy_modify_ext(phydev, YT8531_RGMII_CONFIG1_REG, mask, val);
287}
288
289static int yt8531_parse_status(struct phy_device *phydev)
290{
291 int val;
292 int speed, speed_mode;
293
294 val = phy_read(phydev, MDIO_DEVAD_NONE, YTPHY_SPECIFIC_STATUS_REG);
295 if (val < 0)
296 return val;
297
298 speed_mode = (val & YTPHY_SPEED_MODE_MASK) >> YTPHY_SPEED_MODE_SHIFT;
299 switch (speed_mode) {
300 case 2:
301 speed = SPEED_1000;
302 break;
303 case 1:
304 speed = SPEED_100;
305 break;
306 default:
307 speed = SPEED_10;
308 break;
309 }
310
311 phydev->speed = speed;
312 phydev->duplex = (val & YTPHY_DUPLEX_MASK) >> YTPHY_DUPLEX_SHIFT;
313
314 return 0;
315}
316
317static int yt8531_startup(struct phy_device *phydev)
318{
319 struct ytphy_plat_priv *priv = phydev->priv;
320 u16 val = 0;
321 int ret;
322
323 ret = genphy_update_link(phydev);
324 if (ret)
325 return ret;
326
327 ret = yt8531_parse_status(phydev);
328 if (ret)
329 return ret;
330
331 if (phydev->speed < 0)
332 return -EINVAL;
333
334 if (!(priv->flag & TX_CLK_ADJ_ENABLED))
335 return 0;
336
337 switch (phydev->speed) {
338 case SPEED_1000:
339 if (priv->flag & TX_CLK_1000_INVERTED)
340 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
341 break;
342 case SPEED_100:
343 if (priv->flag & TX_CLK_100_INVERTED)
344 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
345 break;
346 case SPEED_10:
347 if (priv->flag & TX_CLK_10_INVERTED)
348 val = YT8531_RC1R_TX_CLK_SEL_INVERTED;
349 break;
350 default:
351 printf("UNKNOWN SPEED\n");
352 return -EINVAL;
353 }
354
355 ret = ytphy_modify_ext(phydev, YT8531_RGMII_CONFIG1_REG,
356 YT8531_RC1R_TX_CLK_SEL_INVERTED, val);
357 if (ret < 0)
358 pr_warn("Modify TX_CLK_SEL err:%d\n", ret);
359
360 return 0;
361}
362
363static void ytphy_dt_parse(struct phy_device *phydev)
364{
365 struct ytphy_plat_priv *priv = phydev->priv;
366
367 priv->clk_out_frequency = ofnode_read_u32_default(phydev->node,
368 "motorcomm,clk-out-frequency-hz",
369 YTPHY_DTS_OUTPUT_CLK_DIS);
370 priv->rx_delay_ps = ofnode_read_u32_default(phydev->node,
371 "rx-internal-delay-ps",
372 YT8531_RC1R_RGMII_1_950_NS);
373 priv->tx_delay_ps = ofnode_read_u32_default(phydev->node,
374 "tx-internal-delay-ps",
375 YT8531_RC1R_RGMII_1_950_NS);
376
377 if (ofnode_read_bool(phydev->node, "motorcomm,auto-sleep-disabled"))
378 priv->flag |= AUTO_SLEEP_DISABLED;
379
380 if (ofnode_read_bool(phydev->node, "motorcomm,keep-pll-enabled"))
381 priv->flag |= KEEP_PLL_ENABLED;
382
383 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-adj-enabled"))
384 priv->flag |= TX_CLK_ADJ_ENABLED;
385
386 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-10-inverted"))
387 priv->flag |= TX_CLK_10_INVERTED;
388
389 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-100-inverted"))
390 priv->flag |= TX_CLK_100_INVERTED;
391
392 if (ofnode_read_bool(phydev->node, "motorcomm,tx-clk-1000-inverted"))
393 priv->flag |= TX_CLK_1000_INVERTED;
394}
395
Nicolas Frattarolif08686a2023-08-05 12:35:01 +0200396static int yt8511_config(struct phy_device *phydev)
397{
398 u32 ge, fe;
399 int ret;
400
401 ret = genphy_config_aneg(phydev);
402 if (ret < 0)
403 return ret;
404
405 switch (phydev->interface) {
406 case PHY_INTERFACE_MODE_RGMII:
407 ge = YT8511_DELAY_GE_TX_DIS;
408 fe = YT8511_DELAY_FE_TX_DIS;
409 break;
410 case PHY_INTERFACE_MODE_RGMII_RXID:
411 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_DIS;
412 fe = YT8511_DELAY_FE_TX_DIS;
413 break;
414 case PHY_INTERFACE_MODE_RGMII_TXID:
415 ge = YT8511_DELAY_GE_TX_EN;
416 fe = YT8511_DELAY_FE_TX_EN;
417 break;
418 case PHY_INTERFACE_MODE_RGMII_ID:
419 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN;
420 fe = YT8511_DELAY_FE_TX_EN;
421 break;
422 default: /* do not support other modes */
423 return -EOPNOTSUPP;
424 }
425
426 ret = ytphy_modify_ext(phydev, YT8511_EXT_CLK_GATE,
427 (YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN), ge);
428 if (ret < 0)
429 return ret;
430 /* set clock mode to 125m */
431 ret = ytphy_modify_ext(phydev, YT8511_EXT_CLK_GATE,
432 YT8511_CLK_125M, YT8511_CLK_125M);
433 if (ret < 0)
434 return ret;
435 ret = ytphy_modify_ext(phydev, YT8511_EXT_DELAY_DRIVE,
436 YT8511_DELAY_FE_TX_EN, fe);
437 if (ret < 0)
438 return ret;
439 /* sleep control, disable PLL in sleep for now */
440 ret = ytphy_modify_ext(phydev, YT8511_EXT_SLEEP_CTRL, YT8511_PLLON_SLP,
441 0);
442 if (ret < 0)
443 return ret;
444
445 return 0;
446}
447
Lukasz Tekieli3282aee2024-01-28 20:22:47 +0100448/**
449 * struct ytphy_ldo_vol_map - map a current value to a register value
450 * @vol: ldo voltage
451 * @ds: value in the register
452 * @cur: value in device configuration
453 */
454struct ytphy_ldo_vol_map {
455 u32 vol;
456 u32 ds;
457 u32 cur;
458};
459
460static const struct ytphy_ldo_vol_map yt8531_ldo_vol[] = {
461 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 0, .cur = 1200},
462 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 1, .cur = 2100},
463 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 2, .cur = 2700},
464 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 3, .cur = 2910},
465 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 4, .cur = 3110},
466 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 5, .cur = 3600},
467 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 6, .cur = 3970},
468 {.vol = YT8531_CCR_CFG_LDO_1V8, .ds = 7, .cur = 4350},
469 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 0, .cur = 3070},
470 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 1, .cur = 4080},
471 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 2, .cur = 4370},
472 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 3, .cur = 4680},
473 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 4, .cur = 5020},
474 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 5, .cur = 5450},
475 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 6, .cur = 5740},
476 {.vol = YT8531_CCR_CFG_LDO_3V3, .ds = 7, .cur = 6140},
477};
478
479static u32 yt8531_get_ldo_vol(struct phy_device *phydev)
480{
481 u32 val;
482
483 val = ytphy_read_ext(phydev, YT8531_CHIP_CONFIG_REG);
484 val = FIELD_GET(YT8531_CCR_CFG_LDO_MASK, val);
485
486 return val <= YT8531_CCR_CFG_LDO_1V8 ? val : YT8531_CCR_CFG_LDO_1V8;
487}
488
489static int yt8531_get_ds_map(struct phy_device *phydev, u32 cur)
490{
491 u32 vol;
492 int i;
493
494 vol = yt8531_get_ldo_vol(phydev);
495 for (i = 0; i < ARRAY_SIZE(yt8531_ldo_vol); i++) {
496 if (yt8531_ldo_vol[i].vol == vol && yt8531_ldo_vol[i].cur == cur)
497 return yt8531_ldo_vol[i].ds;
498 }
499
500 return -EINVAL;
501}
502
503static int yt8531_set_ds(struct phy_device *phydev)
504{
505 u32 ds_field_low, ds_field_hi, val;
506 int ret, ds;
507
508 /* set rgmii rx clk driver strength */
509 if (!ofnode_read_u32(phydev->node, "motorcomm,rx-clk-drv-microamp", &val)) {
510 ds = yt8531_get_ds_map(phydev, val);
511 if (ds < 0) {
512 pr_warn("No matching current value was found.");
513 return -EINVAL;
514 }
515 } else {
516 ds = YT8531_RGMII_RX_DS_DEFAULT;
517 }
518
519 ret = ytphy_modify_ext(phydev,
520 YT8531_PAD_DRIVE_STRENGTH_CFG_REG,
521 YT8531_RGMII_RXC_DS_MASK,
522 FIELD_PREP(YT8531_RGMII_RXC_DS_MASK, ds));
523 if (ret < 0)
524 return ret;
525
526 /* set rgmii rx data driver strength */
527 if (!ofnode_read_u32(phydev->node, "motorcomm,rx-data-drv-microamp", &val)) {
528 ds = yt8531_get_ds_map(phydev, val);
529 if (ds < 0) {
530 pr_warn("No matching current value was found.");
531 return -EINVAL;
532 }
533 } else {
534 ds = YT8531_RGMII_RX_DS_DEFAULT;
535 }
536
537 ds_field_hi = FIELD_GET(BIT(2), ds);
538 ds_field_hi = FIELD_PREP(YT8531_RGMII_RXD_DS_HI_MASK, ds_field_hi);
539
540 ds_field_low = FIELD_GET(GENMASK(1, 0), ds);
541 ds_field_low = FIELD_PREP(YT8531_RGMII_RXD_DS_LOW_MASK, ds_field_low);
542
543 ret = ytphy_modify_ext(phydev,
544 YT8531_PAD_DRIVE_STRENGTH_CFG_REG,
545 YT8531_RGMII_RXD_DS_LOW_MASK | YT8531_RGMII_RXD_DS_HI_MASK,
546 ds_field_low | ds_field_hi);
547 if (ret < 0)
548 return ret;
549
550 return 0;
551}
552
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800553static int yt8531_config(struct phy_device *phydev)
554{
555 struct ytphy_plat_priv *priv = phydev->priv;
556 u16 mask, val;
557 int ret;
558
559 ret = genphy_config_aneg(phydev);
560 if (ret < 0)
561 return ret;
562
563 ytphy_dt_parse(phydev);
564 switch (priv->clk_out_frequency) {
565 case YTPHY_DTS_OUTPUT_CLK_DIS:
566 mask = YT8531_SCR_SYNCE_ENABLE;
567 val = 0;
568 break;
569 case YTPHY_DTS_OUTPUT_CLK_25M:
570 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
571 YT8531_SCR_CLK_FRE_SEL_125M;
572 val = YT8531_SCR_SYNCE_ENABLE |
573 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
574 YT8531_SCR_CLK_SRC_REF_25M);
575 break;
576 case YTPHY_DTS_OUTPUT_CLK_125M:
577 mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
578 YT8531_SCR_CLK_FRE_SEL_125M;
579 val = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_FRE_SEL_125M |
580 FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
581 YT8531_SCR_CLK_SRC_PLL_125M);
582 break;
583 default:
584 pr_warn("Freq err:%u\n", priv->clk_out_frequency);
585 return -EINVAL;
586 }
587
588 ret = ytphy_modify_ext(phydev, YTPHY_SYNCE_CFG_REG, mask,
589 val);
590 if (ret < 0)
591 return ret;
592
593 ret = ytphy_rgmii_clk_delay_config(phydev);
594 if (ret < 0)
595 return ret;
596
597 if (priv->flag & AUTO_SLEEP_DISABLED) {
598 /* disable auto sleep */
599 ret = ytphy_modify_ext(phydev,
600 YT8531_EXTREG_SLEEP_CONTROL1_REG,
601 YT8531_ESC1R_SLEEP_SW, 0);
602 if (ret < 0)
603 return ret;
604 }
605
606 if (priv->flag & KEEP_PLL_ENABLED) {
607 /* enable RXC clock when no wire plug */
608 ret = ytphy_modify_ext(phydev,
609 YT8531_CLOCK_GATING_REG,
610 YT8531_CGR_RX_CLK_EN, 0);
611 if (ret < 0)
612 return ret;
613 }
614
Lukasz Tekieli3282aee2024-01-28 20:22:47 +0100615 ret = yt8531_set_ds(phydev);
616 if (ret < 0)
617 return ret;
618
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800619 return 0;
620}
621
622static int yt8531_probe(struct phy_device *phydev)
623{
624 struct ytphy_plat_priv *priv;
625
626 priv = calloc(1, sizeof(struct ytphy_plat_priv));
627 if (!priv)
628 return -ENOMEM;
629
630 phydev->priv = priv;
631
632 return 0;
633}
634
Nicolas Frattarolif08686a2023-08-05 12:35:01 +0200635U_BOOT_PHY_DRIVER(motorcomm8511) = {
636 .name = "YT8511 Gigabit Ethernet",
637 .uid = PHY_ID_YT8511,
638 .mask = PHY_ID_MASK,
639 .features = PHY_GBIT_FEATURES,
640 .config = &yt8511_config,
641 .startup = &genphy_startup,
642 .shutdown = &genphy_shutdown,
643};
644
Yanhong Wang1d6c3412023-06-15 17:36:42 +0800645U_BOOT_PHY_DRIVER(motorcomm8531) = {
646 .name = "YT8531 Gigabit Ethernet",
647 .uid = PHY_ID_YT8531,
648 .mask = PHY_ID_MASK,
649 .features = PHY_GBIT_FEATURES,
650 .probe = &yt8531_probe,
651 .config = &yt8531_config,
652 .startup = &yt8531_startup,
653 .shutdown = &genphy_shutdown,
654};