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 <rmk+ker...@armlinux.org.uk>
---
 drivers/net/phy/phy-core.c | 157 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/phy.h        |  11 ++++
 2 files changed, 168 insertions(+)

diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c
index 37c039da0c16..df39e6711b76 100644
--- a/drivers/net/phy/phy-core.c
+++ b/drivers/net/phy/phy-core.c
@@ -305,3 +305,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 be called after this
+ * to release the lock even on failure.
+ */
+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 be called after this to restore the page
+ * number (if this call was successful) and release the lock.
+ */
+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);
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 3dae5b7408b4..f408220d95ec 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -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,
@@ -836,6 +839,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);
-- 
2.7.4

Reply via email to