1 From fa6e98cee558622565c97924e922b97340aeabd8 Mon Sep 17 00:00:00 2001
2 From: Heiner Kallweit <hkallweit1@gmail.com>
3 Date: Tue, 22 Oct 2019 11:31:07 -0700
4 Subject: [PATCH] net: phy: add support for clause 37 auto-negotiation
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 This patch adds support for clause 37 1000Base-X auto-negotiation.
11 Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
12 Signed-off-by: Tao Ren <taoren@fb.com>
13 Tested-by: René van Dorst <opensource@vdorst.com>
14 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
15 Signed-off-by: David S. Miller <davem@davemloft.net>
17 drivers/net/phy/phy_device.c | 139 +++++++++++++++++++++++++++++++++++
18 include/linux/phy.h | 4 +
19 2 files changed, 143 insertions(+)
21 diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
22 index f1f60bd4865a..fa71998fea51 100644
23 --- a/drivers/net/phy/phy_device.c
24 +++ b/drivers/net/phy/phy_device.c
25 @@ -1607,6 +1607,40 @@ static int genphy_config_advert(struct phy_device *phydev)
30 + * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
31 + * @phydev: target phy_device struct
33 + * Description: Writes MII_ADVERTISE with the appropriate values,
34 + * after sanitizing the values to make sure we only advertise
35 + * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
36 + * hasn't changed, and > 0 if it has changed. This function is intended
37 + * for Clause 37 1000Base-X mode.
39 +static int genphy_c37_config_advert(struct phy_device *phydev)
43 + /* Only allow advertising what this PHY supports */
44 + linkmode_and(phydev->advertising, phydev->advertising,
47 + if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
48 + phydev->advertising))
49 + adv |= ADVERTISE_1000XFULL;
50 + if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
51 + phydev->advertising))
52 + adv |= ADVERTISE_1000XPAUSE;
53 + if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
54 + phydev->advertising))
55 + adv |= ADVERTISE_1000XPSE_ASYM;
57 + return phy_modify_changed(phydev, MII_ADVERTISE,
58 + ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
59 + ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
64 * genphy_config_eee_advert - disable unwanted eee mode advertisement
65 * @phydev: target phy_device struct
66 @@ -1715,6 +1749,54 @@ int __genphy_config_aneg(struct phy_device *phydev, bool changed)
68 EXPORT_SYMBOL(__genphy_config_aneg);
71 + * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
72 + * @phydev: target phy_device struct
74 + * Description: If auto-negotiation is enabled, we configure the
75 + * advertising, and then restart auto-negotiation. If it is not
76 + * enabled, then we write the BMCR. This function is intended
77 + * for use with Clause 37 1000Base-X mode.
79 +int genphy_c37_config_aneg(struct phy_device *phydev)
83 + if (phydev->autoneg != AUTONEG_ENABLE)
84 + return genphy_setup_forced(phydev);
86 + err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
91 + changed = genphy_c37_config_advert(phydev);
92 + if (changed < 0) /* error */
96 + /* Advertisement hasn't changed, but maybe aneg was never on to
97 + * begin with? Or maybe phy was isolated?
99 + int ctl = phy_read(phydev, MII_BMCR);
104 + if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
105 + changed = 1; /* do restart aneg */
108 + /* Only restart aneg if we are advertising something different
109 + * than we were before.
112 + return genphy_restart_aneg(phydev);
116 +EXPORT_SYMBOL(genphy_c37_config_aneg);
119 * genphy_aneg_done - return auto-negotiation status
120 * @phydev: target phy_device struct
121 @@ -1886,6 +1968,63 @@ int genphy_read_status(struct phy_device *phydev)
123 EXPORT_SYMBOL(genphy_read_status);
126 + * genphy_c37_read_status - check the link status and update current link state
127 + * @phydev: target phy_device struct
129 + * Description: Check the link, then figure out the current state
130 + * by comparing what we advertise with what the link partner
131 + * advertises. This function is for Clause 37 1000Base-X mode.
133 +int genphy_c37_read_status(struct phy_device *phydev)
135 + int lpa, err, old_link = phydev->link;
137 + /* Update the link, but return if there was an error */
138 + err = genphy_update_link(phydev);
142 + /* why bother the PHY if nothing can have changed */
143 + if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
146 + phydev->duplex = DUPLEX_UNKNOWN;
148 + phydev->asym_pause = 0;
150 + if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
151 + lpa = phy_read(phydev, MII_LPA);
155 + linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
156 + phydev->lp_advertising, lpa & LPA_LPACK);
157 + linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
158 + phydev->lp_advertising, lpa & LPA_1000XFULL);
159 + linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
160 + phydev->lp_advertising, lpa & LPA_1000XPAUSE);
161 + linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
162 + phydev->lp_advertising,
163 + lpa & LPA_1000XPAUSE_ASYM);
165 + phy_resolve_aneg_linkmode(phydev);
166 + } else if (phydev->autoneg == AUTONEG_DISABLE) {
167 + int bmcr = phy_read(phydev, MII_BMCR);
172 + if (bmcr & BMCR_FULLDPLX)
173 + phydev->duplex = DUPLEX_FULL;
175 + phydev->duplex = DUPLEX_HALF;
180 +EXPORT_SYMBOL(genphy_c37_read_status);
183 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
184 * @phydev: target phy_device struct
185 diff --git a/include/linux/phy.h b/include/linux/phy.h
186 index 9a0e981df502..78436d58ce7c 100644
187 --- a/include/linux/phy.h
188 +++ b/include/linux/phy.h
189 @@ -1106,6 +1106,10 @@ int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
190 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
191 u16 regnum, u16 val);
194 +int genphy_c37_config_aneg(struct phy_device *phydev);
195 +int genphy_c37_read_status(struct phy_device *phydev);
198 int genphy_c45_restart_aneg(struct phy_device *phydev);
199 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);