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