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