1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Wait for bit with timeout and ctrlc
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
20 * wait_for_bit_x() waits for bit set/cleared in register
22 * Function polls register waiting for specific bit(s) change
23 * (either 0->1 or 1->0). It can fail under two conditions:
25 * - User interaction (CTRL-C)
26 * Function succeeds only if all bits of masked register are set/cleared
27 * (depending on set option).
29 * @param reg Register that will be read (using read_x())
30 * @param mask Bit(s) of register that must be active
31 * @param set Selects wait condition (bit set or clear)
32 * @param timeout_ms Timeout (in milliseconds)
33 * @param breakable Enables CTRL-C interruption
34 * Return: 0 on success, -ETIMEDOUT or -EINTR on failure
37 #define BUILD_WAIT_FOR_BIT(sfx, type, read) \
39 static inline int wait_for_bit_##sfx(const void *reg, \
42 const unsigned int timeout_ms, \
43 const bool breakable) \
46 unsigned long start = get_timer(0); \
54 if ((val & mask) == mask) \
57 if (get_timer(start) > timeout_ms) \
60 if (breakable && ctrlc()) { \
69 debug("%s: Timeout (reg=%p mask=%x wait_set=%i)\n", __func__, \
75 BUILD_WAIT_FOR_BIT(8, u8, readb)
76 BUILD_WAIT_FOR_BIT(le16, u16, readw)
77 BUILD_WAIT_FOR_BIT(16, u16, readw)
79 BUILD_WAIT_FOR_BIT(be16, u16, readw_be)
81 BUILD_WAIT_FOR_BIT(le32, u32, readl)
82 BUILD_WAIT_FOR_BIT(32, u32, readl)
84 BUILD_WAIT_FOR_BIT(be32, u32, readl_be)