wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * armboot - Startup Code for SA1100 CPU |
| 3 | * |
| 4 | * Copyright (C) 1998 Dan Malek <dmalek@jlc.net> |
| 5 | * Copyright (C) 1999 Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se> |
| 6 | * Copyright (C) 2000 Wolfgang Denk <wd@denx.de> |
| 7 | * Copyright (c) 2001 Alex Züpke <azu@sysgo.de> |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 29 | #include <config.h> |
| 30 | #include <version.h> |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | ************************************************************************* |
| 35 | * |
| 36 | * Jump vector table as in table 3.1 in [1] |
| 37 | * |
| 38 | ************************************************************************* |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | .globl _start |
| 43 | _start: b reset |
| 44 | ldr pc, _undefined_instruction |
| 45 | ldr pc, _software_interrupt |
| 46 | ldr pc, _prefetch_abort |
| 47 | ldr pc, _data_abort |
| 48 | ldr pc, _not_used |
| 49 | ldr pc, _irq |
| 50 | ldr pc, _fiq |
| 51 | |
| 52 | _undefined_instruction: .word undefined_instruction |
| 53 | _software_interrupt: .word software_interrupt |
| 54 | _prefetch_abort: .word prefetch_abort |
| 55 | _data_abort: .word data_abort |
| 56 | _not_used: .word not_used |
| 57 | _irq: .word irq |
| 58 | _fiq: .word fiq |
| 59 | |
| 60 | .balignl 16,0xdeadbeef |
| 61 | |
| 62 | |
| 63 | /* |
| 64 | ************************************************************************* |
| 65 | * |
| 66 | * Startup Code (reset vector) |
| 67 | * |
| 68 | * do important init only if we don't start from memory! |
| 69 | * relocate armboot to ram |
| 70 | * setup stack |
| 71 | * jump to second stage |
| 72 | * |
| 73 | ************************************************************************* |
| 74 | */ |
| 75 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 76 | _TEXT_BASE: |
| 77 | .word TEXT_BASE |
| 78 | |
| 79 | .globl _armboot_start |
| 80 | _armboot_start: |
| 81 | .word _start |
| 82 | |
| 83 | /* |
| 84 | * Note: _armboot_end_data and _armboot_end are defined |
| 85 | * by the (board-dependent) linker script. |
| 86 | * _armboot_end_data is the first usable FLASH address after armboot |
| 87 | */ |
| 88 | .globl _armboot_end_data |
| 89 | _armboot_end_data: |
| 90 | .word armboot_end_data |
| 91 | .globl _armboot_end |
| 92 | _armboot_end: |
| 93 | .word armboot_end |
| 94 | |
| 95 | /* |
| 96 | * _armboot_real_end is the first usable RAM address behind armboot |
| 97 | * and the various stacks |
| 98 | */ |
| 99 | .globl _armboot_real_end |
| 100 | _armboot_real_end: |
| 101 | .word 0x0badc0de |
| 102 | |
| 103 | #ifdef CONFIG_USE_IRQ |
| 104 | /* IRQ stack memory (calculated at run-time) */ |
| 105 | .globl IRQ_STACK_START |
| 106 | IRQ_STACK_START: |
| 107 | .word 0x0badc0de |
| 108 | |
| 109 | /* IRQ stack memory (calculated at run-time) */ |
| 110 | .globl FIQ_STACK_START |
| 111 | FIQ_STACK_START: |
| 112 | .word 0x0badc0de |
| 113 | #endif |
| 114 | |
| 115 | |
| 116 | /* |
| 117 | * the actual reset code |
| 118 | */ |
| 119 | |
| 120 | reset: |
| 121 | /* |
| 122 | * set the cpu to SVC32 mode |
| 123 | */ |
| 124 | mrs r0,cpsr |
| 125 | bic r0,r0,#0x1f |
| 126 | orr r0,r0,#0x13 |
| 127 | msr cpsr,r0 |
| 128 | |
| 129 | /* |
| 130 | * we do sys-critical inits only at reboot, |
| 131 | * not when booting from ram! |
| 132 | */ |
| 133 | #ifdef CONFIG_INIT_CRITICAL |
| 134 | bl cpu_init_crit |
| 135 | #endif |
| 136 | |
| 137 | relocate: |
| 138 | /* |
| 139 | * relocate armboot to RAM |
| 140 | */ |
| 141 | adr r0, _start /* r0 <- current position of code */ |
| 142 | ldr r2, _armboot_start |
| 143 | ldr r3, _armboot_end |
| 144 | sub r2, r3, r2 /* r2 <- size of armboot */ |
| 145 | ldr r1, _TEXT_BASE /* r1 <- destination address */ |
| 146 | add r2, r0, r2 /* r2 <- source end address */ |
| 147 | |
| 148 | /* |
| 149 | * r0 = source address |
| 150 | * r1 = target address |
| 151 | * r2 = source end address |
| 152 | */ |
| 153 | copy_loop: |
| 154 | ldmia r0!, {r3-r10} |
| 155 | stmia r1!, {r3-r10} |
| 156 | cmp r0, r2 |
| 157 | ble copy_loop |
| 158 | |
| 159 | /* set up the stack */ |
| 160 | ldr r0, _armboot_end |
| 161 | add r0, r0, #CONFIG_STACKSIZE |
| 162 | sub sp, r0, #12 /* leave 3 words for abort-stack */ |
| 163 | |
| 164 | ldr pc, _start_armboot |
| 165 | |
| 166 | _start_armboot: .word start_armboot |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | ************************************************************************* |
| 171 | * |
| 172 | * CPU_init_critical registers |
| 173 | * |
| 174 | * setup important registers |
| 175 | * setup memory timing |
| 176 | * |
| 177 | ************************************************************************* |
| 178 | */ |
| 179 | |
| 180 | |
| 181 | /* Interupt-Controller base address */ |
| 182 | IC_BASE: .word 0x90050000 |
| 183 | #define ICMR 0x04 |
| 184 | |
| 185 | |
| 186 | /* Reset-Controller */ |
| 187 | RST_BASE: .word 0x90030000 |
| 188 | #define RSRR 0x00 |
| 189 | #define RCSR 0x04 |
| 190 | |
| 191 | |
| 192 | /* PWR */ |
| 193 | PWR_BASE: .word 0x90020000 |
| 194 | #define PSPR 0x08 |
| 195 | #define PPCR 0x14 |
| 196 | cpuspeed: .word CFG_CPUSPEED |
| 197 | |
| 198 | |
| 199 | cpu_init_crit: |
| 200 | /* |
| 201 | * mask all IRQs |
| 202 | */ |
| 203 | ldr r0, IC_BASE |
| 204 | mov r1, #0x00 |
| 205 | str r1, [r0, #ICMR] |
| 206 | |
| 207 | /* set clock speed */ |
| 208 | ldr r0, PWR_BASE |
| 209 | ldr r1, cpuspeed |
| 210 | str r1, [r0, #PPCR] |
| 211 | |
| 212 | /* |
| 213 | * before relocating, we have to setup RAM timing |
| 214 | * because memory timing is board-dependend, you will |
| 215 | * find a memsetup.S in your board directory. |
| 216 | */ |
| 217 | mov ip, lr |
| 218 | bl memsetup |
| 219 | mov lr, ip |
| 220 | |
| 221 | /* |
| 222 | * disable MMU stuff and enable I-cache |
| 223 | */ |
| 224 | mrc p15,0,r0,c1,c0 |
| 225 | bic r0, r0, #0x00002000 @ clear bit 13 (X) |
| 226 | bic r0, r0, #0x0000000f @ clear bits 3-0 (WCAM) |
| 227 | orr r0, r0, #0x00001000 @ set bit 12 (I) Icache |
| 228 | orr r0, r0, #0x00000002 @ set bit 2 (A) Align |
| 229 | mcr p15,0,r0,c1,c0 |
| 230 | |
| 231 | /* |
| 232 | * flush v4 I/D caches |
| 233 | */ |
| 234 | mov r0, #0 |
| 235 | mcr p15, 0, r0, c7, c7, 0 /* flush v3/v4 cache */ |
| 236 | mcr p15, 0, r0, c8, c7, 0 /* flush v4 TLB */ |
| 237 | |
| 238 | mov pc, lr |
| 239 | |
| 240 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 241 | /* |
| 242 | ************************************************************************* |
| 243 | * |
| 244 | * Interrupt handling |
| 245 | * |
| 246 | ************************************************************************* |
| 247 | */ |
| 248 | |
| 249 | @ |
| 250 | @ IRQ stack frame. |
| 251 | @ |
| 252 | #define S_FRAME_SIZE 72 |
| 253 | |
| 254 | #define S_OLD_R0 68 |
| 255 | #define S_PSR 64 |
| 256 | #define S_PC 60 |
| 257 | #define S_LR 56 |
| 258 | #define S_SP 52 |
| 259 | |
| 260 | #define S_IP 48 |
| 261 | #define S_FP 44 |
| 262 | #define S_R10 40 |
| 263 | #define S_R9 36 |
| 264 | #define S_R8 32 |
| 265 | #define S_R7 28 |
| 266 | #define S_R6 24 |
| 267 | #define S_R5 20 |
| 268 | #define S_R4 16 |
| 269 | #define S_R3 12 |
| 270 | #define S_R2 8 |
| 271 | #define S_R1 4 |
| 272 | #define S_R0 0 |
| 273 | |
| 274 | #define MODE_SVC 0x13 |
| 275 | #define I_BIT 0x80 |
| 276 | |
| 277 | /* |
| 278 | * use bad_save_user_regs for abort/prefetch/undef/swi ... |
| 279 | * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling |
| 280 | */ |
| 281 | |
| 282 | .macro bad_save_user_regs |
| 283 | sub sp, sp, #S_FRAME_SIZE |
| 284 | stmia sp, {r0 - r12} @ Calling r0-r12 |
| 285 | add r8, sp, #S_PC |
| 286 | |
| 287 | ldr r2, _armboot_end |
| 288 | add r2, r2, #CONFIG_STACKSIZE |
| 289 | sub r2, r2, #8 |
| 290 | ldmia r2, {r2 - r4} @ get pc, cpsr, old_r0 |
| 291 | add r0, sp, #S_FRAME_SIZE @ restore sp_SVC |
| 292 | |
| 293 | add r5, sp, #S_SP |
| 294 | mov r1, lr |
| 295 | stmia r5, {r0 - r4} @ save sp_SVC, lr_SVC, pc, cpsr, old_r |
| 296 | mov r0, sp |
| 297 | .endm |
| 298 | |
| 299 | .macro irq_save_user_regs |
| 300 | sub sp, sp, #S_FRAME_SIZE |
| 301 | stmia sp, {r0 - r12} @ Calling r0-r12 |
| 302 | add r8, sp, #S_PC |
| 303 | stmdb r8, {sp, lr}^ @ Calling SP, LR |
| 304 | str lr, [r8, #0] @ Save calling PC |
| 305 | mrs r6, spsr |
| 306 | str r6, [r8, #4] @ Save CPSR |
| 307 | str r0, [r8, #8] @ Save OLD_R0 |
| 308 | mov r0, sp |
| 309 | .endm |
| 310 | |
| 311 | .macro irq_restore_user_regs |
| 312 | ldmia sp, {r0 - lr}^ @ Calling r0 - lr |
| 313 | mov r0, r0 |
| 314 | ldr lr, [sp, #S_PC] @ Get PC |
| 315 | add sp, sp, #S_FRAME_SIZE |
| 316 | subs pc, lr, #4 @ return & move spsr_svc into cpsr |
| 317 | .endm |
| 318 | |
| 319 | .macro get_bad_stack |
| 320 | ldr r13, _armboot_end @ setup our mode stack |
| 321 | add r13, r13, #CONFIG_STACKSIZE @ resides at top of normal stack |
| 322 | sub r13, r13, #8 |
| 323 | |
| 324 | str lr, [r13] @ save caller lr / spsr |
| 325 | mrs lr, spsr |
| 326 | str lr, [r13, #4] |
| 327 | |
| 328 | mov r13, #MODE_SVC @ prepare SVC-Mode |
| 329 | msr spsr_c, r13 |
| 330 | mov lr, pc |
| 331 | movs pc, lr |
| 332 | .endm |
| 333 | |
| 334 | .macro get_irq_stack @ setup IRQ stack |
| 335 | ldr sp, IRQ_STACK_START |
| 336 | .endm |
| 337 | |
| 338 | .macro get_fiq_stack @ setup FIQ stack |
| 339 | ldr sp, FIQ_STACK_START |
| 340 | .endm |
| 341 | |
| 342 | /* |
| 343 | * exception handlers |
| 344 | */ |
| 345 | .align 5 |
| 346 | undefined_instruction: |
| 347 | get_bad_stack |
| 348 | bad_save_user_regs |
| 349 | bl do_undefined_instruction |
| 350 | |
| 351 | .align 5 |
| 352 | software_interrupt: |
| 353 | get_bad_stack |
| 354 | bad_save_user_regs |
| 355 | bl do_software_interrupt |
| 356 | |
| 357 | .align 5 |
| 358 | prefetch_abort: |
| 359 | get_bad_stack |
| 360 | bad_save_user_regs |
| 361 | bl do_prefetch_abort |
| 362 | |
| 363 | .align 5 |
| 364 | data_abort: |
| 365 | get_bad_stack |
| 366 | bad_save_user_regs |
| 367 | bl do_data_abort |
| 368 | |
| 369 | .align 5 |
| 370 | not_used: |
| 371 | get_bad_stack |
| 372 | bad_save_user_regs |
| 373 | bl do_not_used |
| 374 | |
| 375 | #ifdef CONFIG_USE_IRQ |
| 376 | |
| 377 | .align 5 |
| 378 | irq: |
| 379 | get_irq_stack |
| 380 | irq_save_user_regs |
| 381 | bl do_irq |
| 382 | irq_restore_user_regs |
| 383 | |
| 384 | .align 5 |
| 385 | fiq: |
| 386 | get_fiq_stack |
| 387 | /* someone ought to write a more effiction fiq_save_user_regs */ |
| 388 | irq_save_user_regs |
| 389 | bl do_fiq |
| 390 | irq_restore_user_regs |
| 391 | |
| 392 | #else |
| 393 | |
| 394 | .align 5 |
| 395 | irq: |
| 396 | get_bad_stack |
| 397 | bad_save_user_regs |
| 398 | bl do_irq |
| 399 | |
| 400 | .align 5 |
| 401 | fiq: |
| 402 | get_bad_stack |
| 403 | bad_save_user_regs |
| 404 | bl do_fiq |
| 405 | |
| 406 | #endif |
| 407 | |
| 408 | .align 5 |
| 409 | .globl reset_cpu |
| 410 | reset_cpu: |
| 411 | ldr r0, RST_BASE |
| 412 | mov r1, #0x0 @ set bit 3-0 ... |
| 413 | str r1, [r0, #RCSR] @ ... to clear in RCSR |
| 414 | mov r1, #0x1 |
| 415 | str r1, [r0, #RSRR] @ and perform reset |
| 416 | b reset_cpu @ silly, but repeat endlessly |