blob: cfd9a15e308b369aaee1bd3043fce6e8bba3e165 [file] [log] [blame]
Varun Wadekarbc74fec2015-07-16 15:47:03 +05301/*
Anthony Zhouee4be0f2017-04-27 22:00:54 +08002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekar787a1292018-06-18 16:15:51 -07003 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
Varun Wadekarbc74fec2015-07-16 15:47:03 +05304 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekarbc74fec2015-07-16 15:47:03 +05306 */
7
Varun Wadekar787a1292018-06-18 16:15:51 -07008#include <arch.h>
9
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <drivers/delay_timer.h>
11#include <lib/mmio.h>
Varun Wadekar787a1292018-06-18 16:15:51 -070012#include <lib/utils_def.h>
13#include <plat/common/platform.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014
Varun Wadekarbc74fec2015-07-16 15:47:03 +053015#include <tegra_def.h>
Anthony Zhouee4be0f2017-04-27 22:00:54 +080016#include <tegra_private.h>
Varun Wadekarbc74fec2015-07-16 15:47:03 +053017
Varun Wadekar787a1292018-06-18 16:15:51 -070018static uint32_t tegra_timer_get_value(void)
Varun Wadekarbc74fec2015-07-16 15:47:03 +053019{
Varun Wadekar787a1292018-06-18 16:15:51 -070020 /* enable cntps_tval_el1 timer, mask interrupt */
21 write_cntps_ctl_el1(CNTP_CTL_IMASK_BIT | CNTP_CTL_ENABLE_BIT);
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_cntps_tval_el1() to simulate the down counter. The value is
27 * clipped from 64 to 32 bits.
28 */
29 return (uint32_t)(~read_cntps_tval_el1());
Varun Wadekarbc74fec2015-07-16 15:47:03 +053030}
31
Varun Wadekarbc74fec2015-07-16 15:47:03 +053032/*
Varun Wadekar787a1292018-06-18 16:15:51 -070033 * Initialise the architecture provided counter as the delay timer.
Varun Wadekarbc74fec2015-07-16 15:47:03 +053034 */
35void tegra_delay_timer_init(void)
36{
Varun Wadekar787a1292018-06-18 16:15:51 -070037 static timer_ops_t tegra_timer_ops;
38
39 /* Value in ticks */
40 uint32_t multiplier = MHZ_TICKS_PER_SEC;
41
42 /* Value in ticks per second (Hz) */
43 uint32_t divider = plat_get_syscnt_freq2();
44
45 /* Reduce multiplier and divider by dividing them repeatedly by 10 */
46 while (((multiplier % 10U) == 0U) && ((divider % 10U) == 0U)) {
47 multiplier /= 10U;
48 divider /= 10U;
49 }
50
51 /* enable cntps_tval_el1 timer, mask interrupt */
52 write_cntps_ctl_el1(CNTP_CTL_IMASK_BIT | CNTP_CTL_ENABLE_BIT);
Anthony Zhouee4be0f2017-04-27 22:00:54 +080053
Varun Wadekar787a1292018-06-18 16:15:51 -070054 /* register the timer */
55 tegra_timer_ops.get_timer_value = tegra_timer_get_value;
56 tegra_timer_ops.clk_mult = multiplier;
57 tegra_timer_ops.clk_div = divider;
Varun Wadekarbc74fec2015-07-16 15:47:03 +053058 timer_init(&tegra_timer_ops);
59}