paul@8 | 1 | /* |
paul@63 | 2 | * Initialisation code for the stage 2 payload. |
paul@63 | 3 | * |
paul@63 | 4 | * Copyright 2009 (C) Qi Hardware Inc. |
paul@63 | 5 | * Author: Wolfgang Spraul <wolfgang@sharism.cc> |
paul@63 | 6 | * |
paul@87 | 7 | * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@8 | 8 | * |
paul@63 | 9 | * This program is free software: you can redistribute it and/or modify |
paul@63 | 10 | * it under the terms of the GNU General Public License as published by |
paul@63 | 11 | * the Free Software Foundation, either version 3 of the License, or |
paul@63 | 12 | * (at your option) any later version. |
paul@63 | 13 | * |
paul@63 | 14 | * This program is distributed in the hope that it will be useful, |
paul@63 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@63 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@63 | 17 | * GNU General Public License for more details. |
paul@63 | 18 | * |
paul@63 | 19 | * You should have received a copy of the GNU General Public License |
paul@63 | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@8 | 21 | */ |
paul@8 | 22 | |
paul@73 | 23 | #include "sdram.h" |
paul@73 | 24 | |
paul@68 | 25 | .text |
paul@68 | 26 | .extern c_main |
paul@141 | 27 | .extern _tlb_entry |
paul@141 | 28 | .extern _exc_entry |
paul@68 | 29 | .extern _irq_entry |
paul@68 | 30 | .extern _end_entries |
paul@128 | 31 | .extern _got_start |
paul@128 | 32 | .extern _got_end |
paul@128 | 33 | .extern _got_copy_start |
paul@68 | 34 | .globl _start |
paul@68 | 35 | .set noreorder |
paul@68 | 36 | |
paul@8 | 37 | _start: |
paul@87 | 38 | b real_start |
paul@87 | 39 | nop |
paul@87 | 40 | |
paul@87 | 41 | /* Apparently reserved region which, if used, breaks the USB Boot process. */ |
paul@87 | 42 | |
paul@87 | 43 | .word 0x0 |
paul@87 | 44 | .word 0x0 |
paul@87 | 45 | .word 0x0 |
paul@87 | 46 | .word 0x0 |
paul@87 | 47 | .word 0x0 |
paul@87 | 48 | .word 0x0 |
paul@87 | 49 | .word 0x0 |
paul@87 | 50 | .word 0x0 |
paul@87 | 51 | |
paul@87 | 52 | real_start: |
paul@86 | 53 | /* Initialise the stack. */ |
paul@68 | 54 | |
paul@68 | 55 | la $sp, 0x80080000 |
paul@68 | 56 | |
paul@69 | 57 | /* Initialise the globals pointer. */ |
paul@69 | 58 | |
paul@69 | 59 | lui $gp, %hi(_GLOBAL_OFFSET_TABLE_) |
paul@69 | 60 | ori $gp, $gp, %lo(_GLOBAL_OFFSET_TABLE_) |
paul@69 | 61 | |
paul@75 | 62 | move $k0, $ra |
paul@75 | 63 | |
paul@114 | 64 | /* Copy TLB handling instructions. */ |
paul@114 | 65 | |
paul@114 | 66 | la $t0, _tlb_entry /* start */ |
paul@114 | 67 | li $t1, 0x80000000 |
paul@141 | 68 | la $t2, _exc_entry /* end */ |
paul@141 | 69 | jal _copy |
paul@141 | 70 | nop |
paul@141 | 71 | |
paul@141 | 72 | /* Copy exception handling instructions. */ |
paul@141 | 73 | |
paul@141 | 74 | la $t0, _exc_entry /* start */ |
paul@141 | 75 | li $t1, 0x80000180 |
paul@114 | 76 | la $t2, _irq_entry /* end */ |
paul@114 | 77 | jal _copy |
paul@114 | 78 | nop |
paul@114 | 79 | |
paul@68 | 80 | /* Copy IRQ handling instructions. */ |
paul@8 | 81 | |
paul@86 | 82 | la $t0, _irq_entry /* start */ |
paul@68 | 83 | li $t1, 0x80000200 |
paul@69 | 84 | la $t2, _end_entries /* end */ |
paul@75 | 85 | jal _copy |
paul@75 | 86 | nop |
paul@75 | 87 | |
paul@128 | 88 | /* Copy the offset table for user mode programs. */ |
paul@128 | 89 | |
paul@128 | 90 | la $t0, _got_start /* start */ |
paul@128 | 91 | la $t1, _got_copy_start |
paul@128 | 92 | la $t2, _got_end /* end */ |
paul@128 | 93 | li $t3, 0x80000000 /* adjustment */ |
paul@128 | 94 | jal _copy_adjust |
paul@128 | 95 | nop |
paul@128 | 96 | |
paul@75 | 97 | move $ra, $k0 |
paul@68 | 98 | |
paul@73 | 99 | /* Enable caching. */ |
paul@68 | 100 | |
paul@73 | 101 | li $t0, CONFIG_CM_CACHABLE_NONCOHERENT |
paul@73 | 102 | mtc0 $t0, $16 /* CP0_CONFIG */ |
paul@68 | 103 | nop |
paul@68 | 104 | |
paul@68 | 105 | /* Start the program. */ |
paul@68 | 106 | |
paul@122 | 107 | la $t9, c_main /* load the address of the routine */ |
paul@68 | 108 | j c_main |
paul@68 | 109 | nop |
paul@68 | 110 | |
paul@75 | 111 | _copy: |
paul@75 | 112 | /* Copy via $t3 the region from $t0 to $t2 into $t1. */ |
paul@75 | 113 | |
paul@75 | 114 | lw $t3, 0($t0) |
paul@75 | 115 | addiu $t0, $t0, 4 |
paul@75 | 116 | sw $t3, 0($t1) |
paul@75 | 117 | bne $t0, $t2, _copy |
paul@75 | 118 | addiu $t1, $t1, 4 /* executed in delay slot before branch */ |
paul@159 | 119 | jr $ra |
paul@75 | 120 | nop |
paul@75 | 121 | |
paul@128 | 122 | _copy_adjust: |
paul@128 | 123 | /* Copy via $t4 the region from $t0 to $t2 into $t1, adjusting by $t3. */ |
paul@128 | 124 | |
paul@128 | 125 | lw $t4, 0($t0) |
paul@128 | 126 | addiu $t0, $t0, 4 |
paul@128 | 127 | subu $t4, $t4, $t3 |
paul@128 | 128 | sw $t4, 0($t1) |
paul@128 | 129 | bne $t0, $t2, _copy_adjust |
paul@128 | 130 | addiu $t1, $t1, 4 /* executed in delay slot before branch */ |
paul@159 | 131 | jr $ra |
paul@128 | 132 | nop |
paul@128 | 133 | |
paul@68 | 134 | .set reorder |