Skip to content

Commit 2e88810

Browse files
buytenhdavem330
authored andcommitted
phylib: add mdiobus_{read,write}
Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives. Signed-off-by: Lennert Buytenhek <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 46abc02 commit 2e88810

File tree

3 files changed

+87
-57
lines changed

3 files changed

+87
-57
lines changed

drivers/net/phy/mdio_bus.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,55 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
210210
}
211211
EXPORT_SYMBOL(mdiobus_scan);
212212

213+
/**
214+
* mdiobus_read - Convenience function for reading a given MII mgmt register
215+
* @bus: the mii_bus struct
216+
* @addr: the phy address
217+
* @regnum: register number to read
218+
*
219+
* NOTE: MUST NOT be called from interrupt context,
220+
* because the bus read/write functions may wait for an interrupt
221+
* to conclude the operation.
222+
*/
223+
int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
224+
{
225+
int retval;
226+
227+
BUG_ON(in_interrupt());
228+
229+
mutex_lock(&bus->mdio_lock);
230+
retval = bus->read(bus, addr, regnum);
231+
mutex_unlock(&bus->mdio_lock);
232+
233+
return retval;
234+
}
235+
EXPORT_SYMBOL(mdiobus_read);
236+
237+
/**
238+
* mdiobus_write - Convenience function for writing a given MII mgmt register
239+
* @bus: the mii_bus struct
240+
* @addr: the phy address
241+
* @regnum: register number to write
242+
* @val: value to write to @regnum
243+
*
244+
* NOTE: MUST NOT be called from interrupt context,
245+
* because the bus read/write functions may wait for an interrupt
246+
* to conclude the operation.
247+
*/
248+
int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
249+
{
250+
int err;
251+
252+
BUG_ON(in_interrupt());
253+
254+
mutex_lock(&bus->mdio_lock);
255+
err = bus->write(bus, addr, regnum, val);
256+
mutex_unlock(&bus->mdio_lock);
257+
258+
return err;
259+
}
260+
EXPORT_SYMBOL(mdiobus_write);
261+
213262
/**
214263
* mdio_bus_match - determine if given PHY driver supports the given PHY device
215264
* @dev: target PHY device

drivers/net/phy/phy.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,55 +57,6 @@ void phy_print_status(struct phy_device *phydev)
5757
EXPORT_SYMBOL(phy_print_status);
5858

5959

60-
/**
61-
* phy_read - Convenience function for reading a given PHY register
62-
* @phydev: the phy_device struct
63-
* @regnum: register number to read
64-
*
65-
* NOTE: MUST NOT be called from interrupt context,
66-
* because the bus read/write functions may wait for an interrupt
67-
* to conclude the operation.
68-
*/
69-
int phy_read(struct phy_device *phydev, u16 regnum)
70-
{
71-
int retval;
72-
struct mii_bus *bus = phydev->bus;
73-
74-
BUG_ON(in_interrupt());
75-
76-
mutex_lock(&bus->mdio_lock);
77-
retval = bus->read(bus, phydev->addr, regnum);
78-
mutex_unlock(&bus->mdio_lock);
79-
80-
return retval;
81-
}
82-
EXPORT_SYMBOL(phy_read);
83-
84-
/**
85-
* phy_write - Convenience function for writing a given PHY register
86-
* @phydev: the phy_device struct
87-
* @regnum: register number to write
88-
* @val: value to write to @regnum
89-
*
90-
* NOTE: MUST NOT be called from interrupt context,
91-
* because the bus read/write functions may wait for an interrupt
92-
* to conclude the operation.
93-
*/
94-
int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
95-
{
96-
int err;
97-
struct mii_bus *bus = phydev->bus;
98-
99-
BUG_ON(in_interrupt());
100-
101-
mutex_lock(&bus->mdio_lock);
102-
err = bus->write(bus, phydev->addr, regnum, val);
103-
mutex_unlock(&bus->mdio_lock);
104-
105-
return err;
106-
}
107-
EXPORT_SYMBOL(phy_write);
108-
10960
/**
11061
* phy_clear_interrupt - Ack the phy device's interrupt
11162
* @phydev: the phy_device struct

include/linux/phy.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ struct mii_bus {
122122
};
123123
#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
124124

125+
struct mii_bus *mdiobus_alloc(void);
126+
int mdiobus_register(struct mii_bus *bus);
127+
void mdiobus_unregister(struct mii_bus *bus);
128+
void mdiobus_free(struct mii_bus *bus);
129+
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
130+
int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum);
131+
int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val);
132+
133+
125134
#define PHY_INTERRUPT_DISABLED 0x0
126135
#define PHY_INTERRUPT_ENABLED 0x80000000
127136

@@ -399,8 +408,35 @@ struct phy_fixup {
399408
int (*run)(struct phy_device *phydev);
400409
};
401410

402-
int phy_read(struct phy_device *phydev, u16 regnum);
403-
int phy_write(struct phy_device *phydev, u16 regnum, u16 val);
411+
/**
412+
* phy_read - Convenience function for reading a given PHY register
413+
* @phydev: the phy_device struct
414+
* @regnum: register number to read
415+
*
416+
* NOTE: MUST NOT be called from interrupt context,
417+
* because the bus read/write functions may wait for an interrupt
418+
* to conclude the operation.
419+
*/
420+
static inline int phy_read(struct phy_device *phydev, u16 regnum)
421+
{
422+
return mdiobus_read(phydev->bus, phydev->addr, regnum);
423+
}
424+
425+
/**
426+
* phy_write - Convenience function for writing a given PHY register
427+
* @phydev: the phy_device struct
428+
* @regnum: register number to write
429+
* @val: value to write to @regnum
430+
*
431+
* NOTE: MUST NOT be called from interrupt context,
432+
* because the bus read/write functions may wait for an interrupt
433+
* to conclude the operation.
434+
*/
435+
static inline int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
436+
{
437+
return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
438+
}
439+
404440
int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id);
405441
struct phy_device* get_phy_device(struct mii_bus *bus, int addr);
406442
int phy_clear_interrupt(struct phy_device *phydev);
@@ -416,12 +452,6 @@ void phy_start(struct phy_device *phydev);
416452
void phy_stop(struct phy_device *phydev);
417453
int phy_start_aneg(struct phy_device *phydev);
418454

419-
struct mii_bus *mdiobus_alloc(void);
420-
int mdiobus_register(struct mii_bus *bus);
421-
void mdiobus_unregister(struct mii_bus *bus);
422-
void mdiobus_free(struct mii_bus *bus);
423-
struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
424-
425455
void phy_sanitize_settings(struct phy_device *phydev);
426456
int phy_stop_interrupts(struct phy_device *phydev);
427457
int phy_enable_interrupts(struct phy_device *phydev);

0 commit comments

Comments
 (0)