head.S 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * linux/arch/arm/boot/compressed/head.S
  3. *
  4. * Copyright (C) 1996-2002 Russell King
  5. * Copyright (C) 2004 Hyok S. Choi (MPU support)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/linkage.h>
  12. /*
  13. * Debugging stuff
  14. *
  15. * Note that these macros must not contain any code which is not
  16. * 100% relocatable. Any attempt to do so will result in a crash.
  17. * Please select one of the following when turning on debugging.
  18. */
  19. #ifdef DEBUG
  20. #if defined(CONFIG_DEBUG_ICEDCC)
  21. #ifdef CONFIG_CPU_V6
  22. .macro loadsp, rb
  23. .endm
  24. .macro writeb, ch, rb
  25. mcr p14, 0, \ch, c0, c5, 0
  26. .endm
  27. #else
  28. .macro loadsp, rb
  29. .endm
  30. .macro writeb, ch, rb
  31. mcr p14, 0, \ch, c0, c1, 0
  32. .endm
  33. #endif
  34. #else
  35. #include <asm/arch/debug-macro.S>
  36. .macro writeb, ch, rb
  37. senduart \ch, \rb
  38. .endm
  39. #if defined(CONFIG_ARCH_SA1100)
  40. .macro loadsp, rb
  41. mov \rb, #0x80000000 @ physical base address
  42. #ifdef CONFIG_DEBUG_LL_SER3
  43. add \rb, \rb, #0x00050000 @ Ser3
  44. #else
  45. add \rb, \rb, #0x00010000 @ Ser1
  46. #endif
  47. .endm
  48. #elif defined(CONFIG_ARCH_S3C2410)
  49. .macro loadsp, rb
  50. mov \rb, #0x50000000
  51. add \rb, \rb, #0x4000 * CONFIG_S3C2410_LOWLEVEL_UART_PORT
  52. .endm
  53. #else
  54. .macro loadsp, rb
  55. addruart \rb
  56. .endm
  57. #endif
  58. #endif
  59. #endif
  60. .macro kputc,val
  61. mov r0, \val
  62. bl putc
  63. .endm
  64. .macro kphex,val,len
  65. mov r0, \val
  66. mov r1, #\len
  67. bl phex
  68. .endm
  69. .macro debug_reloc_start
  70. #ifdef DEBUG
  71. kputc #'\n'
  72. kphex r6, 8 /* processor id */
  73. kputc #':'
  74. kphex r7, 8 /* architecture id */
  75. #ifdef CONFIG_CPU_CP15
  76. kputc #':'
  77. mrc p15, 0, r0, c1, c0
  78. kphex r0, 8 /* control reg */
  79. #endif
  80. kputc #'\n'
  81. kphex r5, 8 /* decompressed kernel start */
  82. kputc #'-'
  83. kphex r9, 8 /* decompressed kernel end */
  84. kputc #'>'
  85. kphex r4, 8 /* kernel execution address */
  86. kputc #'\n'
  87. #endif
  88. .endm
  89. .macro debug_reloc_end
  90. #ifdef DEBUG
  91. kphex r5, 8 /* end of kernel */
  92. kputc #'\n'
  93. mov r0, r4
  94. bl memdump /* dump 256 bytes at start of kernel */
  95. #endif
  96. .endm
  97. .section ".start", #alloc, #execinstr
  98. /*
  99. * sort out different calling conventions
  100. */
  101. .align
  102. start:
  103. .type start,#function
  104. .rept 8
  105. mov r0, r0
  106. .endr
  107. b 1f
  108. .word 0x016f2818 @ Magic numbers to help the loader
  109. .word start @ absolute load/run zImage address
  110. .word _edata @ zImage end address
  111. 1: mov r7, r1 @ save architecture ID
  112. mov r8, r2 @ save atags pointer
  113. #ifndef __ARM_ARCH_2__
  114. /*
  115. * Booting from Angel - need to enter SVC mode and disable
  116. * FIQs/IRQs (numeric definitions from angel arm.h source).
  117. * We only do this if we were in user mode on entry.
  118. */
  119. mrs r2, cpsr @ get current mode
  120. tst r2, #3 @ not user?
  121. bne not_angel
  122. mov r0, #0x17 @ angel_SWIreason_EnterSVC
  123. swi 0x123456 @ angel_SWI_ARM
  124. not_angel:
  125. mrs r2, cpsr @ turn off interrupts to
  126. orr r2, r2, #0xc0 @ prevent angel from running
  127. msr cpsr_c, r2
  128. #else
  129. teqp pc, #0x0c000003 @ turn off interrupts
  130. #endif
  131. /*
  132. * Note that some cache flushing and other stuff may
  133. * be needed here - is there an Angel SWI call for this?
  134. */
  135. /*
  136. * some architecture specific code can be inserted
  137. * by the linker here, but it should preserve r7, r8, and r9.
  138. */
  139. .text
  140. adr r0, LC0
  141. ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp}
  142. subs r0, r0, r1 @ calculate the delta offset
  143. @ if delta is zero, we are
  144. beq not_relocated @ running at the address we
  145. @ were linked at.
  146. /*
  147. * We're running at a different address. We need to fix
  148. * up various pointers:
  149. * r5 - zImage base address
  150. * r6 - GOT start
  151. * ip - GOT end
  152. */
  153. add r5, r5, r0
  154. add r6, r6, r0
  155. add ip, ip, r0
  156. #ifndef CONFIG_ZBOOT_ROM
  157. /*
  158. * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
  159. * we need to fix up pointers into the BSS region.
  160. * r2 - BSS start
  161. * r3 - BSS end
  162. * sp - stack pointer
  163. */
  164. add r2, r2, r0
  165. add r3, r3, r0
  166. add sp, sp, r0
  167. /*
  168. * Relocate all entries in the GOT table.
  169. */
  170. 1: ldr r1, [r6, #0] @ relocate entries in the GOT
  171. add r1, r1, r0 @ table. This fixes up the
  172. str r1, [r6], #4 @ C references.
  173. cmp r6, ip
  174. blo 1b
  175. #else
  176. /*
  177. * Relocate entries in the GOT table. We only relocate
  178. * the entries that are outside the (relocated) BSS region.
  179. */
  180. 1: ldr r1, [r6, #0] @ relocate entries in the GOT
  181. cmp r1, r2 @ entry < bss_start ||
  182. cmphs r3, r1 @ _end < entry
  183. addlo r1, r1, r0 @ table. This fixes up the
  184. str r1, [r6], #4 @ C references.
  185. cmp r6, ip
  186. blo 1b
  187. #endif
  188. not_relocated: mov r0, #0
  189. 1: str r0, [r2], #4 @ clear bss
  190. str r0, [r2], #4
  191. str r0, [r2], #4
  192. str r0, [r2], #4
  193. cmp r2, r3
  194. blo 1b
  195. /*
  196. * The C runtime environment should now be setup
  197. * sufficiently. Turn the cache on, set up some
  198. * pointers, and start decompressing.
  199. */
  200. bl cache_on
  201. mov r1, sp @ malloc space above stack
  202. add r2, sp, #0x10000 @ 64k max
  203. /*
  204. * Check to see if we will overwrite ourselves.
  205. * r4 = final kernel address
  206. * r5 = start of this image
  207. * r2 = end of malloc space (and therefore this image)
  208. * We basically want:
  209. * r4 >= r2 -> OK
  210. * r4 + image length <= r5 -> OK
  211. */
  212. cmp r4, r2
  213. bhs wont_overwrite
  214. sub r3, sp, r5 @ > compressed kernel size
  215. add r0, r4, r3, lsl #2 @ allow for 4x expansion
  216. cmp r0, r5
  217. bls wont_overwrite
  218. mov r5, r2 @ decompress after malloc space
  219. mov r0, r5
  220. mov r3, r7
  221. bl decompress_kernel
  222. add r0, r0, #127 + 128 @ alignment + stack
  223. bic r0, r0, #127 @ align the kernel length
  224. /*
  225. * r0 = decompressed kernel length
  226. * r1-r3 = unused
  227. * r4 = kernel execution address
  228. * r5 = decompressed kernel start
  229. * r6 = processor ID
  230. * r7 = architecture ID
  231. * r8 = atags pointer
  232. * r9-r14 = corrupted
  233. */
  234. add r1, r5, r0 @ end of decompressed kernel
  235. adr r2, reloc_start
  236. ldr r3, LC1
  237. add r3, r2, r3
  238. 1: ldmia r2!, {r9 - r14} @ copy relocation code
  239. stmia r1!, {r9 - r14}
  240. ldmia r2!, {r9 - r14}
  241. stmia r1!, {r9 - r14}
  242. cmp r2, r3
  243. blo 1b
  244. add sp, r1, #128 @ relocate the stack
  245. bl cache_clean_flush
  246. add pc, r5, r0 @ call relocation code
  247. /*
  248. * We're not in danger of overwriting ourselves. Do this the simple way.
  249. *
  250. * r4 = kernel execution address
  251. * r7 = architecture ID
  252. */
  253. wont_overwrite: mov r0, r4
  254. mov r3, r7
  255. bl decompress_kernel
  256. b call_kernel
  257. .type LC0, #object
  258. LC0: .word LC0 @ r1
  259. .word __bss_start @ r2
  260. .word _end @ r3
  261. .word zreladdr @ r4
  262. .word _start @ r5
  263. .word _got_start @ r6
  264. .word _got_end @ ip
  265. .word user_stack+4096 @ sp
  266. LC1: .word reloc_end - reloc_start
  267. .size LC0, . - LC0
  268. #ifdef CONFIG_ARCH_RPC
  269. .globl params
  270. params: ldr r0, =params_phys
  271. mov pc, lr
  272. .ltorg
  273. .align
  274. #endif
  275. /*
  276. * Turn on the cache. We need to setup some page tables so that we
  277. * can have both the I and D caches on.
  278. *
  279. * We place the page tables 16k down from the kernel execution address,
  280. * and we hope that nothing else is using it. If we're using it, we
  281. * will go pop!
  282. *
  283. * On entry,
  284. * r4 = kernel execution address
  285. * r6 = processor ID
  286. * r7 = architecture number
  287. * r8 = atags pointer
  288. * r9 = run-time address of "start" (???)
  289. * On exit,
  290. * r1, r2, r3, r9, r10, r12 corrupted
  291. * This routine must preserve:
  292. * r4, r5, r6, r7, r8
  293. */
  294. .align 5
  295. cache_on: mov r3, #8 @ cache_on function
  296. b call_cache_fn
  297. /*
  298. * Initialize the highest priority protection region, PR7
  299. * to cover all 32bit address and cacheable and bufferable.
  300. */
  301. __armv4_mpu_cache_on:
  302. mov r0, #0x3f @ 4G, the whole
  303. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  304. mcr p15, 0, r0, c6, c7, 1
  305. mov r0, #0x80 @ PR7
  306. mcr p15, 0, r0, c2, c0, 0 @ D-cache on
  307. mcr p15, 0, r0, c2, c0, 1 @ I-cache on
  308. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  309. mov r0, #0xc000
  310. mcr p15, 0, r0, c5, c0, 1 @ I-access permission
  311. mcr p15, 0, r0, c5, c0, 0 @ D-access permission
  312. mov r0, #0
  313. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  314. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  315. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  316. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  317. @ ...I .... ..D. WC.M
  318. orr r0, r0, #0x002d @ .... .... ..1. 11.1
  319. orr r0, r0, #0x1000 @ ...1 .... .... ....
  320. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  321. mov r0, #0
  322. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  323. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  324. mov pc, lr
  325. __armv3_mpu_cache_on:
  326. mov r0, #0x3f @ 4G, the whole
  327. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  328. mov r0, #0x80 @ PR7
  329. mcr p15, 0, r0, c2, c0, 0 @ cache on
  330. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  331. mov r0, #0xc000
  332. mcr p15, 0, r0, c5, c0, 0 @ access permission
  333. mov r0, #0
  334. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  335. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  336. @ .... .... .... WC.M
  337. orr r0, r0, #0x000d @ .... .... .... 11.1
  338. mov r0, #0
  339. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  340. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  341. mov pc, lr
  342. __setup_mmu: sub r3, r4, #16384 @ Page directory size
  343. bic r3, r3, #0xff @ Align the pointer
  344. bic r3, r3, #0x3f00
  345. /*
  346. * Initialise the page tables, turning on the cacheable and bufferable
  347. * bits for the RAM area only.
  348. */
  349. mov r0, r3
  350. mov r9, r0, lsr #18
  351. mov r9, r9, lsl #18 @ start of RAM
  352. add r10, r9, #0x10000000 @ a reasonable RAM size
  353. mov r1, #0x12
  354. orr r1, r1, #3 << 10
  355. add r2, r3, #16384
  356. 1: cmp r1, r9 @ if virt > start of RAM
  357. orrhs r1, r1, #0x0c @ set cacheable, bufferable
  358. cmp r1, r10 @ if virt > end of RAM
  359. bichs r1, r1, #0x0c @ clear cacheable, bufferable
  360. str r1, [r0], #4 @ 1:1 mapping
  361. add r1, r1, #1048576
  362. teq r0, r2
  363. bne 1b
  364. /*
  365. * If ever we are running from Flash, then we surely want the cache
  366. * to be enabled also for our execution instance... We map 2MB of it
  367. * so there is no map overlap problem for up to 1 MB compressed kernel.
  368. * If the execution is in RAM then we would only be duplicating the above.
  369. */
  370. mov r1, #0x1e
  371. orr r1, r1, #3 << 10
  372. mov r2, pc, lsr #20
  373. orr r1, r1, r2, lsl #20
  374. add r0, r3, r2, lsl #2
  375. str r1, [r0], #4
  376. add r1, r1, #1048576
  377. str r1, [r0]
  378. mov pc, lr
  379. __armv4_mmu_cache_on:
  380. mov r12, lr
  381. bl __setup_mmu
  382. mov r0, #0
  383. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  384. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  385. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  386. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  387. orr r0, r0, #0x0030
  388. bl __common_mmu_cache_on
  389. mov r0, #0
  390. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  391. mov pc, r12
  392. __arm6_mmu_cache_on:
  393. mov r12, lr
  394. bl __setup_mmu
  395. mov r0, #0
  396. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  397. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  398. mov r0, #0x30
  399. bl __common_mmu_cache_on
  400. mov r0, #0
  401. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  402. mov pc, r12
  403. __common_mmu_cache_on:
  404. #ifndef DEBUG
  405. orr r0, r0, #0x000d @ Write buffer, mmu
  406. #endif
  407. mov r1, #-1
  408. mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
  409. mcr p15, 0, r1, c3, c0, 0 @ load domain access control
  410. b 1f
  411. .align 5 @ cache line aligned
  412. 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
  413. mrc p15, 0, r0, c1, c0, 0 @ and read it back to
  414. sub pc, lr, r0, lsr #32 @ properly flush pipeline
  415. /*
  416. * All code following this line is relocatable. It is relocated by
  417. * the above code to the end of the decompressed kernel image and
  418. * executed there. During this time, we have no stacks.
  419. *
  420. * r0 = decompressed kernel length
  421. * r1-r3 = unused
  422. * r4 = kernel execution address
  423. * r5 = decompressed kernel start
  424. * r6 = processor ID
  425. * r7 = architecture ID
  426. * r8 = atags pointer
  427. * r9-r14 = corrupted
  428. */
  429. .align 5
  430. reloc_start: add r9, r5, r0
  431. sub r9, r9, #128 @ do not copy the stack
  432. debug_reloc_start
  433. mov r1, r4
  434. 1:
  435. .rept 4
  436. ldmia r5!, {r0, r2, r3, r10 - r14} @ relocate kernel
  437. stmia r1!, {r0, r2, r3, r10 - r14}
  438. .endr
  439. cmp r5, r9
  440. blo 1b
  441. add sp, r1, #128 @ relocate the stack
  442. debug_reloc_end
  443. call_kernel: bl cache_clean_flush
  444. bl cache_off
  445. mov r0, #0 @ must be zero
  446. mov r1, r7 @ restore architecture number
  447. mov r2, r8 @ restore atags pointer
  448. mov pc, r4 @ call kernel
  449. /*
  450. * Here follow the relocatable cache support functions for the
  451. * various processors. This is a generic hook for locating an
  452. * entry and jumping to an instruction at the specified offset
  453. * from the start of the block. Please note this is all position
  454. * independent code.
  455. *
  456. * r1 = corrupted
  457. * r2 = corrupted
  458. * r3 = block offset
  459. * r6 = corrupted
  460. * r12 = corrupted
  461. */
  462. call_cache_fn: adr r12, proc_types
  463. #ifdef CONFIG_CPU_CP15
  464. mrc p15, 0, r6, c0, c0 @ get processor ID
  465. #else
  466. ldr r6, =CONFIG_PROCESSOR_ID
  467. #endif
  468. 1: ldr r1, [r12, #0] @ get value
  469. ldr r2, [r12, #4] @ get mask
  470. eor r1, r1, r6 @ (real ^ match)
  471. tst r1, r2 @ & mask
  472. addeq pc, r12, r3 @ call cache function
  473. add r12, r12, #4*5
  474. b 1b
  475. /*
  476. * Table for cache operations. This is basically:
  477. * - CPU ID match
  478. * - CPU ID mask
  479. * - 'cache on' method instruction
  480. * - 'cache off' method instruction
  481. * - 'cache flush' method instruction
  482. *
  483. * We match an entry using: ((real_id ^ match) & mask) == 0
  484. *
  485. * Writethrough caches generally only need 'on' and 'off'
  486. * methods. Writeback caches _must_ have the flush method
  487. * defined.
  488. */
  489. .type proc_types,#object
  490. proc_types:
  491. .word 0x41560600 @ ARM6/610
  492. .word 0xffffffe0
  493. b __arm6_mmu_cache_off @ works, but slow
  494. b __arm6_mmu_cache_off
  495. mov pc, lr
  496. @ b __arm6_mmu_cache_on @ untested
  497. @ b __arm6_mmu_cache_off
  498. @ b __armv3_mmu_cache_flush
  499. .word 0x00000000 @ old ARM ID
  500. .word 0x0000f000
  501. mov pc, lr
  502. mov pc, lr
  503. mov pc, lr
  504. .word 0x41007000 @ ARM7/710
  505. .word 0xfff8fe00
  506. b __arm7_mmu_cache_off
  507. b __arm7_mmu_cache_off
  508. mov pc, lr
  509. .word 0x41807200 @ ARM720T (writethrough)
  510. .word 0xffffff00
  511. b __armv4_mmu_cache_on
  512. b __armv4_mmu_cache_off
  513. mov pc, lr
  514. .word 0x41007400 @ ARM74x
  515. .word 0xff00ff00
  516. b __armv3_mpu_cache_on
  517. b __armv3_mpu_cache_off
  518. b __armv3_mpu_cache_flush
  519. .word 0x41009400 @ ARM94x
  520. .word 0xff00ff00
  521. b __armv4_mpu_cache_on
  522. b __armv4_mpu_cache_off
  523. b __armv4_mpu_cache_flush
  524. .word 0x00007000 @ ARM7 IDs
  525. .word 0x0000f000
  526. mov pc, lr
  527. mov pc, lr
  528. mov pc, lr
  529. @ Everything from here on will be the new ID system.
  530. .word 0x4401a100 @ sa110 / sa1100
  531. .word 0xffffffe0
  532. b __armv4_mmu_cache_on
  533. b __armv4_mmu_cache_off
  534. b __armv4_mmu_cache_flush
  535. .word 0x6901b110 @ sa1110
  536. .word 0xfffffff0
  537. b __armv4_mmu_cache_on
  538. b __armv4_mmu_cache_off
  539. b __armv4_mmu_cache_flush
  540. @ These match on the architecture ID
  541. .word 0x00020000 @ ARMv4T
  542. .word 0x000f0000
  543. b __armv4_mmu_cache_on
  544. b __armv4_mmu_cache_off
  545. b __armv4_mmu_cache_flush
  546. .word 0x00050000 @ ARMv5TE
  547. .word 0x000f0000
  548. b __armv4_mmu_cache_on
  549. b __armv4_mmu_cache_off
  550. b __armv4_mmu_cache_flush
  551. .word 0x00060000 @ ARMv5TEJ
  552. .word 0x000f0000
  553. b __armv4_mmu_cache_on
  554. b __armv4_mmu_cache_off
  555. b __armv4_mmu_cache_flush
  556. .word 0x0007b000 @ ARMv6
  557. .word 0x0007f000
  558. b __armv4_mmu_cache_on
  559. b __armv4_mmu_cache_off
  560. b __armv6_mmu_cache_flush
  561. .word 0 @ unrecognised type
  562. .word 0
  563. mov pc, lr
  564. mov pc, lr
  565. mov pc, lr
  566. .size proc_types, . - proc_types
  567. /*
  568. * Turn off the Cache and MMU. ARMv3 does not support
  569. * reading the control register, but ARMv4 does.
  570. *
  571. * On entry, r6 = processor ID
  572. * On exit, r0, r1, r2, r3, r12 corrupted
  573. * This routine must preserve: r4, r6, r7
  574. */
  575. .align 5
  576. cache_off: mov r3, #12 @ cache_off function
  577. b call_cache_fn
  578. __armv4_mpu_cache_off:
  579. mrc p15, 0, r0, c1, c0
  580. bic r0, r0, #0x000d
  581. mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
  582. mov r0, #0
  583. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  584. mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
  585. mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
  586. mov pc, lr
  587. __armv3_mpu_cache_off:
  588. mrc p15, 0, r0, c1, c0
  589. bic r0, r0, #0x000d
  590. mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
  591. mov r0, #0
  592. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  593. mov pc, lr
  594. __armv4_mmu_cache_off:
  595. mrc p15, 0, r0, c1, c0
  596. bic r0, r0, #0x000d
  597. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  598. mov r0, #0
  599. mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
  600. mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
  601. mov pc, lr
  602. __arm6_mmu_cache_off:
  603. mov r0, #0x00000030 @ ARM6 control reg.
  604. b __armv3_mmu_cache_off
  605. __arm7_mmu_cache_off:
  606. mov r0, #0x00000070 @ ARM7 control reg.
  607. b __armv3_mmu_cache_off
  608. __armv3_mmu_cache_off:
  609. mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
  610. mov r0, #0
  611. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  612. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  613. mov pc, lr
  614. /*
  615. * Clean and flush the cache to maintain consistency.
  616. *
  617. * On entry,
  618. * r6 = processor ID
  619. * On exit,
  620. * r1, r2, r3, r11, r12 corrupted
  621. * This routine must preserve:
  622. * r0, r4, r5, r6, r7
  623. */
  624. .align 5
  625. cache_clean_flush:
  626. mov r3, #16
  627. b call_cache_fn
  628. __armv4_mpu_cache_flush:
  629. mov r2, #1
  630. mov r3, #0
  631. mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
  632. mov r1, #7 << 5 @ 8 segments
  633. 1: orr r3, r1, #63 << 26 @ 64 entries
  634. 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
  635. subs r3, r3, #1 << 26
  636. bcs 2b @ entries 63 to 0
  637. subs r1, r1, #1 << 5
  638. bcs 1b @ segments 7 to 0
  639. teq r2, #0
  640. mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
  641. mcr p15, 0, ip, c7, c10, 4 @ drain WB
  642. mov pc, lr
  643. __armv6_mmu_cache_flush:
  644. mov r1, #0
  645. mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
  646. mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
  647. mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
  648. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  649. mov pc, lr
  650. __armv4_mmu_cache_flush:
  651. mov r2, #64*1024 @ default: 32K dcache size (*2)
  652. mov r11, #32 @ default: 32 byte line size
  653. mrc p15, 0, r3, c0, c0, 1 @ read cache type
  654. teq r3, r6 @ cache ID register present?
  655. beq no_cache_id
  656. mov r1, r3, lsr #18
  657. and r1, r1, #7
  658. mov r2, #1024
  659. mov r2, r2, lsl r1 @ base dcache size *2
  660. tst r3, #1 << 14 @ test M bit
  661. addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
  662. mov r3, r3, lsr #12
  663. and r3, r3, #3
  664. mov r11, #8
  665. mov r11, r11, lsl r3 @ cache line size in bytes
  666. no_cache_id:
  667. bic r1, pc, #63 @ align to longest cache line
  668. add r2, r1, r2
  669. 1: ldr r3, [r1], r11 @ s/w flush D cache
  670. teq r1, r2
  671. bne 1b
  672. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  673. mcr p15, 0, r1, c7, c6, 0 @ flush D cache
  674. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  675. mov pc, lr
  676. __armv3_mmu_cache_flush:
  677. __armv3_mpu_cache_flush:
  678. mov r1, #0
  679. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  680. mov pc, lr
  681. /*
  682. * Various debugging routines for printing hex characters and
  683. * memory, which again must be relocatable.
  684. */
  685. #ifdef DEBUG
  686. .type phexbuf,#object
  687. phexbuf: .space 12
  688. .size phexbuf, . - phexbuf
  689. phex: adr r3, phexbuf
  690. mov r2, #0
  691. strb r2, [r3, r1]
  692. 1: subs r1, r1, #1
  693. movmi r0, r3
  694. bmi puts
  695. and r2, r0, #15
  696. mov r0, r0, lsr #4
  697. cmp r2, #10
  698. addge r2, r2, #7
  699. add r2, r2, #'0'
  700. strb r2, [r3, r1]
  701. b 1b
  702. puts: loadsp r3
  703. 1: ldrb r2, [r0], #1
  704. teq r2, #0
  705. moveq pc, lr
  706. 2: writeb r2, r3
  707. mov r1, #0x00020000
  708. 3: subs r1, r1, #1
  709. bne 3b
  710. teq r2, #'\n'
  711. moveq r2, #'\r'
  712. beq 2b
  713. teq r0, #0
  714. bne 1b
  715. mov pc, lr
  716. putc:
  717. mov r2, r0
  718. mov r0, #0
  719. loadsp r3
  720. b 2b
  721. memdump: mov r12, r0
  722. mov r10, lr
  723. mov r11, #0
  724. 2: mov r0, r11, lsl #2
  725. add r0, r0, r12
  726. mov r1, #8
  727. bl phex
  728. mov r0, #':'
  729. bl putc
  730. 1: mov r0, #' '
  731. bl putc
  732. ldr r0, [r12, r11, lsl #2]
  733. mov r1, #8
  734. bl phex
  735. and r0, r11, #7
  736. teq r0, #3
  737. moveq r0, #' '
  738. bleq putc
  739. and r0, r11, #7
  740. add r11, r11, #1
  741. teq r0, #7
  742. bne 1b
  743. mov r0, #'\n'
  744. bl putc
  745. cmp r11, #64
  746. blt 2b
  747. mov pc, r10
  748. #endif
  749. reloc_end:
  750. .align
  751. .section ".stack", "w"
  752. user_stack: .space 4096