2018-05-06 21:58:06 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2016-01-23 10:54:29 +00:00
|
|
|
/*
|
|
|
|
* Wait for bit with timeout and ctrlc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __WAIT_BIT_H
|
|
|
|
#define __WAIT_BIT_H
|
|
|
|
|
|
|
|
#include <console.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
|
|
|
#include <time.h>
|
2016-12-12 08:46:09 +00:00
|
|
|
#include <watchdog.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <linux/delay.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2016-01-23 10:54:29 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
2018-01-23 16:14:54 +00:00
|
|
|
/**
|
|
|
|
* wait_for_bit_x() waits for bit set/cleared in register
|
|
|
|
*
|
|
|
|
* Function polls register waiting for specific bit(s) change
|
|
|
|
* (either 0->1 or 1->0). It can fail under two conditions:
|
|
|
|
* - Timeout
|
|
|
|
* - User interaction (CTRL-C)
|
|
|
|
* Function succeeds only if all bits of masked register are set/cleared
|
|
|
|
* (depending on set option).
|
|
|
|
*
|
|
|
|
* @param reg Register that will be read (using read_x())
|
|
|
|
* @param mask Bit(s) of register that must be active
|
|
|
|
* @param set Selects wait condition (bit set or clear)
|
|
|
|
* @param timeout_ms Timeout (in milliseconds)
|
|
|
|
* @param breakable Enables CTRL-C interruption
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 on success, -ETIMEDOUT or -EINTR on failure
|
2018-01-23 16:14:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define BUILD_WAIT_FOR_BIT(sfx, type, read) \
|
|
|
|
\
|
|
|
|
static inline int wait_for_bit_##sfx(const void *reg, \
|
|
|
|
const type mask, \
|
|
|
|
const bool set, \
|
|
|
|
const unsigned int timeout_ms, \
|
|
|
|
const bool breakable) \
|
|
|
|
{ \
|
|
|
|
type val; \
|
|
|
|
unsigned long start = get_timer(0); \
|
|
|
|
\
|
|
|
|
while (1) { \
|
|
|
|
val = read(reg); \
|
|
|
|
\
|
|
|
|
if (!set) \
|
|
|
|
val = ~val; \
|
|
|
|
\
|
|
|
|
if ((val & mask) == mask) \
|
|
|
|
return 0; \
|
|
|
|
\
|
|
|
|
if (get_timer(start) > timeout_ms) \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
if (breakable && ctrlc()) { \
|
|
|
|
puts("Abort\n"); \
|
|
|
|
return -EINTR; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
udelay(1); \
|
2022-09-02 12:10:46 +00:00
|
|
|
schedule(); \
|
2018-01-23 16:14:54 +00:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
debug("%s: Timeout (reg=%p mask=%x wait_set=%i)\n", __func__, \
|
|
|
|
reg, mask, set); \
|
|
|
|
\
|
|
|
|
return -ETIMEDOUT; \
|
|
|
|
}
|
|
|
|
|
|
|
|
BUILD_WAIT_FOR_BIT(8, u8, readb)
|
|
|
|
BUILD_WAIT_FOR_BIT(le16, u16, readw)
|
2019-08-14 13:18:33 +00:00
|
|
|
BUILD_WAIT_FOR_BIT(16, u16, readw)
|
2018-01-23 16:14:54 +00:00
|
|
|
#ifdef readw_be
|
|
|
|
BUILD_WAIT_FOR_BIT(be16, u16, readw_be)
|
|
|
|
#endif
|
|
|
|
BUILD_WAIT_FOR_BIT(le32, u32, readl)
|
2019-08-14 13:18:33 +00:00
|
|
|
BUILD_WAIT_FOR_BIT(32, u32, readl)
|
2018-01-23 16:14:54 +00:00
|
|
|
#ifdef readl_be
|
|
|
|
BUILD_WAIT_FOR_BIT(be32, u32, readl_be)
|
|
|
|
#endif
|
2016-01-23 10:54:29 +00:00
|
|
|
|
|
|
|
#endif
|