Home | History | Annotate | Line # | Download | only in armadillo
armadillo9_machdep.c revision 1.8
      1 /*	$NetBSD: armadillo9_machdep.c,v 1.8 2006/05/17 04:22:46 mrg 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.8 2006/05/17 04:22:46 mrg 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 <net/if.h>
    131 #include <net/if_ether.h>
    132 
    133 #include <uvm/uvm_extern.h>
    134 
    135 #include <dev/cons.h>
    136 
    137 #include <machine/db_machdep.h>
    138 #include <ddb/db_sym.h>
    139 #include <ddb/db_extern.h>
    140 
    141 #define	DRAM_BLOCKS	4
    142 #include <machine/bootconfig.h>
    143 #include <machine/autoconf.h>
    144 #include <machine/bus.h>
    145 #include <machine/cpu.h>
    146 #include <machine/frame.h>
    147 #include <arm/undefined.h>
    148 
    149 #include <arm/arm32/machdep.h>
    150 
    151 #include <arm/ep93xx/ep93xxreg.h>
    152 #include <arm/ep93xx/ep93xxvar.h>
    153 
    154 #include "epwdog.h"
    155 #if NEPWDOG > 0
    156 #include <arm/ep93xx/epwdogvar.h>
    157 #endif
    158 #include <arm/ep93xx/epwdogreg.h>
    159 
    160 #include <dev/ic/comreg.h>
    161 #include <dev/ic/comvar.h>
    162 
    163 #include "epcom.h"
    164 #if NEPCOM > 0
    165 #include <arm/ep93xx/epcomvar.h>
    166 #endif
    167 
    168 #include "isa.h"
    169 #if NISA > 0
    170 #include <dev/isa/isareg.h>
    171 #include <dev/isa/isavar.h>
    172 #endif
    173 
    174 #include <machine/isa_machdep.h>
    175 
    176 #include <evbarm/armadillo/armadillo9reg.h>
    177 #include <evbarm/armadillo/armadillo9var.h>
    178 
    179 struct armadillo_model_t *armadillo_model = 0;
    180 static struct armadillo_model_t armadillo_model_table[] = {
    181 	{ DEVCFG_ARMADILLO9, "Armadillo-9" },
    182 	{ DEVCFG_ARMADILLO210, "Armadillo-210" },
    183 	{ 0, "Armadillo(unknown model)" } };
    184 
    185 #include "opt_ipkdb.h"
    186 #include "ksyms.h"
    187 
    188 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    189 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    190 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    191 
    192 /*
    193  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    194  * Core-logic registers and I/O mappings occupy 0xf0000000 - 0xffffffff
    195  */
    196 #define KERNEL_VM_SIZE		0x0c000000
    197 
    198 /*
    199  * Address to call from cpu_reset() to reset the machine.
    200  * This is machine architecture dependant as it varies depending
    201  * on where the ROM appears when you turn the MMU off.
    202  */
    203 
    204 u_int cpu_reset_address = 0x80090000;
    205 
    206 /* Define various stack sizes in pages */
    207 #define IRQ_STACK_SIZE	8
    208 #define ABT_STACK_SIZE	8
    209 #ifdef IPKDB
    210 #define UND_STACK_SIZE	16
    211 #else
    212 #define UND_STACK_SIZE	8
    213 #endif
    214 
    215 BootConfig bootconfig;	/* Boot config storage */
    216 char *boot_args = NULL;
    217 char *boot_file = NULL;
    218 
    219 vm_offset_t physical_start;
    220 vm_offset_t physical_freestart;
    221 vm_offset_t physical_freeend;
    222 vm_offset_t physical_freeend_low;
    223 vm_offset_t physical_end;
    224 u_int free_pages;
    225 int physmem = 0;
    226 
    227 /* Physical and virtual addresses for some global pages */
    228 pv_addr_t systempage;
    229 pv_addr_t irqstack;
    230 pv_addr_t undstack;
    231 pv_addr_t abtstack;
    232 pv_addr_t kernelstack;
    233 
    234 vm_offset_t msgbufphys;
    235 
    236 static struct arm32_dma_range armadillo9_dma_ranges[4];
    237 
    238 #if NISA > 0
    239 extern void isa_armadillo9_init(u_int, u_int);
    240 #endif
    241 
    242 extern u_int data_abort_handler_address;
    243 extern u_int prefetch_abort_handler_address;
    244 extern u_int undefined_handler_address;
    245 
    246 #ifdef PMAP_DEBUG
    247 extern int pmap_debug_level;
    248 #endif
    249 
    250 #define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
    251 
    252 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    253 #define	KERNEL_PT_KERNEL_NUM	4
    254 					/* L2 tables for mapping kernel VM */
    255 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    256 
    257 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    258 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    259 
    260 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    261 
    262 struct user *proc0paddr;
    263 
    264 /* Prototypes */
    265 
    266 void	consinit(void);
    267 /*
    268  * Define the default console speed for the machine.
    269  */
    270 #if NEPCOM > 0
    271 #ifndef CONSPEED
    272 #define CONSPEED B115200
    273 #endif /* ! CONSPEED */
    274 
    275 #ifndef CONMODE
    276 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    277 #endif
    278 
    279 #ifndef CONUNIT
    280 #define	CONUNIT	0
    281 #endif
    282 
    283 int comcnspeed = CONSPEED;
    284 int comcnmode = CONMODE;
    285 const unsigned long comaddr[] = {
    286 	EP93XX_APB_UART1, EP93XX_APB_UART2 };
    287 #endif
    288 
    289 #if KGDB
    290 #ifndef KGDB_DEVNAME
    291 #error Must define KGDB_DEVNAME
    292 #endif
    293 const char kgdb_devname[] = KGDB_DEVNAME;
    294 
    295 #ifndef KGDB_DEVADDR
    296 #error Must define KGDB_DEVADDR
    297 #endif
    298 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    299 
    300 #ifndef KGDB_DEVRATE
    301 #define KGDB_DEVRATE	CONSPEED
    302 #endif
    303 int kgdb_devrate = KGDB_DEVRATE;
    304 
    305 #ifndef KGDB_DEVMODE
    306 #define KGDB_DEVMODE	CONMODE
    307 #endif
    308 int kgdb_devmode = KGDB_DEVMODE;
    309 #endif /* KGDB */
    310 
    311 /*
    312  * MAC address for the built-in Ethernet.
    313  */
    314 uint8_t	armadillo9_ethaddr[ETHER_ADDR_LEN];
    315 
    316 static void
    317 armadillo9_device_register(device_t dev, void *aux)
    318 {
    319 
    320 	/* MAC address for the built-in Ethernet. */
    321 	if (device_is_a(dev, "epe")) {
    322 		prop_data_t pd = prop_data_create_data_nocopy(
    323 		    armadillo9_ethaddr, ETHER_ADDR_LEN);
    324 		KASSERT(pd != NULL);
    325 		if (prop_dictionary_set(device_properties(dev),
    326 					"mac-addr", pd) == FALSE) {
    327 			printf("WARNING: unable to set mac-addr property "
    328 			    "for %s\n", dev->dv_xname);
    329 		}
    330 		prop_object_release(pd);
    331 	}
    332 }
    333 
    334 /*
    335  * void cpu_reboot(int howto, char *bootstr)
    336  *
    337  * Reboots the system
    338  *
    339  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    340  * then reset the CPU.
    341  */
    342 void
    343 cpu_reboot(int howto, char *bootstr)
    344 {
    345 	/*
    346 	 * If we are still cold then hit the air brakes
    347 	 * and crash to earth fast
    348 	 */
    349 	if (cold) {
    350 		doshutdownhooks();
    351 		printf("\r\n");
    352 		printf("The operating system has halted.\r\n");
    353 		printf("Please press any key to reboot.\r\n");
    354 		cngetc();
    355 		printf("\r\nrebooting...\r\n");
    356 		goto reset;
    357 	}
    358 
    359 	/* Disable console buffering */
    360 
    361 	/*
    362 	 * If RB_NOSYNC was not specified sync the discs.
    363 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    364 	 * unmount.  It looks like syslogd is getting woken up only to find
    365 	 * that it cannot page part of the binary in as the filesystem has
    366 	 * been unmounted.
    367 	 */
    368 	if (!(howto & RB_NOSYNC))
    369 		bootsync();
    370 
    371 	/* Say NO to interrupts */
    372 	splhigh();
    373 
    374 	/* Do a dump if requested. */
    375 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    376 		dumpsys();
    377 
    378 	/* Run any shutdown hooks */
    379 	doshutdownhooks();
    380 
    381 	/* Make sure IRQ's are disabled */
    382 	IRQdisable;
    383 
    384 	if (howto & RB_HALT) {
    385 		printf("\r\n");
    386 		printf("The operating system has halted.\r\n");
    387 		printf("Please press any key to reboot.\r\n");
    388 		cngetc();
    389 	}
    390 
    391 	printf("\r\nrebooting...\r\n");
    392  reset:
    393 	/*
    394 	 * Make really really sure that all interrupts are disabled,
    395 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    396 	 */
    397 	(void) disable_interrupts(I32_bit|F32_bit);
    398 #if NEPWDOG > 0
    399 	epwdog_reset();
    400 #else
    401 	{
    402 	u_int32_t ctrl = EP93XX_APB_VBASE + EP93XX_APB_WDOG + EP93XX_WDOG_Ctrl;
    403 	u_int32_t val = EP93XX_WDOG_ENABLE;
    404 	__asm volatile (
    405 		"str %1, [%0]\n"
    406 		:
    407 		: "r" (ctrl), "r" (val)
    408 	);
    409 	}
    410 #endif
    411 	for (;;);
    412 }
    413 
    414 /* Static device mappings. */
    415 static const struct pmap_devmap armadillo9_devmap[] = {
    416     {
    417 	EP93XX_AHB_VBASE,
    418 	EP93XX_AHB_HWBASE,
    419 	EP93XX_AHB_SIZE,
    420 	VM_PROT_READ|VM_PROT_WRITE,
    421 	PTE_NOCACHE,
    422     },
    423 
    424     {
    425 	EP93XX_APB_VBASE,
    426 	EP93XX_APB_HWBASE,
    427 	EP93XX_APB_SIZE,
    428 	VM_PROT_READ|VM_PROT_WRITE,
    429 	PTE_NOCACHE,
    430     },
    431 
    432     {
    433 	EP93XX_PCMCIA0_VBASE,
    434 	EP93XX_PCMCIA0_HWBASE,
    435 	EP93XX_PCMCIA_SIZE,
    436 	VM_PROT_READ|VM_PROT_WRITE,
    437 	PTE_NOCACHE,
    438     },
    439 
    440 	/*
    441 	 * IO8 and IO16 space *must* be mapped contiguously with
    442 	 * IO8_VA == IO16_VA - 64 Mbytes.  ISA busmap driver depends
    443 	 * on that!
    444 	 */
    445     {
    446 	ARMADILLO9_IO8_VBASE,
    447 	ARMADILLO9_IO8_HWBASE,
    448 	ARMADILLO9_IO8_SIZE,
    449 	VM_PROT_READ|VM_PROT_WRITE,
    450 	PTE_NOCACHE,
    451     },
    452 
    453     {
    454 	ARMADILLO9_IO16_VBASE,
    455 	ARMADILLO9_IO16_HWBASE,
    456 	ARMADILLO9_IO16_SIZE,
    457 	VM_PROT_READ|VM_PROT_WRITE,
    458 	PTE_NOCACHE,
    459     },
    460 
    461    {
    462 	0,
    463 	0,
    464 	0,
    465 	0,
    466 	0,
    467     }
    468 };
    469 
    470 /*
    471  * u_int initarm(...)
    472  *
    473  * Initial entry point on startup. This gets called before main() is
    474  * entered.
    475  * It should be responsible for setting up everything that must be
    476  * in place when main is called.
    477  * This includes
    478  *   Taking a copy of the boot configuration structure.
    479  *   Initialising the physical console so characters can be printed.
    480  *   Setting up page tables for the kernel
    481  *   Initialising interrupt controllers to a sane default state
    482  */
    483 u_int
    484 initarm(void *arg)
    485 {
    486 	int loop;
    487 	int loop1;
    488 	u_int l1pagetable;
    489 	pv_addr_t kernel_l1pt;
    490 	struct bootparam_tag *bootparam_p;
    491 	unsigned long devcfg;
    492 
    493 	/*
    494 	 * Since we map the on-board devices VA==PA, and the kernel
    495 	 * is running VA==PA, it's possible for us to initialize
    496 	 * the console now.
    497 	 */
    498 	consinit();
    499 
    500 	/* identify model */
    501 	devcfg = *((volatile unsigned long*)(EP93XX_APB_HWBASE
    502 					     + EP93XX_APB_SYSCON
    503 					     + EP93XX_SYSCON_DeviceCfg));
    504 	for (armadillo_model = &armadillo_model_table[0];
    505 				armadillo_model->devcfg; armadillo_model++)
    506 		if (devcfg == armadillo_model->devcfg)
    507 			break;
    508 
    509 	/* Talk to the user */
    510 	printf("\nNetBSD/%s booting ...\n", armadillo_model->name);
    511 
    512 	/* set some informations from bootloader */
    513 	bootparam_p = (struct bootparam_tag *)bootparam;
    514 	bootconfig.dramblocks = 0;
    515 	while (bootparam_p->hdr.tag != BOOTPARAM_TAG_NONE) {
    516 		switch (bootparam_p->hdr.tag) {
    517 		case BOOTPARAM_TAG_MEM:
    518 			if (bootconfig.dramblocks < DRAM_BLOCKS) {
    519 #ifdef VERBOSE_INIT_ARM
    520 			printf("dram[%d]: address=0x%08lx, size=0x%08lx\n",
    521 						bootconfig.dramblocks,
    522 						bootparam_p->u.mem.start,
    523 						bootparam_p->u.mem.size);
    524 #endif
    525 				bootconfig.dram[bootconfig.dramblocks].address =
    526 					bootparam_p->u.mem.start;
    527 				bootconfig.dram[bootconfig.dramblocks].pages =
    528 					bootparam_p->u.mem.size / PAGE_SIZE;
    529 				bootconfig.dramblocks++;
    530 			}
    531 			break;
    532 		case BOOTPARAM_TAG_CMDLINE:
    533 #ifdef VERBOSE_INIT_ARM
    534 			printf("cmdline: %s\n", bootparam_p->u.cmdline.cmdline);
    535 #endif
    536 			parse_mi_bootargs(bootparam_p->u.cmdline.cmdline);
    537 			break;
    538 		}
    539 		bootparam_p = bootparam_tag_next(bootparam_p);
    540 	}
    541 
    542 	/*
    543 	 * Heads up ... Setup the CPU / MMU / TLB functions
    544 	 */
    545 	if (set_cpufuncs())
    546 		panic("cpu not recognized!");
    547 
    548 #ifdef VERBOSE_INIT_ARM
    549 	printf("initarm: Configuring system ...\n");
    550 #endif
    551 	/*
    552 	 * Set up the variables that define the availablilty of
    553 	 * physical memory.  For now, we're going to set
    554 	 * physical_freestart to 0xc0200000 (where the kernel
    555 	 * was loaded), and allocate the memory we need downwards.
    556 	 * If we get too close to the L1 table that we set up, we
    557 	 * will panic.  We will update physical_freestart and
    558 	 * physical_freeend later to reflect what pmap_bootstrap()
    559 	 * wants to see.
    560 	 *
    561 	 * XXX pmap_bootstrap() needs an enema.
    562 	 */
    563 	physical_start = bootconfig.dram[0].address;
    564 	physical_end = bootconfig.dram[0].address
    565 			+ (bootconfig.dram[0].pages * PAGE_SIZE);
    566 
    567 	physical_freestart = 0xc0018000UL;
    568 	physical_freeend = 0xc0200000UL;
    569 
    570 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    571 
    572 #ifdef VERBOSE_INIT_ARM
    573 	/* Tell the user about the memory */
    574 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    575 	    physical_start, physical_end - 1);
    576 #endif
    577 
    578 	/*
    579 	 * Okay, the kernel starts 2MB in from the bottom of physical
    580 	 * memory.  We are going to allocate our bootstrap pages downwards
    581 	 * from there.
    582 	 *
    583 	 * We need to allocate some fixed page tables to get the kernel
    584 	 * going.  We allocate one page directory and a number of page
    585 	 * tables and store the physical addresses in the kernel_pt_table
    586 	 * array.
    587 	 *
    588 	 * The kernel page directory must be on a 16K boundary.  The page
    589 	 * tables must be on 4K bounaries.  What we do is allocate the
    590 	 * page directory on the first 16K boundary that we encounter, and
    591 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    592 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    593 	 * least one 16K aligned region.
    594 	 */
    595 
    596 #ifdef VERBOSE_INIT_ARM
    597 	printf("Allocating page tables\n");
    598 #endif
    599 
    600 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    601 
    602 #ifdef VERBOSE_INIT_ARM
    603 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    604 	       physical_freestart, free_pages, free_pages);
    605 #endif
    606 
    607 	/* Define a macro to simplify memory allocation */
    608 #define	valloc_pages(var, np)				\
    609 	alloc_pages((var).pv_pa, (np));			\
    610 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    611 
    612 #define alloc_pages(var, np)				\
    613 	physical_freeend -= ((np) * PAGE_SIZE);		\
    614 	if (physical_freeend < physical_freestart)	\
    615 		panic("initarm: out of memory");	\
    616 	(var) = physical_freeend;			\
    617 	free_pages -= (np);				\
    618 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    619 
    620 	loop1 = 0;
    621 	kernel_l1pt.pv_pa = 0;
    622 	kernel_l1pt.pv_va = 0;
    623 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    624 		/* Are we 16KB aligned for an L1 ? */
    625 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    626 		    && kernel_l1pt.pv_pa == 0) {
    627 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    628 		} else {
    629 			valloc_pages(kernel_pt_table[loop1],
    630 			    L2_TABLE_SIZE / PAGE_SIZE);
    631 			++loop1;
    632 		}
    633 	}
    634 
    635 	/* This should never be able to happen but better confirm that. */
    636 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    637 		panic("initarm: Failed to align the kernel page directory");
    638 
    639 	/*
    640 	 * Allocate a page for the system vectors page
    641 	 */
    642 	alloc_pages(systempage.pv_pa, 1);
    643 
    644 	/* Allocate stacks for all modes */
    645 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    646 	valloc_pages(abtstack, ABT_STACK_SIZE);
    647 	valloc_pages(undstack, UND_STACK_SIZE);
    648 	valloc_pages(kernelstack, UPAGES);
    649 
    650 #ifdef VERBOSE_INIT_ARM
    651 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    652 	    irqstack.pv_va);
    653 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    654 	    abtstack.pv_va);
    655 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    656 	    undstack.pv_va);
    657 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    658 	    kernelstack.pv_va);
    659 #endif
    660 
    661 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    662 
    663 	/*
    664 	 * Ok we have allocated physical pages for the primary kernel
    665 	 * page tables.  Save physical_freeend for when we give whats left
    666 	 * of memory below 2Mbyte to UVM.
    667 	 */
    668 
    669 	physical_freeend_low = physical_freeend;
    670 
    671 #ifdef VERBOSE_INIT_ARM
    672 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    673 #endif
    674 
    675 	/*
    676 	 * Now we start construction of the L1 page table
    677 	 * We start by mapping the L2 page tables into the L1.
    678 	 * This means that we can replace L1 mappings later on if necessary
    679 	 */
    680 	l1pagetable = kernel_l1pt.pv_pa;
    681 
    682 	/* Map the L2 pages tables in the L1 page table */
    683 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    684 	    &kernel_pt_table[KERNEL_PT_SYS]);
    685 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    686 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    687 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    688 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    689 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    690 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    691 
    692 	/* update the top of the kernel VM */
    693 	pmap_curmaxkvaddr =
    694 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    695 
    696 #ifdef VERBOSE_INIT_ARM
    697 	printf("Mapping kernel\n");
    698 #endif
    699 
    700 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    701 	{
    702 		extern char etext[], _end[];
    703 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    704 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    705 		u_int logical;
    706 
    707 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    708 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    709 
    710 		logical = 0x00200000;	/* offset of kernel in RAM */
    711 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    712 		    physical_start + logical, textsize,
    713 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    714 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    715 		    physical_start + logical, totalsize - textsize,
    716 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    717 	}
    718 
    719 #ifdef VERBOSE_INIT_ARM
    720 	printf("Constructing L2 page tables\n");
    721 #endif
    722 
    723 	/* Map the stack pages */
    724 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    725 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    726 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    727 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    728 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    729 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    730 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    731 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    732 
    733 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    734 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    735 
    736 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    737 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    738 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    739 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    740 	}
    741 
    742 	/* Map the vector page. */
    743 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    744 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    745 
    746 	/* Map the statically mapped devices. */
    747 	pmap_devmap_bootstrap(l1pagetable, armadillo9_devmap);
    748 
    749 	/*
    750 	 * Update the physical_freestart/physical_freeend/free_pages
    751 	 * variables.
    752 	 */
    753 	{
    754 		extern char _end[];
    755 
    756 		physical_freestart = physical_start +
    757 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    758 		     KERNEL_BASE);
    759 		physical_freeend = physical_end;
    760 		free_pages =
    761 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    762 	}
    763 
    764 	/*
    765 	 * Now we have the real page tables in place so we can switch to them.
    766 	 * Once this is done we will be running with the REAL kernel page
    767 	 * tables.
    768 	 */
    769 
    770 	/* Switch tables */
    771 #ifdef VERBOSE_INIT_ARM
    772 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    773 	       physical_freestart, free_pages, free_pages);
    774 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    775 #endif
    776 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    777 	setttb(kernel_l1pt.pv_pa);
    778 	cpu_tlb_flushID();
    779 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    780 
    781 	/*
    782 	 * Moved from cpu_startup() as data_abort_handler() references
    783 	 * this during uvm init
    784 	 */
    785 	proc0paddr = (struct user *)kernelstack.pv_va;
    786 	lwp0.l_addr = proc0paddr;
    787 
    788 #ifdef VERBOSE_INIT_ARM
    789 	printf("done!\n");
    790 #endif
    791 
    792 #ifdef VERBOSE_INIT_ARM
    793 	printf("bootstrap done.\n");
    794 #endif
    795 
    796 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    797 
    798 	/*
    799 	 * Pages were allocated during the secondary bootstrap for the
    800 	 * stacks for different CPU modes.
    801 	 * We must now set the r13 registers in the different CPU modes to
    802 	 * point to these stacks.
    803 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    804 	 * of the stack memory.
    805 	 */
    806 #ifdef VERBOSE_INIT_ARM
    807 	printf("init subsystems: stacks ");
    808 #endif
    809 
    810 	set_stackptr(PSR_IRQ32_MODE,
    811 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    812 	set_stackptr(PSR_ABT32_MODE,
    813 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    814 	set_stackptr(PSR_UND32_MODE,
    815 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    816 
    817 	/*
    818 	 * Well we should set a data abort handler.
    819 	 * Once things get going this will change as we will need a proper
    820 	 * handler.
    821 	 * Until then we will use a handler that just panics but tells us
    822 	 * why.
    823 	 * Initialisation of the vectors will just panic on a data abort.
    824 	 * This just fills in a slightly better one.
    825 	 */
    826 #ifdef VERBOSE_INIT_ARM
    827 	printf("vectors ");
    828 #endif
    829 	data_abort_handler_address = (u_int)data_abort_handler;
    830 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    831 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    832 
    833 	/* Initialise the undefined instruction handlers */
    834 #ifdef VERBOSE_INIT_ARM
    835 	printf("undefined ");
    836 #endif
    837 	undefined_init();
    838 
    839 	/* Load memory into UVM. */
    840 #ifdef VERBOSE_INIT_ARM
    841 	printf("page ");
    842 #endif
    843 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    844 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    845 	    atop(physical_freestart), atop(physical_freeend),
    846 	    VM_FREELIST_DEFAULT);
    847 	uvm_page_physload(atop(0xc0000000), atop(physical_freeend_low),
    848 	    atop(0xc0000000), atop(physical_freeend_low),
    849 	    VM_FREELIST_DEFAULT);
    850 	physmem = bootconfig.dram[0].pages;
    851 	for (loop = 1; loop < bootconfig.dramblocks; ++loop) {
    852 		size_t start = bootconfig.dram[loop].address;
    853 		size_t size = bootconfig.dram[loop].pages * PAGE_SIZE;
    854 		uvm_page_physload(atop(start), atop(start + size),
    855 				  atop(start), atop(start + size),
    856 				  VM_FREELIST_DEFAULT);
    857 		physmem += bootconfig.dram[loop].pages;
    858 	}
    859 
    860 	/* Boot strap pmap telling it where the kernel page table is */
    861 #ifdef VERBOSE_INIT_ARM
    862 	printf("pmap ");
    863 #endif
    864 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    865 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    866 
    867 	/* Setup the IRQ system */
    868 #ifdef VERBOSE_INIT_ARM
    869 	printf("irq ");
    870 #endif
    871 	ep93xx_intr_init();
    872 #if NISA > 0
    873 	isa_intr_init();
    874 
    875 #ifdef VERBOSE_INIT_ARM
    876 	printf("isa ");
    877 #endif
    878 	isa_armadillo9_init(ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAIO,
    879 		ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAMEM);
    880 #endif
    881 
    882 #ifdef VERBOSE_INIT_ARM
    883 	printf("done.\n");
    884 #endif
    885 
    886 #ifdef BOOTHOWTO
    887 	boothowto = BOOTHOWTO;
    888 #endif
    889 
    890 #ifdef IPKDB
    891 	/* Initialise ipkdb */
    892 	ipkdb_init();
    893 	if (boothowto & RB_KDB)
    894 		ipkdb_connect(0);
    895 #endif
    896 
    897 #if NKSYMS || defined(DDB) || defined(LKM)
    898 	/* Firmware doesn't load symbols. */
    899 	ksyms_init(0, NULL, NULL);
    900 #endif
    901 
    902 #ifdef DDB
    903 	db_machine_init();
    904 	if (boothowto & RB_KDB)
    905 		Debugger();
    906 #endif
    907 
    908 	/* We have our own device_register() */
    909 	evbarm_device_register = armadillo9_device_register;
    910 
    911 	/* We return the new stack pointer address */
    912 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    913 }
    914 
    915 void
    916 consinit(void)
    917 {
    918 	static int consinit_called;
    919 #if NEPCOM > 0
    920 	bus_space_handle_t ioh;
    921 #endif
    922 
    923 	if (consinit_called != 0)
    924 		return;
    925 
    926 	consinit_called = 1;
    927 
    928 	/*
    929 	 * Console devices are already mapped in VA.  Our devmap reflects
    930 	 * this, so register it now so drivers can map the console
    931 	 * device.
    932 	 */
    933 	pmap_devmap_register(armadillo9_devmap);
    934 
    935 #if NEPCOM > 0
    936 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + comaddr[CONUNIT],
    937 		EP93XX_APB_UART_SIZE, 0, &ioh);
    938         if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + comaddr[CONUNIT],
    939 		ioh, comcnspeed, comcnmode))
    940 	{
    941 		panic("can't init serial console");
    942 	}
    943 #else
    944 	panic("serial console not configured");
    945 #endif
    946 #if KGDB
    947 #if NEPCOM > 0
    948 	if (strcmp(kgdb_devname, "epcom") == 0) {
    949 		com_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
    950 			kgdb_devmode);
    951 	}
    952 #endif	/* NEPCOM > 0 */
    953 #endif	/* KGDB */
    954 }
    955 
    956 
    957 bus_dma_tag_t
    958 ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    959 {
    960 	int i;
    961 	struct arm32_bus_dma_tag *dmat;
    962 
    963 	for (i = 0; i < bootconfig.dramblocks; i++) {
    964 		armadillo9_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    965 		armadillo9_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    966 		armadillo9_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    967 			PAGE_SIZE;
    968 	}
    969 
    970 	dmat = dma_tag_template;
    971 
    972 	dmat->_ranges = armadillo9_dma_ranges;
    973 	dmat->_nranges = bootconfig.dramblocks;
    974 
    975 	return dmat;
    976 }
    977