1 /*
2 * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <soc.h>
7 #include <zephyr/drivers/counter.h>
8 #include <zephyr/drivers/clock_control/nrf_clock_control.h>
9 #include <zephyr/devicetree.h>
10 #include <hal/nrf_timer.h>
11 #include <zephyr/sys/atomic.h>
12
13 #define LOG_LEVEL CONFIG_COUNTER_LOG_LEVEL
14 #define LOG_MODULE_NAME counter_timer
15 #include <zephyr/logging/log.h>
16 #include <zephyr/irq.h>
17 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL);
18
19 #define DT_DRV_COMPAT nordic_nrf_timer
20
21 #define CC_TO_ID(cc_num) (cc_num - 2)
22
23 #define ID_TO_CC(idx) (nrf_timer_cc_channel_t)(idx + 2)
24
25 #define TOP_CH NRF_TIMER_CC_CHANNEL0
26 #define COUNTER_TOP_EVT NRF_TIMER_EVENT_COMPARE0
27 #define COUNTER_TOP_INT_MASK NRF_TIMER_INT_COMPARE0_MASK
28
29 #define COUNTER_OVERFLOW_SHORT NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK
30 #define COUNTER_READ_CC NRF_TIMER_CC_CHANNEL1
31
32 #if defined(CONFIG_SOC_SERIES_BSIM_NRFXX)
33 #define MAYBE_CONST_CONFIG
34 #else
35 #define MAYBE_CONST_CONFIG const
36 #endif
37
38 #ifdef CONFIG_SOC_NRF54H20_GPD
39 #include <nrf/gpd.h>
40
41 #define NRF_CLOCKS_INSTANCE_IS_FAST(node) \
42 COND_CODE_1(DT_NODE_HAS_PROP(node, power_domains), \
43 (IS_EQ(DT_PHA(node, power_domains, id), NRF_GPD_FAST_ACTIVE1)), \
44 (0))
45
46 /* Macro must resolve to literal 0 or 1 */
47 #define INSTANCE_IS_FAST(idx) NRF_CLOCKS_INSTANCE_IS_FAST(DT_DRV_INST(idx))
48
49 #define INSTANCE_IS_FAST_OR(idx) INSTANCE_IS_FAST(idx) ||
50
51 #if (DT_INST_FOREACH_STATUS_OKAY(INSTANCE_IS_FAST_OR) 0)
52 #define COUNTER_ANY_FAST 1
53 #endif
54 #endif
55
56 struct counter_nrfx_data {
57 counter_top_callback_t top_cb;
58 void *top_user_data;
59 uint32_t guard_period;
60 atomic_t cc_int_pending;
61 #ifdef COUNTER_ANY_FAST
62 atomic_t active;
63 #endif
64 };
65
66 struct counter_nrfx_ch_data {
67 counter_alarm_callback_t callback;
68 void *user_data;
69 };
70
71 struct counter_nrfx_config {
72 struct counter_config_info info;
73 struct counter_nrfx_ch_data *ch_data;
74 NRF_TIMER_Type *timer;
75 #ifdef COUNTER_ANY_FAST
76 const struct device *clk_dev;
77 struct nrf_clock_spec clk_spec;
78 #endif
79 LOG_INSTANCE_PTR_DECLARE(log);
80 };
81
82 struct counter_timer_config {
83 nrf_timer_bit_width_t bit_width;
84 nrf_timer_mode_t mode;
85 uint32_t prescaler;
86 };
87
start(const struct device * dev)88 static int start(const struct device *dev)
89 {
90 const struct counter_nrfx_config *config = dev->config;
91
92 #ifdef COUNTER_ANY_FAST
93 struct counter_nrfx_data *data = dev->data;
94
95 if (config->clk_dev && atomic_cas(&data->active, 0, 1)) {
96 int err;
97
98 err = nrf_clock_control_request_sync(config->clk_dev, &config->clk_spec, K_FOREVER);
99 if (err < 0) {
100 return err;
101 }
102 }
103 #endif
104 nrf_timer_task_trigger(config->timer, NRF_TIMER_TASK_START);
105
106 return 0;
107 }
108
stop(const struct device * dev)109 static int stop(const struct device *dev)
110 {
111 const struct counter_nrfx_config *config = dev->config;
112
113 nrf_timer_task_trigger(config->timer, NRF_TIMER_TASK_STOP);
114 #ifdef COUNTER_ANY_FAST
115 struct counter_nrfx_data *data = dev->data;
116
117 if (config->clk_dev && atomic_cas(&data->active, 1, 0)) {
118 int err;
119
120 err = nrf_clock_control_release(config->clk_dev, &config->clk_spec);
121 if (err < 0) {
122 return err;
123 }
124 }
125 #endif
126
127 return 0;
128 }
129
get_top_value(const struct device * dev)130 static uint32_t get_top_value(const struct device *dev)
131 {
132 const struct counter_nrfx_config *config = dev->config;
133
134 return nrf_timer_cc_get(config->timer, TOP_CH);
135 }
136
read(const struct device * dev)137 static uint32_t read(const struct device *dev)
138 {
139 const struct counter_nrfx_config *config = dev->config;
140 NRF_TIMER_Type *timer = config->timer;
141
142 nrf_timer_task_trigger(timer,
143 nrf_timer_capture_task_get(COUNTER_READ_CC));
144 nrf_barrier_w();
145
146 return nrf_timer_cc_get(timer, COUNTER_READ_CC);
147 }
148
get_value(const struct device * dev,uint32_t * ticks)149 static int get_value(const struct device *dev, uint32_t *ticks)
150 {
151 *ticks = read(dev);
152 return 0;
153 }
154
ticks_add(uint32_t val1,uint32_t val2,uint32_t top)155 static uint32_t ticks_add(uint32_t val1, uint32_t val2, uint32_t top)
156 {
157 uint32_t to_top;
158
159 if (likely(IS_BIT_MASK(top))) {
160 return (val1 + val2) & top;
161 }
162
163 to_top = top - val1;
164
165 return (val2 <= to_top) ? val1 + val2 : val2 - to_top;
166 }
167
ticks_sub(uint32_t val,uint32_t old,uint32_t top)168 static uint32_t ticks_sub(uint32_t val, uint32_t old, uint32_t top)
169 {
170 if (likely(IS_BIT_MASK(top))) {
171 return (val - old) & top;
172 }
173
174 /* if top is not 2^n-1 */
175 return (val >= old) ? (val - old) : val + top + 1 - old;
176 }
177
set_cc_int_pending(const struct device * dev,uint8_t chan)178 static void set_cc_int_pending(const struct device *dev, uint8_t chan)
179 {
180 const struct counter_nrfx_config *config = dev->config;
181 struct counter_nrfx_data *data = dev->data;
182
183 atomic_or(&data->cc_int_pending, BIT(chan));
184 NRFX_IRQ_PENDING_SET(NRFX_IRQ_NUMBER_GET(config->timer));
185 }
186
set_cc(const struct device * dev,uint8_t id,uint32_t val,uint32_t flags)187 static int set_cc(const struct device *dev, uint8_t id, uint32_t val,
188 uint32_t flags)
189 {
190 const struct counter_nrfx_config *config = dev->config;
191 struct counter_nrfx_data *data = dev->data;
192
193 __ASSERT_NO_MSG(data->guard_period < get_top_value(dev));
194 bool absolute = flags & COUNTER_ALARM_CFG_ABSOLUTE;
195 bool irq_on_late;
196 NRF_TIMER_Type *reg = config->timer;
197 uint8_t chan = ID_TO_CC(id);
198 nrf_timer_event_t evt = nrf_timer_compare_event_get(chan);
199 uint32_t top = get_top_value(dev);
200 int err = 0;
201 uint32_t prev_val;
202 uint32_t now;
203 uint32_t diff;
204 uint32_t max_rel_val;
205
206 __ASSERT(nrf_timer_int_enable_check(reg,
207 nrf_timer_compare_int_get(chan)) == 0,
208 "Expected that CC interrupt is disabled.");
209
210 /* First take care of a risk of an event coming from CC being set to
211 * next tick. Reconfigure CC to future (now tick is the furthest
212 * future).
213 */
214 now = read(dev);
215 prev_val = nrf_timer_cc_get(reg, chan);
216 nrf_barrier_r();
217 nrf_timer_cc_set(reg, chan, now);
218 nrf_timer_event_clear(reg, evt);
219
220 if (absolute) {
221 max_rel_val = top - data->guard_period;
222 irq_on_late = flags & COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE;
223 } else {
224 /* If relative value is smaller than half of the counter range
225 * it is assumed that there is a risk of setting value too late
226 * and late detection algorithm must be applied. When late
227 * setting is detected, interrupt shall be triggered for
228 * immediate expiration of the timer. Detection is performed
229 * by limiting relative distance between CC and counter.
230 *
231 * Note that half of counter range is an arbitrary value.
232 */
233 irq_on_late = val < (top / 2);
234 /* limit max to detect short relative being set too late. */
235 max_rel_val = irq_on_late ? top / 2 : top;
236 val = ticks_add(now, val, top);
237 }
238
239 nrf_timer_cc_set(reg, chan, val);
240
241 /* decrement value to detect also case when val == read(dev). Otherwise,
242 * condition would need to include comparing diff against 0.
243 */
244 diff = ticks_sub(val - 1, read(dev), top);
245 if (diff > max_rel_val) {
246 if (absolute) {
247 err = -ETIME;
248 }
249
250 /* Interrupt is triggered always for relative alarm and
251 * for absolute depending on the flag.
252 */
253 if (irq_on_late) {
254 set_cc_int_pending(dev, chan);
255 } else {
256 config->ch_data[id].callback = NULL;
257 }
258 } else {
259 nrf_timer_int_enable(reg, nrf_timer_compare_int_get(chan));
260 }
261
262 return err;
263 }
264
set_alarm(const struct device * dev,uint8_t chan,const struct counter_alarm_cfg * alarm_cfg)265 static int set_alarm(const struct device *dev, uint8_t chan,
266 const struct counter_alarm_cfg *alarm_cfg)
267 {
268 const struct counter_nrfx_config *nrfx_config = dev->config;
269 struct counter_nrfx_ch_data *chdata = &nrfx_config->ch_data[chan];
270
271 if (alarm_cfg->ticks > get_top_value(dev)) {
272 return -EINVAL;
273 }
274
275 if (chdata->callback) {
276 return -EBUSY;
277 }
278
279 chdata->callback = alarm_cfg->callback;
280 chdata->user_data = alarm_cfg->user_data;
281
282 return set_cc(dev, chan, alarm_cfg->ticks, alarm_cfg->flags);
283 }
284
cancel_alarm(const struct device * dev,uint8_t chan_id)285 static int cancel_alarm(const struct device *dev, uint8_t chan_id)
286 {
287 const struct counter_nrfx_config *config = dev->config;
288 uint32_t int_mask = nrf_timer_compare_int_get(ID_TO_CC(chan_id));
289
290 nrf_timer_int_disable(config->timer, int_mask);
291 config->ch_data[chan_id].callback = NULL;
292
293 return 0;
294 }
295
set_top_value(const struct device * dev,const struct counter_top_cfg * cfg)296 static int set_top_value(const struct device *dev,
297 const struct counter_top_cfg *cfg)
298 {
299 const struct counter_nrfx_config *nrfx_config = dev->config;
300 NRF_TIMER_Type *timer = nrfx_config->timer;
301 struct counter_nrfx_data *data = dev->data;
302 int err = 0;
303 for (int i = 0; i < counter_get_num_of_channels(dev); i++) {
304 /* Overflow can be changed only when all alarms are
305 * disables.
306 */
307 if (nrfx_config->ch_data[i].callback) {
308 return -EBUSY;
309 }
310 }
311
312 nrf_timer_int_disable(timer, COUNTER_TOP_INT_MASK);
313 nrf_timer_cc_set(timer, TOP_CH, cfg->ticks);
314 nrf_timer_event_clear(timer, COUNTER_TOP_EVT);
315 nrf_timer_shorts_enable(timer, COUNTER_OVERFLOW_SHORT);
316
317 data->top_cb = cfg->callback;
318 data->top_user_data = cfg->user_data;
319
320 if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) {
321 nrf_timer_task_trigger(timer, NRF_TIMER_TASK_CLEAR);
322 } else if (read(dev) >= cfg->ticks) {
323 err = -ETIME;
324 if (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE) {
325 nrf_timer_task_trigger(timer, NRF_TIMER_TASK_CLEAR);
326 }
327 }
328
329 if (cfg->callback) {
330 nrf_timer_int_enable(timer, COUNTER_TOP_INT_MASK);
331 }
332
333 return err;
334 }
335
get_pending_int(const struct device * dev)336 static uint32_t get_pending_int(const struct device *dev)
337 {
338 return 0;
339 }
340
init_timer(const struct device * dev,const struct counter_timer_config * config)341 static int init_timer(const struct device *dev,
342 const struct counter_timer_config *config)
343 {
344 MAYBE_CONST_CONFIG struct counter_nrfx_config *nrfx_config =
345 (MAYBE_CONST_CONFIG struct counter_nrfx_config *)dev->config;
346
347 #if defined(CONFIG_SOC_SERIES_BSIM_NRFXX)
348 /* For simulated devices we need to convert the hardcoded DT address from the real
349 * peripheral into the correct one for simulation
350 */
351 nrfx_config->timer = nhw_convert_periph_base_addr(nrfx_config->timer);
352 #endif
353
354 NRF_TIMER_Type *reg = nrfx_config->timer;
355
356 nrf_timer_bit_width_set(reg, config->bit_width);
357 nrf_timer_mode_set(reg, config->mode);
358 nrf_timer_prescaler_set(reg, config->prescaler);
359
360 nrf_timer_cc_set(reg, TOP_CH, counter_get_max_top_value(dev));
361
362 NRFX_IRQ_ENABLE(NRFX_IRQ_NUMBER_GET(reg));
363
364 return 0;
365 }
366
get_guard_period(const struct device * dev,uint32_t flags)367 static uint32_t get_guard_period(const struct device *dev, uint32_t flags)
368 {
369 struct counter_nrfx_data *data = dev->data;
370
371 return data->guard_period;
372 }
373
set_guard_period(const struct device * dev,uint32_t guard,uint32_t flags)374 static int set_guard_period(const struct device *dev, uint32_t guard,
375 uint32_t flags)
376 {
377 struct counter_nrfx_data *data = dev->data;
378
379 __ASSERT_NO_MSG(guard < get_top_value(dev));
380
381 data->guard_period = guard;
382 return 0;
383 }
384
top_irq_handle(const struct device * dev)385 static void top_irq_handle(const struct device *dev)
386 {
387 const struct counter_nrfx_config *config = dev->config;
388 struct counter_nrfx_data *data = dev->data;
389
390 NRF_TIMER_Type *reg = config->timer;
391 counter_top_callback_t cb = data->top_cb;
392
393 if (nrf_timer_event_check(reg, COUNTER_TOP_EVT) &&
394 nrf_timer_int_enable_check(reg, COUNTER_TOP_INT_MASK)) {
395 nrf_timer_event_clear(reg, COUNTER_TOP_EVT);
396 __ASSERT(cb != NULL, "top event enabled - expecting callback");
397 cb(dev, data->top_user_data);
398 }
399 }
400
alarm_irq_handle(const struct device * dev,uint32_t id)401 static void alarm_irq_handle(const struct device *dev, uint32_t id)
402 {
403 const struct counter_nrfx_config *config = dev->config;
404 struct counter_nrfx_data *data = dev->data;
405
406 uint32_t cc = ID_TO_CC(id);
407 NRF_TIMER_Type *reg = config->timer;
408 uint32_t int_mask = nrf_timer_compare_int_get(cc);
409 nrf_timer_event_t evt = nrf_timer_compare_event_get(cc);
410 bool hw_irq_pending = nrf_timer_event_check(reg, evt) &&
411 nrf_timer_int_enable_check(reg, int_mask);
412 bool sw_irq_pending = data->cc_int_pending & BIT(cc);
413
414 if (hw_irq_pending || sw_irq_pending) {
415 struct counter_nrfx_ch_data *chdata;
416 counter_alarm_callback_t cb;
417
418 nrf_timer_event_clear(reg, evt);
419 atomic_and(&data->cc_int_pending, ~BIT(cc));
420 nrf_timer_int_disable(reg, int_mask);
421
422 chdata = &config->ch_data[id];
423 cb = chdata->callback;
424 chdata->callback = NULL;
425
426 if (cb) {
427 uint32_t cc_val = nrf_timer_cc_get(reg, cc);
428
429 cb(dev, id, cc_val, chdata->user_data);
430 }
431 }
432 }
433
irq_handler(const void * arg)434 static void irq_handler(const void *arg)
435 {
436 const struct device *dev = arg;
437
438 top_irq_handle(dev);
439
440 for (uint32_t i = 0; i < counter_get_num_of_channels(dev); i++) {
441 alarm_irq_handle(dev, i);
442 }
443 }
444
445 static DEVICE_API(counter, counter_nrfx_driver_api) = {
446 .start = start,
447 .stop = stop,
448 .get_value = get_value,
449 .set_alarm = set_alarm,
450 .cancel_alarm = cancel_alarm,
451 .set_top_value = set_top_value,
452 .get_pending_int = get_pending_int,
453 .get_top_value = get_top_value,
454 .get_guard_period = get_guard_period,
455 .set_guard_period = set_guard_period,
456 };
457
458 /* Get initialization level of an instance. Instances that requires clock control
459 * which is using nrfs (IPC) are initialized later.
460 */
461 #define TIMER_INIT_LEVEL(idx) \
462 COND_CODE_1(INSTANCE_IS_FAST(idx), (POST_KERNEL), (PRE_KERNEL_1))
463
464 /* Get initialization priority of an instance. Instances that requires clock control
465 * which is using nrfs (IPC) are initialized later.
466 */
467 #define TIMER_INIT_PRIO(idx) \
468 COND_CODE_1(INSTANCE_IS_FAST(idx), \
469 (UTIL_INC(CONFIG_CLOCK_CONTROL_NRF2_GLOBAL_HSFLL_INIT_PRIORITY)), \
470 (CONFIG_COUNTER_INIT_PRIORITY))
471
472 /*
473 * Device instantiation is done with node labels due to HAL API
474 * requirements. In particular, TIMERx_MAX_SIZE values from HALs
475 * are indexed by peripheral number, so DT_INST APIs won't work.
476 */
477
478 #define TIMER_IRQ_CONNECT(idx) \
479 COND_CODE_1(DT_INST_PROP(idx, zli), \
480 (IRQ_DIRECT_CONNECT(DT_INST_IRQN(idx), \
481 DT_INST_IRQ(idx, priority), \
482 counter_timer##idx##_isr_wrapper, \
483 IRQ_ZERO_LATENCY)), \
484 (IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), \
485 irq_handler, DEVICE_DT_INST_GET(idx), 0)) \
486 )
487
488 #define COUNTER_NRFX_TIMER_DEVICE(idx) \
489 BUILD_ASSERT(DT_INST_PROP(idx, prescaler) <= \
490 TIMER_PRESCALER_PRESCALER_Msk, \
491 "TIMER prescaler out of range"); \
492 COND_CODE_1(DT_INST_PROP(idx, zli), ( \
493 ISR_DIRECT_DECLARE(counter_timer##idx##_isr_wrapper) \
494 { \
495 irq_handler(DEVICE_DT_INST_GET(idx)); \
496 /* No rescheduling, it shall not access zephyr primitives. */ \
497 return 0; \
498 }), ()) \
499 static int counter_##idx##_init(const struct device *dev) \
500 { \
501 TIMER_IRQ_CONNECT(idx); \
502 static const struct counter_timer_config config = { \
503 .prescaler = DT_INST_PROP(idx, prescaler), \
504 .mode = NRF_TIMER_MODE_TIMER, \
505 .bit_width = (DT_INST_PROP(idx, max_bit_width) == 32) ? \
506 NRF_TIMER_BIT_WIDTH_32 : NRF_TIMER_BIT_WIDTH_16, \
507 }; \
508 return init_timer(dev, &config); \
509 } \
510 static struct counter_nrfx_data counter_##idx##_data; \
511 static struct counter_nrfx_ch_data \
512 counter##idx##_ch_data[CC_TO_ID(DT_INST_PROP(idx, cc_num))]; \
513 LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \
514 static MAYBE_CONST_CONFIG struct counter_nrfx_config nrfx_counter_##idx##_config = { \
515 .info = { \
516 .max_top_value = (uint32_t)BIT64_MASK(DT_INST_PROP(idx, max_bit_width)),\
517 .freq = NRF_PERIPH_GET_FREQUENCY(DT_DRV_INST(idx)) / \
518 BIT(DT_INST_PROP(idx, prescaler)), \
519 .flags = COUNTER_CONFIG_INFO_COUNT_UP, \
520 .channels = CC_TO_ID(DT_INST_PROP(idx, cc_num)), \
521 }, \
522 .ch_data = counter##idx##_ch_data, \
523 .timer = (NRF_TIMER_Type *)DT_INST_REG_ADDR(idx), \
524 IF_ENABLED(INSTANCE_IS_FAST(idx), \
525 (.clk_dev = DEVICE_DT_GET(DT_CLOCKS_CTLR(DT_DRV_INST(idx))), \
526 .clk_spec = { \
527 .frequency = NRF_PERIPH_GET_FREQUENCY(DT_DRV_INST(idx)), \
528 .accuracy = 0, \
529 .precision = NRF_CLOCK_CONTROL_PRECISION_DEFAULT, \
530 }, \
531 )) \
532 LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \
533 }; \
534 DEVICE_DT_INST_DEFINE(idx, \
535 counter_##idx##_init, \
536 NULL, \
537 &counter_##idx##_data, \
538 &nrfx_counter_##idx##_config.info, \
539 TIMER_INIT_LEVEL(idx), TIMER_INIT_PRIO(idx), \
540 &counter_nrfx_driver_api);
541
542 DT_INST_FOREACH_STATUS_OKAY(COUNTER_NRFX_TIMER_DEVICE)
543