xref: /netbsd-src/sys/arch/evbarm/iq80321/iq80321_machdep.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: iq80321_machdep.c,v 1.34 2005/12/11 12:17:09 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1997,1998 Mark Brinicombe.
40  * Copyright (c) 1997,1998 Causality Limited.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by Mark Brinicombe
54  *	for the NetBSD Project.
55  * 4. The name of the company nor the name of the author may be used to
56  *    endorse or promote products derived from this software without specific
57  *    prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  * Machine dependant functions for kernel setup for Intel IQ80321 evaluation
72  * boards using RedBoot firmware.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: iq80321_machdep.c,v 1.34 2005/12/11 12:17:09 christos Exp $");
77 
78 #include "opt_ddb.h"
79 #include "opt_kgdb.h"
80 #include "opt_pmap_debug.h"
81 
82 #include <sys/param.h>
83 #include <sys/device.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/exec.h>
87 #include <sys/proc.h>
88 #include <sys/msgbuf.h>
89 #include <sys/reboot.h>
90 #include <sys/termios.h>
91 #include <sys/ksyms.h>
92 
93 #include <uvm/uvm_extern.h>
94 
95 #include <dev/cons.h>
96 
97 #include <machine/db_machdep.h>
98 #include <ddb/db_sym.h>
99 #include <ddb/db_extern.h>
100 
101 #include <machine/bootconfig.h>
102 #include <machine/bus.h>
103 #include <machine/cpu.h>
104 #include <machine/frame.h>
105 #include <arm/undefined.h>
106 
107 #include <arm/arm32/machdep.h>
108 
109 #include <arm/xscale/i80321reg.h>
110 #include <arm/xscale/i80321var.h>
111 
112 #include <dev/pci/ppbreg.h>
113 
114 #include <evbarm/iq80321/iq80321reg.h>
115 #include <evbarm/iq80321/iq80321var.h>
116 #include <evbarm/iq80321/obiovar.h>
117 
118 #include "opt_ipkdb.h"
119 #include "ksyms.h"
120 
121 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
122 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
123 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
124 
125 /*
126  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
127  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
128  */
129 #define KERNEL_VM_SIZE		0x0C000000
130 
131 /*
132  * Address to call from cpu_reset() to reset the machine.
133  * This is machine architecture dependant as it varies depending
134  * on where the ROM appears when you turn the MMU off.
135  *
136  * XXX Not actally used on IQ80321 -- clean up the generic
137  * ARM code.
138  */
139 
140 u_int cpu_reset_address = 0x00000000;
141 
142 /* Define various stack sizes in pages */
143 #define IRQ_STACK_SIZE	1
144 #define ABT_STACK_SIZE	1
145 #ifdef IPKDB
146 #define UND_STACK_SIZE	2
147 #else
148 #define UND_STACK_SIZE	1
149 #endif
150 
151 BootConfig bootconfig;		/* Boot config storage */
152 char *boot_args = NULL;
153 char *boot_file = NULL;
154 
155 vm_offset_t physical_start;
156 vm_offset_t physical_freestart;
157 vm_offset_t physical_freeend;
158 vm_offset_t physical_end;
159 u_int free_pages;
160 vm_offset_t pagetables_start;
161 int physmem = 0;
162 
163 /*int debug_flags;*/
164 #ifndef PMAP_STATIC_L1S
165 int max_processes = 64;			/* Default number */
166 #endif	/* !PMAP_STATIC_L1S */
167 
168 /* Physical and virtual addresses for some global pages */
169 pv_addr_t systempage;
170 pv_addr_t irqstack;
171 pv_addr_t undstack;
172 pv_addr_t abtstack;
173 pv_addr_t kernelstack;
174 pv_addr_t minidataclean;
175 
176 vm_offset_t msgbufphys;
177 
178 extern u_int data_abort_handler_address;
179 extern u_int prefetch_abort_handler_address;
180 extern u_int undefined_handler_address;
181 
182 #ifdef PMAP_DEBUG
183 extern int pmap_debug_level;
184 #endif
185 
186 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
187 
188 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
189 #define	KERNEL_PT_KERNEL_NUM	4
190 
191 					/* L2 table for mapping i80321 */
192 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
193 
194 					/* L2 tables for mapping kernel VM */
195 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
196 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
197 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
198 
199 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
200 
201 struct user *proc0paddr;
202 
203 /* Prototypes */
204 
205 void	consinit(void);
206 
207 #include "com.h"
208 #if NCOM > 0
209 #include <dev/ic/comreg.h>
210 #include <dev/ic/comvar.h>
211 #endif
212 
213 /*
214  * Define the default console speed for the board.  This is generally
215  * what the firmware provided with the board defaults to.
216  */
217 #ifndef CONSPEED
218 #define CONSPEED B115200
219 #endif /* ! CONSPEED */
220 
221 #ifndef CONUNIT
222 #define	CONUNIT	0
223 #endif
224 
225 #ifndef CONMODE
226 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
227 #endif
228 
229 int comcnspeed = CONSPEED;
230 int comcnmode = CONMODE;
231 int comcnunit = CONUNIT;
232 
233 #if KGDB
234 #ifndef KGDB_DEVNAME
235 #error Must define KGDB_DEVNAME
236 #endif
237 const char kgdb_devname[] = KGDB_DEVNAME;
238 
239 #ifndef KGDB_DEVADDR
240 #error Must define KGDB_DEVADDR
241 #endif
242 unsigned long kgdb_devaddr = KGDB_DEVADDR;
243 
244 #ifndef KGDB_DEVRATE
245 #define KGDB_DEVRATE	CONSPEED
246 #endif
247 int kgdb_devrate = KGDB_DEVRATE;
248 
249 #ifndef KGDB_DEVMODE
250 #define KGDB_DEVMODE	CONMODE
251 #endif
252 int kgdb_devmode = KGDB_DEVMODE;
253 #endif /* KGDB */
254 
255 /*
256  * void cpu_reboot(int howto, char *bootstr)
257  *
258  * Reboots the system
259  *
260  * Deal with any syncing, unmounting, dumping and shutdown hooks,
261  * then reset the CPU.
262  */
263 void
264 cpu_reboot(int howto, char *bootstr)
265 {
266 
267 	/*
268 	 * If we are still cold then hit the air brakes
269 	 * and crash to earth fast
270 	 */
271 	if (cold) {
272 		doshutdownhooks();
273 		printf("The operating system has halted.\n");
274 		printf("Please press any key to reboot.\n\n");
275 		cngetc();
276 		printf("rebooting...\n");
277 		goto reset;
278 	}
279 
280 	/* Disable console buffering */
281 
282 	/*
283 	 * If RB_NOSYNC was not specified sync the discs.
284 	 * Note: Unless cold is set to 1 here, syslogd will die during the
285 	 * unmount.  It looks like syslogd is getting woken up only to find
286 	 * that it cannot page part of the binary in as the filesystem has
287 	 * been unmounted.
288 	 */
289 	if (!(howto & RB_NOSYNC))
290 		bootsync();
291 
292 	/* Say NO to interrupts */
293 	splhigh();
294 
295 	/* Do a dump if requested. */
296 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
297 		dumpsys();
298 
299 	/* Run any shutdown hooks */
300 	doshutdownhooks();
301 
302 	/* Make sure IRQ's are disabled */
303 	IRQdisable;
304 
305 	if (howto & RB_HALT) {
306 		iq80321_7seg('.', '.');
307 		printf("The operating system has halted.\n");
308 		printf("Please press any key to reboot.\n\n");
309 		cngetc();
310 	}
311 
312 	printf("rebooting...\n\r");
313  reset:
314 	/*
315 	 * Make really really sure that all interrupts are disabled,
316 	 * and poke the Internal Bus and Peripheral Bus reset lines.
317 	 */
318 	(void) disable_interrupts(I32_bit|F32_bit);
319 	*(__volatile uint32_t *)(IQ80321_80321_VBASE + VERDE_ATU_BASE +
320 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
321 
322 	/* ...and if that didn't work, just croak. */
323 	printf("RESET FAILED!\n");
324 	for (;;);
325 }
326 
327 /* Static device mappings. */
328 static const struct pmap_devmap iq80321_devmap[] = {
329     /*
330      * Map the on-board devices VA == PA so that we can access them
331      * with the MMU on or off.
332      */
333     {
334 	IQ80321_OBIO_BASE,
335 	IQ80321_OBIO_BASE,
336 	IQ80321_OBIO_SIZE,
337 	VM_PROT_READ|VM_PROT_WRITE,
338 	PTE_NOCACHE,
339     },
340 
341     {
342 	IQ80321_IOW_VBASE,
343 	VERDE_OUT_XLATE_IO_WIN0_BASE,
344 	VERDE_OUT_XLATE_IO_WIN_SIZE,
345 	VM_PROT_READ|VM_PROT_WRITE,
346 	PTE_NOCACHE,
347    },
348 
349    {
350 	IQ80321_80321_VBASE,
351 	VERDE_PMMR_BASE,
352 	VERDE_PMMR_SIZE,
353 	VM_PROT_READ|VM_PROT_WRITE,
354 	PTE_NOCACHE,
355    },
356 
357    {
358 	0,
359 	0,
360 	0,
361 	0,
362 	0,
363     }
364 };
365 
366 static void
367 iq80321_hardclock_hook(void)
368 {
369 	static int snakefreq;
370 
371 	if ((snakefreq++ & 15) == 0)
372 		iq80321_7seg_snake();
373 }
374 
375 /*
376  * u_int initarm(...)
377  *
378  * Initial entry point on startup. This gets called before main() is
379  * entered.
380  * It should be responsible for setting up everything that must be
381  * in place when main is called.
382  * This includes
383  *   Taking a copy of the boot configuration structure.
384  *   Initialising the physical console so characters can be printed.
385  *   Setting up page tables for the kernel
386  *   Relocating the kernel to the bottom of physical memory
387  */
388 u_int
389 initarm(void *arg)
390 {
391 	extern vaddr_t xscale_cache_clean_addr;
392 #ifdef DIAGNOSTIC
393 	extern vsize_t xscale_minidata_clean_size;
394 #endif
395 	int loop;
396 	int loop1;
397 	u_int l1pagetable;
398 	pv_addr_t kernel_l1pt;
399 	paddr_t memstart;
400 	psize_t memsize;
401 
402 	/*
403 	 * Clear out the 7-segment display.  Whee, the first visual
404 	 * indication that we're running kernel code.
405 	 */
406 	iq80321_7seg(' ', ' ');
407 
408 	/* Calibrate the delay loop. */
409 	i80321_calibrate_delay();
410 	i80321_hardclock_hook = iq80321_hardclock_hook;
411 
412 	/*
413 	 * Since we map the on-board devices VA==PA, and the kernel
414 	 * is running VA==PA, it's possible for us to initialize
415 	 * the console now.
416 	 */
417 	consinit();
418 
419 #ifdef VERBOSE_INIT_ARM
420 	/* Talk to the user */
421 	printf("\nNetBSD/evbarm (IQ80321) booting ...\n");
422 #endif
423 
424 	/*
425 	 * Heads up ... Setup the CPU / MMU / TLB functions
426 	 */
427 	if (set_cpufuncs())
428 		panic("CPU not recognized!");
429 
430 	/*
431 	 * We are currently running with the MMU enabled and the
432 	 * entire address space mapped VA==PA, except for the
433 	 * first 64M of RAM is also double-mapped at 0xc0000000.
434 	 * There is an L1 page table at 0xa0004000.
435 	 */
436 
437 	/*
438 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
439 	 * registers.
440 	 */
441 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
442 	    &memstart, &memsize);
443 
444 #ifdef VERBOSE_INIT_ARM
445 	printf("initarm: Configuring system ...\n");
446 #endif
447 
448 	/* Fake bootconfig structure for the benefit of pmap.c */
449 	/* XXX must make the memory description h/w independant */
450 	bootconfig.dramblocks = 1;
451 	bootconfig.dram[0].address = memstart;
452 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
453 
454 	/*
455 	 * Set up the variables that define the availablilty of
456 	 * physical memory.  For now, we're going to set
457 	 * physical_freestart to 0xa0200000 (where the kernel
458 	 * was loaded), and allocate the memory we need downwards.
459 	 * If we get too close to the L1 table that we set up, we
460 	 * will panic.  We will update physical_freestart and
461 	 * physical_freeend later to reflect what pmap_bootstrap()
462 	 * wants to see.
463 	 *
464 	 * XXX pmap_bootstrap() needs an enema.
465 	 */
466 	physical_start = bootconfig.dram[0].address;
467 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
468 
469 	physical_freestart = 0xa0009000UL;
470 	physical_freeend = 0xa0200000UL;
471 
472 	physmem = (physical_end - physical_start) / PAGE_SIZE;
473 
474 #ifdef VERBOSE_INIT_ARM
475 	/* Tell the user about the memory */
476 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
477 	    physical_start, physical_end - 1);
478 #endif
479 
480 	/*
481 	 * Okay, the kernel starts 2MB in from the bottom of physical
482 	 * memory.  We are going to allocate our bootstrap pages downwards
483 	 * from there.
484 	 *
485 	 * We need to allocate some fixed page tables to get the kernel
486 	 * going.  We allocate one page directory and a number of page
487 	 * tables and store the physical addresses in the kernel_pt_table
488 	 * array.
489 	 *
490 	 * The kernel page directory must be on a 16K boundary.  The page
491 	 * tables must be on 4K boundaries.  What we do is allocate the
492 	 * page directory on the first 16K boundary that we encounter, and
493 	 * the page tables on 4K boundaries otherwise.  Since we allocate
494 	 * at least 3 L2 page tables, we are guaranteed to encounter at
495 	 * least one 16K aligned region.
496 	 */
497 
498 #ifdef VERBOSE_INIT_ARM
499 	printf("Allocating page tables\n");
500 #endif
501 
502 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
503 
504 #ifdef VERBOSE_INIT_ARM
505 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
506 	       physical_freestart, free_pages, free_pages);
507 #endif
508 
509 	/* Define a macro to simplify memory allocation */
510 #define	valloc_pages(var, np)				\
511 	alloc_pages((var).pv_pa, (np));			\
512 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
513 
514 #define alloc_pages(var, np)				\
515 	physical_freeend -= ((np) * PAGE_SIZE);		\
516 	if (physical_freeend < physical_freestart)	\
517 		panic("initarm: out of memory");	\
518 	(var) = physical_freeend;			\
519 	free_pages -= (np);				\
520 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
521 
522 	loop1 = 0;
523 	kernel_l1pt.pv_pa = 0;
524 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
525 		/* Are we 16KB aligned for an L1 ? */
526 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
527 		    && kernel_l1pt.pv_pa == 0) {
528 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
529 		} else {
530 			valloc_pages(kernel_pt_table[loop1],
531 			    L2_TABLE_SIZE / PAGE_SIZE);
532 			++loop1;
533 		}
534 	}
535 
536 	/* This should never be able to happen but better confirm that. */
537 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
538 		panic("initarm: Failed to align the kernel page directory");
539 
540 	/*
541 	 * Allocate a page for the system page mapped to V0x00000000
542 	 * This page will just contain the system vectors and can be
543 	 * shared by all processes.
544 	 */
545 	alloc_pages(systempage.pv_pa, 1);
546 
547 	/* Allocate stacks for all modes */
548 	valloc_pages(irqstack, IRQ_STACK_SIZE);
549 	valloc_pages(abtstack, ABT_STACK_SIZE);
550 	valloc_pages(undstack, UND_STACK_SIZE);
551 	valloc_pages(kernelstack, UPAGES);
552 
553 	/* Allocate enough pages for cleaning the Mini-Data cache. */
554 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
555 	valloc_pages(minidataclean, 1);
556 
557 #ifdef VERBOSE_INIT_ARM
558 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
559 	    irqstack.pv_va);
560 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
561 	    abtstack.pv_va);
562 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
563 	    undstack.pv_va);
564 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
565 	    kernelstack.pv_va);
566 #endif
567 
568 	/*
569 	 * XXX Defer this to later so that we can reclaim the memory
570 	 * XXX used by the RedBoot page tables.
571 	 */
572 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
573 
574 	/*
575 	 * Ok we have allocated physical pages for the primary kernel
576 	 * page tables
577 	 */
578 
579 #ifdef VERBOSE_INIT_ARM
580 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
581 #endif
582 
583 	/*
584 	 * Now we start construction of the L1 page table
585 	 * We start by mapping the L2 page tables into the L1.
586 	 * This means that we can replace L1 mappings later on if necessary
587 	 */
588 	l1pagetable = kernel_l1pt.pv_pa;
589 
590 	/* Map the L2 pages tables in the L1 page table */
591 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
592 	    &kernel_pt_table[KERNEL_PT_SYS]);
593 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
594 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
595 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
596 	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
597 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
598 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
599 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
600 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
601 
602 	/* update the top of the kernel VM */
603 	pmap_curmaxkvaddr =
604 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
605 
606 #ifdef VERBOSE_INIT_ARM
607 	printf("Mapping kernel\n");
608 #endif
609 
610 	/* Now we fill in the L2 pagetable for the kernel static code/data */
611 	{
612 		extern char etext[], _end[];
613 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
614 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
615 		u_int logical;
616 
617 		textsize = (textsize + PGOFSET) & ~PGOFSET;
618 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
619 
620 		logical = 0x00200000;	/* offset of kernel in RAM */
621 
622 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
623 		    physical_start + logical, textsize,
624 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
625 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
626 		    physical_start + logical, totalsize - textsize,
627 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
628 	}
629 
630 #ifdef VERBOSE_INIT_ARM
631 	printf("Constructing L2 page tables\n");
632 #endif
633 
634 	/* Map the stack pages */
635 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
636 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
637 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
638 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
639 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
640 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
641 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
642 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
643 
644 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
645 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
646 
647 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
648 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
649 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
650 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
651 	}
652 
653 	/* Map the Mini-Data cache clean area. */
654 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
655 	    minidataclean.pv_pa);
656 
657 	/* Map the vector page. */
658 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
659 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
660 
661 	/* Map the statically mapped devices. */
662 	pmap_devmap_bootstrap(l1pagetable, iq80321_devmap);
663 
664 	/*
665 	 * Give the XScale global cache clean code an appropriately
666 	 * sized chunk of unmapped VA space starting at 0xff000000
667 	 * (our device mappings end before this address).
668 	 */
669 	xscale_cache_clean_addr = 0xff000000U;
670 
671 	/*
672 	 * Now we have the real page tables in place so we can switch to them.
673 	 * Once this is done we will be running with the REAL kernel page
674 	 * tables.
675 	 */
676 
677 	/*
678 	 * Update the physical_freestart/physical_freeend/free_pages
679 	 * variables.
680 	 */
681 	{
682 		extern char _end[];
683 
684 		physical_freestart = physical_start +
685 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
686 		     KERNEL_BASE);
687 		physical_freeend = physical_end;
688 		free_pages =
689 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
690 	}
691 
692 	/* Switch tables */
693 #ifdef VERBOSE_INIT_ARM
694 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
695 	       physical_freestart, free_pages, free_pages);
696 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
697 #endif
698 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
699 	setttb(kernel_l1pt.pv_pa);
700 	cpu_tlb_flushID();
701 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
702 
703 	/*
704 	 * Moved from cpu_startup() as data_abort_handler() references
705 	 * this during uvm init
706 	 */
707 	proc0paddr = (struct user *)kernelstack.pv_va;
708 	lwp0.l_addr = proc0paddr;
709 
710 #ifdef VERBOSE_INIT_ARM
711 	printf("done!\n");
712 #endif
713 
714 #ifdef VERBOSE_INIT_ARM
715 	printf("bootstrap done.\n");
716 #endif
717 
718 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
719 
720 	/*
721 	 * Pages were allocated during the secondary bootstrap for the
722 	 * stacks for different CPU modes.
723 	 * We must now set the r13 registers in the different CPU modes to
724 	 * point to these stacks.
725 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
726 	 * of the stack memory.
727 	 */
728 #ifdef VERBOSE_INIT_ARM
729 	printf("init subsystems: stacks ");
730 #endif
731 
732 	set_stackptr(PSR_IRQ32_MODE,
733 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
734 	set_stackptr(PSR_ABT32_MODE,
735 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
736 	set_stackptr(PSR_UND32_MODE,
737 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
738 
739 	/*
740 	 * Well we should set a data abort handler.
741 	 * Once things get going this will change as we will need a proper
742 	 * handler.
743 	 * Until then we will use a handler that just panics but tells us
744 	 * why.
745 	 * Initialisation of the vectors will just panic on a data abort.
746 	 * This just fills in a slightly better one.
747 	 */
748 #ifdef VERBOSE_INIT_ARM
749 	printf("vectors ");
750 #endif
751 	data_abort_handler_address = (u_int)data_abort_handler;
752 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
753 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
754 
755 	/* Initialise the undefined instruction handlers */
756 #ifdef VERBOSE_INIT_ARM
757 	printf("undefined ");
758 #endif
759 	undefined_init();
760 
761 	/* Load memory into UVM. */
762 #ifdef VERBOSE_INIT_ARM
763 	printf("page ");
764 #endif
765 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
766 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
767 	    atop(physical_freestart), atop(physical_freeend),
768 	    VM_FREELIST_DEFAULT);
769 
770 	/* Boot strap pmap telling it where the kernel page table is */
771 #ifdef VERBOSE_INIT_ARM
772 	printf("pmap ");
773 #endif
774 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
775 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
776 
777 	/* Setup the IRQ system */
778 #ifdef VERBOSE_INIT_ARM
779 	printf("irq ");
780 #endif
781 	i80321_intr_init();
782 
783 #ifdef VERBOSE_INIT_ARM
784 	printf("done.\n");
785 #endif
786 
787 #ifdef BOOTHOWTO
788 	boothowto = BOOTHOWTO;
789 #endif
790 
791 #ifdef IPKDB
792 	/* Initialise ipkdb */
793 	ipkdb_init();
794 	if (boothowto & RB_KDB)
795 		ipkdb_connect(0);
796 #endif
797 
798 #if NKSYMS || defined(DDB) || defined(LKM)
799 	/* Firmware doesn't load symbols. */
800 	ksyms_init(0, NULL, NULL);
801 #endif
802 
803 #ifdef DDB
804 	db_machine_init();
805 	if (boothowto & RB_KDB)
806 		Debugger();
807 #endif
808 
809 	/* We return the new stack pointer address */
810 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
811 }
812 
813 void
814 consinit(void)
815 {
816 	static const bus_addr_t comcnaddrs[] = {
817 		IQ80321_UART1,		/* com0 */
818 	};
819 	static int consinit_called;
820 
821 	if (consinit_called != 0)
822 		return;
823 
824 	consinit_called = 1;
825 
826 	/*
827 	 * Console devices are mapped VA==PA.  Our devmap reflects
828 	 * this, so register it now so drivers can map the console
829 	 * device.
830 	 */
831 	pmap_devmap_register(iq80321_devmap);
832 
833 #if NCOM > 0
834 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
835 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
836 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
837 #else
838 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
839 #endif
840 #if KGDB
841 #if NCOM > 0
842 	if (strcmp(kgdb_devname, "com") == 0) {
843 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
844 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
845 	}
846 #endif	/* NCOM > 0 */
847 #endif	/* KGDB */
848 }
849