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 | |
Masahiro Yamada | 267cab6 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 20 | /* |
Neil Armstrong | 77c0dbc | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 21 | * timer_test_add_offset() |
| 22 | * |
| 23 | * Allow tests to add to the time reported through lib/time.c functions |
| 24 | * offset: number of milliseconds to advance the system time |
| 25 | */ |
| 26 | void timer_test_add_offset(unsigned long offset); |
| 27 | |
Heinrich Schuchardt | daf69fc | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 28 | /** |
| 29 | * usec_to_tick() - convert microseconds to clock ticks |
| 30 | * |
| 31 | * @usec: duration in microseconds |
| 32 | * Return: duration in clock ticks |
| 33 | */ |
| 34 | uint64_t usec_to_tick(unsigned long usec); |
| 35 | |
Neil Armstrong | 77c0dbc | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 36 | /* |
Masahiro Yamada | 267cab6 | 2016-12-28 00:36:02 +0900 | [diff] [blame] | 37 | * These inlines deal with timer wrapping correctly. You are |
| 38 | * strongly encouraged to use them |
| 39 | * 1. Because people otherwise forget |
| 40 | * 2. Because if the timer wrap changes in future you won't have to |
| 41 | * alter your driver code. |
| 42 | * |
| 43 | * time_after(a,b) returns true if the time a is after time b. |
| 44 | * |
| 45 | * Do this with "<0" and ">=0" to only test the sign of the result. A |
| 46 | * good compiler would generate better code (and a really good compiler |
| 47 | * wouldn't care). Gcc is currently neither. |
| 48 | */ |
| 49 | #define time_after(a,b) \ |
| 50 | (typecheck(unsigned long, a) && \ |
| 51 | typecheck(unsigned long, b) && \ |
| 52 | ((long)((b) - (a)) < 0)) |
| 53 | #define time_before(a,b) time_after(b,a) |
| 54 | |
| 55 | #define time_after_eq(a,b) \ |
| 56 | (typecheck(unsigned long, a) && \ |
| 57 | typecheck(unsigned long, b) && \ |
| 58 | ((long)((a) - (b)) >= 0)) |
| 59 | #define time_before_eq(a,b) time_after_eq(b,a) |
| 60 | |
| 61 | /* |
| 62 | * Calculate whether a is in the range of [b, c]. |
| 63 | */ |
| 64 | #define time_in_range(a,b,c) \ |
| 65 | (time_after_eq(a,b) && \ |
| 66 | time_before_eq(a,c)) |
| 67 | |
| 68 | /* |
| 69 | * Calculate whether a is in the range of [b, c). |
| 70 | */ |
| 71 | #define time_in_range_open(a,b,c) \ |
| 72 | (time_after_eq(a,b) && \ |
| 73 | time_before(a,c)) |
| 74 | |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 75 | /** |
| 76 | * usec2ticks() - Convert microseconds to internal ticks |
| 77 | * |
| 78 | * @usec: Value of microseconds to convert |
| 79 | * @return Corresponding internal ticks value, calculated using get_tbclk() |
| 80 | */ |
| 81 | ulong usec2ticks(unsigned long usec); |
| 82 | |
| 83 | /** |
| 84 | * ticks2usec() - Convert internal ticks to microseconds |
| 85 | * |
| 86 | * @ticks: Value of ticks to convert |
| 87 | * @return Corresponding microseconds value, calculated using get_tbclk() |
| 88 | */ |
| 89 | ulong ticks2usec(unsigned long ticks); |
| 90 | |
Simon Glass | 77f80e6 | 2019-11-14 12:57:27 -0700 | [diff] [blame] | 91 | /** |
| 92 | * wait_ticks() - waits a given number of ticks |
| 93 | * |
| 94 | * This is an internal function typically used to implement udelay() and |
| 95 | * similar. Normally you should use udelay() or mdelay() instead. |
| 96 | * |
| 97 | * @ticks: Number of ticks to wait |
| 98 | */ |
| 99 | void wait_ticks(unsigned long ticks); |
| 100 | |
Simon Glass | 2e3b8a2 | 2019-11-14 12:57:29 -0700 | [diff] [blame] | 101 | /** |
| 102 | * timer_get_us() - Get monotonic microsecond timer |
| 103 | * |
| 104 | * @return value of monotonic microsecond timer |
| 105 | */ |
| 106 | unsigned long timer_get_us(void); |
| 107 | |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 108 | /** |
| 109 | * get_ticks() - Get the current tick value |
| 110 | * |
| 111 | * This is an internal value used by the timer on the system. Ticks increase |
| 112 | * monotonically at the rate given by get_tbclk(). |
| 113 | * |
| 114 | * @return current tick value |
| 115 | */ |
| 116 | uint64_t get_ticks(void); |
| 117 | |
Masahiro Yamada | a4f0b8e | 2016-12-28 00:36:00 +0900 | [diff] [blame] | 118 | #endif /* _TIME_H */ |