1 /*
2  * Copyright (c) 2021 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_os_timer
8 
9 #include <drivers/timer/system_timer.h>
10 #include <sys_clock.h>
11 #include <spinlock.h>
12 #include "fsl_ostimer.h"
13 #include "fsl_power.h"
14 
15 #define CYC_PER_TICK ((uint32_t)((uint64_t)sys_clock_hw_cycles_per_sec()	\
16 			      / (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC))
17 #define MAX_CYC INT_MAX
18 #define MAX_TICKS ((MAX_CYC - CYC_PER_TICK) / CYC_PER_TICK)
19 #define MIN_DELAY 1000
20 
21 #define TICKLESS IS_ENABLED(CONFIG_TICKLESS_KERNEL)
22 
23 static struct k_spinlock lock;
24 static uint64_t last_count;
25 static OSTIMER_Type *base;
26 
mcux_lpc_ostick_isr(void * arg)27 void mcux_lpc_ostick_isr(void *arg)
28 {
29 	ARG_UNUSED(arg);
30 
31 	k_spinlock_key_t key = k_spin_lock(&lock);
32 	uint64_t now = OSTIMER_GetCurrentTimerValue(base);
33 	uint32_t dticks = (uint32_t)((now - last_count) / CYC_PER_TICK);
34 
35 	/* Clear interrupt flag by writing 1. */
36 	base->OSEVENT_CTRL &= ~OSTIMER_OSEVENT_CTRL_OSTIMER_INTENA_MASK;
37 
38 	last_count += dticks * CYC_PER_TICK;
39 
40 	if (!TICKLESS) {
41 		uint64_t next = last_count + CYC_PER_TICK;
42 
43 		if ((int64_t)(next - now) < MIN_DELAY) {
44 			next += CYC_PER_TICK;
45 		}
46 		OSTIMER_SetMatchValue(base, next, NULL);
47 	}
48 
49 	k_spin_unlock(&lock, key);
50 	sys_clock_announce(IS_ENABLED(CONFIG_TICKLESS_KERNEL) ? dticks : 1);
51 }
52 
sys_clock_driver_init(const struct device * device)53 int sys_clock_driver_init(const struct device *device)
54 {
55 	ARG_UNUSED(device);
56 
57 	/* Configure event timer's ISR */
58 	IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
59 					mcux_lpc_ostick_isr, NULL, 0);
60 
61 	base = (OSTIMER_Type *)DT_INST_REG_ADDR(0);
62 
63 	EnableDeepSleepIRQ(DT_INST_IRQN(0));
64 
65 	/* Initialize the OS timer, setting clock configuration. */
66 	OSTIMER_Init(base);
67 
68 	last_count = OSTIMER_GetCurrentTimerValue(base);
69 	OSTIMER_SetMatchValue(base, last_count + CYC_PER_TICK, NULL);
70 
71 	/* Enable event timer interrupt */
72 	irq_enable(DT_INST_IRQN(0));
73 
74 	return 0;
75 }
76 
sys_clock_set_timeout(int32_t ticks,bool idle)77 void sys_clock_set_timeout(int32_t ticks, bool idle)
78 {
79 	ARG_UNUSED(idle);
80 
81 	if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
82 		/* Only for tickless kernel system */
83 		return;
84 	}
85 
86 	ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks;
87 	ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
88 
89 	k_spinlock_key_t key = k_spin_lock(&lock);
90 	uint64_t now = OSTIMER_GetCurrentTimerValue(base);
91 	uint32_t adj, cyc = ticks * CYC_PER_TICK;
92 
93 	/* Round up to next tick boundary. */
94 	adj = (uint32_t)(now - last_count) + (CYC_PER_TICK - 1);
95 	if (cyc <= MAX_CYC - adj) {
96 		cyc += adj;
97 	} else {
98 		cyc = MAX_CYC;
99 	}
100 	cyc = (cyc / CYC_PER_TICK) * CYC_PER_TICK;
101 
102 	if ((int32_t)(cyc + last_count - now) < MIN_DELAY) {
103 		cyc += CYC_PER_TICK;
104 	}
105 
106 	OSTIMER_SetMatchValue(base, cyc + last_count, NULL);
107 
108 	k_spin_unlock(&lock, key);
109 }
110 
sys_clock_elapsed(void)111 uint32_t sys_clock_elapsed(void)
112 {
113 	if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
114 		/* Always return 0 for tickful kernel system */
115 		return 0;
116 	}
117 
118 	k_spinlock_key_t key = k_spin_lock(&lock);
119 	uint32_t ret = ((uint32_t)OSTIMER_GetCurrentTimerValue(base) -
120 					(uint32_t)last_count) / CYC_PER_TICK;
121 
122 	k_spin_unlock(&lock, key);
123 	return ret;
124 }
125 
sys_clock_cycle_get_32(void)126 uint32_t sys_clock_cycle_get_32(void)
127 {
128 	return (uint32_t)OSTIMER_GetCurrentTimerValue(base);
129 }
130