start.S 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 - 2003 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 MPC5xxx CPUs
  26. */
  27. #include <config.h>
  28. #include <mpc5xxx.h>
  29. #include <version.h>
  30. #define CONFIG_MPC5xxx 1 /* needed for Linux kernel header files */
  31. #define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */
  32. #include <ppc_asm.tmpl>
  33. #include <ppc_defs.h>
  34. #include <asm/cache.h>
  35. #include <asm/mmu.h>
  36. #ifndef CONFIG_IDENT_STRING
  37. #define CONFIG_IDENT_STRING ""
  38. #endif
  39. /* We don't want the MMU yet.
  40. */
  41. #undef MSR_KERNEL
  42. /* Floating Point enable, Machine Check and Recoverable Interr. */
  43. #ifdef DEBUG
  44. #define MSR_KERNEL (MSR_FP|MSR_RI)
  45. #else
  46. #define MSR_KERNEL (MSR_FP|MSR_ME|MSR_RI)
  47. #endif
  48. /*
  49. * Set up GOT: Global Offset Table
  50. *
  51. * Use r14 to access the GOT
  52. */
  53. START_GOT
  54. GOT_ENTRY(_GOT2_TABLE_)
  55. GOT_ENTRY(_FIXUP_TABLE_)
  56. GOT_ENTRY(_start)
  57. GOT_ENTRY(_start_of_vectors)
  58. GOT_ENTRY(_end_of_vectors)
  59. GOT_ENTRY(transfer_to_handler)
  60. GOT_ENTRY(__init_end)
  61. GOT_ENTRY(_end)
  62. GOT_ENTRY(__bss_start)
  63. END_GOT
  64. /*
  65. * Version string
  66. */
  67. .data
  68. .globl version_string
  69. version_string:
  70. .ascii U_BOOT_VERSION
  71. .ascii " (", __DATE__, " - ", __TIME__, ")"
  72. .ascii CONFIG_IDENT_STRING, "\0"
  73. /*
  74. * Exception vectors
  75. */
  76. .text
  77. . = EXC_OFF_SYS_RESET
  78. .globl _start
  79. _start:
  80. li r21, BOOTFLAG_COLD /* Normal Power-On */
  81. nop
  82. b boot_cold
  83. . = EXC_OFF_SYS_RESET + 0x10
  84. .globl _start_warm
  85. _start_warm:
  86. li r21, BOOTFLAG_WARM /* Software reboot */
  87. b boot_warm
  88. boot_cold:
  89. boot_warm:
  90. mfmsr r5 /* save msr contents */
  91. /* Move CSBoot and adjust instruction pointer */
  92. /*--------------------------------------------------------------*/
  93. #if defined(CFG_LOWBOOT)
  94. # if defined(CFG_RAMBOOT)
  95. # error CFG_LOWBOOT is incompatible with CFG_RAMBOOT
  96. # endif /* CFG_RAMBOOT */
  97. # if defined(CONFIG_MGT5100)
  98. # error CFG_LOWBOOT is incompatible with MGT5100
  99. # endif /* CONFIG_MGT5100 */
  100. lis r4, CFG_DEFAULT_MBAR@h
  101. lis r3, START_REG(CFG_BOOTCS_START)@h
  102. ori r3, r3, START_REG(CFG_BOOTCS_START)@l
  103. stw r3, 0x4(r4) /* CS0 start */
  104. lis r3, STOP_REG(CFG_BOOTCS_START, CFG_BOOTCS_SIZE)@h
  105. ori r3, r3, STOP_REG(CFG_BOOTCS_START, CFG_BOOTCS_SIZE)@l
  106. stw r3, 0x8(r4) /* CS0 stop */
  107. lis r3, 0x02010000@h
  108. ori r3, r3, 0x02010000@l
  109. stw r3, 0x54(r4) /* CS0 and Boot enable */
  110. lis r3, lowboot_reentry@h /* jump from bootlow address space (0x0000xxxx) */
  111. ori r3, r3, lowboot_reentry@l /* to the address space the linker used */
  112. mtlr r3
  113. blr
  114. lowboot_reentry:
  115. lis r3, START_REG(CFG_BOOTCS_START)@h
  116. ori r3, r3, START_REG(CFG_BOOTCS_START)@l
  117. stw r3, 0x4c(r4) /* Boot start */
  118. lis r3, STOP_REG(CFG_BOOTCS_START, CFG_BOOTCS_SIZE)@h
  119. ori r3, r3, STOP_REG(CFG_BOOTCS_START, CFG_BOOTCS_SIZE)@l
  120. stw r3, 0x50(r4) /* Boot stop */
  121. lis r3, 0x02000001@h
  122. ori r3, r3, 0x02000001@l
  123. stw r3, 0x54(r4) /* Boot enable, CS0 disable */
  124. #endif /* CFG_LOWBOOT */
  125. #if defined(CFG_DEFAULT_MBAR) && !defined(CFG_RAMBOOT)
  126. lis r3, CFG_MBAR@h
  127. ori r3, r3, CFG_MBAR@l
  128. #if defined(CONFIG_MPC5200)
  129. /* MBAR is mirrored into the MBAR SPR */
  130. mtspr MBAR,r3
  131. rlwinm r3, r3, 16, 16, 31
  132. #endif
  133. #if defined(CONFIG_MGT5100)
  134. rlwinm r3, r3, 17, 15, 31
  135. #endif
  136. lis r4, CFG_DEFAULT_MBAR@h
  137. stw r3, 0(r4)
  138. #endif /* CFG_DEFAULT_MBAR */
  139. /* Initialise the MPC5xxx processor core */
  140. /*--------------------------------------------------------------*/
  141. bl init_5xxx_core
  142. /* initialize some things that are hard to access from C */
  143. /*--------------------------------------------------------------*/
  144. /* set up stack in on-chip SRAM */
  145. lis r3, CFG_INIT_RAM_ADDR@h
  146. ori r3, r3, CFG_INIT_RAM_ADDR@l
  147. ori r1, r3, CFG_INIT_SP_OFFSET
  148. li r0, 0 /* Make room for stack frame header and */
  149. stwu r0, -4(r1) /* clear final stack frame so that */
  150. stwu r0, -4(r1) /* stack backtraces terminate cleanly */
  151. /* let the C-code set up the rest */
  152. /* */
  153. /* Be careful to keep code relocatable ! */
  154. /*--------------------------------------------------------------*/
  155. GET_GOT /* initialize GOT access */
  156. /* r3: IMMR */
  157. bl cpu_init_f /* run low-level CPU init code (in Flash)*/
  158. mr r3, r21
  159. /* r3: BOOTFLAG */
  160. bl board_init_f /* run 1st part of board init code (in Flash)*/
  161. /*
  162. * Vector Table
  163. */
  164. .globl _start_of_vectors
  165. _start_of_vectors:
  166. /* Machine check */
  167. STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
  168. /* Data Storage exception. */
  169. STD_EXCEPTION(0x300, DataStorage, UnknownException)
  170. /* Instruction Storage exception. */
  171. STD_EXCEPTION(0x400, InstStorage, UnknownException)
  172. /* External Interrupt exception. */
  173. STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
  174. /* Alignment exception. */
  175. . = 0x600
  176. Alignment:
  177. EXCEPTION_PROLOG(SRR0, SRR1)
  178. mfspr r4,DAR
  179. stw r4,_DAR(r21)
  180. mfspr r5,DSISR
  181. stw r5,_DSISR(r21)
  182. addi r3,r1,STACK_FRAME_OVERHEAD
  183. li r20,MSR_KERNEL
  184. rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
  185. rlwimi r20,r23,0,25,25 /* copy IP bit from saved MSR */
  186. lwz r6,GOT(transfer_to_handler)
  187. mtlr r6
  188. blrl
  189. .L_Alignment:
  190. .long AlignmentException - _start + EXC_OFF_SYS_RESET
  191. .long int_return - _start + EXC_OFF_SYS_RESET
  192. /* Program check exception */
  193. . = 0x700
  194. ProgramCheck:
  195. EXCEPTION_PROLOG(SRR0, SRR1)
  196. addi r3,r1,STACK_FRAME_OVERHEAD
  197. li r20,MSR_KERNEL
  198. rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
  199. rlwimi r20,r23,0,25,25 /* copy IP bit from saved MSR */
  200. lwz r6,GOT(transfer_to_handler)
  201. mtlr r6
  202. blrl
  203. .L_ProgramCheck:
  204. .long ProgramCheckException - _start + EXC_OFF_SYS_RESET
  205. .long int_return - _start + EXC_OFF_SYS_RESET
  206. STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
  207. /* I guess we could implement decrementer, and may have
  208. * to someday for timekeeping.
  209. */
  210. STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
  211. STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
  212. STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
  213. STD_EXCEPTION(0xc00, SystemCall, UnknownException)
  214. STD_EXCEPTION(0xd00, SingleStep, UnknownException)
  215. STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
  216. STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
  217. STD_EXCEPTION(0x1000, InstructionTLBMiss, UnknownException)
  218. STD_EXCEPTION(0x1100, DataLoadTLBMiss, UnknownException)
  219. STD_EXCEPTION(0x1200, DataStoreTLBMiss, UnknownException)
  220. #ifdef DEBUG
  221. . = 0x1300
  222. /*
  223. * This exception occurs when the program counter matches the
  224. * Instruction Address Breakpoint Register (IABR).
  225. *
  226. * I want the cpu to halt if this occurs so I can hunt around
  227. * with the debugger and look at things.
  228. *
  229. * When DEBUG is defined, both machine check enable (in the MSR)
  230. * and checkstop reset enable (in the reset mode register) are
  231. * turned off and so a checkstop condition will result in the cpu
  232. * halting.
  233. *
  234. * I force the cpu into a checkstop condition by putting an illegal
  235. * instruction here (at least this is the theory).
  236. *
  237. * well - that didnt work, so just do an infinite loop!
  238. */
  239. 1: b 1b
  240. #else
  241. STD_EXCEPTION(0x1300, InstructionBreakpoint, DebugException)
  242. #endif
  243. STD_EXCEPTION(0x1400, SMI, UnknownException)
  244. STD_EXCEPTION(0x1500, Trap_15, UnknownException)
  245. STD_EXCEPTION(0x1600, Trap_16, UnknownException)
  246. STD_EXCEPTION(0x1700, Trap_17, UnknownException)
  247. STD_EXCEPTION(0x1800, Trap_18, UnknownException)
  248. STD_EXCEPTION(0x1900, Trap_19, UnknownException)
  249. STD_EXCEPTION(0x1a00, Trap_1a, UnknownException)
  250. STD_EXCEPTION(0x1b00, Trap_1b, UnknownException)
  251. STD_EXCEPTION(0x1c00, Trap_1c, UnknownException)
  252. STD_EXCEPTION(0x1d00, Trap_1d, UnknownException)
  253. STD_EXCEPTION(0x1e00, Trap_1e, UnknownException)
  254. STD_EXCEPTION(0x1f00, Trap_1f, UnknownException)
  255. STD_EXCEPTION(0x2000, Trap_20, UnknownException)
  256. STD_EXCEPTION(0x2100, Trap_21, UnknownException)
  257. STD_EXCEPTION(0x2200, Trap_22, UnknownException)
  258. STD_EXCEPTION(0x2300, Trap_23, UnknownException)
  259. STD_EXCEPTION(0x2400, Trap_24, UnknownException)
  260. STD_EXCEPTION(0x2500, Trap_25, UnknownException)
  261. STD_EXCEPTION(0x2600, Trap_26, UnknownException)
  262. STD_EXCEPTION(0x2700, Trap_27, UnknownException)
  263. STD_EXCEPTION(0x2800, Trap_28, UnknownException)
  264. STD_EXCEPTION(0x2900, Trap_29, UnknownException)
  265. STD_EXCEPTION(0x2a00, Trap_2a, UnknownException)
  266. STD_EXCEPTION(0x2b00, Trap_2b, UnknownException)
  267. STD_EXCEPTION(0x2c00, Trap_2c, UnknownException)
  268. STD_EXCEPTION(0x2d00, Trap_2d, UnknownException)
  269. STD_EXCEPTION(0x2e00, Trap_2e, UnknownException)
  270. STD_EXCEPTION(0x2f00, Trap_2f, UnknownException)
  271. .globl _end_of_vectors
  272. _end_of_vectors:
  273. . = 0x3000
  274. /*
  275. * This code finishes saving the registers to the exception frame
  276. * and jumps to the appropriate handler for the exception.
  277. * Register r21 is pointer into trap frame, r1 has new stack pointer.
  278. */
  279. .globl transfer_to_handler
  280. transfer_to_handler:
  281. stw r22,_NIP(r21)
  282. lis r22,MSR_POW@h
  283. andc r23,r23,r22
  284. stw r23,_MSR(r21)
  285. SAVE_GPR(7, r21)
  286. SAVE_4GPRS(8, r21)
  287. SAVE_8GPRS(12, r21)
  288. SAVE_8GPRS(24, r21)
  289. mflr r23
  290. andi. r24,r23,0x3f00 /* get vector offset */
  291. stw r24,TRAP(r21)
  292. li r22,0
  293. stw r22,RESULT(r21)
  294. lwz r24,0(r23) /* virtual address of handler */
  295. lwz r23,4(r23) /* where to go when done */
  296. mtspr SRR0,r24
  297. mtspr SRR1,r20
  298. mtlr r23
  299. SYNC
  300. rfi /* jump to handler, enable MMU */
  301. int_return:
  302. mfmsr r28 /* Disable interrupts */
  303. li r4,0
  304. ori r4,r4,MSR_EE
  305. andc r28,r28,r4
  306. SYNC /* Some chip revs need this... */
  307. mtmsr r28
  308. SYNC
  309. lwz r2,_CTR(r1)
  310. lwz r0,_LINK(r1)
  311. mtctr r2
  312. mtlr r0
  313. lwz r2,_XER(r1)
  314. lwz r0,_CCR(r1)
  315. mtspr XER,r2
  316. mtcrf 0xFF,r0
  317. REST_10GPRS(3, r1)
  318. REST_10GPRS(13, r1)
  319. REST_8GPRS(23, r1)
  320. REST_GPR(31, r1)
  321. lwz r2,_NIP(r1) /* Restore environment */
  322. lwz r0,_MSR(r1)
  323. mtspr SRR0,r2
  324. mtspr SRR1,r0
  325. lwz r0,GPR0(r1)
  326. lwz r2,GPR2(r1)
  327. lwz r1,GPR1(r1)
  328. SYNC
  329. rfi
  330. /*
  331. * This code initialises the MPC5xxx processor core
  332. * (conforms to PowerPC 603e spec)
  333. * Note: expects original MSR contents to be in r5.
  334. */
  335. .globl init_5xx_core
  336. init_5xxx_core:
  337. /* Initialize machine status; enable machine check interrupt */
  338. /*--------------------------------------------------------------*/
  339. li r3, MSR_KERNEL /* Set ME and RI flags */
  340. rlwimi r3, r5, 0, 25, 25 /* preserve IP bit set by HRCW */
  341. #ifdef DEBUG
  342. rlwimi r3, r5, 0, 21, 22 /* debugger might set SE & BE bits */
  343. #endif
  344. SYNC /* Some chip revs need this... */
  345. mtmsr r3
  346. SYNC
  347. mtspr SRR1, r3 /* Make SRR1 match MSR */
  348. /* Initialize the Hardware Implementation-dependent Registers */
  349. /* HID0 also contains cache control */
  350. /*--------------------------------------------------------------*/
  351. lis r3, CFG_HID0_INIT@h
  352. ori r3, r3, CFG_HID0_INIT@l
  353. SYNC
  354. mtspr HID0, r3
  355. lis r3, CFG_HID0_FINAL@h
  356. ori r3, r3, CFG_HID0_FINAL@l
  357. SYNC
  358. mtspr HID0, r3
  359. /* clear all BAT's */
  360. /*--------------------------------------------------------------*/
  361. li r0, 0
  362. mtspr DBAT0U, r0
  363. mtspr DBAT0L, r0
  364. mtspr DBAT1U, r0
  365. mtspr DBAT1L, r0
  366. mtspr DBAT2U, r0
  367. mtspr DBAT2L, r0
  368. mtspr DBAT3U, r0
  369. mtspr DBAT3L, r0
  370. mtspr DBAT4U, r0
  371. mtspr DBAT4L, r0
  372. mtspr DBAT5U, r0
  373. mtspr DBAT5L, r0
  374. mtspr DBAT6U, r0
  375. mtspr DBAT6L, r0
  376. mtspr DBAT7U, r0
  377. mtspr DBAT7L, r0
  378. mtspr IBAT0U, r0
  379. mtspr IBAT0L, r0
  380. mtspr IBAT1U, r0
  381. mtspr IBAT1L, r0
  382. mtspr IBAT2U, r0
  383. mtspr IBAT2L, r0
  384. mtspr IBAT3U, r0
  385. mtspr IBAT3L, r0
  386. mtspr IBAT4U, r0
  387. mtspr IBAT4L, r0
  388. mtspr IBAT5U, r0
  389. mtspr IBAT5L, r0
  390. mtspr IBAT6U, r0
  391. mtspr IBAT6L, r0
  392. mtspr IBAT7U, r0
  393. mtspr IBAT7L, r0
  394. SYNC
  395. /* invalidate all tlb's */
  396. /* */
  397. /* From the 603e User Manual: "The 603e provides the ability to */
  398. /* invalidate a TLB entry. The TLB Invalidate Entry (tlbie) */
  399. /* instruction invalidates the TLB entry indexed by the EA, and */
  400. /* operates on both the instruction and data TLBs simultaneously*/
  401. /* invalidating four TLB entries (both sets in each TLB). The */
  402. /* index corresponds to bits 15-19 of the EA. To invalidate all */
  403. /* entries within both TLBs, 32 tlbie instructions should be */
  404. /* issued, incrementing this field by one each time." */
  405. /* */
  406. /* "Note that the tlbia instruction is not implemented on the */
  407. /* 603e." */
  408. /* */
  409. /* bits 15-19 correspond to addresses 0x00000000 to 0x0001F000 */
  410. /* incrementing by 0x1000 each time. The code below is sort of */
  411. /* based on code in "flush_tlbs" from arch/ppc/kernel/head.S */
  412. /* */
  413. /*--------------------------------------------------------------*/
  414. li r3, 32
  415. mtctr r3
  416. li r3, 0
  417. 1: tlbie r3
  418. addi r3, r3, 0x1000
  419. bdnz 1b
  420. SYNC
  421. /* Done! */
  422. /*--------------------------------------------------------------*/
  423. blr
  424. /* Cache functions.
  425. *
  426. * Note: requires that all cache bits in
  427. * HID0 are in the low half word.
  428. */
  429. .globl icache_enable
  430. icache_enable:
  431. mfspr r3, HID0
  432. ori r3, r3, HID0_ICE
  433. lis r4, 0
  434. ori r4, r4, HID0_ILOCK
  435. andc r3, r3, r4
  436. ori r4, r3, HID0_ICFI
  437. isync
  438. mtspr HID0, r4 /* sets enable and invalidate, clears lock */
  439. isync
  440. mtspr HID0, r3 /* clears invalidate */
  441. blr
  442. .globl icache_disable
  443. icache_disable:
  444. mfspr r3, HID0
  445. lis r4, 0
  446. ori r4, r4, HID0_ICE|HID0_ILOCK
  447. andc r3, r3, r4
  448. ori r4, r3, HID0_ICFI
  449. isync
  450. mtspr HID0, r4 /* sets invalidate, clears enable and lock */
  451. isync
  452. mtspr HID0, r3 /* clears invalidate */
  453. blr
  454. .globl icache_status
  455. icache_status:
  456. mfspr r3, HID0
  457. rlwinm r3, r3, HID0_ICE_BITPOS + 1, 31, 31
  458. blr
  459. .globl dcache_enable
  460. dcache_enable:
  461. mfspr r3, HID0
  462. ori r3, r3, HID0_DCE
  463. lis r4, 0
  464. ori r4, r4, HID0_DLOCK
  465. andc r3, r3, r4
  466. ori r4, r3, HID0_DCI
  467. sync
  468. mtspr HID0, r4 /* sets enable and invalidate, clears lock */
  469. sync
  470. mtspr HID0, r3 /* clears invalidate */
  471. blr
  472. .globl dcache_disable
  473. dcache_disable:
  474. mfspr r3, HID0
  475. lis r4, 0
  476. ori r4, r4, HID0_DCE|HID0_DLOCK
  477. andc r3, r3, r4
  478. ori r4, r3, HID0_DCI
  479. sync
  480. mtspr HID0, r4 /* sets invalidate, clears enable and lock */
  481. sync
  482. mtspr HID0, r3 /* clears invalidate */
  483. blr
  484. .globl dcache_status
  485. dcache_status:
  486. mfspr r3, HID0
  487. rlwinm r3, r3, HID0_DCE_BITPOS + 1, 31, 31
  488. blr
  489. .globl get_svr
  490. get_svr:
  491. mfspr r3, SVR
  492. blr
  493. .globl get_pvr
  494. get_pvr:
  495. mfspr r3, PVR
  496. blr
  497. /*------------------------------------------------------------------------------*/
  498. /*
  499. * void relocate_code (addr_sp, gd, addr_moni)
  500. *
  501. * This "function" does not return, instead it continues in RAM
  502. * after relocating the monitor code.
  503. *
  504. * r3 = dest
  505. * r4 = src
  506. * r5 = length in bytes
  507. * r6 = cachelinesize
  508. */
  509. .globl relocate_code
  510. relocate_code:
  511. mr r1, r3 /* Set new stack pointer */
  512. mr r9, r4 /* Save copy of Global Data pointer */
  513. mr r10, r5 /* Save copy of Destination Address */
  514. mr r3, r5 /* Destination Address */
  515. lis r4, CFG_MONITOR_BASE@h /* Source Address */
  516. ori r4, r4, CFG_MONITOR_BASE@l
  517. lwz r5, GOT(__init_end)
  518. sub r5, r5, r4
  519. li r6, CFG_CACHELINE_SIZE /* Cache Line Size */
  520. /*
  521. * Fix GOT pointer:
  522. *
  523. * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
  524. *
  525. * Offset:
  526. */
  527. sub r15, r10, r4
  528. /* First our own GOT */
  529. add r14, r14, r15
  530. /* then the one used by the C code */
  531. add r30, r30, r15
  532. /*
  533. * Now relocate code
  534. */
  535. cmplw cr1,r3,r4
  536. addi r0,r5,3
  537. srwi. r0,r0,2
  538. beq cr1,4f /* In place copy is not necessary */
  539. beq 7f /* Protect against 0 count */
  540. mtctr r0
  541. bge cr1,2f
  542. la r8,-4(r4)
  543. la r7,-4(r3)
  544. 1: lwzu r0,4(r8)
  545. stwu r0,4(r7)
  546. bdnz 1b
  547. b 4f
  548. 2: slwi r0,r0,2
  549. add r8,r4,r0
  550. add r7,r3,r0
  551. 3: lwzu r0,-4(r8)
  552. stwu r0,-4(r7)
  553. bdnz 3b
  554. /*
  555. * Now flush the cache: note that we must start from a cache aligned
  556. * address. Otherwise we might miss one cache line.
  557. */
  558. 4: cmpwi r6,0
  559. add r5,r3,r5
  560. beq 7f /* Always flush prefetch queue in any case */
  561. subi r0,r6,1
  562. andc r3,r3,r0
  563. mfspr r7,HID0 /* don't do dcbst if dcache is disabled */
  564. rlwinm r7,r7,HID0_DCE_BITPOS+1,31,31
  565. cmpwi r7,0
  566. beq 9f
  567. mr r4,r3
  568. 5: dcbst 0,r4
  569. add r4,r4,r6
  570. cmplw r4,r5
  571. blt 5b
  572. sync /* Wait for all dcbst to complete on bus */
  573. 9: mfspr r7,HID0 /* don't do icbi if icache is disabled */
  574. rlwinm r7,r7,HID0_ICE_BITPOS+1,31,31
  575. cmpwi r7,0
  576. beq 7f
  577. mr r4,r3
  578. 6: icbi 0,r4
  579. add r4,r4,r6
  580. cmplw r4,r5
  581. blt 6b
  582. 7: sync /* Wait for all icbi to complete on bus */
  583. isync
  584. /*
  585. * We are done. Do not return, instead branch to second part of board
  586. * initialization, now running from RAM.
  587. */
  588. addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
  589. mtlr r0
  590. blr
  591. in_ram:
  592. /*
  593. * Relocation Function, r14 point to got2+0x8000
  594. *
  595. * Adjust got2 pointers, no need to check for 0, this code
  596. * already puts a few entries in the table.
  597. */
  598. li r0,__got2_entries@sectoff@l
  599. la r3,GOT(_GOT2_TABLE_)
  600. lwz r11,GOT(_GOT2_TABLE_)
  601. mtctr r0
  602. sub r11,r3,r11
  603. addi r3,r3,-4
  604. 1: lwzu r0,4(r3)
  605. add r0,r0,r11
  606. stw r0,0(r3)
  607. bdnz 1b
  608. /*
  609. * Now adjust the fixups and the pointers to the fixups
  610. * in case we need to move ourselves again.
  611. */
  612. 2: li r0,__fixup_entries@sectoff@l
  613. lwz r3,GOT(_FIXUP_TABLE_)
  614. cmpwi r0,0
  615. mtctr r0
  616. addi r3,r3,-4
  617. beq 4f
  618. 3: lwzu r4,4(r3)
  619. lwzux r0,r4,r11
  620. add r0,r0,r11
  621. stw r10,0(r3)
  622. stw r0,0(r4)
  623. bdnz 3b
  624. 4:
  625. clear_bss:
  626. /*
  627. * Now clear BSS segment
  628. */
  629. lwz r3,GOT(__bss_start)
  630. lwz r4,GOT(_end)
  631. cmplw 0, r3, r4
  632. beq 6f
  633. li r0, 0
  634. 5:
  635. stw r0, 0(r3)
  636. addi r3, r3, 4
  637. cmplw 0, r3, r4
  638. bne 5b
  639. 6:
  640. mr r3, r9 /* Global Data pointer */
  641. mr r4, r10 /* Destination Address */
  642. bl board_init_r
  643. /*
  644. * Copy exception vector code to low memory
  645. *
  646. * r3: dest_addr
  647. * r7: source address, r8: end address, r9: target address
  648. */
  649. .globl trap_init
  650. trap_init:
  651. lwz r7, GOT(_start)
  652. lwz r8, GOT(_end_of_vectors)
  653. li r9, 0x100 /* reset vector always at 0x100 */
  654. cmplw 0, r7, r8
  655. bgelr /* return if r7>=r8 - just in case */
  656. mflr r4 /* save link register */
  657. 1:
  658. lwz r0, 0(r7)
  659. stw r0, 0(r9)
  660. addi r7, r7, 4
  661. addi r9, r9, 4
  662. cmplw 0, r7, r8
  663. bne 1b
  664. /*
  665. * relocate `hdlr' and `int_return' entries
  666. */
  667. li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
  668. li r8, Alignment - _start + EXC_OFF_SYS_RESET
  669. 2:
  670. bl trap_reloc
  671. addi r7, r7, 0x100 /* next exception vector */
  672. cmplw 0, r7, r8
  673. blt 2b
  674. li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
  675. bl trap_reloc
  676. li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
  677. bl trap_reloc
  678. li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
  679. li r8, SystemCall - _start + EXC_OFF_SYS_RESET
  680. 3:
  681. bl trap_reloc
  682. addi r7, r7, 0x100 /* next exception vector */
  683. cmplw 0, r7, r8
  684. blt 3b
  685. li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
  686. li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
  687. 4:
  688. bl trap_reloc
  689. addi r7, r7, 0x100 /* next exception vector */
  690. cmplw 0, r7, r8
  691. blt 4b
  692. mfmsr r3 /* now that the vectors have */
  693. lis r7, MSR_IP@h /* relocated into low memory */
  694. ori r7, r7, MSR_IP@l /* MSR[IP] can be turned off */
  695. andc r3, r3, r7 /* (if it was on) */
  696. SYNC /* Some chip revs need this... */
  697. mtmsr r3
  698. SYNC
  699. mtlr r4 /* restore link register */
  700. blr
  701. /*
  702. * Function: relocate entries for one exception vector
  703. */
  704. trap_reloc:
  705. lwz r0, 0(r7) /* hdlr ... */
  706. add r0, r0, r3 /* ... += dest_addr */
  707. stw r0, 0(r7)
  708. lwz r0, 4(r7) /* int_return ... */
  709. add r0, r0, r3 /* ... += dest_addr */
  710. stw r0, 4(r7)
  711. blr