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