blob: 51966d83da92c2296297346fdb952170b73da878 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0 */
Masahiro Yamada9b5e2df2016-12-28 00:36:03 +09002/*
3 * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
Masahiro Yamada9b5e2df2016-12-28 00:36:03 +09004 */
5
6#ifndef _LINUX_IOPOLL_H
7#define _LINUX_IOPOLL_H
8
9#include <linux/errno.h>
10#include <linux/io.h>
11#include <time.h>
12
13/**
Jagan Teki9a4c65b2020-05-02 12:45:01 +053014 * read_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
Masahiro Yamada9b5e2df2016-12-28 00:36:03 +090015 * @op: accessor function (takes @addr as its only argument)
16 * @addr: Address to poll
17 * @val: Variable to read the value into
18 * @cond: Break condition (usually involving @val)
19 * @timeout_us: Timeout in us, 0 means never timeout
20 *
21 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
22 * case, the last read value at @addr is stored in @val.
23 *
24 * When available, you'll probably want to use one of the specialized
25 * macros defined below rather than this macro directly.
26 */
Jagan Teki9a4c65b2020-05-02 12:45:01 +053027#define read_poll_timeout(op, addr, val, cond, timeout_us) \
Masahiro Yamada9b5e2df2016-12-28 00:36:03 +090028({ \
29 unsigned long timeout = timer_get_us() + timeout_us; \
30 for (;;) { \
31 (val) = op(addr); \
32 if (cond) \
33 break; \
34 if (timeout_us && time_after(timer_get_us(), timeout)) { \
35 (val) = op(addr); \
36 break; \
37 } \
38 } \
39 (cond) ? 0 : -ETIMEDOUT; \
40})
41
Jagan Teki9a4c65b2020-05-02 12:45:01 +053042#define readx_poll_timeout(op, addr, val, cond, timeout_us) \
43 read_poll_timeout(op, addr, val, cond, timeout_us)
Masahiro Yamada9b5e2df2016-12-28 00:36:03 +090044
45#define readb_poll_timeout(addr, val, cond, timeout_us) \
46 readx_poll_timeout(readb, addr, val, cond, timeout_us)
47
48#define readw_poll_timeout(addr, val, cond, timeout_us) \
49 readx_poll_timeout(readw, addr, val, cond, timeout_us)
50
51#define readl_poll_timeout(addr, val, cond, timeout_us) \
52 readx_poll_timeout(readl, addr, val, cond, timeout_us)
53
54#define readq_poll_timeout(addr, val, cond, timeout_us) \
55 readx_poll_timeout(readq, addr, val, cond, timeout_us)
56
57#define readb_relaxed_poll_timeout(addr, val, cond, timeout_us) \
58 readx_poll_timeout(readb_relaxed, addr, val, cond, timeout_us)
59
60#define readw_relaxed_poll_timeout(addr, val, cond, timeout_us) \
61 readx_poll_timeout(readw_relaxed, addr, val, cond, timeout_us)
62
63#define readl_relaxed_poll_timeout(addr, val, cond, timeout_us) \
64 readx_poll_timeout(readl_relaxed, addr, val, cond, timeout_us)
65
66#define readq_relaxed_poll_timeout(addr, val, cond, timeout_us) \
67 readx_poll_timeout(readq_relaxed, addr, val, cond, timeout_us)
68
69#endif /* _LINUX_IOPOLL_H */