blob: 3b2ba091247030d8a440c524441dfde060ad2580 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Masahiro Yamadaa7b81762016-12-28 00:36:00 +09002
3#ifndef _TIME_H
4#define _TIME_H
5
Masahiro Yamada21cdd132016-12-28 00:36:02 +09006#include <linux/typecheck.h>
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +02007#include <linux/types.h>
Masahiro Yamada21cdd132016-12-28 00:36:02 +09008
Simon Glass049f8d62019-12-28 10:44:59 -07009ulong get_tbclk(void);
10
Masahiro Yamadaa7b81762016-12-28 00:36:00 +090011unsigned 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 */
17unsigned long timer_get_us(void);
Marek Vasut80e7e7c2019-10-15 22:43:41 +020018uint64_t get_timer_us(uint64_t base);
Masahiro Yamadaa7b81762016-12-28 00:36:00 +090019
Simon Glasse1ddf672020-07-09 18:43:14 -060020/**
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 Schuchardt185f8122022-01-19 18:05:50 +010027 *Return: elapsed time since @base
Simon Glasse1ddf672020-07-09 18:43:14 -060028 */
29unsigned long get_timer_us_long(unsigned long base);
30
Masahiro Yamada21cdd132016-12-28 00:36:02 +090031/*
Neil Armstrongd0a9b822019-04-11 17:01:23 +020032 * 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 */
37void timer_test_add_offset(unsigned long offset);
38
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +020039/**
40 * usec_to_tick() - convert microseconds to clock ticks
41 *
42 * @usec: duration in microseconds
43 * Return: duration in clock ticks
44 */
45uint64_t usec_to_tick(unsigned long usec);
46
Neil Armstrongd0a9b822019-04-11 17:01:23 +020047/*
Masahiro Yamada21cdd132016-12-28 00:36:02 +090048 * These inlines deal with timer wrapping correctly. You are
49 * strongly encouraged to use them
50 * 1. Because people otherwise forget
51 * 2. Because if the timer wrap changes in future you won't have to
52 * alter your driver code.
53 *
54 * time_after(a,b) returns true if the time a is after time b.
55 *
56 * Do this with "<0" and ">=0" to only test the sign of the result. A
57 * good compiler would generate better code (and a really good compiler
58 * wouldn't care). Gcc is currently neither.
59 */
60#define time_after(a,b) \
61 (typecheck(unsigned long, a) && \
62 typecheck(unsigned long, b) && \
63 ((long)((b) - (a)) < 0))
64#define time_before(a,b) time_after(b,a)
65
66#define time_after_eq(a,b) \
67 (typecheck(unsigned long, a) && \
68 typecheck(unsigned long, b) && \
69 ((long)((a) - (b)) >= 0))
70#define time_before_eq(a,b) time_after_eq(b,a)
71
72/*
73 * Calculate whether a is in the range of [b, c].
74 */
75#define time_in_range(a,b,c) \
76 (time_after_eq(a,b) && \
77 time_before_eq(a,c))
78
79/*
80 * Calculate whether a is in the range of [b, c).
81 */
82#define time_in_range_open(a,b,c) \
83 (time_after_eq(a,b) && \
84 time_before(a,c))
85
Stefan Roesee29178e2022-09-02 13:57:47 +020086/* Same as above, but does so with platform independent 64bit types.
87 * These must be used when utilizing jiffies_64 (i.e. return value of
88 * get_jiffies_64() */
89#define time_after64(a,b) \
90 (typecheck(__u64, a) && \
91 typecheck(__u64, b) && \
92 ((__s64)((b) - (a)) < 0))
93#define time_before64(a,b) time_after64(b,a)
94
95#define time_after_eq64(a,b) \
96 (typecheck(__u64, a) && \
97 typecheck(__u64, b) && \
98 ((__s64)((a) - (b)) >= 0))
99#define time_before_eq64(a,b) time_after_eq64(b,a)
100
101#define time_in_range64(a, b, c) \
102 (time_after_eq64(a, b) && \
103 time_before_eq64(a, c))
104
Simon Glass6887c5b2019-11-14 12:57:26 -0700105/**
106 * usec2ticks() - Convert microseconds to internal ticks
107 *
108 * @usec: Value of microseconds to convert
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100109 * Return: Corresponding internal ticks value, calculated using get_tbclk()
Simon Glass6887c5b2019-11-14 12:57:26 -0700110 */
111ulong usec2ticks(unsigned long usec);
112
113/**
114 * ticks2usec() - Convert internal ticks to microseconds
115 *
116 * @ticks: Value of ticks to convert
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100117 * Return: Corresponding microseconds value, calculated using get_tbclk()
Simon Glass6887c5b2019-11-14 12:57:26 -0700118 */
119ulong ticks2usec(unsigned long ticks);
120
Simon Glass036a0172019-11-14 12:57:27 -0700121/**
122 * wait_ticks() - waits a given number of ticks
123 *
124 * This is an internal function typically used to implement udelay() and
125 * similar. Normally you should use udelay() or mdelay() instead.
126 *
127 * @ticks: Number of ticks to wait
128 */
129void wait_ticks(unsigned long ticks);
130
Simon Glassf0143a82019-11-14 12:57:29 -0700131/**
132 * timer_get_us() - Get monotonic microsecond timer
133 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100134 * Return: value of monotonic microsecond timer
Simon Glassf0143a82019-11-14 12:57:29 -0700135 */
136unsigned long timer_get_us(void);
137
Simon Glass10453152019-11-14 12:57:30 -0700138/**
139 * get_ticks() - Get the current tick value
140 *
141 * This is an internal value used by the timer on the system. Ticks increase
142 * monotonically at the rate given by get_tbclk().
143 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100144 * Return: current tick value
Simon Glass10453152019-11-14 12:57:30 -0700145 */
146uint64_t get_ticks(void);
147
Masahiro Yamadaa7b81762016-12-28 00:36:00 +0900148#endif /* _TIME_H */