1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-12-20     GuEe-GUI     first version
9  */
10 
11 #include <rtthread.h>
12 #include <rthw.h>
13 #include <gtimer.h>
14 #include <cpuport.h>
15 
16 #ifdef RT_USING_KTIME
17 #include <ktime.h>
18 #endif
19 
20 #define EL1_PHY_TIMER_IRQ_NUM 30
21 
22 static volatile rt_uint64_t timer_step;
23 
rt_hw_timer_isr(int vector,void * parameter)24 static void rt_hw_timer_isr(int vector, void *parameter)
25 {
26     rt_hw_set_gtimer_val(timer_step);
27     rt_tick_increase();
28 }
29 
rt_hw_gtimer_init(void)30 void rt_hw_gtimer_init(void)
31 {
32     rt_hw_interrupt_install(EL1_PHY_TIMER_IRQ_NUM, rt_hw_timer_isr, RT_NULL, "tick");
33     rt_hw_isb();
34     timer_step = rt_hw_get_gtimer_frq();
35     rt_hw_dsb();
36     timer_step /= RT_TICK_PER_SECOND;
37     rt_hw_gtimer_local_enable();
38 }
39 
rt_hw_gtimer_local_enable(void)40 void rt_hw_gtimer_local_enable(void)
41 {
42     rt_hw_gtimer_disable();
43     rt_hw_set_gtimer_val(timer_step);
44     rt_hw_interrupt_umask(EL1_PHY_TIMER_IRQ_NUM);
45 #ifdef RT_USING_KTIME
46     rt_ktime_cputimer_init();
47 #endif
48     rt_hw_gtimer_enable();
49 }
50 
rt_hw_gtimer_local_disable(void)51 void rt_hw_gtimer_local_disable(void)
52 {
53     rt_hw_gtimer_disable();
54     rt_hw_interrupt_mask(EL1_PHY_TIMER_IRQ_NUM);
55 }
56