Skip to content

Commit 8a5c578

Browse files
adamkondraciukgmarull
authored andcommitted
[nrf fromlist] drivers: timer: grtc: Update GRTC driver
This commit aligns the GRTC driver to changes introduced in hal_nordic. Some of the features regarding GRTC sleep/wakeup functionality has been modified and moved out to the nrfx driver's code. Upstream PR: zephyrproject-rtos/zephyr#71688 Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent 2570119 commit 8a5c578

File tree

3 files changed

+45
-70
lines changed

3 files changed

+45
-70
lines changed

drivers/timer/Kconfig.nrf_grtc

+12-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@ config NRF_GRTC_TIMER_CLOCK_MANAGEMENT
4040
the GRTC. Usually this is only needed by the processor that is starting
4141
the SYSCOUNTER, but can be shared by multiple processors in the system.
4242

43-
config NRF_GRTC_SLEEP_MINIMUM_LATENCY
43+
config NRF_GRTC_SYSCOUNTER_SLEEP_MINIMUM_LATENCY
4444
int
4545
default 1000
4646
depends on NRF_GRTC_SLEEP_ALLOWED
47+
help
48+
The value (in us) ensures that the wakeup event will not fire
49+
too early. In other words, applying SYSCOUNTER sleep state for less than
50+
NRF_GRTC_SYSCOUNTER_SLEEP_MINIMUM_LATENCY period makes no sense.
51+
52+
config NRF_GRTC_TIMER_AUTO_KEEP_ALIVE
53+
bool
54+
default y if NRF_GRTC_START_SYSCOUNTER
55+
help
56+
This feature prevents the SYSCOUNTER to sleep when any core is in
57+
active state.
4758

4859
endif # NRF_GRTC_TIMER

drivers/timer/nrf_grtc_timer.c

+20-67
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929

3030
#define CHAN_COUNT NRFX_GRTC_CONFIG_NUM_OF_CC_CHANNELS
3131
#define EXT_CHAN_COUNT (CHAN_COUNT - 1)
32-
/* The reset value of waketime is 1, which doesn't seem to work.
33-
* It's being looked into, but for the time being use 4.
34-
* Timeout must always be higher than waketime, so setting that to 5.
35-
*/
36-
#define WAKETIME (4)
37-
#define TIMEOUT (WAKETIME + 1)
3832

3933
#ifndef GRTC_SYSCOUNTERL_VALUE_Msk
4034
#define GRTC_SYSCOUNTERL_VALUE_Msk GRTC_SYSCOUNTER_SYSCOUNTERL_VALUE_Msk
@@ -55,9 +49,6 @@
5549

5650
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
5751

58-
/* The maximum SYSCOUNTERVALID settling time equals 1x32k cycles + 20x1MHz cycles. */
59-
#define GRTC_SYSCOUNTERVALID_SETTLE_MAX_TIME_US 51
60-
6152
#if defined(CONFIG_TEST)
6253
const int32_t z_sys_timer_irq_for_test = DT_IRQN(GRTC_NODE);
6354
#endif
@@ -78,36 +69,6 @@ static nrfx_grtc_channel_t system_clock_channel_data = {
7869
__ASSERT_NO_MSG((NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK & (1UL << (chan))) && \
7970
((chan) != system_clock_channel_data.channel))
8071

81-
static inline void grtc_active_set(void)
82-
{
83-
#if defined(NRF_GRTC_HAS_SYSCOUNTER_ARRAY) && (NRF_GRTC_HAS_SYSCOUNTER_ARRAY == 1)
84-
nrfy_grtc_sys_counter_active_set(NRF_GRTC, true);
85-
while (!nrfy_grtc_sys_conter_ready_check(NRF_GRTC)) {
86-
}
87-
#else
88-
nrfy_grtc_sys_counter_active_state_request_set(NRF_GRTC, true);
89-
k_busy_wait(GRTC_SYSCOUNTERVALID_SETTLE_MAX_TIME_US);
90-
#endif
91-
}
92-
93-
static inline void grtc_wakeup(void)
94-
{
95-
if (IS_ENABLED(CONFIG_NRF_GRTC_SLEEP_ALLOWED)) {
96-
grtc_active_set();
97-
}
98-
}
99-
100-
static inline void grtc_sleep(void)
101-
{
102-
if (IS_ENABLED(CONFIG_NRF_GRTC_SLEEP_ALLOWED)) {
103-
#if defined(NRF_GRTC_HAS_SYSCOUNTER_ARRAY) && (NRF_GRTC_HAS_SYSCOUNTER_ARRAY == 1)
104-
nrfy_grtc_sys_counter_active_set(NRF_GRTC, false);
105-
#else
106-
nrfy_grtc_sys_counter_active_state_request_set(NRF_GRTC, false);
107-
#endif
108-
}
109-
}
110-
11172
static inline uint64_t counter_sub(uint64_t a, uint64_t b)
11273
{
11374
return (a - b);
@@ -116,10 +77,7 @@ static inline uint64_t counter_sub(uint64_t a, uint64_t b)
11677
static inline uint64_t counter(void)
11778
{
11879
uint64_t now;
119-
120-
grtc_wakeup();
12180
nrfx_grtc_syscounter_get(&now);
122-
grtc_sleep();
12381
return now;
12482
}
12583

@@ -141,10 +99,8 @@ static inline uint64_t get_comparator(uint32_t chan)
14199
static void system_timeout_set(uint64_t value)
142100
{
143101
if (value <= NRF_GRTC_SYSCOUNTER_CCADD_MASK) {
144-
grtc_wakeup();
145102
nrfx_grtc_syscounter_cc_relative_set(&system_clock_channel_data, value, true,
146103
NRFX_GRTC_CC_RELATIVE_SYSCOUNTER);
147-
grtc_sleep();
148104
} else {
149105
nrfx_grtc_syscounter_cc_absolute_set(&system_clock_channel_data, value + counter(),
150106
true);
@@ -373,6 +329,7 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
373329
*/
374330

375331
uint64_t capt_time;
332+
nrfx_err_t result;
376333

377334
IS_CHANNEL_ALLOWED_ASSERT(chan);
378335

@@ -383,8 +340,10 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
383340
*/
384341
return -EBUSY;
385342
}
386-
387-
capt_time = nrfy_grtc_sys_counter_cc_get(NRF_GRTC, chan);
343+
result = nrfx_grtc_syscounter_cc_value_read(chan, &capt_time);
344+
if (result != NRFX_SUCCESS) {
345+
return -EPERM;
346+
}
388347

389348
__ASSERT_NO_MSG(capt_time < COUNTER_SPAN);
390349

@@ -399,16 +358,22 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
399358
nrfx_err_t err_code;
400359
static uint8_t systemoff_channel;
401360
uint64_t now = counter();
361+
nrfx_grtc_sleep_config_t sleep_cfg;
402362
/* Minimum time that ensures valid execution of system-off procedure. */
403-
uint32_t minimum_latency_us = nrfy_grtc_waketime_get(NRF_GRTC) +
404-
nrfy_grtc_timeout_get(NRF_GRTC) +
405-
CONFIG_NRF_GRTC_SLEEP_MINIMUM_LATENCY;
363+
uint32_t minimum_latency_us;
406364
uint32_t chan;
407365
int ret;
408366

367+
nrfx_grtc_sleep_configuration_get(&sleep_cfg);
368+
minimum_latency_us = (sleep_cfg.waketime + sleep_cfg.timeout) * USEC_PER_SEC / 32768 +
369+
CONFIG_NRF_GRTC_SYSCOUNTER_SLEEP_MINIMUM_LATENCY;
370+
sleep_cfg.auto_mode = false;
371+
nrfx_grtc_sleep_configure(&sleep_cfg);
372+
409373
if (minimum_latency_us > wake_time_us) {
410374
return -EINVAL;
411375
}
376+
412377
k_spinlock_key_t key = k_spin_lock(&lock);
413378

414379
err_code = nrfx_grtc_channel_alloc(&systemoff_channel);
@@ -417,7 +382,9 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
417382
return -ENOMEM;
418383
}
419384
(void)nrfx_grtc_syscounter_cc_int_disable(systemoff_channel);
420-
ret = compare_set(systemoff_channel, now + wake_time_us, NULL, NULL);
385+
ret = compare_set(systemoff_channel,
386+
now + wake_time_us * sys_clock_hw_cycles_per_sec() / USEC_PER_SEC, NULL,
387+
NULL);
421388
if (ret < 0) {
422389
k_spin_unlock(&lock, key);
423390
return ret;
@@ -433,7 +400,7 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
433400
}
434401

435402
/* Make sure that wake_time_us was not triggered yet. */
436-
if (nrfy_grtc_sys_counter_compare_event_check(NRF_GRTC, systemoff_channel)) {
403+
if (nrfx_grtc_syscounter_compare_event_check(systemoff_channel)) {
437404
k_spin_unlock(&lock, key);
438405
return -EINVAL;
439406
}
@@ -444,7 +411,7 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
444411
MAX_CC_LATCH_WAIT_TIME_US;
445412
k_busy_wait(wait_time);
446413
#if NRF_GRTC_HAS_CLKSEL
447-
nrfy_grtc_clksel_set(NRF_GRTC, NRF_GRTC_CLKSEL_LFXO);
414+
nrfx_grtc_clock_source_set(NRF_GRTC_CLKSEL_LFXO);
448415
#endif
449416
k_spin_unlock(&lock, key);
450417
return 0;
@@ -485,16 +452,9 @@ static int sys_clock_driver_init(void)
485452
#if defined(CONFIG_NRF_GRTC_TIMER_CLOCK_MANAGEMENT) && \
486453
(defined(NRF_GRTC_HAS_CLKSEL) && (NRF_GRTC_HAS_CLKSEL == 1))
487454
/* Use System LFCLK as the low-frequency clock source. */
488-
nrfy_grtc_clksel_set(NRF_GRTC, NRF_GRTC_CLKSEL_LFCLK);
455+
nrfx_grtc_clock_source_set(NRF_GRTC_CLKSEL_LFCLK);
489456
#endif
490457

491-
#if defined(CONFIG_NRF_GRTC_START_SYSCOUNTER)
492-
/* SYSCOUNTER needs to be turned off before initialization. */
493-
nrfy_grtc_sys_counter_set(NRF_GRTC, false);
494-
nrfy_grtc_timeout_set(NRF_GRTC, TIMEOUT);
495-
nrfy_grtc_waketime_set(NRF_GRTC, WAKETIME);
496-
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
497-
498458
IRQ_CONNECT(DT_IRQN(GRTC_NODE), DT_IRQ(GRTC_NODE, priority), nrfx_grtc_irq_handler, 0, 0);
499459

500460
err_code = nrfx_grtc_init(0);
@@ -507,20 +467,13 @@ static int sys_clock_driver_init(void)
507467
if (err_code != NRFX_SUCCESS) {
508468
return err_code == NRFX_ERROR_NO_MEM ? -ENOMEM : -EPERM;
509469
}
510-
if (IS_ENABLED(CONFIG_NRF_GRTC_SLEEP_ALLOWED)) {
511-
nrfy_grtc_sys_counter_auto_mode_set(NRF_GRTC, false);
512-
}
513470
#else
514471
err_code = nrfx_grtc_channel_alloc(&system_clock_channel_data.channel);
515472
if (err_code != NRFX_SUCCESS) {
516473
return -ENOMEM;
517474
}
518475
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
519476

520-
if (!IS_ENABLED(CONFIG_NRF_GRTC_SLEEP_ALLOWED)) {
521-
grtc_active_set();
522-
}
523-
524477
int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK;
525478
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
526479
system_timeout_set(CYC_PER_TICK);

modules/hal_nordic/nrfx/CMakeLists.txt

+13-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,19 @@ if(CONFIG_NRFX_TWI OR CONFIG_NRFX_TWIM)
127127
zephyr_library_sources(${SRC_DIR}/nrfx_twi_twim.c)
128128
endif()
129129

130-
if (CONFIG_NRF_GRTC_TIMER AND CONFIG_NRF_GRTC_TIMER_CLOCK_MANAGEMENT)
131-
zephyr_library_compile_definitions(NRF_GRTC_HAS_EXTENDED=1)
130+
if (CONFIG_NRF_GRTC_TIMER)
131+
if (CONFIG_NRF_GRTC_TIMER_CLOCK_MANAGEMENT)
132+
zephyr_library_compile_definitions(NRF_GRTC_HAS_EXTENDED=1)
133+
endif()
134+
if (CONFIG_NRF_GRTC_SLEEP_ALLOWED)
135+
zephyr_compile_definitions(NRFX_GRTC_CONFIG_SLEEP_ALLOWED=1)
136+
endif()
137+
if (CONFIG_NRF_GRTC_TIMER_AUTO_KEEP_ALIVE)
138+
zephyr_compile_definitions(NRFX_GRTC_CONFIG_AUTOEN=1)
139+
endif()
140+
if (CONFIG_NRF_GRTC_START_SYSCOUNTER)
141+
zephyr_compile_definitions(NRFX_GRTC_CONFIG_AUTOSTART=1)
142+
endif()
132143
endif()
133144

134145
# Inject HAL "CONFIG_NFCT_PINS_AS_GPIOS" definition if user requests to

0 commit comments

Comments
 (0)