Home | History | Annotate | Line # | Download | only in npwr_fc
npwr_fc_machdep.c revision 1.17
      1 /*	$NetBSD: npwr_fc_machdep.c,v 1.17 2012/07/29 00:07:09 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1997,1998 Mark Brinicombe.
     40  * Copyright (c) 1997,1998 Causality Limited.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Mark Brinicombe
     54  *	for the NetBSD Project.
     55  * 4. The name of the company nor the name of the author may be used to
     56  *    endorse or promote products derived from this software without specific
     57  *    prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * Machine dependent functions for kernel setup for Intel IQ80321 evaluation
     72  * boards using RedBoot firmware.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: npwr_fc_machdep.c,v 1.17 2012/07/29 00:07:09 matt Exp $");
     77 
     78 #include "opt_ddb.h"
     79 #include "opt_kgdb.h"
     80 #include "opt_pmap_debug.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/device.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/exec.h>
     87 #include <sys/proc.h>
     88 #include <sys/msgbuf.h>
     89 #include <sys/reboot.h>
     90 #include <sys/termios.h>
     91 #include <sys/ksyms.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <dev/cons.h>
     96 
     97 #include <machine/db_machdep.h>
     98 #include <ddb/db_sym.h>
     99 #include <ddb/db_extern.h>
    100 
    101 #include <machine/bootconfig.h>
    102 #include <sys/bus.h>
    103 #include <machine/cpu.h>
    104 #include <machine/frame.h>
    105 #include <arm/undefined.h>
    106 
    107 #include <arm/arm32/machdep.h>
    108 
    109 #include <arm/xscale/i80321reg.h>
    110 #include <arm/xscale/i80321var.h>
    111 
    112 #include <dev/pci/ppbreg.h>
    113 
    114 #include <evbarm/iq80321/iq80321reg.h>
    115 #include <evbarm/iq80321/iq80321var.h>
    116 #include <evbarm/iq80321/obiovar.h>
    117 
    118 #include "ksyms.h"
    119 
    120 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    121 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    122 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    123 
    124 /*
    125  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    126  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    127  */
    128 #define KERNEL_VM_SIZE		0x0C000000
    129 
    130 /*
    131  * Address to call from cpu_reset() to reset the machine.
    132  * This is machine architecture dependent as it varies depending
    133  * on where the ROM appears when you turn the MMU off.
    134  *
    135  * XXX Not actally used on IQ80321 -- clean up the generic
    136  * ARM code.
    137  */
    138 
    139 u_int cpu_reset_address = 0x00000000;
    140 
    141 /* Define various stack sizes in pages */
    142 #define IRQ_STACK_SIZE	1
    143 #define ABT_STACK_SIZE	1
    144 #define UND_STACK_SIZE	1
    145 
    146 BootConfig bootconfig;		/* Boot config storage */
    147 char *boot_args = NULL;
    148 char *boot_file = NULL;
    149 
    150 vm_offset_t physical_start;
    151 vm_offset_t physical_freestart;
    152 vm_offset_t physical_freeend;
    153 vm_offset_t physical_end;
    154 u_int free_pages;
    155 
    156 /*int debug_flags;*/
    157 #ifndef PMAP_STATIC_L1S
    158 int max_processes = 64;			/* Default number */
    159 #endif	/* !PMAP_STATIC_L1S */
    160 
    161 /* Physical and virtual addresses for some global pages */
    162 pv_addr_t minidataclean;
    163 
    164 vm_offset_t msgbufphys;
    165 
    166 #ifdef PMAP_DEBUG
    167 extern int pmap_debug_level;
    168 #endif
    169 
    170 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    171 
    172 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    173 #define	KERNEL_PT_KERNEL_NUM	4
    174 
    175 					/* L2 table for mapping i80321 */
    176 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    177 
    178 					/* L2 tables for mapping kernel VM */
    179 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
    180 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    181 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    182 
    183 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    184 
    185 /* Prototypes */
    186 
    187 void	consinit(void);
    188 
    189 #include "com.h"
    190 #if NCOM > 0
    191 #include <dev/ic/comreg.h>
    192 #include <dev/ic/comvar.h>
    193 #endif
    194 
    195 /*
    196  * Define the default console speed for the board.  This is generally
    197  * what the firmware provided with the board defaults to.
    198  */
    199 #ifndef CONSPEED
    200 #define CONSPEED B115200
    201 #endif /* ! CONSPEED */
    202 
    203 #ifndef CONUNIT
    204 #define	CONUNIT	0
    205 #endif
    206 
    207 #ifndef CONMODE
    208 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    209 #endif
    210 
    211 int comcnspeed = CONSPEED;
    212 int comcnmode = CONMODE;
    213 int comcnunit = CONUNIT;
    214 
    215 #if KGDB
    216 #ifndef KGDB_DEVNAME
    217 #error Must define KGDB_DEVNAME
    218 #endif
    219 const char kgdb_devname[] = KGDB_DEVNAME;
    220 
    221 #ifndef KGDB_DEVADDR
    222 #error Must define KGDB_DEVADDR
    223 #endif
    224 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    225 
    226 #ifndef KGDB_DEVRATE
    227 #define KGDB_DEVRATE	CONSPEED
    228 #endif
    229 int kgdb_devrate = KGDB_DEVRATE;
    230 
    231 #ifndef KGDB_DEVMODE
    232 #define KGDB_DEVMODE	CONMODE
    233 #endif
    234 int kgdb_devmode = KGDB_DEVMODE;
    235 #endif /* KGDB */
    236 
    237 /*
    238  * void cpu_reboot(int howto, char *bootstr)
    239  *
    240  * Reboots the system
    241  *
    242  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    243  * then reset the CPU.
    244  */
    245 void
    246 cpu_reboot(int howto, char *bootstr)
    247 {
    248 
    249 	/*
    250 	 * If we are still cold then hit the air brakes
    251 	 * and crash to earth fast
    252 	 */
    253 	if (cold) {
    254 		doshutdownhooks();
    255 		pmf_system_shutdown(boothowto);
    256 		printf("The operating system has halted.\n");
    257 		printf("Please press any key to reboot.\n\n");
    258 		cngetc();
    259 		printf("rebooting...\n");
    260 		goto reset;
    261 	}
    262 
    263 	/* Disable console buffering */
    264 
    265 	/*
    266 	 * If RB_NOSYNC was not specified sync the discs.
    267 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    268 	 * unmount.  It looks like syslogd is getting woken up only to find
    269 	 * that it cannot page part of the binary in as the filesystem has
    270 	 * been unmounted.
    271 	 */
    272 	if (!(howto & RB_NOSYNC))
    273 		bootsync();
    274 
    275 	/* Say NO to interrupts */
    276 	splhigh();
    277 
    278 	/* Do a dump if requested. */
    279 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    280 		dumpsys();
    281 
    282 	/* Run any shutdown hooks */
    283 	doshutdownhooks();
    284 
    285 	pmf_system_shutdown(boothowto);
    286 
    287 	/* Make sure IRQ's are disabled */
    288 	IRQdisable;
    289 
    290 	if (howto & RB_HALT) {
    291 		printf("The operating system has halted.\n");
    292 		printf("Please press any key to reboot.\n\n");
    293 		cngetc();
    294 	}
    295 
    296 	printf("rebooting...\n\r");
    297  reset:
    298 	/*
    299 	 * Make really really sure that all interrupts are disabled,
    300 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    301 	 */
    302 	(void) disable_interrupts(I32_bit|F32_bit);
    303 	*(volatile uint32_t *)(IQ80321_80321_VBASE + VERDE_ATU_BASE +
    304 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
    305 
    306 	/* ...and if that didn't work, just croak. */
    307 	printf("RESET FAILED!\n");
    308 	for (;;);
    309 }
    310 
    311 /* Static device mappings. */
    312 static const struct pmap_devmap iq80321_devmap[] = {
    313     /*
    314      * Map the on-board devices VA == PA so that we can access them
    315      * with the MMU on or off.
    316      */
    317     {
    318 	IQ80321_OBIO_BASE,
    319 	IQ80321_OBIO_BASE,
    320 	IQ80321_OBIO_SIZE,
    321 	VM_PROT_READ|VM_PROT_WRITE,
    322 	PTE_NOCACHE,
    323     },
    324 
    325     {
    326 	IQ80321_IOW_VBASE,
    327 	VERDE_OUT_XLATE_IO_WIN0_BASE,
    328 	VERDE_OUT_XLATE_IO_WIN_SIZE,
    329 	VM_PROT_READ|VM_PROT_WRITE,
    330 	PTE_NOCACHE,
    331    },
    332 
    333    {
    334 	IQ80321_80321_VBASE,
    335 	VERDE_PMMR_BASE,
    336 	VERDE_PMMR_SIZE,
    337 	VM_PROT_READ|VM_PROT_WRITE,
    338 	PTE_NOCACHE,
    339    },
    340 
    341    {
    342 	0,
    343 	0,
    344 	0,
    345 	0,
    346 	0,
    347     }
    348 };
    349 
    350 /*
    351  * u_int initarm(...)
    352  *
    353  * Initial entry point on startup. This gets called before main() is
    354  * entered.
    355  * It should be responsible for setting up everything that must be
    356  * in place when main is called.
    357  * This includes
    358  *   Taking a copy of the boot configuration structure.
    359  *   Initialising the physical console so characters can be printed.
    360  *   Setting up page tables for the kernel
    361  *   Relocating the kernel to the bottom of physical memory
    362  */
    363 u_int
    364 initarm(void *arg)
    365 {
    366 	extern vaddr_t xscale_cache_clean_addr;
    367 #ifdef DIAGNOSTIC
    368 	extern vsize_t xscale_minidata_clean_size;
    369 #endif
    370 	int loop;
    371 	int loop1;
    372 	u_int l1pagetable;
    373 	paddr_t memstart;
    374 	psize_t memsize;
    375 
    376 	/* Calibrate the delay loop. */
    377 	i80321_calibrate_delay();
    378 	i80321_hardclock_hook = NULL;
    379 
    380 	/*
    381 	 * Since we map the on-board devices VA==PA, and the kernel
    382 	 * is running VA==PA, it's possible for us to initialize
    383 	 * the console now.
    384 	 */
    385 	consinit();
    386 
    387 #ifdef VERBOSE_INIT_ARM
    388 	/* Talk to the user */
    389 	printf("\nNetBSD/evbarm (NPWR_FC) booting ...\n");
    390 #endif
    391 
    392 	/*
    393 	 * Heads up ... Setup the CPU / MMU / TLB functions
    394 	 */
    395 	if (set_cpufuncs())
    396 		panic("cpu not recognized!");
    397 
    398 	/*
    399 	 * We are currently running with the MMU enabled and the
    400 	 * entire address space mapped VA==PA, except for the
    401 	 * first 64M of RAM is also double-mapped at 0xc0000000.
    402 	 * There is an L1 page table at 0xa0004000.
    403 	 */
    404 
    405 	/*
    406 	 * Fetch the SDRAM start/size from the i80321 SDRAM configration
    407 	 * registers.
    408 	 */
    409 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    410 	    &memstart, &memsize);
    411 
    412 #ifdef VERBOSE_INIT_ARM
    413 	printf("initarm: Configuring system ...\n");
    414 #endif
    415 
    416 	/* Fake bootconfig structure for the benefit of pmap.c */
    417 	/* XXX must make the memory description h/w independent */
    418 	bootconfig.dramblocks = 1;
    419 	bootconfig.dram[0].address = memstart;
    420 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    421 
    422 	/*
    423 	 * Set up the variables that define the availablilty of
    424 	 * physical memory.  For now, we're going to set
    425 	 * physical_freestart to 0xa0200000 (where the kernel
    426 	 * was loaded), and allocate the memory we need downwards.
    427 	 * If we get too close to the L1 table that we set up, we
    428 	 * will panic.  We will update physical_freestart and
    429 	 * physical_freeend later to reflect what pmap_bootstrap()
    430 	 * wants to see.
    431 	 *
    432 	 * XXX pmap_bootstrap() needs an enema.
    433 	 */
    434 	physical_start = bootconfig.dram[0].address;
    435 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    436 
    437 	physical_freestart = 0xa0009000UL;
    438 	physical_freeend = 0xa0200000UL;
    439 
    440 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    441 
    442 #ifdef VERBOSE_INIT_ARM
    443 	/* Tell the user about the memory */
    444 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    445 	    physical_start, physical_end - 1);
    446 #endif
    447 
    448 	/*
    449 	 * Okay, the kernel starts 2MB in from the bottom of physical
    450 	 * memory.  We are going to allocate our bootstrap pages downwards
    451 	 * from there.
    452 	 *
    453 	 * We need to allocate some fixed page tables to get the kernel
    454 	 * going.  We allocate one page directory and a number of page
    455 	 * tables and store the physical addresses in the kernel_pt_table
    456 	 * array.
    457 	 *
    458 	 * The kernel page directory must be on a 16K boundary.  The page
    459 	 * tables must be on 4K bounaries.  What we do is allocate the
    460 	 * page directory on the first 16K boundary that we encounter, and
    461 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    462 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    463 	 * least one 16K aligned region.
    464 	 */
    465 
    466 #ifdef VERBOSE_INIT_ARM
    467 	printf("Allocating page tables\n");
    468 #endif
    469 
    470 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    471 
    472 #ifdef VERBOSE_INIT_ARM
    473 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    474 	       physical_freestart, free_pages, free_pages);
    475 #endif
    476 
    477 	/* Define a macro to simplify memory allocation */
    478 #define	valloc_pages(var, np)				\
    479 	alloc_pages((var).pv_pa, (np));			\
    480 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    481 
    482 #define alloc_pages(var, np)				\
    483 	physical_freeend -= ((np) * PAGE_SIZE);		\
    484 	if (physical_freeend < physical_freestart)	\
    485 		panic("initarm: out of memory");	\
    486 	(var) = physical_freeend;			\
    487 	free_pages -= (np);				\
    488 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    489 
    490 	loop1 = 0;
    491 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    492 		/* Are we 16KB aligned for an L1 ? */
    493 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    494 		    && kernel_l1pt.pv_pa == 0) {
    495 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    496 		} else {
    497 			valloc_pages(kernel_pt_table[loop1],
    498 			    L2_TABLE_SIZE / PAGE_SIZE);
    499 			++loop1;
    500 		}
    501 	}
    502 
    503 	/* This should never be able to happen but better confirm that. */
    504 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    505 		panic("initarm: Failed to align the kernel page directory");
    506 
    507 	/*
    508 	 * Allocate a page for the system page mapped to V0x00000000
    509 	 * This page will just contain the system vectors and can be
    510 	 * shared by all processes.
    511 	 */
    512 	alloc_pages(systempage.pv_pa, 1);
    513 
    514 	/* Allocate stacks for all modes */
    515 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    516 	valloc_pages(abtstack, ABT_STACK_SIZE);
    517 	valloc_pages(undstack, UND_STACK_SIZE);
    518 	valloc_pages(kernelstack, UPAGES);
    519 
    520 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    521 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    522 	valloc_pages(minidataclean, 1);
    523 
    524 #ifdef VERBOSE_INIT_ARM
    525 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    526 	    irqstack.pv_va);
    527 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    528 	    abtstack.pv_va);
    529 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    530 	    undstack.pv_va);
    531 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    532 	    kernelstack.pv_va);
    533 #endif
    534 
    535 	/*
    536 	 * XXX Defer this to later so that we can reclaim the memory
    537 	 * XXX used by the RedBoot page tables.
    538 	 */
    539 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    540 
    541 	/*
    542 	 * Ok we have allocated physical pages for the primary kernel
    543 	 * page tables
    544 	 */
    545 
    546 #ifdef VERBOSE_INIT_ARM
    547 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    548 #endif
    549 
    550 	/*
    551 	 * Now we start construction of the L1 page table
    552 	 * We start by mapping the L2 page tables into the L1.
    553 	 * This means that we can replace L1 mappings later on if necessary
    554 	 */
    555 	l1pagetable = kernel_l1pt.pv_pa;
    556 
    557 	/* Map the L2 pages tables in the L1 page table */
    558 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    559 	    &kernel_pt_table[KERNEL_PT_SYS]);
    560 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    561 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    562 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    563 	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
    564 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    565 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    566 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    567 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    568 
    569 	/* update the top of the kernel VM */
    570 	pmap_curmaxkvaddr =
    571 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    572 
    573 #ifdef VERBOSE_INIT_ARM
    574 	printf("Mapping kernel\n");
    575 #endif
    576 
    577 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    578 	{
    579 		extern char etext[], _end[];
    580 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    581 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    582 		u_int logical;
    583 
    584 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    585 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    586 
    587 		logical = 0x00200000;	/* offset of kernel in RAM */
    588 
    589 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    590 		    physical_start + logical, textsize,
    591 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    592 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    593 		    physical_start + logical, totalsize - textsize,
    594 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    595 	}
    596 
    597 #ifdef VERBOSE_INIT_ARM
    598 	printf("Constructing L2 page tables\n");
    599 #endif
    600 
    601 	/* Map the stack pages */
    602 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    603 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    604 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    605 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    606 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    607 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    608 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    609 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    610 
    611 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    612 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    613 
    614 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    615 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    616 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    617 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    618 	}
    619 
    620 	/* Map the Mini-Data cache clean area. */
    621 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    622 	    minidataclean.pv_pa);
    623 
    624 	/* Map the vector page. */
    625 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    626 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    627 
    628 	/* Map the statically mapped devices. */
    629 	pmap_devmap_bootstrap(l1pagetable, iq80321_devmap);
    630 
    631 	/*
    632 	 * Give the XScale global cache clean code an appropriately
    633 	 * sized chunk of unmapped VA space starting at 0xff000000
    634 	 * (our device mappings end before this address).
    635 	 */
    636 	xscale_cache_clean_addr = 0xff000000U;
    637 
    638 	/*
    639 	 * Now we have the real page tables in place so we can switch to them.
    640 	 * Once this is done we will be running with the REAL kernel page
    641 	 * tables.
    642 	 */
    643 
    644 	/*
    645 	 * Update the physical_freestart/physical_freeend/free_pages
    646 	 * variables.
    647 	 */
    648 	{
    649 		extern char _end[];
    650 
    651 		physical_freestart = physical_start +
    652 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    653 		     KERNEL_BASE);
    654 		physical_freeend = physical_end;
    655 		free_pages =
    656 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    657 	}
    658 
    659 	/* Switch tables */
    660 #ifdef VERBOSE_INIT_ARM
    661 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    662 	       physical_freestart, free_pages, free_pages);
    663 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    664 #endif
    665 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    666 	cpu_setttb(kernel_l1pt.pv_pa);
    667 	cpu_tlb_flushID();
    668 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    669 
    670 	/*
    671 	 * Moved from cpu_startup() as data_abort_handler() references
    672 	 * this during uvm init
    673 	 */
    674 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    675 
    676 #ifdef VERBOSE_INIT_ARM
    677 	printf("done!\n");
    678 #endif
    679 
    680 #ifdef VERBOSE_INIT_ARM
    681 	printf("bootstrap done.\n");
    682 #endif
    683 
    684 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    685 
    686 	/*
    687 	 * Pages were allocated during the secondary bootstrap for the
    688 	 * stacks for different CPU modes.
    689 	 * We must now set the r13 registers in the different CPU modes to
    690 	 * point to these stacks.
    691 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    692 	 * of the stack memory.
    693 	 */
    694 #ifdef VERBOSE_INIT_ARM
    695 	printf("init subsystems: stacks ");
    696 #endif
    697 
    698 	set_stackptr(PSR_IRQ32_MODE,
    699 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    700 	set_stackptr(PSR_ABT32_MODE,
    701 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    702 	set_stackptr(PSR_UND32_MODE,
    703 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    704 
    705 	/*
    706 	 * Well we should set a data abort handler.
    707 	 * Once things get going this will change as we will need a proper
    708 	 * handler.
    709 	 * Until then we will use a handler that just panics but tells us
    710 	 * why.
    711 	 * Initialisation of the vectors will just panic on a data abort.
    712 	 * This just fills in a slighly better one.
    713 	 */
    714 #ifdef VERBOSE_INIT_ARM
    715 	printf("vectors ");
    716 #endif
    717 	data_abort_handler_address = (u_int)data_abort_handler;
    718 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    719 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    720 
    721 	/* Initialise the undefined instruction handlers */
    722 #ifdef VERBOSE_INIT_ARM
    723 	printf("undefined ");
    724 #endif
    725 	undefined_init();
    726 
    727 	/* Load memory into UVM. */
    728 #ifdef VERBOSE_INIT_ARM
    729 	printf("page ");
    730 #endif
    731 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    732 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    733 	    atop(physical_freestart), atop(physical_freeend),
    734 	    VM_FREELIST_DEFAULT);
    735 
    736 	/* Boot strap pmap telling it where the kernel page table is */
    737 #ifdef VERBOSE_INIT_ARM
    738 	printf("pmap ");
    739 #endif
    740 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    741 
    742 	/* Setup the IRQ system */
    743 #ifdef VERBOSE_INIT_ARM
    744 	printf("irq ");
    745 #endif
    746 	i80321_intr_init();
    747 
    748 #ifdef VERBOSE_INIT_ARM
    749 	printf("done.\n");
    750 #endif
    751 
    752 #ifdef BOOTHOWTO
    753 	boothowto = BOOTHOWTO;
    754 #endif
    755 
    756 #ifdef DDB
    757 	db_machine_init();
    758 	if (boothowto & RB_KDB)
    759 		Debugger();
    760 #endif
    761 
    762 	/* We return the new stack pointer address */
    763 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    764 }
    765 
    766 void
    767 consinit(void)
    768 {
    769 	static const bus_addr_t comcnaddrs[] = {
    770 		IQ80321_UART1,		/* com0 */
    771 	};
    772 	static int consinit_called;
    773 
    774 	if (consinit_called != 0)
    775 		return;
    776 
    777 	consinit_called = 1;
    778 
    779 	/*
    780 	 * Console devices are mapped VA==PA.  Our devmap reflects
    781 	 * this, so register it now so drivers can map the console
    782 	 * device.
    783 	 */
    784 	pmap_devmap_register(iq80321_devmap);
    785 
    786 #if NCOM > 0
    787 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    788 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    789 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    790 #else
    791 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    792 #endif
    793 #if KGDB
    794 #if NCOM > 0
    795 	if (strcmp(kgdb_devname, "com") == 0) {
    796 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
    797 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
    798 	}
    799 #endif	/* NCOM > 0 */
    800 #endif	/* KGDB */
    801 }
    802