Home | History | Annotate | Line # | Download | only in viper
viper_machdep.c revision 1.6
      1 /*	$NetBSD: viper_machdep.c,v 1.6 2006/11/24 22:04:22 wiz 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.6 2006/11/24 22:04:22 wiz 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 /*
    269  * void cpu_reboot(int howto, char *bootstr)
    270  *
    271  * Reboots the system
    272  *
    273  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    274  * then reset the CPU.
    275  */
    276 void
    277 cpu_reboot(int howto, char *bootstr)
    278 {
    279 #ifdef DIAGNOSTIC
    280 	/* info */
    281 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    282 #endif
    283 
    284 	/*
    285 	 * If we are still cold then hit the air brakes
    286 	 * and crash to earth fast
    287 	 */
    288 	if (cold) {
    289 		doshutdownhooks();
    290 		printf("The operating system has halted.\n");
    291 		printf("Please press any key to reboot.\n\n");
    292 		cngetc();
    293 		printf("rebooting...\n");
    294 		cpu_reset();
    295 		/*NOTREACHED*/
    296 	}
    297 
    298 	/* Disable console buffering */
    299 /*	cnpollc(1);*/
    300 
    301 	/*
    302 	 * If RB_NOSYNC was not specified sync the discs.
    303 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    304 	 * unmount.  It looks like syslogd is getting woken up only to find
    305 	 * that it cannot page part of the binary in as the filesystem has
    306 	 * been unmounted.
    307 	 */
    308 	if (!(howto & RB_NOSYNC))
    309 		bootsync();
    310 
    311 	/* Say NO to interrupts */
    312 	splhigh();
    313 
    314 	/* Do a dump if requested. */
    315 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    316 		dumpsys();
    317 
    318 	/* Run any shutdown hooks */
    319 	doshutdownhooks();
    320 
    321 	/* Make sure IRQ's are disabled */
    322 	IRQdisable;
    323 
    324 	if (howto & RB_HALT) {
    325 		printf("The operating system has halted.\n");
    326 		printf("Please press any key to reboot.\n\n");
    327 		cngetc();
    328 	}
    329 
    330 	printf("rebooting...\n");
    331 	cpu_reset();
    332 	/*NOTREACHED*/
    333 }
    334 
    335 /*
    336  * Static device mappings. These peripheral registers are mapped at
    337  * fixed virtual addresses very early in viper_start() so that we
    338  * can use them while booting the kernel, and stay at the same address
    339  * throughout whole kernel's life time.
    340  *
    341  * We use this table twice; once with bootstrap page table, and once
    342  * with kernel's page table which we build up in initarm().
    343  */
    344 
    345 static const struct pmap_devmap viper_devmap[] = {
    346     {
    347 	    VIPER_GPIO_VBASE,
    348 	    PXA2X0_GPIO_BASE,
    349 	    L1_S_SIZE,
    350 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    351     },
    352     {
    353 	    VIPER_CLKMAN_VBASE,
    354 	    PXA2X0_CLKMAN_BASE,
    355 	    L1_S_SIZE,
    356 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    357     },
    358     {
    359 	    VIPER_INTCTL_VBASE,
    360 	    PXA2X0_INTCTL_BASE,
    361 	    L1_S_SIZE,
    362 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    363     },
    364     {
    365 	    VIPER_FFUART_VBASE,
    366 	    PXA2X0_FFUART_BASE,
    367 	    L1_S_SIZE,
    368 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    369     },
    370     {
    371 	    VIPER_BTUART_VBASE,
    372 	    PXA2X0_BTUART_BASE,
    373 	    L1_S_SIZE,
    374 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    375     },
    376 
    377     {0, 0, 0, 0,}
    378 };
    379 
    380 #ifndef MEMSTART
    381 #define MEMSTART 0xa0000000
    382 #endif
    383 #ifndef MEMSIZE
    384 #define MEMSIZE 0x4000000
    385 #endif
    386 
    387 /*
    388  * u_int initarm(...)
    389  *
    390  * Initial entry point on startup. This gets called before main() is
    391  * entered.
    392  * It should be responsible for setting up everything that must be
    393  * in place when main is called.
    394  * This includes
    395  *   Taking a copy of the boot configuration structure.
    396  *   Initialising the physical console so characters can be printed.
    397  *   Setting up page tables for the kernel
    398  *   Relocating the kernel to the bottom of physical memory
    399  */
    400 u_int
    401 initarm(void *arg)
    402 {
    403 	extern vaddr_t xscale_cache_clean_addr;
    404 	int loop;
    405 	int loop1;
    406 	u_int l1pagetable;
    407 	pv_addr_t kernel_l1pt;
    408 #ifdef DIAGNOSTIC
    409 	extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
    410 #endif
    411 
    412 	/* Register devmap for devices we mapped in start */
    413 	pmap_devmap_register(viper_devmap);
    414 
    415 	/* start 32.768 kHz OSC */
    416 	ioreg_write(VIPER_CLKMAN_VBASE + 0x08, 2);
    417 	/* Get ready for splfoo() */
    418 	pxa2x0_intr_bootstrap(VIPER_INTCTL_VBASE);
    419 
    420 	/*
    421 	 * Heads up ... Setup the CPU / MMU / TLB functions
    422 	 */
    423 	if (set_cpufuncs())
    424 		panic("cpu not recognized!");
    425 
    426 #if 0
    427 	/* Calibrate the delay loop. */
    428 #endif
    429 
    430 	/* setup GPIO for BTUART, in case bootloader doesn't take care of it */
    431 	pxa2x0_gpio_bootstrap(VIPER_GPIO_VBASE);
    432 	pxa2x0_gpio_set_function(42, GPIO_ALT_FN_1_IN);
    433 	pxa2x0_gpio_set_function(43, GPIO_ALT_FN_2_OUT);
    434 	pxa2x0_gpio_set_function(44, GPIO_ALT_FN_1_IN);
    435 	pxa2x0_gpio_set_function(45, GPIO_ALT_FN_2_OUT);
    436 
    437 	/* turn on clock to UART block.
    438 	   XXX: this should not be done here. */
    439 	ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, CKEN_FFUART|CKEN_BTUART |
    440 	    ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN));
    441 
    442 	consinit();
    443 #ifdef KGDB
    444 	kgdb_port_init();
    445 #endif
    446 	/* Talk to the user */
    447 	printf("\nNetBSD/evbarm (viper) booting ...\n");
    448 
    449 #if 0
    450 	/*
    451 	 * Examine the boot args string for options we need to know about
    452 	 * now.
    453 	 */
    454 	process_kernel_args((char *)nwbootinfo.bt_args);
    455 #endif
    456 
    457 	printf("initarm: Configuring system ...\n");
    458 
    459 	/* Fake bootconfig structure for the benefit of pmap.c */
    460 	/* XXX must make the memory description h/w independent */
    461 	bootconfig.dramblocks = 1;
    462 	bootconfig.dram[0].address = MEMSTART;
    463 	bootconfig.dram[0].pages = MEMSIZE / PAGE_SIZE;
    464 
    465 	/*
    466 	 * Set up the variables that define the availablilty of
    467 	 * physical memory.  For now, we're going to set
    468 	 * physical_freestart to 0xa0200000 (where the kernel
    469 	 * was loaded), and allocate the memory we need downwards.
    470 	 * If we get too close to the page tables that RedBoot
    471 	 * set up, we will panic.  We will update physical_freestart
    472 	 * and physical_freeend later to reflect what pmap_bootstrap()
    473 	 * wants to see.
    474 	 *
    475 	 * XXX pmap_bootstrap() needs an enema.
    476 	 * (now that would be truly hardcore XXX)
    477 	 */
    478 	physical_start = bootconfig.dram[0].address;
    479 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    480 
    481 	physical_freestart = 0xa0009000UL;
    482 	physical_freeend = 0xa0200000UL;
    483 
    484 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    485 
    486 #ifdef VERBOSE_INIT_ARM
    487 	/* Tell the user about the memory */
    488 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    489 	    physical_start, physical_end - 1);
    490 #endif
    491 
    492 	/*
    493 	 * Okay, the kernel starts 2MB in from the bottom of physical
    494 	 * memory.  We are going to allocate our bootstrap pages downwards
    495 	 * from there.
    496 	 *
    497 	 * We need to allocate some fixed page tables to get the kernel
    498 	 * going.  We allocate one page directory and a number of page
    499 	 * tables and store the physical addresses in the kernel_pt_table
    500 	 * array.
    501 	 *
    502 	 * The kernel page directory must be on a 16K boundary.  The page
    503 	 * tables must be on 4K boundaries.  What we do is allocate the
    504 	 * page directory on the first 16K boundary that we encounter, and
    505 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    506 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    507 	 * least one 16K aligned region.
    508 	 */
    509 
    510 #ifdef VERBOSE_INIT_ARM
    511 	printf("Allocating page tables\n");
    512 #endif
    513 
    514 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    515 
    516 #ifdef VERBOSE_INIT_ARM
    517 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    518 	       physical_freestart, free_pages, free_pages);
    519 #endif
    520 
    521 	/* Define a macro to simplify memory allocation */
    522 #define	valloc_pages(var, np)				\
    523 	alloc_pages((var).pv_pa, (np));			\
    524 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    525 
    526 #define alloc_pages(var, np)				\
    527 	physical_freeend -= ((np) * PAGE_SIZE);		\
    528 	if (physical_freeend < physical_freestart)	\
    529 		panic("initarm: out of memory");	\
    530 	(var) = physical_freeend;			\
    531 	free_pages -= (np);				\
    532 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    533 
    534 	loop1 = 0;
    535 	kernel_l1pt.pv_pa = 0;
    536 	kernel_l1pt.pv_va = 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 	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 	proc0paddr = (struct user *)kernelstack.pv_va;
    730 	lwp0.l_addr = proc0paddr;
    731 
    732 #ifdef VERBOSE_INIT_ARM
    733 	printf("bootstrap done.\n");
    734 #endif
    735 
    736 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    737 
    738 	/*
    739 	 * Pages were allocated during the secondary bootstrap for the
    740 	 * stacks for different CPU modes.
    741 	 * We must now set the r13 registers in the different CPU modes to
    742 	 * point to these stacks.
    743 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    744 	 * of the stack memory.
    745 	 */
    746 	printf("init subsystems: stacks ");
    747 
    748 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    749 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    750 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    751 
    752 	/*
    753 	 * Well we should set a data abort handler.
    754 	 * Once things get going this will change as we will need a proper
    755 	 * handler.
    756 	 * Until then we will use a handler that just panics but tells us
    757 	 * why.
    758 	 * Initialisation of the vectors will just panic on a data abort.
    759 	 * This just fills in a slightly better one.
    760 	 */
    761 	printf("vectors ");
    762 	data_abort_handler_address = (u_int)data_abort_handler;
    763 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    764 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    765 
    766 	/* Initialise the undefined instruction handlers */
    767 	printf("undefined ");
    768 	undefined_init();
    769 
    770 	/* Load memory into UVM. */
    771 	printf("page ");
    772 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
    773 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    774 	    atop(physical_freestart), atop(physical_freeend),
    775 	    VM_FREELIST_DEFAULT);
    776 
    777 	/* Boot strap pmap telling it where the kernel page table is */
    778 	printf("pmap ");
    779 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    780 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    781 
    782 #ifdef __HAVE_MEMORY_DISK__
    783 	md_root_setconf(memory_disk, sizeof memory_disk);
    784 #endif
    785 
    786 #ifdef IPKDB
    787 	/* Initialise ipkdb */
    788 	ipkdb_init();
    789 	if (boothowto & RB_KDB)
    790 		ipkdb_connect(0);
    791 #endif
    792 
    793 #ifdef KGDB
    794 	if (boothowto & RB_KDB) {
    795 		kgdb_debug_init = 1;
    796 		kgdb_connect(1);
    797 	}
    798 #endif
    799 
    800 #ifdef DDB
    801 	db_machine_init();
    802 
    803 	/* Firmware doesn't load symbols. */
    804 	ddb_init(0, NULL, NULL);
    805 
    806 	if (boothowto & RB_KDB)
    807 		Debugger();
    808 #endif
    809 
    810 	/* We return the new stack pointer address */
    811 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    812 }
    813 
    814 #if 0
    815 void
    816 process_kernel_args(char *args)
    817 {
    818 
    819 	boothowto = 0;
    820 
    821 	/* Make a local copy of the bootargs */
    822 	strncpy(bootargs, args, MAX_BOOT_STRING);
    823 
    824 	args = bootargs;
    825 	boot_file = bootargs;
    826 
    827 	/* Skip the kernel image filename */
    828 	while (*args != ' ' && *args != 0)
    829 		++args;
    830 
    831 	if (*args != 0)
    832 		*args++ = 0;
    833 
    834 	while (*args == ' ')
    835 		++args;
    836 
    837 	boot_args = args;
    838 
    839 	printf("bootfile: %s\n", boot_file);
    840 	printf("bootargs: %s\n", boot_args);
    841 
    842 	parse_mi_bootargs(boot_args);
    843 }
    844 #endif
    845 
    846 #ifdef KGDB
    847 #ifndef KGDB_DEVNAME
    848 #define KGDB_DEVNAME "ffuart"
    849 #endif
    850 const char kgdb_devname[] = KGDB_DEVNAME;
    851 
    852 #if (NCOM > 0)
    853 #ifndef KGDB_DEVMODE
    854 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    855 #endif
    856 int comkgdbmode = KGDB_DEVMODE;
    857 #endif /* NCOM */
    858 
    859 #endif /* KGDB */
    860 
    861 
    862 void
    863 consinit(void)
    864 {
    865 	static int consinit_called = 0;
    866 	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
    867 #if 0
    868 	char *console = CONSDEVNAME;
    869 #endif
    870 
    871 	if (consinit_called != 0)
    872 		return;
    873 	consinit_called = 1;
    874 
    875 #if NCOM > 0
    876 
    877 #ifdef FFUARTCONSOLE
    878 #ifdef KGDB
    879 	if (0 == strcmp(kgdb_devname, "ffuart")) {
    880 		/* port is reserved for kgdb */
    881 	} else
    882 #endif
    883 	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
    884 		     comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
    885 
    886 #if 0
    887 		/* XXX: can't call pxa2x0_clkman_config yet */
    888 		pxa2x0_clkman_config(CKEN_FFUART, 1);
    889 #else
    890 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN,
    891 		    ckenreg|CKEN_FFUART);
    892 #endif
    893 
    894 		return;
    895 	}
    896 
    897 #endif /* FFUARTCONSOLE */
    898 
    899 #ifdef BTUARTCONSOLE
    900 #ifdef KGDB
    901 	if (0 == strcmp(kgdb_devname, "btuart")) {
    902 		/* port is reserved for kgdb */
    903 	} else
    904 #endif
    905 	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
    906 		comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
    907 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN,
    908 		    ckenreg|CKEN_BTUART);
    909 		return;
    910 	}
    911 #endif /* BTUARTCONSOLE */
    912 
    913 	/* no console, guess we're flying blind */
    914 
    915 #endif /* NCOM */
    916 
    917 }
    918 
    919 #ifdef KGDB
    920 void
    921 kgdb_port_init(void)
    922 {
    923 #if (NCOM > 0) && defined(COM_PXA2X0)
    924 	paddr_t paddr = 0;
    925 	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
    926 
    927 	if (0 == strcmp(kgdb_devname, "ffuart")) {
    928 		paddr = PXA2X0_FFUART_BASE;
    929 		ckenreg |= CKEN_FFUART;
    930 	}
    931 	else if (0 == strcmp(kgdb_devname, "btuart")) {
    932 		paddr = PXA2X0_BTUART_BASE;
    933 		ckenreg |= CKEN_BTUART;
    934 	}
    935 
    936 	if (paddr &&
    937 	    0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
    938 		kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
    939 
    940 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
    941 	}
    942 #endif
    943 }
    944 #endif
    945