1 From 8a74a77e9df9565c247da3df37657d6eabb314c6 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@armlinux.org.uk>
3 Date: Tue, 2 Jan 2018 10:58:43 +0000
4 Subject: [PATCH 295/454] net: phy: add paged phy register accessors
6 commit 78ffc4acceff48522b92d8fbf8f4a0ffe78838b2 upstream.
8 Add a set of paged phy register accessors which are inherently safe in
9 their design against other accesses interfering with the paged access.
11 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
12 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
13 Signed-off-by: David S. Miller <davem@davemloft.net>
15 drivers/net/phy/phy-core.c | 157 +++++++++++++++++++++++++++++++++++++
16 include/linux/phy.h | 11 +++
17 2 files changed, 168 insertions(+)
19 --- a/drivers/net/phy/phy-core.c
20 +++ b/drivers/net/phy/phy-core.c
21 @@ -305,3 +305,160 @@ int __phy_modify(struct phy_device *phyd
24 EXPORT_SYMBOL_GPL(__phy_modify);
26 +static int __phy_read_page(struct phy_device *phydev)
28 + return phydev->drv->read_page(phydev);
31 +static int __phy_write_page(struct phy_device *phydev, int page)
33 + return phydev->drv->write_page(phydev, page);
37 + * phy_save_page() - take the bus lock and save the current page
38 + * @phydev: a pointer to a &struct phy_device
40 + * Take the MDIO bus lock, and return the current page number. On error,
41 + * returns a negative errno. phy_restore_page() must always be called
42 + * after this, irrespective of success or failure of this call.
44 +int phy_save_page(struct phy_device *phydev)
46 + mutex_lock(&phydev->mdio.bus->mdio_lock);
47 + return __phy_read_page(phydev);
49 +EXPORT_SYMBOL_GPL(phy_save_page);
52 + * phy_select_page() - take the bus lock, save the current page, and set a page
53 + * @phydev: a pointer to a &struct phy_device
54 + * @page: desired page
56 + * Take the MDIO bus lock to protect against concurrent access, save the
57 + * current PHY page, and set the current page. On error, returns a
58 + * negative errno, otherwise returns the previous page number.
59 + * phy_restore_page() must always be called after this, irrespective
60 + * of success or failure of this call.
62 +int phy_select_page(struct phy_device *phydev, int page)
66 + oldpage = ret = phy_save_page(phydev);
70 + if (oldpage != page) {
71 + ret = __phy_write_page(phydev, page);
78 +EXPORT_SYMBOL_GPL(phy_select_page);
81 + * phy_restore_page() - restore the page register and release the bus lock
82 + * @phydev: a pointer to a &struct phy_device
83 + * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
84 + * @ret: operation's return code
86 + * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
87 + * This function propagates the earliest error code from the group of
91 + * @oldpage if it was a negative value, otherwise
92 + * @ret if it was a negative errno value, otherwise
93 + * phy_write_page()'s negative value if it were in error, otherwise
96 +int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
100 + if (oldpage >= 0) {
101 + r = __phy_write_page(phydev, oldpage);
103 + /* Propagate the operation return code if the page write
106 + if (ret >= 0 && r < 0)
109 + /* Propagate the phy page selection error code */
113 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
117 +EXPORT_SYMBOL_GPL(phy_restore_page);
120 + * phy_read_paged() - Convenience function for reading a paged register
121 + * @phydev: a pointer to a &struct phy_device
122 + * @page: the page for the phy
123 + * @regnum: register number
125 + * Same rules as for phy_read().
127 +int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
129 + int ret = 0, oldpage;
131 + oldpage = phy_select_page(phydev, page);
133 + ret = __phy_read(phydev, regnum);
135 + return phy_restore_page(phydev, oldpage, ret);
137 +EXPORT_SYMBOL(phy_read_paged);
140 + * phy_write_paged() - Convenience function for writing a paged register
141 + * @phydev: a pointer to a &struct phy_device
142 + * @page: the page for the phy
143 + * @regnum: register number
144 + * @val: value to write
146 + * Same rules as for phy_write().
148 +int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
150 + int ret = 0, oldpage;
152 + oldpage = phy_select_page(phydev, page);
154 + ret = __phy_write(phydev, regnum, val);
156 + return phy_restore_page(phydev, oldpage, ret);
158 +EXPORT_SYMBOL(phy_write_paged);
161 + * phy_modify_paged() - Convenience function for modifying a paged register
162 + * @phydev: a pointer to a &struct phy_device
163 + * @page: the page for the phy
164 + * @regnum: register number
165 + * @mask: bit mask of bits to clear
166 + * @set: bit mask of bits to set
168 + * Same rules as for phy_read() and phy_write().
170 +int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
173 + int ret = 0, oldpage;
175 + oldpage = phy_select_page(phydev, page);
177 + ret = __phy_modify(phydev, regnum, mask, set);
179 + return phy_restore_page(phydev, oldpage, ret);
181 +EXPORT_SYMBOL(phy_modify_paged);
182 --- a/include/linux/phy.h
183 +++ b/include/linux/phy.h
184 @@ -644,6 +644,9 @@ struct phy_driver {
185 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
188 + int (*read_page)(struct phy_device *dev);
189 + int (*write_page)(struct phy_device *dev, int page);
191 /* Get the size and type of the eeprom contained within a plug-in
193 int (*module_info)(struct phy_device *dev,
194 @@ -832,6 +835,14 @@ static inline bool phy_is_pseudo_fixed_l
196 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
198 +int phy_save_page(struct phy_device *phydev);
199 +int phy_select_page(struct phy_device *phydev, int page);
200 +int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
201 +int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
202 +int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
203 +int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
204 + u16 mask, u16 set);
206 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
208 struct phy_c45_device_ids *c45_ids);