head.S 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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_S3C_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. __armv7_mmu_cache_on:
  393. mov r12, lr
  394. mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
  395. tst r11, #0xf @ VMSA
  396. blne __setup_mmu
  397. mov r0, #0
  398. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  399. tst r11, #0xf @ VMSA
  400. mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  401. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  402. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  403. orr r0, r0, #0x003c @ write buffer
  404. orrne r0, r0, #1 @ MMU enabled
  405. movne r1, #-1
  406. mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
  407. mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
  408. mcr p15, 0, r0, c1, c0, 0 @ load control register
  409. mrc p15, 0, r0, c1, c0, 0 @ and read it back
  410. mov r0, #0
  411. mcr p15, 0, r0, c7, c5, 4 @ ISB
  412. mov pc, r12
  413. __arm6_mmu_cache_on:
  414. mov r12, lr
  415. bl __setup_mmu
  416. mov r0, #0
  417. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  418. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  419. mov r0, #0x30
  420. bl __common_mmu_cache_on
  421. mov r0, #0
  422. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  423. mov pc, r12
  424. __common_mmu_cache_on:
  425. #ifndef DEBUG
  426. orr r0, r0, #0x000d @ Write buffer, mmu
  427. #endif
  428. mov r1, #-1
  429. mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
  430. mcr p15, 0, r1, c3, c0, 0 @ load domain access control
  431. b 1f
  432. .align 5 @ cache line aligned
  433. 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
  434. mrc p15, 0, r0, c1, c0, 0 @ and read it back to
  435. sub pc, lr, r0, lsr #32 @ properly flush pipeline
  436. /*
  437. * All code following this line is relocatable. It is relocated by
  438. * the above code to the end of the decompressed kernel image and
  439. * executed there. During this time, we have no stacks.
  440. *
  441. * r0 = decompressed kernel length
  442. * r1-r3 = unused
  443. * r4 = kernel execution address
  444. * r5 = decompressed kernel start
  445. * r6 = processor ID
  446. * r7 = architecture ID
  447. * r8 = atags pointer
  448. * r9-r14 = corrupted
  449. */
  450. .align 5
  451. reloc_start: add r9, r5, r0
  452. sub r9, r9, #128 @ do not copy the stack
  453. debug_reloc_start
  454. mov r1, r4
  455. 1:
  456. .rept 4
  457. ldmia r5!, {r0, r2, r3, r10 - r14} @ relocate kernel
  458. stmia r1!, {r0, r2, r3, r10 - r14}
  459. .endr
  460. cmp r5, r9
  461. blo 1b
  462. add sp, r1, #128 @ relocate the stack
  463. debug_reloc_end
  464. call_kernel: bl cache_clean_flush
  465. bl cache_off
  466. mov r0, #0 @ must be zero
  467. mov r1, r7 @ restore architecture number
  468. mov r2, r8 @ restore atags pointer
  469. mov pc, r4 @ call kernel
  470. /*
  471. * Here follow the relocatable cache support functions for the
  472. * various processors. This is a generic hook for locating an
  473. * entry and jumping to an instruction at the specified offset
  474. * from the start of the block. Please note this is all position
  475. * independent code.
  476. *
  477. * r1 = corrupted
  478. * r2 = corrupted
  479. * r3 = block offset
  480. * r6 = corrupted
  481. * r12 = corrupted
  482. */
  483. call_cache_fn: adr r12, proc_types
  484. #ifdef CONFIG_CPU_CP15
  485. mrc p15, 0, r6, c0, c0 @ get processor ID
  486. #else
  487. ldr r6, =CONFIG_PROCESSOR_ID
  488. #endif
  489. 1: ldr r1, [r12, #0] @ get value
  490. ldr r2, [r12, #4] @ get mask
  491. eor r1, r1, r6 @ (real ^ match)
  492. tst r1, r2 @ & mask
  493. addeq pc, r12, r3 @ call cache function
  494. add r12, r12, #4*5
  495. b 1b
  496. /*
  497. * Table for cache operations. This is basically:
  498. * - CPU ID match
  499. * - CPU ID mask
  500. * - 'cache on' method instruction
  501. * - 'cache off' method instruction
  502. * - 'cache flush' method instruction
  503. *
  504. * We match an entry using: ((real_id ^ match) & mask) == 0
  505. *
  506. * Writethrough caches generally only need 'on' and 'off'
  507. * methods. Writeback caches _must_ have the flush method
  508. * defined.
  509. */
  510. .type proc_types,#object
  511. proc_types:
  512. .word 0x41560600 @ ARM6/610
  513. .word 0xffffffe0
  514. b __arm6_mmu_cache_off @ works, but slow
  515. b __arm6_mmu_cache_off
  516. mov pc, lr
  517. @ b __arm6_mmu_cache_on @ untested
  518. @ b __arm6_mmu_cache_off
  519. @ b __armv3_mmu_cache_flush
  520. .word 0x00000000 @ old ARM ID
  521. .word 0x0000f000
  522. mov pc, lr
  523. mov pc, lr
  524. mov pc, lr
  525. .word 0x41007000 @ ARM7/710
  526. .word 0xfff8fe00
  527. b __arm7_mmu_cache_off
  528. b __arm7_mmu_cache_off
  529. mov pc, lr
  530. .word 0x41807200 @ ARM720T (writethrough)
  531. .word 0xffffff00
  532. b __armv4_mmu_cache_on
  533. b __armv4_mmu_cache_off
  534. mov pc, lr
  535. .word 0x41007400 @ ARM74x
  536. .word 0xff00ff00
  537. b __armv3_mpu_cache_on
  538. b __armv3_mpu_cache_off
  539. b __armv3_mpu_cache_flush
  540. .word 0x41009400 @ ARM94x
  541. .word 0xff00ff00
  542. b __armv4_mpu_cache_on
  543. b __armv4_mpu_cache_off
  544. b __armv4_mpu_cache_flush
  545. .word 0x00007000 @ ARM7 IDs
  546. .word 0x0000f000
  547. mov pc, lr
  548. mov pc, lr
  549. mov pc, lr
  550. @ Everything from here on will be the new ID system.
  551. .word 0x4401a100 @ sa110 / sa1100
  552. .word 0xffffffe0
  553. b __armv4_mmu_cache_on
  554. b __armv4_mmu_cache_off
  555. b __armv4_mmu_cache_flush
  556. .word 0x6901b110 @ sa1110
  557. .word 0xfffffff0
  558. b __armv4_mmu_cache_on
  559. b __armv4_mmu_cache_off
  560. b __armv4_mmu_cache_flush
  561. @ These match on the architecture ID
  562. .word 0x00020000 @ ARMv4T
  563. .word 0x000f0000
  564. b __armv4_mmu_cache_on
  565. b __armv4_mmu_cache_off
  566. b __armv4_mmu_cache_flush
  567. .word 0x00050000 @ ARMv5TE
  568. .word 0x000f0000
  569. b __armv4_mmu_cache_on
  570. b __armv4_mmu_cache_off
  571. b __armv4_mmu_cache_flush
  572. .word 0x00060000 @ ARMv5TEJ
  573. .word 0x000f0000
  574. b __armv4_mmu_cache_on
  575. b __armv4_mmu_cache_off
  576. b __armv4_mmu_cache_flush
  577. .word 0x0007b000 @ ARMv6
  578. .word 0x000ff000
  579. b __armv4_mmu_cache_on
  580. b __armv4_mmu_cache_off
  581. b __armv6_mmu_cache_flush
  582. .word 0x000f0000 @ new CPU Id
  583. .word 0x000f0000
  584. b __armv7_mmu_cache_on
  585. b __armv7_mmu_cache_off
  586. b __armv7_mmu_cache_flush
  587. .word 0 @ unrecognised type
  588. .word 0
  589. mov pc, lr
  590. mov pc, lr
  591. mov pc, lr
  592. .size proc_types, . - proc_types
  593. /*
  594. * Turn off the Cache and MMU. ARMv3 does not support
  595. * reading the control register, but ARMv4 does.
  596. *
  597. * On entry, r6 = processor ID
  598. * On exit, r0, r1, r2, r3, r12 corrupted
  599. * This routine must preserve: r4, r6, r7
  600. */
  601. .align 5
  602. cache_off: mov r3, #12 @ cache_off function
  603. b call_cache_fn
  604. __armv4_mpu_cache_off:
  605. mrc p15, 0, r0, c1, c0
  606. bic r0, r0, #0x000d
  607. mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
  608. mov r0, #0
  609. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  610. mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
  611. mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
  612. mov pc, lr
  613. __armv3_mpu_cache_off:
  614. mrc p15, 0, r0, c1, c0
  615. bic r0, r0, #0x000d
  616. mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
  617. mov r0, #0
  618. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  619. mov pc, lr
  620. __armv4_mmu_cache_off:
  621. mrc p15, 0, r0, c1, c0
  622. bic r0, r0, #0x000d
  623. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  624. mov r0, #0
  625. mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
  626. mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
  627. mov pc, lr
  628. __armv7_mmu_cache_off:
  629. mrc p15, 0, r0, c1, c0
  630. bic r0, r0, #0x000d
  631. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  632. mov r12, lr
  633. bl __armv7_mmu_cache_flush
  634. mov r0, #0
  635. mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
  636. mov pc, r12
  637. __arm6_mmu_cache_off:
  638. mov r0, #0x00000030 @ ARM6 control reg.
  639. b __armv3_mmu_cache_off
  640. __arm7_mmu_cache_off:
  641. mov r0, #0x00000070 @ ARM7 control reg.
  642. b __armv3_mmu_cache_off
  643. __armv3_mmu_cache_off:
  644. mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
  645. mov r0, #0
  646. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  647. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  648. mov pc, lr
  649. /*
  650. * Clean and flush the cache to maintain consistency.
  651. *
  652. * On entry,
  653. * r6 = processor ID
  654. * On exit,
  655. * r1, r2, r3, r11, r12 corrupted
  656. * This routine must preserve:
  657. * r0, r4, r5, r6, r7
  658. */
  659. .align 5
  660. cache_clean_flush:
  661. mov r3, #16
  662. b call_cache_fn
  663. __armv4_mpu_cache_flush:
  664. mov r2, #1
  665. mov r3, #0
  666. mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
  667. mov r1, #7 << 5 @ 8 segments
  668. 1: orr r3, r1, #63 << 26 @ 64 entries
  669. 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
  670. subs r3, r3, #1 << 26
  671. bcs 2b @ entries 63 to 0
  672. subs r1, r1, #1 << 5
  673. bcs 1b @ segments 7 to 0
  674. teq r2, #0
  675. mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
  676. mcr p15, 0, ip, c7, c10, 4 @ drain WB
  677. mov pc, lr
  678. __armv6_mmu_cache_flush:
  679. mov r1, #0
  680. mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
  681. mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
  682. mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
  683. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  684. mov pc, lr
  685. __armv7_mmu_cache_flush:
  686. mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
  687. tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
  688. beq hierarchical
  689. mov r10, #0
  690. mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
  691. b iflush
  692. hierarchical:
  693. stmfd sp!, {r0-r5, r7, r9-r11}
  694. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  695. ands r3, r0, #0x7000000 @ extract loc from clidr
  696. mov r3, r3, lsr #23 @ left align loc bit field
  697. beq finished @ if loc is 0, then no need to clean
  698. mov r10, #0 @ start clean at cache level 0
  699. loop1:
  700. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  701. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  702. and r1, r1, #7 @ mask of the bits for current cache only
  703. cmp r1, #2 @ see what cache we have at this level
  704. blt skip @ skip if no cache, or just i-cache
  705. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  706. mcr p15, 0, r10, c7, c5, 4 @ isb to sych the new cssr&csidr
  707. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  708. and r2, r1, #7 @ extract the length of the cache lines
  709. add r2, r2, #4 @ add 4 (line length offset)
  710. ldr r4, =0x3ff
  711. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  712. .word 0xe16f5f14 @ clz r5, r4 - find bit position of way size increment
  713. ldr r7, =0x7fff
  714. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  715. loop2:
  716. mov r9, r4 @ create working copy of max way size
  717. loop3:
  718. orr r11, r10, r9, lsl r5 @ factor way and cache number into r11
  719. orr r11, r11, r7, lsl r2 @ factor index number into r11
  720. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  721. subs r9, r9, #1 @ decrement the way
  722. bge loop3
  723. subs r7, r7, #1 @ decrement the index
  724. bge loop2
  725. skip:
  726. add r10, r10, #2 @ increment cache number
  727. cmp r3, r10
  728. bgt loop1
  729. finished:
  730. mov r10, #0 @ swith back to cache level 0
  731. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  732. ldmfd sp!, {r0-r5, r7, r9-r11}
  733. iflush:
  734. mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
  735. mcr p15, 0, r10, c7, c10, 4 @ drain WB
  736. mov pc, lr
  737. __armv4_mmu_cache_flush:
  738. mov r2, #64*1024 @ default: 32K dcache size (*2)
  739. mov r11, #32 @ default: 32 byte line size
  740. mrc p15, 0, r3, c0, c0, 1 @ read cache type
  741. teq r3, r6 @ cache ID register present?
  742. beq no_cache_id
  743. mov r1, r3, lsr #18
  744. and r1, r1, #7
  745. mov r2, #1024
  746. mov r2, r2, lsl r1 @ base dcache size *2
  747. tst r3, #1 << 14 @ test M bit
  748. addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
  749. mov r3, r3, lsr #12
  750. and r3, r3, #3
  751. mov r11, #8
  752. mov r11, r11, lsl r3 @ cache line size in bytes
  753. no_cache_id:
  754. bic r1, pc, #63 @ align to longest cache line
  755. add r2, r1, r2
  756. 1: ldr r3, [r1], r11 @ s/w flush D cache
  757. teq r1, r2
  758. bne 1b
  759. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  760. mcr p15, 0, r1, c7, c6, 0 @ flush D cache
  761. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  762. mov pc, lr
  763. __armv3_mmu_cache_flush:
  764. __armv3_mpu_cache_flush:
  765. mov r1, #0
  766. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  767. mov pc, lr
  768. /*
  769. * Various debugging routines for printing hex characters and
  770. * memory, which again must be relocatable.
  771. */
  772. #ifdef DEBUG
  773. .type phexbuf,#object
  774. phexbuf: .space 12
  775. .size phexbuf, . - phexbuf
  776. phex: adr r3, phexbuf
  777. mov r2, #0
  778. strb r2, [r3, r1]
  779. 1: subs r1, r1, #1
  780. movmi r0, r3
  781. bmi puts
  782. and r2, r0, #15
  783. mov r0, r0, lsr #4
  784. cmp r2, #10
  785. addge r2, r2, #7
  786. add r2, r2, #'0'
  787. strb r2, [r3, r1]
  788. b 1b
  789. puts: loadsp r3
  790. 1: ldrb r2, [r0], #1
  791. teq r2, #0
  792. moveq pc, lr
  793. 2: writeb r2, r3
  794. mov r1, #0x00020000
  795. 3: subs r1, r1, #1
  796. bne 3b
  797. teq r2, #'\n'
  798. moveq r2, #'\r'
  799. beq 2b
  800. teq r0, #0
  801. bne 1b
  802. mov pc, lr
  803. putc:
  804. mov r2, r0
  805. mov r0, #0
  806. loadsp r3
  807. b 2b
  808. memdump: mov r12, r0
  809. mov r10, lr
  810. mov r11, #0
  811. 2: mov r0, r11, lsl #2
  812. add r0, r0, r12
  813. mov r1, #8
  814. bl phex
  815. mov r0, #':'
  816. bl putc
  817. 1: mov r0, #' '
  818. bl putc
  819. ldr r0, [r12, r11, lsl #2]
  820. mov r1, #8
  821. bl phex
  822. and r0, r11, #7
  823. teq r0, #3
  824. moveq r0, #' '
  825. bleq putc
  826. and r0, r11, #7
  827. add r11, r11, #1
  828. teq r0, #7
  829. bne 1b
  830. mov r0, #'\n'
  831. bl putc
  832. cmp r11, #64
  833. blt 2b
  834. mov pc, r10
  835. #endif
  836. .ltorg
  837. reloc_end:
  838. .align
  839. .section ".stack", "w"
  840. user_stack: .space 4096