Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 1 | /* |
Anthony Zhou | ee4be0f | 2017-04-27 22:00:54 +0800 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 3 | * Copyright (c) 2020, NVIDIA Corporation. All rights reserved. |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 4 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 5 | * SPDX-License-Identifier: BSD-3-Clause |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 8 | #include <arch.h> |
| 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <drivers/delay_timer.h> |
| 11 | #include <lib/mmio.h> |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 12 | #include <lib/utils_def.h> |
| 13 | #include <plat/common/platform.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 15 | #include <tegra_def.h> |
Anthony Zhou | ee4be0f | 2017-04-27 22:00:54 +0800 | [diff] [blame] | 16 | #include <tegra_private.h> |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 17 | |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 18 | static uint32_t tegra_timer_get_value(void) |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 19 | { |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 20 | /* 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 |
anzhou | 8462027 | 2020-06-26 15:21:10 +0800 | [diff] [blame] | 25 | * counter. The value is clipped from 64 to 32 bits. |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 26 | */ |
anzhou | 8462027 | 2020-06-26 15:21:10 +0800 | [diff] [blame] | 27 | return (uint32_t)(read_cntps_tval_el1()); |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 28 | } |
| 29 | |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 30 | /* |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 31 | * Initialise the architecture provided counter as the delay timer. |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 32 | */ |
| 33 | void tegra_delay_timer_init(void) |
| 34 | { |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 35 | static timer_ops_t tegra_timer_ops; |
| 36 | |
| 37 | /* Value in ticks */ |
| 38 | uint32_t multiplier = MHZ_TICKS_PER_SEC; |
| 39 | |
| 40 | /* Value in ticks per second (Hz) */ |
| 41 | uint32_t divider = plat_get_syscnt_freq2(); |
| 42 | |
| 43 | /* Reduce multiplier and divider by dividing them repeatedly by 10 */ |
| 44 | while (((multiplier % 10U) == 0U) && ((divider % 10U) == 0U)) { |
| 45 | multiplier /= 10U; |
| 46 | divider /= 10U; |
| 47 | } |
| 48 | |
| 49 | /* enable cntps_tval_el1 timer, mask interrupt */ |
| 50 | write_cntps_ctl_el1(CNTP_CTL_IMASK_BIT | CNTP_CTL_ENABLE_BIT); |
Anthony Zhou | ee4be0f | 2017-04-27 22:00:54 +0800 | [diff] [blame] | 51 | |
Varun Wadekar | 787a129 | 2018-06-18 16:15:51 -0700 | [diff] [blame] | 52 | /* register the timer */ |
| 53 | tegra_timer_ops.get_timer_value = tegra_timer_get_value; |
| 54 | tegra_timer_ops.clk_mult = multiplier; |
| 55 | tegra_timer_ops.clk_div = divider; |
Varun Wadekar | bc74fec | 2015-07-16 15:47:03 +0530 | [diff] [blame] | 56 | timer_init(&tegra_timer_ops); |
| 57 | } |