Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Startup Code for MIPS32 CPU-core |
| 4 | * |
| 5 | * Copyright (c) 2003 Wolfgang Denk <wd@denx.de> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Wolfgang Denk | 0191e47 | 2010-10-26 14:34:52 +0200 | [diff] [blame] | 8 | #include <asm-offsets.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 9 | #include <config.h> |
Paul Burton | ce14da2 | 2015-01-29 10:04:08 +0000 | [diff] [blame] | 10 | #include <asm/asm.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 11 | #include <asm/regdef.h> |
| 12 | #include <asm/mipsregs.h> |
Tom Rini | 4ddbade | 2022-05-25 12:16:03 -0400 | [diff] [blame] | 13 | #include <system-constants.h> |
Daniel Schwierzeck | 2814459 | 2015-01-18 22:18:38 +0100 | [diff] [blame] | 14 | |
Paul Burton | cb2ab2f | 2015-01-29 10:04:09 +0000 | [diff] [blame] | 15 | #ifdef CONFIG_32BIT |
Paul Burton | debf0e0 | 2015-01-29 10:04:10 +0000 | [diff] [blame] | 16 | # define STATUS_SET 0 |
Paul Burton | cb2ab2f | 2015-01-29 10:04:09 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
| 19 | #ifdef CONFIG_64BIT |
Paul Burton | debf0e0 | 2015-01-29 10:04:10 +0000 | [diff] [blame] | 20 | # define STATUS_SET ST0_KX |
Paul Burton | cb2ab2f | 2015-01-29 10:04:09 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 23 | .set noreorder |
| 24 | |
Daniel Schwierzeck | ecf0d79 | 2016-02-08 00:37:59 +0100 | [diff] [blame] | 25 | .macro init_wr sel |
| 26 | MTC0 zero, CP0_WATCHLO,\sel |
| 27 | mtc0 t1, CP0_WATCHHI,\sel |
| 28 | mfc0 t0, CP0_WATCHHI,\sel |
| 29 | bgez t0, wr_done |
| 30 | nop |
| 31 | .endm |
| 32 | |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 33 | .macro uhi_mips_exception |
| 34 | move k0, t9 # preserve t9 in k0 |
| 35 | move k1, a0 # preserve a0 in k1 |
| 36 | li t9, 15 # UHI exception operation |
| 37 | li a0, 0 # Use hard register context |
| 38 | sdbbp 1 # Invoke UHI operation |
| 39 | .endm |
| 40 | |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 41 | .macro setup_stack_gd |
| 42 | li t0, -16 |
Tom Rini | 4ddbade | 2022-05-25 12:16:03 -0400 | [diff] [blame] | 43 | PTR_LI t1, SYS_INIT_SP_ADDR |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 44 | and sp, t1, t0 # force 16 byte alignment |
| 45 | PTR_SUBU \ |
| 46 | sp, sp, GD_SIZE # reserve space for gd |
| 47 | and sp, sp, t0 # force 16 byte alignment |
| 48 | move k0, sp # save gd pointer |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 49 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) && \ |
developer | 01a2828 | 2020-04-21 09:28:33 +0200 | [diff] [blame] | 50 | !CONFIG_IS_ENABLED(INIT_STACK_WITHOUT_MALLOC_F) |
Andy Yan | 984c10d | 2017-07-24 17:45:27 +0800 | [diff] [blame] | 51 | li t2, CONFIG_VAL(SYS_MALLOC_F_LEN) |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 52 | PTR_SUBU \ |
| 53 | sp, sp, t2 # reserve space for early malloc |
| 54 | and sp, sp, t0 # force 16 byte alignment |
| 55 | #endif |
| 56 | move fp, sp |
| 57 | |
| 58 | /* Clear gd */ |
| 59 | move t0, k0 |
| 60 | 1: |
| 61 | PTR_S zero, 0(t0) |
developer | de8b4cf | 2020-04-21 09:28:28 +0200 | [diff] [blame] | 62 | PTR_ADDIU t0, PTRSIZE |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 63 | blt t0, t1, 1b |
developer | de8b4cf | 2020-04-21 09:28:28 +0200 | [diff] [blame] | 64 | nop |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 65 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 66 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) && \ |
developer | 01a2828 | 2020-04-21 09:28:33 +0200 | [diff] [blame] | 67 | !CONFIG_IS_ENABLED(INIT_STACK_WITHOUT_MALLOC_F) |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 68 | PTR_S sp, GD_MALLOC_BASE(k0) # gd->malloc_base offset |
| 69 | #endif |
| 70 | .endm |
| 71 | |
Daniel Schwierzeck | 7509b57 | 2015-12-19 20:20:45 +0100 | [diff] [blame] | 72 | ENTRY(_start) |
Stefan Roese | 04c8b5a | 2020-10-28 15:09:59 +0100 | [diff] [blame] | 73 | /* |
| 74 | * U-Boot entry point. |
| 75 | * Do not add instructions to the branch delay slot! Some SoC's |
| 76 | * like Octeon might patch the final U-Boot binary at this location |
| 77 | * with additional boot headers. |
| 78 | */ |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 79 | b reset |
Stefan Roese | 04c8b5a | 2020-10-28 15:09:59 +0100 | [diff] [blame] | 80 | nop |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 81 | |
Daniel Schwierzeck | 2cc9a77 | 2018-09-07 19:18:44 +0200 | [diff] [blame] | 82 | #if defined(CONFIG_MIPS_INSERT_BOOT_CONFIG) |
Daniel Schwierzeck | 6ff8ae0 | 2011-07-27 13:22:37 +0200 | [diff] [blame] | 83 | /* |
Daniel Schwierzeck | 2cc9a77 | 2018-09-07 19:18:44 +0200 | [diff] [blame] | 84 | * Store some board-specific boot configuration. This is used by some |
| 85 | * MIPS systems like Malta. |
Daniel Schwierzeck | 6ff8ae0 | 2011-07-27 13:22:37 +0200 | [diff] [blame] | 86 | */ |
Daniel Schwierzeck | 754cd05 | 2016-02-14 18:52:57 +0100 | [diff] [blame] | 87 | .org 0x10 |
Daniel Schwierzeck | 2cc9a77 | 2018-09-07 19:18:44 +0200 | [diff] [blame] | 88 | .word CONFIG_MIPS_BOOT_CONFIG_WORD0 |
| 89 | .word CONFIG_MIPS_BOOT_CONFIG_WORD1 |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 90 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 91 | |
Daniel Schwierzeck | 754cd05 | 2016-02-14 18:52:57 +0100 | [diff] [blame] | 92 | #if defined(CONFIG_ROM_EXCEPTION_VECTORS) |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Exception vector entry points. When running from ROM, an exception |
| 95 | * cannot be handled. Halt execution and transfer control to debugger, |
| 96 | * if one is attached. |
| 97 | */ |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 98 | .org 0x200 |
| 99 | /* TLB refill, 32 bit task */ |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 100 | uhi_mips_exception |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 101 | |
| 102 | .org 0x280 |
| 103 | /* XTLB refill, 64 bit task */ |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 104 | uhi_mips_exception |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 105 | |
| 106 | .org 0x300 |
| 107 | /* Cache error exception */ |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 108 | uhi_mips_exception |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 109 | |
| 110 | .org 0x380 |
| 111 | /* General exception */ |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 112 | uhi_mips_exception |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 113 | |
| 114 | .org 0x400 |
| 115 | /* Catch interrupt exceptions */ |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 116 | uhi_mips_exception |
Daniel Schwierzeck | ec44316 | 2013-02-12 22:22:12 +0100 | [diff] [blame] | 117 | |
| 118 | .org 0x480 |
| 119 | /* EJTAG debug exception */ |
| 120 | 1: b 1b |
| 121 | nop |
| 122 | |
Daniel Schwierzeck | 754cd05 | 2016-02-14 18:52:57 +0100 | [diff] [blame] | 123 | .org 0x500 |
| 124 | #endif |
| 125 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 126 | reset: |
Stefan Roese | 04c8b5a | 2020-10-28 15:09:59 +0100 | [diff] [blame] | 127 | mtc0 zero, CP0_COUNT # clear cp0 count for most accurate boot timing |
Paul Burton | fcdc1fb | 2016-09-21 14:59:54 +0100 | [diff] [blame] | 128 | #if __mips_isa_rev >= 6 |
| 129 | mfc0 t0, CP0_CONFIG, 5 |
| 130 | and t0, t0, MIPS_CONF5_VP |
| 131 | beqz t0, 1f |
| 132 | nop |
| 133 | |
| 134 | b 2f |
| 135 | mfc0 t0, CP0_GLOBALNUMBER |
| 136 | #endif |
| 137 | |
Álvaro Fernández Rojas | 98a97a8 | 2017-04-25 00:39:20 +0200 | [diff] [blame] | 138 | #ifdef CONFIG_ARCH_BMIPS |
| 139 | 1: mfc0 t0, CP0_DIAGNOSTIC, 3 |
| 140 | and t0, t0, (1 << 31) |
| 141 | #else |
Paul Burton | fcdc1fb | 2016-09-21 14:59:54 +0100 | [diff] [blame] | 142 | 1: mfc0 t0, CP0_EBASE |
Daniel Schwierzeck | e4ccb47 | 2020-07-12 01:46:18 +0200 | [diff] [blame] | 143 | and t0, t0, MIPS_EBASE_CPUNUM |
Álvaro Fernández Rojas | 98a97a8 | 2017-04-25 00:39:20 +0200 | [diff] [blame] | 144 | #endif |
Paul Burton | fcdc1fb | 2016-09-21 14:59:54 +0100 | [diff] [blame] | 145 | |
| 146 | /* Hang if this isn't the first CPU in the system */ |
| 147 | 2: beqz t0, 4f |
| 148 | nop |
| 149 | 3: wait |
| 150 | b 3b |
| 151 | nop |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 152 | |
Daniel Schwierzeck | ecf0d79 | 2016-02-08 00:37:59 +0100 | [diff] [blame] | 153 | /* Init CP0 Status */ |
| 154 | 4: mfc0 t0, CP0_STATUS |
| 155 | and t0, ST0_IMPL |
| 156 | or t0, ST0_BEV | ST0_ERL | STATUS_SET |
| 157 | mtc0 t0, CP0_STATUS |
| 158 | |
| 159 | /* |
| 160 | * Check whether CP0 Config1 is implemented. If not continue |
| 161 | * with legacy Watch register initialization. |
| 162 | */ |
| 163 | mfc0 t0, CP0_CONFIG |
| 164 | bgez t0, wr_legacy |
| 165 | nop |
| 166 | |
| 167 | /* |
| 168 | * Check WR bit in CP0 Config1 to determine if Watch registers |
| 169 | * are implemented. |
| 170 | */ |
| 171 | mfc0 t0, CP0_CONFIG, 1 |
| 172 | andi t0, (1 << 3) |
| 173 | beqz t0, wr_done |
| 174 | nop |
| 175 | |
| 176 | /* Clear Watch Status bits and disable watch exceptions */ |
| 177 | li t1, 0x7 # Clear I, R and W conditions |
| 178 | init_wr 0 |
| 179 | init_wr 1 |
| 180 | init_wr 2 |
| 181 | init_wr 3 |
| 182 | init_wr 4 |
| 183 | init_wr 5 |
| 184 | init_wr 6 |
| 185 | init_wr 7 |
| 186 | b wr_done |
| 187 | nop |
| 188 | |
| 189 | wr_legacy: |
| 190 | MTC0 zero, CP0_WATCHLO |
Daniel Schwierzeck | 7aa7164 | 2016-01-09 22:24:47 +0100 | [diff] [blame] | 191 | mtc0 zero, CP0_WATCHHI |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 192 | |
Daniel Schwierzeck | ecf0d79 | 2016-02-08 00:37:59 +0100 | [diff] [blame] | 193 | wr_done: |
| 194 | /* Clear WP, IV and SW interrupts */ |
Shinya Kuribayashi | 79727f8 | 2008-03-25 21:30:07 +0900 | [diff] [blame] | 195 | mtc0 zero, CP0_CAUSE |
| 196 | |
Daniel Schwierzeck | ecf0d79 | 2016-02-08 00:37:59 +0100 | [diff] [blame] | 197 | /* Clear timer interrupt (CP0_COUNT cleared on branch to 'reset') */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 198 | mtc0 zero, CP0_COMPARE |
| 199 | |
Daniel Schwierzeck | c95e7f1 | 2020-07-12 00:45:57 +0200 | [diff] [blame] | 200 | #ifdef CONFIG_MIPS_CACHE_DISABLE |
Daniel Schwierzeck | 765f417 | 2020-07-12 00:45:56 +0200 | [diff] [blame] | 201 | /* Disable caches */ |
| 202 | PTR_LA t9, mips_cache_disable |
| 203 | jalr t9 |
| 204 | nop |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 205 | #endif |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 206 | |
Paul Burton | 79ac174 | 2016-09-21 11:18:53 +0100 | [diff] [blame] | 207 | #ifdef CONFIG_MIPS_CM |
| 208 | PTR_LA t9, mips_cm_map |
| 209 | jalr t9 |
| 210 | nop |
| 211 | #endif |
| 212 | |
Daniel Schwierzeck | 41dc35e | 2016-06-04 16:13:21 +0200 | [diff] [blame] | 213 | #ifdef CONFIG_MIPS_INIT_STACK_IN_SRAM |
developer | eb7d3a2 | 2020-04-21 09:28:27 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_MIPS_SRAM_INIT |
| 215 | /* Initialize the SRAM first */ |
| 216 | PTR_LA t9, mips_sram_init |
| 217 | jalr t9 |
| 218 | nop |
| 219 | #endif |
| 220 | |
Daniel Schwierzeck | 41dc35e | 2016-06-04 16:13:21 +0200 | [diff] [blame] | 221 | /* Set up initial stack and global data */ |
| 222 | setup_stack_gd |
Daniel Schwierzeck | fd32b13 | 2017-04-24 19:03:34 +0200 | [diff] [blame] | 223 | |
| 224 | # ifdef CONFIG_DEBUG_UART |
| 225 | /* Earliest point to set up debug uart */ |
| 226 | PTR_LA t9, debug_uart_init |
| 227 | jalr t9 |
| 228 | nop |
| 229 | # endif |
Daniel Schwierzeck | 41dc35e | 2016-06-04 16:13:21 +0200 | [diff] [blame] | 230 | #endif |
| 231 | |
Tom Rini | e1e8544 | 2021-08-27 21:18:30 -0400 | [diff] [blame] | 232 | #if !CONFIG_IS_ENABLED(SKIP_LOWLEVEL_INIT) |
Paul Burton | 68b4c75 | 2016-09-21 11:18:51 +0100 | [diff] [blame] | 233 | # ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 234 | /* Initialize any external memory */ |
Paul Burton | ce14da2 | 2015-01-29 10:04:08 +0000 | [diff] [blame] | 235 | PTR_LA t9, lowlevel_init |
Shinya Kuribayashi | c7faac5 | 2007-10-27 15:27:06 +0900 | [diff] [blame] | 236 | jalr t9 |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 237 | nop |
Paul Burton | 68b4c75 | 2016-09-21 11:18:51 +0100 | [diff] [blame] | 238 | # endif |
Daniel Schwierzeck | c95e7f1 | 2020-07-12 00:45:57 +0200 | [diff] [blame] | 239 | #endif |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 240 | |
Stefan Roese | c6f54b4 | 2020-06-30 12:33:16 +0200 | [diff] [blame] | 241 | #ifdef CONFIG_MIPS_MACH_EARLY_INIT |
| 242 | bal mips_mach_early_init |
| 243 | nop |
| 244 | #endif |
| 245 | |
Daniel Schwierzeck | c95e7f1 | 2020-07-12 00:45:57 +0200 | [diff] [blame] | 246 | #ifdef CONFIG_MIPS_CACHE_SETUP |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 247 | /* Initialize caches... */ |
Paul Burton | ce14da2 | 2015-01-29 10:04:08 +0000 | [diff] [blame] | 248 | PTR_LA t9, mips_cache_reset |
Shinya Kuribayashi | c7faac5 | 2007-10-27 15:27:06 +0900 | [diff] [blame] | 249 | jalr t9 |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 250 | nop |
Daniel Schwierzeck | c95e7f1 | 2020-07-12 00:45:57 +0200 | [diff] [blame] | 251 | #endif |
Paul Burton | 68b4c75 | 2016-09-21 11:18:51 +0100 | [diff] [blame] | 252 | |
Tom Rini | e1e8544 | 2021-08-27 21:18:30 -0400 | [diff] [blame] | 253 | #if !CONFIG_IS_ENABLED(SKIP_LOWLEVEL_INIT) |
Paul Burton | 68b4c75 | 2016-09-21 11:18:51 +0100 | [diff] [blame] | 254 | # ifndef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD |
| 255 | /* Initialize any external memory */ |
| 256 | PTR_LA t9, lowlevel_init |
| 257 | jalr t9 |
| 258 | nop |
| 259 | # endif |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 260 | #endif |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 261 | |
Daniel Schwierzeck | 41dc35e | 2016-06-04 16:13:21 +0200 | [diff] [blame] | 262 | #ifndef CONFIG_MIPS_INIT_STACK_IN_SRAM |
Daniel Schwierzeck | 993a122 | 2016-09-25 18:36:38 +0200 | [diff] [blame] | 263 | /* Set up initial stack and global data */ |
| 264 | setup_stack_gd |
Daniel Schwierzeck | fd32b13 | 2017-04-24 19:03:34 +0200 | [diff] [blame] | 265 | |
| 266 | # ifdef CONFIG_DEBUG_UART |
| 267 | /* Earliest point to set up debug uart */ |
| 268 | PTR_LA t9, debug_uart_init |
| 269 | jalr t9 |
| 270 | nop |
| 271 | # endif |
Daniel Schwierzeck | 41dc35e | 2016-06-04 16:13:21 +0200 | [diff] [blame] | 272 | #endif |
Daniel Schwierzeck | 7aa7164 | 2016-01-09 22:24:47 +0100 | [diff] [blame] | 273 | |
Purna Chandra Mandal | 5c8cdf4 | 2016-01-21 20:02:51 +0530 | [diff] [blame] | 274 | move a0, zero # a0 <-- boot_flags = 0 |
Paul Burton | ce14da2 | 2015-01-29 10:04:08 +0000 | [diff] [blame] | 275 | PTR_LA t9, board_init_f |
Daniel Schwierzeck | 8b2fd07 | 2016-02-07 19:39:58 +0100 | [diff] [blame] | 276 | |
Shinya Kuribayashi | 9dabea1 | 2008-04-17 23:35:13 +0900 | [diff] [blame] | 277 | jr t9 |
Daniel Schwierzeck | f224c1a | 2014-11-20 23:55:32 +0100 | [diff] [blame] | 278 | move ra, zero |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 279 | |
Daniel Schwierzeck | 7509b57 | 2015-12-19 20:20:45 +0100 | [diff] [blame] | 280 | END(_start) |