tegra_delay_timer.c 1.59 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3
 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
4
 *
dp-arm's avatar
dp-arm committed
5
 * SPDX-License-Identifier: BSD-3-Clause
6
7
 */

8
9
#include <arch.h>

10
11
#include <drivers/delay_timer.h>
#include <lib/mmio.h>
12
13
#include <lib/utils_def.h>
#include <plat/common/platform.h>
14

15
#include <tegra_def.h>
16
#include <tegra_private.h>
17

18
static uint32_t tegra_timer_get_value(void)
19
{
20
21
22
23
24
25
26
27
28
29
	/* enable cntps_tval_el1 timer, mask interrupt */
	write_cntps_ctl_el1(CNTP_CTL_IMASK_BIT | CNTP_CTL_ENABLE_BIT);

	/*
	 * Generic delay timer implementation expects the timer to be a down
	 * counter. We apply bitwise NOT operator to the tick values returned
	 * by read_cntps_tval_el1() to simulate the down counter. The value is
	 * clipped from 64 to 32 bits.
	 */
	return (uint32_t)(~read_cntps_tval_el1());
30
31
32
}

/*
33
 * Initialise the architecture provided counter as the delay timer.
34
35
36
 */
void tegra_delay_timer_init(void)
{
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	static timer_ops_t tegra_timer_ops;

	/* Value in ticks */
	uint32_t multiplier = MHZ_TICKS_PER_SEC;

	/* Value in ticks per second (Hz) */
	uint32_t divider  = plat_get_syscnt_freq2();

	/* Reduce multiplier and divider by dividing them repeatedly by 10 */
	while (((multiplier % 10U) == 0U) && ((divider % 10U) == 0U)) {
		multiplier /= 10U;
		divider /= 10U;
	}

	/* enable cntps_tval_el1 timer, mask interrupt */
	write_cntps_ctl_el1(CNTP_CTL_IMASK_BIT | CNTP_CTL_ENABLE_BIT);
53

54
55
56
57
	/* register the timer */
	tegra_timer_ops.get_timer_value = tegra_timer_get_value;
	tegra_timer_ops.clk_mult = multiplier;
	tegra_timer_ops.clk_div = divider;
58
59
	timer_init(&tegra_timer_ops);
}