xref: /rk3399_ARM-atf/include/lib/mmio_poll.h (revision c97b2c8081a04a421624a3a42cd0f798a9e9a0fa)
1 /*
2  * Copyright 2025 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef MMIO_POLL_H
8 #define MMIO_POLL_H
9 
10 #include <errno.h>
11 #include <drivers/delay_timer.h>
12 #include <lib/mmio.h>
13 
14 /**
15  * mmio_read_poll_timeout - Continuously check an address until a specific
16  * condition is satisfied or a timeout is reached.
17  * @op: The mmio_read_* operator to read the register
18  * @val: The variable where the read value is stored.
19  * @cond: The condition used to stop polling, which can be a macro using @val.
20  * @timeout_us: Timeout in microseconds.
21  * @args: Arguments to be passed to @op.
22  *
23  * Return: 0 if the condition @cond is evaluated to true within @timeout_us
24  *         microseconds, or -ETIMEOUT in case of a timeout. In either case,
25  *         the last read value will be stored in @val.
26  */
27 #define mmio_read_poll_timeout(op, val, cond, timeout_us, args...)\
28 ({\
29 	int _rv = -ETIMEDOUT; \
30 	uint32_t _tout_us = (timeout_us); \
31 	uint64_t _tout = timeout_init_us(_tout_us);\
32 	do {\
33 		(val) = (op)(args);\
34 		if (cond) { \
35 			_rv = 0;\
36 			break;\
37 		} \
38 	} while (!timeout_elapsed(_tout));\
39 	_rv;\
40 })
41 
42 #define mmio_read_32_poll_timeout(addr, val, cond, timeout_us) \
43 	mmio_read_poll_timeout(&mmio_read_32, val, cond, timeout_us, addr)
44 
45 #endif /* MMIO_POLL_H */
46