Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Masahiro Yamada | a4f0b8e | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 2 | |
| 3 | #ifndef _TIME_H |
| 4 | #define _TIME_H |
| 5 | |
Masahiro Yamada | 267cab6 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 6 | #include <linux/typecheck.h> |
Heinrich Schuchardt | daf69fc | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 7 | #include <linux/types.h> |
Masahiro Yamada | 267cab6 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 8 | |
Simon Glass | a9dc068 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 9 | ulong get_tbclk(void); |
| 10 | |
Masahiro Yamada | a4f0b8e | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 11 | unsigned long get_timer(unsigned long base); |
| 12 | |
| 13 | /* |
| 14 | * Return the current value of a monotonically increasing microsecond timer. |
| 15 | * Granularity may be larger than 1us if hardware does not support this. |
| 16 | */ |
| 17 | unsigned long timer_get_us(void); |
Marek Vasut | ebc5033 | 2019-10-15 22:43:41 +0200 | [diff] [blame] | 18 | uint64_t get_timer_us(uint64_t base); |
Masahiro Yamada | a4f0b8e | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 19 | |
Simon Glass | 82213d8 | 2020-07-09 18:43:14 -0600 | [diff] [blame] | 20 | /** |
| 21 | * get_timer_us_long() - Get the number of elapsed microseconds |
| 22 | * |
| 23 | * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle |
| 24 | * delays of over an hour. For 64-bit machines it uses a 64-bit value. |
| 25 | * |
| 26 | *@base: Base time to consider |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 27 | *Return: elapsed time since @base |
Simon Glass | 82213d8 | 2020-07-09 18:43:14 -0600 | [diff] [blame] | 28 | */ |
| 29 | unsigned long get_timer_us_long(unsigned long base); |
| 30 | |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame^] | 31 | /** |
Neil Armstrong | 77c0dbc | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 32 | * timer_test_add_offset() |
| 33 | * |
| 34 | * Allow tests to add to the time reported through lib/time.c functions |
| 35 | * offset: number of milliseconds to advance the system time |
| 36 | */ |
| 37 | void timer_test_add_offset(unsigned long offset); |
| 38 | |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame^] | 39 | #ifdef CONFIG_SANDBOX |
| 40 | /** |
| 41 | * timer_test_get_offset() |
| 42 | * |
| 43 | * Get the total offset currently being added the time |
| 44 | * |
| 45 | * Return:: number of milliseconds the system time has been advanced |
| 46 | */ |
| 47 | ulong timer_test_get_offset(void); |
| 48 | #else |
| 49 | static inline ulong timer_test_get_offset(void) { return 0; } |
| 50 | #endif |
| 51 | |
Heinrich Schuchardt | daf69fc | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 52 | /** |
| 53 | * usec_to_tick() - convert microseconds to clock ticks |
| 54 | * |
| 55 | * @usec: duration in microseconds |
| 56 | * Return: duration in clock ticks |
| 57 | */ |
| 58 | uint64_t usec_to_tick(unsigned long usec); |
| 59 | |
Neil Armstrong | 77c0dbc | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 60 | /* |
Masahiro Yamada | 267cab6 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 61 | * These inlines deal with timer wrapping correctly. You are |
| 62 | * strongly encouraged to use them |
| 63 | * 1. Because people otherwise forget |
| 64 | * 2. Because if the timer wrap changes in future you won't have to |
| 65 | * alter your driver code. |
| 66 | * |
| 67 | * time_after(a,b) returns true if the time a is after time b. |
| 68 | * |
| 69 | * Do this with "<0" and ">=0" to only test the sign of the result. A |
| 70 | * good compiler would generate better code (and a really good compiler |
| 71 | * wouldn't care). Gcc is currently neither. |
| 72 | */ |
| 73 | #define time_after(a,b) \ |
| 74 | (typecheck(unsigned long, a) && \ |
| 75 | typecheck(unsigned long, b) && \ |
| 76 | ((long)((b) - (a)) < 0)) |
| 77 | #define time_before(a,b) time_after(b,a) |
| 78 | |
| 79 | #define time_after_eq(a,b) \ |
| 80 | (typecheck(unsigned long, a) && \ |
| 81 | typecheck(unsigned long, b) && \ |
| 82 | ((long)((a) - (b)) >= 0)) |
| 83 | #define time_before_eq(a,b) time_after_eq(b,a) |
| 84 | |
| 85 | /* |
| 86 | * Calculate whether a is in the range of [b, c]. |
| 87 | */ |
| 88 | #define time_in_range(a,b,c) \ |
| 89 | (time_after_eq(a,b) && \ |
| 90 | time_before_eq(a,c)) |
| 91 | |
| 92 | /* |
| 93 | * Calculate whether a is in the range of [b, c). |
| 94 | */ |
| 95 | #define time_in_range_open(a,b,c) \ |
| 96 | (time_after_eq(a,b) && \ |
| 97 | time_before(a,c)) |
| 98 | |
Stefan Roese | 44a99a9 | 2022-09-02 13:57:47 +0200 | [diff] [blame] | 99 | /* Same as above, but does so with platform independent 64bit types. |
| 100 | * These must be used when utilizing jiffies_64 (i.e. return value of |
| 101 | * get_jiffies_64() */ |
| 102 | #define time_after64(a,b) \ |
| 103 | (typecheck(__u64, a) && \ |
| 104 | typecheck(__u64, b) && \ |
| 105 | ((__s64)((b) - (a)) < 0)) |
| 106 | #define time_before64(a,b) time_after64(b,a) |
| 107 | |
| 108 | #define time_after_eq64(a,b) \ |
| 109 | (typecheck(__u64, a) && \ |
| 110 | typecheck(__u64, b) && \ |
| 111 | ((__s64)((a) - (b)) >= 0)) |
| 112 | #define time_before_eq64(a,b) time_after_eq64(b,a) |
| 113 | |
| 114 | #define time_in_range64(a, b, c) \ |
| 115 | (time_after_eq64(a, b) && \ |
| 116 | time_before_eq64(a, c)) |
| 117 | |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 118 | /** |
| 119 | * usec2ticks() - Convert microseconds to internal ticks |
| 120 | * |
| 121 | * @usec: Value of microseconds to convert |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 122 | * Return: Corresponding internal ticks value, calculated using get_tbclk() |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 123 | */ |
| 124 | ulong usec2ticks(unsigned long usec); |
| 125 | |
| 126 | /** |
| 127 | * ticks2usec() - Convert internal ticks to microseconds |
| 128 | * |
| 129 | * @ticks: Value of ticks to convert |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 130 | * Return: Corresponding microseconds value, calculated using get_tbclk() |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 131 | */ |
| 132 | ulong ticks2usec(unsigned long ticks); |
| 133 | |
Simon Glass | 77f80e6 | 2019-11-14 12:57:27 -0700 | [diff] [blame] | 134 | /** |
| 135 | * wait_ticks() - waits a given number of ticks |
| 136 | * |
| 137 | * This is an internal function typically used to implement udelay() and |
| 138 | * similar. Normally you should use udelay() or mdelay() instead. |
| 139 | * |
| 140 | * @ticks: Number of ticks to wait |
| 141 | */ |
| 142 | void wait_ticks(unsigned long ticks); |
| 143 | |
Simon Glass | 2e3b8a2 | 2019-11-14 12:57:29 -0700 | [diff] [blame] | 144 | /** |
| 145 | * timer_get_us() - Get monotonic microsecond timer |
| 146 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 147 | * Return: value of monotonic microsecond timer |
Simon Glass | 2e3b8a2 | 2019-11-14 12:57:29 -0700 | [diff] [blame] | 148 | */ |
| 149 | unsigned long timer_get_us(void); |
| 150 | |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 151 | /** |
| 152 | * get_ticks() - Get the current tick value |
| 153 | * |
| 154 | * This is an internal value used by the timer on the system. Ticks increase |
| 155 | * monotonically at the rate given by get_tbclk(). |
| 156 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 157 | * Return: current tick value |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 158 | */ |
| 159 | uint64_t get_ticks(void); |
| 160 | |
Masahiro Yamada | a4f0b8e | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 161 | #endif /* _TIME_H */ |