1 /*
2 * Copyright (c) 2021 Nuvoton Technology Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nuvoton_npcx_itim_timer
8
9 /**
10 * @file
11 * @brief Nuvoton NPCX kernel device driver for "system clock driver" interface
12 *
13 * This file contains a kernel device driver implemented by the internal
14 * 64/32-bit timers in Nuvoton NPCX series. Via these two kinds of timers, the
15 * driver provides an standard "system clock driver" interface.
16 *
17 * It includes:
18 * - A system timer based on an ITIM64 (Internal 64-bit timer) instance, clocked
19 * by APB2 which freq is the same as CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC.
20 * - Provide a 64-bit cycles reading and ticks computation based on it.
21 * - Its prescaler is set to 1 and provide the kernel cycles reading without
22 * handling overflow mechanism.
23 * - After ec entered "sleep/deep sleep" power state which is used for better
24 * power consumption, then its clock will stop.
25 *
26 * - A event timer based on an ITIM32 (Internal 32-bit timer) instance, clocked
27 * by LFCLK which frequency is 32KHz and still activated when ec entered
28 * "sleep/deep sleep" power state.
29 * - Provide a system clock timeout notification. In its ISR, the driver informs
30 * the kernel that the specified number of ticks have elapsed.
31 * - Its prescaler is set to 1 and the formula between event timer's cycles and
32 * ticks is 'cycles = (ticks * 32768) / CONFIG_SYS_CLOCK_TICKS_PER_SEC'
33 * - Compensate reading of ITIM64 which clock is gating after ec entered
34 * "sleep/deep sleep" power state if CONFIG_PM is enabled.
35 */
36
37 #include <zephyr/device.h>
38 #include <zephyr/drivers/clock_control.h>
39 #include <zephyr/drivers/timer/system_timer.h>
40 #include <zephyr/kernel.h>
41 #include <zephyr/sys_clock.h>
42 #include <zephyr/spinlock.h>
43 #include <soc.h>
44
45 #include <zephyr/logging/log.h>
46 #include <zephyr/irq.h>
47 LOG_MODULE_REGISTER(itim, LOG_LEVEL_ERR);
48
49 #define NPCX_ITIM32_MAX_CNT 0xffffffff
50 #define NPCX_ITIM64_MAX_HALF_CNT 0xffffffff
51 #define EVT_CYCLES_PER_SEC LFCLK /* 32768 Hz */
52 #define SYS_CYCLES_PER_TICK (sys_clock_hw_cycles_per_sec() \
53 / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
54 #define SYS_CYCLES_PER_USEC (sys_clock_hw_cycles_per_sec() / 1000000)
55 #define EVT_CYCLES_FROM_TICKS(ticks) \
56 DIV_ROUND_UP(ticks * EVT_CYCLES_PER_SEC, \
57 CONFIG_SYS_CLOCK_TICKS_PER_SEC)
58 #define NPCX_ITIM_CLK_SEL_DELAY 92 /* Delay for clock selection (Unit:us) */
59 /* Timeout for enabling ITIM module: 100us (Unit:cycles) */
60 #define NPCX_ITIM_EN_TIMEOUT_CYCLES (100 * SYS_CYCLES_PER_USEC)
61
62 /* Instance of system and event timers */
63 static struct itim64_reg *const sys_tmr = (struct itim64_reg *)
64 DT_INST_REG_ADDR_BY_NAME(0, sys_itim);
65 static struct itim32_reg *const evt_tmr = (struct itim32_reg *)
66 DT_INST_REG_ADDR_BY_NAME(0, evt_itim);
67
68 static const struct npcx_clk_cfg itim_clk_cfg[] = NPCX_DT_CLK_CFG_ITEMS_LIST(0);
69
70 static struct k_spinlock lock;
71 /* Announced cycles in system timer before executing sys_clock_announce() */
72 static uint64_t cyc_sys_announced;
73 /* Current target cycles of time-out signal in event timer */
74 static uint32_t cyc_evt_timeout;
75 /* Total cycles of system timer stopped in "sleep/deep sleep" mode */
76 __unused static uint64_t cyc_sys_compensated;
77 /* Current cycles in event timer when ec entered "sleep/deep sleep" mode */
78 __unused static uint32_t cyc_evt_enter_deep_idle;
79
80 /* ITIM local inline functions */
npcx_itim_get_sys_cyc64(void)81 static inline uint64_t npcx_itim_get_sys_cyc64(void)
82 {
83 uint32_t cnt64h, cnt64h_check, cnt64l;
84
85 /* Read 64-bit counter value from two 32-bit registers */
86 do {
87 cnt64h_check = sys_tmr->ITCNT64H;
88 cnt64l = sys_tmr->ITCNT64L;
89 cnt64h = sys_tmr->ITCNT64H;
90 } while (cnt64h != cnt64h_check);
91
92 cnt64h = NPCX_ITIM64_MAX_HALF_CNT - cnt64h;
93 cnt64l = NPCX_ITIM64_MAX_HALF_CNT - cnt64l + 1;
94
95 /* Return current value of 64-bit counter value of system timer */
96 if (IS_ENABLED(CONFIG_PM)) {
97 return ((((uint64_t)cnt64h) << 32) | cnt64l) +
98 cyc_sys_compensated;
99 } else {
100 return (((uint64_t)cnt64h) << 32) | cnt64l;
101 }
102 }
103
npcx_itim_evt_enable(void)104 static inline int npcx_itim_evt_enable(void)
105 {
106 uint64_t cyc_start;
107
108 /* Enable event timer and wait for it to take effect */
109 evt_tmr->ITCTS32 |= BIT(NPCX_ITCTSXX_ITEN);
110
111 /*
112 * Usually, it need one clock (30.5 us) to take effect since
113 * asynchronization between core and itim32's source clock (LFCLK).
114 */
115 cyc_start = npcx_itim_get_sys_cyc64();
116 while (!IS_BIT_SET(evt_tmr->ITCTS32, NPCX_ITCTSXX_ITEN)) {
117 if (npcx_itim_get_sys_cyc64() - cyc_start >
118 NPCX_ITIM_EN_TIMEOUT_CYCLES) {
119 /* ITEN bit is still unset? */
120 if (!IS_BIT_SET(evt_tmr->ITCTS32, NPCX_ITCTSXX_ITEN)) {
121 LOG_ERR("Timeout: enabling EVT timer!");
122 return -ETIMEDOUT;
123 }
124 }
125 }
126
127 return 0;
128 }
129
npcx_itim_evt_disable(void)130 static inline void npcx_itim_evt_disable(void)
131 {
132 /* Disable event timer and no need to wait for it to take effect */
133 evt_tmr->ITCTS32 &= ~BIT(NPCX_ITCTSXX_ITEN);
134 }
135
136 /* ITIM local functions */
npcx_itim_start_evt_tmr_by_tick(int32_t ticks)137 static int npcx_itim_start_evt_tmr_by_tick(int32_t ticks)
138 {
139 /*
140 * Get desired cycles of event timer from the requested ticks which
141 * round up to next tick boundary.
142 */
143 if (ticks == K_TICKS_FOREVER) {
144 cyc_evt_timeout = NPCX_ITIM32_MAX_CNT;
145 } else {
146 if (ticks <= 0) {
147 ticks = 1;
148 }
149 cyc_evt_timeout = MIN(EVT_CYCLES_FROM_TICKS(ticks),
150 NPCX_ITIM32_MAX_CNT);
151 }
152 LOG_DBG("ticks %x, cyc_evt_timeout %x", ticks, cyc_evt_timeout);
153
154 /* Disable event timer if needed before configuring counter */
155 if (IS_BIT_SET(evt_tmr->ITCTS32, NPCX_ITCTSXX_ITEN)) {
156 npcx_itim_evt_disable();
157 }
158
159 /* Upload counter of event timer */
160 evt_tmr->ITCNT32 = MAX(cyc_evt_timeout - 1, 1);
161
162 /* Enable event timer and start ticking */
163 return npcx_itim_evt_enable();
164
165 }
166
npcx_itim_evt_isr(const struct device * dev)167 static void npcx_itim_evt_isr(const struct device *dev)
168 {
169 ARG_UNUSED(dev);
170
171 /* Disable ITIM event module first */
172 npcx_itim_evt_disable();
173 /* Clear timeout status for event */
174 evt_tmr->ITCTS32 |= BIT(NPCX_ITCTSXX_TO_STS);
175
176 if (IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
177 k_spinlock_key_t key = k_spin_lock(&lock);
178 uint32_t delta_ticks = (uint32_t)((npcx_itim_get_sys_cyc64() -
179 cyc_sys_announced) / SYS_CYCLES_PER_TICK);
180
181 /* Store announced cycles of system timer */
182 cyc_sys_announced = npcx_itim_get_sys_cyc64();
183 k_spin_unlock(&lock, key);
184
185 /* Informs kernel that specified number of ticks have elapsed */
186 sys_clock_announce(delta_ticks);
187 } else {
188 /* Enable event timer for ticking and wait to it take effect */
189 npcx_itim_evt_enable();
190
191 /* Informs kernel that one tick has elapsed */
192 sys_clock_announce(1);
193 }
194 }
195
196 #if defined(CONFIG_PM)
npcx_itim_get_evt_cyc32(void)197 static inline uint32_t npcx_itim_get_evt_cyc32(void)
198 {
199 uint32_t cnt1, cnt2;
200
201 cnt1 = evt_tmr->ITCNT32;
202 /*
203 * Wait for two consecutive equal values are read since the source clock
204 * of event timer is 32KHz.
205 */
206 while ((cnt2 = evt_tmr->ITCNT32) != cnt1)
207 cnt1 = cnt2;
208
209 /* Return current value of 32-bit counter of event timer */
210 return cnt2;
211 }
212
npcx_itim_evt_elapsed_cyc32(void)213 static uint32_t npcx_itim_evt_elapsed_cyc32(void)
214 {
215 uint32_t cnt1 = npcx_itim_get_evt_cyc32();
216 uint8_t sys_cts = evt_tmr->ITCTS32;
217 uint16_t cnt2 = npcx_itim_get_evt_cyc32();
218
219 /* Event has been triggered but timer ISR doesn't handle it */
220 if (IS_BIT_SET(sys_cts, NPCX_ITCTSXX_TO_STS) || (cnt2 > cnt1)) {
221 cnt2 = cyc_evt_timeout;
222 } else {
223 cnt2 = cyc_evt_timeout - cnt2;
224 }
225
226 /* Return elapsed cycles of 32-bit counter of event timer */
227 return cnt2;
228 }
229 #endif /* CONFIG_PM */
230
231 /* System timer api functions */
sys_clock_set_timeout(int32_t ticks,bool idle)232 void sys_clock_set_timeout(int32_t ticks, bool idle)
233 {
234 ARG_UNUSED(idle);
235
236 if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
237 /* Only for tickless kernel system */
238 return;
239 }
240
241 LOG_DBG("timeout is %d", ticks);
242 /* Start a event timer in ticks */
243 npcx_itim_start_evt_tmr_by_tick(ticks);
244 }
245
sys_clock_elapsed(void)246 uint32_t sys_clock_elapsed(void)
247 {
248 if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
249 /* Always return 0 for tickful kernel system */
250 return 0;
251 }
252
253 k_spinlock_key_t key = k_spin_lock(&lock);
254 uint64_t delta_cycle = npcx_itim_get_sys_cyc64() - cyc_sys_announced;
255
256 k_spin_unlock(&lock, key);
257
258 /* Return how many ticks elapsed since last sys_clock_announce() call */
259 return (uint32_t)(delta_cycle / SYS_CYCLES_PER_TICK);
260 }
261
sys_clock_cycle_get_32(void)262 uint32_t sys_clock_cycle_get_32(void)
263 {
264 k_spinlock_key_t key = k_spin_lock(&lock);
265 uint64_t current = npcx_itim_get_sys_cyc64();
266
267 k_spin_unlock(&lock, key);
268
269 /* Return how many cycles since system kernel timer start counting */
270 return (uint32_t)(current);
271 }
272
sys_clock_cycle_get_64(void)273 uint64_t sys_clock_cycle_get_64(void)
274 {
275 k_spinlock_key_t key = k_spin_lock(&lock);
276 uint64_t current = npcx_itim_get_sys_cyc64();
277
278 k_spin_unlock(&lock, key);
279
280 /* Return how many cycles since system kernel timer start counting */
281 return current;
282 }
283
284 /* Platform specific system timer functions */
285 #if defined(CONFIG_PM)
npcx_clock_capture_low_freq_timer(void)286 void npcx_clock_capture_low_freq_timer(void)
287 {
288 cyc_evt_enter_deep_idle = npcx_itim_evt_elapsed_cyc32();
289 }
290
npcx_clock_compensate_system_timer(void)291 void npcx_clock_compensate_system_timer(void)
292 {
293 uint32_t cyc_evt_elapsed_in_deep = npcx_itim_evt_elapsed_cyc32() -
294 cyc_evt_enter_deep_idle;
295
296 cyc_sys_compensated += ((uint64_t)cyc_evt_elapsed_in_deep *
297 sys_clock_hw_cycles_per_sec()) / EVT_CYCLES_PER_SEC;
298 }
299
npcx_clock_get_sleep_ticks(void)300 uint64_t npcx_clock_get_sleep_ticks(void)
301 {
302 return cyc_sys_compensated / SYS_CYCLES_PER_TICK;
303 }
304 #endif /* CONFIG_PM */
305
sys_clock_driver_init(void)306 static int sys_clock_driver_init(void)
307 {
308 int ret;
309 uint32_t sys_tmr_rate;
310 const struct device *const clk_dev = DEVICE_DT_GET(NPCX_CLK_CTRL_NODE);
311
312 if (!device_is_ready(clk_dev)) {
313 LOG_ERR("clock control device not ready");
314 return -ENODEV;
315 }
316
317 /* Turn on all itim module clocks used for counting */
318 for (int i = 0; i < ARRAY_SIZE(itim_clk_cfg); i++) {
319 ret = clock_control_on(clk_dev, (clock_control_subsys_t)
320 &itim_clk_cfg[i]);
321 if (ret < 0) {
322 LOG_ERR("Turn on timer %d clock failed.", i);
323 return ret;
324 }
325 }
326
327 /*
328 * In npcx series, we use ITIM64 as system kernel timer. Its source
329 * clock frequency must equal to CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC.
330 */
331 ret = clock_control_get_rate(clk_dev, (clock_control_subsys_t)
332 &itim_clk_cfg[1], &sys_tmr_rate);
333 if (ret < 0) {
334 LOG_ERR("Get ITIM64 clock rate failed %d", ret);
335 return ret;
336 }
337
338 if (sys_tmr_rate != CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC) {
339 LOG_ERR("CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC doesn't match "
340 "ITIM64 clock frequency %d", sys_tmr_rate);
341 return -EINVAL;
342 }
343
344 /*
345 * Step 1. Use a ITIM64 timer as system kernel timer for counting.
346 * Configure 64-bit timer counter and its prescaler to 1 first.
347 */
348 sys_tmr->ITPRE64 = 0;
349 sys_tmr->ITCNT64L = NPCX_ITIM64_MAX_HALF_CNT;
350 sys_tmr->ITCNT64H = NPCX_ITIM64_MAX_HALF_CNT;
351 /*
352 * Select APB2 clock which freq is CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC,
353 * and clear timeout status bit before enabling the whole module.
354 */
355 sys_tmr->ITCTS64 = BIT(NPCX_ITCTSXX_TO_STS);
356 /* Enable 64-bit timer and start ticking */
357 sys_tmr->ITCTS64 |= BIT(NPCX_ITCTSXX_ITEN);
358
359 /*
360 * Step 2. Use a ITIM32 timer for event handling (ex. timeout event).
361 * Configure 32-bit timer's prescaler to 1 first.
362 */
363 evt_tmr->ITPRE32 = 0;
364 /*
365 * Select low frequency clock source (The freq is 32kHz), enable its
366 * interrupt/wake-up sources, and clear timeout status bit before
367 * enabling it.
368 */
369 evt_tmr->ITCTS32 = BIT(NPCX_ITCTSXX_CKSEL) | BIT(NPCX_ITCTSXX_TO_WUE)
370 | BIT(NPCX_ITCTSXX_TO_IE) | BIT(NPCX_ITCTSXX_TO_STS);
371
372 /* A delay for ITIM source clock selection */
373 k_busy_wait(NPCX_ITIM_CLK_SEL_DELAY);
374
375 /* Configure event timer's ISR */
376 IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
377 npcx_itim_evt_isr, NULL, 0);
378 /* Enable event timer interrupt */
379 irq_enable(DT_INST_IRQN(0));
380
381 if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
382 /* Start a event timer in one tick */
383 ret = npcx_itim_start_evt_tmr_by_tick(1);
384 if (ret < 0) {
385 return ret;
386 }
387 }
388
389 return 0;
390 }
391 SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2,
392 CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
393