blob: 2f5721430e053b1e2758cb6247102fee77c59d48 [file] [log] [blame]
Russell King5acde342017-06-05 12:22:50 +01001/*
2 * Clause 45 PHY support
3 */
4#include <linux/ethtool.h>
5#include <linux/export.h>
6#include <linux/mdio.h>
7#include <linux/mii.h>
8#include <linux/phy.h>
9
10/**
11 * genphy_c45_setup_forced - configures a forced speed
12 * @phydev: target phy_device struct
13 */
14int genphy_c45_pma_setup_forced(struct phy_device *phydev)
15{
16 int ctrl1, ctrl2, ret;
17
18 /* Half duplex is not supported */
19 if (phydev->duplex != DUPLEX_FULL)
20 return -EINVAL;
21
22 ctrl1 = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1);
23 if (ctrl1 < 0)
24 return ctrl1;
25
26 ctrl2 = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL2);
27 if (ctrl2 < 0)
28 return ctrl2;
29
30 ctrl1 &= ~MDIO_CTRL1_SPEEDSEL;
31 /*
32 * PMA/PMD type selection is 1.7.5:0 not 1.7.3:0. See 45.2.1.6.1
33 * in 802.3-2012 and 802.3-2015.
34 */
35 ctrl2 &= ~(MDIO_PMA_CTRL2_TYPE | 0x30);
36
37 switch (phydev->speed) {
38 case SPEED_10:
39 ctrl2 |= MDIO_PMA_CTRL2_10BT;
40 break;
41 case SPEED_100:
42 ctrl1 |= MDIO_PMA_CTRL1_SPEED100;
43 ctrl2 |= MDIO_PMA_CTRL2_100BTX;
44 break;
45 case SPEED_1000:
46 ctrl1 |= MDIO_PMA_CTRL1_SPEED1000;
47 /* Assume 1000base-T */
48 ctrl2 |= MDIO_PMA_CTRL2_1000BT;
49 break;
Maxime Chevallier7fd8afa2019-02-11 15:25:29 +010050 case SPEED_2500:
51 ctrl1 |= MDIO_CTRL1_SPEED2_5G;
52 /* Assume 2.5Gbase-T */
53 ctrl2 |= MDIO_PMA_CTRL2_2_5GBT;
54 break;
55 case SPEED_5000:
56 ctrl1 |= MDIO_CTRL1_SPEED5G;
57 /* Assume 5Gbase-T */
58 ctrl2 |= MDIO_PMA_CTRL2_5GBT;
59 break;
Russell King5acde342017-06-05 12:22:50 +010060 case SPEED_10000:
61 ctrl1 |= MDIO_CTRL1_SPEED10G;
62 /* Assume 10Gbase-T */
63 ctrl2 |= MDIO_PMA_CTRL2_10GBT;
64 break;
65 default:
66 return -EINVAL;
67 }
68
69 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1, ctrl1);
70 if (ret < 0)
71 return ret;
72
Heiner Kallweit29f000f72019-02-16 20:44:16 +010073 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL2, ctrl2);
74 if (ret < 0)
75 return ret;
76
77 return genphy_c45_an_disable_aneg(phydev);
Russell King5acde342017-06-05 12:22:50 +010078}
79EXPORT_SYMBOL_GPL(genphy_c45_pma_setup_forced);
80
81/**
Andrew Lunn9a5dc8a2019-02-17 10:29:19 +010082 * genphy_c45_an_config_aneg - configure advertisement registers
83 * @phydev: target phy_device struct
84 *
85 * Configure advertisement registers based on modes set in phydev->advertising
86 *
87 * Returns negative errno code on failure, 0 if advertisement didn't change,
88 * or 1 if advertised modes changed.
89 */
90int genphy_c45_an_config_aneg(struct phy_device *phydev)
91{
Heiner Kallweitcc429d52019-02-18 21:27:12 +010092 int changed, ret;
Andrew Lunn9a5dc8a2019-02-17 10:29:19 +010093 u32 adv;
94
95 linkmode_and(phydev->advertising, phydev->advertising,
96 phydev->supported);
97
Heiner Kallweitcc429d52019-02-18 21:27:12 +010098 changed = genphy_config_eee_advert(phydev);
99
Andrew Lunn9a5dc8a2019-02-17 10:29:19 +0100100 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
101
102 ret = phy_modify_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE,
103 ADVERTISE_ALL | ADVERTISE_100BASE4 |
104 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
105 adv);
106 if (ret < 0)
107 return ret;
108 if (ret > 0)
109 changed = 1;
110
111 adv = linkmode_adv_to_mii_10gbt_adv_t(phydev->advertising);
112
113 ret = phy_modify_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
114 MDIO_AN_10GBT_CTRL_ADV10G |
115 MDIO_AN_10GBT_CTRL_ADV5G |
116 MDIO_AN_10GBT_CTRL_ADV2_5G,
117 adv);
118 if (ret < 0)
119 return ret;
120 if (ret > 0)
121 changed = 1;
122
123 return changed;
124}
125EXPORT_SYMBOL_GPL(genphy_c45_an_config_aneg);
126
127/**
Russell King5acde342017-06-05 12:22:50 +0100128 * genphy_c45_an_disable_aneg - disable auto-negotiation
129 * @phydev: target phy_device struct
130 *
131 * Disable auto-negotiation in the Clause 45 PHY. The link parameters
132 * parameters are controlled through the PMA/PMD MMD registers.
133 *
134 * Returns zero on success, negative errno code on failure.
135 */
136int genphy_c45_an_disable_aneg(struct phy_device *phydev)
137{
Russell King5acde342017-06-05 12:22:50 +0100138
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100139 return phy_clear_bits_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1,
140 MDIO_AN_CTRL1_ENABLE | MDIO_AN_CTRL1_RESTART);
Russell King5acde342017-06-05 12:22:50 +0100141}
142EXPORT_SYMBOL_GPL(genphy_c45_an_disable_aneg);
143
144/**
145 * genphy_c45_restart_aneg - Enable and restart auto-negotiation
146 * @phydev: target phy_device struct
147 *
148 * This assumes that the auto-negotiation MMD is present.
149 *
150 * Enable and restart auto-negotiation.
151 */
152int genphy_c45_restart_aneg(struct phy_device *phydev)
153{
Heiner Kallweitb52c0182019-02-06 07:38:43 +0100154 return phy_set_bits_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1,
155 MDIO_AN_CTRL1_ENABLE | MDIO_AN_CTRL1_RESTART);
Russell King5acde342017-06-05 12:22:50 +0100156}
157EXPORT_SYMBOL_GPL(genphy_c45_restart_aneg);
158
159/**
160 * genphy_c45_aneg_done - return auto-negotiation complete status
161 * @phydev: target phy_device struct
162 *
163 * This assumes that the auto-negotiation MMD is present.
164 *
165 * Reads the status register from the auto-negotiation MMD, returning:
166 * - positive if auto-negotiation is complete
167 * - negative errno code on error
168 * - zero otherwise
169 */
170int genphy_c45_aneg_done(struct phy_device *phydev)
171{
172 int val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
173
174 return val < 0 ? val : val & MDIO_AN_STAT1_COMPLETE ? 1 : 0;
175}
176EXPORT_SYMBOL_GPL(genphy_c45_aneg_done);
177
178/**
179 * genphy_c45_read_link - read the overall link status from the MMDs
180 * @phydev: target phy_device struct
Russell King5acde342017-06-05 12:22:50 +0100181 *
182 * Read the link status from the specified MMDs, and if they all indicate
Heiner Kallweita6e11f62019-02-05 20:41:37 +0100183 * that the link is up, set phydev->link to 1. If an error is encountered,
Russell King5acde342017-06-05 12:22:50 +0100184 * a negative errno will be returned, otherwise zero.
185 */
Heiner Kallweit998a8a82019-02-07 21:41:46 +0100186int genphy_c45_read_link(struct phy_device *phydev)
Russell King5acde342017-06-05 12:22:50 +0100187{
Heiner Kallweit998a8a82019-02-07 21:41:46 +0100188 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Russell King5acde342017-06-05 12:22:50 +0100189 int val, devad;
190 bool link = true;
191
Heiner Kallweit998a8a82019-02-07 21:41:46 +0100192 /* The vendor devads and C22EXT do not report link status. Avoid the
193 * PHYXS instance as its status may depend on the MAC being
194 * appropriately configured for the negotiated speed.
195 */
196 mmd_mask &= ~(MDIO_DEVS_VEND1 | MDIO_DEVS_VEND2 | MDIO_DEVS_C22EXT |
197 MDIO_DEVS_PHYXS);
198
Heiner Kallweita6e11f62019-02-05 20:41:37 +0100199 while (mmd_mask && link) {
Russell King5acde342017-06-05 12:22:50 +0100200 devad = __ffs(mmd_mask);
201 mmd_mask &= ~BIT(devad);
202
203 /* The link state is latched low so that momentary link
Heiner Kallweit93c09702019-02-06 19:39:52 +0100204 * drops can be detected. Do not double-read the status
205 * in polling mode to detect such short link drops.
Russell King5acde342017-06-05 12:22:50 +0100206 */
Heiner Kallweit93c09702019-02-06 19:39:52 +0100207 if (!phy_polling_mode(phydev)) {
208 val = phy_read_mmd(phydev, devad, MDIO_STAT1);
209 if (val < 0)
210 return val;
Heiner Kallweitc397ab22019-02-07 20:22:20 +0100211 else if (val & MDIO_STAT1_LSTATUS)
212 continue;
Heiner Kallweit93c09702019-02-06 19:39:52 +0100213 }
214
Russell King5acde342017-06-05 12:22:50 +0100215 val = phy_read_mmd(phydev, devad, MDIO_STAT1);
216 if (val < 0)
217 return val;
218
219 if (!(val & MDIO_STAT1_LSTATUS))
220 link = false;
221 }
222
Heiner Kallweita6e11f62019-02-05 20:41:37 +0100223 phydev->link = link;
224
225 return 0;
Russell King5acde342017-06-05 12:22:50 +0100226}
227EXPORT_SYMBOL_GPL(genphy_c45_read_link);
228
229/**
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000230 * genphy_c45_read_lpa - read the link partner advertisement and pause
Russell King5acde342017-06-05 12:22:50 +0100231 * @phydev: target phy_device struct
232 *
233 * Read the Clause 45 defined base (7.19) and 10G (7.33) status registers,
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000234 * filling in the link partner advertisement, pause and asym_pause members
Russell King5acde342017-06-05 12:22:50 +0100235 * in @phydev. This assumes that the auto-negotiation MMD is present, and
236 * the backplane bit (7.48.0) is clear. Clause 45 PHY drivers are expected
237 * to fill in the remainder of the link partner advert from vendor registers.
238 */
239int genphy_c45_read_lpa(struct phy_device *phydev)
240{
241 int val;
242
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000243 /* Read the link partner's base page advertisement */
Russell King5acde342017-06-05 12:22:50 +0100244 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
245 if (val < 0)
246 return val;
247
Andrew Lunnc0ec3c22018-11-10 23:43:34 +0100248 mii_lpa_to_linkmode_lpa_t(phydev->lp_advertising, val);
Russell King5acde342017-06-05 12:22:50 +0100249 phydev->pause = val & LPA_PAUSE_CAP ? 1 : 0;
250 phydev->asym_pause = val & LPA_PAUSE_ASYM ? 1 : 0;
251
Colin Ian Kingcc1122b2018-03-01 10:23:03 +0000252 /* Read the link partner's 10G advertisement */
Russell King5acde342017-06-05 12:22:50 +0100253 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT);
254 if (val < 0)
255 return val;
256
Heiner Kallweit96c2be32019-02-16 17:26:50 +0100257 mii_10gbt_stat_mod_linkmode_lpa_t(phydev->lp_advertising, val);
Russell King5acde342017-06-05 12:22:50 +0100258
259 return 0;
260}
261EXPORT_SYMBOL_GPL(genphy_c45_read_lpa);
262
263/**
264 * genphy_c45_read_pma - read link speed etc from PMA
265 * @phydev: target phy_device struct
266 */
267int genphy_c45_read_pma(struct phy_device *phydev)
268{
269 int val;
270
271 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1);
272 if (val < 0)
273 return val;
274
275 switch (val & MDIO_CTRL1_SPEEDSEL) {
276 case 0:
277 phydev->speed = SPEED_10;
278 break;
279 case MDIO_PMA_CTRL1_SPEED100:
280 phydev->speed = SPEED_100;
281 break;
282 case MDIO_PMA_CTRL1_SPEED1000:
283 phydev->speed = SPEED_1000;
284 break;
Maxime Chevallier7fd8afa2019-02-11 15:25:29 +0100285 case MDIO_CTRL1_SPEED2_5G:
286 phydev->speed = SPEED_2500;
287 break;
288 case MDIO_CTRL1_SPEED5G:
289 phydev->speed = SPEED_5000;
290 break;
Russell King5acde342017-06-05 12:22:50 +0100291 case MDIO_CTRL1_SPEED10G:
292 phydev->speed = SPEED_10000;
293 break;
294 default:
295 phydev->speed = SPEED_UNKNOWN;
296 break;
297 }
298
299 phydev->duplex = DUPLEX_FULL;
300
301 return 0;
302}
303EXPORT_SYMBOL_GPL(genphy_c45_read_pma);
Russell King921690f2017-06-05 12:23:05 +0100304
Russell Kingea4efe22017-12-29 12:46:27 +0000305/**
306 * genphy_c45_read_mdix - read mdix status from PMA
307 * @phydev: target phy_device struct
308 */
309int genphy_c45_read_mdix(struct phy_device *phydev)
310{
311 int val;
312
313 if (phydev->speed == SPEED_10000) {
314 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
315 MDIO_PMA_10GBT_SWAPPOL);
316 if (val < 0)
317 return val;
318
319 switch (val) {
320 case MDIO_PMA_10GBT_SWAPPOL_ABNX | MDIO_PMA_10GBT_SWAPPOL_CDNX:
321 phydev->mdix = ETH_TP_MDI;
322 break;
323
324 case 0:
325 phydev->mdix = ETH_TP_MDI_X;
326 break;
327
328 default:
329 phydev->mdix = ETH_TP_MDI_INVALID;
330 break;
331 }
332 }
333
334 return 0;
335}
336EXPORT_SYMBOL_GPL(genphy_c45_read_mdix);
337
Maxime Chevallierac3f5532019-02-11 15:25:28 +0100338/**
339 * genphy_c45_pma_read_abilities - read supported link modes from PMA
340 * @phydev: target phy_device struct
341 *
342 * Read the supported link modes from the PMA Status 2 (1.8) register. If bit
343 * 1.8.9 is set, the list of supported modes is build using the values in the
344 * PMA Extended Abilities (1.11) register, indicating 1000BASET an 10G related
345 * modes. If bit 1.11.14 is set, then the list is also extended with the modes
346 * in the 2.5G/5G PMA Extended register (1.21), indicating if 2.5GBASET and
347 * 5GBASET are supported.
348 */
349int genphy_c45_pma_read_abilities(struct phy_device *phydev)
350{
351 int val;
352
353 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_STAT2);
354 if (val < 0)
355 return val;
356
357 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
358 phydev->supported,
359 val & MDIO_PMA_STAT2_10GBSR);
360
361 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
362 phydev->supported,
363 val & MDIO_PMA_STAT2_10GBLR);
364
365 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT,
366 phydev->supported,
367 val & MDIO_PMA_STAT2_10GBER);
368
369 if (val & MDIO_PMA_STAT2_EXTABLE) {
370 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_EXTABLE);
371 if (val < 0)
372 return val;
373
374 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT,
375 phydev->supported,
376 val & MDIO_PMA_EXTABLE_10GBLRM);
377 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
378 phydev->supported,
379 val & MDIO_PMA_EXTABLE_10GBT);
380 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
381 phydev->supported,
382 val & MDIO_PMA_EXTABLE_10GBKX4);
383 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
384 phydev->supported,
385 val & MDIO_PMA_EXTABLE_10GBKR);
386 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
387 phydev->supported,
388 val & MDIO_PMA_EXTABLE_1000BT);
389 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
390 phydev->supported,
391 val & MDIO_PMA_EXTABLE_1000BKX);
392
393 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
394 phydev->supported,
395 val & MDIO_PMA_EXTABLE_100BTX);
396 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
397 phydev->supported,
398 val & MDIO_PMA_EXTABLE_100BTX);
399
400 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
401 phydev->supported,
402 val & MDIO_PMA_EXTABLE_10BT);
403 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
404 phydev->supported,
405 val & MDIO_PMA_EXTABLE_10BT);
Maxime Chevallier7fd8afa2019-02-11 15:25:29 +0100406
407 if (val & MDIO_PMA_EXTABLE_NBT) {
408 val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
409 MDIO_PMA_NG_EXTABLE);
410 if (val < 0)
411 return val;
412
413 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
414 phydev->supported,
415 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
416
417 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
418 phydev->supported,
419 val & MDIO_PMA_NG_EXTABLE_5GBT);
420 }
Maxime Chevallierac3f5532019-02-11 15:25:28 +0100421 }
422
423 return 0;
424}
425EXPORT_SYMBOL_GPL(genphy_c45_pma_read_abilities);
426
Russell King921690f2017-06-05 12:23:05 +0100427/* The gen10g_* functions are the old Clause 45 stub */
428
Florian Fainellie8a714e2018-03-01 16:08:56 -0800429int gen10g_config_aneg(struct phy_device *phydev)
Russell King921690f2017-06-05 12:23:05 +0100430{
431 return 0;
432}
Florian Fainellie8a714e2018-03-01 16:08:56 -0800433EXPORT_SYMBOL_GPL(gen10g_config_aneg);
Russell King921690f2017-06-05 12:23:05 +0100434
Florian Fainellie8a714e2018-03-01 16:08:56 -0800435int gen10g_read_status(struct phy_device *phydev)
Russell King921690f2017-06-05 12:23:05 +0100436{
Russell King921690f2017-06-05 12:23:05 +0100437 /* For now just lie and say it's 10G all the time */
438 phydev->speed = SPEED_10000;
439 phydev->duplex = DUPLEX_FULL;
440
Heiner Kallweit998a8a82019-02-07 21:41:46 +0100441 return genphy_c45_read_link(phydev);
Russell King921690f2017-06-05 12:23:05 +0100442}
Florian Fainellie8a714e2018-03-01 16:08:56 -0800443EXPORT_SYMBOL_GPL(gen10g_read_status);
Russell King921690f2017-06-05 12:23:05 +0100444
Florian Fainellie8a714e2018-03-01 16:08:56 -0800445int gen10g_no_soft_reset(struct phy_device *phydev)
Russell King921690f2017-06-05 12:23:05 +0100446{
447 /* Do nothing for now */
448 return 0;
449}
Florian Fainellie8a714e2018-03-01 16:08:56 -0800450EXPORT_SYMBOL_GPL(gen10g_no_soft_reset);
Russell King921690f2017-06-05 12:23:05 +0100451
Florian Fainellie8a714e2018-03-01 16:08:56 -0800452int gen10g_config_init(struct phy_device *phydev)
Russell King921690f2017-06-05 12:23:05 +0100453{
454 /* Temporarily just say we support everything */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100455 linkmode_zero(phydev->supported);
456
457 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
458 phydev->supported);
459 linkmode_copy(phydev->advertising, phydev->supported);
Russell King921690f2017-06-05 12:23:05 +0100460
461 return 0;
462}
Florian Fainellie8a714e2018-03-01 16:08:56 -0800463EXPORT_SYMBOL_GPL(gen10g_config_init);
Russell King921690f2017-06-05 12:23:05 +0100464
Florian Fainellie8a714e2018-03-01 16:08:56 -0800465int gen10g_suspend(struct phy_device *phydev)
Russell King921690f2017-06-05 12:23:05 +0100466{
467 return 0;
468}
Florian Fainellie8a714e2018-03-01 16:08:56 -0800469EXPORT_SYMBOL_GPL(gen10g_suspend);
Russell King921690f2017-06-05 12:23:05 +0100470
Florian Fainellie8a714e2018-03-01 16:08:56 -0800471int gen10g_resume(struct phy_device *phydev)
Russell King921690f2017-06-05 12:23:05 +0100472{
473 return 0;
474}
Florian Fainellie8a714e2018-03-01 16:08:56 -0800475EXPORT_SYMBOL_GPL(gen10g_resume);
Russell King921690f2017-06-05 12:23:05 +0100476
477struct phy_driver genphy_10g_driver = {
478 .phy_id = 0xffffffff,
479 .phy_id_mask = 0xffffffff,
480 .name = "Generic 10G PHY",
Florian Fainellie8a714e2018-03-01 16:08:56 -0800481 .soft_reset = gen10g_no_soft_reset,
Russell King921690f2017-06-05 12:23:05 +0100482 .config_init = gen10g_config_init,
Andrew Lunnf8029122018-10-25 14:42:38 +0200483 .features = PHY_10GBIT_FEATURES,
Russell King921690f2017-06-05 12:23:05 +0100484 .config_aneg = gen10g_config_aneg,
485 .read_status = gen10g_read_status,
486 .suspend = gen10g_suspend,
487 .resume = gen10g_resume,
488};