start.S 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * Copyright 2004, 2007 Freescale Semiconductor.
  3. * Srikanth Srinivasan <srikanth.srinivaan@freescale.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* U-Boot - Startup Code for 86xx PowerPC based Embedded Boards
  24. *
  25. *
  26. * The processor starts at 0xfff00100 and the code is executed
  27. * from flash. The code is organized to be at an other address
  28. * in memory, but as long we don't jump around before relocating.
  29. * board_init lies at a quite high address and when the cpu has
  30. * jumped there, everything is ok.
  31. */
  32. #include <config.h>
  33. #include <mpc86xx.h>
  34. #include <timestamp.h>
  35. #include <version.h>
  36. #include <ppc_asm.tmpl>
  37. #include <ppc_defs.h>
  38. #include <asm/cache.h>
  39. #include <asm/mmu.h>
  40. #ifndef CONFIG_IDENT_STRING
  41. #define CONFIG_IDENT_STRING ""
  42. #endif
  43. /*
  44. * Need MSR_DR | MSR_IR enabled to access I/O (printf) in exceptions
  45. */
  46. /*
  47. * Set up GOT: Global Offset Table
  48. *
  49. * Use r14 to access the GOT
  50. */
  51. START_GOT
  52. GOT_ENTRY(_GOT2_TABLE_)
  53. GOT_ENTRY(_FIXUP_TABLE_)
  54. GOT_ENTRY(_start)
  55. GOT_ENTRY(_start_of_vectors)
  56. GOT_ENTRY(_end_of_vectors)
  57. GOT_ENTRY(transfer_to_handler)
  58. GOT_ENTRY(__init_end)
  59. GOT_ENTRY(_end)
  60. GOT_ENTRY(__bss_start)
  61. END_GOT
  62. /*
  63. * r3 - 1st arg to board_init(): IMMP pointer
  64. * r4 - 2nd arg to board_init(): boot flag
  65. */
  66. .text
  67. .long 0x27051956 /* U-Boot Magic Number */
  68. .globl version_string
  69. version_string:
  70. .ascii U_BOOT_VERSION
  71. .ascii " (", U_BOOT_DATE, " - ", U_BOOT_TIME, ")"
  72. .ascii CONFIG_IDENT_STRING, "\0"
  73. . = EXC_OFF_SYS_RESET
  74. .globl _start
  75. _start:
  76. li r21, BOOTFLAG_COLD /* Normal Power-On: Boot from FLASH */
  77. b boot_cold
  78. sync
  79. . = EXC_OFF_SYS_RESET + 0x10
  80. .globl _start_warm
  81. _start_warm:
  82. li r21, BOOTFLAG_WARM /* Software reboot */
  83. b boot_warm
  84. sync
  85. /* the boot code is located below the exception table */
  86. .globl _start_of_vectors
  87. _start_of_vectors:
  88. /* Machine check */
  89. STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
  90. /* Data Storage exception. */
  91. STD_EXCEPTION(0x300, DataStorage, UnknownException)
  92. /* Instruction Storage exception. */
  93. STD_EXCEPTION(0x400, InstStorage, UnknownException)
  94. /* External Interrupt exception. */
  95. STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
  96. /* Alignment exception. */
  97. . = 0x600
  98. Alignment:
  99. EXCEPTION_PROLOG(SRR0, SRR1)
  100. mfspr r4,DAR
  101. stw r4,_DAR(r21)
  102. mfspr r5,DSISR
  103. stw r5,_DSISR(r21)
  104. addi r3,r1,STACK_FRAME_OVERHEAD
  105. li r20,MSR_KERNEL
  106. rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
  107. lwz r6,GOT(transfer_to_handler)
  108. mtlr r6
  109. blrl
  110. .L_Alignment:
  111. .long AlignmentException - _start + EXC_OFF_SYS_RESET
  112. .long int_return - _start + EXC_OFF_SYS_RESET
  113. /* Program check exception */
  114. . = 0x700
  115. ProgramCheck:
  116. EXCEPTION_PROLOG(SRR0, SRR1)
  117. addi r3,r1,STACK_FRAME_OVERHEAD
  118. li r20,MSR_KERNEL
  119. rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
  120. lwz r6,GOT(transfer_to_handler)
  121. mtlr r6
  122. blrl
  123. .L_ProgramCheck:
  124. .long ProgramCheckException - _start + EXC_OFF_SYS_RESET
  125. .long int_return - _start + EXC_OFF_SYS_RESET
  126. STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
  127. /* I guess we could implement decrementer, and may have
  128. * to someday for timekeeping.
  129. */
  130. STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
  131. STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
  132. STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
  133. STD_EXCEPTION(0xc00, SystemCall, UnknownException)
  134. STD_EXCEPTION(0xd00, SingleStep, UnknownException)
  135. STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
  136. STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
  137. STD_EXCEPTION(0x1000, SoftEmu, SoftEmuException)
  138. STD_EXCEPTION(0x1100, InstructionTLBMiss, UnknownException)
  139. STD_EXCEPTION(0x1200, DataTLBMiss, UnknownException)
  140. STD_EXCEPTION(0x1300, InstructionTLBError, UnknownException)
  141. STD_EXCEPTION(0x1400, DataTLBError, UnknownException)
  142. STD_EXCEPTION(0x1500, Reserved5, UnknownException)
  143. STD_EXCEPTION(0x1600, Reserved6, UnknownException)
  144. STD_EXCEPTION(0x1700, Reserved7, UnknownException)
  145. STD_EXCEPTION(0x1800, Reserved8, UnknownException)
  146. STD_EXCEPTION(0x1900, Reserved9, UnknownException)
  147. STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
  148. STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
  149. STD_EXCEPTION(0x1c00, DataBreakpoint, UnknownException)
  150. STD_EXCEPTION(0x1d00, InstructionBreakpoint, UnknownException)
  151. STD_EXCEPTION(0x1e00, PeripheralBreakpoint, UnknownException)
  152. STD_EXCEPTION(0x1f00, DevPortBreakpoint, UnknownException)
  153. .globl _end_of_vectors
  154. _end_of_vectors:
  155. . = 0x2000
  156. boot_cold:
  157. boot_warm:
  158. /*
  159. * NOTE: Only Cpu 0 will ever come here. Other cores go to an
  160. * address specified by the BPTR
  161. */
  162. 1:
  163. #ifdef CONFIG_SYS_RAMBOOT
  164. /* disable everything */
  165. li r0, 0
  166. mtspr HID0, r0
  167. sync
  168. mtmsr 0
  169. #endif
  170. /* Invalidate BATs */
  171. bl invalidate_bats
  172. sync
  173. /* Invalidate all of TLB before MMU turn on */
  174. bl clear_tlbs
  175. sync
  176. #ifdef CONFIG_SYS_L2
  177. /* init the L2 cache */
  178. lis r3, L2_INIT@h
  179. ori r3, r3, L2_INIT@l
  180. mtspr l2cr, r3
  181. /* invalidate the L2 cache */
  182. bl l2cache_invalidate
  183. sync
  184. #endif
  185. /*
  186. * Calculate absolute address in FLASH and jump there
  187. *------------------------------------------------------*/
  188. lis r3, CONFIG_SYS_MONITOR_BASE_EARLY@h
  189. ori r3, r3, CONFIG_SYS_MONITOR_BASE_EARLY@l
  190. addi r3, r3, in_flash - _start + EXC_OFF_SYS_RESET
  191. mtlr r3
  192. blr
  193. in_flash:
  194. /* let the C-code set up the rest */
  195. /* */
  196. /* Be careful to keep code relocatable ! */
  197. /*------------------------------------------------------*/
  198. /* perform low-level init */
  199. /* enable extended addressing */
  200. bl enable_ext_addr
  201. /* setup the bats */
  202. bl early_bats
  203. /*
  204. * Cache must be enabled here for stack-in-cache trick.
  205. * This means we need to enable the BATS.
  206. * Cache should be turned on after BATs, since by default
  207. * everything is write-through.
  208. */
  209. /* enable address translation */
  210. mfmsr r5
  211. ori r5, r5, (MSR_IR | MSR_DR)
  212. lis r3,addr_trans_enabled@h
  213. ori r3, r3, addr_trans_enabled@l
  214. mtspr SPRN_SRR0,r3
  215. mtspr SPRN_SRR1,r5
  216. rfi
  217. addr_trans_enabled:
  218. /* enable and invalidate the data cache */
  219. /* bl l1dcache_enable */
  220. bl dcache_enable
  221. sync
  222. #if 1
  223. bl icache_enable
  224. #endif
  225. #ifdef CONFIG_SYS_INIT_RAM_LOCK
  226. bl lock_ram_in_cache
  227. sync
  228. #endif
  229. #if (CONFIG_SYS_CCSRBAR_DEFAULT != CONFIG_SYS_CCSRBAR)
  230. bl setup_ccsrbar
  231. #endif
  232. /* set up the stack pointer in our newly created
  233. * cache-ram (r1) */
  234. lis r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@h
  235. ori r1, r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@l
  236. li r0, 0 /* Make room for stack frame header and */
  237. stwu r0, -4(r1) /* clear final stack frame so that */
  238. stwu r0, -4(r1) /* stack backtraces terminate cleanly */
  239. GET_GOT /* initialize GOT access */
  240. /* run low-level CPU init code (from Flash) */
  241. bl cpu_init_f
  242. sync
  243. #ifdef RUN_DIAG
  244. /* Load PX_AUX register address in r4 */
  245. lis r4, PIXIS_BASE@h
  246. ori r4, r4, 0x6
  247. /* Load contents of PX_AUX in r3 bits 24 to 31*/
  248. lbz r3, 0(r4)
  249. /* Mask and obtain the bit in r3 */
  250. rlwinm. r3, r3, 0, 24, 24
  251. /* If not zero, jump and continue with u-boot */
  252. bne diag_done
  253. /* Load back contents of PX_AUX in r3 bits 24 to 31 */
  254. lbz r3, 0(r4)
  255. /* Set the MSB of the register value */
  256. ori r3, r3, 0x80
  257. /* Write value in r3 back to PX_AUX */
  258. stb r3, 0(r4)
  259. /* Get the address to jump to in r3*/
  260. lis r3, CONFIG_SYS_DIAG_ADDR@h
  261. ori r3, r3, CONFIG_SYS_DIAG_ADDR@l
  262. /* Load the LR with the branch address */
  263. mtlr r3
  264. /* Branch to diagnostic */
  265. blr
  266. diag_done:
  267. #endif
  268. /* bl l2cache_enable */
  269. mr r3, r21
  270. /* r3: BOOTFLAG */
  271. /* run 1st part of board init code (from Flash) */
  272. bl board_init_f
  273. sync
  274. /* NOTREACHED */
  275. .globl invalidate_bats
  276. invalidate_bats:
  277. li r0, 0
  278. /* invalidate BATs */
  279. mtspr IBAT0U, r0
  280. mtspr IBAT1U, r0
  281. mtspr IBAT2U, r0
  282. mtspr IBAT3U, r0
  283. mtspr IBAT4U, r0
  284. mtspr IBAT5U, r0
  285. mtspr IBAT6U, r0
  286. mtspr IBAT7U, r0
  287. isync
  288. mtspr DBAT0U, r0
  289. mtspr DBAT1U, r0
  290. mtspr DBAT2U, r0
  291. mtspr DBAT3U, r0
  292. mtspr DBAT4U, r0
  293. mtspr DBAT5U, r0
  294. mtspr DBAT6U, r0
  295. mtspr DBAT7U, r0
  296. isync
  297. sync
  298. blr
  299. /*
  300. * early_bats:
  301. *
  302. * Set up bats needed early on - this is usually the BAT for the
  303. * stack-in-cache, the Flash, and CCSR space
  304. */
  305. .globl early_bats
  306. early_bats:
  307. /* IBAT 3 */
  308. lis r4, CONFIG_SYS_IBAT3L@h
  309. ori r4, r4, CONFIG_SYS_IBAT3L@l
  310. lis r3, CONFIG_SYS_IBAT3U@h
  311. ori r3, r3, CONFIG_SYS_IBAT3U@l
  312. mtspr IBAT3L, r4
  313. mtspr IBAT3U, r3
  314. isync
  315. /* DBAT 3 */
  316. lis r4, CONFIG_SYS_DBAT3L@h
  317. ori r4, r4, CONFIG_SYS_DBAT3L@l
  318. lis r3, CONFIG_SYS_DBAT3U@h
  319. ori r3, r3, CONFIG_SYS_DBAT3U@l
  320. mtspr DBAT3L, r4
  321. mtspr DBAT3U, r3
  322. isync
  323. /* IBAT 5 */
  324. lis r4, CONFIG_SYS_IBAT5L@h
  325. ori r4, r4, CONFIG_SYS_IBAT5L@l
  326. lis r3, CONFIG_SYS_IBAT5U@h
  327. ori r3, r3, CONFIG_SYS_IBAT5U@l
  328. mtspr IBAT5L, r4
  329. mtspr IBAT5U, r3
  330. isync
  331. /* DBAT 5 */
  332. lis r4, CONFIG_SYS_DBAT5L@h
  333. ori r4, r4, CONFIG_SYS_DBAT5L@l
  334. lis r3, CONFIG_SYS_DBAT5U@h
  335. ori r3, r3, CONFIG_SYS_DBAT5U@l
  336. mtspr DBAT5L, r4
  337. mtspr DBAT5U, r3
  338. isync
  339. /* IBAT 6 */
  340. lis r4, CONFIG_SYS_IBAT6L_EARLY@h
  341. ori r4, r4, CONFIG_SYS_IBAT6L_EARLY@l
  342. lis r3, CONFIG_SYS_IBAT6U_EARLY@h
  343. ori r3, r3, CONFIG_SYS_IBAT6U_EARLY@l
  344. mtspr IBAT6L, r4
  345. mtspr IBAT6U, r3
  346. isync
  347. /* DBAT 6 */
  348. lis r4, CONFIG_SYS_DBAT6L_EARLY@h
  349. ori r4, r4, CONFIG_SYS_DBAT6L_EARLY@l
  350. lis r3, CONFIG_SYS_DBAT6U_EARLY@h
  351. ori r3, r3, CONFIG_SYS_DBAT6U_EARLY@l
  352. mtspr DBAT6L, r4
  353. mtspr DBAT6U, r3
  354. isync
  355. #if(CONFIG_SYS_CCSRBAR_DEFAULT != CONFIG_SYS_CCSRBAR)
  356. /* IBAT 7 */
  357. lis r4, CONFIG_SYS_CCSR_DEFAULT_IBATL@h
  358. ori r4, r4, CONFIG_SYS_CCSR_DEFAULT_IBATL@l
  359. lis r3, CONFIG_SYS_CCSR_DEFAULT_IBATU@h
  360. ori r3, r3, CONFIG_SYS_CCSR_DEFAULT_IBATU@l
  361. mtspr IBAT7L, r4
  362. mtspr IBAT7U, r3
  363. isync
  364. /* DBAT 7 */
  365. lis r4, CONFIG_SYS_CCSR_DEFAULT_DBATL@h
  366. ori r4, r4, CONFIG_SYS_CCSR_DEFAULT_DBATL@l
  367. lis r3, CONFIG_SYS_CCSR_DEFAULT_DBATU@h
  368. ori r3, r3, CONFIG_SYS_CCSR_DEFAULT_DBATU@l
  369. mtspr DBAT7L, r4
  370. mtspr DBAT7U, r3
  371. isync
  372. #endif
  373. blr
  374. .globl clear_tlbs
  375. clear_tlbs:
  376. addis r3, 0, 0x0000
  377. addis r5, 0, 0x4
  378. isync
  379. tlblp:
  380. tlbie r3
  381. sync
  382. addi r3, r3, 0x1000
  383. cmp 0, 0, r3, r5
  384. blt tlblp
  385. blr
  386. .globl disable_addr_trans
  387. disable_addr_trans:
  388. /* disable address translation */
  389. mflr r4
  390. mfmsr r3
  391. andi. r0, r3, (MSR_IR | MSR_DR)
  392. beqlr
  393. andc r3, r3, r0
  394. mtspr SRR0, r4
  395. mtspr SRR1, r3
  396. rfi
  397. /*
  398. * This code finishes saving the registers to the exception frame
  399. * and jumps to the appropriate handler for the exception.
  400. * Register r21 is pointer into trap frame, r1 has new stack pointer.
  401. */
  402. .globl transfer_to_handler
  403. transfer_to_handler:
  404. stw r22,_NIP(r21)
  405. lis r22,MSR_POW@h
  406. andc r23,r23,r22
  407. stw r23,_MSR(r21)
  408. SAVE_GPR(7, r21)
  409. SAVE_4GPRS(8, r21)
  410. SAVE_8GPRS(12, r21)
  411. SAVE_8GPRS(24, r21)
  412. mflr r23
  413. andi. r24,r23,0x3f00 /* get vector offset */
  414. stw r24,TRAP(r21)
  415. li r22,0
  416. stw r22,RESULT(r21)
  417. mtspr SPRG2,r22 /* r1 is now kernel sp */
  418. lwz r24,0(r23) /* virtual address of handler */
  419. lwz r23,4(r23) /* where to go when done */
  420. mtspr SRR0,r24
  421. mtspr SRR1,r20
  422. mtlr r23
  423. SYNC
  424. rfi /* jump to handler, enable MMU */
  425. int_return:
  426. mfmsr r28 /* Disable interrupts */
  427. li r4,0
  428. ori r4,r4,MSR_EE
  429. andc r28,r28,r4
  430. SYNC /* Some chip revs need this... */
  431. mtmsr r28
  432. SYNC
  433. lwz r2,_CTR(r1)
  434. lwz r0,_LINK(r1)
  435. mtctr r2
  436. mtlr r0
  437. lwz r2,_XER(r1)
  438. lwz r0,_CCR(r1)
  439. mtspr XER,r2
  440. mtcrf 0xFF,r0
  441. REST_10GPRS(3, r1)
  442. REST_10GPRS(13, r1)
  443. REST_8GPRS(23, r1)
  444. REST_GPR(31, r1)
  445. lwz r2,_NIP(r1) /* Restore environment */
  446. lwz r0,_MSR(r1)
  447. mtspr SRR0,r2
  448. mtspr SRR1,r0
  449. lwz r0,GPR0(r1)
  450. lwz r2,GPR2(r1)
  451. lwz r1,GPR1(r1)
  452. SYNC
  453. rfi
  454. .globl dc_read
  455. dc_read:
  456. blr
  457. .globl get_pvr
  458. get_pvr:
  459. mfspr r3, PVR
  460. blr
  461. .globl get_svr
  462. get_svr:
  463. mfspr r3, SVR
  464. blr
  465. /*
  466. * Function: in8
  467. * Description: Input 8 bits
  468. */
  469. .globl in8
  470. in8:
  471. lbz r3,0x0000(r3)
  472. blr
  473. /*
  474. * Function: out8
  475. * Description: Output 8 bits
  476. */
  477. .globl out8
  478. out8:
  479. stb r4,0x0000(r3)
  480. blr
  481. /*
  482. * Function: out16
  483. * Description: Output 16 bits
  484. */
  485. .globl out16
  486. out16:
  487. sth r4,0x0000(r3)
  488. blr
  489. /*
  490. * Function: out16r
  491. * Description: Byte reverse and output 16 bits
  492. */
  493. .globl out16r
  494. out16r:
  495. sthbrx r4,r0,r3
  496. blr
  497. /*
  498. * Function: out32
  499. * Description: Output 32 bits
  500. */
  501. .globl out32
  502. out32:
  503. stw r4,0x0000(r3)
  504. blr
  505. /*
  506. * Function: out32r
  507. * Description: Byte reverse and output 32 bits
  508. */
  509. .globl out32r
  510. out32r:
  511. stwbrx r4,r0,r3
  512. blr
  513. /*
  514. * Function: in16
  515. * Description: Input 16 bits
  516. */
  517. .globl in16
  518. in16:
  519. lhz r3,0x0000(r3)
  520. blr
  521. /*
  522. * Function: in16r
  523. * Description: Input 16 bits and byte reverse
  524. */
  525. .globl in16r
  526. in16r:
  527. lhbrx r3,r0,r3
  528. blr
  529. /*
  530. * Function: in32
  531. * Description: Input 32 bits
  532. */
  533. .globl in32
  534. in32:
  535. lwz 3,0x0000(3)
  536. blr
  537. /*
  538. * Function: in32r
  539. * Description: Input 32 bits and byte reverse
  540. */
  541. .globl in32r
  542. in32r:
  543. lwbrx r3,r0,r3
  544. blr
  545. /*
  546. * void relocate_code (addr_sp, gd, addr_moni)
  547. *
  548. * This "function" does not return, instead it continues in RAM
  549. * after relocating the monitor code.
  550. *
  551. * r3 = dest
  552. * r4 = src
  553. * r5 = length in bytes
  554. * r6 = cachelinesize
  555. */
  556. .globl relocate_code
  557. relocate_code:
  558. mr r1, r3 /* Set new stack pointer */
  559. mr r9, r4 /* Save copy of Global Data pointer */
  560. mr r10, r5 /* Save copy of Destination Address */
  561. mr r3, r5 /* Destination Address */
  562. lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
  563. ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
  564. lwz r5, GOT(__init_end)
  565. sub r5, r5, r4
  566. li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
  567. /*
  568. * Fix GOT pointer:
  569. *
  570. * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
  571. *
  572. * Offset:
  573. */
  574. sub r15, r10, r4
  575. /* First our own GOT */
  576. add r14, r14, r15
  577. /* then the one used by the C code */
  578. add r30, r30, r15
  579. /*
  580. * Now relocate code
  581. */
  582. cmplw cr1,r3,r4
  583. addi r0,r5,3
  584. srwi. r0,r0,2
  585. beq cr1,4f /* In place copy is not necessary */
  586. beq 7f /* Protect against 0 count */
  587. mtctr r0
  588. bge cr1,2f
  589. la r8,-4(r4)
  590. la r7,-4(r3)
  591. 1: lwzu r0,4(r8)
  592. stwu r0,4(r7)
  593. bdnz 1b
  594. b 4f
  595. 2: slwi r0,r0,2
  596. add r8,r4,r0
  597. add r7,r3,r0
  598. 3: lwzu r0,-4(r8)
  599. stwu r0,-4(r7)
  600. bdnz 3b
  601. /*
  602. * Now flush the cache: note that we must start from a cache aligned
  603. * address. Otherwise we might miss one cache line.
  604. */
  605. 4: cmpwi r6,0
  606. add r5,r3,r5
  607. beq 7f /* Always flush prefetch queue in any case */
  608. subi r0,r6,1
  609. andc r3,r3,r0
  610. mr r4,r3
  611. 5: dcbst 0,r4
  612. add r4,r4,r6
  613. cmplw r4,r5
  614. blt 5b
  615. sync /* Wait for all dcbst to complete on bus */
  616. mr r4,r3
  617. 6: icbi 0,r4
  618. add r4,r4,r6
  619. cmplw r4,r5
  620. blt 6b
  621. 7: sync /* Wait for all icbi to complete on bus */
  622. isync
  623. /*
  624. * We are done. Do not return, instead branch to second part of board
  625. * initialization, now running from RAM.
  626. */
  627. addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
  628. mtlr r0
  629. blr
  630. in_ram:
  631. /*
  632. * Relocation Function, r14 point to got2+0x8000
  633. *
  634. * Adjust got2 pointers, no need to check for 0, this code
  635. * already puts a few entries in the table.
  636. */
  637. li r0,__got2_entries@sectoff@l
  638. la r3,GOT(_GOT2_TABLE_)
  639. lwz r11,GOT(_GOT2_TABLE_)
  640. mtctr r0
  641. sub r11,r3,r11
  642. addi r3,r3,-4
  643. 1: lwzu r0,4(r3)
  644. add r0,r0,r11
  645. stw r0,0(r3)
  646. bdnz 1b
  647. /*
  648. * Now adjust the fixups and the pointers to the fixups
  649. * in case we need to move ourselves again.
  650. */
  651. 2: li r0,__fixup_entries@sectoff@l
  652. lwz r3,GOT(_FIXUP_TABLE_)
  653. cmpwi r0,0
  654. mtctr r0
  655. addi r3,r3,-4
  656. beq 4f
  657. 3: lwzu r4,4(r3)
  658. lwzux r0,r4,r11
  659. add r0,r0,r11
  660. stw r10,0(r3)
  661. stw r0,0(r4)
  662. bdnz 3b
  663. 4:
  664. /* clear_bss: */
  665. /*
  666. * Now clear BSS segment
  667. */
  668. lwz r3,GOT(__bss_start)
  669. lwz r4,GOT(_end)
  670. cmplw 0, r3, r4
  671. beq 6f
  672. li r0, 0
  673. 5:
  674. stw r0, 0(r3)
  675. addi r3, r3, 4
  676. cmplw 0, r3, r4
  677. bne 5b
  678. 6:
  679. mr r3, r9 /* Init Date pointer */
  680. mr r4, r10 /* Destination Address */
  681. bl board_init_r
  682. /* not reached - end relocate_code */
  683. /*-----------------------------------------------------------------------*/
  684. /*
  685. * Copy exception vector code to low memory
  686. *
  687. * r3: dest_addr
  688. * r7: source address, r8: end address, r9: target address
  689. */
  690. .globl trap_init
  691. trap_init:
  692. lwz r7, GOT(_start)
  693. lwz r8, GOT(_end_of_vectors)
  694. li r9, 0x100 /* reset vector always at 0x100 */
  695. cmplw 0, r7, r8
  696. bgelr /* return if r7>=r8 - just in case */
  697. mflr r4 /* save link register */
  698. 1:
  699. lwz r0, 0(r7)
  700. stw r0, 0(r9)
  701. addi r7, r7, 4
  702. addi r9, r9, 4
  703. cmplw 0, r7, r8
  704. bne 1b
  705. /*
  706. * relocate `hdlr' and `int_return' entries
  707. */
  708. li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
  709. li r8, Alignment - _start + EXC_OFF_SYS_RESET
  710. 2:
  711. bl trap_reloc
  712. addi r7, r7, 0x100 /* next exception vector */
  713. cmplw 0, r7, r8
  714. blt 2b
  715. li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
  716. bl trap_reloc
  717. li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
  718. bl trap_reloc
  719. li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
  720. li r8, SystemCall - _start + EXC_OFF_SYS_RESET
  721. 3:
  722. bl trap_reloc
  723. addi r7, r7, 0x100 /* next exception vector */
  724. cmplw 0, r7, r8
  725. blt 3b
  726. li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
  727. li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
  728. 4:
  729. bl trap_reloc
  730. addi r7, r7, 0x100 /* next exception vector */
  731. cmplw 0, r7, r8
  732. blt 4b
  733. /* enable execptions from RAM vectors */
  734. mfmsr r7
  735. li r8,MSR_IP
  736. andc r7,r7,r8
  737. ori r7,r7,MSR_ME /* Enable Machine Check */
  738. mtmsr r7
  739. mtlr r4 /* restore link register */
  740. blr
  741. /*
  742. * Function: relocate entries for one exception vector
  743. */
  744. trap_reloc:
  745. lwz r0, 0(r7) /* hdlr ... */
  746. add r0, r0, r3 /* ... += dest_addr */
  747. stw r0, 0(r7)
  748. lwz r0, 4(r7) /* int_return ... */
  749. add r0, r0, r3 /* ... += dest_addr */
  750. stw r0, 4(r7)
  751. sync
  752. isync
  753. blr
  754. .globl enable_ext_addr
  755. enable_ext_addr:
  756. mfspr r0, HID0
  757. lis r0, (HID0_HIGH_BAT_EN | HID0_XBSEN | HID0_XAEN)@h
  758. ori r0, r0, (HID0_HIGH_BAT_EN | HID0_XBSEN | HID0_XAEN)@l
  759. mtspr HID0, r0
  760. sync
  761. isync
  762. blr
  763. #if (CONFIG_SYS_CCSRBAR_DEFAULT != CONFIG_SYS_CCSRBAR)
  764. .globl setup_ccsrbar
  765. setup_ccsrbar:
  766. /* Special sequence needed to update CCSRBAR itself */
  767. lis r4, CONFIG_SYS_CCSRBAR_DEFAULT@h
  768. ori r4, r4, CONFIG_SYS_CCSRBAR_DEFAULT@l
  769. lis r5, CONFIG_SYS_CCSRBAR_PHYS_LOW@h
  770. ori r5, r5, CONFIG_SYS_CCSRBAR_PHYS_LOW@l
  771. srwi r5,r5,12
  772. li r6, CONFIG_SYS_CCSRBAR_PHYS_HIGH@l
  773. rlwimi r5,r6,20,8,11
  774. stw r5, 0(r4) /* Store physical value of CCSR */
  775. isync
  776. lis r5, TEXT_BASE@h
  777. ori r5,r5,TEXT_BASE@l
  778. lwz r5, 0(r5)
  779. isync
  780. /* Use VA of CCSR to do read */
  781. lis r3, CONFIG_SYS_CCSRBAR@h
  782. lwz r5, CONFIG_SYS_CCSRBAR@l(r3)
  783. isync
  784. blr
  785. #endif
  786. #ifdef CONFIG_SYS_INIT_RAM_LOCK
  787. lock_ram_in_cache:
  788. /* Allocate Initial RAM in data cache.
  789. */
  790. lis r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@h
  791. ori r3, r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@l
  792. li r4, ((CONFIG_SYS_INIT_RAM_END & ~31) + \
  793. (CONFIG_SYS_INIT_RAM_ADDR & 31) + 31) / 32
  794. mtctr r4
  795. 1:
  796. dcbz r0, r3
  797. addi r3, r3, 32
  798. bdnz 1b
  799. #if 1
  800. /* Lock the data cache */
  801. mfspr r0, HID0
  802. ori r0, r0, 0x1000
  803. sync
  804. mtspr HID0, r0
  805. sync
  806. blr
  807. #endif
  808. #if 0
  809. /* Lock the first way of the data cache */
  810. mfspr r0, LDSTCR
  811. ori r0, r0, 0x0080
  812. #if defined(CONFIG_ALTIVEC)
  813. dssall
  814. #endif
  815. sync
  816. mtspr LDSTCR, r0
  817. sync
  818. isync
  819. blr
  820. #endif
  821. .globl unlock_ram_in_cache
  822. unlock_ram_in_cache:
  823. /* invalidate the INIT_RAM section */
  824. lis r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@h
  825. ori r3, r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@l
  826. li r4, ((CONFIG_SYS_INIT_RAM_END & ~31) + \
  827. (CONFIG_SYS_INIT_RAM_ADDR & 31) + 31) / 32
  828. mtctr r4
  829. 1: icbi r0, r3
  830. addi r3, r3, 32
  831. bdnz 1b
  832. sync /* Wait for all icbi to complete on bus */
  833. isync
  834. #if 1
  835. /* Unlock the data cache and invalidate it */
  836. mfspr r0, HID0
  837. li r3,0x1000
  838. andc r0,r0,r3
  839. li r3,0x0400
  840. or r0,r0,r3
  841. sync
  842. mtspr HID0, r0
  843. sync
  844. blr
  845. #endif
  846. #if 0
  847. /* Unlock the first way of the data cache */
  848. mfspr r0, LDSTCR
  849. li r3,0x0080
  850. andc r0,r0,r3
  851. #ifdef CONFIG_ALTIVEC
  852. dssall
  853. #endif
  854. sync
  855. mtspr LDSTCR, r0
  856. sync
  857. isync
  858. li r3,0x0400
  859. or r0,r0,r3
  860. sync
  861. mtspr HID0, r0
  862. sync
  863. blr
  864. #endif
  865. #endif