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