1 /*
2  * Copyright (c) 2022, Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/arch/cpu.h>
8 #include <zephyr/linker/sections.h>
9 
10 #include <zephyr/arch/common/pm_s2ram.h>
11 
12 #define MAGIC (0xDABBAD00)
13 
14 /**
15  * CPU context for S2RAM
16  */
17 __noinit _cpu_context_t _cpu_context;
18 
19 #ifndef CONFIG_PM_S2RAM_CUSTOM_MARKING
20 /**
21  * S2RAM Marker
22  */
23 static __noinit uint32_t marker;
24 
pm_s2ram_mark_set(void)25 void __attribute__((naked)) pm_s2ram_mark_set(void)
26 {
27 	__asm__ volatile(
28 		/* Set the marker to MAGIC value */
29 		"str	%[_magic_val], [%[_marker]]\n"
30 
31 		"bx	lr\n"
32 		:
33 		: [_magic_val] "r"(MAGIC), [_marker] "r"(&marker)
34 		: "r1", "r4", "memory");
35 }
36 
pm_s2ram_mark_check_and_clear(void)37 bool __attribute__((naked)) pm_s2ram_mark_check_and_clear(void)
38 {
39 	__asm__ volatile(
40 		/* Set return value to 0 */
41 		"mov	r0, #0\n"
42 
43 		/* Check the marker */
44 		"ldr	r3, [%[_marker]]\n"
45 		"cmp	r3, %[_magic_val]\n"
46 		"bne	exit\n"
47 
48 		/*
49 		 * Reset the marker
50 		 */
51 		"str	r0, [%[_marker]]\n"
52 
53 		/*
54 		 * Set return value to 1
55 		 */
56 		"mov	r0, #1\n"
57 
58 		"exit:\n"
59 		"bx lr\n"
60 		:
61 		: [_magic_val] "r"(MAGIC), [_marker] "r"(&marker)
62 		: "r0", "r1", "r3", "r4", "memory");
63 }
64 
65 #endif /* CONFIG_PM_S2RAM_CUSTOM_MARKING */
66