1 /* This is where the SPARC/LEON3 starts
3 * Copyright (C) 2007, 2015
4 * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com
6 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm-offsets.h>
11 #include <asm/asmmacro.h>
12 #include <asm/winmacro.h>
14 #include <asm/stack.h>
18 /* Default Plug&Play I/O area */
19 #ifndef CONFIG_AMBAPP_IOAREA
20 #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
23 /* Default number of SPARC register windows */
24 #ifndef CONFIG_SYS_SPARC_NWINDOWS
25 #define CONFIG_SYS_SPARC_NWINDOWS 8
28 /* Entry for traps which jump to a programmer-specified trap handler. */
30 wr %g0, 0xfe0, %psr; \
40 #define TRAPI(ilevel) \
46 /* Unexcpected trap will halt the processor by forcing it to error state */
48 #define BAD_TRAP ta 0; nop; nop; nop;
50 /* Software trap. Treat as BAD_TRAP for the time being... */
51 #define SOFT_TRAP TRAP(_hwerr)
53 #define PSR_INIT 0x1FC0 /* Disable traps, set s and ps */
56 /* All traps low-level code here must end with this macro. */
57 #define RESTORE_ALL b ret_trap_entry; clr %l6;
59 #define WRITE_PAUSE nop;nop;nop
63 ARGPUSH = (WINDOWSIZE + 4)
64 MINFRAME = (WINDOWSIZE + ARGPUSHSIZE + 4)
66 /* Number of register windows */
67 #ifndef CONFIG_SYS_SPARC_NWINDOWS
68 #error Must define number of SPARC register windows, default is 8
71 /* Macros to load address into a register. Uses GOT table for PIC */
74 #define SPARC_PIC_THUNK_CALL(reg) \
75 sethi %pc22(_GLOBAL_OFFSET_TABLE_-4), %##reg; \
76 call __sparc_get_pc_thunk.reg; \
77 add %##reg, %pc10(_GLOBAL_OFFSET_TABLE_+4), %##reg;
79 #define SPARC_LOAD_ADDRESS(sym, got, reg) \
80 sethi %gdop_hix22(sym), %##reg; \
81 xor %##reg, %gdop_lox10(sym), %##reg; \
82 ld [%##got + %##reg], %##reg, %gdop(sym);
86 #define SPARC_PIC_THUNK_CALL(reg)
87 #define SPARC_LOAD_ADDRESS(sym, got, tmp) \
93 #define SA(X) (((X)+(STACK_ALIGN-1)) & ~(STACK_ALIGN-1))
95 .section ".start", "ax"
96 .globl _start, start, _trap_table
97 .globl _irq_entry, nmi_trap
106 TRAPR(_hardreset); ! 00 reset trap
107 BAD_TRAP; ! 01 instruction_access_exception
108 BAD_TRAP; ! 02 illegal_instruction
109 BAD_TRAP; ! 03 priveleged_instruction
110 BAD_TRAP; ! 04 fp_disabled
111 TRAP(_window_overflow); ! 05 window_overflow
112 TRAP(_window_underflow); ! 06 window_underflow
113 BAD_TRAP; ! 07 Memory Address Not Aligned
114 BAD_TRAP; ! 08 Floating Point Exception
115 BAD_TRAP; ! 09 Data Miss Exception
116 BAD_TRAP; ! 0a Tagged Instruction Ovrflw
117 BAD_TRAP; ! 0b Watchpoint Detected
123 TRAPI(1); ! 11 IRQ level 1
124 TRAPI(2); ! 12 IRQ level 2
125 TRAPI(3); ! 13 IRQ level 3
126 TRAPI(4); ! 14 IRQ level 4
127 TRAPI(5); ! 15 IRQ level 5
128 TRAPI(6); ! 16 IRQ level 6
129 TRAPI(7); ! 17 IRQ level 7
130 TRAPI(8); ! 18 IRQ level 8
131 TRAPI(9); ! 19 IRQ level 9
132 TRAPI(10); ! 1a IRQ level 10
133 TRAPI(11); ! 1b IRQ level 11
134 TRAPI(12); ! 1c IRQ level 12
135 TRAPI(13); ! 1d IRQ level 13
136 TRAPI(14); ! 1e IRQ level 14
137 TRAP(_nmi_trap); ! 1f IRQ level 15 /
138 ! NMI (non maskable interrupt)
139 BAD_TRAP; ! 20 r_register_access_error
140 BAD_TRAP; ! 21 instruction access error
143 BAD_TRAP; ! 24 co-processor disabled
144 BAD_TRAP; ! 25 uniplemented FLUSH
147 BAD_TRAP; ! 28 co-processor exception
148 BAD_TRAP; ! 29 data access error
149 BAD_TRAP; ! 2a division by zero
150 BAD_TRAP; ! 2b data store error
151 BAD_TRAP; ! 2c data access MMU miss
155 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 30-33
156 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 34-37
157 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 38-3b
158 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 3c-3f
159 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 40-43
160 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 44-47
161 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 48-4b
162 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 4c-4f
163 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 50-53
164 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 54-57
165 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 58-5b
166 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 5c-5f
168 /* implementaion dependent */
169 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 60-63
170 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 64-67
171 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 68-6b
172 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 6c-6f
173 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 70-73
174 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 74-77
175 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 78-7b
176 BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP; ! 7c-7f
178 /* Software traps, not handled */
179 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 80-83
180 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 84-87
181 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 88-8b
182 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 8c-8f
183 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 90-93
184 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 94-97
185 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 98-9b
186 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! 9c-9f
187 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! a0-a3
188 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! a4-a7
189 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! a8-ab
190 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! ac-af
191 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! b0-b3
192 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! b4-b7
193 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! b8-bb
194 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! bc-bf
195 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! c0-c3
196 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! c4-c7
197 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! c8-cb
198 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! cc-cf
199 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! d0-d3
200 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! d4-d7
201 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! d8-db
202 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! dc-df
203 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! e0-e3
204 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! e4-e7
205 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! e8-eb
206 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! ec-ef
207 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! f0-f3
208 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! f4-f7
209 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! f8-fb
210 SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; ! fc-ff
213 .extern _nomem_amba_init, _nomem_memory_ctrl_init
220 /* Enable I/D-Cache and Snooping */
261 set CONFIG_SYS_INIT_SP_OFFSET, %fp
265 /* Obtain the address of _GLOBAL_OFFSET_TABLE_ */
266 SPARC_PIC_THUNK_CALL(l7)
268 /* Scan AMBA Bus for AMBA buses using PnP information. All found
269 * AMBA buses I/O area will be located in i0-i5 upon return.
270 * The i0-i5 registers are later used by _nomem_amba_init2
273 call _nomem_amba_init
274 sethi %hi(CONFIG_AMBAPP_IOAREA), %o0
276 /* Scan AMBA Buses for memory controllers, then initialize the
277 * memory controllers. Note that before setting up the memory controller
278 * the stack can not be used.
281 SPARC_LOAD_ADDRESS(grlib_mctrl_handlers, l7, o0)
283 call _nomem_memory_ctrl_init
286 /* The return valu indicate how many memory controllers where found and
287 * initialized, if no memory controller was initialized, we can not continue
288 * because from here on we expect memory to be working.
291 memory_ctrl_init_failed:
292 beq memory_ctrl_init_failed
295 /*** From now on the stack can be used. ***/
301 /* un relocated start address of monitor */
302 #define TEXT_START _text
304 /* un relocated end address of monitor */
305 #define DATA_END __init_end
308 SPARC_LOAD_ADDRESS(TEXT_START, l7, g2)
309 SPARC_LOAD_ADDRESS(DATA_END, l7, g3)
310 set CONFIG_SYS_RELOC_MONITOR_BASE,%g4
327 /* register g4 contain address to start
328 * This means that BSS must be directly after data and code segments
330 * g3 is length of bss = (__bss_end-__bss_start)
335 /* clear bss area (the relocated) */
336 SPARC_LOAD_ADDRESS(__bss_start, l7, g2)
337 SPARC_LOAD_ADDRESS(__bss_end, l7, g3)
340 clr %g1 /* std %g0 uses g0 and g1 */
341 /* clearing 16byte a time ==> linker script need to align to 16 byte offset */
350 /* add offsets to GOT table */
352 SPARC_LOAD_ADDRESS(__got_start, l7, g4)
353 SPARC_LOAD_ADDRESS(__got_end, l7, g3)
355 * new got offset = (old GOT-PTR (read with ld) -
356 * CONFIG_SYS_RELOC_MONITOR_BASE(from define) ) +
357 * Destination Address (from define)
359 set CONFIG_SYS_RELOC_MONITOR_BASE,%g2
360 SPARC_LOAD_ADDRESS(TEXT_START, l7, g1)
365 sub %g2,%g1,%g2 ! prepare register with (new base address) -
368 ld [%g4],%l0 ! load old GOT-PTR
369 add %l0,%g2,%l0 ! increase with (new base address) -
378 SPARC_LOAD_ADDRESS(__prom_start, l7, g2)
379 SPARC_LOAD_ADDRESS(__prom_end, l7, g3)
380 set CONFIG_SYS_PROM_OFFSET, %g4
389 bne prom_relocate_loop
392 /* Trap table has been moved, lets tell CPU about
393 * the new trap table address
396 set CONFIG_SYS_RELOC_MONITOR_BASE, %g2
402 /* Call relocated init functions */
404 SPARC_LOAD_ADDRESS(cpu_init_f2, l7, o1)
405 set CONFIG_SYS_RELOC_MONITOR_BASE,%o2
411 SPARC_LOAD_ADDRESS(board_init_f, l7, o1)
412 set CONFIG_SYS_RELOC_MONITOR_BASE,%o2
413 SPARC_LOAD_ADDRESS(TEXT_START, l7, g1)
419 dead: ta 0 ! if call returns...
422 /* Interrupt handler caller,
423 * reg L7: interrupt number
424 * reg L0: psr after interrupt
437 mov %l7, %o0 ! irq level
439 set (CONFIG_SYS_RELOC_MONITOR_BASE-CONFIG_SYS_TEXT_BASE), %o2
442 add %sp, SF_REGS_SZ, %o1 ! pt_regs ptr
443 or %l0, PSR_PIL, %g2 ! restore PIL after handler_irq
444 wr %g2, PSR_ET, %psr ! keep ET up
449 !Window overflow trap handler.
450 .global _window_overflow
454 mov %wim, %l3 ! Calculate next WIM
457 sll %l3, (CONFIG_SYS_SPARC_NWINDOWS-1) , %l4
460 save ! Get into window to be saved.
481 restore ! Go back to trap window.
483 jmp %l1 ! Re-execute save.
486 /* Window underflow trap handler. */
488 .global _window_underflow
492 mov %wim, %l3 ! Calculate next WIM
494 srl %l3, (CONFIG_SYS_SPARC_NWINDOWS-1), %l5
498 restore ! Two restores to get into the
499 restore ! window to restore
500 ld [%sp + 0], %l0; ! Restore window from the stack
516 save ! Get back to the trap window.
518 jmp %l1 ! Re-execute restore.
532 b _hwerr ! loop infinite
535 /* Registers to not touch at all. */
536 #define t_psr l0 /* Set by caller */
537 #define t_pc l1 /* Set by caller */
538 #define t_npc l2 /* Set by caller */
539 #define t_wim l3 /* Set by caller */
540 #define t_twinmask l4 /* Set at beginning of this entry routine. */
541 #define t_kstack l5 /* Set right before pt_regs frame is built */
542 #define t_retpc l6 /* If you change this, change winmacro.h header file */
543 #define t_systable l7 /* Never touch this, could be the syscall table ptr. */
544 #define curptr g6 /* Set after pt_regs frame is built */
547 /* build a pt_regs trap frame. */
548 sub %fp, (SF_REGS_SZ + PT_REGS_SZ), %t_kstack
549 PT_STORE_ALL(t_kstack, t_psr, t_pc, t_npc, g2)
551 /* See if we are in the trap window. */
553 sll %t_twinmask, %t_psr, %t_twinmask ! t_twinmask = (1 << psr)
554 andcc %t_twinmask, %t_wim, %g0
555 beq 1f ! in trap window, clean up
558 /*-------------------------------------------------
559 * Spill , adjust %wim and go.
561 srl %t_wim, 0x1, %g2 ! begin computation of new %wim
563 set (CONFIG_SYS_SPARC_NWINDOWS-1), %g3 !NWINDOWS-1
565 sll %t_wim, %g3, %t_wim ! NWINDOWS-1
569 save %g0, %g0, %g0 ! get in window to be saved
571 /* Set new %wim value */
574 /* Save the kernel window onto the corresponding stack. */
577 restore %g0, %g0, %g0
578 /*-------------------------------------------------*/
581 /* Trap from kernel with a window available.
584 jmpl %t_retpc + 0x8, %g0 ! return to caller
585 mov %t_kstack, %sp ! jump onto new stack
591 wr %t_psr, 0x0, %psr ! enable nesting again, clear ET
593 /* Will the rett land us in the invalid window? */
597 set CONFIG_SYS_SPARC_NWINDOWS, %g2 !NWINDOWS
603 be 1f ! Nope, just return from the trap
606 /* We have to grab a window before returning. */
607 set (CONFIG_SYS_SPARC_NWINDOWS-1), %g3 !NWINDOWS-1
615 /* Grrr, make sure we load from the right %sp... */
616 PT_LOAD_ALL(sp, t_psr, t_pc, t_npc, g1)
618 restore %g0, %g0, %g0
623 /* Reload the entire frame in case this is from a
624 * kernel system call or whatever...
627 PT_LOAD_ALL(sp, t_psr, t_pc, t_npc, g1)
637 /* This is called from relocated C-code.
638 * It resets the system by jumping to _start