da76523730e68e8bfb84c6e25480b5d397c51b80
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 750-v5.5-net-phy-add-support-for-clause-37-auto-negotiation.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This patch adds support for clause 37 1000Base-X auto-negotiation.
10
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>
16 ---
17 drivers/net/phy/phy_device.c | 139 +++++++++++++++++++++++++++++++++++
18 include/linux/phy.h | 4 +
19 2 files changed, 143 insertions(+)
20
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)
26 return changed;
27 }
28
29 +/**
30 + * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
31 + * @phydev: target phy_device struct
32 + *
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.
38 + */
39 +static int genphy_c37_config_advert(struct phy_device *phydev)
40 +{
41 + u16 adv = 0;
42 +
43 + /* Only allow advertising what this PHY supports */
44 + linkmode_and(phydev->advertising, phydev->advertising,
45 + phydev->supported);
46 +
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;
56 +
57 + return phy_modify_changed(phydev, MII_ADVERTISE,
58 + ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
59 + ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
60 + adv);
61 +}
62 +
63 /**
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)
67 }
68 EXPORT_SYMBOL(__genphy_config_aneg);
69
70 +/**
71 + * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
72 + * @phydev: target phy_device struct
73 + *
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.
78 + */
79 +int genphy_c37_config_aneg(struct phy_device *phydev)
80 +{
81 + int err, changed;
82 +
83 + if (phydev->autoneg != AUTONEG_ENABLE)
84 + return genphy_setup_forced(phydev);
85 +
86 + err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
87 + BMCR_SPEED1000);
88 + if (err)
89 + return err;
90 +
91 + changed = genphy_c37_config_advert(phydev);
92 + if (changed < 0) /* error */
93 + return changed;
94 +
95 + if (!changed) {
96 + /* Advertisement hasn't changed, but maybe aneg was never on to
97 + * begin with? Or maybe phy was isolated?
98 + */
99 + int ctl = phy_read(phydev, MII_BMCR);
100 +
101 + if (ctl < 0)
102 + return ctl;
103 +
104 + if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
105 + changed = 1; /* do restart aneg */
106 + }
107 +
108 + /* Only restart aneg if we are advertising something different
109 + * than we were before.
110 + */
111 + if (changed > 0)
112 + return genphy_restart_aneg(phydev);
113 +
114 + return 0;
115 +}
116 +EXPORT_SYMBOL(genphy_c37_config_aneg);
117 +
118 /**
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)
122 }
123 EXPORT_SYMBOL(genphy_read_status);
124
125 +/**
126 + * genphy_c37_read_status - check the link status and update current link state
127 + * @phydev: target phy_device struct
128 + *
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.
132 + */
133 +int genphy_c37_read_status(struct phy_device *phydev)
134 +{
135 + int lpa, err, old_link = phydev->link;
136 +
137 + /* Update the link, but return if there was an error */
138 + err = genphy_update_link(phydev);
139 + if (err)
140 + return err;
141 +
142 + /* why bother the PHY if nothing can have changed */
143 + if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
144 + return 0;
145 +
146 + phydev->duplex = DUPLEX_UNKNOWN;
147 + phydev->pause = 0;
148 + phydev->asym_pause = 0;
149 +
150 + if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
151 + lpa = phy_read(phydev, MII_LPA);
152 + if (lpa < 0)
153 + return lpa;
154 +
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);
164 +
165 + phy_resolve_aneg_linkmode(phydev);
166 + } else if (phydev->autoneg == AUTONEG_DISABLE) {
167 + int bmcr = phy_read(phydev, MII_BMCR);
168 +
169 + if (bmcr < 0)
170 + return bmcr;
171 +
172 + if (bmcr & BMCR_FULLDPLX)
173 + phydev->duplex = DUPLEX_FULL;
174 + else
175 + phydev->duplex = DUPLEX_HALF;
176 + }
177 +
178 + return 0;
179 +}
180 +EXPORT_SYMBOL(genphy_c37_read_status);
181 +
182 /**
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);
192
193 +/* Clause 37 */
194 +int genphy_c37_config_aneg(struct phy_device *phydev);
195 +int genphy_c37_read_status(struct phy_device *phydev);
196 +
197 /* Clause 45 PHY */
198 int genphy_c45_restart_aneg(struct phy_device *phydev);
199 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);