start.S 19 KB

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