Home | History | Annotate | Line # | Download | only in nslu2
nslu2_machdep.c revision 1.28
      1 /*	$NetBSD: nslu2_machdep.c,v 1.28 2018/07/31 06:46:27 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Steve C. Woodford.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 2003
     33  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     49  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  */
     57 /*
     58  * Copyright (c) 1997,1998 Mark Brinicombe.
     59  * Copyright (c) 1997,1998 Causality Limited.
     60  * All rights reserved.
     61  *
     62  * Redistribution and use in source and binary forms, with or without
     63  * modification, are permitted provided that the following conditions
     64  * are met:
     65  * 1. Redistributions of source code must retain the above copyright
     66  *    notice, this list of conditions and the following disclaimer.
     67  * 2. Redistributions in binary form must reproduce the above copyright
     68  *    notice, this list of conditions and the following disclaimer in the
     69  *    documentation and/or other materials provided with the distribution.
     70  * 3. All advertising materials mentioning features or use of this software
     71  *    must display the following acknowledgement:
     72  *	This product includes software developed by Mark Brinicombe
     73  *	for the NetBSD Project.
     74  * 4. The name of the company nor the name of the author may be used to
     75  *    endorse or promote products derived from this software without specific
     76  *    prior written permission.
     77  *
     78  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     79  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     80  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     81  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     82  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     83  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     84  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     88  * SUCH DAMAGE.
     89  */
     90 
     91 /*
     92  * Machine dependent functions for kernel setup for Linksys NSLU2
     93  * using RedBoot firmware.
     94  */
     95 
     96 #include <sys/cdefs.h>
     97 __KERNEL_RCSID(0, "$NetBSD: nslu2_machdep.c,v 1.28 2018/07/31 06:46:27 skrll Exp $");
     98 
     99 #include "opt_arm_debug.h"
    100 #include "opt_ddb.h"
    101 #include "opt_kgdb.h"
    102 #include "opt_pmap_debug.h"
    103 
    104 #include <sys/param.h>
    105 #include <sys/device.h>
    106 #include <sys/systm.h>
    107 #include <sys/kernel.h>
    108 #include <sys/exec.h>
    109 #include <sys/proc.h>
    110 #include <sys/msgbuf.h>
    111 #include <sys/reboot.h>
    112 #include <sys/termios.h>
    113 #include <sys/ksyms.h>
    114 #include <sys/bus.h>
    115 #include <sys/cpu.h>
    116 
    117 #include <uvm/uvm_extern.h>
    118 
    119 #include <dev/cons.h>
    120 
    121 #include <machine/db_machdep.h>
    122 #include <ddb/db_sym.h>
    123 #include <ddb/db_extern.h>
    124 
    125 #include <machine/bootconfig.h>
    126 #include <arm/locore.h>
    127 #include <arm/undefined.h>
    128 
    129 #include <arm/arm32/machdep.h>
    130 
    131 #include <arm/xscale/ixp425reg.h>
    132 #include <arm/xscale/ixp425var.h>
    133 #include <arm/xscale/ixp425_sipvar.h>
    134 
    135 #include <evbarm/nslu2/nslu2reg.h>
    136 
    137 #include "com.h"
    138 #if NCOM > 0
    139 #include <dev/ic/comreg.h>
    140 #include <dev/ic/comvar.h>
    141 #endif
    142 
    143 #include "ksyms.h"
    144 
    145 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    146 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    147 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    148 
    149 /*
    150  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    151  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    152  */
    153 #define	KERNEL_VM_SIZE		0x0C000000
    154 
    155 BootConfig bootconfig;		/* Boot config storage */
    156 char *boot_args = NULL;
    157 char *boot_file = NULL;
    158 
    159 vaddr_t physical_start;
    160 vaddr_t physical_freestart;
    161 vaddr_t physical_freeend;
    162 vaddr_t physical_end;
    163 u_int free_pages;
    164 
    165 /* Physical and virtual addresses for some global pages */
    166 pv_addr_t minidataclean;
    167 
    168 paddr_t msgbufphys;
    169 
    170 extern int end;
    171 
    172 #ifdef PMAP_DEBUG
    173 extern int pmap_debug_level;
    174 #endif
    175 
    176 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    177 
    178 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    179 #define	KERNEL_PT_KERNEL_NUM	4
    180 #define	KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    181 					/* L2 tables for mapping kernel VM */
    182 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
    183 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    184 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    185 
    186 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    187 
    188 /* Prototypes */
    189 
    190 void	consinit(void);
    191 u_int	cpu_get_control(void);
    192 
    193 /*
    194  * Define the default console speed for the board.  This is generally
    195  * what the firmware provided with the board defaults to.
    196  */
    197 #ifndef CONSPEED
    198 #define CONSPEED B115200
    199 #endif /* ! CONSPEED */
    200 
    201 #ifndef CONUNIT
    202 #define	CONUNIT	0
    203 #endif
    204 
    205 #ifndef CONMODE
    206 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
    207 #endif
    208 
    209 int comcnspeed = CONSPEED;
    210 int comcnmode = CONMODE;
    211 int comcnunit = CONUNIT;
    212 
    213 #if KGDB
    214 #ifndef KGDB_DEVNAME
    215 #error Must define KGDB_DEVNAME
    216 #endif
    217 const char kgdb_devname[] = KGDB_DEVNAME;
    218 
    219 #ifndef KGDB_DEVADDR
    220 #error Must define KGDB_DEVADDR
    221 #endif
    222 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    223 
    224 #ifndef KGDB_DEVRATE
    225 #define KGDB_DEVRATE	CONSPEED
    226 #endif
    227 int kgdb_devrate = KGDB_DEVRATE;
    228 
    229 #ifndef KGDB_DEVMODE
    230 #define KGDB_DEVMODE	CONMODE
    231 #endif
    232 int kgdb_devmode = KGDB_DEVMODE;
    233 #endif /* KGDB */
    234 
    235 /*
    236  * void cpu_reboot(int howto, char *bootstr)
    237  *
    238  * Reboots the system
    239  *
    240  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    241  * then reset the CPU.
    242  */
    243 void
    244 cpu_reboot(int howto, char *bootstr)
    245 {
    246 
    247 #ifdef DIAGNOSTIC
    248 	/* info */
    249 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    250 #endif
    251 
    252 	/*
    253 	 * If we are still cold then hit the air brakes
    254 	 * and crash to earth fast
    255 	 */
    256 	if (cold) {
    257 		doshutdownhooks();
    258 		pmf_system_shutdown(boothowto);
    259 		printf("The operating system has halted.\n");
    260 		printf("Please press any key to reboot.\n\n");
    261 		cngetc();
    262 		goto reset;
    263 	}
    264 
    265 	/* Disable console buffering */
    266 
    267 	/*
    268 	 * If RB_NOSYNC was not specified sync the discs.
    269 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    270 	 * unmount.  It looks like syslogd is getting woken up only to find
    271 	 * that it cannot page part of the binary in as the filesystem has
    272 	 * been unmounted.
    273 	 */
    274 	if (!(howto & RB_NOSYNC))
    275 		bootsync();
    276 
    277 	/* Say NO to interrupts */
    278 	splhigh();
    279 
    280 	/* Do a dump if requested. */
    281 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    282 		dumpsys();
    283 
    284 	/* Run any shutdown hooks */
    285 	doshutdownhooks();
    286 
    287 	pmf_system_shutdown(boothowto);
    288 
    289 	/* Make sure IRQ's are disabled */
    290 	IRQdisable;
    291 
    292 	if ((howto & (RB_HALT | RB_POWERDOWN)) == RB_HALT) {
    293 		printf("The operating system has halted.\n");
    294 		printf("Please press any key to reboot.\n\n");
    295 		cngetc();
    296 	}
    297 
    298  reset:
    299 	/*
    300 	 * Make really really sure that all interrupts are disabled,
    301 	 */
    302 	(void) disable_interrupts(I32_bit | F32_bit);
    303 
    304 	if (howto & RB_POWERDOWN) {
    305 		uint32_t reg;
    306 
    307 		printf("powering down...\n\r");
    308 		/* Delay to allow the UART's Tx FIFO to drain */
    309 		delay(50000);
    310 
    311 #define	GPRD(r)		*((volatile uint32_t *)(IXP425_GPIO_VBASE+(r)))
    312 #define	GPWR(r,v)	*((volatile uint32_t *)(IXP425_GPIO_VBASE+(r))) = (v)
    313 
    314 		/*
    315 		 * Power-down pin requires a short pulse
    316 		 */
    317 		reg = GPRD(IXP425_GPIO_GPOUTR);
    318 		reg |= 1u << GPIO_POWER_OFF;
    319 		GPWR(IXP425_GPIO_GPOUTR, reg);
    320 
    321 		delay(1000);
    322 
    323 		reg = GPRD(IXP425_GPIO_GPOUTR);
    324 		reg &= ~(1u << GPIO_POWER_OFF);
    325 		GPWR(IXP425_GPIO_GPOUTR, reg);
    326 
    327 		delay(500000);
    328 		printf("POWER OFF FAILED! TRYING TO REBOOT INSTEAD\n\r");
    329 	}
    330 
    331 	printf("rebooting...\n\r");
    332 
    333 #define	WDWR(r,v) *((volatile uint32_t *)(IXP425_OST_WDOG_VBASE+(r))) = (v)
    334 	/* Force a watchdog reset */
    335 	WDWR(IXP425_OST_WDOG_KEY, OST_WDOG_KEY_MAJICK);
    336 	WDWR(IXP425_OST_WDOG_ENAB, OST_WDOG_ENAB_RST_ENA);
    337 	WDWR(IXP425_OST_WDOG, 0x1000);
    338 	WDWR(IXP425_OST_WDOG_ENAB,
    339 	    OST_WDOG_ENAB_RST_ENA | OST_WDOG_ENAB_CNT_ENA);
    340 
    341 	delay(500000);
    342 
    343 	/* ...and if that didn't work, just croak. */
    344 	printf("RESET FAILED!\n");
    345 
    346 	for (;;);
    347 }
    348 
    349 /* Static device mappings. */
    350 static const struct pmap_devmap nslu2_devmap[] = {
    351 	/* Physical/Virtual address for I/O space */
    352 	{
    353 		IXP425_IO_VBASE,
    354 		IXP425_IO_HWBASE,
    355 		IXP425_IO_SIZE,
    356 		VM_PROT_READ|VM_PROT_WRITE,
    357 		PTE_NOCACHE,
    358 	},
    359 
    360 	/* Expansion Bus */
    361 	{
    362 		IXP425_EXP_VBASE,
    363 		IXP425_EXP_HWBASE,
    364 		IXP425_EXP_SIZE,
    365 		VM_PROT_READ|VM_PROT_WRITE,
    366 		PTE_NOCACHE,
    367 	},
    368 
    369 	/* IXP425 PCI Configuration */
    370 	{
    371 		IXP425_PCI_VBASE,
    372 		IXP425_PCI_HWBASE,
    373 		IXP425_PCI_SIZE,
    374 		VM_PROT_READ|VM_PROT_WRITE,
    375 		PTE_NOCACHE,
    376 	},
    377 
    378 	/* SDRAM Controller */
    379 	{
    380 		IXP425_MCU_VBASE,
    381 		IXP425_MCU_HWBASE,
    382 		IXP425_MCU_SIZE,
    383 		VM_PROT_READ|VM_PROT_WRITE,
    384 		PTE_NOCACHE,
    385 	},
    386 
    387 	/* PCI Memory Space */
    388 	{
    389 		IXP425_PCI_MEM_VBASE,
    390 		IXP425_PCI_MEM_HWBASE,
    391 		IXP425_PCI_MEM_SIZE,
    392 		VM_PROT_READ|VM_PROT_WRITE,
    393 		PTE_NOCACHE,
    394 	},
    395 
    396 	/* Flash memory */
    397 	{
    398 		NSLU2_FLASH_VBASE,
    399 		NSLU2_FLASH_HWBASE,
    400 		NSLU2_FLASH_SIZE,
    401 		VM_PROT_READ|VM_PROT_WRITE,
    402 		PTE_NOCACHE,
    403 	},
    404 
    405 	{
    406 		0,
    407 		0,
    408 		0,
    409 		0,
    410 		0,
    411 	}
    412 };
    413 
    414 /*
    415  * u_int initarm(...)
    416  *
    417  * Initial entry point on startup. This gets called before main() is
    418  * entered.
    419  * It should be responsible for setting up everything that must be
    420  * in place when main is called.
    421  * This includes
    422  *   Taking a copy of the boot configuration structure.
    423  *   Initialising the physical console so characters can be printed.
    424  *   Setting up page tables for the kernel
    425  *   Relocating the kernel to the bottom of physical memory
    426  */
    427 u_int
    428 initarm(void *arg)
    429 {
    430 	extern vaddr_t xscale_cache_clean_addr;
    431 #ifdef DIAGNOSTIC
    432 	extern vsize_t xscale_minidata_clean_size;
    433 #endif
    434 	int loop;
    435 	int loop1;
    436 	u_int kerneldatasize;
    437 	u_int l1pagetable;
    438 	u_int freemempos;
    439 	uint32_t reg;
    440 
    441 	/*
    442 	 * Make sure the power-down GPIO pin is configured correctly, as
    443 	 * cpu_reboot() may be called early on (e.g. from within ddb(9)).
    444 	 */
    445 	/* Pin is active-high, so make sure it's driven low */
    446 	reg = GPRD(IXP425_GPIO_GPOUTR);
    447 	reg &= ~(1u << GPIO_POWER_OFF);
    448 	GPWR(IXP425_GPIO_GPOUTR, reg);
    449 
    450 	/* Set as output */
    451 	reg = GPRD(IXP425_GPIO_GPOER);
    452 	reg &= ~(1u << GPIO_POWER_OFF);
    453 	GPWR(IXP425_GPIO_GPOER, reg);
    454 
    455 	/*
    456 	 * Since we map v0xf0000000 == p0xc8000000, it's possible for
    457 	 * us to initialize the console now.
    458 	 */
    459 	consinit();
    460 
    461 #ifdef VERBOSE_INIT_ARM
    462 	/* Talk to the user */
    463 	printf("\nNetBSD/evbarm (Linksys NSLU2) booting ...\n");
    464 #endif
    465 
    466 	/*
    467 	 * Heads up ... Setup the CPU / MMU / TLB functions
    468 	 */
    469 	if (set_cpufuncs())
    470 		panic("cpu not recognized!");
    471 
    472 	/* XXX overwrite bootconfig to hardcoded values */
    473 	bootconfig.dramblocks = 1;
    474 	bootconfig.dram[0].address = 0x10000000;
    475 	bootconfig.dram[0].pages = ixp425_sdram_size() / PAGE_SIZE;
    476 
    477 	kerneldatasize = (uint32_t)&end - (uint32_t)KERNEL_TEXT_BASE;
    478 
    479 #ifdef VERBOSE_INIT_ARM
    480         printf("kernsize=0x%x\n", kerneldatasize);
    481 #endif
    482         kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) + PAGE_SIZE * 8;
    483 
    484 	/*
    485 	 * Set up the variables that define the availablilty of
    486 	 * physical memory.  For now, we're going to set
    487 	 * physical_freestart to 0x10200000 (where the kernel
    488 	 * was loaded), and allocate the memory we need downwards.
    489 	 * If we get too close to the L1 table that we set up, we
    490 	 * will panic.  We will update physical_freestart and
    491 	 * physical_freeend later to reflect what pmap_bootstrap()
    492 	 * wants to see.
    493 	 *
    494 	 * XXX pmap_bootstrap() needs an enema.
    495 	 */
    496 	physical_start = bootconfig.dram[0].address;
    497 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    498 
    499 	physical_freestart = physical_start
    500                 + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
    501         physical_freeend = physical_end;
    502 
    503 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    504 
    505 	/* Tell the user about the memory */
    506 #ifdef VERBOSE_INIT_ARM
    507 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    508 	    physical_start, physical_end - 1);
    509 
    510 	printf("Allocating page tables\n");
    511 #endif
    512 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    513 
    514 	freemempos = 0x10000000;
    515 
    516 #ifdef VERBOSE_INIT_ARM
    517         printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
    518                 physical_start, physical_end);
    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 #if 0
    527 #define alloc_pages(var, np)				\
    528 	physical_freeend -= ((np) * PAGE_SIZE);		\
    529 	if (physical_freeend < physical_freestart)	\
    530 		panic("initarm: out of memory");	\
    531 	(var) = physical_freeend;			\
    532 	free_pages -= (np);				\
    533 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    534 #else
    535 #define alloc_pages(var, np)				\
    536         (var) = freemempos;                             \
    537         memset((char *)(var), 0, ((np) * PAGE_SIZE));   \
    538         freemempos += (np) * PAGE_SIZE;
    539 #endif
    540 
    541 	loop1 = 0;
    542 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    543 		/* Are we 16KB aligned for an L1 ? */
    544 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    545 		    && kernel_l1pt.pv_pa == 0) {
    546 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    547 		} else {
    548 			valloc_pages(kernel_pt_table[loop1],
    549 			    L2_TABLE_SIZE / PAGE_SIZE);
    550 			++loop1;
    551 		}
    552 	}
    553 
    554 	/* This should never be able to happen but better confirm that. */
    555 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    556 		panic("initarm: Failed to align the kernel page directory");
    557 
    558 	/*
    559 	 * Allocate a page for the system page.
    560 	 * This page will just contain the system vectors and can be
    561 	 * shared by all processes.
    562 	 */
    563 	alloc_pages(systempage.pv_pa, 1);
    564 
    565 	/* Allocate stacks for all modes */
    566 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    567 	valloc_pages(abtstack, ABT_STACK_SIZE);
    568 	valloc_pages(undstack, UND_STACK_SIZE);
    569 	valloc_pages(kernelstack, UPAGES);
    570 
    571 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    572 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    573 	valloc_pages(minidataclean, 1);
    574 
    575 #ifdef VERBOSE_INIT_ARM
    576 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    577 	    irqstack.pv_va);
    578 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    579 	    abtstack.pv_va);
    580 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    581 	    undstack.pv_va);
    582 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    583 	    kernelstack.pv_va);
    584 #endif
    585 
    586 	/*
    587 	 * XXX Defer this to later so that we can reclaim the memory
    588 	 * XXX used by the RedBoot page tables.
    589 	 */
    590 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    591 
    592 	/*
    593 	 * Ok we have allocated physical pages for the primary kernel
    594 	 * page tables
    595 	 */
    596 
    597 #ifdef VERBOSE_INIT_ARM
    598 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    599 #endif
    600 
    601 	/*
    602 	 * Now we start construction of the L1 page table
    603 	 * We start by mapping the L2 page tables into the L1.
    604 	 * This means that we can replace L1 mappings later on if necessary
    605 	 */
    606 	l1pagetable = kernel_l1pt.pv_pa;
    607 
    608 	/* Map the L2 pages tables in the L1 page table */
    609 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    610 	    &kernel_pt_table[KERNEL_PT_SYS]);
    611 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    612 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    613 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    614 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    615 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    616 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    617 
    618 	/* update the top of the kernel VM */
    619 	pmap_curmaxkvaddr =
    620 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    621 
    622 	pmap_link_l2pt(l1pagetable, IXP425_IO_VBASE,
    623 	    &kernel_pt_table[KERNEL_PT_IO]);
    624 
    625 #ifdef VERBOSE_INIT_ARM
    626 	printf("Mapping kernel\n");
    627 #endif
    628 
    629 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    630 	{
    631 		extern char etext[], _end[];
    632 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    633 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    634 		u_int logical;
    635 
    636 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    637 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    638 
    639 		logical = 0x00200000;	/* offset of kernel in RAM */
    640 
    641 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    642 		    physical_start + logical, textsize,
    643 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    644 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    645 		    physical_start + logical, totalsize - textsize,
    646 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    647 	}
    648 
    649 #ifdef VERBOSE_INIT_ARM
    650 	printf("Constructing L2 page tables\n");
    651 #endif
    652 
    653 	/* Map the stack pages */
    654 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    655 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    656 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    657 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    658 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    659 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    660 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    661 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    662 
    663 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    664 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    665 
    666 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    667 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    668 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    669 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    670 	}
    671 
    672 	/* Map the Mini-Data cache clean area. */
    673 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    674 	    minidataclean.pv_pa);
    675 
    676 	/* Map the vector page. */
    677 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    678 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    679 
    680         /*
    681          * Map the IXP425 registers
    682          */
    683 	pmap_devmap_bootstrap(l1pagetable, nslu2_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 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    720 	cpu_setttb(kernel_l1pt.pv_pa, true);
    721 	cpu_tlb_flushID();
    722 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    723 
    724 	/*
    725 	 * Moved from cpu_startup() as data_abort_handler() references
    726 	 * this during uvm init
    727 	 */
    728 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    729 
    730 #ifdef VERBOSE_INIT_ARM
    731 	printf("bootstrap done.\n");
    732 #endif
    733 
    734 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    735 
    736 	/*
    737 	 * Pages were allocated during the secondary bootstrap for the
    738 	 * stacks for different CPU modes.
    739 	 * We must now set the r13 registers in the different CPU modes to
    740 	 * point to these stacks.
    741 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    742 	 * of the stack memory.
    743 	 */
    744 #ifdef VERBOSE_INIT_ARM
    745 	printf("init subsystems: stacks ");
    746 #endif
    747 
    748 	set_stackptr(PSR_IRQ32_MODE,
    749 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    750 	set_stackptr(PSR_ABT32_MODE,
    751 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    752 	set_stackptr(PSR_UND32_MODE,
    753 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    754 
    755 	/*
    756 	 * Well we should set a data abort handler.
    757 	 * Once things get going this will change as we will need a proper
    758 	 * handler.
    759 	 * Until then we will use a handler that just panics but tells us
    760 	 * why.
    761 	 * Initialisation of the vectors will just panic on a data abort.
    762 	 * This just fills in a slightly better one.
    763 	 */
    764 #ifdef VERBOSE_INIT_ARM
    765 	printf("vectors ");
    766 #endif
    767 	data_abort_handler_address = (u_int)data_abort_handler;
    768 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    769 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    770 
    771 	/* Initialise the undefined instruction handlers */
    772 #ifdef VERBOSE_INIT_ARM
    773 	printf("undefined ");
    774 #endif
    775 	undefined_init();
    776 
    777 	/* Load memory into UVM. */
    778 #ifdef VERBOSE_INIT_ARM
    779 	printf("page ");
    780 #endif
    781 	uvm_md_init();
    782 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    783 	    atop(physical_freestart), atop(physical_freeend),
    784 	    VM_FREELIST_DEFAULT);
    785 
    786 	/* Boot strap pmap telling it where the kernel page table is */
    787 #ifdef VERBOSE_INIT_ARM
    788 	printf("pmap ");
    789 #endif
    790 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    791 
    792 	/* Setup the IRQ system */
    793 #ifdef VERBOSE_INIT_ARM
    794 	printf("irq ");
    795 #endif
    796 	ixp425_intr_init();
    797 #ifdef VERBOSE_INIT_ARM
    798 	printf("\nAll initialization done!\nNow Starting NetBSD, Here we go!\n");
    799 #endif
    800 
    801 #ifdef BOOTHOWTO
    802 	boothowto = BOOTHOWTO;
    803 #endif
    804 
    805 #ifdef DDB
    806 	db_machine_init();
    807 	if (boothowto & RB_KDB)
    808 		Debugger();
    809 #endif
    810 
    811 	/* We return the new stack pointer address */
    812 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    813 }
    814 
    815 /*
    816  * consinit
    817  */
    818 void
    819 consinit(void)
    820 {
    821 	static int consinit_called;
    822 	static const bus_addr_t addrs[2] = {
    823 		IXP425_UART0_HWBASE, IXP425_UART1_HWBASE
    824 	};
    825 
    826 	if (consinit_called != 0)
    827 		return;
    828 
    829 	consinit_called = 1;
    830 
    831 	pmap_devmap_register(nslu2_devmap);
    832 
    833 	if (comcnattach(&ixp425_a4x_bs_tag, addrs[comcnunit],
    834 	    comcnspeed, IXP425_UART_FREQ, COM_TYPE_PXA2x0, comcnmode))
    835 		panic("can't init serial console (UART%d)", comcnunit);
    836 }
    837