1
2
3
4
5
6
7
8#ifndef __WAIT_BIT_H
9#define __WAIT_BIT_H
10
11#include <console.h>
12#include <log.h>
13#include <time.h>
14#include <watchdog.h>
15#include <linux/delay.h>
16#include <linux/errno.h>
17#include <asm/io.h>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 schedule(); \
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)
77BUILD_WAIT_FOR_BIT(16, u16, readw)
78#ifdef readw_be
79BUILD_WAIT_FOR_BIT(be16, u16, readw_be)
80#endif
81BUILD_WAIT_FOR_BIT(le32, u32, readl)
82BUILD_WAIT_FOR_BIT(32, u32, readl)
83#ifdef readl_be
84BUILD_WAIT_FOR_BIT(be32, u32, readl_be)
85#endif
86
87#endif
88