blob: 8a36c8abd7ef6349b80c3383b355cfe171e7e20f [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
7#include <arch_helpers.h>
8#include <assert.h>
9#include <bl_common.h>
10#include <debug.h>
11#include <delay_timer.h>
Etienne Carrieref4eecb22017-06-07 16:42:26 +020012#include <generic_delay_timer.h>
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010013#include <platform.h>
14
15/* Ticks elapsed in one second by a signal of 1 MHz */
16#define MHZ_TICKS_PER_SEC 1000000
17
18static timer_ops_t ops;
19
20static uint32_t get_timer_value(void)
21{
22 /*
23 * Generic delay timer implementation expects the timer to be a down
24 * counter. We apply bitwise NOT operator to the tick values returned
25 * by read_cntpct_el0() to simulate the down counter. The value is
26 * clipped from 64 to 32 bits.
27 */
28 return (uint32_t)(~read_cntpct_el0());
29}
30
31void generic_delay_timer_init_args(uint32_t mult, uint32_t div)
32{
33 ops.get_timer_value = get_timer_value;
34 ops.clk_mult = mult;
35 ops.clk_div = div;
36
37 timer_init(&ops);
38
39 VERBOSE("Generic delay timer configured with mult=%u and div=%u\n",
40 mult, div);
41}
42
43void generic_delay_timer_init(void)
44{
45 /* Value in ticks */
46 unsigned int mult = MHZ_TICKS_PER_SEC;
47
48 /* Value in ticks per second (Hz) */
49 unsigned int div = plat_get_syscnt_freq2();
50
51 /* Reduce multiplier and divider by dividing them repeatedly by 10 */
52 while ((mult % 10 == 0) && (div % 10 == 0)) {
53 mult /= 10;
54 div /= 10;
55 }
56
57 generic_delay_timer_init_args(mult, div);
58}
59