1 /*
2 * Copyright (c) 2024 Microchip Technology Incorporated
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT microchip_mec5_ktimer
7
8 #include <zephyr/init.h>
9 #include <zephyr/devicetree.h>
10 #include <soc.h>
11 #include <zephyr/drivers/timer/system_timer.h>
12 #include <zephyr/sys_clock.h>
13 #include <zephyr/spinlock.h>
14 #include <cmsis_core.h>
15 #include <zephyr/irq.h>
16
17 #include <device_mec5.h>
18 #include <mec_btimer_api.h>
19 #include <mec_rtimer_api.h>
20
21 BUILD_ASSERT(!IS_ENABLED(CONFIG_SMP), "MCHP MEC5 ktimer doesn't support SMP");
22 BUILD_ASSERT(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 32768,
23 "MCHP MEC5 ktimer HW frequency is fixed at 32768");
24
25 #ifndef CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT
26 BUILD_ASSERT(0, "MCHP MEC5 ktimer requires ARCH_HAS_CUSTOM_BUSY_WAIT");
27 #endif
28
29 #ifdef CONFIG_SOC_MEC_DEBUG_AND_TRACING
30 #define RTIMER_START_VAL MEC_RTIMER_START_EXT_HALT
31 #else
32 #define RTIMER_START_VAL MEC_RTIMER_START
33 #endif
34
35 /*
36 * Overview:
37 *
38 * This driver enables the Microchip XEC 32KHz based RTOS timer as the Zephyr
39 * system timer. It supports both legacy ("tickful") mode as well as
40 * TICKLESS_KERNEL. The XEC RTOS timer is a down counter with a fixed
41 * frequency of 32768 Hz. The driver is based upon the Intel local APIC
42 * timer driver.
43 * Configuration:
44 *
45 * CONFIG_MCHP_XEC_RTOS_TIMER=y
46 *
47 * CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=<hz> must be set to 32768.
48 *
49 * To reduce truncation errors from accumulating due to conversion
50 * to/from time, ticks, and HW cycles set ticks per second equal to
51 * the frequency. With tickless kernel mode enabled the kernel will not
52 * program a periodic timer at this fast rate.
53 * CONFIG_SYS_CLOCK_TICKS_PER_SEC=32768
54 */
55
56 #define CYCLES_PER_TICK (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
57
58 /* Mask off bits[31:28] of 32-bit count */
59 #define RTIMER_MAX 0x0fffffffu
60 #define RTIMER_COUNT_MASK 0x0fffffffu
61 #define RTIMER_STOPPED 0xf0000000u
62
63 /* Adjust cycle count programmed into timer for HW restart latency */
64 #define RTIMER_ADJUST_LIMIT 2
65 #define RTIMER_ADJUST_CYCLES 1
66
67 /* max number of ticks we can load into the timer in one shot */
68 #define MAX_TICKS (RTIMER_MAX / CYCLES_PER_TICK)
69
70 #define RTIMER_NODE_ID DT_INST(0, DT_DRV_COMPAT)
71 #define RTIMER_NVIC_NO DT_INST_IRQN(0)
72 #define RTIMER_NVIC_PRIO DT_INST_IRQ(0, priority)
73
74 static struct mec_rtmr_regs *const rtimer = (struct mec_rtmr_regs *)DT_INST_REG_ADDR(0);
75
76 #ifdef CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT
77 #define BTIMER_NODE_ID DT_CHOSEN(rtimer_busy_wait_timer)
78 #define MEC5_BTIMER_FDIV (MEC5_BTIMER_MAX_FREQ_HZ / 1000000u)
79
80 static struct mec_btmr_regs *const btimer = (struct mec_btmr_regs *)DT_REG_ADDR(BTIMER_NODE_ID);
81 #endif
82
83 /*
84 * The spinlock protects all access to the RTIMER registers, as well as
85 * 'total_cycles', 'last_announcement', and 'cached_icr'.
86 *
87 * One important invariant that must be observed: `total_cycles` + `cached_icr`
88 * is always an integral multiple of CYCLE_PER_TICK; this is, timer interrupts
89 * are only ever scheduled to occur at tick boundaries.
90 */
91
92 static struct k_spinlock lock;
93 static uint32_t total_cycles;
94 static uint32_t cached_icr = CYCLES_PER_TICK;
95
96 /*
97 * Read the RTOS timer counter handling the case where the timer
98 * has been reloaded within 1 32KHz clock of reading its count register.
99 * The RTOS timer hardware must synchronize the write to its control register
100 * on the AHB clock domain with the 32KHz clock domain of its internal logic.
101 * This synchronization can take from nearly 0 time up to 1 32KHz clock as it
102 * depends upon which 48MHz AHB clock with a 32KHz period the register write
103 * was on. We detect the timer is in the load state by checking the read-only
104 * count register and the START bit in the control register. If count register
105 * is 0 and the START bit is set then the timer has been started and is in the
106 * process of moving the preload register value into the count register.
107 */
rtimer_count(void)108 static inline uint32_t rtimer_count(void)
109 {
110 uint32_t ccr = mec_hal_rtimer_count(rtimer);
111
112 if ((ccr == 0) && mec_hal_rtimer_is_started(rtimer)) {
113 ccr = cached_icr;
114 }
115
116 return ccr;
117 }
118
119 #ifdef CONFIG_TICKLESS_KERNEL
120
121 static uint32_t last_announcement; /* last time we called sys_clock_announce() */
122
123 /*
124 * Request a timeout n Zephyr ticks in the future from now.
125 * Requested number of ticks in the future of n <= 1 means the kernel wants
126 * the tick announced as soon as possible, ideally no more than one tick
127 * in the future.
128 *
129 * Per comment below we don't clear RTMR pending interrupt.
130 * RTMR counter register is read-only and is loaded from the preload
131 * register by a 0->1 transition of the control register start bit.
132 * Writing a new value to preload only takes effect once the count
133 * register reaches 0.
134 */
sys_clock_set_timeout(int32_t n,bool idle)135 void sys_clock_set_timeout(int32_t n, bool idle)
136 {
137 ARG_UNUSED(idle);
138
139 uint32_t ccr, temp;
140 int full_ticks; /* number of complete ticks we'll wait */
141 uint32_t full_cycles; /* full_ticks represented as cycles */
142 uint32_t partial_cycles; /* number of cycles to first tick boundary */
143
144 if (idle && (n == K_TICKS_FOREVER)) {
145 /*
146 * We are not in a locked section. Are writes to two
147 * global objects safe from pre-emption?
148 */
149 mec_hal_rtimer_stop(rtimer);
150 cached_icr = RTIMER_STOPPED;
151 return;
152 }
153
154 if (n < 1) {
155 full_ticks = 0;
156 } else if ((n == K_TICKS_FOREVER) || (n > MAX_TICKS)) {
157 full_ticks = MAX_TICKS - 1;
158 } else {
159 full_ticks = n - 1;
160 }
161
162 full_cycles = full_ticks * CYCLES_PER_TICK;
163
164 k_spinlock_key_t key = k_spin_lock(&lock);
165
166 ccr = rtimer_count();
167
168 /* turn off to clear any pending interrupt status */
169 mec_hal_rtimer_stop(rtimer);
170 mec_hal_rtimer_status_clear_all(rtimer);
171 NVIC_ClearPendingIRQ(RTIMER_NVIC_NO);
172
173 temp = total_cycles;
174 temp += (cached_icr - ccr);
175 temp &= RTIMER_COUNT_MASK;
176 total_cycles = temp;
177
178 partial_cycles = CYCLES_PER_TICK - (total_cycles % CYCLES_PER_TICK);
179 cached_icr = full_cycles + partial_cycles;
180 /* adjust for up to one 32KHz cycle startup time */
181 temp = cached_icr;
182 if (temp > RTIMER_ADJUST_LIMIT) {
183 temp -= RTIMER_ADJUST_CYCLES;
184 }
185
186 mec_hal_rtimer_stop_and_load(rtimer, temp, RTIMER_START_VAL);
187
188 k_spin_unlock(&lock, key);
189 }
190
191 /*
192 * Return the number of Zephyr ticks elapsed from last call to
193 * sys_clock_announce in the ISR. The caller casts uint32_t to int32_t.
194 * We must make sure bit[31] is 0 in the return value.
195 */
sys_clock_elapsed(void)196 uint32_t sys_clock_elapsed(void)
197 {
198 uint32_t ccr;
199 uint32_t ticks;
200 int32_t elapsed;
201
202 k_spinlock_key_t key = k_spin_lock(&lock);
203
204 ccr = rtimer_count();
205
206 /* It may not look efficient but the compiler does a good job */
207 elapsed = (int32_t)total_cycles - (int32_t)last_announcement;
208 if (elapsed < 0) {
209 elapsed = -1 * elapsed;
210 }
211 ticks = (uint32_t)elapsed;
212 ticks += cached_icr - ccr;
213 ticks /= CYCLES_PER_TICK;
214 ticks &= RTIMER_COUNT_MASK;
215
216 k_spin_unlock(&lock, key);
217
218 return ticks;
219 }
220
mec5_ktimer_isr(const void * arg)221 static void mec5_ktimer_isr(const void *arg)
222 {
223 ARG_UNUSED(arg);
224
225 uint32_t cycles;
226 int32_t ticks;
227
228 k_spinlock_key_t key = k_spin_lock(&lock);
229
230 mec_hal_rtimer_status_clear_all(rtimer);
231
232 /* Restart the timer as early as possible to minimize drift... */
233 mec_hal_rtimer_stop_and_load(rtimer, MAX_TICKS * CYCLES_PER_TICK, RTIMER_START_VAL);
234
235 cycles = cached_icr;
236 cached_icr = MAX_TICKS * CYCLES_PER_TICK;
237
238 total_cycles += cycles;
239 total_cycles &= RTIMER_COUNT_MASK;
240
241 /* handle wrap by using (power of 2) - 1 mask */
242 ticks = total_cycles - last_announcement;
243 ticks &= RTIMER_COUNT_MASK;
244 ticks /= CYCLES_PER_TICK;
245
246 last_announcement = total_cycles;
247
248 k_spin_unlock(&lock, key);
249 sys_clock_announce(ticks);
250 }
251
252 #else
253 /* Non-tickless kernel build. */
mec5_ktimer_isr(const void * arg)254 static void mec5_ktimer_isr(const void *arg)
255 {
256 ARG_UNUSED(arg);
257
258 k_spinlock_key_t key = k_spin_lock(&lock);
259
260 mec_hal_rtimer_status_clear_all(rtimer);
261
262 /* Restart the timer as early as possible to minimize drift... */
263 mec_hal_rtimer_stop_and_load(rtimer, cached_icr, RTIMER_START_VAL);
264
265 uint32_t temp = total_cycles + CYCLES_PER_TICK;
266
267 total_cycles = temp & RTIMER_COUNT_MASK;
268 k_spin_unlock(&lock, key);
269
270 sys_clock_announce(1);
271 }
272
sys_clock_elapsed(void)273 uint32_t sys_clock_elapsed(void)
274 {
275 return 0U;
276 }
277 #endif /* CONFIG_TICKLESS_KERNEL */
278
279 /*
280 * Warning RTOS timer resolution is 30.5 us.
281 * This is called by two code paths:
282 * 1. Kernel call to k_cycle_get_32() -> arch_k_cycle_get_32() -> here.
283 * The kernel is casting return to (int) and using it uncasted in math
284 * expressions with int types. Expression result is stored in an int.
285 * 2. If CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT is not defined then
286 * z_impl_k_busy_wait calls here. This code path uses the value as uint32_t.
287 *
288 */
sys_clock_cycle_get_32(void)289 uint32_t sys_clock_cycle_get_32(void)
290 {
291 uint32_t ret;
292 uint32_t ccr;
293
294 k_spinlock_key_t key = k_spin_lock(&lock);
295
296 ccr = rtimer_count();
297 ret = (total_cycles + (cached_icr - ccr)) & RTIMER_COUNT_MASK;
298
299 k_spin_unlock(&lock, key);
300
301 return ret;
302 }
303
sys_clock_idle_exit(void)304 void sys_clock_idle_exit(void)
305 {
306 if (cached_icr == RTIMER_STOPPED) {
307 cached_icr = CYCLES_PER_TICK;
308 mec_hal_rtimer_stop_and_load(rtimer, cached_icr, RTIMER_START_VAL);
309 }
310 }
311
sys_clock_disable(void)312 void sys_clock_disable(void)
313 {
314 mec_hal_rtimer_stop(rtimer);
315 }
316
317 #ifdef CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT
318 /* Custom kernel busy wait API implementation using a 48MHz based
319 * 32-bit basic timer divided down to 1 MHz. Basic timer configured
320 * for count up, auto-reload, and no interrupt mode.
321 */
arch_busy_wait(uint32_t usec_to_wait)322 void arch_busy_wait(uint32_t usec_to_wait)
323 {
324 if (usec_to_wait == 0) {
325 return;
326 }
327
328 uint32_t start = mec_hal_btimer_count(btimer);
329
330 for (;;) {
331 uint32_t curr = mec_hal_btimer_count(btimer);
332
333 if ((curr - start) >= usec_to_wait) {
334 break;
335 }
336 }
337 }
338
339 /* k_busy_wait parameter is the number of microseconds to wait.
340 * Configure basic timer for 1 MHz (1 us tick) operation.
341 */
config_custom_busy_wait(void)342 static int config_custom_busy_wait(void)
343 {
344 uint32_t bflags =
345 (BIT(MEC5_BTIMER_CFG_FLAG_START_POS) | BIT(MEC5_BTIMER_CFG_FLAG_AUTO_RELOAD_POS) |
346 BIT(MEC5_BTIMER_CFG_FLAG_COUNT_UP_POS));
347 uint32_t count = 0;
348
349 mec_hal_btimer_init(btimer, MEC5_BTIMER_FDIV, count, bflags);
350
351 return 0;
352 }
353
soc_ktimer_pm_entry(bool is_deep_sleep)354 void soc_ktimer_pm_entry(bool is_deep_sleep)
355 {
356 if (is_deep_sleep) {
357 mec_hal_btimer_disable(btimer);
358 }
359 }
360
soc_ktimer_pm_exit(bool is_deep_sleep)361 void soc_ktimer_pm_exit(bool is_deep_sleep)
362 {
363 if (is_deep_sleep) {
364 mec_hal_btimer_enable(btimer);
365 }
366 }
367 #else
soc_ktimer_pm_entry(void)368 void soc_ktimer_pm_entry(void)
369 {
370 }
soc_ktimer_pm_exit(void)371 void soc_ktimer_pm_exit(void)
372 {
373 }
374 #endif /* CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT */
375
sys_clock_driver_init(void)376 static int sys_clock_driver_init(void)
377 {
378 uint32_t rtmr_cfg = BIT(MEC_RTMR_CFG_EN_POS) | BIT(MEC_RTMR_CFG_IEN_POS);
379
380 if (IS_ENABLED(CONFIG_SOC_MEC_DEBUG_AND_TRACING)) {
381 rtmr_cfg |= BIT(MEC_RTMR_CFG_DBG_HALT_POS);
382 }
383
384 #ifdef CONFIG_TICKLESS_KERNEL
385 cached_icr = MAX_TICKS;
386 #endif
387
388 mec_hal_rtimer_init(rtimer, rtmr_cfg, cached_icr);
389
390 IRQ_CONNECT(RTIMER_NVIC_NO, RTIMER_NVIC_PRIO, mec5_ktimer_isr, 0, 0);
391 irq_enable(RTIMER_NVIC_NO);
392
393 #ifdef CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT
394 config_custom_busy_wait();
395 #endif
396
397 mec_hal_rtimer_start(rtimer);
398 while (!mec_hal_rtimer_is_counting(rtimer)) {
399 ;
400 }
401
402 return 0;
403 }
404
405 SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
406