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