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
266 set CONFIG_SYS_TEXT_BASE, %g2
272 /* Obtain the address of _GLOBAL_OFFSET_TABLE_ */
273 SPARC_PIC_THUNK_CALL(l7)
275 /* Scan AMBA Bus for AMBA buses using PnP information. All found
276 * AMBA buses I/O area will be located in i0-i5 upon return.
277 * The i0-i5 registers are later used by _nomem_amba_init2
280 call _nomem_amba_init
281 sethi %hi(CONFIG_AMBAPP_IOAREA), %o0
283 /* Scan AMBA Buses for memory controllers, then initialize the
284 * memory controllers. Note that before setting up the memory controller
285 * the stack can not be used.
288 SPARC_LOAD_ADDRESS(grlib_mctrl_handlers, l7, o0)
290 call _nomem_memory_ctrl_init
293 /* The return valu indicate how many memory controllers where found and
294 * initialized, if no memory controller was initialized, we can not continue
295 * because from here on we expect memory to be working.
298 memory_ctrl_init_failed:
299 beq memory_ctrl_init_failed
302 /*** From now on the stack can be used. ***/
313 mov 1, %g1 ! For GRMON2 to exit normally.
314 ta 0 ! If board_init_f call returns.. (unlikely)
317 ba dead_unreloc ! infinte loop
320 !-------------------------------------------------------------------------------
322 /* void relocate_code (addr_sp, gd, addr_moni)
324 * This "function" does not return, instead it continues in RAM after
325 * relocating the monitor code.
327 * %o0 = Relocated stack pointer
328 * %o1 = Relocated global data pointer
329 * %o2 = Relocated text pointer
331 * %l7 = _GLOBAL_OFFSET_TABLE_ address
334 .type relocate_code, #function
337 !SPARC_PIC_THUNK_CALL(l7)
339 SPARC_LOAD_ADDRESS(_text, l7, g2) ! start address of monitor
340 SPARC_LOAD_ADDRESS(__init_end, l7, g3) ! end address of monitor
341 mov %o2, %g4 ! relocation address
342 sub %g4, %g2, %g6 ! relocation offset
343 /* copy .text & .data to relocated address */
348 inc 16, %g2 ! src += 16
350 bcs 10b ! while (src < end)
351 inc 16, %g4 ! dst += 16
359 /* register g4 contain address to start
360 * This means that BSS must be directly after data and code segments
362 * g3 is length of bss = (__bss_end-__bss_start)
366 /* clear the relocated .bss area */
368 SPARC_LOAD_ADDRESS(__bss_start, l7, g2)
369 SPARC_LOAD_ADDRESS(__bss_end, l7, g3)
370 sub %g3,%g2,%g3 ! length of .bss area
372 /* clearing 16byte a time ==> linker script need to align to 16 byte offset */
373 clr %g1 /* std %g0 uses g0 and g1 */
377 inc 16, %g4 ! ptr += 16
379 bcs 20b ! while (ptr < end)
382 /* add offsets to GOT table */
384 SPARC_LOAD_ADDRESS(__got_start, l7, g4)
386 SPARC_LOAD_ADDRESS(__got_end, l7, g3)
389 #ifdef CONFIG_RELOC_GOT_SKIP_NULL
393 add %l0, %g6, %l0 ! relocate GOT pointer
395 32: inc 4, %g4 ! ptr += 4
397 bcs 30b ! while (ptr < end)
401 SPARC_LOAD_ADDRESS(__prom_start, l7, g2)
402 SPARC_LOAD_ADDRESS(__prom_end, l7, g3)
404 * Calculated addres is stored in this variable by
405 * reserve_prom() function in common/board_f.c
407 SPARC_LOAD_ADDRESS(__prom_start_reloc, l7, g4)
419 ! %o0 = stack pointer (relocated)
420 ! %o1 = global data pointer (relocated)
421 ! %o2 = text pointer (relocated)
423 ! %g6 = relocation offset
424 ! %l7 = _GLOBAL_OFFSET_TABLE_
426 /* Trap table has been moved, lets tell CPU about
427 * the new trap table address
429 update_trap_table_address:
435 update_stack_pointers:
437 andn %fp, 0x0f, %fp ! align to 16 bytes
438 add %fp, -64, %fp ! make space for a window push
439 mov %fp, %sp ! setup stack pointer
442 mov %o1, %o0 ! relocated global data pointer
443 mov %o2, %o1 ! relocated text pointer
444 SPARC_LOAD_ADDRESS(board_init_r, l7, o3)
445 add %o3, %g6, %o3 ! add relocation offset
450 mov 1, %g1 ! For GRMON2 to exit normally.
451 ta 0 ! if call returns.. (unlikely)
453 b dead ! infinte loop
456 !------------------------------------------------------------------------------
458 /* Interrupt handler caller,
459 * reg L7: interrupt number
460 * reg L0: psr after interrupt
473 mov %l7, %o0 ! irq level
475 set (CONFIG_SYS_RELOC_MONITOR_BASE-CONFIG_SYS_TEXT_BASE), %o2
478 add %sp, SF_REGS_SZ, %o1 ! pt_regs ptr
479 or %l0, PSR_PIL, %g2 ! restore PIL after handler_irq
480 wr %g2, PSR_ET, %psr ! keep ET up
485 !------------------------------------------------------------------------------
488 * Window overflow trap handler
490 .global _window_overflow
494 mov %wim, %l3 ! Calculate next WIM
497 sll %l3, (CONFIG_SYS_SPARC_NWINDOWS-1), %l4
499 save ! Get into window to be saved.
502 st %l0, [%sp + 0] ! Save window to the stack
518 restore ! Go back to trap window.
520 jmp %l1 ! Re-execute save.
524 * Window underflow trap handler
526 .global _window_underflow
530 mov %wim, %l3 ! Calculate next WIM
531 srl %l3, (CONFIG_SYS_SPARC_NWINDOWS-1), %l5
536 restore ! Two restores to get into the
537 restore ! window to restore
538 ld [%sp + 0], %l0; ! Restore window from the stack
554 save ! Get back to the trap window.
556 jmp %l1 ! Re-execute restore.
559 !------------------------------------------------------------------------------
570 b _hwerr ! loop infinite
573 /* Registers to not touch at all. */
574 #define t_psr l0 /* Set by caller */
575 #define t_pc l1 /* Set by caller */
576 #define t_npc l2 /* Set by caller */
577 #define t_wim l3 /* Set by caller */
578 #define t_twinmask l4 /* Set at beginning of this entry routine. */
579 #define t_kstack l5 /* Set right before pt_regs frame is built */
580 #define t_retpc l6 /* If you change this, change winmacro.h header file */
581 #define t_systable l7 /* Never touch this, could be the syscall table ptr. */
582 #define curptr g6 /* Set after pt_regs frame is built */
585 /* build a pt_regs trap frame. */
586 sub %fp, (SF_REGS_SZ + PT_REGS_SZ), %t_kstack
587 PT_STORE_ALL(t_kstack, t_psr, t_pc, t_npc, g2)
589 /* See if we are in the trap window. */
591 sll %t_twinmask, %t_psr, %t_twinmask ! t_twinmask = (1 << psr)
592 andcc %t_twinmask, %t_wim, %g0
593 beq 1f ! in trap window, clean up
596 /*-------------------------------------------------
597 * Spill , adjust %wim and go.
599 srl %t_wim, 0x1, %g2 ! begin computation of new %wim
601 set (CONFIG_SYS_SPARC_NWINDOWS-1), %g3 !NWINDOWS-1
603 sll %t_wim, %g3, %t_wim ! NWINDOWS-1
607 save %g0, %g0, %g0 ! get in window to be saved
609 /* Set new %wim value */
612 /* Save the kernel window onto the corresponding stack. */
615 restore %g0, %g0, %g0
616 /*-------------------------------------------------*/
619 /* Trap from kernel with a window available.
622 jmpl %t_retpc + 0x8, %g0 ! return to caller
623 mov %t_kstack, %sp ! jump onto new stack
629 wr %t_psr, 0x0, %psr ! enable nesting again, clear ET
631 /* Will the rett land us in the invalid window? */
635 set CONFIG_SYS_SPARC_NWINDOWS, %g2 !NWINDOWS
641 be 1f ! Nope, just return from the trap
644 /* We have to grab a window before returning. */
645 set (CONFIG_SYS_SPARC_NWINDOWS-1), %g3 !NWINDOWS-1
653 /* Grrr, make sure we load from the right %sp... */
654 PT_LOAD_ALL(sp, t_psr, t_pc, t_npc, g1)
656 restore %g0, %g0, %g0
661 /* Reload the entire frame in case this is from a
662 * kernel system call or whatever...
665 PT_LOAD_ALL(sp, t_psr, t_pc, t_npc, g1)
675 /* This is called from relocated C-code.
676 * It resets the system by jumping to _start