1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <nrf_sys_event.h>
8 
9 #if CONFIG_SOC_SERIES_NRF54HX
10 
11 /*
12  * The 54HX is not yet supported by an nrfx driver nor the system controller so
13  * we implement an ISR and concurrent access safe reference counting implementation
14  * here using the nrfx hal.
15  */
16 
17 #include <hal/nrf_lrcconf.h>
18 
19 static struct k_spinlock global_constlat_lock;
20 static uint16_t global_constlat_count;
21 
nrf_sys_event_request_global_constlat(void)22 int nrf_sys_event_request_global_constlat(void)
23 {
24 	K_SPINLOCK(&global_constlat_lock) {
25 		if (global_constlat_count == 0) {
26 #if CONFIG_SOC_NRF54H20_CPUAPP
27 			nrf_lrcconf_task_trigger(NRF_LRCCONF010,
28 						 NRF_LRCCONF_TASK_CONSTLAT_ENABLE);
29 #elif CONFIG_SOC_NRF54H20_CPURAD
30 			nrf_lrcconf_task_trigger(NRF_LRCCONF000,
31 						 NRF_LRCCONF_TASK_CONSTLAT_ENABLE);
32 			nrf_lrcconf_task_trigger(NRF_LRCCONF020,
33 						 NRF_LRCCONF_TASK_CONSTLAT_ENABLE);
34 #else
35 #error "unsupported"
36 #endif
37 		}
38 
39 		global_constlat_count++;
40 	}
41 
42 	return 0;
43 }
44 
nrf_sys_event_release_global_constlat(void)45 int nrf_sys_event_release_global_constlat(void)
46 {
47 	K_SPINLOCK(&global_constlat_lock) {
48 		if (global_constlat_count == 1) {
49 #if CONFIG_SOC_NRF54H20_CPUAPP
50 			nrf_lrcconf_task_trigger(NRF_LRCCONF010,
51 						 NRF_LRCCONF_TASK_CONSTLAT_DISABLE);
52 #elif CONFIG_SOC_NRF54H20_CPURAD
53 			nrf_lrcconf_task_trigger(NRF_LRCCONF000,
54 						 NRF_LRCCONF_TASK_CONSTLAT_DISABLE);
55 			nrf_lrcconf_task_trigger(NRF_LRCCONF020,
56 						 NRF_LRCCONF_TASK_CONSTLAT_DISABLE);
57 #else
58 #error "unsupported"
59 #endif
60 		}
61 
62 		global_constlat_count--;
63 	}
64 
65 	return 0;
66 }
67 
68 #else
69 
70 /*
71  * The nrfx power driver already contains an ISR and concurrent access safe reference
72  * counting API so we just use it directly when available.
73  */
74 
75 #include <nrfx_power.h>
76 
nrf_sys_event_request_global_constlat(void)77 int nrf_sys_event_request_global_constlat(void)
78 {
79 	nrfx_err_t err;
80 
81 	err = nrfx_power_constlat_mode_request();
82 
83 	return (err == NRFX_SUCCESS || err == NRFX_ERROR_ALREADY) ? 0 : -EAGAIN;
84 }
85 
nrf_sys_event_release_global_constlat(void)86 int nrf_sys_event_release_global_constlat(void)
87 {
88 	nrfx_err_t err;
89 
90 	err = nrfx_power_constlat_mode_free();
91 
92 	return (err == NRFX_SUCCESS || err == NRFX_ERROR_BUSY) ? 0 : -EAGAIN;
93 }
94 
95 #endif
96