brcm2708: add kernel 4.14 support
[openwrt/staging/chunkeey.git] / target / linux / brcm2708 / patches-4.14 / 950-0295-net-phy-add-paged-phy-register-accessors.patch
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
5
6 commit 78ffc4acceff48522b92d8fbf8f4a0ffe78838b2 upstream.
7
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.
10
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>
14 ---
15 drivers/net/phy/phy-core.c | 157 +++++++++++++++++++++++++++++++++++++
16 include/linux/phy.h | 11 +++
17 2 files changed, 168 insertions(+)
18
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
22 return ret;
23 }
24 EXPORT_SYMBOL_GPL(__phy_modify);
25 +
26 +static int __phy_read_page(struct phy_device *phydev)
27 +{
28 + return phydev->drv->read_page(phydev);
29 +}
30 +
31 +static int __phy_write_page(struct phy_device *phydev, int page)
32 +{
33 + return phydev->drv->write_page(phydev, page);
34 +}
35 +
36 +/**
37 + * phy_save_page() - take the bus lock and save the current page
38 + * @phydev: a pointer to a &struct phy_device
39 + *
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.
43 + */
44 +int phy_save_page(struct phy_device *phydev)
45 +{
46 + mutex_lock(&phydev->mdio.bus->mdio_lock);
47 + return __phy_read_page(phydev);
48 +}
49 +EXPORT_SYMBOL_GPL(phy_save_page);
50 +
51 +/**
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
55 + *
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.
61 + */
62 +int phy_select_page(struct phy_device *phydev, int page)
63 +{
64 + int ret, oldpage;
65 +
66 + oldpage = ret = phy_save_page(phydev);
67 + if (ret < 0)
68 + return ret;
69 +
70 + if (oldpage != page) {
71 + ret = __phy_write_page(phydev, page);
72 + if (ret < 0)
73 + return ret;
74 + }
75 +
76 + return oldpage;
77 +}
78 +EXPORT_SYMBOL_GPL(phy_select_page);
79 +
80 +/**
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
85 + *
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
88 + * operations.
89 + *
90 + * Returns:
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
94 + * @ret.
95 + */
96 +int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
97 +{
98 + int r;
99 +
100 + if (oldpage >= 0) {
101 + r = __phy_write_page(phydev, oldpage);
102 +
103 + /* Propagate the operation return code if the page write
104 + * was successful.
105 + */
106 + if (ret >= 0 && r < 0)
107 + ret = r;
108 + } else {
109 + /* Propagate the phy page selection error code */
110 + ret = oldpage;
111 + }
112 +
113 + mutex_unlock(&phydev->mdio.bus->mdio_lock);
114 +
115 + return ret;
116 +}
117 +EXPORT_SYMBOL_GPL(phy_restore_page);
118 +
119 +/**
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
124 + *
125 + * Same rules as for phy_read().
126 + */
127 +int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
128 +{
129 + int ret = 0, oldpage;
130 +
131 + oldpage = phy_select_page(phydev, page);
132 + if (oldpage >= 0)
133 + ret = __phy_read(phydev, regnum);
134 +
135 + return phy_restore_page(phydev, oldpage, ret);
136 +}
137 +EXPORT_SYMBOL(phy_read_paged);
138 +
139 +/**
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
145 + *
146 + * Same rules as for phy_write().
147 + */
148 +int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
149 +{
150 + int ret = 0, oldpage;
151 +
152 + oldpage = phy_select_page(phydev, page);
153 + if (oldpage >= 0)
154 + ret = __phy_write(phydev, regnum, val);
155 +
156 + return phy_restore_page(phydev, oldpage, ret);
157 +}
158 +EXPORT_SYMBOL(phy_write_paged);
159 +
160 +/**
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
167 + *
168 + * Same rules as for phy_read() and phy_write().
169 + */
170 +int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
171 + u16 mask, u16 set)
172 +{
173 + int ret = 0, oldpage;
174 +
175 + oldpage = phy_select_page(phydev, page);
176 + if (oldpage >= 0)
177 + ret = __phy_modify(phydev, regnum, mask, set);
178 +
179 + return phy_restore_page(phydev, oldpage, ret);
180 +}
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,
186 u16 val);
187
188 + int (*read_page)(struct phy_device *dev);
189 + int (*write_page)(struct phy_device *dev, int page);
190 +
191 /* Get the size and type of the eeprom contained within a plug-in
192 * module */
193 int (*module_info)(struct phy_device *dev,
194 @@ -832,6 +835,14 @@ static inline bool phy_is_pseudo_fixed_l
195 */
196 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
197
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);
205 +
206 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
207 bool is_c45,
208 struct phy_c45_device_ids *c45_ids);