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