1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2003	Josef Baumgartner <josef.baumgartner@telex.de>
4 * Based on code from Bernhard Kuhn <bkuhn@metrowerks.com>
5 */
6
7#include <asm-offsets.h>
8#include <config.h>
9#include <asm/cache.h>
10
11#define _START	_start
12#define _FAULT	_fault
13
14
15#define SAVE_ALL						\
16	move.w	#0x2700,%sr;		/* disable intrs */	\
17	subl	#60,%sp;		/* space for 15 regs */ \
18	moveml	%d0-%d7/%a0-%a6,%sp@;				\
19
20#define RESTORE_ALL						\
21	moveml	%sp@,%d0-%d7/%a0-%a6;				\
22	addl	#60,%sp;		/* space for 15 regs */ \
23	rte
24
25/* If we come from a pre-loader we don't need an initial exception
26 * table.
27 */
28#if !defined(CONFIG_MONITOR_IS_IN_RAM)
29
30.text
31
32/*
33 * Vector table. This is used for initial platform startup.
34 * These vectors are to catch any un-intended traps.
35 */
36_vectors:
37.long	0x00000000		/* Flash offset is 0 until we setup CS0 */
38#if defined(CONFIG_M5282) && (CONFIG_TEXT_BASE == CFG_SYS_INT_FLASH_BASE)
39.long	_start - CONFIG_TEXT_BASE
40#else
41.long	_START
42#endif
43
44.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
45.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
46.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
47.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
48.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
49.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
50.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
51.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
52
53.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
54.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
55.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
56.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
57.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
58.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
59.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
60.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
61
62.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
63.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
64.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
65.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
66.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
67.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
68.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
69.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
70
71.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
72.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
73.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
74.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
75.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
76.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
77.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
78.long	_FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT, _FAULT
79
80#endif
81
82.text
83
84#if defined(CFG_SYS_INT_FLASH_BASE) && \
85    (defined(CONFIG_M5282) || defined(CONFIG_M5281))
86#if (CONFIG_TEXT_BASE == CFG_SYS_INT_FLASH_BASE)
87.long	0x55AA55AA,0xAA55AA55		/* CFM Backdoorkey */
88.long	0xFFFFFFFF			/* all sectors protected */
89.long	0x00000000			/* supervisor/User restriction */
90.long	0x00000000			/* programm/data space restriction */
91.long	0x00000000			/* Flash security */
92#endif
93#endif
94
95.globl _start
96_start:
97	nop
98	nop
99	move.w	#0x2700,%sr
100
101#if defined(CONFIG_M5208) && !defined(CONFIG_M68K_QEMU)
102	/* Initialize RAMBAR: locate SRAM and validate it */
103	move.l	#(CFG_SYS_INIT_RAM_ADDR + CFG_SYS_INIT_RAM_CTRL), %d0
104	movec	%d0, %RAMBAR1
105#endif
106
107#if defined(CONFIG_M5272) || defined(CONFIG_M5249) || defined(CONFIG_M5253)
108	/* set MBAR address + valid flag */
109	move.l	#(CFG_SYS_MBAR + 1), %d0
110	move.c	%d0, %MBAR
111
112	/*** The 5249 has MBAR2 as well ***/
113#ifdef CFG_SYS_MBAR2
114	/* Get MBAR2 address */
115	move.l	#(CFG_SYS_MBAR2 + 1), %d0
116	 /* Set MBAR2 */
117	movec	%d0, #0xc0e
118#endif
119	move.l	#(CFG_SYS_INIT_RAM_ADDR + 1), %d0
120	movec	%d0, %RAMBAR0
121#endif /* CONFIG_M5272 || CONFIG_M5249 || CONFIG_M5253 */
122
123#if (defined(CONFIG_M5282) || defined(CONFIG_M5271)) && !defined(CONFIG_M68K_QEMU)
124	/* set MBAR address + valid flag */
125	move.l	#(CFG_SYS_MBAR + 1), %d0
126	move.l	%d0, 0x40000000
127
128	/* Initialize RAMBAR1: locate SRAM and validate it */
129	move.l	#(CFG_SYS_INIT_RAM_ADDR + 0x21), %d0
130	movec	%d0, %RAMBAR1
131
132#if defined(CONFIG_M5282)
133#if (CONFIG_TEXT_BASE == CFG_SYS_INT_FLASH_BASE)
134	/*
135	 * Setup code in SRAM to initialize FLASHBAR,
136	 * if start from internal Flash
137	 */
138	move.l	#(_flashbar_setup-CFG_SYS_INT_FLASH_BASE), %a0
139	move.l	#(_flashbar_setup_end-CFG_SYS_INT_FLASH_BASE), %a1
140	move.l	#(CFG_SYS_INIT_RAM_ADDR), %a2
141_copy_flash:
142	move.l	(%a0)+, (%a2)+
143	cmp.l	%a0, %a1
144	bgt.s	_copy_flash
145	jmp	CFG_SYS_INIT_RAM_ADDR
146
147_flashbar_setup:
148	/* Initialize FLASHBAR: locate internal Flash and validate it */
149	move.l	#(CFG_SYS_INT_FLASH_BASE + CFG_SYS_INT_FLASH_ENABLE), %d0
150	movec	%d0, %FLASHBAR
151	jmp	_after_flashbar_copy.L	/* Force jump to absolute address */
152_flashbar_setup_end:
153	nop
154_after_flashbar_copy:
155#else
156	/* Setup code to initialize FLASHBAR, if start from external Memory */
157	move.l	#(CFG_SYS_INT_FLASH_BASE + CFG_SYS_INT_FLASH_ENABLE), %d0
158	movec	%d0, %FLASHBAR
159#endif /* (CONFIG_TEXT_BASE == CFG_SYS_INT_FLASH_BASE) */
160
161#endif
162#endif
163	/*
164	 * if we come from a pre-loader we have no exception table and
165	 * therefore no VBR to set
166	 */
167#if !defined(CONFIG_MONITOR_IS_IN_RAM)
168#if defined(CONFIG_M5282) && (CONFIG_TEXT_BASE == CFG_SYS_INT_FLASH_BASE)
169	move.l	#CFG_SYS_INT_FLASH_BASE, %d0
170#else
171	move.l	#CFG_SYS_FLASH_BASE, %d0
172#endif
173	movec	%d0, %VBR
174#endif
175
176#ifdef CONFIG_M5275
177	/* set MBAR address + valid flag */
178	move.l	#(CFG_SYS_MBAR + 1), %d0
179	move.l	%d0, 0x40000000
180/*	movec	%d0, %MBAR */
181
182	/* Initialize RAMBAR: locate SRAM and validate it */
183	move.l	#(CFG_SYS_INIT_RAM_ADDR + 0x21), %d0
184	movec	%d0, %RAMBAR1
185#endif
186
187	/* initialize general use internal ram */
188	move.l	#0, %d0
189	move.l	#(ICACHE_STATUS), %a1	/* icache */
190	move.l	#(DCACHE_STATUS), %a2	/* icache */
191	move.l	%d0, (%a1)
192	move.l	%d0, (%a2)
193
194	/* put relocation table address to a5 */
195	move.l	#__got_start, %a5
196
197	/* setup stack initially on top of internal static ram  */
198	move.l	#(CFG_SYS_INIT_RAM_ADDR + CFG_SYS_INIT_RAM_SIZE), %sp
199
200	/*
201	 * if configured, malloc_f arena will be reserved first,
202	 * then (and always) gd struct space will be reserved
203	 */
204	move.l	%sp, -(%sp)
205	move.l	#board_init_f_alloc_reserve, %a1
206	jsr	(%a1)
207
208	/* update stack and frame-pointers */
209	move.l	%d0, %sp
210	move.l	%sp, %fp
211
212	/* initialize reserved area */
213	move.l	%d0, -(%sp)
214	move.l	#board_init_f_init_reserve, %a1
215	jsr	(%a1)
216
217	/* run low-level CPU init code (from flash) */
218	move.l	#cpu_init_f, %a1
219	jsr	(%a1)
220
221	/* run low-level board init code (from flash) */
222	clr.l	%sp@-
223	move.l	#board_init_f, %a1
224	jsr	(%a1)
225
226	/* board_init_f() does not return */
227
228/******************************************************************************/
229
230/*
231 * void relocate_code(addr_sp, gd, addr_moni)
232 *
233 * This "function" does not return, instead it continues in RAM
234 * after relocating the monitor code.
235 *
236 * r3 = dest
237 * r4 = src
238 * r5 = length in bytes
239 * r6 = cachelinesize
240 */
241.globl relocate_code
242relocate_code:
243	link.w	%a6,#0
244	move.l	8(%a6), %sp		/* set new stack pointer */
245
246	move.l	12(%a6), %d0		/* Save copy of Global Data pointer */
247	move.l	16(%a6), %a0		/* Save copy of Destination Address */
248
249	move.l	#CONFIG_SYS_MONITOR_BASE, %a1
250	move.l	#__init_end, %a2
251	move.l	%a0, %a3
252	/* copy the code to RAM */
2531:
254	move.l	(%a1)+, (%a3)+
255	cmp.l	%a1,%a2
256	bgt.s	1b
257
258#define R_68K_32	1
259#define R_68K_RELATIVE	22
260
261	move.l #(__rel_dyn_start), %a1
262	move.l #(__rel_dyn_end), %a2
263
264fixloop:
265	move.l	(%a1)+, %d1	/* Elf32_Rela r_offset */
266	move.l	(%a1)+, %d2	/* Elf32_Rela r_info */
267	move.l	(%a1)+, %d3	/* Elf32_Rela r_addend */
268
269	andi.l	#0xff, %d2
270	cmp.l	#R_68K_32, %d2
271	beq.s	fixup
272	cmp.l	#R_68K_RELATIVE, %d2
273	beq.s	fixup
274
275	bra	fixnext
276
277fixup:
278	/* relative fix: store addend plus offset at dest location */
279	move.l	%a0, %a3
280	add.l	%d1, %a3
281	sub.l   #CONFIG_SYS_MONITOR_BASE, %a3
282	move.l	(%a3), %d4
283	add.l	%a0, %d4
284	sub.l   #CONFIG_SYS_MONITOR_BASE, %d4
285	move.l	%d4, (%a3)
286
287fixnext:
288	cmp.l	%a1, %a2
289	bge.s	fixloop
290
291/*
292 * We are done. Do not return, instead branch to second part of board
293 * initialization, now running from RAM.
294 */
295	move.l	%a0, %a1
296	add.l	#(in_ram - CONFIG_SYS_MONITOR_BASE), %a1
297	jmp	(%a1)
298
299in_ram:
300
301clear_bss:
302	/*
303	 * Now clear BSS segment
304	 */
305	move.l	#(_sbss), %a1
306	move.l	#(_ebss), %d1
3076:
308	clr.l	(%a1)+
309	cmp.l	%a1,%d1
310	bgt.s	6b
311
312	/*
313	 * fix got table in RAM
314	 */
315	move.l	#(__got_start), %a5	/* fix got pointer register a5 */
316
317	/* calculate relative jump to board_init_r in ram */
318	move.l	#(board_init_r), %a1
319
320	/* set parameters for board_init_r */
321	move.l	%a0,-(%sp)		/* dest_addr */
322	move.l	%d0,-(%sp)		/* gd */
323	jsr	(%a1)
324
325/******************************************************************************/
326
327/* exception code */
328.globl _fault
329_fault:
330	bra	_fault
331
332.globl _exc_handler
333_exc_handler:
334	SAVE_ALL
335	movel	%sp,%sp@-
336	bsr	exc_handler
337	addql	#4,%sp
338	RESTORE_ALL
339
340.globl _int_handler
341_int_handler:
342	SAVE_ALL
343	movel	%sp,%sp@-
344	bsr	int_handler
345	addql	#4,%sp
346	RESTORE_ALL
347
348/******************************************************************************/
349
350.align 4
351