Skip to content

Commit 24082d5

Browse files
nordic-krchcarlescufi
authored andcommitted
arch: arm: cortex_m: pm_s2ram: Add option for custom marking
s2ram procedure used RAM magic word for marking suspend-to-RAM. This method may not work in some cases, e.g. when global reset does not reset RAM content. In that case resuming from s2ram is detected when global reset occurred. RAM magic word method is the default but with CONFIG_PM_S2RAM_CUSTOM_MARKING a custom implementation can be provided. Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent c547079 commit 24082d5

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

arch/arm/core/cortex_m/pm_s2ram.S

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
#include <zephyr/arch/cpu.h>
1515
#include <zephyr/arch/common/pm_s2ram.h>
1616

17-
#define MAGIC (0xDABBAD00)
18-
1917
_ASM_FILE_PROLOGUE
2018

19+
GTEXT(pm_s2ram_mark_set)
20+
GTEXT(pm_s2ram_mark_check_and_clear)
2121
GDATA(_cpu_context)
22-
GDATA(marker)
2322

2423
SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
2524
/*
@@ -64,11 +63,9 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
6463
str r2, [r1, #___cpu_context_t_control_OFFSET]
6564

6665
/*
67-
* Set the marker to MAGIC value
66+
* Mark entering suspend to RAM.
6867
*/
69-
ldr r1, =marker
70-
ldr r2, =MAGIC
71-
str r2, [r1]
68+
bl pm_s2ram_mark_set
7269

7370
/*
7471
* Call the system_off function passed as parameter. This should never
@@ -82,35 +79,29 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
8279
*/
8380

8481
/*
85-
* Reset the marker
82+
* Reset the marking of suspend to RAM, return is ignored.
8683
*/
87-
ldr r1, =marker
88-
mov r2, #0x0
89-
str r2, [r1]
84+
push {r0}
85+
bl pm_s2ram_mark_check_and_clear
86+
pop {r0}
9087

9188
pop {r4-r12, lr}
9289
bx lr
9390

91+
9492
GTEXT(arch_pm_s2ram_resume)
9593
SECTION_FUNC(TEXT, arch_pm_s2ram_resume)
9694
/*
97-
* Check if the marker is set
95+
* Check if reset occurred after suspending to RAM.
9896
*/
99-
ldr r0, =marker
100-
ldr r0, [r0]
101-
ldr r1, =MAGIC
102-
cmp r0, r1
97+
push {lr}
98+
bl pm_s2ram_mark_check_and_clear
99+
cmp r0, #0x1
100+
pop {lr}
103101
beq resume
104102
bx lr
105103

106104
resume:
107-
/*
108-
* Reset the marker
109-
*/
110-
ldr r0, =marker
111-
mov r1, #0x0
112-
str r1, [r0]
113-
114105
/*
115106
* Restore the CPU context
116107
*/

arch/arm/core/cortex_m/pm_s2ram.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,33 @@
99

1010
#include <zephyr/arch/common/pm_s2ram.h>
1111

12+
#define MAGIC (0xDABBAD00)
13+
1214
/**
1315
* CPU context for S2RAM
1416
*/
1517
__noinit _cpu_context_t _cpu_context;
1618

19+
#ifndef CONFIG_PM_S2RAM_CUSTOM_MARKING
1720
/**
1821
* S2RAM Marker
1922
*/
20-
__noinit uint32_t marker;
23+
static __noinit uint32_t marker;
24+
25+
void pm_s2ram_mark_set(void)
26+
{
27+
marker = MAGIC;
28+
}
29+
30+
bool pm_s2ram_mark_check_and_clear(void)
31+
{
32+
if (marker == MAGIC) {
33+
marker = 0;
34+
35+
return true;
36+
}
37+
38+
return false;
39+
}
40+
41+
#endif /* CONFIG_PM_S2RAM_CUSTOM_MARKING */

include/zephyr/arch/common/pm_s2ram.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*
88
* @brief public S2RAM APIs.
99
* @defgroup pm_s2ram S2RAM APIs
10-
* @ingroup subsys_pm
1110
* @{
1211
*/
1312

@@ -57,6 +56,30 @@ typedef int (*pm_s2ram_system_off_fn_t)(void);
5756
*/
5857
int arch_pm_s2ram_suspend(pm_s2ram_system_off_fn_t system_off);
5958

59+
/**
60+
* @brief Mark that core is entering suspend-to-RAM state.
61+
*
62+
* Function is called when system state is stored to RAM, just before going to system
63+
* off.
64+
*
65+
* Default implementation is setting a magic word in RAM. CONFIG_PM_S2RAM_CUSTOM_MARKING
66+
* allows custom implementation.
67+
*/
68+
void pm_s2ram_mark_set(void);
69+
70+
/**
71+
* @brief Check suspend-to-RAM marking and clear its state.
72+
*
73+
* Function is used to determine if resuming after suspend-to-RAM shall be performed
74+
* or standard boot code shall be executed.
75+
*
76+
* Default implementation is checking a magic word in RAM. CONFIG_PM_S2RAM_CUSTOM_MARKING
77+
* allows custom implementation.
78+
*
79+
* @retval true if marking is found which indicates resuming after suspend-to-RAM.
80+
* @retval false if marking is not found which indicates standard boot.
81+
*/
82+
bool pm_s2ram_mark_check_and_clear(void);
6083
/**
6184
* @}
6285
*/

subsys/pm/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ config PM_S2RAM
3737
help
3838
This option enables suspend-to-RAM (S2RAM).
3939

40+
config PM_S2RAM_CUSTOM_MARKING
41+
bool "Use custom marking functions"
42+
depends on PM_S2RAM
43+
help
44+
By default a magic word in RAM is used to mark entering suspend-to-RAM. Enabling
45+
this option allows custom implementation of functions which handles the marking.
46+
4047
config PM_NEED_ALL_DEVICES_IDLE
4148
bool "System Low Power Mode Needs All Devices Idle"
4249
depends on PM_DEVICE && !SMP

0 commit comments

Comments
 (0)