blob: f1d70aef873dd7afdc7d7d8323c87773733521dd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Mateusz Kulikowskicc42c6b2016-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 Kulikowskicc42c6b2016-01-23 11:54:29 +01006 */
7
8#ifndef __WAIT_BIT_H
9#define __WAIT_BIT_H
10
Mateusz Kulikowskicc42c6b2016-01-23 11:54:29 +010011#include <console.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
13#include <time.h>
Michal Simekd16a04b2016-12-12 09:46:09 +010014#include <watchdog.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Mateusz Kulikowskicc42c6b2016-01-23 11:54:29 +010017#include <asm/io.h>
18
19/**
Álvaro Fernández Rojas952831c2018-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 Schuchardt47b4c022022-01-19 18:05:50 +010034 * Return: 0 on success, -ETIMEDOUT or -EINTR on failure
Álvaro Fernández Rojas952831c2018-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); \
Stefan Roese80877fa2022-09-02 14:10:46 +020066 schedule(); \
Álvaro Fernández Rojas952831c2018-01-23 17:14:54 +010067 } \
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 Oney98d95962019-08-14 15:18:33 +020077BUILD_WAIT_FOR_BIT(16, u16, readw)
Álvaro Fernández Rojas952831c2018-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 Oney98d95962019-08-14 15:18:33 +020082BUILD_WAIT_FOR_BIT(32, u32, readl)
Álvaro Fernández Rojas952831c2018-01-23 17:14:54 +010083#ifdef readl_be
84BUILD_WAIT_FOR_BIT(be32, u32, readl_be)
85#endif
Mateusz Kulikowskicc42c6b2016-01-23 11:54:29 +010086
87#endif