blob: 0a49015eb89a89d70f746267ac4f4cab4f842fe2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming60ca78b2011-04-07 21:56:05 -05002/*
3 * Broadcom PHY drivers
4 *
Andy Fleming60ca78b2011-04-07 21:56:05 -05005 * Copyright 2010-2011 Freescale Semiconductor, Inc.
6 * author Andy Fleming
Andy Fleming60ca78b2011-04-07 21:56:05 -05007 */
Andy Fleming60ca78b2011-04-07 21:56:05 -05008#include <phy.h>
Simon Glassdbd79542020-05-10 11:40:11 -06009#include <linux/delay.h>
Andy Fleming60ca78b2011-04-07 21:56:05 -050010
11/* Broadcom BCM54xx -- taken from linux sungem_phy */
12#define MIIM_BCM54xx_AUXCNTL 0x18
13#define MIIM_BCM54xx_AUXCNTL_ENCODE(val) (((val & 0x7) << 12)|(val & 0x7))
14#define MIIM_BCM54xx_AUXSTATUS 0x19
15#define MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK 0x0700
16#define MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT 8
17
18#define MIIM_BCM54XX_SHD 0x1c
19#define MIIM_BCM54XX_SHD_WRITE 0x8000
20#define MIIM_BCM54XX_SHD_VAL(x) ((x & 0x1f) << 10)
21#define MIIM_BCM54XX_SHD_DATA(x) ((x & 0x3ff) << 0)
22#define MIIM_BCM54XX_SHD_WR_ENCODE(val, data) \
23 (MIIM_BCM54XX_SHD_WRITE | MIIM_BCM54XX_SHD_VAL(val) | \
24 MIIM_BCM54XX_SHD_DATA(data))
25
26#define MIIM_BCM54XX_EXP_DATA 0x15 /* Expansion register data */
27#define MIIM_BCM54XX_EXP_SEL 0x17 /* Expansion register select */
28#define MIIM_BCM54XX_EXP_SEL_SSD 0x0e00 /* Secondary SerDes select */
29#define MIIM_BCM54XX_EXP_SEL_ER 0x0f00 /* Expansion register select */
30
Arun Parameswaran84248b22017-07-19 15:34:35 -070031#define MIIM_BCM_AUXCNTL_SHDWSEL_MISC 0x0007
Marek Vasutc9cade72023-08-05 16:10:08 +020032#define MIIM_BCM_AUXCNTL_SHDWSEL_MISC_WIRESPEED_EN 0x0010
33#define MIIM_BCM_AUXCNTL_SHDWSEL_MISC_RGMII_EN 0x0080
34#define MIIM_BCM_AUXCNTL_SHDWSEL_MISC_RGMII_SKEW_EN 0x0100
35#define MIIM_BCM_AUXCNTL_MISC_FORCE_AMDIX 0x0200
36#define MIIM_BCM_AUXCNTL_ACTL_SMDSP_EN 0x0800
37#define MIIM_BCM_AUXCNTL_MISC_WREN 0x8000
Arun Parameswaran84248b22017-07-19 15:34:35 -070038
39#define MIIM_BCM_CHANNEL_WIDTH 0x2000
40
Marek Vasutc9cade72023-08-05 16:10:08 +020041#define BCM54810_SHD_CLK_CTL 0x3
42#define BCM54810_SHD_CLK_CTL_GTXCLK_EN BIT(9)
43
Marek Vasutae4bec72024-01-01 22:07:47 +010044#define BCM54XX_SHD_LEDS1 0x0d
45#define BCM_LED_SRC_LINKSPD2 0x1
46#define BCM_LED_SRC_ACTIVITYLED 0x3
47#define BCM54XX_SHD_LEDS1_LED3(src) (((src) & 0xf) << 4)
48#define BCM54XX_SHD_LEDS1_LED1(src) (((src) & 0xf) << 0)
49
Marek Vasutc9cade72023-08-05 16:10:08 +020050static int bcm54xx_auxctl_read(struct phy_device *phydev, u16 regnum)
51{
52 /* The register must be written to both the Shadow Register Select and
53 * the Shadow Read Register Selector
54 */
55 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL,
56 MIIM_BCM54xx_AUXCNTL_ENCODE(regnum));
57 return phy_read(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL);
58}
59
60static int bcm54xx_auxctl_write(struct phy_device *phydev, u16 regnum, u16 val)
61{
62 return phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL, regnum | val);
63}
64
65static int bcm_phy_read_shadow(struct phy_device *phydev, u16 shadow)
66{
67 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_SHD,
68 MIIM_BCM54XX_SHD_VAL(shadow));
69 return MIIM_BCM54XX_SHD_DATA(phy_read(phydev, MDIO_DEVAD_NONE,
70 MIIM_BCM54XX_SHD));
71}
72
73static int bcm_phy_write_shadow(struct phy_device *phydev, u16 shadow, u16 val)
74{
75 return phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_SHD,
76 MIIM_BCM54XX_SHD_WR_ENCODE(shadow, val));
77}
78
79static int bcm54xx_config_clock_delay(struct phy_device *phydev)
80{
81 int rc, val;
82
83 /* handling PHY's internal RX clock delay */
84 val = bcm54xx_auxctl_read(phydev, MIIM_BCM_AUXCNTL_SHDWSEL_MISC);
85 val |= MIIM_BCM_AUXCNTL_MISC_WREN;
86 if (phydev->interface == PHY_INTERFACE_MODE_RGMII ||
87 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
88 /* Disable RGMII RXC-RXD skew */
89 val &= ~MIIM_BCM_AUXCNTL_SHDWSEL_MISC_RGMII_SKEW_EN;
90 }
91 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
92 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
93 /* Enable RGMII RXC-RXD skew */
94 val |= MIIM_BCM_AUXCNTL_SHDWSEL_MISC_RGMII_SKEW_EN;
95 }
96 rc = bcm54xx_auxctl_write(phydev, MIIM_BCM_AUXCNTL_SHDWSEL_MISC, val);
97 if (rc < 0)
98 return rc;
99
100 /* handling PHY's internal TX clock delay */
101 val = bcm_phy_read_shadow(phydev, BCM54810_SHD_CLK_CTL);
102 if (phydev->interface == PHY_INTERFACE_MODE_RGMII ||
103 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
104 /* Disable internal TX clock delay */
105 val &= ~BCM54810_SHD_CLK_CTL_GTXCLK_EN;
106 }
107 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
108 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
109 /* Enable internal TX clock delay */
110 val |= BCM54810_SHD_CLK_CTL_GTXCLK_EN;
111 }
112 rc = bcm_phy_write_shadow(phydev, BCM54810_SHD_CLK_CTL, val);
113 if (rc < 0)
114 return rc;
115
116 return 0;
117}
118
Arun Parameswaran84248b22017-07-19 15:34:35 -0700119static void bcm_phy_write_misc(struct phy_device *phydev,
120 u16 reg, u16 chl, u16 value)
121{
122 int reg_val;
123
124 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL,
125 MIIM_BCM_AUXCNTL_SHDWSEL_MISC);
126
127 reg_val = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL);
128 reg_val |= MIIM_BCM_AUXCNTL_ACTL_SMDSP_EN;
129 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL, reg_val);
130
131 reg_val = (chl * MIIM_BCM_CHANNEL_WIDTH) | reg;
132 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_SEL, reg_val);
133
134 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_DATA, value);
135}
136
Andy Fleming60ca78b2011-04-07 21:56:05 -0500137/* Broadcom BCM5461S */
138static int bcm5461_config(struct phy_device *phydev)
139{
140 genphy_config_aneg(phydev);
141
142 phy_reset(phydev);
143
144 return 0;
145}
146
Marek Vasutc9cade72023-08-05 16:10:08 +0200147/* Broadcom BCM54210E */
148static int bcm54210e_config(struct phy_device *phydev)
149{
150 int ret;
151
152 ret = bcm54xx_config_clock_delay(phydev);
153 if (ret < 0)
154 return ret;
155
Marek Vasutae4bec72024-01-01 22:07:47 +0100156 ret = bcm5461_config(phydev);
157 if (ret < 0)
158 return ret;
159
160 /* Configure LEDs to blink. */
161 bcm_phy_write_shadow(phydev, BCM54XX_SHD_LEDS1,
162 BCM54XX_SHD_LEDS1_LED1(BCM_LED_SRC_ACTIVITYLED) |
163 BCM54XX_SHD_LEDS1_LED3(BCM_LED_SRC_LINKSPD2));
164
165 return 0;
Marek Vasutc9cade72023-08-05 16:10:08 +0200166}
167
Andy Fleming60ca78b2011-04-07 21:56:05 -0500168static int bcm54xx_parse_status(struct phy_device *phydev)
169{
170 unsigned int mii_reg;
171
172 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXSTATUS);
173
174 switch ((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >>
175 MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT) {
176 case 1:
177 phydev->duplex = DUPLEX_HALF;
178 phydev->speed = SPEED_10;
179 break;
180 case 2:
181 phydev->duplex = DUPLEX_FULL;
182 phydev->speed = SPEED_10;
183 break;
184 case 3:
185 phydev->duplex = DUPLEX_HALF;
186 phydev->speed = SPEED_100;
187 break;
188 case 5:
189 phydev->duplex = DUPLEX_FULL;
190 phydev->speed = SPEED_100;
191 break;
192 case 6:
193 phydev->duplex = DUPLEX_HALF;
194 phydev->speed = SPEED_1000;
195 break;
196 case 7:
197 phydev->duplex = DUPLEX_FULL;
198 phydev->speed = SPEED_1000;
199 break;
200 default:
201 printf("Auto-neg error, defaulting to 10BT/HD\n");
202 phydev->duplex = DUPLEX_HALF;
203 phydev->speed = SPEED_10;
204 break;
205 }
206
207 return 0;
208}
209
210static int bcm54xx_startup(struct phy_device *phydev)
211{
Michal Simek5ff89662016-05-18 12:46:12 +0200212 int ret;
213
Andy Fleming60ca78b2011-04-07 21:56:05 -0500214 /* Read the Status (2x to make sure link is right) */
Michal Simek5ff89662016-05-18 12:46:12 +0200215 ret = genphy_update_link(phydev);
216 if (ret)
217 return ret;
Andy Fleming60ca78b2011-04-07 21:56:05 -0500218
Michal Simek5ff89662016-05-18 12:46:12 +0200219 return bcm54xx_parse_status(phydev);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500220}
221
222/* Broadcom BCM5482S */
223/*
224 * "Ethernet@Wirespeed" needs to be enabled to achieve link in certain
225 * circumstances. eg a gigabit TSEC connected to a gigabit switch with
226 * a 4-wire ethernet cable. Both ends advertise gigabit, but can't
227 * link. "Ethernet@Wirespeed" reduces advertised speed until link
228 * can be achieved.
229 */
230static u32 bcm5482_read_wirespeed(struct phy_device *phydev, u32 reg)
231{
232 return (phy_read(phydev, MDIO_DEVAD_NONE, reg) & 0x8FFF) | 0x8010;
233}
234
235static int bcm5482_config(struct phy_device *phydev)
236{
237 unsigned int reg;
238
239 /* reset the PHY */
240 reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
241 reg |= BMCR_RESET;
242 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, reg);
243
244 /* Setup read from auxilary control shadow register 7 */
245 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL,
246 MIIM_BCM54xx_AUXCNTL_ENCODE(7));
247 /* Read Misc Control register and or in Ethernet@Wirespeed */
248 reg = bcm5482_read_wirespeed(phydev, MIIM_BCM54xx_AUXCNTL);
249 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL, reg);
250
251 /* Initial config/enable of secondary SerDes interface */
252 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_SHD,
253 MIIM_BCM54XX_SHD_WR_ENCODE(0x14, 0xf));
254 /* Write intial value to secondary SerDes Contol */
255 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_SEL,
256 MIIM_BCM54XX_EXP_SEL_SSD | 0);
257 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_DATA,
258 BMCR_ANRESTART);
259 /* Enable copper/fiber auto-detect */
260 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_SHD,
261 MIIM_BCM54XX_SHD_WR_ENCODE(0x1e, 0x201));
262
263 genphy_config_aneg(phydev);
264
265 return 0;
266}
267
Arun Parameswaran84248b22017-07-19 15:34:35 -0700268static void bcm_cygnus_afe(struct phy_device *phydev)
269{
270 /* ensures smdspclk is enabled */
271 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54xx_AUXCNTL, 0x0c30);
272
273 /* AFE_VDAC_ICTRL_0 bit 7:4 Iq=1100 for 1g 10bt, normal modes */
274 bcm_phy_write_misc(phydev, 0x39, 0x01, 0xA7C8);
275
276 /* AFE_HPF_TRIM_OTHERS bit11=1, short cascode for all modes*/
277 bcm_phy_write_misc(phydev, 0x3A, 0x00, 0x0803);
278
279 /* AFE_TX_CONFIG_1 bit 7:4 Iq=1100 for test modes */
280 bcm_phy_write_misc(phydev, 0x3A, 0x01, 0xA740);
281
282 /* AFE TEMPSEN_OTHERS rcal_HT, rcal_LT 10000 */
283 bcm_phy_write_misc(phydev, 0x3A, 0x03, 0x8400);
284
285 /* AFE_FUTURE_RSV bit 2:0 rccal <2:0>=100 */
286 bcm_phy_write_misc(phydev, 0x3B, 0x00, 0x0004);
287
288 /* Adjust bias current trim to overcome digital offSet */
289 phy_write(phydev, MDIO_DEVAD_NONE, 0x1E, 0x02);
290
291 /* make rcal=100, since rdb default is 000 */
292 phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0x00B1);
293 phy_write(phydev, MDIO_DEVAD_NONE, 0x15, 0x0010);
294
295 /* CORE_EXPB0, Reset R_CAL/RC_CAL Engine */
296 phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0x00B0);
297 phy_write(phydev, MDIO_DEVAD_NONE, 0x15, 0x0010);
298
299 /* CORE_EXPB0, Disable Reset R_CAL/RC_CAL Engine */
300 phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0x00B0);
301 phy_write(phydev, MDIO_DEVAD_NONE, 0x15, 0x0000);
302}
303
Jiandong Zheng68c2fd72015-07-15 16:28:13 -0700304static int bcm_cygnus_config(struct phy_device *phydev)
305{
306 genphy_config_aneg(phydev);
Jiandong Zheng68c2fd72015-07-15 16:28:13 -0700307 phy_reset(phydev);
Arun Parameswaran84248b22017-07-19 15:34:35 -0700308 /* AFE settings for PHY stability */
309 bcm_cygnus_afe(phydev);
310 /* Forcing aneg after applying the AFE settings */
311 genphy_restart_aneg(phydev);
Jiandong Zheng68c2fd72015-07-15 16:28:13 -0700312
313 return 0;
314}
315
Andy Fleming60ca78b2011-04-07 21:56:05 -0500316/*
317 * Find out if PHY is in copper or serdes mode by looking at Expansion Reg
318 * 0x42 - "Operating Mode Status Register"
319 */
320static int bcm5482_is_serdes(struct phy_device *phydev)
321{
322 u16 val;
323 int serdes = 0;
324
325 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_SEL,
326 MIIM_BCM54XX_EXP_SEL_ER | 0x42);
327 val = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_DATA);
328
329 switch (val & 0x1f) {
330 case 0x0d: /* RGMII-to-100Base-FX */
331 case 0x0e: /* RGMII-to-SGMII */
332 case 0x0f: /* RGMII-to-SerDes */
333 case 0x12: /* SGMII-to-SerDes */
334 case 0x13: /* SGMII-to-100Base-FX */
335 case 0x16: /* SerDes-to-Serdes */
336 serdes = 1;
337 break;
338 case 0x6: /* RGMII-to-Copper */
339 case 0x14: /* SGMII-to-Copper */
340 case 0x17: /* SerDes-to-Copper */
341 break;
342 default:
343 printf("ERROR, invalid PHY mode (0x%x\n)", val);
344 break;
345 }
346
347 return serdes;
348}
349
350/*
351 * Determine SerDes link speed and duplex from Expansion reg 0x42 "Operating
352 * Mode Status Register"
353 */
354static u32 bcm5482_parse_serdes_sr(struct phy_device *phydev)
355{
356 u16 val;
357 int i = 0;
358
359 /* Wait 1s for link - Clause 37 autonegotiation happens very fast */
360 while (1) {
361 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_SEL,
362 MIIM_BCM54XX_EXP_SEL_ER | 0x42);
363 val = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_BCM54XX_EXP_DATA);
364
365 if (val & 0x8000)
366 break;
367
368 if (i++ > 1000) {
369 phydev->link = 0;
370 return 1;
371 }
372
373 udelay(1000); /* 1 ms */
374 }
375
376 phydev->link = 1;
377 switch ((val >> 13) & 0x3) {
378 case (0x00):
379 phydev->speed = 10;
380 break;
381 case (0x01):
382 phydev->speed = 100;
383 break;
384 case (0x02):
385 phydev->speed = 1000;
386 break;
387 }
388
389 phydev->duplex = (val & 0x1000) == 0x1000;
390
391 return 0;
392}
393
394/*
395 * Figure out if BCM5482 is in serdes or copper mode and determine link
396 * configuration accordingly
397 */
398static int bcm5482_startup(struct phy_device *phydev)
399{
Michal Simek5ff89662016-05-18 12:46:12 +0200400 int ret;
401
Andy Fleming60ca78b2011-04-07 21:56:05 -0500402 if (bcm5482_is_serdes(phydev)) {
403 bcm5482_parse_serdes_sr(phydev);
404 phydev->port = PORT_FIBRE;
Michal Simek5ff89662016-05-18 12:46:12 +0200405 return 0;
Andy Fleming60ca78b2011-04-07 21:56:05 -0500406 }
407
Michal Simek5ff89662016-05-18 12:46:12 +0200408 /* Wait for auto-negotiation to complete or fail */
409 ret = genphy_update_link(phydev);
410 if (ret)
411 return ret;
412
413 /* Parse BCM54xx copper aux status register */
414 return bcm54xx_parse_status(phydev);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500415}
416
Marek Vasutc9cade72023-08-05 16:10:08 +0200417U_BOOT_PHY_DRIVER(bcm54210e) = {
418 .name = "Broadcom BCM54210E",
419 .uid = 0x600d84a0,
420 .mask = 0xfffffff0,
421 .features = PHY_GBIT_FEATURES,
422 .config = &bcm54210e_config,
423 .startup = &bcm54xx_startup,
424 .shutdown = &genphy_shutdown,
425};
426
Marek Vasute06c37b2023-03-19 18:02:47 +0100427U_BOOT_PHY_DRIVER(bcm5461s) = {
Andy Fleming60ca78b2011-04-07 21:56:05 -0500428 .name = "Broadcom BCM5461S",
429 .uid = 0x2060c0,
430 .mask = 0xfffff0,
431 .features = PHY_GBIT_FEATURES,
432 .config = &bcm5461_config,
433 .startup = &bcm54xx_startup,
434 .shutdown = &genphy_shutdown,
435};
436
Marek Vasute06c37b2023-03-19 18:02:47 +0100437U_BOOT_PHY_DRIVER(bcm5464s) = {
Andy Fleming60ca78b2011-04-07 21:56:05 -0500438 .name = "Broadcom BCM5464S",
439 .uid = 0x2060b0,
440 .mask = 0xfffff0,
441 .features = PHY_GBIT_FEATURES,
442 .config = &bcm5461_config,
443 .startup = &bcm54xx_startup,
444 .shutdown = &genphy_shutdown,
445};
446
Marek Vasute06c37b2023-03-19 18:02:47 +0100447U_BOOT_PHY_DRIVER(bcm5482s) = {
Andy Fleming60ca78b2011-04-07 21:56:05 -0500448 .name = "Broadcom BCM5482S",
449 .uid = 0x143bcb0,
450 .mask = 0xffffff0,
451 .features = PHY_GBIT_FEATURES,
452 .config = &bcm5482_config,
453 .startup = &bcm5482_startup,
454 .shutdown = &genphy_shutdown,
455};
456
Marek Vasute06c37b2023-03-19 18:02:47 +0100457U_BOOT_PHY_DRIVER(bcm_cygnus) = {
Jiandong Zheng68c2fd72015-07-15 16:28:13 -0700458 .name = "Broadcom CYGNUS GPHY",
459 .uid = 0xae025200,
460 .mask = 0xfffff0,
461 .features = PHY_GBIT_FEATURES,
462 .config = &bcm_cygnus_config,
Rasmus Villemoes9b8aa542023-03-28 23:12:47 +0200463 .startup = &genphy_startup,
Jiandong Zheng68c2fd72015-07-15 16:28:13 -0700464 .shutdown = &genphy_shutdown,
465};