1 /*
2  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3  * Copyright (c) 2019, Linaro Limited
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef DELAY_TIMER_H
9 #define DELAY_TIMER_H
10 
11 #include <stdbool.h>
12 #include <stdint.h>
13 
14 #include <arch_helpers.h>
15 
16 /********************************************************************
17  * A simple timer driver providing synchronous delay functionality.
18  * The driver must be initialized with a structure that provides a
19  * function pointer to return the timer value and a clock
20  * multiplier/divider. The ratio of the multiplier and the divider is
21  * the clock period in microseconds.
22  ********************************************************************/
23 
24 typedef struct timer_ops {
25 	uint32_t (*get_timer_value)(void);
26 	uint32_t clk_mult;
27 	uint32_t clk_div;
28 } timer_ops_t;
29 
timeout_cnt_us2cnt(uint32_t us)30 static inline uint64_t timeout_cnt_us2cnt(uint32_t us)
31 {
32 	return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
33 }
34 
timeout_init_us(uint32_t us)35 static inline uint64_t timeout_init_us(uint32_t us)
36 {
37 	uint64_t cnt = timeout_cnt_us2cnt(us);
38 
39 	cnt += read_cntpct_el0();
40 
41 	return cnt;
42 }
43 
timeout_elapsed(uint64_t expire_cnt)44 static inline bool timeout_elapsed(uint64_t expire_cnt)
45 {
46 	return read_cntpct_el0() > expire_cnt;
47 }
48 
49 void mdelay(uint32_t msec);
50 void udelay(uint32_t usec);
51 void timer_init(const timer_ops_t *ops_ptr);
52 
53 #endif /* DELAY_TIMER_H */
54