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