blob: 67fa05d6752369aa98d15fe059046647bbdebaed [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +01002/*
3 * drivers/net/phy/broadcom.c
4 *
5 * Broadcom BCM5411, BCM5421 and BCM5461 Gigabit Ethernet
6 * transceivers.
7 *
8 * Copyright (c) 2006 Maciej W. Rozycki
9 *
10 * Inspired by code written by Amy Fong.
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +010011 */
12
Arun Parameswarana1cba562015-10-06 12:25:48 -070013#include "bcm-phy-lib.h"
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +010014#include <linux/module.h>
15#include <linux/phy.h>
Matt Carlson8649f132009-11-02 14:30:00 +000016#include <linux/brcmphy.h>
Jon Masonb14995a2016-11-04 01:10:58 -040017#include <linux/of.h>
Matt Carlsond9221e62009-08-25 10:11:26 +000018
19#define BRCM_PHY_MODEL(phydev) \
20 ((phydev)->drv->phy_id & (phydev)->drv->phy_id_mask)
21
Matt Carlson32e5a8d2009-11-02 14:31:39 +000022#define BRCM_PHY_REV(phydev) \
23 ((phydev)->drv->phy_id & ~((phydev)->drv->phy_id_mask))
24
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +010025MODULE_DESCRIPTION("Broadcom PHY driver");
26MODULE_AUTHOR("Maciej W. Rozycki");
27MODULE_LICENSE("GPL");
28
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +010029static int bcm54210e_config_init(struct phy_device *phydev)
30{
31 int val;
32
33 val = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
34 val &= ~MII_BCM54XX_AUXCTL_SHDWSEL_MISC_RGMII_SKEW_EN;
35 val |= MII_BCM54XX_AUXCTL_MISC_WREN;
36 bcm54xx_auxctl_write(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC, val);
37
38 val = bcm_phy_read_shadow(phydev, BCM54810_SHD_CLK_CTL);
39 val &= ~BCM54810_SHD_CLK_CTL_GTXCLK_EN;
40 bcm_phy_write_shadow(phydev, BCM54810_SHD_CLK_CTL, val);
41
Rafał Miłecki2355a652017-10-12 10:21:25 +020042 if (phydev->dev_flags & PHY_BRCM_EN_MASTER_MODE) {
43 val = phy_read(phydev, MII_CTRL1000);
44 val |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
45 phy_write(phydev, MII_CTRL1000, val);
46 }
47
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +010048 return 0;
49}
50
Rafał Miłecki62e13092017-01-31 22:54:54 +010051static int bcm54612e_config_init(struct phy_device *phydev)
52{
Kun Yi69e2ecc2018-06-04 13:17:04 -070053 int reg;
54
Rafał Miłecki62e13092017-01-31 22:54:54 +010055 /* Clear TX internal delay unless requested. */
56 if ((phydev->interface != PHY_INTERFACE_MODE_RGMII_ID) &&
57 (phydev->interface != PHY_INTERFACE_MODE_RGMII_TXID)) {
58 /* Disable TXD to GTXCLK clock delay (default set) */
59 /* Bit 9 is the only field in shadow register 00011 */
60 bcm_phy_write_shadow(phydev, 0x03, 0);
61 }
62
63 /* Clear RX internal delay unless requested. */
64 if ((phydev->interface != PHY_INTERFACE_MODE_RGMII_ID) &&
65 (phydev->interface != PHY_INTERFACE_MODE_RGMII_RXID)) {
Rafał Miłecki62e13092017-01-31 22:54:54 +010066 reg = bcm54xx_auxctl_read(phydev,
67 MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
68 /* Disable RXD to RXC delay (default set) */
69 reg &= ~MII_BCM54XX_AUXCTL_SHDWSEL_MISC_RGMII_SKEW_EN;
70 /* Clear shadow selector field */
71 reg &= ~MII_BCM54XX_AUXCTL_SHDWSEL_MASK;
72 bcm54xx_auxctl_write(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC,
73 MII_BCM54XX_AUXCTL_MISC_WREN | reg);
74 }
75
Kun Yi69e2ecc2018-06-04 13:17:04 -070076 /* Enable CLK125 MUX on LED4 if ref clock is enabled. */
77 if (!(phydev->dev_flags & PHY_BRCM_RX_REFCLK_UNUSED)) {
78 int err;
79
80 reg = bcm_phy_read_exp(phydev, BCM54612E_EXP_SPARE0);
81 err = bcm_phy_write_exp(phydev, BCM54612E_EXP_SPARE0,
82 BCM54612E_LED4_CLK125OUT_EN | reg);
83
84 if (err < 0)
85 return err;
86 }
87
Rafał Miłecki62e13092017-01-31 22:54:54 +010088 return 0;
89}
90
Tao Ren042cb562018-11-05 14:35:40 -080091static int bcm54xx_config_clock_delay(struct phy_device *phydev)
Jon Masonb14995a2016-11-04 01:10:58 -040092{
93 int rc, val;
94
Abhishek Shah73333622017-04-30 11:04:21 +053095 /* handling PHY's internal RX clock delay */
Jon Masonb14995a2016-11-04 01:10:58 -040096 val = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
Jon Masonb14995a2016-11-04 01:10:58 -040097 val |= MII_BCM54XX_AUXCTL_MISC_WREN;
Abhishek Shah73333622017-04-30 11:04:21 +053098 if (phydev->interface == PHY_INTERFACE_MODE_RGMII ||
99 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
100 /* Disable RGMII RXC-RXD skew */
101 val &= ~MII_BCM54XX_AUXCTL_SHDWSEL_MISC_RGMII_SKEW_EN;
102 }
103 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
104 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
105 /* Enable RGMII RXC-RXD skew */
106 val |= MII_BCM54XX_AUXCTL_SHDWSEL_MISC_RGMII_SKEW_EN;
107 }
Jon Masonb14995a2016-11-04 01:10:58 -0400108 rc = bcm54xx_auxctl_write(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC,
109 val);
110 if (rc < 0)
111 return rc;
112
Abhishek Shah73333622017-04-30 11:04:21 +0530113 /* handling PHY's internal TX clock delay */
Jon Masonb14995a2016-11-04 01:10:58 -0400114 val = bcm_phy_read_shadow(phydev, BCM54810_SHD_CLK_CTL);
Abhishek Shah73333622017-04-30 11:04:21 +0530115 if (phydev->interface == PHY_INTERFACE_MODE_RGMII ||
116 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
117 /* Disable internal TX clock delay */
118 val &= ~BCM54810_SHD_CLK_CTL_GTXCLK_EN;
119 }
120 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
121 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
122 /* Enable internal TX clock delay */
123 val |= BCM54810_SHD_CLK_CTL_GTXCLK_EN;
124 }
Jon Masonb14995a2016-11-04 01:10:58 -0400125 rc = bcm_phy_write_shadow(phydev, BCM54810_SHD_CLK_CTL, val);
126 if (rc < 0)
127 return rc;
128
129 return 0;
130}
131
Matt Carlson47b1b532009-11-02 14:28:04 +0000132/* Needs SMDSP clock enabled via bcm54xx_phydsp_config() */
Matt Carlson772638b2008-11-03 16:56:51 -0800133static int bcm50610_a0_workaround(struct phy_device *phydev)
134{
135 int err;
136
Arun Parameswarana1cba562015-10-06 12:25:48 -0700137 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_AADJ1CH0,
Matt Carlson47b1b532009-11-02 14:28:04 +0000138 MII_BCM54XX_EXP_AADJ1CH0_SWP_ABCD_OEN |
139 MII_BCM54XX_EXP_AADJ1CH0_SWSEL_THPF);
140 if (err < 0)
141 return err;
142
Arun Parameswarana1cba562015-10-06 12:25:48 -0700143 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_AADJ1CH3,
144 MII_BCM54XX_EXP_AADJ1CH3_ADCCKADJ);
Matt Carlson47b1b532009-11-02 14:28:04 +0000145 if (err < 0)
146 return err;
147
Arun Parameswarana1cba562015-10-06 12:25:48 -0700148 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_EXP75,
Matt Carlson47b1b532009-11-02 14:28:04 +0000149 MII_BCM54XX_EXP_EXP75_VDACCTRL);
150 if (err < 0)
151 return err;
152
Arun Parameswarana1cba562015-10-06 12:25:48 -0700153 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_EXP96,
Matt Carlson47b1b532009-11-02 14:28:04 +0000154 MII_BCM54XX_EXP_EXP96_MYST);
155 if (err < 0)
156 return err;
157
Arun Parameswarana1cba562015-10-06 12:25:48 -0700158 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_EXP97,
Matt Carlson47b1b532009-11-02 14:28:04 +0000159 MII_BCM54XX_EXP_EXP97_MYST);
160
161 return err;
162}
163
164static int bcm54xx_phydsp_config(struct phy_device *phydev)
165{
166 int err, err2;
167
168 /* Enable the SMDSP clock */
Matt Carlson772638b2008-11-03 16:56:51 -0800169 err = bcm54xx_auxctl_write(phydev,
170 MII_BCM54XX_AUXCTL_SHDWSEL_AUXCTL,
171 MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA |
172 MII_BCM54XX_AUXCTL_ACTL_TX_6DB);
173 if (err < 0)
174 return err;
175
Matt Carlson219c6ef2009-11-02 14:28:33 +0000176 if (BRCM_PHY_MODEL(phydev) == PHY_ID_BCM50610 ||
177 BRCM_PHY_MODEL(phydev) == PHY_ID_BCM50610M) {
178 /* Clear bit 9 to fix a phy interop issue. */
Arun Parameswarana1cba562015-10-06 12:25:48 -0700179 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_EXP08,
Matt Carlson219c6ef2009-11-02 14:28:33 +0000180 MII_BCM54XX_EXP_EXP08_RJCT_2MHZ);
181 if (err < 0)
182 goto error;
183
184 if (phydev->drv->phy_id == PHY_ID_BCM50610) {
185 err = bcm50610_a0_workaround(phydev);
186 if (err < 0)
187 goto error;
188 }
189 }
Matt Carlson772638b2008-11-03 16:56:51 -0800190
Matt Carlson47b1b532009-11-02 14:28:04 +0000191 if (BRCM_PHY_MODEL(phydev) == PHY_ID_BCM57780) {
192 int val;
Matt Carlson772638b2008-11-03 16:56:51 -0800193
Arun Parameswarana1cba562015-10-06 12:25:48 -0700194 val = bcm_phy_read_exp(phydev, MII_BCM54XX_EXP_EXP75);
Matt Carlson47b1b532009-11-02 14:28:04 +0000195 if (val < 0)
196 goto error;
Matt Carlson772638b2008-11-03 16:56:51 -0800197
Matt Carlson47b1b532009-11-02 14:28:04 +0000198 val |= MII_BCM54XX_EXP_EXP75_CM_OSC;
Arun Parameswarana1cba562015-10-06 12:25:48 -0700199 err = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_EXP75, val);
Matt Carlson47b1b532009-11-02 14:28:04 +0000200 }
Matt Carlson772638b2008-11-03 16:56:51 -0800201
202error:
Matt Carlson47b1b532009-11-02 14:28:04 +0000203 /* Disable the SMDSP clock */
204 err2 = bcm54xx_auxctl_write(phydev,
205 MII_BCM54XX_AUXCTL_SHDWSEL_AUXCTL,
206 MII_BCM54XX_AUXCTL_ACTL_TX_6DB);
Matt Carlson772638b2008-11-03 16:56:51 -0800207
Matt Carlson47b1b532009-11-02 14:28:04 +0000208 /* Return the first error reported. */
209 return err ? err : err2;
Matt Carlson772638b2008-11-03 16:56:51 -0800210}
211
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000212static void bcm54xx_adjust_rxrefclk(struct phy_device *phydev)
213{
Roel Kluin5ee6f6a2009-12-18 20:16:10 -0800214 u32 orig;
215 int val;
Matt Carlsonc704dc22009-11-02 14:32:12 +0000216 bool clk125en = true;
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000217
218 /* Abort if we are using an untested phy. */
roel kluin7ec4e7d2009-12-30 06:43:06 +0000219 if (BRCM_PHY_MODEL(phydev) != PHY_ID_BCM57780 &&
220 BRCM_PHY_MODEL(phydev) != PHY_ID_BCM50610 &&
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000221 BRCM_PHY_MODEL(phydev) != PHY_ID_BCM50610M)
222 return;
223
Arun Parameswarana1cba562015-10-06 12:25:48 -0700224 val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR3);
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000225 if (val < 0)
226 return;
227
228 orig = val;
229
Matt Carlsonc704dc22009-11-02 14:32:12 +0000230 if ((BRCM_PHY_MODEL(phydev) == PHY_ID_BCM50610 ||
231 BRCM_PHY_MODEL(phydev) == PHY_ID_BCM50610M) &&
232 BRCM_PHY_REV(phydev) >= 0x3) {
233 /*
234 * Here, bit 0 _disables_ CLK125 when set.
235 * This bit is set by default.
236 */
237 clk125en = false;
238 } else {
239 if (phydev->dev_flags & PHY_BRCM_RX_REFCLK_UNUSED) {
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000240 /* Here, bit 0 _enables_ CLK125 when set */
241 val &= ~BCM54XX_SHD_SCR3_DEF_CLK125;
Matt Carlsonc704dc22009-11-02 14:32:12 +0000242 clk125en = false;
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000243 }
244 }
245
Joe Perches23677ce2012-02-09 11:17:23 +0000246 if (!clk125en || (phydev->dev_flags & PHY_BRCM_AUTO_PWRDWN_ENABLE))
Matt Carlsonc704dc22009-11-02 14:32:12 +0000247 val &= ~BCM54XX_SHD_SCR3_DLLAPD_DIS;
248 else
249 val |= BCM54XX_SHD_SCR3_DLLAPD_DIS;
250
Matt Carlson52fae082009-11-02 14:32:38 +0000251 if (phydev->dev_flags & PHY_BRCM_DIS_TXCRXC_NOENRGY)
252 val |= BCM54XX_SHD_SCR3_TRDDAPD;
253
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000254 if (orig != val)
Arun Parameswarana1cba562015-10-06 12:25:48 -0700255 bcm_phy_write_shadow(phydev, BCM54XX_SHD_SCR3, val);
Matt Carlsonc704dc22009-11-02 14:32:12 +0000256
Arun Parameswarana1cba562015-10-06 12:25:48 -0700257 val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_APD);
Matt Carlsonc704dc22009-11-02 14:32:12 +0000258 if (val < 0)
259 return;
260
261 orig = val;
262
Joe Perches23677ce2012-02-09 11:17:23 +0000263 if (!clk125en || (phydev->dev_flags & PHY_BRCM_AUTO_PWRDWN_ENABLE))
Matt Carlsonc704dc22009-11-02 14:32:12 +0000264 val |= BCM54XX_SHD_APD_EN;
265 else
266 val &= ~BCM54XX_SHD_APD_EN;
267
268 if (orig != val)
Arun Parameswarana1cba562015-10-06 12:25:48 -0700269 bcm_phy_write_shadow(phydev, BCM54XX_SHD_APD, val);
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000270}
271
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100272static int bcm54xx_config_init(struct phy_device *phydev)
273{
Abhishek Shah73333622017-04-30 11:04:21 +0530274 int reg, err, val;
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100275
276 reg = phy_read(phydev, MII_BCM54XX_ECR);
277 if (reg < 0)
278 return reg;
279
280 /* Mask interrupts globally. */
281 reg |= MII_BCM54XX_ECR_IM;
282 err = phy_write(phydev, MII_BCM54XX_ECR, reg);
283 if (err < 0)
284 return err;
285
286 /* Unmask events we are interested in. */
287 reg = ~(MII_BCM54XX_INT_DUPLEX |
288 MII_BCM54XX_INT_SPEED |
289 MII_BCM54XX_INT_LINK);
290 err = phy_write(phydev, MII_BCM54XX_IMR, reg);
291 if (err < 0)
292 return err;
Matt Carlson772638b2008-11-03 16:56:51 -0800293
Matt Carlson63a14ce2009-11-02 14:30:40 +0000294 if ((BRCM_PHY_MODEL(phydev) == PHY_ID_BCM50610 ||
295 BRCM_PHY_MODEL(phydev) == PHY_ID_BCM50610M) &&
296 (phydev->dev_flags & PHY_BRCM_CLEAR_RGMII_MODE))
Arun Parameswarana1cba562015-10-06 12:25:48 -0700297 bcm_phy_write_shadow(phydev, BCM54XX_SHD_RGMII_MODE, 0);
Matt Carlson63a14ce2009-11-02 14:30:40 +0000298
Matt Carlsonc704dc22009-11-02 14:32:12 +0000299 if ((phydev->dev_flags & PHY_BRCM_RX_REFCLK_UNUSED) ||
Matt Carlson52fae082009-11-02 14:32:38 +0000300 (phydev->dev_flags & PHY_BRCM_DIS_TXCRXC_NOENRGY) ||
Matt Carlsonc704dc22009-11-02 14:32:12 +0000301 (phydev->dev_flags & PHY_BRCM_AUTO_PWRDWN_ENABLE))
Matt Carlson32e5a8d2009-11-02 14:31:39 +0000302 bcm54xx_adjust_rxrefclk(phydev);
303
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +0100304 if (BRCM_PHY_MODEL(phydev) == PHY_ID_BCM54210E) {
305 err = bcm54210e_config_init(phydev);
306 if (err)
307 return err;
Rafał Miłecki62e13092017-01-31 22:54:54 +0100308 } else if (BRCM_PHY_MODEL(phydev) == PHY_ID_BCM54612E) {
309 err = bcm54612e_config_init(phydev);
310 if (err)
311 return err;
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +0100312 } else if (BRCM_PHY_MODEL(phydev) == PHY_ID_BCM54810) {
Abhishek Shah73333622017-04-30 11:04:21 +0530313 /* For BCM54810, we need to disable BroadR-Reach function */
314 val = bcm_phy_read_exp(phydev,
315 BCM54810_EXP_BROADREACH_LRE_MISC_CTL);
316 val &= ~BCM54810_EXP_BROADREACH_LRE_MISC_CTL_EN;
317 err = bcm_phy_write_exp(phydev,
318 BCM54810_EXP_BROADREACH_LRE_MISC_CTL,
319 val);
320 if (err < 0)
Jon Masonb14995a2016-11-04 01:10:58 -0400321 return err;
322 }
323
Matt Carlson47b1b532009-11-02 14:28:04 +0000324 bcm54xx_phydsp_config(phydev);
Matt Carlsond9221e62009-08-25 10:11:26 +0000325
Vladimir Oltean450895d2019-03-24 00:18:46 +0200326 /* Encode link speed into LED1 and LED3 pair (green/amber).
327 * Also flash these two LEDs on activity. This means configuring
328 * them for MULTICOLOR and encoding link/activity into them.
329 */
330 val = BCM5482_SHD_LEDS1_LED1(BCM_LED_SRC_MULTICOLOR1) |
331 BCM5482_SHD_LEDS1_LED3(BCM_LED_SRC_MULTICOLOR1);
332 bcm_phy_write_shadow(phydev, BCM5482_SHD_LEDS1, val);
333
334 val = BCM_LED_MULTICOLOR_IN_PHASE |
335 BCM5482_SHD_LEDS1_LED1(BCM_LED_MULTICOLOR_LINK_ACT) |
336 BCM5482_SHD_LEDS1_LED3(BCM_LED_MULTICOLOR_LINK_ACT);
337 bcm_phy_write_exp(phydev, BCM_EXP_MULTICOLOR, val);
338
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100339 return 0;
340}
341
Nate Casecd9af3d2008-05-17 06:40:39 +0100342static int bcm5482_config_init(struct phy_device *phydev)
343{
344 int err, reg;
345
346 err = bcm54xx_config_init(phydev);
347
348 if (phydev->dev_flags & PHY_BCM_FLAGS_MODE_1000BX) {
349 /*
350 * Enable secondary SerDes and its use as an LED source
351 */
Arun Parameswarana1cba562015-10-06 12:25:48 -0700352 reg = bcm_phy_read_shadow(phydev, BCM5482_SHD_SSD);
353 bcm_phy_write_shadow(phydev, BCM5482_SHD_SSD,
Nate Casecd9af3d2008-05-17 06:40:39 +0100354 reg |
355 BCM5482_SHD_SSD_LEDM |
356 BCM5482_SHD_SSD_EN);
357
358 /*
359 * Enable SGMII slave mode and auto-detection
360 */
Matt Carlson042a75b2008-11-03 16:56:29 -0800361 reg = BCM5482_SSD_SGMII_SLAVE | MII_BCM54XX_EXP_SEL_SSD;
Arun Parameswarana1cba562015-10-06 12:25:48 -0700362 err = bcm_phy_read_exp(phydev, reg);
Matt Carlson042a75b2008-11-03 16:56:29 -0800363 if (err < 0)
364 return err;
Arun Parameswarana1cba562015-10-06 12:25:48 -0700365 err = bcm_phy_write_exp(phydev, reg, err |
Matt Carlson042a75b2008-11-03 16:56:29 -0800366 BCM5482_SSD_SGMII_SLAVE_EN |
367 BCM5482_SSD_SGMII_SLAVE_AD);
368 if (err < 0)
369 return err;
Nate Casecd9af3d2008-05-17 06:40:39 +0100370
371 /*
372 * Disable secondary SerDes powerdown
373 */
Matt Carlson042a75b2008-11-03 16:56:29 -0800374 reg = BCM5482_SSD_1000BX_CTL | MII_BCM54XX_EXP_SEL_SSD;
Arun Parameswarana1cba562015-10-06 12:25:48 -0700375 err = bcm_phy_read_exp(phydev, reg);
Matt Carlson042a75b2008-11-03 16:56:29 -0800376 if (err < 0)
377 return err;
Arun Parameswarana1cba562015-10-06 12:25:48 -0700378 err = bcm_phy_write_exp(phydev, reg,
Matt Carlson042a75b2008-11-03 16:56:29 -0800379 err & ~BCM5482_SSD_1000BX_CTL_PWRDOWN);
380 if (err < 0)
381 return err;
Nate Casecd9af3d2008-05-17 06:40:39 +0100382
383 /*
384 * Select 1000BASE-X register set (primary SerDes)
385 */
Arun Parameswarana1cba562015-10-06 12:25:48 -0700386 reg = bcm_phy_read_shadow(phydev, BCM5482_SHD_MODE);
387 bcm_phy_write_shadow(phydev, BCM5482_SHD_MODE,
Nate Casecd9af3d2008-05-17 06:40:39 +0100388 reg | BCM5482_SHD_MODE_1000BX);
389
390 /*
391 * LED1=ACTIVITYLED, LED3=LINKSPD[2]
392 * (Use LED1 as secondary SerDes ACTIVITY LED)
393 */
Arun Parameswarana1cba562015-10-06 12:25:48 -0700394 bcm_phy_write_shadow(phydev, BCM5482_SHD_LEDS1,
Nate Casecd9af3d2008-05-17 06:40:39 +0100395 BCM5482_SHD_LEDS1_LED1(BCM_LED_SRC_ACTIVITYLED) |
396 BCM5482_SHD_LEDS1_LED3(BCM_LED_SRC_LINKSPD2));
397
398 /*
399 * Auto-negotiation doesn't seem to work quite right
400 * in this mode, so we disable it and force it to the
401 * right speed/duplex setting. Only 'link status'
402 * is important.
403 */
404 phydev->autoneg = AUTONEG_DISABLE;
405 phydev->speed = SPEED_1000;
406 phydev->duplex = DUPLEX_FULL;
407 }
408
409 return err;
410}
411
412static int bcm5482_read_status(struct phy_device *phydev)
413{
414 int err;
415
416 err = genphy_read_status(phydev);
417
418 if (phydev->dev_flags & PHY_BCM_FLAGS_MODE_1000BX) {
419 /*
420 * Only link status matters for 1000Base-X mode, so force
421 * 1000 Mbit/s full-duplex status
422 */
423 if (phydev->link) {
424 phydev->speed = SPEED_1000;
425 phydev->duplex = DUPLEX_FULL;
426 }
427 }
428
429 return err;
430}
431
Anton Vorontsov57bb7e22008-03-04 19:41:32 +0300432static int bcm5481_config_aneg(struct phy_device *phydev)
433{
Jon Masonb14995a2016-11-04 01:10:58 -0400434 struct device_node *np = phydev->mdio.dev.of_node;
Anton Vorontsov57bb7e22008-03-04 19:41:32 +0300435 int ret;
436
437 /* Aneg firsly. */
438 ret = genphy_config_aneg(phydev);
439
440 /* Then we can set up the delay. */
Tao Ren042cb562018-11-05 14:35:40 -0800441 bcm54xx_config_clock_delay(phydev);
Anton Vorontsov57bb7e22008-03-04 19:41:32 +0300442
Jon Masonb14995a2016-11-04 01:10:58 -0400443 if (of_property_read_bool(np, "enet-phy-lane-swap")) {
444 /* Lane Swap - Undocumented register...magic! */
445 ret = bcm_phy_write_exp(phydev, MII_BCM54XX_EXP_SEL_ER + 0x9,
446 0x11B);
447 if (ret < 0)
448 return ret;
449 }
450
Anton Vorontsov57bb7e22008-03-04 19:41:32 +0300451 return ret;
452}
453
Tao Ren042cb562018-11-05 14:35:40 -0800454static int bcm54616s_config_aneg(struct phy_device *phydev)
455{
456 int ret;
457
458 /* Aneg firsly. */
459 ret = genphy_config_aneg(phydev);
460
461 /* Then we can set up the delay. */
462 bcm54xx_config_clock_delay(phydev);
463
464 return ret;
465}
466
Matt Carlsond7a2ed92009-08-25 10:10:58 +0000467static int brcm_phy_setbits(struct phy_device *phydev, int reg, int set)
468{
469 int val;
470
471 val = phy_read(phydev, reg);
472 if (val < 0)
473 return val;
474
475 return phy_write(phydev, reg, val | set);
476}
477
478static int brcm_fet_config_init(struct phy_device *phydev)
479{
480 int reg, err, err2, brcmtest;
481
482 /* Reset the PHY to bring it to a known state. */
483 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
484 if (err < 0)
485 return err;
486
487 reg = phy_read(phydev, MII_BRCM_FET_INTREG);
488 if (reg < 0)
489 return reg;
490
491 /* Unmask events we are interested in and mask interrupts globally. */
492 reg = MII_BRCM_FET_IR_DUPLEX_EN |
493 MII_BRCM_FET_IR_SPEED_EN |
494 MII_BRCM_FET_IR_LINK_EN |
495 MII_BRCM_FET_IR_ENABLE |
496 MII_BRCM_FET_IR_MASK;
497
498 err = phy_write(phydev, MII_BRCM_FET_INTREG, reg);
499 if (err < 0)
500 return err;
501
502 /* Enable shadow register access */
503 brcmtest = phy_read(phydev, MII_BRCM_FET_BRCMTEST);
504 if (brcmtest < 0)
505 return brcmtest;
506
507 reg = brcmtest | MII_BRCM_FET_BT_SRE;
508
509 err = phy_write(phydev, MII_BRCM_FET_BRCMTEST, reg);
510 if (err < 0)
511 return err;
512
513 /* Set the LED mode */
514 reg = phy_read(phydev, MII_BRCM_FET_SHDW_AUXMODE4);
515 if (reg < 0) {
516 err = reg;
517 goto done;
518 }
519
520 reg &= ~MII_BRCM_FET_SHDW_AM4_LED_MASK;
521 reg |= MII_BRCM_FET_SHDW_AM4_LED_MODE1;
522
523 err = phy_write(phydev, MII_BRCM_FET_SHDW_AUXMODE4, reg);
524 if (err < 0)
525 goto done;
526
527 /* Enable auto MDIX */
528 err = brcm_phy_setbits(phydev, MII_BRCM_FET_SHDW_MISCCTRL,
529 MII_BRCM_FET_SHDW_MC_FAME);
530 if (err < 0)
531 goto done;
532
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +0000533 if (phydev->dev_flags & PHY_BRCM_AUTO_PWRDWN_ENABLE) {
534 /* Enable auto power down */
535 err = brcm_phy_setbits(phydev, MII_BRCM_FET_SHDW_AUXSTAT2,
536 MII_BRCM_FET_SHDW_AS2_APDE);
537 }
Matt Carlsond7a2ed92009-08-25 10:10:58 +0000538
539done:
540 /* Disable shadow register access */
541 err2 = phy_write(phydev, MII_BRCM_FET_BRCMTEST, brcmtest);
542 if (!err)
543 err = err2;
544
545 return err;
546}
547
548static int brcm_fet_ack_interrupt(struct phy_device *phydev)
549{
550 int reg;
551
552 /* Clear pending interrupts. */
553 reg = phy_read(phydev, MII_BRCM_FET_INTREG);
554 if (reg < 0)
555 return reg;
556
557 return 0;
558}
559
560static int brcm_fet_config_intr(struct phy_device *phydev)
561{
562 int reg, err;
563
564 reg = phy_read(phydev, MII_BRCM_FET_INTREG);
565 if (reg < 0)
566 return reg;
567
568 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
569 reg &= ~MII_BRCM_FET_IR_MASK;
570 else
571 reg |= MII_BRCM_FET_IR_MASK;
572
573 err = phy_write(phydev, MII_BRCM_FET_INTREG, reg);
574 return err;
575}
576
Florian Fainelli28dc4c82017-12-14 17:48:16 -0800577struct bcm53xx_phy_priv {
578 u64 *stats;
579};
580
581static int bcm53xx_phy_probe(struct phy_device *phydev)
582{
583 struct bcm53xx_phy_priv *priv;
584
585 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
586 if (!priv)
587 return -ENOMEM;
588
589 phydev->priv = priv;
590
591 priv->stats = devm_kcalloc(&phydev->mdio.dev,
592 bcm_phy_get_sset_count(phydev), sizeof(u64),
593 GFP_KERNEL);
594 if (!priv->stats)
595 return -ENOMEM;
596
597 return 0;
598}
599
600static void bcm53xx_phy_get_stats(struct phy_device *phydev,
601 struct ethtool_stats *stats, u64 *data)
602{
603 struct bcm53xx_phy_priv *priv = phydev->priv;
604
605 bcm_phy_get_stats(phydev, priv->stats, stats, data);
606}
607
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000608static struct phy_driver broadcom_drivers[] = {
609{
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000610 .phy_id = PHY_ID_BCM5411,
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100611 .phy_id_mask = 0xfffffff0,
612 .name = "Broadcom BCM5411",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200613 /* PHY_GBIT_FEATURES */
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100614 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700615 .ack_interrupt = bcm_phy_ack_intr,
616 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000617}, {
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000618 .phy_id = PHY_ID_BCM5421,
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100619 .phy_id_mask = 0xfffffff0,
620 .name = "Broadcom BCM5421",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200621 /* PHY_GBIT_FEATURES */
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100622 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700623 .ack_interrupt = bcm_phy_ack_intr,
624 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000625}, {
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +0100626 .phy_id = PHY_ID_BCM54210E,
627 .phy_id_mask = 0xfffffff0,
628 .name = "Broadcom BCM54210E",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200629 /* PHY_GBIT_FEATURES */
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +0100630 .config_init = bcm54xx_config_init,
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +0100631 .ack_interrupt = bcm_phy_ack_intr,
632 .config_intr = bcm_phy_config_intr,
633}, {
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000634 .phy_id = PHY_ID_BCM5461,
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100635 .phy_id_mask = 0xfffffff0,
636 .name = "Broadcom BCM5461",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200637 /* PHY_GBIT_FEATURES */
Maciej W. Rozyckic4b41c92006-10-03 16:18:13 +0100638 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700639 .ack_interrupt = bcm_phy_ack_intr,
640 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000641}, {
Xo Wangd92ead12016-10-21 10:20:13 -0700642 .phy_id = PHY_ID_BCM54612E,
643 .phy_id_mask = 0xfffffff0,
644 .name = "Broadcom BCM54612E",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200645 /* PHY_GBIT_FEATURES */
Xo Wangd92ead12016-10-21 10:20:13 -0700646 .config_init = bcm54xx_config_init,
Xo Wangd92ead12016-10-21 10:20:13 -0700647 .ack_interrupt = bcm_phy_ack_intr,
648 .config_intr = bcm_phy_config_intr,
649}, {
Alessio Igor Bogani3bca4cf62015-04-08 12:15:18 +0200650 .phy_id = PHY_ID_BCM54616S,
651 .phy_id_mask = 0xfffffff0,
652 .name = "Broadcom BCM54616S",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200653 /* PHY_GBIT_FEATURES */
Alessio Igor Bogani3bca4cf62015-04-08 12:15:18 +0200654 .config_init = bcm54xx_config_init,
Tao Ren042cb562018-11-05 14:35:40 -0800655 .config_aneg = bcm54616s_config_aneg,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700656 .ack_interrupt = bcm_phy_ack_intr,
657 .config_intr = bcm_phy_config_intr,
Alessio Igor Bogani3bca4cf62015-04-08 12:15:18 +0200658}, {
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000659 .phy_id = PHY_ID_BCM5464,
Paul Gortmakerb1394f92008-04-14 23:35:41 -0400660 .phy_id_mask = 0xfffffff0,
661 .name = "Broadcom BCM5464",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200662 /* PHY_GBIT_FEATURES */
Paul Gortmakerb1394f92008-04-14 23:35:41 -0400663 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700664 .ack_interrupt = bcm_phy_ack_intr,
665 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000666}, {
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000667 .phy_id = PHY_ID_BCM5481,
Anton Vorontsov57bb7e22008-03-04 19:41:32 +0300668 .phy_id_mask = 0xfffffff0,
669 .name = "Broadcom BCM5481",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200670 /* PHY_GBIT_FEATURES */
Anton Vorontsov57bb7e22008-03-04 19:41:32 +0300671 .config_init = bcm54xx_config_init,
Heiner Kallweit9753c212017-12-02 10:50:37 +0100672 .config_aneg = bcm5481_config_aneg,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700673 .ack_interrupt = bcm_phy_ack_intr,
674 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000675}, {
Jon Masonb14995a2016-11-04 01:10:58 -0400676 .phy_id = PHY_ID_BCM54810,
677 .phy_id_mask = 0xfffffff0,
678 .name = "Broadcom BCM54810",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200679 /* PHY_GBIT_FEATURES */
Jon Masonb14995a2016-11-04 01:10:58 -0400680 .config_init = bcm54xx_config_init,
Heiner Kallweit9753c212017-12-02 10:50:37 +0100681 .config_aneg = bcm5481_config_aneg,
Jon Masonb14995a2016-11-04 01:10:58 -0400682 .ack_interrupt = bcm_phy_ack_intr,
683 .config_intr = bcm_phy_config_intr,
684}, {
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000685 .phy_id = PHY_ID_BCM5482,
Nate Case03157ac2008-01-29 10:19:00 -0600686 .phy_id_mask = 0xfffffff0,
687 .name = "Broadcom BCM5482",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200688 /* PHY_GBIT_FEATURES */
Nate Casecd9af3d2008-05-17 06:40:39 +0100689 .config_init = bcm5482_config_init,
Heiner Kallweit9753c212017-12-02 10:50:37 +0100690 .read_status = bcm5482_read_status,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700691 .ack_interrupt = bcm_phy_ack_intr,
692 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000693}, {
Matt Carlson772638b2008-11-03 16:56:51 -0800694 .phy_id = PHY_ID_BCM50610,
695 .phy_id_mask = 0xfffffff0,
696 .name = "Broadcom BCM50610",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200697 /* PHY_GBIT_FEATURES */
Matt Carlson772638b2008-11-03 16:56:51 -0800698 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700699 .ack_interrupt = bcm_phy_ack_intr,
700 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000701}, {
Matt Carlson4f4598f2009-08-25 10:10:30 +0000702 .phy_id = PHY_ID_BCM50610M,
703 .phy_id_mask = 0xfffffff0,
704 .name = "Broadcom BCM50610M",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200705 /* PHY_GBIT_FEATURES */
Matt Carlson4f4598f2009-08-25 10:10:30 +0000706 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700707 .ack_interrupt = bcm_phy_ack_intr,
708 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000709}, {
Matt Carlsond9221e62009-08-25 10:11:26 +0000710 .phy_id = PHY_ID_BCM57780,
Matt Carlson2fbb69a2008-11-21 17:22:53 -0800711 .phy_id_mask = 0xfffffff0,
712 .name = "Broadcom BCM57780",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200713 /* PHY_GBIT_FEATURES */
Matt Carlson2fbb69a2008-11-21 17:22:53 -0800714 .config_init = bcm54xx_config_init,
Arun Parameswarana1cba562015-10-06 12:25:48 -0700715 .ack_interrupt = bcm_phy_ack_intr,
716 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000717}, {
Matt Carlson6a443a02010-02-17 15:17:04 +0000718 .phy_id = PHY_ID_BCMAC131,
Matt Carlsond7a2ed92009-08-25 10:10:58 +0000719 .phy_id_mask = 0xfffffff0,
720 .name = "Broadcom BCMAC131",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200721 /* PHY_BASIC_FEATURES */
Matt Carlsond7a2ed92009-08-25 10:10:58 +0000722 .config_init = brcm_fet_config_init,
Matt Carlsond7a2ed92009-08-25 10:10:58 +0000723 .ack_interrupt = brcm_fet_ack_interrupt,
724 .config_intr = brcm_fet_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000725}, {
Dmitry Baryshkov7a938f82010-06-16 23:02:24 +0000726 .phy_id = PHY_ID_BCM5241,
727 .phy_id_mask = 0xfffffff0,
728 .name = "Broadcom BCM5241",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200729 /* PHY_BASIC_FEATURES */
Dmitry Baryshkov7a938f82010-06-16 23:02:24 +0000730 .config_init = brcm_fet_config_init,
Dmitry Baryshkov7a938f82010-06-16 23:02:24 +0000731 .ack_interrupt = brcm_fet_ack_interrupt,
732 .config_intr = brcm_fet_config_intr,
Florian Fainelli28dc4c82017-12-14 17:48:16 -0800733}, {
734 .phy_id = PHY_ID_BCM5395,
735 .phy_id_mask = 0xfffffff0,
736 .name = "Broadcom BCM5395",
737 .flags = PHY_IS_INTERNAL,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200738 /* PHY_GBIT_FEATURES */
Florian Fainelli28dc4c82017-12-14 17:48:16 -0800739 .get_sset_count = bcm_phy_get_sset_count,
740 .get_strings = bcm_phy_get_strings,
741 .get_stats = bcm53xx_phy_get_stats,
742 .probe = bcm53xx_phy_probe,
Bhadram Varka23b83922018-05-02 20:43:58 +0530743}, {
744 .phy_id = PHY_ID_BCM89610,
745 .phy_id_mask = 0xfffffff0,
746 .name = "Broadcom BCM89610",
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200747 /* PHY_GBIT_FEATURES */
Bhadram Varka23b83922018-05-02 20:43:58 +0530748 .config_init = bcm54xx_config_init,
749 .ack_interrupt = bcm_phy_ack_intr,
750 .config_intr = bcm_phy_config_intr,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000751} };
Dmitry Baryshkov7a938f82010-06-16 23:02:24 +0000752
Johan Hovold50fd7152014-11-11 19:45:59 +0100753module_phy_driver(broadcom_drivers);
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000754
Uwe Kleine-Königcf93c942010-10-03 23:43:32 +0000755static struct mdio_device_id __maybe_unused broadcom_tbl[] = {
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000756 { PHY_ID_BCM5411, 0xfffffff0 },
757 { PHY_ID_BCM5421, 0xfffffff0 },
Rafał Miłecki0fc9ae12017-01-27 14:07:01 +0100758 { PHY_ID_BCM54210E, 0xfffffff0 },
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000759 { PHY_ID_BCM5461, 0xfffffff0 },
Xo Wangd92ead12016-10-21 10:20:13 -0700760 { PHY_ID_BCM54612E, 0xfffffff0 },
Alessio Igor Bogani3bca4cf62015-04-08 12:15:18 +0200761 { PHY_ID_BCM54616S, 0xfffffff0 },
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000762 { PHY_ID_BCM5464, 0xfffffff0 },
Aaro Koskinen3c25a862015-11-22 01:08:54 +0200763 { PHY_ID_BCM5481, 0xfffffff0 },
Jon Masonb14995a2016-11-04 01:10:58 -0400764 { PHY_ID_BCM54810, 0xfffffff0 },
Dmitry Baryshkovfcb26ec2010-06-16 23:02:23 +0000765 { PHY_ID_BCM5482, 0xfffffff0 },
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000766 { PHY_ID_BCM50610, 0xfffffff0 },
767 { PHY_ID_BCM50610M, 0xfffffff0 },
768 { PHY_ID_BCM57780, 0xfffffff0 },
769 { PHY_ID_BCMAC131, 0xfffffff0 },
Dmitry Baryshkov7a938f82010-06-16 23:02:24 +0000770 { PHY_ID_BCM5241, 0xfffffff0 },
Florian Fainelli28dc4c82017-12-14 17:48:16 -0800771 { PHY_ID_BCM5395, 0xfffffff0 },
Bhadram Varka23b83922018-05-02 20:43:58 +0530772 { PHY_ID_BCM89610, 0xfffffff0 },
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000773 { }
774};
775
776MODULE_DEVICE_TABLE(mdio, broadcom_tbl);