start.S 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. * Copyright (C) 2001 Josh Huber <huber@mclx.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /* U-Boot - Startup Code for PowerPC based Embedded Boards
  26. *
  27. *
  28. * The processor starts at 0xfff00100 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. */
  34. #include <config.h>
  35. #include <74xx_7xx.h>
  36. #include <version.h>
  37. #include <ppc_asm.tmpl>
  38. #include <ppc_defs.h>
  39. #include <asm/cache.h>
  40. #include <asm/mmu.h>
  41. #if !defined(CONFIG_DB64360) && \
  42. !defined(CONFIG_DB64460) && \
  43. !defined(CONFIG_CPCI750) && \
  44. !defined(CONFIG_P3Mx)
  45. #include <galileo/gt64260R.h>
  46. #endif
  47. #ifndef CONFIG_IDENT_STRING
  48. #define CONFIG_IDENT_STRING ""
  49. #endif
  50. /* We don't want the MMU yet.
  51. */
  52. #undef MSR_KERNEL
  53. /* Machine Check and Recoverable Interr. */
  54. #define MSR_KERNEL ( MSR_ME | MSR_RI )
  55. /*
  56. * Set up GOT: Global Offset Table
  57. *
  58. * Use r14 to access the GOT
  59. */
  60. START_GOT
  61. GOT_ENTRY(_GOT2_TABLE_)
  62. GOT_ENTRY(_FIXUP_TABLE_)
  63. GOT_ENTRY(_start)
  64. GOT_ENTRY(_start_of_vectors)
  65. GOT_ENTRY(_end_of_vectors)
  66. GOT_ENTRY(transfer_to_handler)
  67. GOT_ENTRY(__init_end)
  68. GOT_ENTRY(_end)
  69. GOT_ENTRY(__bss_start)
  70. END_GOT
  71. /*
  72. * r3 - 1st arg to board_init(): IMMP pointer
  73. * r4 - 2nd arg to board_init(): boot flag
  74. */
  75. .text
  76. .long 0x27051956 /* U-Boot Magic Number */
  77. .globl version_string
  78. version_string:
  79. .ascii U_BOOT_VERSION
  80. .ascii " (", __DATE__, " - ", __TIME__, ")"
  81. .ascii CONFIG_IDENT_STRING, "\0"
  82. . = EXC_OFF_SYS_RESET
  83. .globl _start
  84. _start:
  85. li r21, BOOTFLAG_COLD /* Normal Power-On: Boot from FLASH */
  86. b boot_cold
  87. sync
  88. . = EXC_OFF_SYS_RESET + 0x10
  89. .globl _start_warm
  90. _start_warm:
  91. li r21, BOOTFLAG_WARM /* Software reboot */
  92. b boot_warm
  93. sync
  94. /* the boot code is located below the exception table */
  95. .globl _start_of_vectors
  96. _start_of_vectors:
  97. /* Machine check */
  98. STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
  99. /* Data Storage exception. "Never" generated on the 860. */
  100. STD_EXCEPTION(0x300, DataStorage, UnknownException)
  101. /* Instruction Storage exception. "Never" generated on the 860. */
  102. STD_EXCEPTION(0x400, InstStorage, UnknownException)
  103. /* External Interrupt exception. */
  104. STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
  105. /* Alignment exception. */
  106. . = 0x600
  107. Alignment:
  108. EXCEPTION_PROLOG(SRR0, SRR1)
  109. mfspr r4,DAR
  110. stw r4,_DAR(r21)
  111. mfspr r5,DSISR
  112. stw r5,_DSISR(r21)
  113. addi r3,r1,STACK_FRAME_OVERHEAD
  114. li r20,MSR_KERNEL
  115. rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
  116. lwz r6,GOT(transfer_to_handler)
  117. mtlr r6
  118. blrl
  119. .L_Alignment:
  120. .long AlignmentException - _start + EXC_OFF_SYS_RESET
  121. .long int_return - _start + EXC_OFF_SYS_RESET
  122. /* Program check exception */
  123. . = 0x700
  124. ProgramCheck:
  125. EXCEPTION_PROLOG(SRR0, SRR1)
  126. addi r3,r1,STACK_FRAME_OVERHEAD
  127. li r20,MSR_KERNEL
  128. rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
  129. lwz r6,GOT(transfer_to_handler)
  130. mtlr r6
  131. blrl
  132. .L_ProgramCheck:
  133. .long ProgramCheckException - _start + EXC_OFF_SYS_RESET
  134. .long int_return - _start + EXC_OFF_SYS_RESET
  135. /* No FPU on MPC8xx. This exception is not supposed to happen.
  136. */
  137. STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
  138. /* I guess we could implement decrementer, and may have
  139. * to someday for timekeeping.
  140. */
  141. STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
  142. STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
  143. STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
  144. STD_EXCEPTION(0xc00, SystemCall, UnknownException)
  145. STD_EXCEPTION(0xd00, SingleStep, UnknownException)
  146. STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
  147. STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
  148. /*
  149. * On the MPC8xx, this is a software emulation interrupt. It
  150. * occurs for all unimplemented and illegal instructions.
  151. */
  152. STD_EXCEPTION(0x1000, SoftEmu, SoftEmuException)
  153. STD_EXCEPTION(0x1100, InstructionTLBMiss, UnknownException)
  154. STD_EXCEPTION(0x1200, DataTLBMiss, UnknownException)
  155. STD_EXCEPTION(0x1300, InstructionTLBError, UnknownException)
  156. STD_EXCEPTION(0x1400, DataTLBError, UnknownException)
  157. STD_EXCEPTION(0x1500, Reserved5, UnknownException)
  158. STD_EXCEPTION(0x1600, Reserved6, UnknownException)
  159. STD_EXCEPTION(0x1700, Reserved7, UnknownException)
  160. STD_EXCEPTION(0x1800, Reserved8, UnknownException)
  161. STD_EXCEPTION(0x1900, Reserved9, UnknownException)
  162. STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
  163. STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
  164. STD_EXCEPTION(0x1c00, DataBreakpoint, UnknownException)
  165. STD_EXCEPTION(0x1d00, InstructionBreakpoint, UnknownException)
  166. STD_EXCEPTION(0x1e00, PeripheralBreakpoint, UnknownException)
  167. STD_EXCEPTION(0x1f00, DevPortBreakpoint, UnknownException)
  168. .globl _end_of_vectors
  169. _end_of_vectors:
  170. . = 0x2000
  171. boot_cold:
  172. boot_warm:
  173. /* disable everything */
  174. li r0, 0
  175. mtspr HID0, r0
  176. sync
  177. mtmsr 0
  178. bl invalidate_bats
  179. sync
  180. #ifdef CFG_L2
  181. /* init the L2 cache */
  182. addis r3, r0, L2_INIT@h
  183. ori r3, r3, L2_INIT@l
  184. sync
  185. mtspr l2cr, r3
  186. #endif
  187. #if defined(CONFIG_ALTIVEC) && defined(CONFIG_74xx)
  188. .long 0x7e00066c
  189. /*
  190. * dssall instruction, gas doesn't have it yet
  191. * ...for altivec, data stream stop all this probably
  192. * isn't needed unless we warm (software) reboot U-Boot
  193. */
  194. #endif
  195. #ifdef CFG_L2
  196. /* invalidate the L2 cache */
  197. bl l2cache_invalidate
  198. sync
  199. #endif
  200. #ifdef CFG_BOARD_ASM_INIT
  201. /* do early init */
  202. bl board_asm_init
  203. #endif
  204. /*
  205. * Calculate absolute address in FLASH and jump there
  206. *------------------------------------------------------*/
  207. lis r3, CFG_MONITOR_BASE@h
  208. ori r3, r3, CFG_MONITOR_BASE@l
  209. addi r3, r3, in_flash - _start + EXC_OFF_SYS_RESET
  210. mtlr r3
  211. blr
  212. in_flash:
  213. /* let the C-code set up the rest */
  214. /* */
  215. /* Be careful to keep code relocatable ! */
  216. /*------------------------------------------------------*/
  217. /* perform low-level init */
  218. /* sdram init, galileo init, etc */
  219. /* r3: NHR bit from HID0 */
  220. /* setup the bats */
  221. bl setup_bats
  222. sync
  223. /*
  224. * Cache must be enabled here for stack-in-cache trick.
  225. * This means we need to enable the BATS.
  226. * This means:
  227. * 1) for the EVB, original gt regs need to be mapped
  228. * 2) need to have an IBAT for the 0xf region,
  229. * we are running there!
  230. * Cache should be turned on after BATs, since by default
  231. * everything is write-through.
  232. * The init-mem BAT can be reused after reloc. The old
  233. * gt-regs BAT can be reused after board_init_f calls
  234. * board_early_init_f (EVB only).
  235. */
  236. #if !defined(CONFIG_BAB7xx) && !defined(CONFIG_ELPPC) && !defined(CONFIG_P3Mx)
  237. /* enable address translation */
  238. bl enable_addr_trans
  239. sync
  240. /* enable and invalidate the data cache */
  241. bl l1dcache_enable
  242. sync
  243. #endif
  244. #ifdef CFG_INIT_RAM_LOCK
  245. bl lock_ram_in_cache
  246. sync
  247. #endif
  248. /* set up the stack pointer in our newly created
  249. * cache-ram (r1) */
  250. lis r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@h
  251. ori r1, r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@l
  252. li r0, 0 /* Make room for stack frame header and */
  253. stwu r0, -4(r1) /* clear final stack frame so that */
  254. stwu r0, -4(r1) /* stack backtraces terminate cleanly */
  255. GET_GOT /* initialize GOT access */
  256. /* run low-level CPU init code (from Flash) */
  257. bl cpu_init_f
  258. sync
  259. mr r3, r21
  260. /* r3: BOOTFLAG */
  261. /* run 1st part of board init code (from Flash) */
  262. bl board_init_f
  263. sync
  264. /* NOTREACHED */
  265. .globl invalidate_bats
  266. invalidate_bats:
  267. /* invalidate BATs */
  268. mtspr IBAT0U, r0
  269. mtspr IBAT1U, r0
  270. mtspr IBAT2U, r0
  271. mtspr IBAT3U, r0
  272. #ifdef CONFIG_HIGH_BATS
  273. mtspr IBAT4U, r0
  274. mtspr IBAT5U, r0
  275. mtspr IBAT6U, r0
  276. mtspr IBAT7U, r0
  277. #endif
  278. isync
  279. mtspr DBAT0U, r0
  280. mtspr DBAT1U, r0
  281. mtspr DBAT2U, r0
  282. mtspr DBAT3U, r0
  283. #ifdef CONFIG_HIGH_BATS
  284. mtspr DBAT4U, r0
  285. mtspr DBAT5U, r0
  286. mtspr DBAT6U, r0
  287. mtspr DBAT7U, r0
  288. #endif
  289. isync
  290. sync
  291. blr
  292. /* setup_bats - set them up to some initial state */
  293. .globl setup_bats
  294. setup_bats:
  295. addis r0, r0, 0x0000
  296. /* IBAT 0 */
  297. addis r4, r0, CFG_IBAT0L@h
  298. ori r4, r4, CFG_IBAT0L@l
  299. addis r3, r0, CFG_IBAT0U@h
  300. ori r3, r3, CFG_IBAT0U@l
  301. mtspr IBAT0L, r4
  302. mtspr IBAT0U, r3
  303. isync
  304. /* DBAT 0 */
  305. addis r4, r0, CFG_DBAT0L@h
  306. ori r4, r4, CFG_DBAT0L@l
  307. addis r3, r0, CFG_DBAT0U@h
  308. ori r3, r3, CFG_DBAT0U@l
  309. mtspr DBAT0L, r4
  310. mtspr DBAT0U, r3
  311. isync
  312. /* IBAT 1 */
  313. addis r4, r0, CFG_IBAT1L@h
  314. ori r4, r4, CFG_IBAT1L@l
  315. addis r3, r0, CFG_IBAT1U@h
  316. ori r3, r3, CFG_IBAT1U@l
  317. mtspr IBAT1L, r4
  318. mtspr IBAT1U, r3
  319. isync
  320. /* DBAT 1 */
  321. addis r4, r0, CFG_DBAT1L@h
  322. ori r4, r4, CFG_DBAT1L@l
  323. addis r3, r0, CFG_DBAT1U@h
  324. ori r3, r3, CFG_DBAT1U@l
  325. mtspr DBAT1L, r4
  326. mtspr DBAT1U, r3
  327. isync
  328. /* IBAT 2 */
  329. addis r4, r0, CFG_IBAT2L@h
  330. ori r4, r4, CFG_IBAT2L@l
  331. addis r3, r0, CFG_IBAT2U@h
  332. ori r3, r3, CFG_IBAT2U@l
  333. mtspr IBAT2L, r4
  334. mtspr IBAT2U, r3
  335. isync
  336. /* DBAT 2 */
  337. addis r4, r0, CFG_DBAT2L@h
  338. ori r4, r4, CFG_DBAT2L@l
  339. addis r3, r0, CFG_DBAT2U@h
  340. ori r3, r3, CFG_DBAT2U@l
  341. mtspr DBAT2L, r4
  342. mtspr DBAT2U, r3
  343. isync
  344. /* IBAT 3 */
  345. addis r4, r0, CFG_IBAT3L@h
  346. ori r4, r4, CFG_IBAT3L@l
  347. addis r3, r0, CFG_IBAT3U@h
  348. ori r3, r3, CFG_IBAT3U@l
  349. mtspr IBAT3L, r4
  350. mtspr IBAT3U, r3
  351. isync
  352. /* DBAT 3 */
  353. addis r4, r0, CFG_DBAT3L@h
  354. ori r4, r4, CFG_DBAT3L@l
  355. addis r3, r0, CFG_DBAT3U@h
  356. ori r3, r3, CFG_DBAT3U@l
  357. mtspr DBAT3L, r4
  358. mtspr DBAT3U, r3
  359. isync
  360. #ifdef CONFIG_HIGH_BATS
  361. /* IBAT 4 */
  362. addis r4, r0, CFG_IBAT4L@h
  363. ori r4, r4, CFG_IBAT4L@l
  364. addis r3, r0, CFG_IBAT4U@h
  365. ori r3, r3, CFG_IBAT4U@l
  366. mtspr IBAT4L, r4
  367. mtspr IBAT4U, r3
  368. isync
  369. /* DBAT 4 */
  370. addis r4, r0, CFG_DBAT4L@h
  371. ori r4, r4, CFG_DBAT4L@l
  372. addis r3, r0, CFG_DBAT4U@h
  373. ori r3, r3, CFG_DBAT4U@l
  374. mtspr DBAT4L, r4
  375. mtspr DBAT4U, r3
  376. isync
  377. /* IBAT 5 */
  378. addis r4, r0, CFG_IBAT5L@h
  379. ori r4, r4, CFG_IBAT5L@l
  380. addis r3, r0, CFG_IBAT5U@h
  381. ori r3, r3, CFG_IBAT5U@l
  382. mtspr IBAT5L, r4
  383. mtspr IBAT5U, r3
  384. isync
  385. /* DBAT 5 */
  386. addis r4, r0, CFG_DBAT5L@h
  387. ori r4, r4, CFG_DBAT5L@l
  388. addis r3, r0, CFG_DBAT5U@h
  389. ori r3, r3, CFG_DBAT5U@l
  390. mtspr DBAT5L, r4
  391. mtspr DBAT5U, r3
  392. isync
  393. /* IBAT 6 */
  394. addis r4, r0, CFG_IBAT6L@h
  395. ori r4, r4, CFG_IBAT6L@l
  396. addis r3, r0, CFG_IBAT6U@h
  397. ori r3, r3, CFG_IBAT6U@l
  398. mtspr IBAT6L, r4
  399. mtspr IBAT6U, r3
  400. isync
  401. /* DBAT 6 */
  402. addis r4, r0, CFG_DBAT6L@h
  403. ori r4, r4, CFG_DBAT6L@l
  404. addis r3, r0, CFG_DBAT6U@h
  405. ori r3, r3, CFG_DBAT6U@l
  406. mtspr DBAT6L, r4
  407. mtspr DBAT6U, r3
  408. isync
  409. /* IBAT 7 */
  410. addis r4, r0, CFG_IBAT7L@h
  411. ori r4, r4, CFG_IBAT7L@l
  412. addis r3, r0, CFG_IBAT7U@h
  413. ori r3, r3, CFG_IBAT7U@l
  414. mtspr IBAT7L, r4
  415. mtspr IBAT7U, r3
  416. isync
  417. /* DBAT 7 */
  418. addis r4, r0, CFG_DBAT7L@h
  419. ori r4, r4, CFG_DBAT7L@l
  420. addis r3, r0, CFG_DBAT7U@h
  421. ori r3, r3, CFG_DBAT7U@l
  422. mtspr DBAT7L, r4
  423. mtspr DBAT7U, r3
  424. isync
  425. #endif
  426. /* bats are done, now invalidate the TLBs */
  427. addis r3, 0, 0x0000
  428. addis r5, 0, 0x4 /* upper bound of 0x00040000 for 7400/750 */
  429. isync
  430. tlblp:
  431. tlbie r3
  432. sync
  433. addi r3, r3, 0x1000
  434. cmp 0, 0, r3, r5
  435. blt tlblp
  436. blr
  437. .globl enable_addr_trans
  438. enable_addr_trans:
  439. /* enable address translation */
  440. mfmsr r5
  441. ori r5, r5, (MSR_IR | MSR_DR)
  442. mtmsr r5
  443. isync
  444. blr
  445. .globl disable_addr_trans
  446. disable_addr_trans:
  447. /* disable address translation */
  448. mflr r4
  449. mfmsr r3
  450. andi. r0, r3, (MSR_IR | MSR_DR)
  451. beqlr
  452. andc r3, r3, r0
  453. mtspr SRR0, r4
  454. mtspr SRR1, r3
  455. rfi
  456. /*
  457. * This code finishes saving the registers to the exception frame
  458. * and jumps to the appropriate handler for the exception.
  459. * Register r21 is pointer into trap frame, r1 has new stack pointer.
  460. */
  461. .globl transfer_to_handler
  462. transfer_to_handler:
  463. stw r22,_NIP(r21)
  464. lis r22,MSR_POW@h
  465. andc r23,r23,r22
  466. stw r23,_MSR(r21)
  467. SAVE_GPR(7, r21)
  468. SAVE_4GPRS(8, r21)
  469. SAVE_8GPRS(12, r21)
  470. SAVE_8GPRS(24, r21)
  471. mflr r23
  472. andi. r24,r23,0x3f00 /* get vector offset */
  473. stw r24,TRAP(r21)
  474. li r22,0
  475. stw r22,RESULT(r21)
  476. mtspr SPRG2,r22 /* r1 is now kernel sp */
  477. lwz r24,0(r23) /* virtual address of handler */
  478. lwz r23,4(r23) /* where to go when done */
  479. mtspr SRR0,r24
  480. mtspr SRR1,r20
  481. mtlr r23
  482. SYNC
  483. rfi /* jump to handler, enable MMU */
  484. int_return:
  485. mfmsr r28 /* Disable interrupts */
  486. li r4,0
  487. ori r4,r4,MSR_EE
  488. andc r28,r28,r4
  489. SYNC /* Some chip revs need this... */
  490. mtmsr r28
  491. SYNC
  492. lwz r2,_CTR(r1)
  493. lwz r0,_LINK(r1)
  494. mtctr r2
  495. mtlr r0
  496. lwz r2,_XER(r1)
  497. lwz r0,_CCR(r1)
  498. mtspr XER,r2
  499. mtcrf 0xFF,r0
  500. REST_10GPRS(3, r1)
  501. REST_10GPRS(13, r1)
  502. REST_8GPRS(23, r1)
  503. REST_GPR(31, r1)
  504. lwz r2,_NIP(r1) /* Restore environment */
  505. lwz r0,_MSR(r1)
  506. mtspr SRR0,r2
  507. mtspr SRR1,r0
  508. lwz r0,GPR0(r1)
  509. lwz r2,GPR2(r1)
  510. lwz r1,GPR1(r1)
  511. SYNC
  512. rfi
  513. .globl dc_read
  514. dc_read:
  515. blr
  516. .globl get_pvr
  517. get_pvr:
  518. mfspr r3, PVR
  519. blr
  520. /*-----------------------------------------------------------------------*/
  521. /*
  522. * void relocate_code (addr_sp, gd, addr_moni)
  523. *
  524. * This "function" does not return, instead it continues in RAM
  525. * after relocating the monitor code.
  526. *
  527. * r3 = dest
  528. * r4 = src
  529. * r5 = length in bytes
  530. * r6 = cachelinesize
  531. */
  532. .globl relocate_code
  533. relocate_code:
  534. mr r1, r3 /* Set new stack pointer */
  535. mr r9, r4 /* Save copy of Global Data pointer */
  536. mr r10, r5 /* Save copy of Destination Address */
  537. mr r3, r5 /* Destination Address */
  538. lis r4, CFG_MONITOR_BASE@h /* Source Address */
  539. ori r4, r4, CFG_MONITOR_BASE@l
  540. lwz r5, GOT(__init_end)
  541. sub r5, r5, r4
  542. li r6, CFG_CACHELINE_SIZE /* Cache Line Size */
  543. /*
  544. * Fix GOT pointer:
  545. *
  546. * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
  547. *
  548. * Offset:
  549. */
  550. sub r15, r10, r4
  551. /* First our own GOT */
  552. add r14, r14, r15
  553. /* then the one used by the C code */
  554. add r30, r30, r15
  555. /*
  556. * Now relocate code
  557. */
  558. #ifdef CONFIG_ECC
  559. bl board_relocate_rom
  560. sync
  561. mr r3, r10 /* Destination Address */
  562. lis r4, CFG_MONITOR_BASE@h /* Source Address */
  563. ori r4, r4, CFG_MONITOR_BASE@l
  564. lwz r5, GOT(__init_end)
  565. sub r5, r5, r4
  566. li r6, CFG_CACHELINE_SIZE /* Cache Line Size */
  567. #else
  568. cmplw cr1,r3,r4
  569. addi r0,r5,3
  570. srwi. r0,r0,2
  571. beq cr1,4f /* In place copy is not necessary */
  572. beq 7f /* Protect against 0 count */
  573. mtctr r0
  574. bge cr1,2f
  575. la r8,-4(r4)
  576. la r7,-4(r3)
  577. 1: lwzu r0,4(r8)
  578. stwu r0,4(r7)
  579. bdnz 1b
  580. b 4f
  581. 2: slwi r0,r0,2
  582. add r8,r4,r0
  583. add r7,r3,r0
  584. 3: lwzu r0,-4(r8)
  585. stwu r0,-4(r7)
  586. bdnz 3b
  587. #endif
  588. /*
  589. * Now flush the cache: note that we must start from a cache aligned
  590. * address. Otherwise we might miss one cache line.
  591. */
  592. 4: cmpwi r6,0
  593. add r5,r3,r5
  594. beq 7f /* Always flush prefetch queue in any case */
  595. subi r0,r6,1
  596. andc r3,r3,r0
  597. mr r4,r3
  598. 5: dcbst 0,r4
  599. add r4,r4,r6
  600. cmplw r4,r5
  601. blt 5b
  602. sync /* Wait for all dcbst to complete on bus */
  603. mr r4,r3
  604. 6: icbi 0,r4
  605. add r4,r4,r6
  606. cmplw r4,r5
  607. blt 6b
  608. 7: sync /* Wait for all icbi to complete on bus */
  609. isync
  610. /*
  611. * We are done. Do not return, instead branch to second part of board
  612. * initialization, now running from RAM.
  613. */
  614. addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
  615. mtlr r0
  616. blr
  617. in_ram:
  618. #ifdef CONFIG_ECC
  619. bl board_init_ecc
  620. #endif
  621. /*
  622. * Relocation Function, r14 point to got2+0x8000
  623. *
  624. * Adjust got2 pointers, no need to check for 0, this code
  625. * already puts a few entries in the table.
  626. */
  627. li r0,__got2_entries@sectoff@l
  628. la r3,GOT(_GOT2_TABLE_)
  629. lwz r11,GOT(_GOT2_TABLE_)
  630. mtctr r0
  631. sub r11,r3,r11
  632. addi r3,r3,-4
  633. 1: lwzu r0,4(r3)
  634. add r0,r0,r11
  635. stw r0,0(r3)
  636. bdnz 1b
  637. /*
  638. * Now adjust the fixups and the pointers to the fixups
  639. * in case we need to move ourselves again.
  640. */
  641. 2: li r0,__fixup_entries@sectoff@l
  642. lwz r3,GOT(_FIXUP_TABLE_)
  643. cmpwi r0,0
  644. mtctr r0
  645. addi r3,r3,-4
  646. beq 4f
  647. 3: lwzu r4,4(r3)
  648. lwzux r0,r4,r11
  649. add r0,r0,r11
  650. stw r10,0(r3)
  651. stw r0,0(r4)
  652. bdnz 3b
  653. 4:
  654. /* clear_bss: */
  655. /*
  656. * Now clear BSS segment
  657. */
  658. lwz r3,GOT(__bss_start)
  659. lwz r4,GOT(_end)
  660. cmplw 0, r3, r4
  661. beq 6f
  662. li r0, 0
  663. 5:
  664. stw r0, 0(r3)
  665. addi r3, r3, 4
  666. cmplw 0, r3, r4
  667. bne 5b
  668. 6:
  669. mr r3, r10 /* Destination Address */
  670. #if defined(CONFIG_AMIGAONEG3SE) || \
  671. defined(CONFIG_DB64360) || \
  672. defined(CONFIG_DB64460) || \
  673. defined(CONFIG_CPCI750) || \
  674. defined(CONFIG_PPMC7XX) || \
  675. defined(CONFIG_P3Mx)
  676. mr r4, r9 /* Use RAM copy of the global data */
  677. #endif
  678. bl after_reloc
  679. /* not reached - end relocate_code */
  680. /*-----------------------------------------------------------------------*/
  681. /*
  682. * Copy exception vector code to low memory
  683. *
  684. * r3: dest_addr
  685. * r7: source address, r8: end address, r9: target address
  686. */
  687. .globl trap_init
  688. trap_init:
  689. lwz r7, GOT(_start)
  690. lwz r8, GOT(_end_of_vectors)
  691. li r9, 0x100 /* reset vector always at 0x100 */
  692. cmplw 0, r7, r8
  693. bgelr /* return if r7>=r8 - just in case */
  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. * relocate `hdlr' and `int_return' entries
  704. */
  705. li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
  706. li r8, Alignment - _start + EXC_OFF_SYS_RESET
  707. 2:
  708. bl trap_reloc
  709. addi r7, r7, 0x100 /* next exception vector */
  710. cmplw 0, r7, r8
  711. blt 2b
  712. li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
  713. bl trap_reloc
  714. li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
  715. bl trap_reloc
  716. li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
  717. li r8, SystemCall - _start + EXC_OFF_SYS_RESET
  718. 3:
  719. bl trap_reloc
  720. addi r7, r7, 0x100 /* next exception vector */
  721. cmplw 0, r7, r8
  722. blt 3b
  723. li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
  724. li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
  725. 4:
  726. bl trap_reloc
  727. addi r7, r7, 0x100 /* next exception vector */
  728. cmplw 0, r7, r8
  729. blt 4b
  730. /* enable execptions from RAM vectors */
  731. mfmsr r7
  732. li r8,MSR_IP
  733. andc r7,r7,r8
  734. mtmsr r7
  735. mtlr r4 /* restore link register */
  736. blr
  737. /*
  738. * Function: relocate entries for one exception vector
  739. */
  740. trap_reloc:
  741. lwz r0, 0(r7) /* hdlr ... */
  742. add r0, r0, r3 /* ... += dest_addr */
  743. stw r0, 0(r7)
  744. lwz r0, 4(r7) /* int_return ... */
  745. add r0, r0, r3 /* ... += dest_addr */
  746. stw r0, 4(r7)
  747. sync
  748. isync
  749. blr
  750. #ifdef CFG_INIT_RAM_LOCK
  751. lock_ram_in_cache:
  752. /* Allocate Initial RAM in data cache.
  753. */
  754. lis r3, (CFG_INIT_RAM_ADDR & ~31)@h
  755. ori r3, r3, (CFG_INIT_RAM_ADDR & ~31)@l
  756. li r2, ((CFG_INIT_RAM_END & ~31) + \
  757. (CFG_INIT_RAM_ADDR & 31) + 31) / 32
  758. mtctr r2
  759. 1:
  760. dcbz r0, r3
  761. addi r3, r3, 32
  762. bdnz 1b
  763. /* Lock the data cache */
  764. mfspr r0, HID0
  765. ori r0, r0, 0x1000
  766. sync
  767. mtspr HID0, r0
  768. sync
  769. blr
  770. .globl unlock_ram_in_cache
  771. unlock_ram_in_cache:
  772. /* invalidate the INIT_RAM section */
  773. lis r3, (CFG_INIT_RAM_ADDR & ~31)@h
  774. ori r3, r3, (CFG_INIT_RAM_ADDR & ~31)@l
  775. li r2, ((CFG_INIT_RAM_END & ~31) + \
  776. (CFG_INIT_RAM_ADDR & 31) + 31) / 32
  777. mtctr r2
  778. 1: icbi r0, r3
  779. addi r3, r3, 32
  780. bdnz 1b
  781. sync /* Wait for all icbi to complete on bus */
  782. isync
  783. /* Unlock the data cache and invalidate it */
  784. mfspr r0, HID0
  785. li r3,0x1000
  786. andc r0,r0,r3
  787. li r3,0x0400
  788. or r0,r0,r3
  789. sync
  790. mtspr HID0, r0
  791. sync
  792. blr
  793. #endif