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