2 * Clause 45 PHY support
4 #include <linux/ethtool.h>
5 #include <linux/export.h>
6 #include <linux/mdio.h>
11 * genphy_c45_setup_forced - configures a forced speed
12 * @phydev: target phy_device struct
14 int genphy_c45_pma_setup_forced(struct phy_device
*phydev
)
16 int ctrl1
, ctrl2
, ret
;
18 /* Half duplex is not supported */
19 if (phydev
->duplex
!= DUPLEX_FULL
)
22 ctrl1
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_CTRL1
);
26 ctrl2
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_CTRL2
);
30 ctrl1
&= ~MDIO_CTRL1_SPEEDSEL
;
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.
35 ctrl2
&= ~(MDIO_PMA_CTRL2_TYPE
| 0x30);
37 switch (phydev
->speed
) {
39 ctrl2
|= MDIO_PMA_CTRL2_10BT
;
42 ctrl1
|= MDIO_PMA_CTRL1_SPEED100
;
43 ctrl2
|= MDIO_PMA_CTRL2_100BTX
;
46 ctrl1
|= MDIO_PMA_CTRL1_SPEED1000
;
47 /* Assume 1000base-T */
48 ctrl2
|= MDIO_PMA_CTRL2_1000BT
;
51 ctrl1
|= MDIO_CTRL1_SPEED2_5G
;
52 /* Assume 2.5Gbase-T */
53 ctrl2
|= MDIO_PMA_CTRL2_2_5GBT
;
56 ctrl1
|= MDIO_CTRL1_SPEED5G
;
58 ctrl2
|= MDIO_PMA_CTRL2_5GBT
;
61 ctrl1
|= MDIO_CTRL1_SPEED10G
;
62 /* Assume 10Gbase-T */
63 ctrl2
|= MDIO_PMA_CTRL2_10GBT
;
69 ret
= phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_CTRL1
, ctrl1
);
73 ret
= phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_CTRL2
, ctrl2
);
77 return genphy_c45_an_disable_aneg(phydev
);
79 EXPORT_SYMBOL_GPL(genphy_c45_pma_setup_forced
);
82 * genphy_c45_an_config_aneg - configure advertisement registers
83 * @phydev: target phy_device struct
85 * Configure advertisement registers based on modes set in phydev->advertising
87 * Returns negative errno code on failure, 0 if advertisement didn't change,
88 * or 1 if advertised modes changed.
90 int genphy_c45_an_config_aneg(struct phy_device
*phydev
)
95 linkmode_and(phydev
->advertising
, phydev
->advertising
,
98 changed
= genphy_config_eee_advert(phydev
);
100 adv
= linkmode_adv_to_mii_adv_t(phydev
->advertising
);
102 ret
= phy_modify_mmd_changed(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
,
103 ADVERTISE_ALL
| ADVERTISE_100BASE4
|
104 ADVERTISE_PAUSE_CAP
| ADVERTISE_PAUSE_ASYM
,
111 adv
= linkmode_adv_to_mii_10gbt_adv_t(phydev
->advertising
);
113 ret
= phy_modify_mmd_changed(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
, adv
);
124 EXPORT_SYMBOL_GPL(genphy_c45_an_config_aneg
);
127 * genphy_c45_an_disable_aneg - disable auto-negotiation
128 * @phydev: target phy_device struct
130 * Disable auto-negotiation in the Clause 45 PHY. The link parameters
131 * parameters are controlled through the PMA/PMD MMD registers.
133 * Returns zero on success, negative errno code on failure.
135 int genphy_c45_an_disable_aneg(struct phy_device
*phydev
)
138 return phy_clear_bits_mmd(phydev
, MDIO_MMD_AN
, MDIO_CTRL1
,
139 MDIO_AN_CTRL1_ENABLE
| MDIO_AN_CTRL1_RESTART
);
141 EXPORT_SYMBOL_GPL(genphy_c45_an_disable_aneg
);
144 * genphy_c45_restart_aneg - Enable and restart auto-negotiation
145 * @phydev: target phy_device struct
147 * This assumes that the auto-negotiation MMD is present.
149 * Enable and restart auto-negotiation.
151 int genphy_c45_restart_aneg(struct phy_device
*phydev
)
153 return phy_set_bits_mmd(phydev
, MDIO_MMD_AN
, MDIO_CTRL1
,
154 MDIO_AN_CTRL1_ENABLE
| MDIO_AN_CTRL1_RESTART
);
156 EXPORT_SYMBOL_GPL(genphy_c45_restart_aneg
);
159 * genphy_c45_check_and_restart_aneg - Enable and restart auto-negotiation
160 * @phydev: target phy_device struct
161 * @restart: whether aneg restart is requested
163 * This assumes that the auto-negotiation MMD is present.
165 * Check, and restart auto-negotiation if needed.
167 int genphy_c45_check_and_restart_aneg(struct phy_device
*phydev
, bool restart
)
172 /* Configure and restart aneg if it wasn't set before */
173 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_CTRL1
);
177 if (!(ret
& MDIO_AN_CTRL1_ENABLE
))
182 ret
= genphy_c45_restart_aneg(phydev
);
186 EXPORT_SYMBOL_GPL(genphy_c45_check_and_restart_aneg
);
189 * genphy_c45_aneg_done - return auto-negotiation complete status
190 * @phydev: target phy_device struct
192 * This assumes that the auto-negotiation MMD is present.
194 * Reads the status register from the auto-negotiation MMD, returning:
195 * - positive if auto-negotiation is complete
196 * - negative errno code on error
199 int genphy_c45_aneg_done(struct phy_device
*phydev
)
201 int val
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_STAT1
);
203 return val
< 0 ? val
: val
& MDIO_AN_STAT1_COMPLETE
? 1 : 0;
205 EXPORT_SYMBOL_GPL(genphy_c45_aneg_done
);
208 * genphy_c45_read_link - read the overall link status from the MMDs
209 * @phydev: target phy_device struct
211 * Read the link status from the specified MMDs, and if they all indicate
212 * that the link is up, set phydev->link to 1. If an error is encountered,
213 * a negative errno will be returned, otherwise zero.
215 int genphy_c45_read_link(struct phy_device
*phydev
)
217 u32 mmd_mask
= MDIO_DEVS_PMAPMD
;
221 while (mmd_mask
&& link
) {
222 devad
= __ffs(mmd_mask
);
223 mmd_mask
&= ~BIT(devad
);
225 /* The link state is latched low so that momentary link
226 * drops can be detected. Do not double-read the status
227 * in polling mode to detect such short link drops.
229 if (!phy_polling_mode(phydev
)) {
230 val
= phy_read_mmd(phydev
, devad
, MDIO_STAT1
);
233 else if (val
& MDIO_STAT1_LSTATUS
)
237 val
= phy_read_mmd(phydev
, devad
, MDIO_STAT1
);
241 if (!(val
& MDIO_STAT1_LSTATUS
))
249 EXPORT_SYMBOL_GPL(genphy_c45_read_link
);
252 * genphy_c45_read_lpa - read the link partner advertisement and pause
253 * @phydev: target phy_device struct
255 * Read the Clause 45 defined base (7.19) and 10G (7.33) status registers,
256 * filling in the link partner advertisement, pause and asym_pause members
257 * in @phydev. This assumes that the auto-negotiation MMD is present, and
258 * the backplane bit (7.48.0) is clear. Clause 45 PHY drivers are expected
259 * to fill in the remainder of the link partner advert from vendor registers.
261 int genphy_c45_read_lpa(struct phy_device
*phydev
)
265 /* Read the link partner's base page advertisement */
266 val
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPA
);
270 mii_lpa_mod_linkmode_lpa_t(phydev
->lp_advertising
, val
);
271 phydev
->pause
= val
& LPA_PAUSE_CAP
? 1 : 0;
272 phydev
->asym_pause
= val
& LPA_PAUSE_ASYM
? 1 : 0;
274 /* Read the link partner's 10G advertisement */
275 val
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_10GBT_STAT
);
279 mii_10gbt_stat_mod_linkmode_lpa_t(phydev
->lp_advertising
, val
);
283 EXPORT_SYMBOL_GPL(genphy_c45_read_lpa
);
286 * genphy_c45_read_pma - read link speed etc from PMA
287 * @phydev: target phy_device struct
289 int genphy_c45_read_pma(struct phy_device
*phydev
)
293 val
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_CTRL1
);
297 switch (val
& MDIO_CTRL1_SPEEDSEL
) {
299 phydev
->speed
= SPEED_10
;
301 case MDIO_PMA_CTRL1_SPEED100
:
302 phydev
->speed
= SPEED_100
;
304 case MDIO_PMA_CTRL1_SPEED1000
:
305 phydev
->speed
= SPEED_1000
;
307 case MDIO_CTRL1_SPEED2_5G
:
308 phydev
->speed
= SPEED_2500
;
310 case MDIO_CTRL1_SPEED5G
:
311 phydev
->speed
= SPEED_5000
;
313 case MDIO_CTRL1_SPEED10G
:
314 phydev
->speed
= SPEED_10000
;
317 phydev
->speed
= SPEED_UNKNOWN
;
321 phydev
->duplex
= DUPLEX_FULL
;
325 EXPORT_SYMBOL_GPL(genphy_c45_read_pma
);
328 * genphy_c45_read_mdix - read mdix status from PMA
329 * @phydev: target phy_device struct
331 int genphy_c45_read_mdix(struct phy_device
*phydev
)
335 if (phydev
->speed
== SPEED_10000
) {
336 val
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
,
337 MDIO_PMA_10GBT_SWAPPOL
);
342 case MDIO_PMA_10GBT_SWAPPOL_ABNX
| MDIO_PMA_10GBT_SWAPPOL_CDNX
:
343 phydev
->mdix
= ETH_TP_MDI
;
347 phydev
->mdix
= ETH_TP_MDI_X
;
351 phydev
->mdix
= ETH_TP_MDI_INVALID
;
358 EXPORT_SYMBOL_GPL(genphy_c45_read_mdix
);
361 * genphy_c45_pma_read_abilities - read supported link modes from PMA
362 * @phydev: target phy_device struct
364 * Read the supported link modes from the PMA Status 2 (1.8) register. If bit
365 * 1.8.9 is set, the list of supported modes is build using the values in the
366 * PMA Extended Abilities (1.11) register, indicating 1000BASET an 10G related
367 * modes. If bit 1.11.14 is set, then the list is also extended with the modes
368 * in the 2.5G/5G PMA Extended register (1.21), indicating if 2.5GBASET and
369 * 5GBASET are supported.
371 int genphy_c45_pma_read_abilities(struct phy_device
*phydev
)
375 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, phydev
->supported
);
376 if (phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
) {
377 val
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_STAT1
);
381 if (val
& MDIO_AN_STAT1_ABLE
)
382 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
,
386 val
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_STAT2
);
390 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT
,
392 val
& MDIO_PMA_STAT2_10GBSR
);
394 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT
,
396 val
& MDIO_PMA_STAT2_10GBLR
);
398 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT
,
400 val
& MDIO_PMA_STAT2_10GBER
);
402 if (val
& MDIO_PMA_STAT2_EXTABLE
) {
403 val
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_EXTABLE
);
407 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT
,
409 val
& MDIO_PMA_EXTABLE_10GBLRM
);
410 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT
,
412 val
& MDIO_PMA_EXTABLE_10GBT
);
413 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT
,
415 val
& MDIO_PMA_EXTABLE_10GBKX4
);
416 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT
,
418 val
& MDIO_PMA_EXTABLE_10GBKR
);
419 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT
,
421 val
& MDIO_PMA_EXTABLE_1000BT
);
422 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT
,
424 val
& MDIO_PMA_EXTABLE_1000BKX
);
426 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
,
428 val
& MDIO_PMA_EXTABLE_100BTX
);
429 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
,
431 val
& MDIO_PMA_EXTABLE_100BTX
);
433 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT
,
435 val
& MDIO_PMA_EXTABLE_10BT
);
436 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT
,
438 val
& MDIO_PMA_EXTABLE_10BT
);
440 if (val
& MDIO_PMA_EXTABLE_NBT
) {
441 val
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
,
442 MDIO_PMA_NG_EXTABLE
);
446 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT
,
448 val
& MDIO_PMA_NG_EXTABLE_2_5GBT
);
450 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT
,
452 val
& MDIO_PMA_NG_EXTABLE_5GBT
);
458 EXPORT_SYMBOL_GPL(genphy_c45_pma_read_abilities
);
461 * genphy_c45_read_status - read PHY status
462 * @phydev: target phy_device struct
464 * Reads status from PHY and sets phy_device members accordingly.
466 int genphy_c45_read_status(struct phy_device
*phydev
)
470 ret
= genphy_c45_read_link(phydev
);
474 phydev
->speed
= SPEED_UNKNOWN
;
475 phydev
->duplex
= DUPLEX_UNKNOWN
;
477 phydev
->asym_pause
= 0;
479 if (phydev
->autoneg
== AUTONEG_ENABLE
) {
480 ret
= genphy_c45_read_lpa(phydev
);
484 phy_resolve_aneg_linkmode(phydev
);
486 ret
= genphy_c45_read_pma(phydev
);
491 EXPORT_SYMBOL_GPL(genphy_c45_read_status
);
493 /* The gen10g_* functions are the old Clause 45 stub */
495 int gen10g_config_aneg(struct phy_device
*phydev
)
499 EXPORT_SYMBOL_GPL(gen10g_config_aneg
);
501 static int gen10g_read_status(struct phy_device
*phydev
)
503 /* For now just lie and say it's 10G all the time */
504 phydev
->speed
= SPEED_10000
;
505 phydev
->duplex
= DUPLEX_FULL
;
507 return genphy_c45_read_link(phydev
);
510 struct phy_driver genphy_10g_driver
= {
511 .phy_id
= 0xffffffff,
512 .phy_id_mask
= 0xffffffff,
513 .name
= "Generic 10G PHY",
514 .soft_reset
= genphy_no_soft_reset
,
515 .features
= PHY_10GBIT_FEATURES
,
516 .config_aneg
= gen10g_config_aneg
,
517 .read_status
= gen10g_read_status
,