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