ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / kernel / head.S
1 /*
2  *  linux/arch/arm/kernel/head-armv.S
3  *
4  *  Copyright (C) 1994-2002 Russell King
5  *
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.
9  *
10  *  Kernel startup code for all 32-bit CPUs
11  */
12 #include <linux/config.h>
13 #include <linux/linkage.h>
14 #include <linux/init.h>
15
16 #include <asm/assembler.h>
17 #include <asm/mach-types.h>
18 #include <asm/procinfo.h>
19 #include <asm/ptrace.h>
20 #include <asm/mach/arch.h>
21
22 /*
23  * We place the page tables 16K below TEXTADDR.  Therefore, we must make sure
24  * that TEXTADDR is correctly set.  Currently, we expect the least significant
25  * 16 bits to be 0x8000, but we could probably relax this restriction to
26  * TEXTADDR > PAGE_OFFSET + 0x4000
27  *
28  * Note that swapper_pg_dir is the virtual address of the page tables, and
29  * pgtbl gives us a position-independent reference to these tables.  We can
30  * do this because stext == TEXTADDR
31  *
32  * swapper_pg_dir, pgtbl and krnladr are all closely related.
33  */
34 #if (TEXTADDR & 0xffff) != 0x8000
35 #error TEXTADDR must start at 0xXXXX8000
36 #endif
37
38         .globl  swapper_pg_dir
39         .equ    swapper_pg_dir, TEXTADDR - 0x4000
40
41         .macro  pgtbl, reg
42         adr     \reg, stext
43         sub     \reg, \reg, #0x4000
44         .endm
45
46 /*
47  * Since the page table is closely related to the kernel start address, we
48  * can convert the page table base address to the base address of the section
49  * containing both.
50  */
51         .macro  krnladr, rd, pgtable
52         bic     \rd, \pgtable, #0x000ff000
53         .endm
54
55 /*
56  * Kernel startup entry point.
57  * ---------------------------
58  *
59  * This is normally called from the decompressor code.  The requirements
60  * are: MMU = off, D-cache = off, I-cache = dont care, r0 = 0,
61  * r1 = machine nr.
62  *
63  * This code is mostly position independent, so if you link the kernel at
64  * 0xc0008000, you call this at __pa(0xc0008000).
65  *
66  * See linux/arch/arm/tools/mach-types for the complete list of machine
67  * numbers for r1.
68  *
69  * We're trying to keep crap to a minimum; DO NOT add any machine specific
70  * crap here - that's what the boot loader (or in extreme, well justified
71  * circumstances, zImage) is for.
72  */
73         __INIT
74         .type   stext, #function
75 ENTRY(stext)
76         mov     r12, r0
77         mov     r0, #PSR_F_BIT | PSR_I_BIT | MODE_SVC   @ make sure svc mode
78         msr     cpsr_c, r0                      @ and all irqs disabled
79         bl      __lookup_processor_type
80         teq     r10, #0                         @ invalid processor?
81         moveq   r0, #'p'                        @ yes, error 'p'
82         beq     __error
83         bl      __lookup_architecture_type
84         teq     r7, #0                          @ invalid architecture?
85         moveq   r0, #'a'                        @ yes, error 'a'
86         beq     __error
87         bl      __create_page_tables
88
89         /*
90          * The following calls CPU specific code in a position independent
91          * manner.  See arch/arm/mm/proc-*.S for details.  r10 = base of
92          * xxx_proc_info structure selected by __lookup_architecture_type
93          * above.  On return, the CPU will be ready for the MMU to be
94          * turned on, and r0 will hold the CPU control register value.
95          */
96         adr     lr, __turn_mmu_on               @ return (PIC) address
97         add     pc, r10, #12
98
99         .type   __switch_data, %object
100 __switch_data:
101         .long   __mmap_switched
102         .long   __bss_start                     @ r4
103         .long   _end                            @ r5
104         .long   processor_id                    @ r6
105         .long   __machine_arch_type             @ r7
106         .long   cr_alignment                    @ r8
107         .long   init_thread_union+8192          @ sp
108
109 /*
110  * Enable the MMU.  This completely changes the structure of the visible
111  * memory space.  You will not be able to trace execution through this.
112  * If you have an enquiry about this, *please* check the linux-arm-kernel
113  * mailing list archives BEFORE sending another post to the list.
114  */
115         .align  5
116         .type   __turn_mmu_on, %function
117 __turn_mmu_on:
118         ldr     lr, __switch_data
119 #ifdef CONFIG_ALIGNMENT_TRAP
120         orr     r0, r0, #2                      @ ...........A.
121 #endif
122         mcr     p15, 0, r0, c1, c0, 0           @ write control reg
123         mrc     p15, 0, r3, c0, c0, 0           @ read id reg
124         mov     r3, r3
125         mov     r3, r3
126         mov     pc, lr
127
128 /*
129  * The following fragment of code is executed with the MMU on, and uses
130  * absolute addresses; this is not position independent.
131  *
132  *  r0  = processor control register
133  *  r1  = machine ID
134  *  r9  = processor ID
135  *  r12 = value of r0 when kernel was called (currently always zero)
136  */
137         .align  5
138 __mmap_switched:
139         adr     r3, __switch_data + 4
140         ldmia   r3, {r4, r5, r6, r7, r8, sp}
141         mov     fp, #0                          @ Clear BSS (and zero fp)
142 1:      cmp     r4, r5
143         strcc   fp, [r4],#4
144         bcc     1b
145         str     r9, [r6]                        @ Save processor ID
146         str     r1, [r7]                        @ Save machine type
147         bic     r2, r0, #2                      @ Clear 'A' bit
148         stmia   r8, {r0, r2}                    @ Save control register values
149         b       start_kernel
150
151
152
153
154 /*
155  * Setup the initial page tables.  We only setup the barest
156  * amount which are required to get the kernel running, which
157  * generally means mapping in the kernel code.
158  *
159  * We only map in 4MB of RAM, which should be sufficient in
160  * all cases.
161  *
162  * r5 = physical address of start of RAM
163  * r6 = physical IO address
164  * r7 = byte offset into page tables for IO
165  * r8 = page table flags
166  */
167 __create_page_tables:
168         pgtbl   r4                              @ page table address
169
170         /*
171          * Clear the 16K level 1 swapper page table
172          */
173         mov     r0, r4
174         mov     r3, #0
175         add     r2, r0, #0x4000
176 1:      str     r3, [r0], #4
177         str     r3, [r0], #4
178         str     r3, [r0], #4
179         str     r3, [r0], #4
180         teq     r0, r2
181         bne     1b
182
183         /*
184          * Create identity mapping for first MB of kernel to
185          * cater for the MMU enable.  This identity mapping
186          * will be removed by paging_init()
187          */
188         krnladr r2, r4                          @ start of kernel
189         add     r3, r8, r2                      @ flags + kernel base
190         str     r3, [r4, r2, lsr #18]           @ identity mapping
191
192         /*
193          * Now setup the pagetables for our kernel direct
194          * mapped region.  We round TEXTADDR down to the
195          * nearest megabyte boundary.
196          */
197         add     r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel
198         bic     r2, r3, #0x00f00000
199         str     r2, [r0]                        @ PAGE_OFFSET + 0MB
200         add     r0, r0, #(TEXTADDR & 0x00f00000) >> 18
201         str     r3, [r0], #4                    @ KERNEL + 0MB
202         add     r3, r3, #1 << 20
203         str     r3, [r0], #4                    @ KERNEL + 1MB
204         add     r3, r3, #1 << 20
205         str     r3, [r0], #4                    @ KERNEL + 2MB
206         add     r3, r3, #1 << 20
207         str     r3, [r0], #4                    @ KERNEL + 3MB
208
209         bic     r8, r8, #0x0c                   @ turn off cacheable
210                                                 @ and bufferable bits
211 #ifdef CONFIG_DEBUG_LL
212         /*
213          * Map in IO space for serial debugging.
214          * This allows debug messages to be output
215          * via a serial console before paging_init.
216          */
217         add     r0, r4, r7
218         rsb     r3, r7, #0x4000                 @ PTRS_PER_PGD*sizeof(long)
219         cmp     r3, #0x0800
220         addge   r2, r0, #0x0800
221         addlt   r2, r0, r3
222         orr     r3, r6, r8
223 1:      str     r3, [r0], #4
224         add     r3, r3, #1 << 20
225         teq     r0, r2
226         bne     1b
227 #if defined(CONFIG_ARCH_NETWINDER) || defined(CONFIG_ARCH_CATS)
228         /*
229          * If we're using the NetWinder, we need to map in
230          * the 16550-type serial port for the debug messages
231          */
232         teq     r1, #MACH_TYPE_NETWINDER
233         teqne   r1, #MACH_TYPE_CATS
234         bne     1f
235         add     r0, r4, #0x3fc0                 @ ff000000
236         mov     r3, #0x7c000000
237         orr     r3, r3, r8
238         str     r3, [r0], #4
239         add     r3, r3, #1 << 20
240         str     r3, [r0], #4
241 1:
242 #endif
243 #endif
244 #ifdef CONFIG_ARCH_RPC
245         /*
246          * Map in screen at 0x02000000 & SCREEN2_BASE
247          * Similar reasons here - for debug.  This is
248          * only for Acorn RiscPC architectures.
249          */
250         add     r0, r4, #0x80                   @ 02000000
251         mov     r3, #0x02000000
252         orr     r3, r3, r8
253         str     r3, [r0]
254         add     r0, r4, #0x3600                 @ d8000000
255         str     r3, [r0]
256 #endif
257         mov     pc, lr
258
259
260
261 /*
262  * Exception handling.  Something went wrong and we can't proceed.  We
263  * ought to tell the user, but since we don't have any guarantee that
264  * we're even running on the right architecture, we do virtually nothing.
265  *
266  * r0 = ascii error character:
267  *      a = invalid architecture
268  *      p = invalid processor
269  *      i = invalid calling convention
270  *
271  * Generally, only serious errors cause this.
272  */
273 __error:
274 #ifdef CONFIG_DEBUG_LL
275         mov     r8, r0                          @ preserve r0
276         adr     r0, err_str
277         bl      printascii
278         mov     r0, r8
279         bl      printch
280 #endif
281 #ifdef CONFIG_ARCH_RPC
282 /*
283  * Turn the screen red on a error - RiscPC only.
284  */
285         mov     r0, #0x02000000
286         mov     r3, #0x11
287         orr     r3, r3, r3, lsl #8
288         orr     r3, r3, r3, lsl #16
289         str     r3, [r0], #4
290         str     r3, [r0], #4
291         str     r3, [r0], #4
292         str     r3, [r0], #4
293 #endif
294 1:      mov     r0, r0
295         b       1b
296
297 #ifdef CONFIG_DEBUG_LL
298 err_str:
299         .asciz  "\nError: "
300         .align
301 #endif
302
303 /*
304  * Read processor ID register (CP#15, CR0), and look up in the linker-built
305  * supported processor list.  Note that we can't use the absolute addresses
306  * for the __proc_info lists since we aren't running with the MMU on
307  * (and therefore, we are not in the correct address space).  We have to
308  * calculate the offset.
309  *
310  * Returns:
311  *      r5, r6, r7 corrupted
312  *      r8  = page table flags
313  *      r9  = processor ID
314  *      r10 = pointer to processor structure
315  */
316 __lookup_processor_type:
317         adr     r5, 2f
318         ldmia   r5, {r7, r9, r10}
319         sub     r5, r5, r10                     @ convert addresses
320         add     r7, r7, r5                      @ to our address space
321         add     r10, r9, r5
322         mrc     p15, 0, r9, c0, c0              @ get processor id
323 1:      ldmia   r10, {r5, r6, r8}               @ value, mask, mmuflags
324         and     r6, r6, r9                      @ mask wanted bits
325         teq     r5, r6
326         moveq   pc, lr
327         add     r10, r10, #PROC_INFO_SZ         @ sizeof(proc_info_list)
328         cmp     r10, r7
329         blt     1b
330         mov     r10, #0                         @ unknown processor
331         mov     pc, lr
332
333 /*
334  * Look in include/asm-arm/procinfo.h and arch/arm/kernel/arch.[ch] for
335  * more information about the __proc_info and __arch_info structures.
336  */
337 2:      .long   __proc_info_end
338         .long   __proc_info_begin
339         .long   2b
340         .long   __arch_info_begin
341         .long   __arch_info_end
342
343 /*
344  * Lookup machine architecture in the linker-build list of architectures.
345  * Note that we can't use the absolute addresses for the __arch_info
346  * lists since we aren't running with the MMU on (and therefore, we are
347  * not in the correct address space).  We have to calculate the offset.
348  *
349  *  r1 = machine architecture number
350  * Returns:
351  *  r2, r3, r4 corrupted
352  *  r5 = physical start address of RAM
353  *  r6 = physical address of IO
354  *  r7 = byte offset into page tables for IO
355  */
356 __lookup_architecture_type:
357         adr     r4, 2b
358         ldmia   r4, {r2, r3, r5, r6, r7}        @ throw away r2, r3
359         sub     r5, r4, r5                      @ convert addresses
360         add     r4, r6, r5                      @ to our address space
361         add     r7, r7, r5
362 1:      ldr     r5, [r4]                        @ get machine type
363         teq     r5, r1                          @ matches loader number?
364         beq     2f                              @ found
365         add     r4, r4, #SIZEOF_MACHINE_DESC    @ next machine_desc
366         cmp     r4, r7
367         blt     1b
368         mov     r7, #0                          @ unknown architecture
369         mov     pc, lr
370 2:      ldmib   r4, {r5, r6, r7}                @ found, get results
371         mov     pc, lr