2dbf9ebd107d8085d9c19bb359745a57a462108d
[openwrt/staging/lynxis.git] /
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
5 MMD registers
6
7 This adds full set of locked and unlocked accessor functions to read and
8 write PHY MMD registers and/or bitfields.
9
10 Set of functions exactly matches what is already available for PHY
11 legacy registers.
12
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>
18 ---
19 drivers/net/phy/phy-core.c | 116 ++++++++++++++++++++++++++++----
20 include/linux/phy.h | 134 ++++++++++++++++++++++++++++++-------
21 2 files changed, 214 insertions(+), 36 deletions(-)
22
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_
26 }
27
28 /**
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)
35 *
36 - * Same rules as for phy_read();
37 + * Same rules as for __phy_read();
38 */
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)
41 {
42 int val;
43
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);
47
48 - val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
49 + val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
50 } else {
51 struct mii_bus *bus = phydev->mdio.bus;
52 int phy_addr = phydev->mdio.addr;
53
54 - mutex_lock(&bus->mdio_lock);
55 mmd_phy_indirect(bus, phy_addr, devad, regnum);
56
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);
60 }
61 return val;
62 }
63 +EXPORT_SYMBOL(__phy_read_mmd);
64 +
65 +/**
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
71 + *
72 + * Same rules as for phy_read();
73 + */
74 +int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
75 +{
76 + int ret;
77 +
78 + mutex_lock(&phydev->mdio.bus->mdio_lock);
79 + ret = __phy_read_mmd(phydev, devad, regnum);
80 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
81 +
82 + return ret;
83 +}
84 EXPORT_SYMBOL(phy_read_mmd);
85
86 /**
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
94 *
95 - * Same rules as for phy_write();
96 + * Same rules as for __phy_write();
97 */
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)
100 {
101 int ret;
102
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);
106
107 - ret = mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
108 - addr, val);
109 + ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
110 + addr, val);
111 } else {
112 struct mii_bus *bus = phydev->mdio.bus;
113 int phy_addr = phydev->mdio.addr;
114
115 - mutex_lock(&bus->mdio_lock);
116 mmd_phy_indirect(bus, phy_addr, devad, regnum);
117
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);
121
122 ret = 0;
123 }
124 return ret;
125 }
126 +EXPORT_SYMBOL(__phy_write_mmd);
127 +
128 +/**
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
135 + *
136 + * Same rules as for phy_write();
137 + */
138 +int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
139 +{
140 + int ret;
141 +
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);
145 +
146 + return ret;
147 +}
148 EXPORT_SYMBOL(phy_write_mmd);
149
150 /**
151 @@ -371,6 +410,57 @@ int phy_modify(struct phy_device *phydev
152 }
153 EXPORT_SYMBOL_GPL(phy_modify);
154
155 +/**
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
162 + *
163 + * Unlocked helper function which allows a MMD register to be modified as
164 + * new register value = (old register value & ~mask) | set
165 + */
166 +int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
167 + u16 mask, u16 set)
168 +{
169 + int ret;
170 +
171 + ret = __phy_read_mmd(phydev, devad, regnum);
172 + if (ret < 0)
173 + return ret;
174 +
175 + ret = __phy_write_mmd(phydev, devad, regnum, (ret & ~mask) | set);
176 +
177 + return ret < 0 ? ret : 0;
178 +}
179 +EXPORT_SYMBOL_GPL(__phy_modify_mmd);
180 +
181 +/**
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
188 + *
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.
192 + */
193 +int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
194 + u16 mask, u16 set)
195 +{
196 + int ret;
197 +
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);
201 +
202 + return ret;
203 +}
204 +EXPORT_SYMBOL_GPL(phy_modify_mmd);
205 +
206 static int __phy_read_page(struct phy_device *phydev)
207 {
208 return phydev->drv->read_page(phydev);
209 --- a/include/linux/phy.h
210 +++ b/include/linux/phy.h
211 @@ -697,17 +697,6 @@ size_t phy_speeds(unsigned int *speeds,
212 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
213
214 /**
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
220 - *
221 - * Same rules as for phy_read();
222 - */
223 -int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
224 -
225 -/**
226 * phy_read - Convenience function for reading a given PHY register
227 * @phydev: the phy_device struct
228 * @regnum: register number to read
229 @@ -762,9 +751,60 @@ static inline int __phy_write(struct phy
230 val);
231 }
232
233 +/**
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
239 + *
240 + * Same rules as for phy_read();
241 + */
242 +int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
243 +
244 +/**
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
250 + *
251 + * Same rules as for __phy_read();
252 + */
253 +int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
254 +
255 +/**
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
262 + *
263 + * Same rules as for phy_write();
264 + */
265 +int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
266 +
267 +/**
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
274 + *
275 + * Same rules as for __phy_write();
276 + */
277 +int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
278 +
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);
281
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);
286 +
287 /**
288 * __phy_set_bits - Convenience function for setting bits in a PHY register
289 * @phydev: the phy_device struct
290 @@ -815,6 +855,66 @@ static inline int phy_clear_bits(struct
291 }
292
293 /**
294 + * __phy_set_bits_mmd - Convenience function for setting bits in a register
295 + * on MMD
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
300 + *
301 + * The caller must have taken the MDIO bus lock.
302 + */
303 +static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
304 + u32 regnum, u16 val)
305 +{
306 + return __phy_modify_mmd(phydev, devad, regnum, 0, val);
307 +}
308 +
309 +/**
310 + * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
311 + * on MMD
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
316 + *
317 + * The caller must have taken the MDIO bus lock.
318 + */
319 +static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
320 + u32 regnum, u16 val)
321 +{
322 + return __phy_modify_mmd(phydev, devad, regnum, val, 0);
323 +}
324 +
325 +/**
326 + * phy_set_bits_mmd - Convenience function for setting bits in a register
327 + * on MMD
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
332 + */
333 +static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
334 + u32 regnum, u16 val)
335 +{
336 + return phy_modify_mmd(phydev, devad, regnum, 0, val);
337 +}
338 +
339 +/**
340 + * phy_clear_bits_mmd - Convenience function for clearing bits in a register
341 + * on MMD
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
346 + */
347 +static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
348 + u32 regnum, u16 val)
349 +{
350 + return phy_modify_mmd(phydev, devad, regnum, val, 0);
351 +}
352 +
353 +/**
354 * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
355 * @phydev: the phy_device struct
356 *
357 @@ -890,18 +990,6 @@ static inline bool phy_is_pseudo_fixed_l
358 return phydev->is_pseudo_fixed_link;
359 }
360
361 -/**
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
368 - *
369 - * Same rules as for phy_write();
370 - */
371 -int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
372 -
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);