xref: /openbmc/u-boot/arch/arc/lib/start.S (revision 4d93617d)
1/*
2 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier:	GPL-2.0+
5 */
6
7#include <asm-offsets.h>
8#include <config.h>
9#include <linux/linkage.h>
10#include <asm/arcregs.h>
11
12/*
13 * Note on the LD/ST addressing modes with address register write-back
14 *
15 * LD.a same as LD.aw
16 *
17 * LD.a    reg1, [reg2, x]  => Pre Incr
18 *      Eff Addr for load = [reg2 + x]
19 *
20 * LD.ab   reg1, [reg2, x]  => Post Incr
21 *      Eff Addr for load = [reg2]
22 */
23
24.macro PUSH reg
25	st.a	\reg, [%sp, -4]
26.endm
27
28.macro PUSHAX aux
29	lr	%r9, [\aux]
30	PUSH	%r9
31.endm
32
33.macro  SAVE_R1_TO_R24
34	PUSH	%r1
35	PUSH	%r2
36	PUSH	%r3
37	PUSH	%r4
38	PUSH	%r5
39	PUSH	%r6
40	PUSH	%r7
41	PUSH	%r8
42	PUSH	%r9
43	PUSH	%r10
44	PUSH	%r11
45	PUSH	%r12
46	PUSH	%r13
47	PUSH	%r14
48	PUSH	%r15
49	PUSH	%r16
50	PUSH	%r17
51	PUSH	%r18
52	PUSH	%r19
53	PUSH	%r20
54	PUSH	%r21
55	PUSH	%r22
56	PUSH	%r23
57	PUSH	%r24
58.endm
59
60.macro SAVE_ALL_SYS
61	/* saving %r0 to reg->r0 in advance since we read %ecr into it */
62	st	%r0, [%sp, -8]
63	lr	%r0, [%ecr]	/* all stack addressing is manual so far */
64	st	%r0, [%sp]
65	st	%sp, [%sp, -4]
66	/* now move %sp to reg->r0 position so we can do "push" automatically */
67	sub	%sp, %sp, 8
68
69	SAVE_R1_TO_R24
70	PUSH	%r25
71	PUSH	%gp
72	PUSH	%fp
73	PUSH	%blink
74	PUSHAX	%eret
75	PUSHAX	%erstatus
76	PUSH	%lp_count
77	PUSHAX	%lp_end
78	PUSHAX	%lp_start
79	PUSHAX	%erbta
80.endm
81
82.macro SAVE_EXCEPTION_SOURCE
83#ifdef CONFIG_MMU
84	/* If MMU exists exception faulting address is loaded in EFA reg */
85	lr	%r0, [%efa]
86#else
87	/* Otherwise in ERET (exception return) reg */
88	lr	%r0, [%eret]
89#endif
90.endm
91
92ENTRY(_start)
93	/* Setup interrupt vector base that matches "__text_start" */
94	sr	__ivt_start, [ARC_AUX_INTR_VEC_BASE]
95
96	/* Setup stack pointer */
97	mov	%sp, CONFIG_SYS_INIT_SP_ADDR
98	mov	%fp, %sp
99
100	/* Clear bss */
101	mov	%r0, __bss_start
102	mov	%r1, __bss_end
103
104clear_bss:
105	st.ab	0, [%r0, 4]
106	brlt	%r0, %r1, clear_bss
107
108	/* Zero the one and only argument of "board_init_f" */
109	mov_s	%r0, 0
110	j	board_init_f
111ENDPROC(_start)
112
113ENTRY(memory_error)
114	SAVE_ALL_SYS
115	SAVE_EXCEPTION_SOURCE
116	mov	%r1, %sp
117	j	do_memory_error
118ENDPROC(memory_error)
119
120ENTRY(instruction_error)
121	SAVE_ALL_SYS
122	SAVE_EXCEPTION_SOURCE
123	mov	%r1, %sp
124	j	do_instruction_error
125ENDPROC(instruction_error)
126
127ENTRY(interrupt_handler)
128	/* Todo - save and restore CPU context when interrupts will be in use */
129	bl	do_interrupt_handler
130	rtie
131ENDPROC(interrupt_handler)
132
133ENTRY(EV_MachineCheck)
134	SAVE_ALL_SYS
135	SAVE_EXCEPTION_SOURCE
136	mov	%r1, %sp
137	j	do_machine_check_fault
138ENDPROC(EV_MachineCheck)
139
140ENTRY(EV_TLBMissI)
141	SAVE_ALL_SYS
142	mov	%r0, %sp
143	j	do_itlb_miss
144ENDPROC(EV_TLBMissI)
145
146ENTRY(EV_TLBMissD)
147	SAVE_ALL_SYS
148	mov	%r0, %sp
149	j	do_dtlb_miss
150ENDPROC(EV_TLBMissD)
151
152ENTRY(EV_TLBProtV)
153	SAVE_ALL_SYS
154	SAVE_EXCEPTION_SOURCE
155	mov	%r1, %sp
156	j	do_tlb_prot_violation
157ENDPROC(EV_TLBProtV)
158
159ENTRY(EV_PrivilegeV)
160	SAVE_ALL_SYS
161	mov	%r0, %sp
162	j	do_privilege_violation
163ENDPROC(EV_PrivilegeV)
164
165ENTRY(EV_Trap)
166	SAVE_ALL_SYS
167	mov	%r0, %sp
168	j	do_trap
169ENDPROC(EV_Trap)
170
171ENTRY(EV_Extension)
172	SAVE_ALL_SYS
173	mov	%r0, %sp
174	j	do_extension
175ENDPROC(EV_Extension)
176
177/*
178 * void relocate_code (addr_sp, gd, addr_moni)
179 *
180 * This "function" does not return, instead it continues in RAM
181 * after relocating the monitor code.
182 *
183 * r0 = start_addr_sp
184 * r1 = new__gd
185 * r2 = relocaddr
186 */
187ENTRY(relocate_code)
188	/*
189	 * r0-r12 might be clobbered by C functions
190	 * so we use r13-r16 for storage here
191	 */
192	mov	%r13, %r0		/* save addr_sp */
193	mov	%r14, %r1		/* save addr of gd */
194	mov	%r15, %r2		/* save addr of destination */
195
196	mov	%r16, %r2		/* %r9 - relocation offset */
197	sub	%r16, %r16, __image_copy_start
198
199/* Set up the stack */
200stack_setup:
201	mov	%sp, %r13
202	mov	%fp, %sp
203
204/* Check if monitor is loaded right in place for relocation */
205	mov	%r0, __image_copy_start
206	cmp	%r0, %r15		/* skip relocation if code loaded */
207	bz	do_board_init_r		/* in target location already */
208
209/* Copy data (__image_copy_start - __image_copy_end) to new location */
210	mov	%r1, %r15
211	mov	%r2, __image_copy_end
212	sub	%r2, %r2, %r0		/* r3 <- amount of bytes to copy */
213	asr	%r2, %r2, 2		/* r3 <- amount of words to copy */
214	mov	%lp_count, %r2
215	lp	copy_end
216	ld.ab	%r2,[%r0,4]
217	st.ab	%r2,[%r1,4]
218copy_end:
219
220/* Fix relocations related issues */
221	bl	do_elf_reloc_fixups
222#ifndef CONFIG_SYS_ICACHE_OFF
223	bl	invalidate_icache_all
224#endif
225#ifndef CONFIG_SYS_DCACHE_OFF
226	bl	flush_dcache_all
227#endif
228
229/* Update position of intterupt vector table */
230	lr	%r0, [ARC_AUX_INTR_VEC_BASE]	/* Read current position */
231	add	%r0, %r0, %r16			/* Update address */
232	sr	%r0, [ARC_AUX_INTR_VEC_BASE]	/* Write new position */
233
234do_board_init_r:
235/* Prepare for exection of "board_init_r" in relocated monitor */
236	mov	%r2, board_init_r	/* old address of "board_init_r()" */
237	add	%r2, %r2, %r16		/* new address of "board_init_r()" */
238	mov	%r0, %r14		/* 1-st parameter: gd_t */
239	mov	%r1, %r15		/* 2-nd parameter: dest_addr */
240	j	[%r2]
241ENDPROC(relocate_code)
242