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