refactor(delay-timer): add timer callback functions

In order to avoid separate platform definitions when not using the
default timer functions, it is better to move these functions out of the
header file and into the source files, so that they can be built if
needed.

Move timer functions from delay_timer.h into generic_delay_timer.c. Add
them as callback functions which are then called in delay_timer.c.

Change-Id: I96a1eac8948b1a7b1e481899b67a083db4c9b97d
Signed-off-by: Abhi Singh <abhi.singh@arm.com>
This commit is contained in:
Abhi.Singh 2024-08-21 12:55:38 -05:00 committed by Abhi Singh
parent 63912657b9
commit a6485b2b3b
4 changed files with 55 additions and 25 deletions

View file

@ -900,6 +900,9 @@ subsections:
- title: Console
scope: console
- title: Delay Timer
scope: delay-timer
- title: Generic Clock
scope: clk

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
* Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
@ -80,3 +80,25 @@ void timer_init(const timer_ops_t *ops_ptr)
timer_ops = ops_ptr;
}
/***********************************************************
* Initialize the timer in us
***********************************************************/
uint64_t timeout_init_us(uint32_t usec)
{
assert(timer_ops != NULL);
assert(timer_ops->timeout_init_us != NULL);
return timer_ops->timeout_init_us(usec);
}
/***********************************************************
* check the given timeout elapsed or not.
***********************************************************/
bool timeout_elapsed(uint64_t cnt)
{
assert(timer_ops != NULL);
assert(timer_ops->timeout_elapsed != NULL);
return timer_ops->timeout_elapsed(cnt);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
* Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
* Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
@ -18,7 +18,26 @@
static timer_ops_t ops;
static uint32_t get_timer_value(void)
static uint64_t timeout_cnt_us2cnt(uint32_t us)
{
return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
}
static uint64_t generic_delay_timeout_init_us(uint32_t us)
{
uint64_t cnt = timeout_cnt_us2cnt(us);
cnt += read_cntpct_el0();
return cnt;
}
static bool generic_delay_timeout_elapsed(uint64_t expire_cnt)
{
return read_cntpct_el0() > expire_cnt;
}
static uint32_t generic_delay_get_timer_value(void)
{
/*
* Generic delay timer implementation expects the timer to be a down
@ -31,9 +50,11 @@ static uint32_t get_timer_value(void)
void generic_delay_timer_init_args(uint32_t mult, uint32_t div)
{
ops.get_timer_value = get_timer_value;
ops.get_timer_value = generic_delay_get_timer_value;
ops.clk_mult = mult;
ops.clk_div = div;
ops.timeout_init_us = generic_delay_timeout_init_us;
ops.timeout_elapsed = generic_delay_timeout_elapsed;
timer_init(&ops);
@ -59,4 +80,3 @@ void generic_delay_timer_init(void)
generic_delay_timer_init_args(mult, div);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
* Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
* Copyright (c) 2019, Linaro Limited
*
* SPDX-License-Identifier: BSD-3-Clause
@ -25,27 +25,12 @@ typedef struct timer_ops {
uint32_t (*get_timer_value)(void);
uint32_t clk_mult;
uint32_t clk_div;
uint64_t (*timeout_init_us)(uint32_t usec);
bool (*timeout_elapsed)(uint64_t cnt);
} timer_ops_t;
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);
cnt += read_cntpct_el0();
return cnt;
}
static inline bool timeout_elapsed(uint64_t expire_cnt)
{
return read_cntpct_el0() > expire_cnt;
}
uint64_t timeout_init_us(uint32_t usec);
bool timeout_elapsed(uint64_t cnt);
void mdelay(uint32_t msec);
void udelay(uint32_t usec);
void timer_init(const timer_ops_t *ops_ptr);