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