blob: 0407e385960493668b0e95a8a1c33d808d2173a8 [file] [log] [blame]
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01001/*
Abhi.Singhce668112024-08-21 12:55:38 -05002 * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
Varun Wadekar0a176e32020-02-13 13:07:12 -08003 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01004 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01006 */
7
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01008#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Antonio Nino Diazd29d21e2019-02-06 09:23:04 +000010#include <arch_features.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <arch_helpers.h>
12#include <common/bl_common.h>
13#include <common/debug.h>
14#include <drivers/delay_timer.h>
15#include <drivers/generic_delay_timer.h>
Varun Wadekar0a176e32020-02-13 13:07:12 -080016#include <lib/utils_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <plat/common/platform.h>
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010018
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010019static timer_ops_t ops;
20
Abhi.Singhce668112024-08-21 12:55:38 -050021static uint64_t timeout_cnt_us2cnt(uint32_t us)
22{
23 return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
24}
25
26static uint64_t generic_delay_timeout_init_us(uint32_t us)
27{
28 uint64_t cnt = timeout_cnt_us2cnt(us);
29
30 cnt += read_cntpct_el0();
31
32 return cnt;
33}
34
35static bool generic_delay_timeout_elapsed(uint64_t expire_cnt)
36{
37 return read_cntpct_el0() > expire_cnt;
38}
39
40static uint32_t generic_delay_get_timer_value(void)
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010041{
42 /*
43 * Generic delay timer implementation expects the timer to be a down
44 * counter. We apply bitwise NOT operator to the tick values returned
45 * by read_cntpct_el0() to simulate the down counter. The value is
46 * clipped from 64 to 32 bits.
47 */
48 return (uint32_t)(~read_cntpct_el0());
49}
50
51void generic_delay_timer_init_args(uint32_t mult, uint32_t div)
52{
Abhi.Singhce668112024-08-21 12:55:38 -050053 ops.get_timer_value = generic_delay_get_timer_value;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010054 ops.clk_mult = mult;
55 ops.clk_div = div;
Abhi.Singhce668112024-08-21 12:55:38 -050056 ops.timeout_init_us = generic_delay_timeout_init_us;
57 ops.timeout_elapsed = generic_delay_timeout_elapsed;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010058
59 timer_init(&ops);
60
61 VERBOSE("Generic delay timer configured with mult=%u and div=%u\n",
62 mult, div);
63}
64
65void generic_delay_timer_init(void)
66{
Antonio Nino Diazd29d21e2019-02-06 09:23:04 +000067 assert(is_armv7_gentimer_present());
68
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010069 /* Value in ticks */
70 unsigned int mult = MHZ_TICKS_PER_SEC;
71
72 /* Value in ticks per second (Hz) */
73 unsigned int div = plat_get_syscnt_freq2();
74
75 /* Reduce multiplier and divider by dividing them repeatedly by 10 */
Sathees Balya006b15e2018-09-19 14:23:03 +010076 while (((mult % 10U) == 0U) && ((div % 10U) == 0U)) {
77 mult /= 10U;
78 div /= 10U;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010079 }
80
81 generic_delay_timer_init_args(mult, div);
82}