1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <zephyr/sys/poweroff.h>
7 #include <zephyr/toolchain.h>
8 #include <zephyr/drivers/retained_mem/nrf_retained_mem.h>
9
10 #if defined(CONFIG_SOC_SERIES_NRF51X) || defined(CONFIG_SOC_SERIES_NRF52X)
11 #include <hal/nrf_power.h>
12 #elif defined(CONFIG_SOC_SERIES_NRF54HX)
13 #include <power.h>
14 #else
15 #include <hal/nrf_regulators.h>
16 #endif
17 #if defined(CONFIG_SOC_SERIES_NRF54LX)
18 #include <helpers/nrfx_reset_reason.h>
19 #endif
20
21 #if defined(CONFIG_HAS_NORDIC_RAM_CTRL)
22 #include <helpers/nrfx_ram_ctrl.h>
23 #endif
24
z_sys_poweroff(void)25 void z_sys_poweroff(void)
26 {
27 #if defined(CONFIG_HAS_NORDIC_RAM_CTRL)
28 uint8_t *ram_start;
29 size_t ram_size;
30
31 #if defined(NRF_MEMORY_RAM_BASE)
32 ram_start = (uint8_t *)NRF_MEMORY_RAM_BASE;
33 #else
34 ram_start = (uint8_t *)NRF_MEMORY_RAM0_BASE;
35 #endif
36
37 ram_size = 0;
38 #if defined(NRF_MEMORY_RAM_SIZE)
39 ram_size += NRF_MEMORY_RAM_SIZE;
40 #endif
41 #if defined(NRF_MEMORY_RAM0_SIZE)
42 ram_size += NRF_MEMORY_RAM0_SIZE;
43 #endif
44 #if defined(NRF_MEMORY_RAM1_SIZE)
45 ram_size += NRF_MEMORY_RAM1_SIZE;
46 #endif
47 #if defined(NRF_MEMORY_RAM2_SIZE)
48 ram_size += NRF_MEMORY_RAM2_SIZE;
49 #endif
50
51 /* Disable retention for all memory blocks */
52 nrfx_ram_ctrl_retention_enable_set(ram_start, ram_size, false);
53 #if defined(DEVELOP_IN_NRF54L15) && defined(NRF54L05_XXAA)
54 nrf_memconf_ramblock_ret_mask_enable_set(NRF_MEMCONF, 0, 0x1F8, false);
55 nrf_memconf_ramblock_ret2_mask_enable_set(NRF_MEMCONF, 0, 0x1F8, false);
56 #endif
57 #if defined(DEVELOP_IN_NRF54L15) && defined(NRF54L10_XXAA)
58 nrf_memconf_ramblock_ret_mask_enable_set(NRF_MEMCONF, 0, 0x1C0, false);
59 nrf_memconf_ramblock_ret2_mask_enable_set(NRF_MEMCONF, 0, 0x1C0, false);
60 #endif
61
62 #endif /* defined(CONFIG_HAS_NORDIC_RAM_CTRL) */
63
64 #if defined(CONFIG_RETAINED_MEM_NRF_RAM_CTRL)
65 /* Restore retention for retained_mem driver regions defined in devicetree */
66 (void)z_nrf_retained_mem_retention_apply();
67 #endif
68
69 #if defined(CONFIG_SOC_SERIES_NRF54LX)
70 nrfx_reset_reason_clear(UINT32_MAX);
71 #endif
72 #if defined(CONFIG_SOC_SERIES_NRF51X) || defined(CONFIG_SOC_SERIES_NRF52X)
73 nrf_power_system_off(NRF_POWER);
74 #elif defined(CONFIG_SOC_SERIES_NRF54HX)
75 nrf_poweroff();
76 #else
77 nrf_regulators_system_off(NRF_REGULATORS);
78 #endif
79
80 CODE_UNREACHABLE;
81 }
82