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