Skip to content

Commit

Permalink
net: phy: add paged phy register accessors
Browse files Browse the repository at this point in the history
Add a set of paged phy register accessors which are inherently safe in
their design against other accesses interfering with the paged access.

Signed-off-by: Russell King <[email protected]>
Reviewed-by: Andrew Lunn <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Russell King authored and davem330 committed Jan 3, 2018
1 parent 788f993 commit 78ffc4a
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
157 changes: 157 additions & 0 deletions drivers/net/phy/phy-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,160 @@ int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
return ret;
}
EXPORT_SYMBOL_GPL(__phy_modify);

static int __phy_read_page(struct phy_device *phydev)
{
return phydev->drv->read_page(phydev);
}

static int __phy_write_page(struct phy_device *phydev, int page)
{
return phydev->drv->write_page(phydev, page);
}

/**
* phy_save_page() - take the bus lock and save the current page
* @phydev: a pointer to a &struct phy_device
*
* Take the MDIO bus lock, and return the current page number. On error,
* returns a negative errno. phy_restore_page() must always be called
* after this, irrespective of success or failure of this call.
*/
int phy_save_page(struct phy_device *phydev)
{
mutex_lock(&phydev->mdio.bus->mdio_lock);
return __phy_read_page(phydev);
}
EXPORT_SYMBOL_GPL(phy_save_page);

/**
* phy_select_page() - take the bus lock, save the current page, and set a page
* @phydev: a pointer to a &struct phy_device
* @page: desired page
*
* Take the MDIO bus lock to protect against concurrent access, save the
* current PHY page, and set the current page. On error, returns a
* negative errno, otherwise returns the previous page number.
* phy_restore_page() must always be called after this, irrespective
* of success or failure of this call.
*/
int phy_select_page(struct phy_device *phydev, int page)
{
int ret, oldpage;

oldpage = ret = phy_save_page(phydev);
if (ret < 0)
return ret;

if (oldpage != page) {
ret = __phy_write_page(phydev, page);
if (ret < 0)
return ret;
}

return oldpage;
}
EXPORT_SYMBOL_GPL(phy_select_page);

/**
* phy_restore_page() - restore the page register and release the bus lock
* @phydev: a pointer to a &struct phy_device
* @oldpage: the old page, return value from phy_save_page() or phy_select_page()
* @ret: operation's return code
*
* Release the MDIO bus lock, restoring @oldpage if it is a valid page.
* This function propagates the earliest error code from the group of
* operations.
*
* Returns:
* @oldpage if it was a negative value, otherwise
* @ret if it was a negative errno value, otherwise
* phy_write_page()'s negative value if it were in error, otherwise
* @ret.
*/
int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
{
int r;

if (oldpage >= 0) {
r = __phy_write_page(phydev, oldpage);

/* Propagate the operation return code if the page write
* was successful.
*/
if (ret >= 0 && r < 0)
ret = r;
} else {
/* Propagate the phy page selection error code */
ret = oldpage;
}

mutex_unlock(&phydev->mdio.bus->mdio_lock);

return ret;
}
EXPORT_SYMBOL_GPL(phy_restore_page);

/**
* phy_read_paged() - Convenience function for reading a paged register
* @phydev: a pointer to a &struct phy_device
* @page: the page for the phy
* @regnum: register number
*
* Same rules as for phy_read().
*/
int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
{
int ret = 0, oldpage;

oldpage = phy_select_page(phydev, page);
if (oldpage >= 0)
ret = __phy_read(phydev, regnum);

return phy_restore_page(phydev, oldpage, ret);
}
EXPORT_SYMBOL(phy_read_paged);

/**
* phy_write_paged() - Convenience function for writing a paged register
* @phydev: a pointer to a &struct phy_device
* @page: the page for the phy
* @regnum: register number
* @val: value to write
*
* Same rules as for phy_write().
*/
int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
{
int ret = 0, oldpage;

oldpage = phy_select_page(phydev, page);
if (oldpage >= 0)
ret = __phy_write(phydev, regnum, val);

return phy_restore_page(phydev, oldpage, ret);
}
EXPORT_SYMBOL(phy_write_paged);

/**
* phy_modify_paged() - Convenience function for modifying a paged register
* @phydev: a pointer to a &struct phy_device
* @page: the page for the phy
* @regnum: register number
* @mask: bit mask of bits to clear
* @set: bit mask of bits to set
*
* Same rules as for phy_read() and phy_write().
*/
int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
u16 mask, u16 set)
{
int ret = 0, oldpage;

oldpage = phy_select_page(phydev, page);
if (oldpage >= 0)
ret = __phy_modify(phydev, regnum, mask, set);

return phy_restore_page(phydev, oldpage, ret);
}
EXPORT_SYMBOL(phy_modify_paged);
11 changes: 11 additions & 0 deletions include/linux/phy.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ struct phy_driver {
int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
u16 val);

int (*read_page)(struct phy_device *dev);
int (*write_page)(struct phy_device *dev, int page);

/* Get the size and type of the eeprom contained within a plug-in
* module */
int (*module_info)(struct phy_device *dev,
Expand Down Expand Up @@ -838,6 +841,14 @@ static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
*/
int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);

int phy_save_page(struct phy_device *phydev);
int phy_select_page(struct phy_device *phydev, int page);
int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
u16 mask, u16 set);

struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
bool is_c45,
struct phy_c45_device_ids *c45_ids);
Expand Down

0 comments on commit 78ffc4a

Please sign in to comment.