1/*
2 * Renesas SCP/MCP Software
3 * Copyright (c) 2020-2021, Renesas Electronics Corporation. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include <arch.h>
9#include <arch_system.h>
10#include <asm_macros.S>
11
12    .section .entrypoint
13
14    .globl _entrypoint
15    .globl _restore_system
16    .globl _save_system
17    .globl _boot_flag
18    .globl _shutdown_request
19
20    .extern _vector_table
21    .extern arm_main
22    .extern rcar_pwrc_set_suspend_to_ram
23#ifdef BUILD_HAS_NOTIFICATION
24    .extern __fwk_notification_reset
25#endif
26
27func _entrypoint
28    ldr w0, _boot_flag
29    ldr w1, =R_WARMBOOT
30    cmp w1, w0
31    beq _restore_system
32
33    ldr x0,  =__STACK_TOP__
34    mov sp, x0 /* SP_EL3 */
35
36    adr x0, _vector_table
37    msr vbar_el3, x0
38    isb
39
40    msr spsel, #0
41    ldr x0, =__STACK_SP0_TOP__
42    mov sp, x0 /* SP_EL0 */
43    stp x29, x30, [sp, #-32]!
44
45    adr x0, __RW_START__
46    adr x1, __RW_END__
47    sub x1, x1, x0
48    bl  inv_dcache_range
49
50    ldr x0, =__BSS_START__
51    ldr x1, =__BSS_SIZE__
52    bl  zeromem
53
54#if USE_COHERENT_MEM
55    ldr x0, =__COHERENT_RAM_START__
56    ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__
57    bl  zeromem
58#endif
59
60    mrs   x0, scr_el3
61    /* RW[10]=1, HCE[8]=0, SMD[7]=0, EA[3]=1, FIQ[2]=1, IRQ[1]=1, NS[0]=0 */
62    mov   x0, #(1<<10 | 0<<8 | 0<<7 | 1<<3 | 1<<2 | 1<<1 | 0<<0)
63    msr   scr_el3, x0
64
65    /* --------------------------------------------------
66     * Initialize platform and jump to our c-entry point
67     * for this type of reset.
68     * --------------------------------------------------
69     */
70#ifdef BUILD_HAS_NOTIFICATION
71    bl  __fwk_notification_reset
72#endif
73    bl  arm_main
74
75    mov x0, 1
76    ldp x29, x30, [sp], #32
77
78    ret
79
80endfunc _entrypoint
81
82func _save_system /* EL3t */
83    stp x2, x3, [sp, #-0x10]!
84    ldr x2, =_save_area_top
85    stp x0, x1, [x2, #-0x10]!
86    mov x0, x2
87    ldp x2, x3, [sp], #0x10
88    stp x2, x3, [x0, #-0x10]!
89    stp x4, x5, [x0, #-0x10]!
90    stp x6, x7, [x0, #-0x10]!
91    stp x8, x9, [x0, #-0x10]!
92    stp x10, x11, [x0, #-0x10]!
93    stp x12, x13, [x0, #-0x10]!
94    stp x14, x15, [x0, #-0x10]!
95    stp x16, x17, [x0, #-0x10]!
96    stp x18, x19, [x0, #-0x10]!
97    stp x20, x21, [x0, #-0x10]!
98    stp x22, x23, [x0, #-0x10]!
99    stp x24, x25, [x0, #-0x10]!
100    stp x26, x27, [x0, #-0x10]!
101    stp x28, x29, [x0, #-0x10]!
102    stp x30, xzr, [x0, #-0x10]!
103
104    mov x2, sp
105    msr spsel, #1
106    mov x3, sp
107    msr spsel, #0
108    stp x2, x3, [x0, #-0x10]!   /* Save SP_EL0, SP_EL3 */
109
110    mrs x3, scr_el3
111    mov x2, x30
112    stp x2, x3, [x0, #-0x10]!   /* Save elr_el3(lr), scr_el3 */
113
114    mrs x2, nzcv
115    mrs x3, daif
116    orr x2, x2, x3
117    mrs x3, CurrentEL
118    orr x2, x2, x3
119    mrs x3, SPSel
120    orr x2, x2, x3
121    mrs x3, vbar_el3
122    stp x2, x3, [x0, #-0x10]!   /* Save spsr_el3(psr), vbar_el3 */
123
124    bl rcar_pwrc_set_suspend_to_ram
1251:
126    wfi
127    b 1b
128endfunc _save_system
129
130func _restore_system /* EL3h */
131    ldr x0, =_save_area_bottom
132
133    ldp x2, x3, [x0], #0x10     /* Restore spsr_el3(psr), vbar_el3 */
134    msr spsr_el3, x2
135    msr vbar_el3, x3
136
137    ldp x2, x3, [x0], #0x10     /* Restore elr_el3(lr), scr_el3 */
138    msr elr_el3, x2
139    msr scr_el3, x3
140
141    ldp x2, x3, [x0], #0x10     /* Restore SP_EL0, SP_EL3 */
142    mov sp, x3
143    msr spsel, #0
144    mov sp, x2
145
146    ldp x30, xzr, [x0], #0x10
147    ldp x28, x29, [x0], #0x10
148    ldp x26, x27, [x0], #0x10
149    ldp x24, x25, [x0], #0x10
150    ldp x22, x23, [x0], #0x10
151    ldp x20, x21, [x0], #0x10
152    ldp x18, x19, [x0], #0x10
153    ldp x16, x17, [x0], #0x10
154    ldp x14, x15, [x0], #0x10
155    ldp x12, x13, [x0], #0x10
156    ldp x10, x11, [x0], #0x10
157    ldp x8, x9, [x0], #0x10
158    ldp x6, x7, [x0], #0x10
159    ldp x4, x5, [x0], #0x10
160    ldp x2, x3, [x0], #0x10
161    stp x2, x3, [sp, #-0x10]!
162    mov x2, x0
163    ldp x0, x1, [x2], #0x10
164    ldp x2, x3, [sp], #0x10
165
166    msr spsel, #1
167    eret
168endfunc _restore_system
169
170    .section .data.context
171    .align 4
172_boot_flag:
173    .long 0
174
175    .align 4
176_shutdown_request:
177    .long 0
178
179    .align 4
180_save_area_bottom:
181    .rept 38
182    .long 0, 0
183    .endr
184_save_area_top:
185
186    .end
187