1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file Nordic Semiconductor nRF53 processors family management helper for the network CPU.
9  */
10 
11 #include <nrf53_cpunet_mgmt.h>
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/init.h>
15 #include <zephyr/sys/__assert.h>
16 #include <zephyr/sys/notify.h>
17 #include <zephyr/sys/onoff.h>
18 
19 #include <hal/nrf_reset.h>
20 #include <hal/nrf_spu.h>
21 
22 static struct onoff_manager cpunet_mgr;
23 
onoff_start(struct onoff_manager * mgr,onoff_notify_fn notify)24 static void onoff_start(struct onoff_manager *mgr, onoff_notify_fn notify)
25 {
26 	nrf_reset_network_force_off(NRF_RESET, false);
27 
28 	notify(mgr, 0);
29 }
30 
onoff_stop(struct onoff_manager * mgr,onoff_notify_fn notify)31 static void onoff_stop(struct onoff_manager *mgr, onoff_notify_fn notify)
32 {
33 	nrf_reset_network_force_off(NRF_RESET, true);
34 
35 	notify(mgr, 0);
36 }
37 
nrf53_cpunet_mgmt_init(void)38 static int nrf53_cpunet_mgmt_init(void)
39 {
40 	static const struct onoff_transitions transitions = {
41 		.start = onoff_start,
42 		.stop = onoff_stop
43 	};
44 
45 	return onoff_manager_init(&cpunet_mgr, &transitions);
46 }
47 
48 SYS_INIT(nrf53_cpunet_mgmt_init, PRE_KERNEL_1, 0);
49 
nrf53_cpunet_enable(bool on)50 void nrf53_cpunet_enable(bool on)
51 {
52 	int ret;
53 	int ignored;
54 	struct onoff_client cli;
55 
56 	if (on) {
57 		sys_notify_init_spinwait(&cli.notify);
58 
59 		ret = onoff_request(&cpunet_mgr, &cli);
60 		__ASSERT_NO_MSG(ret >= 0);
61 
62 		/* The transition is synchronous and shall take effect immediately. */
63 		ret = sys_notify_fetch_result(&cli.notify, &ignored);
64 	} else {
65 		ret = onoff_release(&cpunet_mgr);
66 	}
67 
68 	__ASSERT_NO_MSG(ret >= 0);
69 }
70 
71 #ifdef CONFIG_SOC_NRF53_CPUNET_ENABLE
nrf53_cpunet_init(void)72 static int nrf53_cpunet_init(void)
73 {
74 #if !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
75 	/* Retain nRF5340 Network MCU in Secure domain (bus
76 	 * accesses by Network MCU will have Secure attribute set).
77 	 */
78 	nrf_spu_extdomain_set((NRF_SPU_Type *)DT_REG_ADDR(DT_NODELABEL(spu)), 0, true, false);
79 #endif /* !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) */
80 
81 #if !defined(CONFIG_TRUSTED_EXECUTION_SECURE)
82 	/*
83 	 * Building Zephyr with CONFIG_TRUSTED_EXECUTION_SECURE=y implies
84 	 * building also a Non-Secure image. The Non-Secure image will, in
85 	 * this case do the remainder of actions to properly configure and
86 	 * boot the Network MCU.
87 	 */
88 
89 	nrf53_cpunet_enable(true);
90 #endif /* !CONFIG_TRUSTED_EXECUTION_SECURE */
91 
92 	return 0;
93 }
94 
95 SYS_INIT(nrf53_cpunet_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
96 #endif /* CONFIG_SOC_NRF53_CPUNET_ENABLE */
97