Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved. |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _LINUX_IOPOLL_H |
| 7 | #define _LINUX_IOPOLL_H |
| 8 | |
Jagan Teki | c094e21 | 2020-05-02 12:45:02 +0530 | [diff] [blame] | 9 | #include <linux/delay.h> |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <time.h> |
| 13 | |
| 14 | /** |
Jagan Teki | 5423247 | 2020-05-02 12:45:01 +0530 | [diff] [blame] | 15 | * read_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 16 | * @op: accessor function (takes @addr as its only argument) |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 17 | * @val: Variable to read the value into |
| 18 | * @cond: Break condition (usually involving @val) |
Jagan Teki | c094e21 | 2020-05-02 12:45:02 +0530 | [diff] [blame] | 19 | * @sleep_us: Maximum time to sleep in us |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 20 | * @timeout_us: Timeout in us, 0 means never timeout |
Ariel D'Alessandro | d7db0e6 | 2022-04-12 10:31:35 -0300 | [diff] [blame] | 21 | * @args: arguments for @op poll |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 22 | * |
| 23 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
| 24 | * case, the last read value at @addr is stored in @val. |
| 25 | * |
| 26 | * When available, you'll probably want to use one of the specialized |
| 27 | * macros defined below rather than this macro directly. |
| 28 | */ |
Ariel D'Alessandro | d7db0e6 | 2022-04-12 10:31:35 -0300 | [diff] [blame] | 29 | #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, args...) \ |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 30 | ({ \ |
| 31 | unsigned long timeout = timer_get_us() + timeout_us; \ |
| 32 | for (;;) { \ |
Ariel D'Alessandro | d7db0e6 | 2022-04-12 10:31:35 -0300 | [diff] [blame] | 33 | (val) = op(args); \ |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 34 | if (cond) \ |
| 35 | break; \ |
| 36 | if (timeout_us && time_after(timer_get_us(), timeout)) { \ |
Ariel D'Alessandro | d7db0e6 | 2022-04-12 10:31:35 -0300 | [diff] [blame] | 37 | (val) = op(args); \ |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 38 | break; \ |
| 39 | } \ |
Jagan Teki | c094e21 | 2020-05-02 12:45:02 +0530 | [diff] [blame] | 40 | if (sleep_us) \ |
| 41 | udelay(sleep_us); \ |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 42 | } \ |
| 43 | (cond) ? 0 : -ETIMEDOUT; \ |
| 44 | }) |
| 45 | |
Jagan Teki | ce786ae | 2020-05-02 12:45:03 +0530 | [diff] [blame] | 46 | #define readx_poll_sleep_timeout(op, addr, val, cond, sleep_us, timeout_us) \ |
Ariel D'Alessandro | d7db0e6 | 2022-04-12 10:31:35 -0300 | [diff] [blame] | 47 | read_poll_timeout(op, val, cond, sleep_us, timeout_us, addr) |
Jagan Teki | ce786ae | 2020-05-02 12:45:03 +0530 | [diff] [blame] | 48 | |
| 49 | #define readl_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \ |
| 50 | readx_poll_sleep_timeout(readl, addr, val, cond, sleep_us, timeout_us) |
| 51 | |
Jagan Teki | 5423247 | 2020-05-02 12:45:01 +0530 | [diff] [blame] | 52 | #define readx_poll_timeout(op, addr, val, cond, timeout_us) \ |
Ariel D'Alessandro | d7db0e6 | 2022-04-12 10:31:35 -0300 | [diff] [blame] | 53 | read_poll_timeout(op, val, cond, false, timeout_us, addr) |
Masahiro Yamada | 6569c0d | 2016-12-28 00:36:03 +0900 | [diff] [blame] | 54 | |
| 55 | #define readb_poll_timeout(addr, val, cond, timeout_us) \ |
| 56 | readx_poll_timeout(readb, addr, val, cond, timeout_us) |
| 57 | |
| 58 | #define readw_poll_timeout(addr, val, cond, timeout_us) \ |
| 59 | readx_poll_timeout(readw, addr, val, cond, timeout_us) |
| 60 | |
| 61 | #define readl_poll_timeout(addr, val, cond, timeout_us) \ |
| 62 | readx_poll_timeout(readl, addr, val, cond, timeout_us) |
| 63 | |
| 64 | #define readq_poll_timeout(addr, val, cond, timeout_us) \ |
| 65 | readx_poll_timeout(readq, addr, val, cond, timeout_us) |
| 66 | |
| 67 | #define readb_relaxed_poll_timeout(addr, val, cond, timeout_us) \ |
| 68 | readx_poll_timeout(readb_relaxed, addr, val, cond, timeout_us) |
| 69 | |
| 70 | #define readw_relaxed_poll_timeout(addr, val, cond, timeout_us) \ |
| 71 | readx_poll_timeout(readw_relaxed, addr, val, cond, timeout_us) |
| 72 | |
| 73 | #define readl_relaxed_poll_timeout(addr, val, cond, timeout_us) \ |
| 74 | readx_poll_timeout(readl_relaxed, addr, val, cond, timeout_us) |
| 75 | |
| 76 | #define readq_relaxed_poll_timeout(addr, val, cond, timeout_us) \ |
| 77 | readx_poll_timeout(readq_relaxed, addr, val, cond, timeout_us) |
| 78 | |
| 79 | #endif /* _LINUX_IOPOLL_H */ |