blob: d7fc25cdb716fd7e68539286064972cbc910cdcf [file] [log] [blame]
developer5d148cb2023-06-02 13:08:11 +08001From bc97a676615bd0ec66bb2a2a42c939455bf5bed6 Mon Sep 17 00:00:00 2001
2From: Sam Shih <sam.shih@mediatek.com>
3Date: Fri, 2 Jun 2023 13:06:27 +0800
4Subject: [PATCH]
5 [networking][999-2700-v5.7-iopoll-introduce-read_poll_timeout-macro.patch]
6
7---
8 drivers/net/phy/phy_device.c | 16 +++++----------
9 include/linux/iopoll.h | 40 +++++++++++++++++++++++++++++-------
10 include/linux/phy.h | 27 ++++++++++++++++++++++++
11 3 files changed, 65 insertions(+), 18 deletions(-)
12
13diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
14index 76a68bb02..0349801df 100644
15--- a/drivers/net/phy/phy_device.c
16+++ b/drivers/net/phy/phy_device.c
17@@ -1056,18 +1056,12 @@ EXPORT_SYMBOL(phy_disconnect);
18 static int phy_poll_reset(struct phy_device *phydev)
19 {
20 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
21- unsigned int retries = 12;
22- int ret;
23-
24- do {
25- msleep(50);
26- ret = phy_read(phydev, MII_BMCR);
27- if (ret < 0)
28- return ret;
29- } while (ret & BMCR_RESET && --retries);
30- if (ret & BMCR_RESET)
31- return -ETIMEDOUT;
32+ int ret, val;
33
34+ ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
35+ 50000, 600000, true);
36+ if (ret)
37+ return ret;
38 /* Some chips (smsc911x) may still need up to another 1ms after the
39 * BMCR_RESET bit is cleared before they are usable.
40 */
41diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h
42index 35e15dfd4..cb20c733b 100644
43--- a/include/linux/iopoll.h
44+++ b/include/linux/iopoll.h
45@@ -14,36 +14,41 @@
46 #include <linux/io.h>
47
48 /**
49- * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
50- * @op: accessor function (takes @addr as its only argument)
51- * @addr: Address to poll
52+ * read_poll_timeout - Periodically poll an address until a condition is
53+ * met or a timeout occurs
54+ * @op: accessor function (takes @args as its arguments)
55 * @val: Variable to read the value into
56 * @cond: Break condition (usually involving @val)
57 * @sleep_us: Maximum time to sleep between reads in us (0
58 * tight-loops). Should be less than ~20ms since usleep_range
59 * is used (see Documentation/timers/timers-howto.rst).
60 * @timeout_us: Timeout in us, 0 means never timeout
61+ * @sleep_before_read: if it is true, sleep @sleep_us before read.
62+ * @args: arguments for @op poll
63 *
64 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
65- * case, the last read value at @addr is stored in @val. Must not
66+ * case, the last read value at @args is stored in @val. Must not
67 * be called from atomic context if sleep_us or timeout_us are used.
68 *
69 * When available, you'll probably want to use one of the specialized
70 * macros defined below rather than this macro directly.
71 */
72-#define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
73+#define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \
74+ sleep_before_read, args...) \
75 ({ \
76 u64 __timeout_us = (timeout_us); \
77 unsigned long __sleep_us = (sleep_us); \
78 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
79 might_sleep_if((__sleep_us) != 0); \
80+ if (sleep_before_read && __sleep_us) \
81+ usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
82 for (;;) { \
83- (val) = op(addr); \
84+ (val) = op(args); \
85 if (cond) \
86 break; \
87 if (__timeout_us && \
88 ktime_compare(ktime_get(), __timeout) > 0) { \
89- (val) = op(addr); \
90+ (val) = op(args); \
91 break; \
92 } \
93 if (__sleep_us) \
94@@ -52,6 +57,27 @@
95 (cond) ? 0 : -ETIMEDOUT; \
96 })
97
98+/**
99+ * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
100+ * @op: accessor function (takes @addr as its only argument)
101+ * @addr: Address to poll
102+ * @val: Variable to read the value into
103+ * @cond: Break condition (usually involving @val)
104+ * @sleep_us: Maximum time to sleep between reads in us (0
105+ * tight-loops). Should be less than ~20ms since usleep_range
106+ * is used (see Documentation/timers/timers-howto.rst).
107+ * @timeout_us: Timeout in us, 0 means never timeout
108+ *
109+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
110+ * case, the last read value at @addr is stored in @val. Must not
111+ * be called from atomic context if sleep_us or timeout_us are used.
112+ *
113+ * When available, you'll probably want to use one of the specialized
114+ * macros defined below rather than this macro directly.
115+ */
116+#define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
117+ read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
118+
119 /**
120 * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
121 * @op: accessor function (takes @addr as its only argument)
122diff --git a/include/linux/phy.h b/include/linux/phy.h
123index a1070d60e..107dcbea4 100644
124--- a/include/linux/phy.h
125+++ b/include/linux/phy.h
126@@ -21,6 +21,7 @@
127 #include <linux/timer.h>
128 #include <linux/workqueue.h>
129 #include <linux/mod_devicetable.h>
130+#include <linux/iopoll.h>
131
132 #include <linux/atomic.h>
133
134@@ -714,6 +715,19 @@ static inline int phy_read(struct phy_device *phydev, u32 regnum)
135 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
136 }
137
138+#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
139+ timeout_us, sleep_before_read) \
140+({ \
141+ int __ret = read_poll_timeout(phy_read, val, (cond) || val < 0, \
142+ sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
143+ if (val < 0) \
144+ __ret = val; \
145+ if (__ret) \
146+ phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
147+ __ret; \
148+})
149+
150+
151 /**
152 * __phy_read - convenience function for reading a given PHY register
153 * @phydev: the phy_device struct
154@@ -766,6 +780,19 @@ static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
155 */
156 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
157
158+#define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
159+ sleep_us, timeout_us, sleep_before_read) \
160+({ \
161+ int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
162+ sleep_us, timeout_us, sleep_before_read, \
163+ phydev, devaddr, regnum); \
164+ if (val < 0) \
165+ __ret = val; \
166+ if (__ret) \
167+ phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
168+ __ret; \
169+})
170+
171 /**
172 * __phy_read_mmd - Convenience function for reading a register
173 * from an MMD on a given PHY.
174--
1752.34.1
176