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