wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1998 Dan Malek <dmalek@jlc.net> |
| 3 | * Copyright (C) 1999 Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se> |
| 4 | * Copyright (C) 2000,2001,2002 Wolfgang Denk <wd@denx.de> |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | /* U-Boot - Startup Code for PowerPC based Embedded Boards |
| 26 | * |
| 27 | * |
| 28 | * The processor starts at 0x00000100 and the code is executed |
| 29 | * from flash. The code is organized to be at an other address |
| 30 | * in memory, but as long we don't jump around before relocating. |
| 31 | * board_init lies at a quite high address and when the cpu has |
| 32 | * jumped there, everything is ok. |
| 33 | * This works because the cpu gives the FLASH (CS0) the whole |
| 34 | * address space at startup, and board_init lies as a echo of |
| 35 | * the flash somewhere up there in the memorymap. |
| 36 | * |
| 37 | * board_init will change CS0 to be positioned at the correct |
| 38 | * address and (s)dram will be positioned at address 0 |
| 39 | */ |
| 40 | #include <config.h> |
| 41 | #include <mpc824x.h> |
| 42 | #include <version.h> |
| 43 | |
| 44 | #define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */ |
| 45 | |
| 46 | #include <ppc_asm.tmpl> |
| 47 | #include <ppc_defs.h> |
| 48 | |
| 49 | #include <asm/cache.h> |
| 50 | #include <asm/mmu.h> |
| 51 | |
| 52 | #ifndef CONFIG_IDENT_STRING |
| 53 | #define CONFIG_IDENT_STRING "" |
| 54 | #endif |
| 55 | |
| 56 | /* We don't want the MMU yet. |
| 57 | */ |
| 58 | #undef MSR_KERNEL |
| 59 | /* FP, Machine Check and Recoverable Interr. */ |
| 60 | #define MSR_KERNEL ( MSR_FP | MSR_ME | MSR_RI ) |
| 61 | |
| 62 | /* |
| 63 | * Set up GOT: Global Offset Table |
| 64 | * |
| 65 | * Use r14 to access the GOT |
| 66 | */ |
| 67 | START_GOT |
| 68 | GOT_ENTRY(_GOT2_TABLE_) |
| 69 | GOT_ENTRY(_FIXUP_TABLE_) |
| 70 | |
| 71 | GOT_ENTRY(_start) |
| 72 | GOT_ENTRY(_start_of_vectors) |
| 73 | GOT_ENTRY(_end_of_vectors) |
| 74 | GOT_ENTRY(transfer_to_handler) |
| 75 | |
| 76 | GOT_ENTRY(_end) |
| 77 | GOT_ENTRY(.bss) |
| 78 | #if defined(CONFIG_FADS) |
| 79 | GOT_ENTRY(environment) |
| 80 | #endif |
| 81 | END_GOT |
| 82 | |
| 83 | /* |
| 84 | * r3 - 1st arg to board_init(): IMMP pointer |
| 85 | * r4 - 2nd arg to board_init(): boot flag |
| 86 | */ |
| 87 | .text |
| 88 | .long 0x27051956 /* U-Boot Magic Number */ |
| 89 | .globl version_string |
| 90 | version_string: |
| 91 | .ascii U_BOOT_VERSION |
| 92 | .ascii " (", __DATE__, " - ", __TIME__, ")" |
| 93 | .ascii CONFIG_IDENT_STRING, "\0" |
| 94 | |
| 95 | . = EXC_OFF_SYS_RESET |
| 96 | .globl _start |
| 97 | _start: |
| 98 | li r21, BOOTFLAG_COLD /* Normal Power-On: Boot from FLASH */ |
| 99 | b boot_cold |
| 100 | |
| 101 | . = EXC_OFF_SYS_RESET + 0x10 |
| 102 | |
| 103 | .globl _start_warm |
| 104 | _start_warm: |
| 105 | li r21, BOOTFLAG_WARM /* Software reboot */ |
| 106 | b boot_warm |
| 107 | |
| 108 | boot_cold: |
| 109 | boot_warm: |
| 110 | |
| 111 | /* Initialize machine status; enable machine check interrupt */ |
| 112 | /*----------------------------------------------------------------------*/ |
| 113 | li r3, MSR_KERNEL /* Set FP, ME, RI flags */ |
| 114 | mtmsr r3 |
| 115 | mtspr SRR1, r3 /* Make SRR1 match MSR */ |
| 116 | |
| 117 | addis r0,0,0x0000 /* lets make sure that r0 is really 0 */ |
| 118 | mtspr HID0, r0 /* disable I and D caches */ |
| 119 | |
| 120 | mfspr r3, ICR /* clear Interrupt Cause Register */ |
| 121 | |
| 122 | mfmsr r3 /* turn off address translation */ |
| 123 | addis r4,0,0xffff |
| 124 | ori r4,r4,0xffcf |
| 125 | and r3,r3,r4 |
| 126 | mtmsr r3 |
| 127 | isync |
| 128 | sync /* the MMU should be off... */ |
| 129 | |
| 130 | |
| 131 | in_flash: |
| 132 | #if defined(CONFIG_BMW) |
| 133 | bl early_init_f /* Must be ASM: no stack yet! */ |
| 134 | #endif |
| 135 | /* |
| 136 | * Setup BATs - cannot be done in C since we don't have a stack yet |
| 137 | */ |
| 138 | bl setup_bats |
| 139 | |
| 140 | /* Enable MMU. |
| 141 | */ |
| 142 | mfmsr r3 |
| 143 | ori r3, r3, (MSR_IR | MSR_DR) |
| 144 | mtmsr r3 |
| 145 | #if !defined(CONFIG_BMW) |
| 146 | /* Enable and invalidate data cache. |
| 147 | */ |
| 148 | mfspr r3, HID0 |
| 149 | mr r2, r3 |
| 150 | ori r3, r3, HID0_DCE | HID0_DCI |
| 151 | ori r2, r2, HID0_DCE |
| 152 | sync |
| 153 | mtspr HID0, r3 |
| 154 | mtspr HID0, r2 |
| 155 | sync |
| 156 | |
| 157 | /* Allocate Initial RAM in data cache. |
| 158 | */ |
| 159 | lis r3, CFG_INIT_RAM_ADDR@h |
| 160 | ori r3, r3, CFG_INIT_RAM_ADDR@l |
| 161 | li r2, 128 |
| 162 | mtctr r2 |
| 163 | 1: |
| 164 | dcbz r0, r3 |
| 165 | addi r3, r3, 32 |
| 166 | bdnz 1b |
| 167 | |
| 168 | /* Lock way0 in data cache. |
| 169 | */ |
| 170 | mfspr r3, 1011 |
| 171 | lis r2, 0xffff |
| 172 | ori r2, r2, 0xff1f |
| 173 | and r3, r3, r2 |
| 174 | ori r3, r3, 0x0080 |
| 175 | sync |
| 176 | mtspr 1011, r3 |
| 177 | #endif /* !CONFIG_BMW */ |
| 178 | /* |
| 179 | * Thisk the stack pointer *somewhere* sensible. Doesnt |
| 180 | * matter much where as we'll move it when we relocate |
| 181 | */ |
| 182 | lis r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@h |
| 183 | ori r1, r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@l |
| 184 | |
| 185 | li r0, 0 /* Make room for stack frame header and */ |
| 186 | stwu r0, -4(r1) /* clear final stack frame so that */ |
| 187 | stwu r0, -4(r1) /* stack backtraces terminate cleanly */ |
| 188 | |
| 189 | /* let the C-code set up the rest */ |
| 190 | /* */ |
| 191 | /* Be careful to keep code relocatable ! */ |
| 192 | /*----------------------------------------------------------------------*/ |
| 193 | |
| 194 | GET_GOT /* initialize GOT access */ |
| 195 | |
| 196 | /* r3: IMMR */ |
| 197 | bl cpu_init_f /* run low-level CPU init code (from Flash) */ |
| 198 | |
| 199 | mr r3, r21 |
| 200 | /* r3: BOOTFLAG */ |
| 201 | bl board_init_f /* run 1st part of board init code (from Flash) */ |
| 202 | |
| 203 | |
| 204 | |
| 205 | .globl _start_of_vectors |
| 206 | _start_of_vectors: |
| 207 | |
| 208 | /* Machine check */ |
| 209 | STD_EXCEPTION(EXC_OFF_MACH_CHCK, MachineCheck, MachineCheckException) |
| 210 | |
| 211 | /* Data Storage exception. "Never" generated on the 860. */ |
| 212 | STD_EXCEPTION(EXC_OFF_DATA_STOR, DataStorage, UnknownException) |
| 213 | |
| 214 | /* Instruction Storage exception. "Never" generated on the 860. */ |
| 215 | STD_EXCEPTION(EXC_OFF_INS_STOR, InstStorage, UnknownException) |
| 216 | |
| 217 | /* External Interrupt exception. */ |
| 218 | STD_EXCEPTION(EXC_OFF_EXTERNAL, ExtInterrupt, external_interrupt) |
| 219 | |
| 220 | /* Alignment exception. */ |
| 221 | . = EXC_OFF_ALIGN |
| 222 | Alignment: |
| 223 | EXCEPTION_PROLOG |
| 224 | mfspr r4,DAR |
| 225 | stw r4,_DAR(r21) |
| 226 | mfspr r5,DSISR |
| 227 | stw r5,_DSISR(r21) |
| 228 | addi r3,r1,STACK_FRAME_OVERHEAD |
| 229 | li r20,MSR_KERNEL |
| 230 | rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */ |
| 231 | lwz r6,GOT(transfer_to_handler) |
| 232 | mtlr r6 |
| 233 | blrl |
| 234 | .L_Alignment: |
| 235 | .long AlignmentException - _start + EXC_OFF_SYS_RESET |
| 236 | .long int_return - _start + EXC_OFF_SYS_RESET |
| 237 | |
| 238 | /* Program check exception */ |
| 239 | . = EXC_OFF_PROGRAM |
| 240 | ProgramCheck: |
| 241 | EXCEPTION_PROLOG |
| 242 | addi r3,r1,STACK_FRAME_OVERHEAD |
| 243 | li r20,MSR_KERNEL |
| 244 | rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */ |
| 245 | lwz r6,GOT(transfer_to_handler) |
| 246 | mtlr r6 |
| 247 | blrl |
| 248 | .L_ProgramCheck: |
| 249 | .long ProgramCheckException - _start + EXC_OFF_SYS_RESET |
| 250 | .long int_return - _start + EXC_OFF_SYS_RESET |
| 251 | |
| 252 | /* No FPU on MPC8xx. This exception is not supposed to happen. |
| 253 | */ |
| 254 | STD_EXCEPTION(EXC_OFF_FPUNAVAIL, FPUnavailable, UnknownException) |
| 255 | |
| 256 | /* I guess we could implement decrementer, and may have |
| 257 | * to someday for timekeeping. |
| 258 | */ |
| 259 | STD_EXCEPTION(EXC_OFF_DECR, Decrementer, timer_interrupt) |
| 260 | STD_EXCEPTION(0xa00, Trap_0a, UnknownException) |
| 261 | STD_EXCEPTION(0xb00, Trap_0b, UnknownException) |
| 262 | |
| 263 | . = 0xc00 |
| 264 | /* |
| 265 | * r0 - SYSCALL number |
| 266 | * r3-... arguments |
| 267 | */ |
| 268 | SystemCall: |
| 269 | addis r11,r0,0 /* get functions table addr */ |
| 270 | ori r11,r11,0 /* Note: this code is patched in trap_init */ |
| 271 | addis r12,r0,0 /* get number of functions */ |
| 272 | ori r12,r12,0 |
| 273 | |
| 274 | cmplw 0, r0, r12 |
| 275 | bge 1f |
| 276 | |
| 277 | rlwinm r0,r0,2,0,31 /* fn_addr = fn_tbl[r0] */ |
| 278 | add r11,r11,r0 |
| 279 | lwz r11,0(r11) |
| 280 | |
| 281 | li r12,0xd00-4*3 /* save LR & SRRx */ |
| 282 | mflr r0 |
| 283 | stw r0,0(r12) |
| 284 | mfspr r0,SRR0 |
| 285 | stw r0,4(r12) |
| 286 | mfspr r0,SRR1 |
| 287 | stw r0,8(r12) |
| 288 | |
| 289 | li r12,0xc00+_back-SystemCall |
| 290 | mtlr r12 |
| 291 | mtspr SRR0,r11 |
| 292 | |
| 293 | 1: SYNC |
| 294 | rfi |
| 295 | |
| 296 | _back: |
| 297 | |
| 298 | mfmsr r11 /* Disable interrupts */ |
| 299 | li r12,0 |
| 300 | ori r12,r12,MSR_EE |
| 301 | andc r11,r11,r12 |
| 302 | SYNC /* Some chip revs need this... */ |
| 303 | mtmsr r11 |
| 304 | SYNC |
| 305 | |
| 306 | li r12,0xd00-4*3 /* restore regs */ |
| 307 | lwz r11,0(r12) |
| 308 | mtlr r11 |
| 309 | lwz r11,4(r12) |
| 310 | mtspr SRR0,r11 |
| 311 | lwz r11,8(r12) |
| 312 | mtspr SRR1,r11 |
| 313 | |
| 314 | SYNC |
| 315 | rfi |
| 316 | |
| 317 | STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException) |
| 318 | |
| 319 | STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException) |
| 320 | STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException) |
| 321 | |
| 322 | STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException) |
| 323 | STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException) |
| 324 | STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException) |
| 325 | STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, UnknownException) |
| 326 | STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException) |
| 327 | STD_EXCEPTION(0x1500, Reserved5, UnknownException) |
| 328 | STD_EXCEPTION(0x1600, Reserved6, UnknownException) |
| 329 | STD_EXCEPTION(0x1700, Reserved7, UnknownException) |
| 330 | STD_EXCEPTION(0x1800, Reserved8, UnknownException) |
| 331 | STD_EXCEPTION(0x1900, Reserved9, UnknownException) |
| 332 | STD_EXCEPTION(0x1a00, ReservedA, UnknownException) |
| 333 | STD_EXCEPTION(0x1b00, ReservedB, UnknownException) |
| 334 | STD_EXCEPTION(0x1c00, ReservedC, UnknownException) |
| 335 | STD_EXCEPTION(0x1d00, ReservedD, UnknownException) |
| 336 | STD_EXCEPTION(0x1e00, ReservedE, UnknownException) |
| 337 | STD_EXCEPTION(0x1f00, ReservedF, UnknownException) |
| 338 | |
| 339 | STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException) |
| 340 | |
| 341 | .globl _end_of_vectors |
| 342 | _end_of_vectors: |
| 343 | |
| 344 | |
| 345 | . = 0x3000 |
| 346 | |
| 347 | /* |
| 348 | * This code finishes saving the registers to the exception frame |
| 349 | * and jumps to the appropriate handler for the exception. |
| 350 | * Register r21 is pointer into trap frame, r1 has new stack pointer. |
| 351 | */ |
| 352 | .globl transfer_to_handler |
| 353 | transfer_to_handler: |
| 354 | stw r22,_NIP(r21) |
| 355 | lis r22,MSR_POW@h |
| 356 | andc r23,r23,r22 |
| 357 | stw r23,_MSR(r21) |
| 358 | SAVE_GPR(7, r21) |
| 359 | SAVE_4GPRS(8, r21) |
| 360 | SAVE_8GPRS(12, r21) |
| 361 | SAVE_8GPRS(24, r21) |
| 362 | #if 0 |
| 363 | andi. r23,r23,MSR_PR |
| 364 | mfspr r23,SPRG3 /* if from user, fix up tss.regs */ |
| 365 | beq 2f |
| 366 | addi r24,r1,STACK_FRAME_OVERHEAD |
| 367 | stw r24,PT_REGS(r23) |
| 368 | 2: addi r2,r23,-TSS /* set r2 to current */ |
| 369 | tovirt(r2,r2,r23) |
| 370 | #endif |
| 371 | mflr r23 |
| 372 | andi. r24,r23,0x3f00 /* get vector offset */ |
| 373 | stw r24,TRAP(r21) |
| 374 | li r22,0 |
| 375 | stw r22,RESULT(r21) |
| 376 | mtspr SPRG2,r22 /* r1 is now kernel sp */ |
| 377 | #if 0 |
| 378 | addi r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */ |
| 379 | cmplw 0,r1,r2 |
| 380 | cmplw 1,r1,r24 |
| 381 | crand 1,1,4 |
| 382 | bgt stack_ovf /* if r2 < r1 < r2+TASK_STRUCT_SIZE */ |
| 383 | #endif |
| 384 | lwz r24,0(r23) /* virtual address of handler */ |
| 385 | lwz r23,4(r23) /* where to go when done */ |
| 386 | mtspr SRR0,r24 |
| 387 | ori r20,r20,0x30 /* enable IR, DR */ |
| 388 | mtspr SRR1,r20 |
| 389 | mtlr r23 |
| 390 | SYNC |
| 391 | rfi /* jump to handler, enable MMU */ |
| 392 | |
| 393 | int_return: |
| 394 | mfmsr r28 /* Disable interrupts */ |
| 395 | li r4,0 |
| 396 | ori r4,r4,MSR_EE |
| 397 | andc r28,r28,r4 |
| 398 | SYNC /* Some chip revs need this... */ |
| 399 | mtmsr r28 |
| 400 | SYNC |
| 401 | lwz r2,_CTR(r1) |
| 402 | lwz r0,_LINK(r1) |
| 403 | mtctr r2 |
| 404 | mtlr r0 |
| 405 | lwz r2,_XER(r1) |
| 406 | lwz r0,_CCR(r1) |
| 407 | mtspr XER,r2 |
| 408 | mtcrf 0xFF,r0 |
| 409 | REST_10GPRS(3, r1) |
| 410 | REST_10GPRS(13, r1) |
| 411 | REST_8GPRS(23, r1) |
| 412 | REST_GPR(31, r1) |
| 413 | lwz r2,_NIP(r1) /* Restore environment */ |
| 414 | lwz r0,_MSR(r1) |
| 415 | mtspr SRR0,r2 |
| 416 | mtspr SRR1,r0 |
| 417 | lwz r0,GPR0(r1) |
| 418 | lwz r2,GPR2(r1) |
| 419 | lwz r1,GPR1(r1) |
| 420 | SYNC |
| 421 | rfi |
| 422 | |
| 423 | /* Cache functions. |
| 424 | */ |
| 425 | .globl icache_enable |
| 426 | icache_enable: |
| 427 | mfspr r5,HID0 /* turn on the I cache. */ |
| 428 | ori r5,r5,0x8800 /* Instruction cache only! */ |
| 429 | addis r6,0,0xFFFF |
| 430 | ori r6,r6,0xF7FF |
| 431 | and r6,r5,r6 /* clear the invalidate bit */ |
| 432 | sync |
| 433 | mtspr HID0,r5 |
| 434 | mtspr HID0,r6 |
| 435 | isync |
| 436 | sync |
| 437 | blr |
| 438 | |
| 439 | .globl icache_disable |
| 440 | icache_disable: |
| 441 | mfspr r5,HID0 |
| 442 | addis r6,0,0xFFFF |
| 443 | ori r6,r6,0x7FFF |
| 444 | and r5,r5,r6 |
| 445 | sync |
| 446 | mtspr HID0,r5 |
| 447 | isync |
| 448 | sync |
| 449 | blr |
| 450 | |
| 451 | .globl icache_status |
| 452 | icache_status: |
| 453 | mfspr r3, HID0 |
| 454 | srwi r3, r3, 15 /* >>15 & 1=> select bit 16 */ |
| 455 | andi. r3, r3, 1 |
| 456 | blr |
| 457 | |
| 458 | .globl dcache_enable |
| 459 | dcache_enable: |
| 460 | mfspr r5,HID0 /* turn on the D cache. */ |
| 461 | ori r5,r5,0x4400 /* Data cache only! */ |
| 462 | mfspr r4, PVR /* read PVR */ |
| 463 | srawi r3, r4, 16 /* shift off the least 16 bits */ |
| 464 | cmpi 0, 0, r3, 0xC /* Check for Max pvr */ |
| 465 | bne NotMax |
| 466 | ori r5,r5,0x0040 /* setting the DCFA bit, for Max rev 1 errata */ |
| 467 | NotMax: |
| 468 | addis r6,0,0xFFFF |
| 469 | ori r6,r6,0xFBFF |
| 470 | and r6,r5,r6 /* clear the invalidate bit */ |
| 471 | sync |
| 472 | mtspr HID0,r5 |
| 473 | mtspr HID0,r6 |
| 474 | isync |
| 475 | sync |
| 476 | blr |
| 477 | |
| 478 | .globl dcache_disable |
| 479 | dcache_disable: |
| 480 | mfspr r5,HID0 |
| 481 | addis r6,0,0xFFFF |
| 482 | ori r6,r6,0xBFFF |
| 483 | and r5,r5,r6 |
| 484 | sync |
| 485 | mtspr HID0,r5 |
| 486 | isync |
| 487 | sync |
| 488 | blr |
| 489 | |
| 490 | .globl dcache_status |
| 491 | dcache_status: |
| 492 | mfspr r3, HID0 |
| 493 | srwi r3, r3, 14 /* >>14 & 1=> select bit 17 */ |
| 494 | andi. r3, r3, 1 |
| 495 | blr |
| 496 | |
| 497 | .globl dc_read |
| 498 | dc_read: |
| 499 | /*TODO : who uses this, what should it do? |
| 500 | */ |
| 501 | blr |
| 502 | |
| 503 | |
| 504 | .globl get_pvr |
| 505 | get_pvr: |
| 506 | mfspr r3, PVR |
| 507 | blr |
| 508 | |
| 509 | |
| 510 | /*------------------------------------------------------------------------------*/ |
| 511 | |
| 512 | /* |
| 513 | * void relocate_code (addr_sp, gd, addr_moni) |
| 514 | * |
| 515 | * This "function" does not return, instead it continues in RAM |
| 516 | * after relocating the monitor code. |
| 517 | * |
| 518 | * r3 = dest |
| 519 | * r4 = src |
| 520 | * r5 = length in bytes |
| 521 | * r6 = cachelinesize |
| 522 | */ |
| 523 | .globl relocate_code |
| 524 | relocate_code: |
| 525 | |
| 526 | mr r1, r3 /* Set new stack pointer */ |
| 527 | mr r9, r4 /* Save copy of Global Data pointer */ |
| 528 | mr r10, r5 /* Save copy of Destination Address */ |
| 529 | |
| 530 | mr r3, r5 /* Destination Address */ |
| 531 | #ifdef DEBUG |
| 532 | lis r4, CFG_SDRAM_BASE@h /* Source Address */ |
| 533 | ori r4, r4, CFG_SDRAM_BASE@l |
| 534 | #else |
| 535 | lis r4, CFG_MONITOR_BASE@h /* Source Address */ |
| 536 | ori r4, r4, CFG_MONITOR_BASE@l |
| 537 | #endif |
| 538 | lis r5, CFG_MONITOR_LEN@h /* Length in Bytes */ |
| 539 | ori r5, r5, CFG_MONITOR_LEN@l |
| 540 | li r6, CFG_CACHELINE_SIZE /* Cache Line Size */ |
| 541 | |
| 542 | /* |
| 543 | * Fix GOT pointer: |
| 544 | * |
| 545 | * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address |
| 546 | * |
| 547 | * Offset: |
| 548 | */ |
| 549 | sub r15, r10, r4 |
| 550 | |
| 551 | /* First our own GOT */ |
| 552 | add r14, r14, r15 |
| 553 | /* the the one used by the C code */ |
| 554 | add r30, r30, r15 |
| 555 | |
| 556 | /* |
| 557 | * Now relocate code |
| 558 | */ |
| 559 | |
| 560 | cmplw cr1,r3,r4 |
| 561 | addi r0,r5,3 |
| 562 | srwi. r0,r0,2 |
| 563 | beq cr1,4f /* In place copy is not necessary */ |
| 564 | beq 7f /* Protect against 0 count */ |
| 565 | mtctr r0 |
| 566 | bge cr1,2f |
| 567 | |
| 568 | la r8,-4(r4) |
| 569 | la r7,-4(r3) |
| 570 | 1: lwzu r0,4(r8) |
| 571 | stwu r0,4(r7) |
| 572 | bdnz 1b |
| 573 | b 4f |
| 574 | |
| 575 | 2: slwi r0,r0,2 |
| 576 | add r8,r4,r0 |
| 577 | add r7,r3,r0 |
| 578 | 3: lwzu r0,-4(r8) |
| 579 | stwu r0,-4(r7) |
| 580 | bdnz 3b |
| 581 | |
| 582 | /* |
| 583 | * Now flush the cache: note that we must start from a cache aligned |
| 584 | * address. Otherwise we might miss one cache line. |
| 585 | */ |
| 586 | 4: cmpwi r6,0 |
| 587 | add r5,r3,r5 |
| 588 | beq 7f /* Always flush prefetch queue in any case */ |
| 589 | subi r0,r6,1 |
| 590 | andc r3,r3,r0 |
| 591 | mr r4,r3 |
| 592 | 5: dcbst 0,r4 |
| 593 | add r4,r4,r6 |
| 594 | cmplw r4,r5 |
| 595 | blt 5b |
| 596 | sync /* Wait for all dcbst to complete on bus */ |
| 597 | mr r4,r3 |
| 598 | 6: icbi 0,r4 |
| 599 | add r4,r4,r6 |
| 600 | cmplw r4,r5 |
| 601 | blt 6b |
| 602 | 7: sync /* Wait for all icbi to complete on bus */ |
| 603 | isync |
| 604 | |
| 605 | /* |
| 606 | * We are done. Do not return, instead branch to second part of board |
| 607 | * initialization, now running from RAM. |
| 608 | */ |
| 609 | |
| 610 | addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET |
| 611 | mtlr r0 |
| 612 | blr |
| 613 | |
| 614 | in_ram: |
| 615 | |
| 616 | /* |
| 617 | * Relocation Function, r14 point to got2+0x8000 |
| 618 | * |
| 619 | * Adjust got2 pointers, no need to check for 0, this code |
| 620 | * already puts a few entries in the table. |
| 621 | */ |
| 622 | li r0,__got2_entries@sectoff@l |
| 623 | la r3,GOT(_GOT2_TABLE_) |
| 624 | lwz r11,GOT(_GOT2_TABLE_) |
| 625 | mtctr r0 |
| 626 | sub r11,r3,r11 |
| 627 | addi r3,r3,-4 |
| 628 | 1: lwzu r0,4(r3) |
| 629 | add r0,r0,r11 |
| 630 | stw r0,0(r3) |
| 631 | bdnz 1b |
| 632 | |
| 633 | /* |
| 634 | * Now adjust the fixups and the pointers to the fixups |
| 635 | * in case we need to move ourselves again. |
| 636 | */ |
| 637 | 2: li r0,__fixup_entries@sectoff@l |
| 638 | lwz r3,GOT(_FIXUP_TABLE_) |
| 639 | cmpwi r0,0 |
| 640 | mtctr r0 |
| 641 | addi r3,r3,-4 |
| 642 | beq 4f |
| 643 | 3: lwzu r4,4(r3) |
| 644 | lwzux r0,r4,r11 |
| 645 | add r0,r0,r11 |
| 646 | stw r10,0(r3) |
| 647 | stw r0,0(r4) |
| 648 | bdnz 3b |
| 649 | 4: |
| 650 | clear_bss: |
| 651 | /* |
| 652 | * Now clear BSS segment |
| 653 | */ |
| 654 | lwz r3,GOT(.bss) |
| 655 | lwz r4,GOT(_end) |
| 656 | |
| 657 | cmplw 0, r3, r4 |
| 658 | beq 6f |
| 659 | |
| 660 | li r0, 0 |
| 661 | 5: |
| 662 | stw r0, 0(r3) |
| 663 | addi r3, r3, 4 |
| 664 | cmplw 0, r3, r4 |
| 665 | blt 5b |
| 666 | 6: |
| 667 | |
| 668 | mr r3, r9 /* Global Data pointer */ |
| 669 | mr r4, r10 /* Destination Address */ |
| 670 | bl board_init_r |
| 671 | |
| 672 | /* Problems accessing "end" in C, so do it here */ |
| 673 | .globl get_endaddr |
| 674 | get_endaddr: |
| 675 | lwz r3,GOT(_end) |
| 676 | blr |
| 677 | |
| 678 | /* |
| 679 | * Copy exception vector code to low memory |
| 680 | * |
| 681 | * r3: dest_addr |
| 682 | * r7: source address, r8: end address, r9: target address |
| 683 | */ |
| 684 | .globl trap_init |
| 685 | trap_init: |
| 686 | lwz r7, GOT(_start) |
| 687 | lwz r8, GOT(_end_of_vectors) |
| 688 | |
| 689 | rlwinm r9, r7, 0, 18, 31 /* _start & 0x3FFF */ |
| 690 | |
| 691 | cmplw 0, r7, r8 |
| 692 | bgelr /* return if r7>=r8 - just in case */ |
| 693 | |
| 694 | mflr r4 /* save link register */ |
| 695 | 1: |
| 696 | lwz r0, 0(r7) |
| 697 | stw r0, 0(r9) |
| 698 | addi r7, r7, 4 |
| 699 | addi r9, r9, 4 |
| 700 | cmplw 0, r7, r8 |
| 701 | bne 1b |
| 702 | |
| 703 | /* |
| 704 | * relocate `hdlr' and `int_return' entries |
| 705 | */ |
| 706 | li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET |
| 707 | li r8, Alignment - _start + EXC_OFF_SYS_RESET |
| 708 | 2: |
| 709 | bl trap_reloc |
| 710 | addi r7, r7, 0x100 /* next exception vector */ |
| 711 | cmplw 0, r7, r8 |
| 712 | blt 2b |
| 713 | |
| 714 | li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET |
| 715 | bl trap_reloc |
| 716 | |
| 717 | li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET |
| 718 | bl trap_reloc |
| 719 | |
| 720 | li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET |
| 721 | li r8, SystemCall - _start + EXC_OFF_SYS_RESET |
| 722 | 3: |
| 723 | bl trap_reloc |
| 724 | addi r7, r7, 0x100 /* next exception vector */ |
| 725 | cmplw 0, r7, r8 |
| 726 | blt 3b |
| 727 | |
| 728 | li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET |
| 729 | li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET |
| 730 | 4: |
| 731 | bl trap_reloc |
| 732 | addi r7, r7, 0x100 /* next exception vector */ |
| 733 | cmplw 0, r7, r8 |
| 734 | blt 4b |
| 735 | |
| 736 | mtlr r4 /* restore link register */ |
| 737 | blr |
| 738 | |
| 739 | /* |
| 740 | * Function: relocate entries for one exception vector |
| 741 | */ |
| 742 | trap_reloc: |
| 743 | lwz r0, 0(r7) /* hdlr ... */ |
| 744 | add r0, r0, r3 /* ... += dest_addr */ |
| 745 | stw r0, 0(r7) |
| 746 | |
| 747 | lwz r0, 4(r7) /* int_return ... */ |
| 748 | add r0, r0, r3 /* ... += dest_addr */ |
| 749 | stw r0, 4(r7) |
| 750 | |
| 751 | blr |
| 752 | |
| 753 | /* Setup the BAT registers. |
| 754 | */ |
| 755 | setup_bats: |
| 756 | lis r4, CFG_IBAT0L@h |
| 757 | ori r4, r4, CFG_IBAT0L@l |
| 758 | lis r3, CFG_IBAT0U@h |
| 759 | ori r3, r3, CFG_IBAT0U@l |
| 760 | mtspr IBAT0L, r4 |
| 761 | mtspr IBAT0U, r3 |
| 762 | isync |
| 763 | |
| 764 | lis r4, CFG_DBAT0L@h |
| 765 | ori r4, r4, CFG_DBAT0L@l |
| 766 | lis r3, CFG_DBAT0U@h |
| 767 | ori r3, r3, CFG_DBAT0U@l |
| 768 | mtspr DBAT0L, r4 |
| 769 | mtspr DBAT0U, r3 |
| 770 | isync |
| 771 | |
| 772 | lis r4, CFG_IBAT1L@h |
| 773 | ori r4, r4, CFG_IBAT1L@l |
| 774 | lis r3, CFG_IBAT1U@h |
| 775 | ori r3, r3, CFG_IBAT1U@l |
| 776 | mtspr IBAT1L, r4 |
| 777 | mtspr IBAT1U, r3 |
| 778 | isync |
| 779 | |
| 780 | lis r4, CFG_DBAT1L@h |
| 781 | ori r4, r4, CFG_DBAT1L@l |
| 782 | lis r3, CFG_DBAT1U@h |
| 783 | ori r3, r3, CFG_DBAT1U@l |
| 784 | mtspr DBAT1L, r4 |
| 785 | mtspr DBAT1U, r3 |
| 786 | isync |
| 787 | |
| 788 | lis r4, CFG_IBAT2L@h |
| 789 | ori r4, r4, CFG_IBAT2L@l |
| 790 | lis r3, CFG_IBAT2U@h |
| 791 | ori r3, r3, CFG_IBAT2U@l |
| 792 | mtspr IBAT2L, r4 |
| 793 | mtspr IBAT2U, r3 |
| 794 | isync |
| 795 | |
| 796 | lis r4, CFG_DBAT2L@h |
| 797 | ori r4, r4, CFG_DBAT2L@l |
| 798 | lis r3, CFG_DBAT2U@h |
| 799 | ori r3, r3, CFG_DBAT2U@l |
| 800 | mtspr DBAT2L, r4 |
| 801 | mtspr DBAT2U, r3 |
| 802 | isync |
| 803 | |
| 804 | lis r4, CFG_IBAT3L@h |
| 805 | ori r4, r4, CFG_IBAT3L@l |
| 806 | lis r3, CFG_IBAT3U@h |
| 807 | ori r3, r3, CFG_IBAT3U@l |
| 808 | mtspr IBAT3L, r4 |
| 809 | mtspr IBAT3U, r3 |
| 810 | isync |
| 811 | |
| 812 | lis r4, CFG_DBAT3L@h |
| 813 | ori r4, r4, CFG_DBAT3L@l |
| 814 | lis r3, CFG_DBAT3U@h |
| 815 | ori r3, r3, CFG_DBAT3U@l |
| 816 | mtspr DBAT3L, r4 |
| 817 | mtspr DBAT3U, r3 |
| 818 | isync |
| 819 | |
| 820 | /* Invalidate TLBs. |
| 821 | * -> for (val = 0; val < 0x20000; val+=0x1000) |
| 822 | * -> tlbie(val); |
| 823 | */ |
| 824 | lis r3, 0 |
| 825 | lis r5, 2 |
| 826 | |
| 827 | 1: |
| 828 | tlbie r3 |
| 829 | addi r3, r3, 0x1000 |
| 830 | cmp 0, 0, r3, r5 |
| 831 | blt 1b |
| 832 | |
| 833 | blr |
| 834 | |
| 835 | |