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