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