viper_start.S revision 1.4 1 1.4 pooka /* $NetBSD: viper_start.S,v 1.4 2007/07/24 14:41:29 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2005 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.4 pooka * 3. The name of the company nor the name of the author may be used to
15 1.1 pooka * endorse or promote products derived from this software without specific
16 1.1 pooka * prior written permission.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <machine/asm.h>
32 1.1 pooka #include <arm/armreg.h>
33 1.1 pooka #include <arm/arm32/pmap.h>
34 1.1 pooka #include <arm/arm32/pte.h>
35 1.1 pooka
36 1.1 pooka /*
37 1.1 pooka * We start out with RAM mapped to the bottom 64MB. We are jogging
38 1.1 pooka * happily there with the MMU on. Our mission: map some important
39 1.1 pooka * bootstrap devices (such as console port) in addition to mapping
40 1.1 pooka * the physical RAM to 0xc0...
41 1.1 pooka *
42 1.1 pooka * If I try to create a mapping from scratch, something important gets
43 1.1 pooka * wiped out (never could figure out exactly what), so we do this with
44 1.1 pooka * the MMU on adding to the existing translation table.
45 1.1 pooka */
46 1.1 pooka
47 1.1 pooka #define CPWAIT_BRANCH \
48 1.1 pooka sub pc, pc, #4
49 1.1 pooka
50 1.1 pooka #define CPWAIT(tmp) \
51 1.1 pooka mrc p15, 0, tmp, c2, c0, 0 /* arbitrary read of CP15 */ ;\
52 1.1 pooka mov tmp, tmp /* wait for it to complete */ ;\
53 1.1 pooka CPWAIT_BRANCH /* branch to next insn */
54 1.1 pooka
55 1.1 pooka #ifndef SDRAM_START
56 1.1 pooka #define SDRAM_START 0xa0000000
57 1.1 pooka #endif
58 1.3 pooka #define XSCALE_DCACHE_SIZE 0x8000
59 1.1 pooka
60 1.1 pooka .text
61 1.1 pooka
62 1.1 pooka .global _C_LABEL(viper_start)
63 1.1 pooka _C_LABEL(viper_start):
64 1.1 pooka
65 1.1 pooka /* Figure out where we want to jump to when the time comes */
66 1.1 pooka adr r8, .Linva
67 1.1 pooka ldr r8, [r8]
68 1.1 pooka
69 1.1 pooka /*
70 1.1 pooka * Start playing with the virtual address space mapping
71 1.1 pooka * for initial bootstrap.
72 1.1 pooka *
73 1.1 pooka * Load registers, which will remain constant throughout
74 1.1 pooka * building the VA mapping.
75 1.1 pooka */
76 1.1 pooka mov r2, #(L1_S_SIZE) /* 1MB chunks */
77 1.1 pooka
78 1.1 pooka /*
79 1.1 pooka * First map SDRAM VA == PA. This enables us to cut&waste
80 1.1 pooka * some existing initarm() code without modification
81 1.1 pooka * (and, if, god forbid, someone would like to unify them
82 1.1 pooka * some day, this'll make that job easier)
83 1.1 pooka */
84 1.1 pooka mrc p15, 0, r0, c2, c0, 0 /* Get L1 */
85 1.1 pooka bic r0, r0, #0xff000000
86 1.1 pooka add r0, r0, #(0xa00 * 4) /* offset to 0xa0.. */
87 1.1 pooka
88 1.1 pooka mov r3, #SDRAM_START /* map to 0xa00.. */
89 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW)) /* the usual perms & stuff */
90 1.1 pooka orr r3, r3, #(L1_TYPE_S)
91 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
92 1.1 pooka mov r1, #0x40 /* 64 1MB entries */
93 1.1 pooka
94 1.1 pooka 1:
95 1.1 pooka /* and looplooploop */
96 1.1 pooka str r3, [r0], #4
97 1.1 pooka add r3, r3, r2
98 1.1 pooka subs r1, r1, #1
99 1.1 pooka bgt 1b
100 1.1 pooka
101 1.1 pooka /*
102 1.1 pooka * Map SDRAM also to VA 0xc00...
103 1.1 pooka */
104 1.1 pooka mrc p15, 0, r0, c2, c0, 0 /* Get L1 */
105 1.1 pooka bic r0, r0, #0xff000000
106 1.1 pooka add r0, r0, #(0xc00 * 4) /* start from 0xc00.. */
107 1.1 pooka
108 1.1 pooka mov r3, #SDRAM_START /* map to 0xa00.. */
109 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW)) /* the usual perms & stuff */
110 1.1 pooka orr r3, r3, #(L1_TYPE_S)
111 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
112 1.1 pooka mov r1, #0x40 /* 64 1MB entries */
113 1.1 pooka
114 1.1 pooka 1:
115 1.1 pooka /* and looplooploop */
116 1.1 pooka str r3, [r0], #4
117 1.1 pooka add r3, r3, r2
118 1.1 pooka subs r1, r1, #1
119 1.1 pooka bgt 1b
120 1.1 pooka
121 1.1 pooka /*
122 1.1 pooka * Here come the devices. Map an L1 section for each device
123 1.1 pooka * to make this easy.
124 1.1 pooka */
125 1.1 pooka
126 1.1 pooka /* INTCTL */
127 1.1 pooka mrc p15, 0, r0, c2, c0, 0 /* Get L1 */
128 1.1 pooka bic r0, r0, #0xff000000
129 1.1 pooka add r0, r0, #(0xfd0 * 4) /* offset to 0xfd000000 */
130 1.1 pooka
131 1.1 pooka mov r3, #0x40000000
132 1.1 pooka orr r3, r3, #0x00d00000
133 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW))
134 1.1 pooka orr r3, r3, #(L1_TYPE_S)
135 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
136 1.1 pooka str r3, [r0], #4
137 1.1 pooka
138 1.1 pooka /* GPIO */
139 1.1 pooka mov r3, #0x40000000
140 1.1 pooka orr r3, r3, #0x00e00000
141 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW))
142 1.1 pooka orr r3, r3, #(L1_TYPE_S)
143 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
144 1.1 pooka str r3, [r0], #4
145 1.1 pooka
146 1.1 pooka /* CLKMAN */
147 1.1 pooka mov r3, #0x41000000
148 1.1 pooka orr r3, r3, #0x00300000
149 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW))
150 1.1 pooka orr r3, r3, #(L1_TYPE_S)
151 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
152 1.1 pooka str r3, [r0], #4
153 1.1 pooka
154 1.1 pooka /* FFUART */
155 1.1 pooka mov r3, #0x40000000
156 1.1 pooka orr r3, r3, #0x00100000
157 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW))
158 1.1 pooka orr r3, r3, #(L1_TYPE_S)
159 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
160 1.1 pooka str r3, [r0], #4
161 1.1 pooka
162 1.1 pooka /* BTUART */
163 1.1 pooka mov r3, #0x40000000
164 1.1 pooka orr r3, r3, #0x00200000
165 1.1 pooka orr r3, r3, #(L1_S_AP(AP_KRW))
166 1.1 pooka orr r3, r3, #(L1_TYPE_S)
167 1.1 pooka orr r3, r3, #(L1_S_DOM(PMAP_DOMAIN_KERNEL))
168 1.1 pooka str r3, [r0], #4
169 1.1 pooka
170 1.1 pooka #if 0
171 1.1 pooka /*
172 1.1 pooka * Cache cleanup. Not needed here? Slight speedup in booting.
173 1.1 pooka */
174 1.3 pooka mov r3, #(XSCALE_DCACHE_SIZE)
175 1.1 pooka subs r3, r3, #32
176 1.1 pooka 1:
177 1.1 pooka mcr p15, 0, r3, c7, c10, 2
178 1.1 pooka subs r3, r3, #32
179 1.1 pooka bne 1b
180 1.1 pooka CPWAIT(r3)
181 1.1 pooka
182 1.1 pooka /* Drain write buffer */
183 1.1 pooka mcr p15, 0, r6, c7, c10, 4
184 1.1 pooka #endif
185 1.1 pooka
186 1.1 pooka /*
187 1.1 pooka * Make domain control go ful fart.
188 1.1 pooka * We probably could be slightly more sensible about this,
189 1.1 pooka * but it'll be replaced soon anyway, so why bother.
190 1.1 pooka */
191 1.1 pooka mov r0, #0xffffffff
192 1.1 pooka mcr p15, 0, r0, c3, c0, 0
193 1.1 pooka
194 1.1 pooka /*
195 1.1 pooka * Relocate the kernel to where we want it, not where Redboot
196 1.1 pooka * let's us load it. Don't bother jumping after this stage,
197 1.1 pooka * we'll do that soon enough anyway, and to the correct virtual
198 1.1 pooka * address space region I might add.
199 1.1 pooka */
200 1.1 pooka adr r0, _C_LABEL(viper_start) /* start copy from here */
201 1.1 pooka add r0, r0, #SDRAM_START /* offset to SDRAM mapping */
202 1.1 pooka
203 1.1 pooka ldr r1, .Lcopy_size /* copy this much (bytes) */
204 1.1 pooka add r1, r1, #3 /* prepare for roundup */
205 1.1 pooka mov r1, r1, LSR #2 /* make it words */
206 1.1 pooka
207 1.1 pooka mov r2, #SDRAM_START /* target address, */
208 1.1 pooka add r2, r2, #0x00200000 /* kernel offsets by 2megs */
209 1.1 pooka
210 1.1 pooka /* after this it's just a load-store-loop */
211 1.1 pooka 1:
212 1.1 pooka ldr r3, [r0], #4
213 1.1 pooka str r3, [r2], #4
214 1.1 pooka subs r1, r1, #1
215 1.1 pooka bgt 1b
216 1.1 pooka
217 1.1 pooka /*
218 1.1 pooka * Now let's clean the cache again to make sure everything
219 1.1 pooka * is in place.
220 1.1 pooka *
221 1.1 pooka * XXX: should this take into account the XScale cache clean bug?
222 1.1 pooka */
223 1.3 pooka mov r3, #(XSCALE_DCACHE_SIZE)
224 1.1 pooka subs r3, r3, #32
225 1.1 pooka 1:
226 1.1 pooka mcr p15, 0, r3, c7, c10, 2
227 1.1 pooka subs r3, r3, #32
228 1.1 pooka bne 1b
229 1.1 pooka CPWAIT(r3)
230 1.1 pooka
231 1.1 pooka /* Drain write buffer */
232 1.1 pooka mcr p15, 0, r6, c7, c10, 4
233 1.1 pooka
234 1.1 pooka /* Invalidate TLBs just to be sure */
235 1.1 pooka mcr p15, 0, r0, c8, c7, 0
236 1.1 pooka
237 1.1 pooka /*
238 1.1 pooka * You are standing at the gate to NetBSD. --More--
239 1.1 pooka * Unspeakable cruelty and harm lurk down there. --More--
240 1.1 pooka * Are you sure you want to enter?
241 1.1 pooka */
242 1.1 pooka mov pc, r8 /* So be it */
243 1.1 pooka
244 1.1 pooka /* symbol to use for address calculation in the right VA */
245 1.1 pooka .Linva:
246 1.1 pooka .word start
247 1.1 pooka
248 1.1 pooka /*
249 1.1 pooka * Calculate size of kernel to copy. Don't bother to copy bss,
250 1.1 pooka * although I guess the CPU could use the warmup exercise ...
251 1.1 pooka */
252 1.1 pooka .Lcopy_size:
253 1.1 pooka .word _edata - _C_LABEL(viper_start)
254