blob: e99f9c8012701e7f0b62a37c0c71c12d2f4b8f87 [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
Masahiro Yamada21cdd132016-12-28 00:36:02 +090020/*
Neil Armstrongd0a9b822019-04-11 17:01:23 +020021 * 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 */
26void timer_test_add_offset(unsigned long offset);
27
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +020028/**
29 * usec_to_tick() - convert microseconds to clock ticks
30 *
31 * @usec: duration in microseconds
32 * Return: duration in clock ticks
33 */
34uint64_t usec_to_tick(unsigned long usec);
35
Neil Armstrongd0a9b822019-04-11 17:01:23 +020036/*
Masahiro Yamada21cdd132016-12-28 00:36:02 +090037 * 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 Glass6887c5b2019-11-14 12:57:26 -070075/**
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 */
81ulong 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 */
89ulong ticks2usec(unsigned long ticks);
90
Simon Glass036a0172019-11-14 12:57:27 -070091/**
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 */
99void wait_ticks(unsigned long ticks);
100
Simon Glassf0143a82019-11-14 12:57:29 -0700101/**
102 * timer_get_us() - Get monotonic microsecond timer
103 *
104 * @return value of monotonic microsecond timer
105 */
106unsigned long timer_get_us(void);
107
Simon Glass10453152019-11-14 12:57:30 -0700108/**
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 */
116uint64_t get_ticks(void);
117
Masahiro Yamadaa7b81762016-12-28 00:36:00 +0900118#endif /* _TIME_H */