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