delay_timer.h 1.31 KB
Newer Older
1
/*
2
3
 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
 * Copyright (c) 2019, Linaro Limited
4
 *
dp-arm's avatar
dp-arm committed
5
 * SPDX-License-Identifier: BSD-3-Clause
6
7
 */

8
9
#ifndef DELAY_TIMER_H
#define DELAY_TIMER_H
10

11
#include <stdbool.h>
12
13
#include <stdint.h>

14
15
#include <arch_helpers.h>

16
17
18
19
20
/********************************************************************
 * A simple timer driver providing synchronous delay functionality.
 * The driver must be initialized with a structure that provides a
 * function pointer to return the timer value and a clock
 * multiplier/divider. The ratio of the multiplier and the divider is
Juan Castillo's avatar
Juan Castillo committed
21
 * the clock period in microseconds.
22
23
24
25
26
27
28
29
 ********************************************************************/

typedef struct timer_ops {
	uint32_t (*get_timer_value)(void);
	uint32_t clk_mult;
	uint32_t clk_div;
} timer_ops_t;

30
31
32
33
34
35
36
37
38
static inline uint64_t timeout_cnt_us2cnt(uint32_t us)
{
	return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
}

static inline uint64_t timeout_init_us(uint32_t us)
{
	uint64_t cnt = timeout_cnt_us2cnt(us);

39
	cnt += read_cntpct_el0();
40
41
42
43
44
45
46
47
48

	return cnt;
}

static inline bool timeout_elapsed(uint64_t expire_cnt)
{
	return read_cntpct_el0() > expire_cnt;
}

49
50
void mdelay(uint32_t msec);
void udelay(uint32_t usec);
51
void timer_init(const timer_ops_t *ops_ptr);
52

53
#endif /* DELAY_TIMER_H */