1 /*
2 * =====================================================================================
3 *
4 * Filename: support.h
5 *
6 * Description: misc utilities definition.
7 *
8 * Version: 2.0
9 * Create: 2017-11-03 11:34:34
10 * Revision: none
11 * Compiler: gcc version 6.3.0 (crosstool-NG crosstool-ng-1.23.0)
12 *
13 * Author: caozilong@allwinnertech.com
14 * Organization: BU1-PSW
15 * Last Modified: 2020-03-25 12:20:13
16 *
17 * =====================================================================================
18 */
19
20 #ifndef __SUPPORT_H__
21 #define __SUPPORT_H__
22 #include <typedef.h>
23 #include <kapi.h>
24 /*
25 * Generic macro to convert pointers to values for comparison purposes.
26 */
27 #ifndef p2n
28 #define p2n(p) ((ptrdiff_t)((ptrdiff_t*)(p)))
29 #endif
30
31 /*
32 * min()/max() macros that also do
33 * strict type-checking.. See the
34 * "unnecessary" pointer comparison.
35 */
36 #ifndef min
37 #define min(x,y) ({ \
38 typeof(x) _x = (x); \
39 typeof(y) _y = (y); \
40 (void) (&_x == &_y); \
41 _x < _y ? _x : _y; })
42 #endif
43
44 #ifndef max
45 #define max(x,y) ({ \
46 typeof(x) _x = (x); \
47 typeof(y) _y = (y); \
48 (void) (&_x == &_y); \
49 _x > _y ? _x : _y; })
50 #endif
51
52 /*
53 * ..and if you can't take the strict
54 * types, you can specify one yourself.
55 *
56 * Or not use min/max at all, of course.
57 */
58 #define min_t(type,x,y) \
59 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
60 #define max_t(type,x,y) \
61 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
62
63
64 //#define BITS_PER_LONG 32
65 //#define BITS_PER_LONG_LONG 64
66
67 #ifndef ALIGN
68 #define ALIGN(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
69 #endif
70 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
71 #ifndef INT_MAX
72 #define INT_MAX ((int)(~0U>>1))
73 #endif
74 #ifndef INT_MIN
75 #define INT_MIN (-INT_MAX - 1)
76 #endif
77 #ifndef UINT_MAX
78 #define UINT_MAX (~0U)
79 #endif
80 #ifndef LONG_MAX
81 #define LONG_MAX ((long)(~0UL>>1))
82 #endif
83 #ifndef LONG_MIN
84 #define LONG_MIN (-LONG_MAX - 1)
85 #endif
86 #ifndef ULONG_MAX
87 #define ULONG_MAX (~0UL)
88 #endif
89 #ifndef LLONG_MAX
90 #define LLONG_MAX ((long long)(~0ULL>>1))
91 #endif
92 #ifndef LLONG_MIN
93 #define LLONG_MIN (-LLONG_MAX - 1)
94 #endif
95 #ifndef ULLONG_MAX
96 #define ULLONG_MAX (~0ULL)
97 #endif
98
99 #ifndef DATA_TYPE_X_BOOL
100 #define DATA_TYPE_X_BOOL
101 typedef enum
102 {
103 #ifndef FALSE
104 FALSE = 0,
105 #endif
106 #ifndef NO
107 NO = 0,
108 #endif
109 #ifndef ZERO
110 ZERO = 0,
111 #endif
112 #ifndef TRUE
113 TRUE = 1,
114 #endif
115 #ifndef YES
116 YES = 1,
117 #endif
118 #ifndef ONE
119 ONE = 1,
120 #endif
121 #ifndef OK
122 OK = 0,
123 #endif
124 #ifndef FAIL
125 FAIL = -1,
126 #endif
127 } BOOL;
128 #endif
129 /*
130 * Check at compile time that something is of a particular type.
131 * Always evaluates to 1 so you may use it easily in comparisons.
132 */
133 #define typecheck(type,x) \
134 ({ type __dummy; \
135 typeof(x) __dummy2; \
136 (void)(&__dummy == &__dummy2); \
137 1; \
138 })
139
is_power_of_2(unsigned long n)140 static inline int is_power_of_2(unsigned long n)
141 {
142 return (n != 0 && ((n & (n - 1)) == 0));
143 }
144
145 /* round "x" up/down to next multiple of "align" (which must be a power of 2) */
146 #define ROUND_UP(x, align) \
147 (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
148 ~((unsigned long)(align) - 1))
149 #define ROUND_DOWN(x, align) \
150 ((unsigned long)(x) & ~((unsigned long)(align) - 1))
151
152 //#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
153
154 /**
155 * test_bit - Determine whether a bit is set
156 * @nr: bit number to test
157 * @addr: Address to start counting from
158 */
test_bit(int nr,long * addr)159 static inline int test_bit(int nr, long *addr)
160 {
161 int mask;
162 addr += nr >> 5;
163 mask = 1 << (nr & 0x1f);
164 return ((mask & *addr) != 0);
165 }
166
167 /*
168 * These functions are the basis of our bit ops.
169 *
170 * First, the atomic bitops. These use native endian.
171 */
set_bit(unsigned int bit,volatile unsigned long * p)172 static inline void set_bit(unsigned int bit, volatile unsigned long *p)
173 {
174 unsigned long flags;
175 unsigned long mask = 1UL << (bit & 31);
176 p += bit >> 5;
177 ENTER_CRITICAL(flags);
178 *p |= mask;
179 EXIT_CRITICAL(flags);
180 }
181
clear_bit(unsigned int bit,volatile unsigned long * p)182 static inline void clear_bit(unsigned int bit, volatile unsigned long *p)
183 {
184 unsigned long flags;
185 unsigned long mask = 1UL << (bit & 31);
186 p += bit >> 5;
187 ENTER_CRITICAL(flags);
188 *p &= ~mask;
189 EXIT_CRITICAL(flags);
190 }
191
change_bit(unsigned int bit,volatile unsigned long * p)192 static inline void change_bit(unsigned int bit, volatile unsigned long *p)
193 {
194 unsigned long flags;
195 unsigned long mask = 1UL << (bit & 31);
196 p += bit >> 5;
197 ENTER_CRITICAL(flags);
198 *p ^= mask;
199 EXIT_CRITICAL(flags);
200 }
201
test_and_set_bit(unsigned int bit,volatile unsigned long * p)202 static inline int test_and_set_bit(unsigned int bit, volatile unsigned long *p)
203 {
204 unsigned long flags;
205 unsigned int res;
206 unsigned long mask = 1UL << (bit & 31);
207 p += bit >> 5;
208 ENTER_CRITICAL(flags);
209 res = *p;
210 *p = res | mask;
211 EXIT_CRITICAL(flags);
212 return res & mask;
213 }
214
test_and_clear_bit(unsigned int bit,volatile unsigned long * p)215 static inline int test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
216 {
217 unsigned long flags;
218 unsigned int res;
219 unsigned long mask = 1UL << (bit & 31);
220 p += bit >> 5;
221 ENTER_CRITICAL(flags);
222 res = *p;
223 *p = res & ~mask;
224 EXIT_CRITICAL(flags);
225 return res & mask;
226 }
227
test_and_change_bit(unsigned int bit,volatile unsigned long * p)228 static inline int test_and_change_bit(unsigned int bit, volatile unsigned long *p)
229 {
230 unsigned long flags;
231 unsigned int res;
232 unsigned long mask = 1UL << (bit & 31);
233 p += bit >> 5;
234 ENTER_CRITICAL(flags);
235 res = *p;
236 *p = res ^ mask;
237 EXIT_CRITICAL(flags);
238 return res & mask;
239 }
240
241 /* -------------------------------- jiffies -----------------------------*/
242 #define HZ 100
243 #define jiffies ((unsigned long)rt_tick_get())
244
245 /*
246 * These inlines deal with timer wrapping correctly. You are
247 * strongly encouraged to use them
248 * 1. Because people otherwise forget
249 * 2. Because if the timer wrap changes in future you won't have to
250 * alter your driver code.
251 *
252 * time_after(a,b) returns true if the time a is after time b.
253 *
254 * Do this with "<0" and ">=0" to only test the sign of the result. A
255 * good compiler would generate better code (and a really good compiler
256 * wouldn't care). Gcc is currently neither.
257 */
258 #define time_after(a,b) \
259 (typecheck(unsigned long, a) && \
260 typecheck(unsigned long, b) && \
261 ((int)(b) - (int)(a) < 0))
262 #define time_before(a,b) time_after(b,a)
263
264 #define time_after_eq(a,b) \
265 (typecheck(unsigned long, a) && \
266 typecheck(unsigned long, b) && \
267 ((int)(a) - (int)(b) >= 0))
268 #define time_before_eq(a,b) time_after_eq(b,a)
269
270 #endif /* __SUPPORT_H__ */
271