at91bus.c revision 1.1.2.1 1 1.1.2.1 matt /* $NetBSD: at91bus.c,v 1.1.2.1 2007/11/10 02:56:28 matt Exp $ */
2 1.1.2.1 matt
3 1.1.2.1 matt /*
4 1.1.2.1 matt * Copyright (c) 2007 Embedtronics Oy
5 1.1.2.1 matt * All rights reserved.
6 1.1.2.1 matt *
7 1.1.2.1 matt * Redistribution and use in source and binary forms, with or without
8 1.1.2.1 matt * modification, are permitted provided that the following conditions
9 1.1.2.1 matt * are met:
10 1.1.2.1 matt * 1. Redistributions of source code must retain the above copyright
11 1.1.2.1 matt * notice, this list of conditions and the following disclaimer.
12 1.1.2.1 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.1 matt * notice, this list of conditions and the following disclaimer in the
14 1.1.2.1 matt * documentation and/or other materials provided with the distribution.
15 1.1.2.1 matt * 3. All advertising materials mentioning features or use of this software
16 1.1.2.1 matt * must display the following acknowledgement:
17 1.1.2.1 matt * This product includes software developed by the NetBSD
18 1.1.2.1 matt * Foundation, Inc. and its contributors.
19 1.1.2.1 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1.2.1 matt * contributors may be used to endorse or promote products derived
21 1.1.2.1 matt * from this software without specific prior written permission.
22 1.1.2.1 matt *
23 1.1.2.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1.2.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1.2.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1.2.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1.2.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1.2.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1.2.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1.2.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1.2.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1.2.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1.2.1 matt * POSSIBILITY OF SUCH DAMAGE.
34 1.1.2.1 matt */
35 1.1.2.1 matt
36 1.1.2.1 matt #include <sys/cdefs.h>
37 1.1.2.1 matt __KERNEL_RCSID(0, "$NetBSD: at91bus.c,v 1.1.2.1 2007/11/10 02:56:28 matt Exp $");
38 1.1.2.1 matt
39 1.1.2.1 matt #include "opt_ddb.h"
40 1.1.2.1 matt #include "opt_kgdb.h"
41 1.1.2.1 matt #include "opt_pmap_debug.h"
42 1.1.2.1 matt
43 1.1.2.1 matt #include <sys/param.h>
44 1.1.2.1 matt #include <sys/device.h>
45 1.1.2.1 matt #include <sys/systm.h>
46 1.1.2.1 matt #include <sys/kernel.h>
47 1.1.2.1 matt #include <sys/exec.h>
48 1.1.2.1 matt #include <sys/proc.h>
49 1.1.2.1 matt #include <sys/msgbuf.h>
50 1.1.2.1 matt #include <sys/reboot.h>
51 1.1.2.1 matt #include <sys/termios.h>
52 1.1.2.1 matt #include <sys/ksyms.h>
53 1.1.2.1 matt
54 1.1.2.1 matt #include <machine/bootconfig.h>
55 1.1.2.1 matt #include <uvm/uvm_extern.h>
56 1.1.2.1 matt
57 1.1.2.1 matt #include <dev/cons.h>
58 1.1.2.1 matt
59 1.1.2.1 matt #include <machine/db_machdep.h>
60 1.1.2.1 matt #include <ddb/db_sym.h>
61 1.1.2.1 matt #include <ddb/db_extern.h>
62 1.1.2.1 matt
63 1.1.2.1 matt #include <machine/bus.h>
64 1.1.2.1 matt #include <machine/cpu.h>
65 1.1.2.1 matt #include <machine/frame.h>
66 1.1.2.1 matt #include <arm/undefined.h>
67 1.1.2.1 matt
68 1.1.2.1 matt #include <arm/arm32/machdep.h>
69 1.1.2.1 matt #include <arm/cpufunc.h>
70 1.1.2.1 matt
71 1.1.2.1 matt #include <arm/at91/at91var.h>
72 1.1.2.1 matt #include <arm/at91/at91busvar.h>
73 1.1.2.1 matt #include <arm/at91/at91dbgureg.h>
74 1.1.2.1 matt
75 1.1.2.1 matt //#include <dev/cons.h>
76 1.1.2.1 matt #include <sys/termios.h>
77 1.1.2.1 matt
78 1.1.2.1 matt #include "locators.h"
79 1.1.2.1 matt
80 1.1.2.1 matt /* console stuff: */
81 1.1.2.1 matt #ifndef CONSPEED
82 1.1.2.1 matt #define CONSPEED B115200
83 1.1.2.1 matt #endif
84 1.1.2.1 matt
85 1.1.2.1 matt #ifndef CONMODE
86 1.1.2.1 matt #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
87 1.1.2.1 matt #endif
88 1.1.2.1 matt
89 1.1.2.1 matt int cnspeed = CONSPEED;
90 1.1.2.1 matt int cnmode = CONMODE;
91 1.1.2.1 matt
92 1.1.2.1 matt
93 1.1.2.1 matt /* kernel mapping: */
94 1.1.2.1 matt #define KERNEL_BASE_PHYS 0x20200000
95 1.1.2.1 matt #define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00200000)
96 1.1.2.1 matt #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
97 1.1.2.1 matt #define KERNEL_VM_SIZE 0x0C000000
98 1.1.2.1 matt
99 1.1.2.1 matt
100 1.1.2.1 matt
101 1.1.2.1 matt /* Define various stack sizes in pages */
102 1.1.2.1 matt #define IRQ_STACK_SIZE 8
103 1.1.2.1 matt #define ABT_STACK_SIZE 8
104 1.1.2.1 matt #ifdef IPKDB
105 1.1.2.1 matt #define UND_STACK_SIZE 16
106 1.1.2.1 matt #else
107 1.1.2.1 matt #define UND_STACK_SIZE 8
108 1.1.2.1 matt #endif
109 1.1.2.1 matt
110 1.1.2.1 matt /* boot configuration: */
111 1.1.2.1 matt vm_offset_t physical_start;
112 1.1.2.1 matt vm_offset_t physical_freestart;
113 1.1.2.1 matt vm_offset_t physical_freeend;
114 1.1.2.1 matt vm_offset_t physical_freeend_low;
115 1.1.2.1 matt vm_offset_t physical_end;
116 1.1.2.1 matt u_int free_pages;
117 1.1.2.1 matt int physmem = 0;
118 1.1.2.1 matt
119 1.1.2.1 matt /* Physical and virtual addresses for some global pages */
120 1.1.2.1 matt pv_addr_t systempage;
121 1.1.2.1 matt pv_addr_t irqstack;
122 1.1.2.1 matt pv_addr_t undstack;
123 1.1.2.1 matt pv_addr_t abtstack;
124 1.1.2.1 matt pv_addr_t kernelstack;
125 1.1.2.1 matt
126 1.1.2.1 matt vm_offset_t msgbufphys;
127 1.1.2.1 matt
128 1.1.2.1 matt //static struct arm32_dma_range dma_ranges[4];
129 1.1.2.1 matt
130 1.1.2.1 matt extern u_int data_abort_handler_address;
131 1.1.2.1 matt extern u_int prefetch_abort_handler_address;
132 1.1.2.1 matt extern u_int undefined_handler_address;
133 1.1.2.1 matt
134 1.1.2.1 matt #ifdef PMAP_DEBUG
135 1.1.2.1 matt extern int pmap_debug_level;
136 1.1.2.1 matt #endif
137 1.1.2.1 matt
138 1.1.2.1 matt #define KERNEL_PT_SYS 0 /* L2 table for mapping vectors page */
139 1.1.2.1 matt
140 1.1.2.1 matt #define KERNEL_PT_KERNEL 1 /* L2 table for mapping kernel */
141 1.1.2.1 matt #define KERNEL_PT_KERNEL_NUM 4
142 1.1.2.1 matt /* L2 tables for mapping kernel VM */
143 1.1.2.1 matt #define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
144 1.1.2.1 matt
145 1.1.2.1 matt #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
146 1.1.2.1 matt #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
147 1.1.2.1 matt
148 1.1.2.1 matt pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
149 1.1.2.1 matt
150 1.1.2.1 matt struct user *proc0paddr;
151 1.1.2.1 matt
152 1.1.2.1 matt
153 1.1.2.1 matt /* prototypes: */
154 1.1.2.1 matt void consinit(void);
155 1.1.2.1 matt static int at91bus_match(struct device *, struct cfdata *, void *);
156 1.1.2.1 matt static void at91bus_attach(struct device *, struct device *, void *);
157 1.1.2.1 matt static int at91bus_search(struct device *, struct cfdata *,
158 1.1.2.1 matt const int *, void *);
159 1.1.2.1 matt static int at91bus_print(void *, const char *);
160 1.1.2.1 matt static int at91bus_submatch(struct device *, struct cfdata *,
161 1.1.2.1 matt const int *, void *);
162 1.1.2.1 matt
163 1.1.2.1 matt
164 1.1.2.1 matt CFATTACH_DECL(at91bus, sizeof(struct at91bus_softc), at91bus_match, at91bus_attach, NULL, NULL);
165 1.1.2.1 matt
166 1.1.2.1 matt struct at91bus_clocks at91bus_clocks = {0};
167 1.1.2.1 matt struct at91bus_softc *at91bus_sc = NULL;
168 1.1.2.1 matt
169 1.1.2.1 matt #include "opt_at91types.h"
170 1.1.2.1 matt
171 1.1.2.1 matt #ifdef AT91RM9200
172 1.1.2.1 matt #include <arm/at91/at91rm9200busvar.h>
173 1.1.2.1 matt #endif
174 1.1.2.1 matt
175 1.1.2.1 matt #ifdef AT91SAM9261
176 1.1.2.1 matt #include <arm/at91/at91sam9261busvar.h>
177 1.1.2.1 matt #endif
178 1.1.2.1 matt
179 1.1.2.1 matt static const struct {
180 1.1.2.1 matt u_int32_t cidr;
181 1.1.2.1 matt const char * name;
182 1.1.2.1 matt const struct at91bus_machdep *machdep;
183 1.1.2.1 matt } at91_types[] = {
184 1.1.2.1 matt {
185 1.1.2.1 matt DBGU_CIDR_AT91RM9200,
186 1.1.2.1 matt "AT91RM9200"
187 1.1.2.1 matt #ifdef AT91RM9200
188 1.1.2.1 matt , &at91rm9200bus
189 1.1.2.1 matt #endif
190 1.1.2.1 matt },
191 1.1.2.1 matt {
192 1.1.2.1 matt DBGU_CIDR_AT91SAM9260,
193 1.1.2.1 matt "AT91SAM9260"
194 1.1.2.1 matt },
195 1.1.2.1 matt {
196 1.1.2.1 matt DBGU_CIDR_AT91SAM9260,
197 1.1.2.1 matt "AT91SAM9261"
198 1.1.2.1 matt #ifdef AT91SAM9261
199 1.1.2.1 matt , &at91sam9261bus
200 1.1.2.1 matt #endif
201 1.1.2.1 matt },
202 1.1.2.1 matt {
203 1.1.2.1 matt DBGU_CIDR_AT91SAM9260,
204 1.1.2.1 matt "AT91SAM9263"
205 1.1.2.1 matt },
206 1.1.2.1 matt {
207 1.1.2.1 matt 0,
208 1.1.2.1 matt 0,
209 1.1.2.1 matt 0
210 1.1.2.1 matt }
211 1.1.2.1 matt };
212 1.1.2.1 matt
213 1.1.2.1 matt u_int32_t at91_chip_id;
214 1.1.2.1 matt static int at91_chip_ndx = -1;
215 1.1.2.1 matt struct at91bus_machdep at91bus_machdep = { 0 };
216 1.1.2.1 matt at91bus_tag_t at91bus_tag = 0;
217 1.1.2.1 matt
218 1.1.2.1 matt static int
219 1.1.2.1 matt match_cid(void)
220 1.1.2.1 matt {
221 1.1.2.1 matt u_int32_t cidr;
222 1.1.2.1 matt int i;
223 1.1.2.1 matt
224 1.1.2.1 matt /* get chip id */
225 1.1.2.1 matt cidr = DBGUREG(DBGU_CIDR);
226 1.1.2.1 matt at91_chip_id = cidr;
227 1.1.2.1 matt
228 1.1.2.1 matt /* do we know it? */
229 1.1.2.1 matt for (i = 0; at91_types[i].name; i++) {
230 1.1.2.1 matt if (cidr == at91_types[i].cidr)
231 1.1.2.1 matt return i;
232 1.1.2.1 matt }
233 1.1.2.1 matt
234 1.1.2.1 matt return -1;
235 1.1.2.1 matt }
236 1.1.2.1 matt
237 1.1.2.1 matt int
238 1.1.2.1 matt at91bus_init(void)
239 1.1.2.1 matt {
240 1.1.2.1 matt int i = at91_chip_ndx = match_cid();
241 1.1.2.1 matt
242 1.1.2.1 matt if (i < 0)
243 1.1.2.1 matt panic("%s: unknown chip", __FUNCTION__);
244 1.1.2.1 matt
245 1.1.2.1 matt if (!at91_types[i].machdep)
246 1.1.2.1 matt panic("%s: %s is not supported", __FUNCTION__, at91_types[i].name);
247 1.1.2.1 matt
248 1.1.2.1 matt memcpy(&at91bus_machdep, at91_types[i].machdep, sizeof(at91bus_machdep));
249 1.1.2.1 matt at91bus_tag = &at91bus_machdep;
250 1.1.2.1 matt
251 1.1.2.1 matt return 0;
252 1.1.2.1 matt }
253 1.1.2.1 matt
254 1.1.2.1 matt u_int
255 1.1.2.1 matt at91bus_setup(BootConfig *mem)
256 1.1.2.1 matt {
257 1.1.2.1 matt int loop;
258 1.1.2.1 matt int loop1;
259 1.1.2.1 matt u_int l1pagetable;
260 1.1.2.1 matt pv_addr_t kernel_l1pt;
261 1.1.2.1 matt
262 1.1.2.1 matt consinit();
263 1.1.2.1 matt
264 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
265 1.1.2.1 matt printf("\nNetBSD/AT91 booting ...\n");
266 1.1.2.1 matt #endif
267 1.1.2.1 matt
268 1.1.2.1 matt // setup the CPU / MMU / TLB functions:
269 1.1.2.1 matt if (set_cpufuncs())
270 1.1.2.1 matt panic("%s: cpu not recognized", __FUNCTION__);
271 1.1.2.1 matt
272 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
273 1.1.2.1 matt printf("%s: configuring system...\n", __FUNCTION__);
274 1.1.2.1 matt #endif
275 1.1.2.1 matt
276 1.1.2.1 matt /*
277 1.1.2.1 matt * Setup the variables that define the availability of
278 1.1.2.1 matt * physical memory.
279 1.1.2.1 matt */
280 1.1.2.1 matt physical_start = mem->dram[0].address;
281 1.1.2.1 matt physical_end = mem->dram[0].address + mem->dram[0].pages * PAGE_SIZE;
282 1.1.2.1 matt
283 1.1.2.1 matt physical_freestart = mem->dram[0].address + 0x9000ULL;
284 1.1.2.1 matt physical_freeend = KERNEL_BASE_PHYS;
285 1.1.2.1 matt physmem = (physical_end - physical_start) / PAGE_SIZE;
286 1.1.2.1 matt
287 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
288 1.1.2.1 matt printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
289 1.1.2.1 matt physical_start, physical_end - 1);
290 1.1.2.1 matt #endif
291 1.1.2.1 matt
292 1.1.2.1 matt free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
293 1.1.2.1 matt
294 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
295 1.1.2.1 matt printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
296 1.1.2.1 matt physical_freestart, free_pages, free_pages);
297 1.1.2.1 matt #endif
298 1.1.2.1 matt /* Define a macro to simplify memory allocation */
299 1.1.2.1 matt #define valloc_pages(var, np) \
300 1.1.2.1 matt alloc_pages((var).pv_pa, (np)); \
301 1.1.2.1 matt (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
302 1.1.2.1 matt
303 1.1.2.1 matt #define alloc_pages(var, np) \
304 1.1.2.1 matt physical_freeend -= ((np) * PAGE_SIZE); \
305 1.1.2.1 matt if (physical_freeend < physical_freestart) \
306 1.1.2.1 matt panic("initarm: out of memory"); \
307 1.1.2.1 matt (var) = physical_freeend; \
308 1.1.2.1 matt free_pages -= (np); \
309 1.1.2.1 matt memset((char *)(var), 0, ((np) * PAGE_SIZE));
310 1.1.2.1 matt
311 1.1.2.1 matt loop1 = 0;
312 1.1.2.1 matt kernel_l1pt.pv_pa = 0;
313 1.1.2.1 matt kernel_l1pt.pv_va = 0;
314 1.1.2.1 matt for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
315 1.1.2.1 matt /* Are we 16KB aligned for an L1 ? */
316 1.1.2.1 matt if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
317 1.1.2.1 matt && kernel_l1pt.pv_pa == 0) {
318 1.1.2.1 matt valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
319 1.1.2.1 matt } else {
320 1.1.2.1 matt valloc_pages(kernel_pt_table[loop1],
321 1.1.2.1 matt L2_TABLE_SIZE / PAGE_SIZE);
322 1.1.2.1 matt ++loop1;
323 1.1.2.1 matt }
324 1.1.2.1 matt }
325 1.1.2.1 matt
326 1.1.2.1 matt /* This should never be able to happen but better confirm that. */
327 1.1.2.1 matt if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
328 1.1.2.1 matt panic("initarm: Failed to align the kernel page directory");
329 1.1.2.1 matt
330 1.1.2.1 matt /*
331 1.1.2.1 matt * Allocate a page for the system vectors page
332 1.1.2.1 matt */
333 1.1.2.1 matt valloc_pages(systempage, 1);
334 1.1.2.1 matt systempage.pv_va = 0x00000000;
335 1.1.2.1 matt
336 1.1.2.1 matt /* Allocate stacks for all modes */
337 1.1.2.1 matt valloc_pages(irqstack, IRQ_STACK_SIZE);
338 1.1.2.1 matt valloc_pages(abtstack, ABT_STACK_SIZE);
339 1.1.2.1 matt valloc_pages(undstack, UND_STACK_SIZE);
340 1.1.2.1 matt valloc_pages(kernelstack, UPAGES);
341 1.1.2.1 matt
342 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
343 1.1.2.1 matt printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
344 1.1.2.1 matt irqstack.pv_va);
345 1.1.2.1 matt printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
346 1.1.2.1 matt abtstack.pv_va);
347 1.1.2.1 matt printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
348 1.1.2.1 matt undstack.pv_va);
349 1.1.2.1 matt printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
350 1.1.2.1 matt kernelstack.pv_va);
351 1.1.2.1 matt #endif
352 1.1.2.1 matt
353 1.1.2.1 matt alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
354 1.1.2.1 matt
355 1.1.2.1 matt /*
356 1.1.2.1 matt * Ok we have allocated physical pages for the primary kernel
357 1.1.2.1 matt * page tables. Save physical_freeend for when we give whats left
358 1.1.2.1 matt * of memory below 2Mbyte to UVM.
359 1.1.2.1 matt */
360 1.1.2.1 matt
361 1.1.2.1 matt physical_freeend_low = physical_freeend;
362 1.1.2.1 matt
363 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
364 1.1.2.1 matt printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
365 1.1.2.1 matt #endif
366 1.1.2.1 matt
367 1.1.2.1 matt /*
368 1.1.2.1 matt * Now we start construction of the L1 page table
369 1.1.2.1 matt * We start by mapping the L2 page tables into the L1.
370 1.1.2.1 matt * This means that we can replace L1 mappings later on if necessary
371 1.1.2.1 matt */
372 1.1.2.1 matt l1pagetable = kernel_l1pt.pv_pa;
373 1.1.2.1 matt
374 1.1.2.1 matt /* Map the L2 pages tables in the L1 page table */
375 1.1.2.1 matt pmap_link_l2pt(l1pagetable, 0x00000000, &kernel_pt_table[KERNEL_PT_SYS]);
376 1.1.2.1 matt for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
377 1.1.2.1 matt pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
378 1.1.2.1 matt &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
379 1.1.2.1 matt for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
380 1.1.2.1 matt pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
381 1.1.2.1 matt &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
382 1.1.2.1 matt
383 1.1.2.1 matt /* update the top of the kernel VM */
384 1.1.2.1 matt pmap_curmaxkvaddr =
385 1.1.2.1 matt KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
386 1.1.2.1 matt
387 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
388 1.1.2.1 matt printf("Mapping kernel\n");
389 1.1.2.1 matt #endif
390 1.1.2.1 matt
391 1.1.2.1 matt /* Now we fill in the L2 pagetable for the kernel static code/data */
392 1.1.2.1 matt {
393 1.1.2.1 matt extern char etext[], _end[];
394 1.1.2.1 matt size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
395 1.1.2.1 matt size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
396 1.1.2.1 matt u_int logical;
397 1.1.2.1 matt
398 1.1.2.1 matt textsize = (textsize + PGOFSET) & ~PGOFSET;
399 1.1.2.1 matt totalsize = (totalsize + PGOFSET) & ~PGOFSET;
400 1.1.2.1 matt
401 1.1.2.1 matt logical = KERNEL_BASE_PHYS - mem->dram[0].address; /* offset of kernel in RAM */
402 1.1.2.1 matt logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
403 1.1.2.1 matt physical_start + logical, textsize,
404 1.1.2.1 matt VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
405 1.1.2.1 matt logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
406 1.1.2.1 matt physical_start + logical, totalsize - textsize,
407 1.1.2.1 matt VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
408 1.1.2.1 matt }
409 1.1.2.1 matt
410 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
411 1.1.2.1 matt printf("Constructing L2 page tables\n");
412 1.1.2.1 matt #endif
413 1.1.2.1 matt
414 1.1.2.1 matt /* Map the stack pages */
415 1.1.2.1 matt pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
416 1.1.2.1 matt IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
417 1.1.2.1 matt pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
418 1.1.2.1 matt ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
419 1.1.2.1 matt pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
420 1.1.2.1 matt UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
421 1.1.2.1 matt pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
422 1.1.2.1 matt UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
423 1.1.2.1 matt
424 1.1.2.1 matt pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
425 1.1.2.1 matt L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
426 1.1.2.1 matt
427 1.1.2.1 matt for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
428 1.1.2.1 matt pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
429 1.1.2.1 matt kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
430 1.1.2.1 matt VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
431 1.1.2.1 matt }
432 1.1.2.1 matt
433 1.1.2.1 matt /* Map the vector page. */
434 1.1.2.1 matt pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
435 1.1.2.1 matt VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
436 1.1.2.1 matt
437 1.1.2.1 matt /* Map the statically mapped devices. */
438 1.1.2.1 matt pmap_devmap_bootstrap(l1pagetable, at91_devmap());
439 1.1.2.1 matt
440 1.1.2.1 matt /*
441 1.1.2.1 matt * Update the physical_freestart/physical_freeend/free_pages
442 1.1.2.1 matt * variables.
443 1.1.2.1 matt */
444 1.1.2.1 matt {
445 1.1.2.1 matt extern char _end[];
446 1.1.2.1 matt
447 1.1.2.1 matt physical_freestart = physical_start +
448 1.1.2.1 matt (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
449 1.1.2.1 matt KERNEL_BASE);
450 1.1.2.1 matt physical_freeend = physical_end;
451 1.1.2.1 matt free_pages =
452 1.1.2.1 matt (physical_freeend - physical_freestart) / PAGE_SIZE;
453 1.1.2.1 matt }
454 1.1.2.1 matt
455 1.1.2.1 matt /*
456 1.1.2.1 matt * Now we have the real page tables in place so we can switch to them.
457 1.1.2.1 matt * Once this is done we will be running with the REAL kernel page
458 1.1.2.1 matt * tables.
459 1.1.2.1 matt */
460 1.1.2.1 matt
461 1.1.2.1 matt /* Switch tables */
462 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
463 1.1.2.1 matt printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
464 1.1.2.1 matt physical_freestart, free_pages, free_pages);
465 1.1.2.1 matt printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
466 1.1.2.1 matt #endif
467 1.1.2.1 matt cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
468 1.1.2.1 matt setttb(kernel_l1pt.pv_pa);
469 1.1.2.1 matt cpu_tlb_flushID();
470 1.1.2.1 matt cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
471 1.1.2.1 matt
472 1.1.2.1 matt /*
473 1.1.2.1 matt * Moved from cpu_startup() as data_abort_handler() references
474 1.1.2.1 matt * this during uvm init
475 1.1.2.1 matt */
476 1.1.2.1 matt proc0paddr = (struct user *)kernelstack.pv_va;
477 1.1.2.1 matt lwp0.l_addr = proc0paddr;
478 1.1.2.1 matt
479 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
480 1.1.2.1 matt printf("done!\n");
481 1.1.2.1 matt #endif
482 1.1.2.1 matt
483 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
484 1.1.2.1 matt printf("bootstrap done.\n");
485 1.1.2.1 matt #endif
486 1.1.2.1 matt
487 1.1.2.1 matt /* @@@@ check this out: @@@ */
488 1.1.2.1 matt arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
489 1.1.2.1 matt
490 1.1.2.1 matt /*
491 1.1.2.1 matt * Pages were allocated during the secondary bootstrap for the
492 1.1.2.1 matt * stacks for different CPU modes.
493 1.1.2.1 matt * We must now set the r13 registers in the different CPU modes to
494 1.1.2.1 matt * point to these stacks.
495 1.1.2.1 matt * Since the ARM stacks use STMFD etc. we must set r13 to the top end
496 1.1.2.1 matt * of the stack memory.
497 1.1.2.1 matt */
498 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
499 1.1.2.1 matt printf("init subsystems: stacks ");
500 1.1.2.1 matt #endif
501 1.1.2.1 matt
502 1.1.2.1 matt set_stackptr(PSR_IRQ32_MODE,
503 1.1.2.1 matt irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
504 1.1.2.1 matt set_stackptr(PSR_ABT32_MODE,
505 1.1.2.1 matt abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
506 1.1.2.1 matt set_stackptr(PSR_UND32_MODE,
507 1.1.2.1 matt undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
508 1.1.2.1 matt
509 1.1.2.1 matt /*
510 1.1.2.1 matt * Well we should set a data abort handler.
511 1.1.2.1 matt * Once things get going this will change as we will need a proper
512 1.1.2.1 matt * handler.
513 1.1.2.1 matt * Until then we will use a handler that just panics but tells us
514 1.1.2.1 matt * why.
515 1.1.2.1 matt * Initialisation of the vectors will just panic on a data abort.
516 1.1.2.1 matt * This just fills in a slightly better one.
517 1.1.2.1 matt */
518 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
519 1.1.2.1 matt printf("vectors ");
520 1.1.2.1 matt #endif
521 1.1.2.1 matt data_abort_handler_address = (u_int)data_abort_handler;
522 1.1.2.1 matt prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
523 1.1.2.1 matt undefined_handler_address = (u_int)undefinedinstruction_bounce;
524 1.1.2.1 matt
525 1.1.2.1 matt /* Initialise the undefined instruction handlers */
526 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
527 1.1.2.1 matt printf("undefined ");
528 1.1.2.1 matt #endif
529 1.1.2.1 matt undefined_init();
530 1.1.2.1 matt
531 1.1.2.1 matt /* Load memory into UVM. */
532 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
533 1.1.2.1 matt printf("page ");
534 1.1.2.1 matt #endif
535 1.1.2.1 matt uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
536 1.1.2.1 matt uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
537 1.1.2.1 matt atop(physical_freestart), atop(physical_freeend),
538 1.1.2.1 matt VM_FREELIST_DEFAULT);
539 1.1.2.1 matt uvm_page_physload(atop(physical_start), atop(physical_freeend_low),
540 1.1.2.1 matt atop(physical_start), atop(physical_freeend_low),
541 1.1.2.1 matt VM_FREELIST_DEFAULT);
542 1.1.2.1 matt
543 1.1.2.1 matt /* Boot strap pmap telling it where the kernel page table is */
544 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
545 1.1.2.1 matt printf("pmap ");
546 1.1.2.1 matt #endif
547 1.1.2.1 matt pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
548 1.1.2.1 matt KERNEL_VM_BASE + KERNEL_VM_SIZE);
549 1.1.2.1 matt
550 1.1.2.1 matt /* Setup the IRQ system */
551 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
552 1.1.2.1 matt printf("irq ");
553 1.1.2.1 matt #endif
554 1.1.2.1 matt at91_intr_init();
555 1.1.2.1 matt
556 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
557 1.1.2.1 matt printf("done.\n");
558 1.1.2.1 matt #endif
559 1.1.2.1 matt
560 1.1.2.1 matt #ifdef BOOTHOWTO
561 1.1.2.1 matt boothowto = BOOTHOWTO;
562 1.1.2.1 matt #endif
563 1.1.2.1 matt boothowto = AB_VERBOSE | AB_DEBUG; // @@@@
564 1.1.2.1 matt
565 1.1.2.1 matt #ifdef IPKDB
566 1.1.2.1 matt /* Initialise ipkdb */
567 1.1.2.1 matt ipkdb_init();
568 1.1.2.1 matt if (boothowto & RB_KDB)
569 1.1.2.1 matt ipkdb_connect(0);
570 1.1.2.1 matt #endif
571 1.1.2.1 matt
572 1.1.2.1 matt #if NKSYMS || defined(DDB) || defined(LKM)
573 1.1.2.1 matt /* Firmware doesn't load symbols. */
574 1.1.2.1 matt ksyms_init(0, NULL, NULL);
575 1.1.2.1 matt #endif
576 1.1.2.1 matt
577 1.1.2.1 matt #ifdef DDB
578 1.1.2.1 matt db_machine_init();
579 1.1.2.1 matt if (boothowto & RB_KDB)
580 1.1.2.1 matt Debugger();
581 1.1.2.1 matt #endif
582 1.1.2.1 matt #if 0
583 1.1.2.1 matt printf("test data abort...\n");
584 1.1.2.1 matt *((volatile uint32_t*)(0x1234567F)) = 0xdeadbeef;
585 1.1.2.1 matt #endif
586 1.1.2.1 matt
587 1.1.2.1 matt #ifdef VERBOSE_INIT_ARM
588 1.1.2.1 matt printf("%s: returning new stack pointer 0x%lX\n", __FUNCTION__, (kernelstack.pv_va + USPACE_SVC_STACK_TOP));
589 1.1.2.1 matt #endif
590 1.1.2.1 matt
591 1.1.2.1 matt /* We return the new stack pointer address */
592 1.1.2.1 matt return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
593 1.1.2.1 matt }
594 1.1.2.1 matt
595 1.1.2.1 matt static int
596 1.1.2.1 matt at91bus_match(struct device *parent, struct cfdata *match, void *aux)
597 1.1.2.1 matt {
598 1.1.2.1 matt // we could detect the device here...
599 1.1.2.1 matt if (strcmp(match->cf_name, "at91bus") == 0)
600 1.1.2.1 matt return 1;
601 1.1.2.1 matt return 0;
602 1.1.2.1 matt }
603 1.1.2.1 matt
604 1.1.2.1 matt static device_t
605 1.1.2.1 matt at91bus_found(struct device *self, bus_addr_t addr, int pid)
606 1.1.2.1 matt {
607 1.1.2.1 matt int locs[AT91BUSCF_NLOCS];
608 1.1.2.1 matt struct at91bus_attach_args sa;
609 1.1.2.1 matt struct at91bus_softc *sc;
610 1.1.2.1 matt
611 1.1.2.1 matt memset(&locs, 0, sizeof(locs));
612 1.1.2.1 matt memset(&sa, 0, sizeof(sa));
613 1.1.2.1 matt
614 1.1.2.1 matt locs[AT91BUSCF_ADDR] = addr;
615 1.1.2.1 matt locs[AT91BUSCF_PID] = pid;
616 1.1.2.1 matt
617 1.1.2.1 matt sc = (struct at91bus_softc*) self;
618 1.1.2.1 matt sa.sa_iot = sc->sc_iot;
619 1.1.2.1 matt sa.sa_dmat = sc->sc_dmat;
620 1.1.2.1 matt sa.sa_addr = addr;
621 1.1.2.1 matt sa.sa_size = 1;
622 1.1.2.1 matt sa.sa_pid = pid;
623 1.1.2.1 matt
624 1.1.2.1 matt return config_found_sm_loc(self, "at91bus", locs, &sa,
625 1.1.2.1 matt at91bus_print, at91bus_submatch);
626 1.1.2.1 matt }
627 1.1.2.1 matt
628 1.1.2.1 matt static void
629 1.1.2.1 matt at91bus_attach(struct device *parent, struct device *self, void *aux)
630 1.1.2.1 matt {
631 1.1.2.1 matt struct at91bus_softc *sc;
632 1.1.2.1 matt
633 1.1.2.1 matt if (at91_chip_ndx < 0)
634 1.1.2.1 matt panic("%s: at91bus_init() has not been called!", __FUNCTION__);
635 1.1.2.1 matt
636 1.1.2.1 matt sc = (struct at91bus_softc*) self;
637 1.1.2.1 matt
638 1.1.2.1 matt /* initialize bus space and bus dma things... */
639 1.1.2.1 matt sc->sc_iot = &at91_bs_tag;
640 1.1.2.1 matt sc->sc_dmat = at91_bus_dma_init(&at91_bd_tag);
641 1.1.2.1 matt
642 1.1.2.1 matt if (at91bus_sc == NULL)
643 1.1.2.1 matt at91bus_sc = sc;
644 1.1.2.1 matt
645 1.1.2.1 matt printf(": %s, sclk %u.%03u kHz, mclk %u.%03u MHz, pclk %u.%03u MHz, mstclk %u.%03u, plla %u.%03u, pllb %u.%03u MHz\n",
646 1.1.2.1 matt at91_types[at91_chip_ndx].name,
647 1.1.2.1 matt AT91_SCLK / 1000U, AT91_SCLK % 1000U,
648 1.1.2.1 matt AT91_MCLK / 1000000U, (AT91_MCLK / 1000U) % 1000U,
649 1.1.2.1 matt AT91_PCLK / 1000000U, (AT91_PCLK / 1000U) % 1000U,
650 1.1.2.1 matt AT91_MSTCLK / 1000000U, (AT91_MSTCLK / 1000U) % 1000U,
651 1.1.2.1 matt AT91_PLLACLK / 1000000U, (AT91_PLLACLK / 1000U) % 1000U,
652 1.1.2.1 matt AT91_PLLBCLK / 1000000U, (AT91_PLLBCLK / 1000U) % 1000U);
653 1.1.2.1 matt
654 1.1.2.1 matt /*
655 1.1.2.1 matt * Attach devices
656 1.1.2.1 matt */
657 1.1.2.1 matt at91_search_peripherals(self, at91bus_found);
658 1.1.2.1 matt
659 1.1.2.1 matt
660 1.1.2.1 matt struct at91bus_attach_args sa;
661 1.1.2.1 matt memset(&sa, 0, sizeof(sa));
662 1.1.2.1 matt sa.sa_iot = sc->sc_iot;
663 1.1.2.1 matt sa.sa_dmat = sc->sc_dmat;
664 1.1.2.1 matt config_search_ia(at91bus_search, self, "at91bus", &sa);
665 1.1.2.1 matt }
666 1.1.2.1 matt
667 1.1.2.1 matt int
668 1.1.2.1 matt at91bus_submatch(parent, cf, ldesc, aux)
669 1.1.2.1 matt struct device *parent;
670 1.1.2.1 matt struct cfdata *cf;
671 1.1.2.1 matt const int *ldesc;
672 1.1.2.1 matt void *aux;
673 1.1.2.1 matt {
674 1.1.2.1 matt struct at91bus_attach_args *sa = aux;
675 1.1.2.1 matt
676 1.1.2.1 matt if (cf->cf_loc[AT91BUSCF_ADDR] == ldesc[AT91BUSCF_ADDR]
677 1.1.2.1 matt && cf->cf_loc[AT91BUSCF_PID] == ldesc[AT91BUSCF_PID]) {
678 1.1.2.1 matt sa->sa_addr = cf->cf_loc[AT91BUSCF_ADDR];
679 1.1.2.1 matt sa->sa_size = cf->cf_loc[AT91BUSCF_SIZE];
680 1.1.2.1 matt sa->sa_pid = cf->cf_loc[AT91BUSCF_PID];
681 1.1.2.1 matt return (config_match(parent, cf, aux));
682 1.1.2.1 matt } else
683 1.1.2.1 matt return (0);
684 1.1.2.1 matt }
685 1.1.2.1 matt
686 1.1.2.1 matt int
687 1.1.2.1 matt at91bus_search(parent, cf, ldesc, aux)
688 1.1.2.1 matt struct device *parent;
689 1.1.2.1 matt struct cfdata *cf;
690 1.1.2.1 matt const int *ldesc;
691 1.1.2.1 matt void *aux;
692 1.1.2.1 matt {
693 1.1.2.1 matt struct at91bus_attach_args *sa = aux;
694 1.1.2.1 matt
695 1.1.2.1 matt sa->sa_addr = cf->cf_loc[AT91BUSCF_ADDR];
696 1.1.2.1 matt sa->sa_size = cf->cf_loc[AT91BUSCF_SIZE];
697 1.1.2.1 matt sa->sa_pid = cf->cf_loc[AT91BUSCF_PID];
698 1.1.2.1 matt
699 1.1.2.1 matt if (config_match(parent, cf, aux) > 0)
700 1.1.2.1 matt config_attach(parent, cf, aux, at91bus_print);
701 1.1.2.1 matt
702 1.1.2.1 matt return (0);
703 1.1.2.1 matt }
704 1.1.2.1 matt
705 1.1.2.1 matt static int
706 1.1.2.1 matt at91bus_print(aux, name)
707 1.1.2.1 matt void *aux;
708 1.1.2.1 matt const char *name;
709 1.1.2.1 matt {
710 1.1.2.1 matt struct at91bus_attach_args *sa = (struct at91bus_attach_args*)aux;
711 1.1.2.1 matt
712 1.1.2.1 matt if (name)
713 1.1.2.1 matt aprint_normal("%s at %s", sa->sa_pid >= 0 ? at91_peripheral_name(sa->sa_pid) : "device", name);
714 1.1.2.1 matt
715 1.1.2.1 matt if (sa->sa_size)
716 1.1.2.1 matt aprint_normal(" at addr 0x%lx", sa->sa_addr);
717 1.1.2.1 matt if (sa->sa_size > 1)
718 1.1.2.1 matt aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
719 1.1.2.1 matt if (sa->sa_pid >= 0)
720 1.1.2.1 matt aprint_normal(" pid %d", sa->sa_pid);
721 1.1.2.1 matt
722 1.1.2.1 matt return (UNCONF);
723 1.1.2.1 matt }
724 1.1.2.1 matt
725 1.1.2.1 matt void consinit(void)
726 1.1.2.1 matt {
727 1.1.2.1 matt static int consinit_called;
728 1.1.2.1 matt
729 1.1.2.1 matt if (consinit_called != 0)
730 1.1.2.1 matt return;
731 1.1.2.1 matt
732 1.1.2.1 matt consinit_called = 1;
733 1.1.2.1 matt
734 1.1.2.1 matt if (at91_chip_ndx < 0)
735 1.1.2.1 matt panic("%s: at91_init() has not been called!", __FUNCTION__);
736 1.1.2.1 matt
737 1.1.2.1 matt // call machine specific bus initialization code
738 1.1.2.1 matt (*at91bus_tag->init)(&at91bus_clocks);
739 1.1.2.1 matt
740 1.1.2.1 matt // attach console
741 1.1.2.1 matt (*at91bus_tag->attach_cn)(&at91_bs_tag, cnspeed, cnmode);
742 1.1.2.1 matt }
743