include: reset: Change to use CONFIG_IS_ENABLED(DM_RESET)
[oweals/u-boot.git] / include / time.h
index 5ed021fabb8716cc56a1d9eb0c537dfb9edb8130..825991e2223a0e5f8a782d97c4f9454e0048e40a 100644 (file)
@@ -1,10 +1,10 @@
-/*
- * SPDX-License-Identifier:    GPL-2.0+
- */
+/* SPDX-License-Identifier: GPL-2.0+ */
 
 #ifndef _TIME_H
 #define _TIME_H
 
+#include <linux/typecheck.h>
+
 unsigned long get_timer(unsigned long base);
 
 /*
@@ -13,4 +13,43 @@ unsigned long get_timer(unsigned long base);
  */
 unsigned long timer_get_us(void);
 
+/*
+ *     These inlines deal with timer wrapping correctly. You are
+ *     strongly encouraged to use them
+ *     1. Because people otherwise forget
+ *     2. Because if the timer wrap changes in future you won't have to
+ *        alter your driver code.
+ *
+ * time_after(a,b) returns true if the time a is after time b.
+ *
+ * Do this with "<0" and ">=0" to only test the sign of the result. A
+ * good compiler would generate better code (and a really good compiler
+ * wouldn't care). Gcc is currently neither.
+ */
+#define time_after(a,b)                \
+       (typecheck(unsigned long, a) && \
+        typecheck(unsigned long, b) && \
+        ((long)((b) - (a)) < 0))
+#define time_before(a,b)       time_after(b,a)
+
+#define time_after_eq(a,b)     \
+       (typecheck(unsigned long, a) && \
+        typecheck(unsigned long, b) && \
+        ((long)((a) - (b)) >= 0))
+#define time_before_eq(a,b)    time_after_eq(b,a)
+
+/*
+ * Calculate whether a is in the range of [b, c].
+ */
+#define time_in_range(a,b,c) \
+       (time_after_eq(a,b) && \
+        time_before_eq(a,c))
+
+/*
+ * Calculate whether a is in the range of [b, c).
+ */
+#define time_in_range_open(a,b,c) \
+       (time_after_eq(a,b) && \
+        time_before(a,c))
+
 #endif /* _TIME_H */