1 /*
2  * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <soc/soc_caps.h>
7 #include <soc/soc.h>
8 #include <soc/interrupt_core0_reg.h>
9 #include <soc/periph_defs.h>
10 #include <soc/system_reg.h>
11 #include <hal/systimer_hal.h>
12 #include <hal/systimer_ll.h>
13 #include <rom/ets_sys.h>
14 #include <esp_attr.h>
15 
16 #include <drivers/timer/system_timer.h>
17 #include <sys_clock.h>
18 #include <soc.h>
19 
20 #define SYS_TIMER_CPU_IRQ               16
21 
22 #define CYC_PER_TICK ((uint32_t)((uint64_t)sys_clock_hw_cycles_per_sec()	\
23 			      / (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC))
24 #define MAX_CYC 0xffffffffu
25 #define MAX_TICKS ((MAX_CYC - CYC_PER_TICK) / CYC_PER_TICK)
26 #define MIN_DELAY 1000
27 
28 #define TICKLESS IS_ENABLED(CONFIG_TICKLESS_KERNEL)
29 
30 static struct k_spinlock lock;
31 static uint64_t last_count;
32 
set_systimer_alarm(uint64_t time)33 static void set_systimer_alarm(uint64_t time)
34 {
35 	systimer_hal_select_alarm_mode(SYSTIMER_ALARM_0, SYSTIMER_ALARM_MODE_ONESHOT);
36 	systimer_hal_set_alarm_target(SYSTIMER_ALARM_0, time);
37 	systimer_hal_enable_alarm_int(SYSTIMER_ALARM_0);
38 }
39 
systimer_alarm(void)40 static uint64_t systimer_alarm(void)
41 {
42 	return systimer_hal_get_time(SYSTIMER_COUNTER_1);
43 }
44 
sys_timer_isr(const void * arg)45 static void sys_timer_isr(const void *arg)
46 {
47 	ARG_UNUSED(arg);
48 
49 	systimer_ll_clear_alarm_int(SYSTIMER_ALARM_0);
50 
51 	k_spinlock_key_t key = k_spin_lock(&lock);
52 	uint64_t now = systimer_alarm();
53 
54 	uint32_t dticks = (uint32_t)((now - last_count) / CYC_PER_TICK);
55 
56 	last_count += dticks * CYC_PER_TICK;
57 
58 	if (!TICKLESS) {
59 		uint64_t next = last_count + CYC_PER_TICK;
60 
61 		if ((int64_t)(next - now) < MIN_DELAY) {
62 			next += CYC_PER_TICK;
63 		}
64 		set_systimer_alarm(next);
65 	}
66 
67 	k_spin_unlock(&lock, key);
68 	sys_clock_announce(IS_ENABLED(CONFIG_TICKLESS_KERNEL) ? dticks : 1);
69 }
70 
sys_clock_driver_init(const struct device * dev)71 int sys_clock_driver_init(const struct device *dev)
72 {
73 	ARG_UNUSED(dev);
74 
75 	esp_rom_intr_matrix_set(0,
76 		ETS_SYSTIMER_TARGET0_EDGE_INTR_SOURCE,
77 		SYS_TIMER_CPU_IRQ);
78 	IRQ_CONNECT(SYS_TIMER_CPU_IRQ, 0, sys_timer_isr, NULL, 0);
79 	systimer_hal_init();
80 	systimer_hal_connect_alarm_counter(SYSTIMER_ALARM_0, SYSTIMER_COUNTER_1);
81 	systimer_hal_enable_counter(SYSTIMER_COUNTER_1);
82 	systimer_hal_counter_can_stall_by_cpu(SYSTIMER_COUNTER_1, 0, true);
83 	last_count = systimer_alarm();
84 	set_systimer_alarm(last_count + CYC_PER_TICK);
85 	irq_enable(SYS_TIMER_CPU_IRQ);
86 
87 	return 0;
88 }
89 
sys_clock_set_timeout(int32_t ticks,bool idle)90 void sys_clock_set_timeout(int32_t ticks, bool idle)
91 {
92 	ARG_UNUSED(idle);
93 
94 #if defined(CONFIG_TICKLESS_KERNEL)
95 	ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks;
96 	ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
97 
98 	k_spinlock_key_t key = k_spin_lock(&lock);
99 	uint64_t now = systimer_alarm();
100 	uint32_t adj, cyc = ticks * CYC_PER_TICK;
101 
102 	/* Round up to next tick boundary. */
103 	adj = (uint32_t)(now - last_count) + (CYC_PER_TICK - 1);
104 	if (cyc <= MAX_CYC - adj) {
105 		cyc += adj;
106 	} else {
107 		cyc = MAX_CYC;
108 	}
109 	cyc = (cyc / CYC_PER_TICK) * CYC_PER_TICK;
110 
111 	if ((int32_t)(cyc + last_count - now) < MIN_DELAY) {
112 		cyc += CYC_PER_TICK;
113 	}
114 
115 	set_systimer_alarm(cyc + last_count);
116 	k_spin_unlock(&lock, key);
117 #endif
118 }
119 
sys_clock_elapsed(void)120 uint32_t sys_clock_elapsed(void)
121 {
122 	if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
123 		return 0;
124 	}
125 
126 	k_spinlock_key_t key = k_spin_lock(&lock);
127 	uint32_t ret = ((uint32_t)systimer_alarm() - (uint32_t)last_count) / CYC_PER_TICK;
128 
129 	k_spin_unlock(&lock, key);
130 	return ret;
131 }
132 
sys_clock_cycle_get_32(void)133 uint32_t sys_clock_cycle_get_32(void)
134 {
135 	return systimer_ll_get_counter_value_low(SYSTIMER_COUNTER_1);
136 }
137