Home | History | Annotate | Line # | Download | only in nslu2
nslu2_machdep.c revision 1.36
      1 /*	$NetBSD: nslu2_machdep.c,v 1.36 2023/04/20 08:28:05 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.36 2023/04/20 08:28:05 skrll Exp $");
     98 
     99 #include "opt_arm_debug.h"
    100 #include "opt_console.h"
    101 #include "opt_ddb.h"
    102 #include "opt_kgdb.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 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    173 
    174 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    175 #define	KERNEL_PT_KERNEL_NUM	4
    176 #define	KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    177 					/* L2 tables for mapping kernel VM */
    178 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
    179 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    180 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    181 
    182 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    183 
    184 /* Prototypes */
    185 
    186 void	consinit(void);
    187 u_int	cpu_get_control(void);
    188 
    189 /*
    190  * Define the default console speed for the board.  This is generally
    191  * what the firmware provided with the board defaults to.
    192  */
    193 #ifndef CONSPEED
    194 #define CONSPEED B115200
    195 #endif /* ! CONSPEED */
    196 
    197 #ifndef CONUNIT
    198 #define	CONUNIT	0
    199 #endif
    200 
    201 #ifndef CONMODE
    202 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
    203 #endif
    204 
    205 int comcnspeed = CONSPEED;
    206 int comcnmode = CONMODE;
    207 int comcnunit = CONUNIT;
    208 
    209 #if KGDB
    210 #ifndef KGDB_DEVNAME
    211 #error Must define KGDB_DEVNAME
    212 #endif
    213 const char kgdb_devname[] = KGDB_DEVNAME;
    214 
    215 #ifndef KGDB_DEVADDR
    216 #error Must define KGDB_DEVADDR
    217 #endif
    218 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    219 
    220 #ifndef KGDB_DEVRATE
    221 #define KGDB_DEVRATE	CONSPEED
    222 #endif
    223 int kgdb_devrate = KGDB_DEVRATE;
    224 
    225 #ifndef KGDB_DEVMODE
    226 #define KGDB_DEVMODE	CONMODE
    227 #endif
    228 int kgdb_devmode = KGDB_DEVMODE;
    229 #endif /* KGDB */
    230 
    231 /*
    232  * void cpu_reboot(int howto, char *bootstr)
    233  *
    234  * Reboots the system
    235  *
    236  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    237  * then reset the CPU.
    238  */
    239 void
    240 cpu_reboot(int howto, char *bootstr)
    241 {
    242 
    243 #ifdef DIAGNOSTIC
    244 	/* info */
    245 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    246 #endif
    247 
    248 	/*
    249 	 * If we are still cold then hit the air brakes
    250 	 * and crash to earth fast
    251 	 */
    252 	if (cold) {
    253 		doshutdownhooks();
    254 		pmf_system_shutdown(boothowto);
    255 		printf("The operating system has halted.\n");
    256 		printf("Please press any key to reboot.\n\n");
    257 		cngetc();
    258 		goto reset;
    259 	}
    260 
    261 	/* Disable console buffering */
    262 
    263 	/*
    264 	 * If RB_NOSYNC was not specified sync the discs.
    265 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    266 	 * unmount.  It looks like syslogd is getting woken up only to find
    267 	 * that it cannot page part of the binary in as the filesystem has
    268 	 * been unmounted.
    269 	 */
    270 	if (!(howto & RB_NOSYNC))
    271 		bootsync();
    272 
    273 	/* Say NO to interrupts */
    274 	splhigh();
    275 
    276 	/* Do a dump if requested. */
    277 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    278 		dumpsys();
    279 
    280 	/* Run any shutdown hooks */
    281 	doshutdownhooks();
    282 
    283 	pmf_system_shutdown(boothowto);
    284 
    285 	/* Make sure IRQ's are disabled */
    286 	IRQdisable;
    287 
    288 	if ((howto & (RB_HALT | RB_POWERDOWN)) == RB_HALT) {
    289 		printf("The operating system has halted.\n");
    290 		printf("Please press any key to reboot.\n\n");
    291 		cngetc();
    292 	}
    293 
    294  reset:
    295 	/*
    296 	 * Make really really sure that all interrupts are disabled,
    297 	 */
    298 	(void) disable_interrupts(I32_bit | F32_bit);
    299 
    300 	if (howto & RB_POWERDOWN) {
    301 		uint32_t reg;
    302 
    303 		printf("powering down...\n\r");
    304 		/* Delay to allow the UART's Tx FIFO to drain */
    305 		delay(50000);
    306 
    307 #define	GPRD(r)		*((volatile uint32_t *)(IXP425_GPIO_VBASE+(r)))
    308 #define	GPWR(r,v)	*((volatile uint32_t *)(IXP425_GPIO_VBASE+(r))) = (v)
    309 
    310 		/*
    311 		 * Power-down pin requires a short pulse
    312 		 */
    313 		reg = GPRD(IXP425_GPIO_GPOUTR);
    314 		reg |= 1u << GPIO_POWER_OFF;
    315 		GPWR(IXP425_GPIO_GPOUTR, reg);
    316 
    317 		delay(1000);
    318 
    319 		reg = GPRD(IXP425_GPIO_GPOUTR);
    320 		reg &= ~(1u << GPIO_POWER_OFF);
    321 		GPWR(IXP425_GPIO_GPOUTR, reg);
    322 
    323 		delay(500000);
    324 		printf("POWER OFF FAILED! TRYING TO REBOOT INSTEAD\n\r");
    325 	}
    326 
    327 	printf("rebooting...\n\r");
    328 
    329 #define	WDWR(r,v) *((volatile uint32_t *)(IXP425_OST_WDOG_VBASE+(r))) = (v)
    330 	/* Force a watchdog reset */
    331 	WDWR(IXP425_OST_WDOG_KEY, OST_WDOG_KEY_MAJICK);
    332 	WDWR(IXP425_OST_WDOG_ENAB, OST_WDOG_ENAB_RST_ENA);
    333 	WDWR(IXP425_OST_WDOG, 0x1000);
    334 	WDWR(IXP425_OST_WDOG_ENAB,
    335 	    OST_WDOG_ENAB_RST_ENA | OST_WDOG_ENAB_CNT_ENA);
    336 
    337 	delay(500000);
    338 
    339 	/* ...and if that didn't work, just croak. */
    340 	printf("RESET FAILED!\n");
    341 
    342 	for (;;);
    343 }
    344 
    345 /* Static device mappings. */
    346 static const struct pmap_devmap nslu2_devmap[] = {
    347 	/* Physical/Virtual address for I/O space */
    348 	DEVMAP_ENTRY(
    349 		IXP425_IO_VBASE,
    350 		IXP425_IO_HWBASE,
    351 		IXP425_IO_SIZE
    352 	),
    353 
    354 	/* Expansion Bus */
    355 	DEVMAP_ENTRY(
    356 		IXP425_EXP_VBASE,
    357 		IXP425_EXP_HWBASE,
    358 		IXP425_EXP_SIZE
    359 	),
    360 
    361 	/* IXP425 PCI Configuration */
    362 	DEVMAP_ENTRY(
    363 		IXP425_PCI_VBASE,
    364 		IXP425_PCI_HWBASE,
    365 		IXP425_PCI_SIZE
    366 	),
    367 
    368 	/* SDRAM Controller */
    369 	DEVMAP_ENTRY(
    370 		IXP425_MCU_VBASE,
    371 		IXP425_MCU_HWBASE,
    372 		IXP425_MCU_SIZE
    373 	),
    374 
    375 	/* PCI Memory Space */
    376 	DEVMAP_ENTRY(
    377 		IXP425_PCI_MEM_VBASE,
    378 		IXP425_PCI_MEM_HWBASE,
    379 		IXP425_PCI_MEM_SIZE
    380 	),
    381 
    382 	/* Flash memory */
    383 	DEVMAP_ENTRY(
    384 		NSLU2_FLASH_VBASE,
    385 		NSLU2_FLASH_HWBASE,
    386 		NSLU2_FLASH_SIZE
    387 	),
    388 
    389 	DEVMAP_ENTRY_END
    390 };
    391 
    392 /*
    393  * vaddr_t initarm(...)
    394  *
    395  * Initial entry point on startup. This gets called before main() is
    396  * entered.
    397  * It should be responsible for setting up everything that must be
    398  * in place when main is called.
    399  * This includes
    400  *   Taking a copy of the boot configuration structure.
    401  *   Initialising the physical console so characters can be printed.
    402  *   Setting up page tables for the kernel
    403  *   Relocating the kernel to the bottom of physical memory
    404  */
    405 vaddr_t
    406 initarm(void *arg)
    407 {
    408 	extern vaddr_t xscale_cache_clean_addr;
    409 #ifdef DIAGNOSTIC
    410 	extern vsize_t xscale_minidata_clean_size;
    411 #endif
    412 	int loop;
    413 	int loop1;
    414 	u_int kerneldatasize;
    415 	u_int l1pagetable;
    416 	u_int freemempos;
    417 	uint32_t reg;
    418 
    419 	/*
    420 	 * Make sure the power-down GPIO pin is configured correctly, as
    421 	 * cpu_reboot() may be called early on (e.g. from within ddb(9)).
    422 	 */
    423 	/* Pin is active-high, so make sure it's driven low */
    424 	reg = GPRD(IXP425_GPIO_GPOUTR);
    425 	reg &= ~(1u << GPIO_POWER_OFF);
    426 	GPWR(IXP425_GPIO_GPOUTR, reg);
    427 
    428 	/* Set as output */
    429 	reg = GPRD(IXP425_GPIO_GPOER);
    430 	reg &= ~(1u << GPIO_POWER_OFF);
    431 	GPWR(IXP425_GPIO_GPOER, reg);
    432 
    433 	/*
    434 	 * Since we map v0xf0000000 == p0xc8000000, it's possible for
    435 	 * us to initialize the console now.
    436 	 */
    437 	consinit();
    438 
    439 #ifdef VERBOSE_INIT_ARM
    440 	/* Talk to the user */
    441 	printf("\nNetBSD/evbarm (Linksys NSLU2) booting ...\n");
    442 #endif
    443 
    444 	/*
    445 	 * Heads up ... Setup the CPU / MMU / TLB functions
    446 	 */
    447 	if (set_cpufuncs())
    448 		panic("cpu not recognized!");
    449 
    450 	/* XXX overwrite bootconfig to hardcoded values */
    451 	bootconfig.dramblocks = 1;
    452 	bootconfig.dram[0].address = 0x10000000;
    453 	bootconfig.dram[0].pages = ixp425_sdram_size() / PAGE_SIZE;
    454 
    455 	kerneldatasize = (uint32_t)&end - (uint32_t)KERNEL_TEXT_BASE;
    456 
    457 #ifdef VERBOSE_INIT_ARM
    458         printf("kernsize=0x%x\n", kerneldatasize);
    459 #endif
    460         kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) + PAGE_SIZE * 8;
    461 
    462 	/*
    463 	 * Set up the variables that define the availability of
    464 	 * physical memory.  For now, we're going to set
    465 	 * physical_freestart to 0x10200000 (where the kernel
    466 	 * was loaded), and allocate the memory we need downwards.
    467 	 * If we get too close to the L1 table that we set up, we
    468 	 * will panic.  We will update physical_freestart and
    469 	 * physical_freeend later to reflect what pmap_bootstrap()
    470 	 * wants to see.
    471 	 *
    472 	 * XXX pmap_bootstrap() needs an enema.
    473 	 */
    474 	physical_start = bootconfig.dram[0].address;
    475 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    476 
    477 	physical_freestart = physical_start
    478                 + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
    479         physical_freeend = physical_end;
    480 
    481 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    482 
    483 	/* Tell the user about the memory */
    484 #ifdef VERBOSE_INIT_ARM
    485 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    486 	    physical_start, physical_end - 1);
    487 
    488 	printf("Allocating page tables\n");
    489 #endif
    490 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    491 
    492 	freemempos = 0x10000000;
    493 
    494 #ifdef VERBOSE_INIT_ARM
    495         printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
    496                 physical_start, physical_end);
    497 #endif
    498 
    499 	/* Define a macro to simplify memory allocation */
    500 #define	valloc_pages(var, np)				\
    501 	alloc_pages((var).pv_pa, (np));			\
    502 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    503 
    504 #if 0
    505 #define alloc_pages(var, np)				\
    506 	physical_freeend -= ((np) * PAGE_SIZE);		\
    507 	if (physical_freeend < physical_freestart)	\
    508 		panic("initarm: out of memory");	\
    509 	(var) = physical_freeend;			\
    510 	free_pages -= (np);				\
    511 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    512 #else
    513 #define alloc_pages(var, np)				\
    514         (var) = freemempos;                             \
    515         memset((char *)(var), 0, ((np) * PAGE_SIZE));   \
    516         freemempos += (np) * PAGE_SIZE;
    517 #endif
    518 
    519 	loop1 = 0;
    520 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    521 		/* Are we 16KB aligned for an L1 ? */
    522 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    523 		    && kernel_l1pt.pv_pa == 0) {
    524 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    525 		} else {
    526 			valloc_pages(kernel_pt_table[loop1],
    527 			    L2_TABLE_SIZE / PAGE_SIZE);
    528 			++loop1;
    529 		}
    530 	}
    531 
    532 	/* This should never be able to happen but better confirm that. */
    533 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    534 		panic("initarm: Failed to align the kernel page directory");
    535 
    536 	/*
    537 	 * Allocate a page for the system page.
    538 	 * This page will just contain the system vectors and can be
    539 	 * shared by all processes.
    540 	 */
    541 	alloc_pages(systempage.pv_pa, 1);
    542 
    543 	/* Allocate stacks for all modes */
    544 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    545 	valloc_pages(abtstack, ABT_STACK_SIZE);
    546 	valloc_pages(undstack, UND_STACK_SIZE);
    547 	valloc_pages(kernelstack, UPAGES);
    548 
    549 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    550 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    551 	valloc_pages(minidataclean, 1);
    552 
    553 #ifdef VERBOSE_INIT_ARM
    554 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    555 	    irqstack.pv_va);
    556 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    557 	    abtstack.pv_va);
    558 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    559 	    undstack.pv_va);
    560 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    561 	    kernelstack.pv_va);
    562 #endif
    563 
    564 	/*
    565 	 * XXX Defer this to later so that we can reclaim the memory
    566 	 * XXX used by the RedBoot page tables.
    567 	 */
    568 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    569 
    570 	/*
    571 	 * Ok we have allocated physical pages for the primary kernel
    572 	 * page tables
    573 	 */
    574 
    575 #ifdef VERBOSE_INIT_ARM
    576 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    577 #endif
    578 
    579 	/*
    580 	 * Now we start construction of the L1 page table
    581 	 * We start by mapping the L2 page tables into the L1.
    582 	 * This means that we can replace L1 mappings later on if necessary
    583 	 */
    584 	l1pagetable = kernel_l1pt.pv_pa;
    585 
    586 	/* Map the L2 pages tables in the L1 page table */
    587 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    588 	    &kernel_pt_table[KERNEL_PT_SYS]);
    589 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    590 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    591 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    592 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    593 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    594 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    595 
    596 	/* update the top of the kernel VM */
    597 	pmap_curmaxkvaddr =
    598 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    599 
    600 	pmap_link_l2pt(l1pagetable, IXP425_IO_VBASE,
    601 	    &kernel_pt_table[KERNEL_PT_IO]);
    602 
    603 #ifdef VERBOSE_INIT_ARM
    604 	printf("Mapping kernel\n");
    605 #endif
    606 
    607 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    608 	{
    609 		extern char etext[], _end[];
    610 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    611 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    612 		u_int logical;
    613 
    614 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    615 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    616 
    617 		logical = 0x00200000;	/* offset of kernel in RAM */
    618 
    619 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    620 		    physical_start + logical, textsize,
    621 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    622 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    623 		    physical_start + logical, totalsize - textsize,
    624 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    625 	}
    626 
    627 #ifdef VERBOSE_INIT_ARM
    628 	printf("Constructing L2 page tables\n");
    629 #endif
    630 
    631 	/* Map the stack pages */
    632 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    633 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    634 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    635 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    636 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    637 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    638 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    639 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    640 
    641 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    642 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    643 
    644 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    645 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    646 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    647 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    648 	}
    649 
    650 	/* Map the Mini-Data cache clean area. */
    651 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    652 	    minidataclean.pv_pa);
    653 
    654 	/* Map the vector page. */
    655 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    656 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    657 
    658         /*
    659          * Map the IXP425 registers
    660          */
    661 	pmap_devmap_bootstrap(l1pagetable, nslu2_devmap);
    662 
    663 	/*
    664 	 * Give the XScale global cache clean code an appropriately
    665 	 * sized chunk of unmapped VA space starting at 0xff000000
    666 	 * (our device mappings end before this address).
    667 	 */
    668 	xscale_cache_clean_addr = 0xff000000U;
    669 
    670 	/*
    671 	 * Now we have the real page tables in place so we can switch to them.
    672 	 * Once this is done we will be running with the REAL kernel page
    673 	 * tables.
    674 	 */
    675 
    676 	/*
    677 	 * Update the physical_freestart/physical_freeend/free_pages
    678 	 * variables.
    679 	 */
    680 	{
    681 		extern char _end[];
    682 
    683 		physical_freestart = physical_start +
    684 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    685 		     KERNEL_BASE);
    686 		physical_freeend = physical_end;
    687 		free_pages =
    688 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    689 	}
    690 
    691 	/* Switch tables */
    692 #ifdef VERBOSE_INIT_ARM
    693 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    694 	       physical_freestart, free_pages, free_pages);
    695 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    696 #endif
    697 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    698 	cpu_setttb(kernel_l1pt.pv_pa, true);
    699 	cpu_tlb_flushID();
    700 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    701 
    702 	/*
    703 	 * Moved from cpu_startup() as data_abort_handler() references
    704 	 * this during uvm init
    705 	 */
    706 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    707 
    708 #ifdef VERBOSE_INIT_ARM
    709 	printf("bootstrap done.\n");
    710 #endif
    711 
    712 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    713 
    714 	/*
    715 	 * Pages were allocated during the secondary bootstrap for the
    716 	 * stacks for different CPU modes.
    717 	 * We must now set the r13 registers in the different CPU modes to
    718 	 * point to these stacks.
    719 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    720 	 * of the stack memory.
    721 	 */
    722 #ifdef VERBOSE_INIT_ARM
    723 	printf("init subsystems: stacks ");
    724 #endif
    725 
    726 	set_stackptr(PSR_IRQ32_MODE,
    727 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    728 	set_stackptr(PSR_ABT32_MODE,
    729 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    730 	set_stackptr(PSR_UND32_MODE,
    731 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    732 
    733 	/*
    734 	 * Well we should set a data abort handler.
    735 	 * Once things get going this will change as we will need a proper
    736 	 * handler.
    737 	 * Until then we will use a handler that just panics but tells us
    738 	 * why.
    739 	 * Initialisation of the vectors will just panic on a data abort.
    740 	 * This just fills in a slightly better one.
    741 	 */
    742 #ifdef VERBOSE_INIT_ARM
    743 	printf("vectors ");
    744 #endif
    745 	data_abort_handler_address = (u_int)data_abort_handler;
    746 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    747 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    748 
    749 	/* Initialise the undefined instruction handlers */
    750 #ifdef VERBOSE_INIT_ARM
    751 	printf("undefined ");
    752 #endif
    753 	undefined_init();
    754 
    755 	/* Load memory into UVM. */
    756 #ifdef VERBOSE_INIT_ARM
    757 	printf("page ");
    758 #endif
    759 	uvm_md_init();
    760 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    761 	    atop(physical_freestart), atop(physical_freeend),
    762 	    VM_FREELIST_DEFAULT);
    763 
    764 	/* Boot strap pmap telling it where managed kernel virtual memory is */
    765 #ifdef VERBOSE_INIT_ARM
    766 	printf("pmap ");
    767 #endif
    768 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    769 
    770 	/* Setup the IRQ system */
    771 #ifdef VERBOSE_INIT_ARM
    772 	printf("irq ");
    773 #endif
    774 	ixp425_intr_init();
    775 #ifdef VERBOSE_INIT_ARM
    776 	printf("\nAll initialization done!\nNow Starting NetBSD, Here we go!\n");
    777 #endif
    778 
    779 #ifdef BOOTHOWTO
    780 	boothowto = BOOTHOWTO;
    781 #endif
    782 
    783 #ifdef DDB
    784 	db_machine_init();
    785 	if (boothowto & RB_KDB)
    786 		Debugger();
    787 #endif
    788 
    789 	/* We return the new stack pointer address */
    790 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
    791 }
    792 
    793 /*
    794  * consinit
    795  */
    796 void
    797 consinit(void)
    798 {
    799 	static int consinit_called;
    800 	static const bus_addr_t addrs[2] = {
    801 		IXP425_UART0_HWBASE, IXP425_UART1_HWBASE
    802 	};
    803 
    804 	if (consinit_called != 0)
    805 		return;
    806 
    807 	consinit_called = 1;
    808 
    809 	pmap_devmap_register(nslu2_devmap);
    810 
    811 	if (comcnattach(&ixp425_a4x_bs_tag, addrs[comcnunit],
    812 	    comcnspeed, IXP425_UART_FREQ, COM_TYPE_PXA2x0, comcnmode))
    813 		panic("can't init serial console (UART%d)", comcnunit);
    814 }
    815