2 * linux/arch/arm/boot/compressed/head.S
4 * Copyright (C) 1996-2002 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/config.h>
11 #include <linux/linkage.h>
19 * Note that these macros must not contain any code which is not
20 * 100% relocatable. Any attempt to do so will result in a crash.
21 * Please select one of the following when turning on debugging.
24 #if defined(CONFIG_DEBUG_DC21285_PORT)
31 #elif defined(CONFIG_FOOTBRIDGE)
36 strb \rb, [r3, #0x3f8]
38 #elif defined(CONFIG_ARCH_RPC)
41 orr \rb, \rb, #0x00010000
44 strb \rb, [r3, #0x3f8 << 2]
46 #elif defined(CONFIG_ARCH_INTEGRATOR)
53 #elif defined(CONFIG_ARCH_PXA) /* Xscale-type */
56 orr \rb, \rb, #0x00100000
61 #elif defined(CONFIG_ARCH_SA1100)
63 mov \rb, #0x80000000 @ physical base address
64 # if defined(CONFIG_DEBUG_LL_SER3)
65 add \rb, \rb, #0x00050000 @ Ser3
67 add \rb, \rb, #0x00010000 @ Ser1
71 str \rb, [r3, #0x14] @ UTDR
74 #error no serial architecture defined
89 .macro debug_reloc_start
92 kphex r6, 8 /* processor id */
94 kphex r7, 8 /* architecture id */
96 mrc p15, 0, r0, c1, c0
97 kphex r0, 8 /* control reg */
99 kphex r5, 8 /* decompressed kernel start */
101 kphex r8, 8 /* decompressed kernel end */
103 kphex r4, 8 /* kernel execution address */
108 .macro debug_reloc_end
110 kphex r5, 8 /* end of kernel */
113 bl memdump /* dump 256 bytes at start of kernel */
117 .section ".start", #alloc, #execinstr
119 * sort out different calling conventions
124 .type start,#function
130 .word 0x016f2818 @ Magic numbers to help the loader
131 .word start @ absolute load/run zImage address
132 .word _edata @ zImage end address
133 1: mov r7, r1 @ save architecture ID
136 #ifndef __ARM_ARCH_2__
138 * Booting from Angel - need to enter SVC mode and disable
139 * FIQs/IRQs (numeric definitions from angel arm.h source).
140 * We only do this if we were in user mode on entry.
142 mrs r2, cpsr @ get current mode
143 tst r2, #3 @ not user?
145 mov r0, #0x17 @ angel_SWIreason_EnterSVC
146 swi 0x123456 @ angel_SWI_ARM
148 mrs r2, cpsr @ turn off interrupts to
149 orr r2, r2, #0xc0 @ prevent angel from running
152 teqp pc, #0x0c000003 @ turn off interrupts
156 * Note that some cache flushing and other stuff may
157 * be needed here - is there an Angel SWI call for this?
159 #endif /* !CONFIG_LAB */
161 * some architecture specific code can be inserted
162 * by the linker here, but it should preserve r7 and r8.
173 ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp}
174 subs r0, r0, r1 @ calculate the delta offset
176 @ if delta is zero, we're
177 beq not_relocated @ running at the address we
181 * We're running at a different address. We need to fix
182 * up various pointers:
183 * r5 - zImage base address
191 #ifndef CONFIG_ZBOOT_ROM
193 * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
194 * we need to fix up pointers into the BSS region.
211 * Relocate all entries in the GOT table.
216 ldr r1, [r6, #0] @ relocate entries in the GOT
217 add r1, r1, r0 @ table. This fixes up the
218 str r1, [r6], #4 @ C references.
225 * Relocate entries in the GOT table. We only relocate
226 * the entries that are outside the (relocated) BSS region.
228 1: ldr r1, [r6, #0] @ relocate entries in the GOT
229 cmp r1, r2 @ entry < bss_start ||
230 cmphs r3, r1 @ _end < entry
231 addlo r1, r1, r0 @ table. This fixes up the
232 str r1, [r6], #4 @ C references.
237 not_relocated: mov r0, #0
238 1: str r0, [r2], #4 @ clear bss
246 kputc #'C' @ say that BSS is clear
250 * The C runtime environment should now be setup
251 * sufficiently. Turn the cache on, set up some
252 * pointers, and start decompressing.
256 mov r1, sp @ malloc space above stack
257 add r2, sp, #0x10000 @ 64k max
260 * Check to see if we will overwrite ourselves.
261 * r4 = final kernel address
262 * r5 = start of this image
263 * r2 = end of malloc space (and therefore this image)
266 * r4 + image length <= r5 -> OK
270 add r0, r4, #4096*1024 @ 4MB largest kernel size
274 mov r5, r2 @ decompress after malloc space
280 bic r0, r0, #127 @ align the kernel length
284 * r0 = decompressed kernel length
286 * r4 = kernel execution address
287 * r5 = decompressed kernel start
289 * r7 = architecture ID
292 add r1, r5, r0 @ end of decompressed kernel
296 1: ldmia r2!, {r8 - r13} @ copy relocation code
297 stmia r1!, {r8 - r13}
298 ldmia r2!, {r8 - r13}
299 stmia r1!, {r8 - r13}
304 add pc, r5, r0 @ call relocation code
307 * We're not in danger of overwriting ourselves. Do this the simple way.
309 * r4 = kernel execution address
310 * r7 = architecture ID
312 wont_overwrite: mov r0, r4
319 .word __bss_start @ r2
321 .word _load_addr @ r4
323 .word _got_start @ r6
325 .word user_stack+4096 @ sp
326 LC1: .word reloc_end - reloc_start
330 * Turn on the cache. We need to setup some page tables so that we
331 * can have both the I and D caches on.
333 * We place the page tables 16k down from the kernel execution address,
334 * and we hope that nothing else is using it. If we're using it, we
338 * r4 = kernel execution address
340 * r7 = architecture number
341 * r8 = run-time address of "start"
343 * r1, r2, r3, r8, r9, r12 corrupted
344 * This routine must preserve:
348 cache_on: mov r3, #8 @ cache_on function
351 __setup_mmu: sub r3, r4, #16384 @ Page directory size
352 bic r3, r3, #0xff @ Align the pointer
355 * Initialise the page tables, turning on the cacheable and bufferable
356 * bits for the RAM area only.
360 mov r8, r8, lsl #18 @ start of RAM
361 add r9, r8, #0x10000000 @ a reasonable RAM size
365 1: cmp r1, r8 @ if virt > start of RAM
366 orrhs r1, r1, #0x0c @ set cacheable, bufferable
367 cmp r1, r9 @ if virt > end of RAM
368 bichs r1, r1, #0x0c @ clear cacheable, bufferable
369 str r1, [r0], #4 @ 1:1 mapping
374 * If ever we are running from Flash, then we surely want the cache
375 * to be enabled also for our execution instance... We map 2MB of it
376 * so there is no map overlap problem for up to 1 MB compressed kernel.
377 * If the execution is in RAM then we would only be duplicating the above.
382 orr r1, r1, r2, lsl #20
383 add r0, r3, r2, lsl #2
393 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
394 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
395 mrc p15, 0, r0, c1, c0, 0 @ read control reg
396 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
400 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
407 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
408 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
412 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
417 orr r0, r0, #0x000d @ Write buffer, mmu
420 mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
421 mcr p15, 0, r1, c3, c0, 0 @ load domain access control
422 mcr p15, 0, r0, c1, c0, 0 @ load control register
426 * All code following this line is relocatable. It is relocated by
427 * the above code to the end of the decompressed kernel image and
428 * executed there. During this time, we have no stacks.
430 * r0 = decompressed kernel length
432 * r4 = kernel execution address
433 * r5 = decompressed kernel start
435 * r7 = architecture ID
439 reloc_start: add r8, r5, r0
444 ldmia r5!, {r0, r2, r3, r9 - r13} @ relocate kernel
445 stmia r1!, {r0, r2, r3, r9 - r13}
452 call_kernel: bl cache_clean_flush
456 mov r1, r7 @ restore architecture number
457 mov pc, r4 @ call kernel
460 * Here follow the relocatable cache support functions for the
461 * various processors. This is a generic hook for locating an
462 * entry and jumping to an instruction at the specified offset
463 * from the start of the block. Please note this is all position
473 call_cache_fn: adr r12, proc_types
474 mrc p15, 0, r6, c0, c0 @ get processor ID
475 1: ldr r1, [r12, #0] @ get value
476 ldr r2, [r12, #4] @ get mask
477 eor r1, r1, r6 @ (real ^ match)
479 addeq pc, r12, r3 @ call cache function
484 * Table for cache operations. This is basically:
487 * - 'cache on' method instruction
488 * - 'cache off' method instruction
489 * - 'cache flush' method instruction
491 * We match an entry using: ((real_id ^ match) & mask) == 0
493 * Writethrough caches generally only need 'on' and 'off'
494 * methods. Writeback caches _must_ have the flush method
497 .type proc_types,#object
499 .word 0x41560600 @ ARM6/610
501 b __arm6_cache_off @ works, but slow
504 @ b __arm6_cache_on @ untested
506 @ b __armv3_cache_flush
508 .word 0x00000000 @ old ARM ID
514 .word 0x41007000 @ ARM7/710
520 .word 0x41807200 @ ARM720T (writethrough)
526 .word 0x00007000 @ ARM7 IDs
532 @ Everything from here on will be the new ID system.
534 .word 0x4401a100 @ sa110 / sa1100
538 b __armv4_cache_flush
540 .word 0x6901b110 @ sa1110
544 b __armv4_cache_flush
546 @ These match on the architecture ID
548 .word 0x00020000 @ ARMv4T
552 b __armv4_cache_flush
554 .word 0x00050000 @ ARMv5TE
558 b __armv4_cache_flush
560 .word 0x00060000 @ ARMv5TEJ
564 b __armv4_cache_flush
566 .word 0 @ unrecognised type
572 .size proc_types, . - proc_types
575 * Turn off the Cache and MMU. ARMv3 does not support
576 * reading the control register, but ARMv4 does.
578 * On entry, r6 = processor ID
579 * On exit, r0, r1, r2, r3, r12 corrupted
580 * This routine must preserve: r4, r6, r7
583 cache_off: mov r3, #12 @ cache_off function
587 mrc p15, 0, r0, c1, c0
589 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
591 mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
592 mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
596 mov r0, #0x00000030 @ ARM6 control reg.
600 mov r0, #0x00000070 @ ARM7 control reg.
604 mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
606 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
607 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
611 * Clean and flush the cache to maintain consistency.
616 * r1, r2, r3, r11, r12 corrupted
617 * This routine must preserve:
626 mov r2, #64*1024 @ default: 32K dcache size (*2)
627 mov r11, #32 @ default: 32 byte line size
628 mrc p15, 0, r3, c0, c0, 1 @ read cache type
629 teq r3, r6 @ cache ID register present?
634 mov r2, r2, lsl r1 @ base dcache size *2
635 tst r3, #1 << 14 @ test M bit
636 addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
640 mov r11, r11, lsl r3 @ cache line size in bytes
642 bic r1, pc, #63 @ align to longest cache line
644 1: ldr r3, [r1], r11 @ s/w flush D cache
648 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
649 mcr p15, 0, r1, c7, c6, 0 @ flush D cache
650 mcr p15, 0, r1, c7, c10, 4 @ drain WB
655 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
659 * Various debugging routines for printing hex characters and
660 * memory, which again must be relocatable.
663 .type phexbuf,#object
665 .size phexbuf, . - phexbuf
667 phex: adr r3, phexbuf
670 strb r2, [r3, r1] @ store a null at the appropriate place
671 1: subs r1, r1, #1 @ in the red yet?
672 movmi r0, r3 @ yeah? get our address
673 bmi puts @ and pass it to puts, have a nice day
674 and r2, r0, #15 @ no? not done? get the low nybble
675 mov r0, r0, lsr #4 @ shift
676 cmp r2, #10 @ add hexidecimalness
677 addge r2, r2, #7 @ ""
678 add r2, r2, #'0' @ ""
679 strb r2, [r3, r1] @ store it
680 b 1b @ go loop around again
702 2: mrc p14, 0, r15, c14, c0, 0
704 mcr p14, 0, r2, c8, c0, 0
724 2: mov r0, r11, lsl #2
732 ldr r0, [r12, r11, lsl #2]
753 .section ".stack", "w"
754 user_stack: .space 4096