blob: 4b67ed0d148bec3b47ad91d44850a8514ef6d8d6 [file] [log] [blame]
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01005 */
6
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +01007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <arch_helpers.h>
10#include <common/bl_common.h>
11#include <common/debug.h>
12#include <drivers/delay_timer.h>
13#include <drivers/generic_delay_timer.h>
14#include <plat/common/platform.h>
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010015
16/* Ticks elapsed in one second by a signal of 1 MHz */
17#define MHZ_TICKS_PER_SEC 1000000
18
19static timer_ops_t ops;
20
21static uint32_t get_timer_value(void)
22{
23 /*
24 * Generic delay timer implementation expects the timer to be a down
25 * counter. We apply bitwise NOT operator to the tick values returned
26 * by read_cntpct_el0() to simulate the down counter. The value is
27 * clipped from 64 to 32 bits.
28 */
29 return (uint32_t)(~read_cntpct_el0());
30}
31
32void generic_delay_timer_init_args(uint32_t mult, uint32_t div)
33{
34 ops.get_timer_value = get_timer_value;
35 ops.clk_mult = mult;
36 ops.clk_div = div;
37
38 timer_init(&ops);
39
40 VERBOSE("Generic delay timer configured with mult=%u and div=%u\n",
41 mult, div);
42}
43
44void generic_delay_timer_init(void)
45{
46 /* Value in ticks */
47 unsigned int mult = MHZ_TICKS_PER_SEC;
48
49 /* Value in ticks per second (Hz) */
50 unsigned int div = plat_get_syscnt_freq2();
51
52 /* Reduce multiplier and divider by dividing them repeatedly by 10 */
Sathees Balya006b15e2018-09-19 14:23:03 +010053 while (((mult % 10U) == 0U) && ((div % 10U) == 0U)) {
54 mult /= 10U;
55 div /= 10U;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010056 }
57
58 generic_delay_timer_init_args(mult, div);
59}
60