1 From 80758d9542205cd2e9fa730067bc3888d4f5a096 Mon Sep 17 00:00:00 2001
2 From: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
3 Date: Wed, 6 Feb 2019 07:36:40 +0100
4 Subject: [PATCH 603/660] net: phy: provide full set of accessor functions to
7 This adds full set of locked and unlocked accessor functions to read and
8 write PHY MMD registers and/or bitfields.
10 Set of functions exactly matches what is already available for PHY
13 Signed-off-by: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
14 Signed-off-by: Andrew Lunn <andrew@lunn.ch>
15 Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
16 Signed-off-by: David S. Miller <davem@davemloft.net>
17 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
19 drivers/net/phy/phy-core.c | 116 ++++++++++++++++++++++++++++----
20 include/linux/phy.h | 134 ++++++++++++++++++++++++++++++-------
21 2 files changed, 214 insertions(+), 36 deletions(-)
23 --- a/drivers/net/phy/phy-core.c
24 +++ b/drivers/net/phy/phy-core.c
25 @@ -247,15 +247,15 @@ static void mmd_phy_indirect(struct mii_
29 - * phy_read_mmd - Convenience function for reading a register
30 + * __phy_read_mmd - Convenience function for reading a register
31 * from an MMD on a given PHY.
32 * @phydev: The phy_device struct
33 * @devad: The MMD to read from (0..31)
34 * @regnum: The register on the MMD to read (0..65535)
36 - * Same rules as for phy_read();
37 + * Same rules as for __phy_read();
39 -int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
40 +int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
44 @@ -267,33 +267,52 @@ int phy_read_mmd(struct phy_device *phyd
45 } else if (phydev->is_c45) {
46 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
48 - val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
49 + val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
51 struct mii_bus *bus = phydev->mdio.bus;
52 int phy_addr = phydev->mdio.addr;
54 - mutex_lock(&bus->mdio_lock);
55 mmd_phy_indirect(bus, phy_addr, devad, regnum);
57 /* Read the content of the MMD's selected register */
58 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
59 - mutex_unlock(&bus->mdio_lock);
63 +EXPORT_SYMBOL(__phy_read_mmd);
66 + * phy_read_mmd - Convenience function for reading a register
67 + * from an MMD on a given PHY.
68 + * @phydev: The phy_device struct
69 + * @devad: The MMD to read from
70 + * @regnum: The register on the MMD to read
72 + * Same rules as for phy_read();
74 +int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
78 + mutex_lock(&phydev->mdio.bus->mdio_lock);
79 + ret = __phy_read_mmd(phydev, devad, regnum);
80 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
84 EXPORT_SYMBOL(phy_read_mmd);
87 - * phy_write_mmd - Convenience function for writing a register
88 + * __phy_write_mmd - Convenience function for writing a register
89 * on an MMD on a given PHY.
90 * @phydev: The phy_device struct
91 * @devad: The MMD to read from
92 * @regnum: The register on the MMD to read
93 * @val: value to write to @regnum
95 - * Same rules as for phy_write();
96 + * Same rules as for __phy_write();
98 -int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
99 +int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
103 @@ -305,23 +324,43 @@ int phy_write_mmd(struct phy_device *phy
104 } else if (phydev->is_c45) {
105 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
107 - ret = mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
109 + ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
112 struct mii_bus *bus = phydev->mdio.bus;
113 int phy_addr = phydev->mdio.addr;
115 - mutex_lock(&bus->mdio_lock);
116 mmd_phy_indirect(bus, phy_addr, devad, regnum);
118 /* Write the data into MMD's selected register */
119 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
120 - mutex_unlock(&bus->mdio_lock);
126 +EXPORT_SYMBOL(__phy_write_mmd);
129 + * phy_write_mmd - Convenience function for writing a register
130 + * on an MMD on a given PHY.
131 + * @phydev: The phy_device struct
132 + * @devad: The MMD to read from
133 + * @regnum: The register on the MMD to read
134 + * @val: value to write to @regnum
136 + * Same rules as for phy_write();
138 +int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
142 + mutex_lock(&phydev->mdio.bus->mdio_lock);
143 + ret = __phy_write_mmd(phydev, devad, regnum, val);
144 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
148 EXPORT_SYMBOL(phy_write_mmd);
151 @@ -371,6 +410,57 @@ int phy_modify(struct phy_device *phydev
153 EXPORT_SYMBOL_GPL(phy_modify);
156 + * __phy_modify_mmd - Convenience function for modifying a register on MMD
157 + * @phydev: the phy_device struct
158 + * @devad: the MMD containing register to modify
159 + * @regnum: register number to modify
160 + * @mask: bit mask of bits to clear
161 + * @set: new value of bits set in mask to write to @regnum
163 + * Unlocked helper function which allows a MMD register to be modified as
164 + * new register value = (old register value & ~mask) | set
166 +int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
171 + ret = __phy_read_mmd(phydev, devad, regnum);
175 + ret = __phy_write_mmd(phydev, devad, regnum, (ret & ~mask) | set);
177 + return ret < 0 ? ret : 0;
179 +EXPORT_SYMBOL_GPL(__phy_modify_mmd);
182 + * phy_modify_mmd - Convenience function for modifying a register on MMD
183 + * @phydev: the phy_device struct
184 + * @devad: the MMD containing register to modify
185 + * @regnum: register number to modify
186 + * @mask: bit mask of bits to clear
187 + * @set: new value of bits set in mask to write to @regnum
189 + * NOTE: MUST NOT be called from interrupt context,
190 + * because the bus read/write functions may wait for an interrupt
191 + * to conclude the operation.
193 +int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
198 + mutex_lock(&phydev->mdio.bus->mdio_lock);
199 + ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
200 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
204 +EXPORT_SYMBOL_GPL(phy_modify_mmd);
206 static int __phy_read_page(struct phy_device *phydev)
208 return phydev->drv->read_page(phydev);
209 --- a/include/linux/phy.h
210 +++ b/include/linux/phy.h
211 @@ -695,17 +695,6 @@ size_t phy_speeds(unsigned int *speeds,
212 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
215 - * phy_read_mmd - Convenience function for reading a register
216 - * from an MMD on a given PHY.
217 - * @phydev: The phy_device struct
218 - * @devad: The MMD to read from
219 - * @regnum: The register on the MMD to read
221 - * Same rules as for phy_read();
223 -int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
226 * phy_read - Convenience function for reading a given PHY register
227 * @phydev: the phy_device struct
228 * @regnum: register number to read
229 @@ -760,9 +749,60 @@ static inline int __phy_write(struct phy
234 + * phy_read_mmd - Convenience function for reading a register
235 + * from an MMD on a given PHY.
236 + * @phydev: The phy_device struct
237 + * @devad: The MMD to read from
238 + * @regnum: The register on the MMD to read
240 + * Same rules as for phy_read();
242 +int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
245 + * __phy_read_mmd - Convenience function for reading a register
246 + * from an MMD on a given PHY.
247 + * @phydev: The phy_device struct
248 + * @devad: The MMD to read from
249 + * @regnum: The register on the MMD to read
251 + * Same rules as for __phy_read();
253 +int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
256 + * phy_write_mmd - Convenience function for writing a register
257 + * on an MMD on a given PHY.
258 + * @phydev: The phy_device struct
259 + * @devad: The MMD to write to
260 + * @regnum: The register on the MMD to read
261 + * @val: value to write to @regnum
263 + * Same rules as for phy_write();
265 +int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
268 + * __phy_write_mmd - Convenience function for writing a register
269 + * on an MMD on a given PHY.
270 + * @phydev: The phy_device struct
271 + * @devad: The MMD to write to
272 + * @regnum: The register on the MMD to read
273 + * @val: value to write to @regnum
275 + * Same rules as for __phy_write();
277 +int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
279 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
280 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
282 +int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
283 + u16 mask, u16 set);
284 +int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
285 + u16 mask, u16 set);
288 * __phy_set_bits - Convenience function for setting bits in a PHY register
289 * @phydev: the phy_device struct
290 @@ -813,6 +853,66 @@ static inline int phy_clear_bits(struct
294 + * __phy_set_bits_mmd - Convenience function for setting bits in a register
296 + * @phydev: the phy_device struct
297 + * @devad: the MMD containing register to modify
298 + * @regnum: register number to modify
299 + * @val: bits to set
301 + * The caller must have taken the MDIO bus lock.
303 +static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
304 + u32 regnum, u16 val)
306 + return __phy_modify_mmd(phydev, devad, regnum, 0, val);
310 + * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
312 + * @phydev: the phy_device struct
313 + * @devad: the MMD containing register to modify
314 + * @regnum: register number to modify
315 + * @val: bits to clear
317 + * The caller must have taken the MDIO bus lock.
319 +static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
320 + u32 regnum, u16 val)
322 + return __phy_modify_mmd(phydev, devad, regnum, val, 0);
326 + * phy_set_bits_mmd - Convenience function for setting bits in a register
328 + * @phydev: the phy_device struct
329 + * @devad: the MMD containing register to modify
330 + * @regnum: register number to modify
331 + * @val: bits to set
333 +static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
334 + u32 regnum, u16 val)
336 + return phy_modify_mmd(phydev, devad, regnum, 0, val);
340 + * phy_clear_bits_mmd - Convenience function for clearing bits in a register
342 + * @phydev: the phy_device struct
343 + * @devad: the MMD containing register to modify
344 + * @regnum: register number to modify
345 + * @val: bits to clear
347 +static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
348 + u32 regnum, u16 val)
350 + return phy_modify_mmd(phydev, devad, regnum, val, 0);
354 * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
355 * @phydev: the phy_device struct
357 @@ -888,18 +988,6 @@ static inline bool phy_is_pseudo_fixed_l
358 return phydev->is_pseudo_fixed_link;
362 - * phy_write_mmd - Convenience function for writing a register
363 - * on an MMD on a given PHY.
364 - * @phydev: The phy_device struct
365 - * @devad: The MMD to read from
366 - * @regnum: The register on the MMD to read
367 - * @val: value to write to @regnum
369 - * Same rules as for phy_write();
371 -int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
373 int phy_save_page(struct phy_device *phydev);
374 int phy_select_page(struct phy_device *phydev, int page);
375 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);