1/*
2 * Copyright (c) 2014 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/**
8 * @file
9 * @brief Reset handler
10 *
11 * Reset handler that prepares the system for running C code.
12 */
13
14#include <zephyr/toolchain.h>
15#include <zephyr/linker/sections.h>
16#include <zephyr/arch/cpu.h>
17#include <swap_macros.h>
18#include <zephyr/arch/arc/asm-compat/assembler.h>
19#ifdef CONFIG_ARC_EARLY_SOC_INIT
20  #include <soc_ctrl.h>
21#endif
22
23GDATA(z_interrupt_stacks)
24GDATA(z_main_stack)
25GDATA(_VectorTable)
26
27/* use one of the available interrupt stacks during init */
28
29
30#define INIT_STACK z_interrupt_stacks
31#define INIT_STACK_SIZE CONFIG_ISR_STACK_SIZE
32
33GTEXT(__reset)
34GTEXT(__start)
35
36/**
37 * @brief Reset vector
38 *
39 * Ran when the system comes out of reset. The processor is at supervisor level.
40 *
41 * Locking interrupts prevents anything from interrupting the CPU.
42 *
43 * When these steps are completed, jump to z_prep_c(), which will finish setting
44 * up the system for running C code.
45 */
46
47SECTION_SUBSEC_FUNC(TEXT,_reset_and__start,__reset)
48SECTION_SUBSEC_FUNC(TEXT,_reset_and__start,__start)
49	/* lock interrupts: will get unlocked when switch to main task
50	 * also make sure the processor in the correct status
51	 */
52	mov_s r0, 0
53	kflag r0
54
55#ifdef CONFIG_ARC_SECURE_FIRMWARE
56	sflag r0
57#endif
58	/* interrupt related init */
59#ifndef CONFIG_ARC_NORMAL_FIRMWARE
60	/* IRQ_ACT and IRQ_CTRL should be initialized and set in secure mode */
61	sr r0, [_ARC_V2_AUX_IRQ_ACT]
62	sr r0, [_ARC_V2_AUX_IRQ_CTRL]
63#endif
64	sr r0, [_ARC_V2_AUX_IRQ_HINT]
65
66	/* set the vector table base early,
67	 * so that exception vectors can be handled.
68	 */
69	MOVR r0, _VectorTable
70#ifdef CONFIG_ARC_SECURE_FIRMWARE
71	sr r0, [_ARC_V2_IRQ_VECT_BASE_S]
72#else
73	SRR r0, [_ARC_V2_IRQ_VECT_BASE]
74#endif
75
76	lr r0, [_ARC_V2_STATUS32]
77	bset r0, r0, _ARC_V2_STATUS32_DZ_BIT
78	kflag r0
79
80#if defined(CONFIG_USERSPACE)
81	lr r0, [_ARC_V2_STATUS32]
82	bset r0, r0, _ARC_V2_STATUS32_US_BIT
83	kflag r0
84#endif
85
86#ifdef CONFIG_ARC_USE_UNALIGNED_MEM_ACCESS
87	lr r0, [_ARC_V2_STATUS32]
88	bset r0, r0, _ARC_V2_STATUS32_AD_BIT
89	kflag r0
90#endif
91
92/* Invalidate icache */
93	lr r0, [_ARC_V2_I_CACHE_BUILD]
94	and.f r0, r0, 0xff
95	bz.nd done_icache_invalidate
96
97	mov_s r2, 0
98	sr r2, [_ARC_V2_IC_IVIC]
99	/* writing to IC_IVIC needs 3 NOPs */
100	nop_s
101	nop_s
102	nop_s
103done_icache_invalidate:
104
105/* Invalidate dcache */
106	lr r3, [_ARC_V2_D_CACHE_BUILD]
107	and.f r3, r3, 0xff
108	bz.nd done_dcache_invalidate
109
110	mov_s r1, 1
111	sr r1, [_ARC_V2_DC_IVDC]
112
113done_dcache_invalidate:
114
115#ifdef CONFIG_ARC_EARLY_SOC_INIT
116	soc_early_asm_init_percpu
117#endif
118
119	_dsp_extension_probe
120
121/*
122 * Init ARC internal architecture state
123 * Force to initialize internal architecture state to reset values
124 * For scenarios where board hardware is not re-initialized between tests,
125 * some settings need to be restored to its default initial states as a
126 * substitution of normal hardware reset sequence.
127 */
128#ifdef CONFIG_INIT_ARCH_HW_AT_BOOT
129	/* Set MPU (v4 or v8) registers to default */
130#if CONFIG_ARC_MPU_VER == 4 || CONFIG_ARC_MPU_VER == 8
131	/* Set default reset value to _ARC_V2_MPU_EN register */
132#define ARC_MPU_EN_RESET_VALUE 0x400181C0
133	mov_s r1, ARC_MPU_EN_RESET_VALUE
134	sr r1, [_ARC_V2_MPU_EN]
135	/* Get MPU region numbers */
136	lr r3, [_ARC_V2_MPU_BUILD]
137	lsr_s r3, r3, 8
138	and r3, r3, 0xff
139	mov_s r1, 0
140	mov_s r2, 0
141	/* Set all MPU regions by iterating index */
142mpu_regions_reset:
143	brge r2, r3, done_mpu_regions_reset
144	sr r2, [_ARC_V2_MPU_INDEX]
145	sr r1, [_ARC_V2_MPU_RSTART]
146	sr r1, [_ARC_V2_MPU_REND]
147	sr r1, [_ARC_V2_MPU_RPER]
148	add_s r2, r2, 1
149	b_s mpu_regions_reset
150done_mpu_regions_reset:
151#endif
152#endif
153
154#ifdef CONFIG_ISA_ARCV3
155	/* Enable HW prefetcher if exist */
156	lr r0, [_ARC_HW_PF_BUILD]
157	breq r0, 0, hw_pf_setup_done
158	lr r1, [_ARC_HW_PF_CTRL]
159	or r1, r1, _ARC_HW_PF_CTRL_ENABLE
160	sr r1, [_ARC_HW_PF_CTRL]
161hw_pf_setup_done:
162#endif
163
164#if defined(CONFIG_SMP) || CONFIG_MP_MAX_NUM_CPUS  > 1
165	_get_cpu_id r0
166	breq r0, 0, _master_core_startup
167
168/*
169 * Non-masters wait for master core (core 0) to boot enough
170 */
171_slave_core_wait:
172#if CONFIG_MP_MAX_NUM_CPUS == 1
173	kflag	1
174#endif
175	ld r1, [arc_cpu_wake_flag]
176	brne r0, r1, _slave_core_wait
177
178	LDR sp, arc_cpu_sp
179	/* signal master core that slave core runs */
180	st 0, [arc_cpu_wake_flag]
181
182#if defined(CONFIG_ARC_FIRQ_STACK)
183	push r0
184	jl z_arc_firq_stack_set
185	pop r0
186#endif
187	j arch_secondary_cpu_init
188
189_master_core_startup:
190#endif
191
192#ifdef CONFIG_INIT_STACKS
193	/*
194	 * use the main stack to call memset on the interrupt stack and the
195	 * FIRQ stack when CONFIG_INIT_STACKS is enabled before switching to
196	 * one of them for the rest of the early boot
197	 */
198	mov_s sp, z_main_stack
199	add sp, sp, CONFIG_MAIN_STACK_SIZE
200
201	mov_s r0, z_interrupt_stacks
202	mov_s r1, 0xaa
203	mov_s r2, CONFIG_ISR_STACK_SIZE
204	jl memset
205
206#endif /* CONFIG_INIT_STACKS */
207
208	mov_s sp, INIT_STACK
209	add sp, sp, INIT_STACK_SIZE
210
211#if defined(CONFIG_ARC_FIRQ_STACK)
212	jl z_arc_firq_stack_set
213#endif
214
215	j z_prep_c
216