Home | History | Annotate | Line # | Download | only in armadillo
armadillo9_machdep.c revision 1.3
      1 /*	$NetBSD: armadillo9_machdep.c,v 1.3 2006/02/06 14:03:22 hamajima Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Based on code written by Jason R. Thorpe and Steve C. Woodford for
      8  * Wasabi Systems, Inc.
      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 for the NetBSD Project by
     21  *	Wasabi Systems, Inc.
     22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     27  * 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 WASABI SYSTEMS, INC
     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 /*
     40  * Copyright (c) 1997,1998 Mark Brinicombe.
     41  * Copyright (c) 1997,1998 Causality Limited.
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Mark Brinicombe
     55  *	for the NetBSD Project.
     56  * 4. The name of the company nor the name of the author may be used to
     57  *    endorse or promote products derived from this software without specific
     58  *    prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     63  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  * Machine dependant functions for kernel setup for Armadillo.
     73  */
     74 
     75 /*	Armadillo-9 physical memory map
     76 	0000 0000 - 0fff ffff	reserved
     77 	1000 0000 - 1000 000f	I/O Control Register
     78 	1000 0010 - 11dd ffff	reserved
     79 	1200 0000 - 1200 ffff	PC/104 I/O space (8bit)
     80 	1201 0000 - 12ff ffff	reserved
     81 	1300 0000 - 13ff ffff	PC/104 Memory space (8bit)
     82 	1400 0000 - 1fff ffff	reserved
     83 	2000 0000 - 21ff ffff	reserved
     84 	2200 0000 - 2200 ffff	PC/104 I/O space (16bit)
     85 	2201 0000 - 22ff ffff	reserved
     86 	2300 0000 - 23ff ffff	PC/104 Memory space (16bit)
     87 	2400 0000 - 2fff ffff	reserved
     88 	3000 0000 - 3fff ffff	reserved
     89 	4000 0000 - 43ff ffff	Compact Flash I/O space
     90 	4400 0000 - 47ff ffff	reserved
     91 	4800 0000 - 4bff ffff	Compact Flash Attribute space
     92 	4c00 0000 - 4fff ffff	Compact Flash memory space
     93 	5000 0000 - 5fff ffff	reserved
     94 	6000 0000 - 607f ffff	Flash Memory (8MByte)
     95 	6080 0000 - 6fff ffff	reserved
     96 	7000 0000 - 7fff ffff	reserved
     97 	8000 0000 - 8008 ffff	EP9315 Internal Register (AHB)
     98 	8009 0000 - 8009 3fff	Internal Boot ROM (16kByte)
     99 	8009 4000 - 8009 ffff	reserved
    100 	800a 0000 - 800f ffff	EP9315 Internal Register (AHB)
    101 	8010 0000 - 807f ffff	reserved
    102 	8080 0000 - 8094 ffff	EP9315 Internal Register (APB)
    103 	8095 0000 - 8fff ffff	reserved
    104 	9000 0000 - bfff ffff	reserved
    105 	c000 0000 - c1ff ffff	SDRAM (32MByte)
    106 	c200 0000 - c3ff ffff	reserved
    107 	c400 0000 - c5ff ffff	SDRAM (32MByte)
    108 	c600 0000 - cfff ffff	reserved
    109 	d000 0000 - ffff ffff	reserved
    110 */
    111 
    112 #include <sys/cdefs.h>
    113 __KERNEL_RCSID(0, "$NetBSD: armadillo9_machdep.c,v 1.3 2006/02/06 14:03:22 hamajima Exp $");
    114 
    115 #include "opt_ddb.h"
    116 #include "opt_kgdb.h"
    117 #include "opt_pmap_debug.h"
    118 
    119 #include <sys/param.h>
    120 #include <sys/device.h>
    121 #include <sys/systm.h>
    122 #include <sys/kernel.h>
    123 #include <sys/exec.h>
    124 #include <sys/proc.h>
    125 #include <sys/msgbuf.h>
    126 #include <sys/reboot.h>
    127 #include <sys/termios.h>
    128 #include <sys/ksyms.h>
    129 
    130 #include <uvm/uvm_extern.h>
    131 
    132 #include <dev/cons.h>
    133 
    134 #include <machine/db_machdep.h>
    135 #include <ddb/db_sym.h>
    136 #include <ddb/db_extern.h>
    137 
    138 #ifdef	ARMADILLO210
    139 #define	DRAM_BLOCKS	4
    140 #endif
    141 #include <machine/bootconfig.h>
    142 #include <machine/bus.h>
    143 #include <machine/cpu.h>
    144 #include <machine/frame.h>
    145 #include <arm/undefined.h>
    146 
    147 #include <arm/arm32/machdep.h>
    148 
    149 #include <arm/ep93xx/ep93xxreg.h>
    150 #include <arm/ep93xx/ep93xxvar.h>
    151 
    152 #include "epwdog.h"
    153 #if NEPWDOG > 0
    154 #include <arm/ep93xx/epwdogvar.h>
    155 #endif
    156 #include <arm/ep93xx/epwdogreg.h>
    157 
    158 #include <dev/ic/comreg.h>
    159 #include <dev/ic/comvar.h>
    160 
    161 #include "epcom.h"
    162 #if NEPCOM > 0
    163 #include <arm/ep93xx/epcomvar.h>
    164 #endif
    165 
    166 #include "isa.h"
    167 #if NISA > 0
    168 #include <dev/isa/isareg.h>
    169 #include <dev/isa/isavar.h>
    170 #endif
    171 
    172 #include <machine/isa_machdep.h>
    173 
    174 #include <evbarm/armadillo/armadillo9reg.h>
    175 
    176 #include "opt_ipkdb.h"
    177 #include "ksyms.h"
    178 
    179 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    180 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    181 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    182 
    183 /*
    184  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    185  * Core-logic registers and I/O mappings occupy 0xf0000000 - 0xffffffff
    186  */
    187 #define KERNEL_VM_SIZE		0x0c000000
    188 
    189 /*
    190  * Address to call from cpu_reset() to reset the machine.
    191  * This is machine architecture dependant as it varies depending
    192  * on where the ROM appears when you turn the MMU off.
    193  */
    194 
    195 u_int cpu_reset_address = 0x80090000;
    196 
    197 /* Define various stack sizes in pages */
    198 #define IRQ_STACK_SIZE	8
    199 #define ABT_STACK_SIZE	8
    200 #ifdef IPKDB
    201 #define UND_STACK_SIZE	16
    202 #else
    203 #define UND_STACK_SIZE	8
    204 #endif
    205 
    206 BootConfig bootconfig;	/* Boot config storage */
    207 char *boot_args = NULL;
    208 char *boot_file = NULL;
    209 
    210 vm_offset_t physical_start;
    211 vm_offset_t physical_freestart;
    212 vm_offset_t physical_freeend;
    213 vm_offset_t physical_freeend_low;
    214 vm_offset_t physical_end;
    215 u_int free_pages;
    216 int physmem = 0;
    217 
    218 /* Physical and virtual addresses for some global pages */
    219 pv_addr_t systempage;
    220 pv_addr_t irqstack;
    221 pv_addr_t undstack;
    222 pv_addr_t abtstack;
    223 pv_addr_t kernelstack;
    224 
    225 vm_offset_t msgbufphys;
    226 
    227 static struct arm32_dma_range armadillo9_dma_ranges[4];
    228 
    229 #if NISA > 0
    230 extern void isa_armadillo9_init(u_int, u_int);
    231 #endif
    232 
    233 extern u_int data_abort_handler_address;
    234 extern u_int prefetch_abort_handler_address;
    235 extern u_int undefined_handler_address;
    236 
    237 #ifdef PMAP_DEBUG
    238 extern int pmap_debug_level;
    239 #endif
    240 
    241 #define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
    242 
    243 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    244 #define	KERNEL_PT_KERNEL_NUM	4
    245 					/* L2 tables for mapping kernel VM */
    246 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    247 
    248 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    249 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    250 
    251 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    252 
    253 struct user *proc0paddr;
    254 
    255 /* Prototypes */
    256 
    257 void	consinit(void);
    258 /*
    259  * Define the default console speed for the machine.
    260  */
    261 #ifndef CONSPEED
    262 #define CONSPEED B115200
    263 #endif /* ! CONSPEED */
    264 
    265 #ifndef CONMODE
    266 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    267 #endif
    268 
    269 int comcnspeed = CONSPEED;
    270 int comcnmode = CONMODE;
    271 
    272 #if KGDB
    273 #ifndef KGDB_DEVNAME
    274 #error Must define KGDB_DEVNAME
    275 #endif
    276 const char kgdb_devname[] = KGDB_DEVNAME;
    277 
    278 #ifndef KGDB_DEVADDR
    279 #error Must define KGDB_DEVADDR
    280 #endif
    281 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    282 
    283 #ifndef KGDB_DEVRATE
    284 #define KGDB_DEVRATE	CONSPEED
    285 #endif
    286 int kgdb_devrate = KGDB_DEVRATE;
    287 
    288 #ifndef KGDB_DEVMODE
    289 #define KGDB_DEVMODE	CONMODE
    290 #endif
    291 int kgdb_devmode = KGDB_DEVMODE;
    292 #endif /* KGDB */
    293 
    294 /*
    295  * void cpu_reboot(int howto, char *bootstr)
    296  *
    297  * Reboots the system
    298  *
    299  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    300  * then reset the CPU.
    301  */
    302 void
    303 cpu_reboot(int howto, char *bootstr)
    304 {
    305 	/*
    306 	 * If we are still cold then hit the air brakes
    307 	 * and crash to earth fast
    308 	 */
    309 	if (cold) {
    310 		doshutdownhooks();
    311 		printf("\r\n");
    312 		printf("The operating system has halted.\r\n");
    313 		printf("Please press any key to reboot.\r\n");
    314 		cngetc();
    315 		printf("\r\nrebooting...\r\n");
    316 		goto reset;
    317 	}
    318 
    319 	/* Disable console buffering */
    320 
    321 	/*
    322 	 * If RB_NOSYNC was not specified sync the discs.
    323 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    324 	 * unmount.  It looks like syslogd is getting woken up only to find
    325 	 * that it cannot page part of the binary in as the filesystem has
    326 	 * been unmounted.
    327 	 */
    328 	if (!(howto & RB_NOSYNC))
    329 		bootsync();
    330 
    331 	/* Say NO to interrupts */
    332 	splhigh();
    333 
    334 	/* Do a dump if requested. */
    335 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    336 		dumpsys();
    337 
    338 	/* Run any shutdown hooks */
    339 	doshutdownhooks();
    340 
    341 	/* Make sure IRQ's are disabled */
    342 	IRQdisable;
    343 
    344 	if (howto & RB_HALT) {
    345 		printf("\r\n");
    346 		printf("The operating system has halted.\r\n");
    347 		printf("Please press any key to reboot.\r\n");
    348 		cngetc();
    349 	}
    350 
    351 	printf("\r\nrebooting...\r\n");
    352  reset:
    353 	/*
    354 	 * Make really really sure that all interrupts are disabled,
    355 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    356 	 */
    357 	(void) disable_interrupts(I32_bit|F32_bit);
    358 #if NEPWDOG > 0
    359 	epwdog_reset();
    360 #else
    361 	{
    362 	u_int32_t ctrl = EP93XX_APB_VBASE + EP93XX_APB_WDOG + EP93XX_WDOG_Ctrl;
    363 	u_int32_t val = EP93XX_WDOG_ENABLE;
    364 	__asm volatile (
    365 		"str %1, [%0]\n"
    366 		:
    367 		: "r" (ctrl), "r" (val)
    368 	);
    369 	}
    370 #endif
    371 	for (;;);
    372 }
    373 
    374 /* Static device mappings. */
    375 static const struct pmap_devmap armadillo9_devmap[] = {
    376     {
    377 	EP93XX_AHB_VBASE,
    378 	EP93XX_AHB_HWBASE,
    379 	EP93XX_AHB_SIZE,
    380 	VM_PROT_READ|VM_PROT_WRITE,
    381 	PTE_NOCACHE,
    382     },
    383 
    384     {
    385 	EP93XX_APB_VBASE,
    386 	EP93XX_APB_HWBASE,
    387 	EP93XX_APB_SIZE,
    388 	VM_PROT_READ|VM_PROT_WRITE,
    389 	PTE_NOCACHE,
    390     },
    391 
    392     {
    393 	EP93XX_PCMCIA0_VBASE,
    394 	EP93XX_PCMCIA0_HWBASE,
    395 	EP93XX_PCMCIA_SIZE,
    396 	VM_PROT_READ|VM_PROT_WRITE,
    397 	PTE_NOCACHE,
    398     },
    399 
    400 	/*
    401 	 * IO8 and IO16 space *must* be mapped contiguously with
    402 	 * IO8_VA == IO16_VA - 64 Mbytes.  ISA busmap driver depends
    403 	 * on that!
    404 	 */
    405     {
    406 	ARMADILLO9_IO8_VBASE,
    407 	ARMADILLO9_IO8_HWBASE,
    408 	ARMADILLO9_IO8_SIZE,
    409 	VM_PROT_READ|VM_PROT_WRITE,
    410 	PTE_NOCACHE,
    411     },
    412 
    413     {
    414 	ARMADILLO9_IO16_VBASE,
    415 	ARMADILLO9_IO16_HWBASE,
    416 	ARMADILLO9_IO16_SIZE,
    417 	VM_PROT_READ|VM_PROT_WRITE,
    418 	PTE_NOCACHE,
    419     },
    420 
    421    {
    422 	0,
    423 	0,
    424 	0,
    425 	0,
    426 	0,
    427     }
    428 };
    429 
    430 /*
    431  * u_int initarm(...)
    432  *
    433  * Initial entry point on startup. This gets called before main() is
    434  * entered.
    435  * It should be responsible for setting up everything that must be
    436  * in place when main is called.
    437  * This includes
    438  *   Taking a copy of the boot configuration structure.
    439  *   Initialising the physical console so characters can be printed.
    440  *   Setting up page tables for the kernel
    441  *   Initialising interrupt controllers to a sane default state
    442  */
    443 u_int
    444 initarm(void *arg)
    445 {
    446 	int loop;
    447 	int loop1;
    448 	u_int l1pagetable;
    449 	pv_addr_t kernel_l1pt;
    450 
    451 	/*
    452 	 * Since we map the on-board devices VA==PA, and the kernel
    453 	 * is running VA==PA, it's possible for us to initialize
    454 	 * the console now.
    455 	 */
    456 	consinit();
    457 
    458 #ifdef VERBOSE_INIT_ARM
    459 	/* Talk to the user */
    460 	printf("\nNetBSD/armadillo9 booting ...\n");
    461 #endif
    462 
    463 	/*
    464 	 * Heads up ... Setup the CPU / MMU / TLB functions
    465 	 */
    466 	if (set_cpufuncs())
    467 		panic("cpu not recognized!");
    468 
    469 #ifdef VERBOSE_INIT_ARM
    470 	printf("initarm: Configuring system ...\n");
    471 #endif
    472 
    473 	/* Fake bootconfig structure for the benefit of pmap.c */
    474 	/* XXX must make the memory description h/w independant */
    475 #ifdef	ARMADILLO210
    476 	bootconfig.dramblocks = 4;
    477 	bootconfig.dram[0].address = 0xc0000000UL;
    478 	bootconfig.dram[0].pages = 0x800000UL / PAGE_SIZE;
    479 	bootconfig.dram[1].address = 0xc1000000UL;
    480 	bootconfig.dram[1].pages = 0x800000UL / PAGE_SIZE;
    481 	bootconfig.dram[2].address = 0xc4000000UL;
    482 	bootconfig.dram[2].pages = 0x800000UL / PAGE_SIZE;
    483 	bootconfig.dram[3].address = 0xc5000000UL;
    484 	bootconfig.dram[3].pages = 0x800000UL / PAGE_SIZE;
    485 #else	/* ARMADILLO9 */
    486 	bootconfig.dramblocks = 2;
    487 	bootconfig.dram[0].address = 0xc0000000UL;
    488 	bootconfig.dram[0].pages = 0x2000000UL / PAGE_SIZE;
    489 	bootconfig.dram[1].address = 0xc4000000UL;
    490 	bootconfig.dram[1].pages = 0x2000000UL / PAGE_SIZE;
    491 #endif
    492 
    493 	/*
    494 	 * Set up the variables that define the availablilty of
    495 	 * physical memory.  For now, we're going to set
    496 	 * physical_freestart to 0xc0200000 (where the kernel
    497 	 * was loaded), and allocate the memory we need downwards.
    498 	 * If we get too close to the L1 table that we set up, we
    499 	 * will panic.  We will update physical_freestart and
    500 	 * physical_freeend later to reflect what pmap_bootstrap()
    501 	 * wants to see.
    502 	 *
    503 	 * XXX pmap_bootstrap() needs an enema.
    504 	 */
    505 	physical_start = bootconfig.dram[0].address;
    506 	physical_end = bootconfig.dram[0].address
    507 			+ (bootconfig.dram[0].pages * PAGE_SIZE);
    508 
    509 	physical_freestart = 0xc0018000UL;
    510 	physical_freeend = 0xc0200000UL;
    511 
    512 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    513 
    514 #ifdef VERBOSE_INIT_ARM
    515 	/* Tell the user about the memory */
    516 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    517 	    physical_start, physical_end - 1);
    518 #endif
    519 
    520 	/*
    521 	 * Okay, the kernel starts 2MB in from the bottom of physical
    522 	 * memory.  We are going to allocate our bootstrap pages downwards
    523 	 * from there.
    524 	 *
    525 	 * We need to allocate some fixed page tables to get the kernel
    526 	 * going.  We allocate one page directory and a number of page
    527 	 * tables and store the physical addresses in the kernel_pt_table
    528 	 * array.
    529 	 *
    530 	 * The kernel page directory must be on a 16K boundary.  The page
    531 	 * tables must be on 4K bounaries.  What we do is allocate the
    532 	 * page directory on the first 16K boundary that we encounter, and
    533 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    534 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    535 	 * least one 16K aligned region.
    536 	 */
    537 
    538 #ifdef VERBOSE_INIT_ARM
    539 	printf("Allocating page tables\n");
    540 #endif
    541 
    542 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    543 
    544 #ifdef VERBOSE_INIT_ARM
    545 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    546 	       physical_freestart, free_pages, free_pages);
    547 #endif
    548 
    549 	/* Define a macro to simplify memory allocation */
    550 #define	valloc_pages(var, np)				\
    551 	alloc_pages((var).pv_pa, (np));			\
    552 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    553 
    554 #define alloc_pages(var, np)				\
    555 	physical_freeend -= ((np) * PAGE_SIZE);		\
    556 	if (physical_freeend < physical_freestart)	\
    557 		panic("initarm: out of memory");	\
    558 	(var) = physical_freeend;			\
    559 	free_pages -= (np);				\
    560 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    561 
    562 	loop1 = 0;
    563 	kernel_l1pt.pv_pa = 0;
    564 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    565 		/* Are we 16KB aligned for an L1 ? */
    566 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    567 		    && kernel_l1pt.pv_pa == 0) {
    568 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    569 		} else {
    570 			valloc_pages(kernel_pt_table[loop1],
    571 			    L2_TABLE_SIZE / PAGE_SIZE);
    572 			++loop1;
    573 		}
    574 	}
    575 
    576 	/* This should never be able to happen but better confirm that. */
    577 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    578 		panic("initarm: Failed to align the kernel page directory");
    579 
    580 	/*
    581 	 * Allocate a page for the system vectors page
    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 #ifdef VERBOSE_INIT_ARM
    592 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    593 	    irqstack.pv_va);
    594 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    595 	    abtstack.pv_va);
    596 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    597 	    undstack.pv_va);
    598 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    599 	    kernelstack.pv_va);
    600 #endif
    601 
    602 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    603 
    604 	/*
    605 	 * Ok we have allocated physical pages for the primary kernel
    606 	 * page tables.  Save physical_freeend for when we give whats left
    607 	 * of memory below 2Mbyte to UVM.
    608 	 */
    609 
    610 	physical_freeend_low = physical_freeend;
    611 
    612 #ifdef VERBOSE_INIT_ARM
    613 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    614 #endif
    615 
    616 	/*
    617 	 * Now we start construction of the L1 page table
    618 	 * We start by mapping the L2 page tables into the L1.
    619 	 * This means that we can replace L1 mappings later on if necessary
    620 	 */
    621 	l1pagetable = kernel_l1pt.pv_pa;
    622 
    623 	/* Map the L2 pages tables in the L1 page table */
    624 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    625 	    &kernel_pt_table[KERNEL_PT_SYS]);
    626 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    627 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    628 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    629 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    630 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    631 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    632 
    633 	/* update the top of the kernel VM */
    634 	pmap_curmaxkvaddr =
    635 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    636 
    637 #ifdef VERBOSE_INIT_ARM
    638 	printf("Mapping kernel\n");
    639 #endif
    640 
    641 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    642 	{
    643 		extern char etext[], _end[];
    644 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    645 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    646 		u_int logical;
    647 
    648 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    649 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    650 
    651 		logical = 0x00200000;	/* offset of kernel in RAM */
    652 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    653 		    physical_start + logical, textsize,
    654 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    655 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    656 		    physical_start + logical, totalsize - textsize,
    657 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    658 	}
    659 
    660 #ifdef VERBOSE_INIT_ARM
    661 	printf("Constructing L2 page tables\n");
    662 #endif
    663 
    664 	/* Map the stack pages */
    665 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    666 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    667 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    668 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    669 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    670 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    671 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    672 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    673 
    674 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    675 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    676 
    677 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    678 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    679 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    680 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    681 	}
    682 
    683 	/* Map the vector page. */
    684 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    685 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    686 
    687 	/* Map the statically mapped devices. */
    688 	pmap_devmap_bootstrap(l1pagetable, armadillo9_devmap);
    689 
    690 	/*
    691 	 * Update the physical_freestart/physical_freeend/free_pages
    692 	 * variables.
    693 	 */
    694 	{
    695 		extern char _end[];
    696 
    697 		physical_freestart = physical_start +
    698 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    699 		     KERNEL_BASE);
    700 		physical_freeend = physical_end;
    701 		free_pages =
    702 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    703 	}
    704 
    705 	/*
    706 	 * Now we have the real page tables in place so we can switch to them.
    707 	 * Once this is done we will be running with the REAL kernel page
    708 	 * tables.
    709 	 */
    710 
    711 	/* Switch tables */
    712 #ifdef VERBOSE_INIT_ARM
    713 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    714 	       physical_freestart, free_pages, free_pages);
    715 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    716 #endif
    717 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    718 	setttb(kernel_l1pt.pv_pa);
    719 	cpu_tlb_flushID();
    720 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    721 
    722 	/*
    723 	 * Moved from cpu_startup() as data_abort_handler() references
    724 	 * this during uvm init
    725 	 */
    726 	proc0paddr = (struct user *)kernelstack.pv_va;
    727 	lwp0.l_addr = proc0paddr;
    728 
    729 #ifdef VERBOSE_INIT_ARM
    730 	printf("done!\n");
    731 #endif
    732 
    733 #ifdef VERBOSE_INIT_ARM
    734 	printf("bootstrap done.\n");
    735 #endif
    736 
    737 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    738 
    739 	/*
    740 	 * Pages were allocated during the secondary bootstrap for the
    741 	 * stacks for different CPU modes.
    742 	 * We must now set the r13 registers in the different CPU modes to
    743 	 * point to these stacks.
    744 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    745 	 * of the stack memory.
    746 	 */
    747 #ifdef VERBOSE_INIT_ARM
    748 	printf("init subsystems: stacks ");
    749 #endif
    750 
    751 	set_stackptr(PSR_IRQ32_MODE,
    752 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    753 	set_stackptr(PSR_ABT32_MODE,
    754 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    755 	set_stackptr(PSR_UND32_MODE,
    756 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    757 
    758 	/*
    759 	 * Well we should set a data abort handler.
    760 	 * Once things get going this will change as we will need a proper
    761 	 * handler.
    762 	 * Until then we will use a handler that just panics but tells us
    763 	 * why.
    764 	 * Initialisation of the vectors will just panic on a data abort.
    765 	 * This just fills in a slightly better one.
    766 	 */
    767 #ifdef VERBOSE_INIT_ARM
    768 	printf("vectors ");
    769 #endif
    770 	data_abort_handler_address = (u_int)data_abort_handler;
    771 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    772 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    773 
    774 	/* Initialise the undefined instruction handlers */
    775 #ifdef VERBOSE_INIT_ARM
    776 	printf("undefined ");
    777 #endif
    778 	undefined_init();
    779 
    780 	/* Load memory into UVM. */
    781 #ifdef VERBOSE_INIT_ARM
    782 	printf("page ");
    783 #endif
    784 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    785 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    786 	    atop(physical_freestart), atop(physical_freeend),
    787 	    VM_FREELIST_DEFAULT);
    788 	uvm_page_physload(atop(0xc0000000), atop(physical_freeend_low),
    789 	    atop(0xc0000000), atop(physical_freeend_low),
    790 	    VM_FREELIST_DEFAULT);
    791 	physmem = bootconfig.dram[0].pages;
    792 	for (loop = 1; loop < bootconfig.dramblocks; ++loop) {
    793 		size_t start = bootconfig.dram[loop].address;
    794 		size_t size = bootconfig.dram[loop].pages * PAGE_SIZE;
    795 		uvm_page_physload(atop(start), atop(start + size),
    796 				  atop(start), atop(start + size),
    797 				  VM_FREELIST_DEFAULT);
    798 		physmem += bootconfig.dram[loop].pages;
    799 	}
    800 
    801 	/* Boot strap pmap telling it where the kernel page table is */
    802 #ifdef VERBOSE_INIT_ARM
    803 	printf("pmap ");
    804 #endif
    805 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    806 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    807 
    808 	/* Setup the IRQ system */
    809 #ifdef VERBOSE_INIT_ARM
    810 	printf("irq ");
    811 #endif
    812 	ep93xx_intr_init();
    813 #if NISA > 0
    814 	isa_intr_init();
    815 
    816 #ifdef VERBOSE_INIT_ARM
    817 	printf("isa ");
    818 #endif
    819 	isa_armadillo9_init(ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAIO,
    820 		ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAMEM);
    821 #endif
    822 
    823 #ifdef VERBOSE_INIT_ARM
    824 	printf("done.\n");
    825 #endif
    826 
    827 #ifdef BOOTHOWTO
    828 	boothowto = BOOTHOWTO;
    829 #endif
    830 
    831 #ifdef IPKDB
    832 	/* Initialise ipkdb */
    833 	ipkdb_init();
    834 	if (boothowto & RB_KDB)
    835 		ipkdb_connect(0);
    836 #endif
    837 
    838 #if NKSYMS || defined(DDB) || defined(LKM)
    839 	/* Firmware doesn't load symbols. */
    840 	ksyms_init(0, NULL, NULL);
    841 #endif
    842 
    843 #ifdef DDB
    844 	db_machine_init();
    845 	if (boothowto & RB_KDB)
    846 		Debugger();
    847 #endif
    848 
    849 	/* We return the new stack pointer address */
    850 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    851 }
    852 
    853 void
    854 consinit(void)
    855 {
    856 	static int consinit_called;
    857 #if NEPCOM > 0
    858 	bus_space_handle_t ioh;
    859 #endif
    860 
    861 	if (consinit_called != 0)
    862 		return;
    863 
    864 	consinit_called = 1;
    865 
    866 	/*
    867 	 * Console devices are already mapped in VA.  Our devmap reflects
    868 	 * this, so register it now so drivers can map the console
    869 	 * device.
    870 	 */
    871 	pmap_devmap_register(armadillo9_devmap);
    872 #if 0
    873 	isa_armadillo9_init(ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAIO,
    874 		ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAMEM);
    875 
    876         if (comcnattach(&isa_io_bs_tag, 0x3e8, comcnspeed,
    877             COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    878         {
    879                 panic("can't init serial console");
    880         }
    881 #endif
    882 
    883 #if NEPCOM > 0
    884 #ifndef CONUNIT
    885 #define	CONUNIT	0
    886 #endif
    887 #if CONUNIT == 0
    888 #define	UART	EP93XX_APB_UART1
    889 #elif CONUNIT == 1
    890 #define	UART	EP93XX_APB_UART2
    891 #elif CONUNIT == 2
    892 #define	UART	EP93XX_APB_UART3
    893 #else
    894 #error	CONUNIT must less than 3.
    895 #endif
    896 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + UART,
    897 		EP93XX_APB_UART_SIZE, 0, &ioh);
    898         if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + UART,
    899 		ioh, comcnspeed, comcnmode))
    900 	{
    901 		panic("can't init serial console");
    902 	}
    903 #else
    904 	panic("serial console not configured");
    905 #endif
    906 #if KGDB
    907 #if NEPCOM > 0
    908 	if (strcmp(kgdb_devname, "epcom") == 0) {
    909 		com_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
    910 			kgdb_devmode);
    911 	}
    912 #endif	/* NEPCOM > 0 */
    913 #endif	/* KGDB */
    914 }
    915 
    916 
    917 bus_dma_tag_t
    918 ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    919 {
    920 	int i;
    921 	struct arm32_bus_dma_tag *dmat;
    922 
    923 	for (i = 0; i < bootconfig.dramblocks; i++) {
    924 		armadillo9_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    925 		armadillo9_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    926 		armadillo9_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    927 			PAGE_SIZE;
    928 	}
    929 
    930 	dmat = dma_tag_template;
    931 
    932 	dmat->_ranges = armadillo9_dma_ranges;
    933 	dmat->_nranges = bootconfig.dramblocks;
    934 
    935 	return dmat;
    936 }
    937