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