xref: /OK3568_Linux_fs/u-boot/include/time.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
3*4882a593Smuzhiyun  */
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #ifndef _TIME_H
6*4882a593Smuzhiyun #define _TIME_H
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/typecheck.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun unsigned long get_timer(unsigned long base);
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun /*
13*4882a593Smuzhiyun  * Return the current value of a monotonically increasing microsecond timer.
14*4882a593Smuzhiyun  * Granularity may be larger than 1us if hardware does not support this.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun unsigned long timer_get_us(void);
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  *	These inlines deal with timer wrapping correctly. You are
20*4882a593Smuzhiyun  *	strongly encouraged to use them
21*4882a593Smuzhiyun  *	1. Because people otherwise forget
22*4882a593Smuzhiyun  *	2. Because if the timer wrap changes in future you won't have to
23*4882a593Smuzhiyun  *	   alter your driver code.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * time_after(a,b) returns true if the time a is after time b.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * Do this with "<0" and ">=0" to only test the sign of the result. A
28*4882a593Smuzhiyun  * good compiler would generate better code (and a really good compiler
29*4882a593Smuzhiyun  * wouldn't care). Gcc is currently neither.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun #define time_after(a,b)		\
32*4882a593Smuzhiyun 	(typecheck(unsigned long, a) && \
33*4882a593Smuzhiyun 	 typecheck(unsigned long, b) && \
34*4882a593Smuzhiyun 	 ((long)((b) - (a)) < 0))
35*4882a593Smuzhiyun #define time_before(a,b)	time_after(b,a)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define time_after_eq(a,b)	\
38*4882a593Smuzhiyun 	(typecheck(unsigned long, a) && \
39*4882a593Smuzhiyun 	 typecheck(unsigned long, b) && \
40*4882a593Smuzhiyun 	 ((long)((a) - (b)) >= 0))
41*4882a593Smuzhiyun #define time_before_eq(a,b)	time_after_eq(b,a)
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun  * Calculate whether a is in the range of [b, c].
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun #define time_in_range(a,b,c) \
47*4882a593Smuzhiyun 	(time_after_eq(a,b) && \
48*4882a593Smuzhiyun 	 time_before_eq(a,c))
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * Calculate whether a is in the range of [b, c).
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun #define time_in_range_open(a,b,c) \
54*4882a593Smuzhiyun 	(time_after_eq(a,b) && \
55*4882a593Smuzhiyun 	 time_before(a,c))
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #endif /* _TIME_H */
58