blob: e99f9c8012701e7f0b62a37c0c71c12d2f4b8f87 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Masahiro Yamadaa4f0b8e2016-12-28 00:36:00 +09002
3#ifndef _TIME_H
4#define _TIME_H
5
Masahiro Yamada267cab62016-12-28 00:36:02 +09006#include <linux/typecheck.h>
Heinrich Schuchardtdaf69fc2019-06-02 21:02:10 +02007#include <linux/types.h>
Masahiro Yamada267cab62016-12-28 00:36:02 +09008
Simon Glassa9dc0682019-12-28 10:44:59 -07009ulong get_tbclk(void);
10
Masahiro Yamadaa4f0b8e2016-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 Vasutebc50332019-10-15 22:43:41 +020018uint64_t get_timer_us(uint64_t base);
Masahiro Yamadaa4f0b8e2016-12-28 00:36:00 +090019
Masahiro Yamada267cab62016-12-28 00:36:02 +090020/*
Neil Armstrong77c0dbc2019-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 Schuchardtdaf69fc2019-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 Armstrong77c0dbc2019-04-11 17:01:23 +020036/*
Masahiro Yamada267cab62016-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 Glass45c78902019-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 Glass77f80e62019-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 Glass2e3b8a22019-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 Glass495a5dc2019-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 Yamadaa4f0b8e2016-12-28 00:36:00 +0900118#endif /* _TIME_H */