1 /*
2  * Copyright (c) 2020-2022, MediaTek Inc. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <common/debug.h>
9 #include <lib/mmio.h>
10 #include <lib/mtk_init/mtk_init.h>
11 #include <mt_timer.h>
12 #include <platform_def.h>
13 
14 uint64_t normal_time_base;
15 uint64_t atf_time_base;
16 
sched_clock_init(uint64_t normal_base,uint64_t atf_base)17 void sched_clock_init(uint64_t normal_base, uint64_t atf_base)
18 {
19 	normal_time_base += normal_base;
20 	atf_time_base = atf_base;
21 }
22 
sched_clock(void)23 uint64_t sched_clock(void)
24 {
25 	uint64_t cval;
26 	uint64_t rel_base;
27 
28 	rel_base = read_cntpct_el0() - atf_time_base;
29 	cval = ((rel_base * 1000U) / SYS_COUNTER_FREQ_IN_MHZ)
30 		- normal_time_base;
31 	return cval;
32 }
33 
mt_systimer_init(void)34 int mt_systimer_init(void)
35 {
36 	INFO("[%s] systimer initialization\n", __func__);
37 
38 	/* Enable access in NS mode */
39 	mmio_write_32(CNTWACR_REG, CNT_WRITE_ACCESS_CTL_MASK);
40 	mmio_write_32(CNTRACR_REG, CNT_READ_ACCESS_CTL_MASK);
41 
42 	return 0;
43 }
44 MTK_PLAT_SETUP_0_INIT(mt_systimer_init);
45