xref: /rk3399_ARM-atf/include/drivers/delay_timer.h (revision 0711ee5cbc5645b55de1a751bd52dc8ce02ae037)
19055c7d1SRyan Harkin /*
2*0711ee5cSLionel Debieve  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3*0711ee5cSLionel Debieve  * Copyright (c) 2019, Linaro Limited
49055c7d1SRyan Harkin  *
582cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
69055c7d1SRyan Harkin  */
79055c7d1SRyan Harkin 
8c3cf06f1SAntonio Nino Diaz #ifndef DELAY_TIMER_H
9c3cf06f1SAntonio Nino Diaz #define DELAY_TIMER_H
109055c7d1SRyan Harkin 
11*0711ee5cSLionel Debieve #include <stdbool.h>
129055c7d1SRyan Harkin #include <stdint.h>
139055c7d1SRyan Harkin 
14*0711ee5cSLionel Debieve #include <arch_helpers.h>
15*0711ee5cSLionel Debieve 
169055c7d1SRyan Harkin /********************************************************************
179055c7d1SRyan Harkin  * A simple timer driver providing synchronous delay functionality.
189055c7d1SRyan Harkin  * The driver must be initialized with a structure that provides a
199055c7d1SRyan Harkin  * function pointer to return the timer value and a clock
209055c7d1SRyan Harkin  * multiplier/divider. The ratio of the multiplier and the divider is
21540a5ba8SJuan Castillo  * the clock period in microseconds.
229055c7d1SRyan Harkin  ********************************************************************/
239055c7d1SRyan Harkin 
249055c7d1SRyan Harkin typedef struct timer_ops {
259055c7d1SRyan Harkin 	uint32_t (*get_timer_value)(void);
269055c7d1SRyan Harkin 	uint32_t clk_mult;
279055c7d1SRyan Harkin 	uint32_t clk_div;
289055c7d1SRyan Harkin } timer_ops_t;
299055c7d1SRyan Harkin 
30*0711ee5cSLionel Debieve static inline uint64_t timeout_cnt_us2cnt(uint32_t us)
31*0711ee5cSLionel Debieve {
32*0711ee5cSLionel Debieve 	return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
33*0711ee5cSLionel Debieve }
34*0711ee5cSLionel Debieve 
35*0711ee5cSLionel Debieve static inline uint64_t timeout_init_us(uint32_t us)
36*0711ee5cSLionel Debieve {
37*0711ee5cSLionel Debieve 	uint64_t cnt = timeout_cnt_us2cnt(us);
38*0711ee5cSLionel Debieve 
39*0711ee5cSLionel Debieve 	cnt += read_cntfrq_el0();
40*0711ee5cSLionel Debieve 
41*0711ee5cSLionel Debieve 	return cnt;
42*0711ee5cSLionel Debieve }
43*0711ee5cSLionel Debieve 
44*0711ee5cSLionel Debieve static inline bool timeout_elapsed(uint64_t expire_cnt)
45*0711ee5cSLionel Debieve {
46*0711ee5cSLionel Debieve 	return read_cntpct_el0() > expire_cnt;
47*0711ee5cSLionel Debieve }
48*0711ee5cSLionel Debieve 
499055c7d1SRyan Harkin void mdelay(uint32_t msec);
509055c7d1SRyan Harkin void udelay(uint32_t usec);
519fb8af33SRoberto Vargas void timer_init(const timer_ops_t *ops_ptr);
529055c7d1SRyan Harkin 
53c3cf06f1SAntonio Nino Diaz #endif /* DELAY_TIMER_H */
54