blob: dcc5c4fd396cac24de9d07eb4c47d250ac3bf1d9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Mateusz Kulikowski9afdf402016-01-23 11:54:29 +01002/*
3 * Wait for bit with timeout and ctrlc
4 *
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
Mateusz Kulikowski9afdf402016-01-23 11:54:29 +01006 */
7
8#ifndef __WAIT_BIT_H
9#define __WAIT_BIT_H
10
Mateusz Kulikowski9afdf402016-01-23 11:54:29 +010011#include <console.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
13#include <time.h>
Michal Simek3cd42182016-12-12 09:46:09 +010014#include <watchdog.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <linux/delay.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090016#include <linux/errno.h>
Mateusz Kulikowski9afdf402016-01-23 11:54:29 +010017#include <asm/io.h>
18
19/**
Álvaro Fernández Rojas91fe4582018-01-23 17:14:54 +010020 * wait_for_bit_x() waits for bit set/cleared in register
21 *
22 * Function polls register waiting for specific bit(s) change
23 * (either 0->1 or 1->0). It can fail under two conditions:
24 * - Timeout
25 * - User interaction (CTRL-C)
26 * Function succeeds only if all bits of masked register are set/cleared
27 * (depending on set option).
28 *
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
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010034 * Return: 0 on success, -ETIMEDOUT or -EINTR on failure
Álvaro Fernández Rojas91fe4582018-01-23 17:14:54 +010035 */
36
37#define BUILD_WAIT_FOR_BIT(sfx, type, read) \
38 \
39static inline int wait_for_bit_##sfx(const void *reg, \
40 const type mask, \
41 const bool set, \
42 const unsigned int timeout_ms, \
43 const bool breakable) \
44{ \
45 type val; \
46 unsigned long start = get_timer(0); \
47 \
48 while (1) { \
49 val = read(reg); \
50 \
51 if (!set) \
52 val = ~val; \
53 \
54 if ((val & mask) == mask) \
55 return 0; \
56 \
57 if (get_timer(start) > timeout_ms) \
58 break; \
59 \
60 if (breakable && ctrlc()) { \
61 puts("Abort\n"); \
62 return -EINTR; \
63 } \
64 \
65 udelay(1); \
66 WATCHDOG_RESET(); \
67 } \
68 \
69 debug("%s: Timeout (reg=%p mask=%x wait_set=%i)\n", __func__, \
70 reg, mask, set); \
71 \
72 return -ETIMEDOUT; \
73}
74
75BUILD_WAIT_FOR_BIT(8, u8, readb)
76BUILD_WAIT_FOR_BIT(le16, u16, readw)
Kursad Oneyc50d6702019-08-14 15:18:33 +020077BUILD_WAIT_FOR_BIT(16, u16, readw)
Álvaro Fernández Rojas91fe4582018-01-23 17:14:54 +010078#ifdef readw_be
79BUILD_WAIT_FOR_BIT(be16, u16, readw_be)
80#endif
81BUILD_WAIT_FOR_BIT(le32, u32, readl)
Kursad Oneyc50d6702019-08-14 15:18:33 +020082BUILD_WAIT_FOR_BIT(32, u32, readl)
Álvaro Fernández Rojas91fe4582018-01-23 17:14:54 +010083#ifdef readl_be
84BUILD_WAIT_FOR_BIT(be32, u32, readl_be)
85#endif
Mateusz Kulikowski9afdf402016-01-23 11:54:29 +010086
87#endif