1 /*
2 * Copyright (c) 2024 Texas Instruments Incorporated
3 * Copyright (c) 2024 BayLibre, SAS
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT ti_cc23x0_systim_timer
9
10 /*
11 * TI SimpleLink CC23X0 timer driver based on SYSTIM
12 */
13
14 #include <soc.h>
15
16 #include <zephyr/device.h>
17 #include <zephyr/drivers/clock_control.h>
18 #include <zephyr/drivers/timer/system_timer.h>
19 #include <zephyr/irq.h>
20 #include <zephyr/spinlock.h>
21 #include <zephyr/sys_clock.h>
22 #include <zephyr/sys/util.h>
23
24 #include <inc/hw_types.h>
25 #include <inc/hw_memmap.h>
26 #include <inc/hw_systim.h>
27 #include <inc/hw_evtsvt.h>
28
29 /* Kernel tick period in microseconds (same timebase as systim) */
30 #define TICK_PERIOD_MICRO_SEC (1000000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
31
32 /*
33 * Max number of systim ticks into the future
34 *
35 * Under the hood, the kernel timer uses the SysTimer whose events trigger
36 * immediately if the compare value is less than 2^22 systimer ticks in the past
37 * (4.194sec at 1us resolution). Therefore, the max number of SysTimer ticks you
38 * can schedule into the future is 2^32 - 2^22 - 1 ticks (~= 4290 sec at 1us
39 * resolution).
40 */
41 #define SYSTIM_TIMEOUT_MAX 0xFFBFFFFFU
42
43 /* Set systim interrupt to lowest priority */
44 #define SYSTIM_ISR_PRIORITY 3U
45
46 /* Keep track of systim counter at previous announcement to the kernel */
47 static uint32_t last_systim_count;
48
49 static void systim_isr(const void *arg);
50 static int sys_clock_driver_init(void);
51
52 /*
53 * Set system clock timeout.
54 */
sys_clock_set_timeout(int32_t ticks,bool idle)55 void sys_clock_set_timeout(int32_t ticks, bool idle)
56 {
57 ARG_UNUSED(idle);
58
59 /* If timeout is necessary */
60 if (ticks != K_TICKS_FOREVER) {
61 /* Get current value as early as possible */
62 uint32_t now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
63 uint32_t timeout = ticks * TICK_PERIOD_MICRO_SEC;
64
65 if (timeout > SYSTIM_TIMEOUT_MAX) {
66 timeout = SYSTIM_TIMEOUT_MAX;
67 }
68 /* This should wrap around */
69 HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + timeout;
70 }
71 }
72
sys_clock_elapsed(void)73 uint32_t sys_clock_elapsed(void)
74 {
75 /* Get current value as early as possible */
76 uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
77 uint32_t elapsed_systim;
78
79 if (current_systim_count >= last_systim_count) {
80 elapsed_systim = current_systim_count - last_systim_count;
81 } else {
82 elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count;
83 }
84
85 int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC;
86
87 return elapsed_ticks;
88 }
89
sys_clock_cycle_get_32(void)90 uint32_t sys_clock_cycle_get_32(void)
91 {
92 return HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
93 }
94
systim_isr(const void * arg)95 void systim_isr(const void *arg)
96 {
97 /* Get current value as early as possible */
98 uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
99 uint32_t elapsed_systim;
100
101 if (current_systim_count >= last_systim_count) {
102 elapsed_systim = current_systim_count - last_systim_count;
103 } else {
104 elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count;
105 }
106
107 int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC;
108
109 sys_clock_announce(elapsed_ticks);
110
111 last_systim_count = current_systim_count;
112
113 /* Do not re-arm systim. Zephyr will do so through sys_clock_set_timeout */
114 }
115
sys_clock_driver_init(void)116 static int sys_clock_driver_init(void)
117 {
118 uint32_t now_tick;
119
120 /* Get current value as early as possible */
121 now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
122 last_systim_count = now_tick;
123
124 /* Clear any pending interrupts on SysTimer channel 0 */
125 HWREG(SYSTIM_BASE + SYSTIM_O_ICLR) = SYSTIM_ICLR_EV0_CLR;
126
127 /*
128 * Configure SysTimer channel 0 to compare mode with timer
129 * resolution of 1 us.
130 */
131 HWREG(SYSTIM_BASE + SYSTIM_O_CH0CFG) = 0;
132
133 /* Make SysTimer halt on CPU debug halt */
134 HWREG(SYSTIM_BASE + SYSTIM_O_EMU) = SYSTIM_EMU_HALT_STOP;
135
136 HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ16SEL) = EVTSVT_CPUIRQ16SEL_PUBID_SYSTIM0;
137
138 /*
139 * Set IMASK for channel 0. IMASK is used by the power driver to know
140 * which systimer channels are active.
141 */
142 HWREG(SYSTIM_BASE + SYSTIM_O_IMSET) = SYSTIM_IMSET_EV0_SET;
143
144 /* This should wrap around and set a maximum timeout */
145 HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + SYSTIM_TIMEOUT_MAX;
146
147 /* Take configurable interrupt IRQ16 for systimer */
148 IRQ_CONNECT(CPUIRQ16_IRQn, SYSTIM_ISR_PRIORITY, systim_isr, 0, 0);
149 irq_enable(CPUIRQ16_IRQn);
150
151 return 0;
152 }
153
154 SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
155