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