head.S 23 KB

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