Home | History | Annotate | Line # | Download | only in tsarm
tsarm_machdep.c revision 1.18
      1 /*	$NetBSD: tsarm_machdep.c,v 1.18 2012/08/16 18:22:46 matt 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 dependent functions for kernel setup for Iyonix.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: tsarm_machdep.c,v 1.18 2012/08/16 18:22:46 matt Exp $");
     77 
     78 #include "opt_ddb.h"
     79 #include "opt_kgdb.h"
     80 #include "opt_pmap_debug.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/device.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/exec.h>
     87 #include <sys/proc.h>
     88 #include <sys/msgbuf.h>
     89 #include <sys/reboot.h>
     90 #include <sys/termios.h>
     91 #include <sys/ksyms.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <dev/cons.h>
     96 
     97 #include <machine/db_machdep.h>
     98 #include <ddb/db_sym.h>
     99 #include <ddb/db_extern.h>
    100 
    101 #include <acorn32/include/bootconfig.h>
    102 #include <sys/bus.h>
    103 #include <machine/cpu.h>
    104 #include <machine/frame.h>
    105 #include <arm/undefined.h>
    106 
    107 /* Define various stack sizes in pages */
    108 #define IRQ_STACK_SIZE	8
    109 #define ABT_STACK_SIZE	8
    110 #define UND_STACK_SIZE	8
    111 
    112 #include <arm/arm32/machdep.h>
    113 
    114 #include <arm/ep93xx/ep93xxreg.h>
    115 #include <arm/ep93xx/ep93xxvar.h>
    116 
    117 #include <dev/ic/comreg.h>
    118 #include <dev/ic/comvar.h>
    119 
    120 #include "epcom.h"
    121 #if NEPCOM > 0
    122 #include <arm/ep93xx/epcomvar.h>
    123 #endif
    124 
    125 #include "isa.h"
    126 #if NISA > 0
    127 #include <dev/isa/isareg.h>
    128 #include <dev/isa/isavar.h>
    129 #endif
    130 
    131 #include <machine/isa_machdep.h>
    132 
    133 #include <evbarm/tsarm/tsarmreg.h>
    134 
    135 #include "ksyms.h"
    136 
    137 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    138 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    139 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    140 
    141 /*
    142  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    143  * Core-logic registers and I/O mappings occupy 0xf0000000 - 0xffffffff
    144  */
    145 #define KERNEL_VM_SIZE		0x0C000000
    146 
    147 struct bootconfig bootconfig;		/* Boot config storage */
    148 char *boot_args = NULL;
    149 char *boot_file = NULL;
    150 
    151 vm_offset_t physical_start;
    152 vm_offset_t physical_freestart;
    153 vm_offset_t physical_freeend;
    154 vm_offset_t physical_freeend_low;
    155 vm_offset_t physical_end;
    156 u_int free_pages;
    157 
    158 vm_offset_t msgbufphys;
    159 
    160 static struct arm32_dma_range tsarm_dma_ranges[4];
    161 
    162 #if NISA > 0
    163 extern void isa_tsarm_init(u_int, u_int);
    164 #endif
    165 
    166 #ifdef PMAP_DEBUG
    167 extern int pmap_debug_level;
    168 #endif
    169 
    170 #define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
    171 
    172 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    173 #define	KERNEL_PT_KERNEL_NUM	4
    174 					/* L2 tables for mapping kernel VM */
    175 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    176 
    177 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    178 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    179 
    180 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    181 
    182 /* Prototypes */
    183 
    184 void	consinit(void);
    185 /*
    186  * Define the default console speed for the machine.
    187  */
    188 #ifndef CONSPEED
    189 #define CONSPEED B115200
    190 #endif /* ! CONSPEED */
    191 
    192 #ifndef CONMODE
    193 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    194 #endif
    195 
    196 int comcnspeed = CONSPEED;
    197 int comcnmode = CONMODE;
    198 
    199 #if KGDB
    200 #ifndef KGDB_DEVNAME
    201 #error Must define KGDB_DEVNAME
    202 #endif
    203 const char kgdb_devname[] = KGDB_DEVNAME;
    204 
    205 #ifndef KGDB_DEVADDR
    206 #error Must define KGDB_DEVADDR
    207 #endif
    208 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    209 
    210 #ifndef KGDB_DEVRATE
    211 #define KGDB_DEVRATE	CONSPEED
    212 #endif
    213 int kgdb_devrate = KGDB_DEVRATE;
    214 
    215 #ifndef KGDB_DEVMODE
    216 #define KGDB_DEVMODE	CONMODE
    217 #endif
    218 int kgdb_devmode = KGDB_DEVMODE;
    219 #endif /* KGDB */
    220 
    221 /*
    222  * void cpu_reboot(int howto, char *bootstr)
    223  *
    224  * Reboots the system
    225  *
    226  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    227  * then reset the CPU.
    228  */
    229 void
    230 cpu_reboot(int howto, char *bootstr)
    231 {
    232 
    233 	/*
    234 	 * If we are still cold then hit the air brakes
    235 	 * and crash to earth fast
    236 	 */
    237 	if (cold) {
    238 		doshutdownhooks();
    239 		pmf_system_shutdown(boothowto);
    240 		printf("\r\n");
    241 		printf("The operating system has halted.\r\n");
    242 		printf("Please press any key to reboot.\r\n");
    243 		cngetc();
    244 		printf("\r\nrebooting...\r\n");
    245 		goto reset;
    246 	}
    247 
    248 	/* Disable console buffering */
    249 
    250 	/*
    251 	 * If RB_NOSYNC was not specified sync the discs.
    252 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    253 	 * unmount.  It looks like syslogd is getting woken up only to find
    254 	 * that it cannot page part of the binary in as the filesystem has
    255 	 * been unmounted.
    256 	 */
    257 	if (!(howto & RB_NOSYNC))
    258 		bootsync();
    259 
    260 	/* Say NO to interrupts */
    261 	splhigh();
    262 
    263 	/* Do a dump if requested. */
    264 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    265 		dumpsys();
    266 
    267 	/* Run any shutdown hooks */
    268 	doshutdownhooks();
    269 
    270 	pmf_system_shutdown(boothowto);
    271 
    272 	/* Make sure IRQ's are disabled */
    273 	IRQdisable;
    274 
    275 	if (howto & RB_HALT) {
    276 		printf("\r\n");
    277 		printf("The operating system has halted.\r\n");
    278 		printf("Please press any key to reboot.\r\n");
    279 		cngetc();
    280 	}
    281 
    282 	printf("\r\nrebooting...\r\n");
    283  reset:
    284 	/*
    285 	 * Make really really sure that all interrupts are disabled,
    286 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    287 	 */
    288 	(void) disable_interrupts(I32_bit|F32_bit);
    289 
    290 	{
    291 		u_int32_t feed, ctrl;
    292 
    293 		feed = TS7XXX_IO16_VBASE + TS7XXX_WDOGFEED;
    294 		ctrl = TS7XXX_IO16_VBASE + TS7XXX_WDOGCTRL;
    295 
    296 		__asm volatile (
    297 			"mov r0, #0x5\n"
    298 			"mov r1, #0x1\n"
    299 			"strh r0, [%0]\n"
    300 			"strh r1, [%1]\n"
    301 			:
    302 			: "r" (feed), "r" (ctrl)
    303 			: "r0", "r1"
    304 		);
    305 	}
    306 
    307 	for (;;);
    308 }
    309 
    310 /* Static device mappings. */
    311 static const struct pmap_devmap tsarm_devmap[] = {
    312     {
    313 	EP93XX_AHB_VBASE,
    314 	EP93XX_AHB_HWBASE,
    315 	EP93XX_AHB_SIZE,
    316 	VM_PROT_READ|VM_PROT_WRITE,
    317 	PTE_NOCACHE,
    318     },
    319 
    320     {
    321 	EP93XX_APB_VBASE,
    322 	EP93XX_APB_HWBASE,
    323 	EP93XX_APB_SIZE,
    324 	VM_PROT_READ|VM_PROT_WRITE,
    325 	PTE_NOCACHE,
    326     },
    327 
    328 	/*
    329 	 * IO8 and IO16 space *must* be mapped contiguously with
    330 	 * IO8_VA == IO16_VA - 64 Mbytes.  ISA busmap driver depends
    331 	 * on that!
    332 	 */
    333     {
    334 	TS7XXX_IO8_VBASE,
    335 	TS7XXX_IO8_HWBASE,
    336 	TS7XXX_IO8_SIZE,
    337 	VM_PROT_READ|VM_PROT_WRITE,
    338 	PTE_NOCACHE,
    339     },
    340 
    341     {
    342 	TS7XXX_IO16_VBASE,
    343 	TS7XXX_IO16_HWBASE,
    344 	TS7XXX_IO16_SIZE,
    345 	VM_PROT_READ|VM_PROT_WRITE,
    346 	PTE_NOCACHE,
    347     },
    348 
    349    {
    350 	0,
    351 	0,
    352 	0,
    353 	0,
    354 	0,
    355     }
    356 };
    357 
    358 /*
    359  * u_int initarm(...)
    360  *
    361  * Initial entry point on startup. This gets called before main() is
    362  * entered.
    363  * It should be responsible for setting up everything that must be
    364  * in place when main is called.
    365  * This includes
    366  *   Taking a copy of the boot configuration structure.
    367  *   Initialising the physical console so characters can be printed.
    368  *   Setting up page tables for the kernel
    369  *   Initialising interrupt controllers to a sane default state
    370  */
    371 u_int
    372 initarm(void *arg)
    373 {
    374 #ifdef FIXME
    375 	struct bootconfig *passed_bootconfig = arg;
    376 	extern char _end[];
    377 #endif
    378 	int loop;
    379 	int loop1;
    380 	u_int l1pagetable;
    381 	paddr_t memstart;
    382 	psize_t memsize;
    383 
    384 #ifdef FIXME
    385 	/* Calibrate the delay loop. */
    386 	i80321_calibrate_delay();
    387 #endif
    388 
    389 	/*
    390 	 * Since we map the on-board devices VA==PA, and the kernel
    391 	 * is running VA==PA, it's possible for us to initialize
    392 	 * the console now.
    393 	 */
    394 	consinit();
    395 
    396 #ifdef VERBOSE_INIT_ARM
    397 	/* Talk to the user */
    398 	printf("\nNetBSD/tsarm booting ...\n");
    399 #endif
    400 
    401 	/*
    402 	 * Heads up ... Setup the CPU / MMU / TLB functions
    403 	 */
    404 	if (set_cpufuncs())
    405 		panic("cpu not recognized!");
    406 
    407 	/*
    408 	 * We are currently running with the MMU enabled
    409 	 */
    410 
    411 #ifdef FIXME
    412 	/*
    413 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
    414 	 * registers.
    415 	 */
    416 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    417 	    &memstart, &memsize);
    418 #else
    419 	memstart = 0x0;
    420 	memsize = 0x2000000;
    421 #endif
    422 
    423 #ifdef VERBOSE_INIT_ARM
    424 	printf("initarm: Configuring system ...\n");
    425 #endif
    426 
    427 	/* Fake bootconfig structure for the benefit of pmap.c */
    428 	/* XXX must make the memory description h/w independent */
    429 	bootconfig.dramblocks = 4;
    430 	bootconfig.dram[0].address = 0x0UL;
    431 	bootconfig.dram[0].pages = 0x800000UL / PAGE_SIZE;
    432 	bootconfig.dram[1].address = 0x1000000UL;
    433 	bootconfig.dram[1].pages = 0x800000UL / PAGE_SIZE;
    434 	bootconfig.dram[2].address = 0x4000000UL;
    435 	bootconfig.dram[2].pages = 0x800000UL / PAGE_SIZE;
    436 	bootconfig.dram[3].address = 0x5000000UL;
    437 	bootconfig.dram[3].pages = 0x800000UL / PAGE_SIZE;
    438 
    439 	/*
    440 	 * Set up the variables that define the availablilty of
    441 	 * physical memory.  For now, we're going to set
    442 	 * physical_freestart to 0x00200000 (where the kernel
    443 	 * was loaded), and allocate the memory we need downwards.
    444 	 * If we get too close to the L1 table that we set up, we
    445 	 * will panic.  We will update physical_freestart and
    446 	 * physical_freeend later to reflect what pmap_bootstrap()
    447 	 * wants to see.
    448 	 *
    449 	 * XXX pmap_bootstrap() needs an enema.
    450 	 */
    451 	physical_start = bootconfig.dram[0].address;
    452 	physical_end = bootconfig.dram[0].address +
    453 		(bootconfig.dram[0].pages * PAGE_SIZE);
    454 
    455 	physical_freestart = 0x00009000UL;
    456 	physical_freeend = 0x00200000UL;
    457 
    458 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    459 
    460 #ifdef VERBOSE_INIT_ARM
    461 	/* Tell the user about the memory */
    462 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    463 	    physical_start, physical_end - 1);
    464 #endif
    465 
    466 	/*
    467 	 * Okay, the kernel starts 2MB in from the bottom of physical
    468 	 * memory.  We are going to allocate our bootstrap pages downwards
    469 	 * from there.
    470 	 *
    471 	 * We need to allocate some fixed page tables to get the kernel
    472 	 * going.  We allocate one page directory and a number of page
    473 	 * tables and store the physical addresses in the kernel_pt_table
    474 	 * array.
    475 	 *
    476 	 * The kernel page directory must be on a 16K boundary.  The page
    477 	 * tables must be on 4K bounaries.  What we do is allocate the
    478 	 * page directory on the first 16K boundary that we encounter, and
    479 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    480 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    481 	 * least one 16K aligned region.
    482 	 */
    483 
    484 #ifdef VERBOSE_INIT_ARM
    485 	printf("Allocating page tables\n");
    486 #endif
    487 
    488 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    489 
    490 #ifdef VERBOSE_INIT_ARM
    491 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    492 	       physical_freestart, free_pages, free_pages);
    493 #endif
    494 
    495 	/* Define a macro to simplify memory allocation */
    496 #define	valloc_pages(var, np)				\
    497 	alloc_pages((var).pv_pa, (np));			\
    498 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    499 
    500 #define alloc_pages(var, np)				\
    501 	physical_freeend -= ((np) * PAGE_SIZE);		\
    502 	if (physical_freeend < physical_freestart)	\
    503 		panic("initarm: out of memory");	\
    504 	(var) = physical_freeend;			\
    505 	free_pages -= (np);				\
    506 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    507 
    508 	loop1 = 0;
    509 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    510 		/* Are we 16KB aligned for an L1 ? */
    511 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    512 		    && kernel_l1pt.pv_pa == 0) {
    513 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    514 		} else {
    515 			valloc_pages(kernel_pt_table[loop1],
    516 			    L2_TABLE_SIZE / PAGE_SIZE);
    517 			++loop1;
    518 		}
    519 	}
    520 
    521 	/* This should never be able to happen but better confirm that. */
    522 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    523 		panic("initarm: Failed to align the kernel page directory");
    524 
    525 	/*
    526 	 * Allocate a page for the system vectors page
    527 	 */
    528 	alloc_pages(systempage.pv_pa, 1);
    529 
    530 	/* Allocate stacks for all modes */
    531 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    532 	valloc_pages(abtstack, ABT_STACK_SIZE);
    533 	valloc_pages(undstack, UND_STACK_SIZE);
    534 	valloc_pages(kernelstack, UPAGES);
    535 
    536 #ifdef VERBOSE_INIT_ARM
    537 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    538 	    irqstack.pv_va);
    539 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    540 	    abtstack.pv_va);
    541 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    542 	    undstack.pv_va);
    543 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    544 	    kernelstack.pv_va);
    545 #endif
    546 
    547 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    548 
    549 	/*
    550 	 * Ok we have allocated physical pages for the primary kernel
    551 	 * page tables.  Save physical_freeend for when we give whats left
    552 	 * of memory below 2Mbyte to UVM.
    553 	 */
    554 
    555 	physical_freeend_low = physical_freeend;
    556 
    557 #ifdef VERBOSE_INIT_ARM
    558 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    559 #endif
    560 
    561 	/*
    562 	 * Now we start construction of the L1 page table
    563 	 * We start by mapping the L2 page tables into the L1.
    564 	 * This means that we can replace L1 mappings later on if necessary
    565 	 */
    566 	l1pagetable = kernel_l1pt.pv_pa;
    567 
    568 	/* Map the L2 pages tables in the L1 page table */
    569 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    570 	    &kernel_pt_table[KERNEL_PT_SYS]);
    571 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    572 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    573 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    574 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    575 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    576 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    577 
    578 	/* update the top of the kernel VM */
    579 	pmap_curmaxkvaddr =
    580 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    581 
    582 #ifdef VERBOSE_INIT_ARM
    583 	printf("Mapping kernel\n");
    584 #endif
    585 
    586 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    587 	{
    588 		extern char etext[], _end[];
    589 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    590 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    591 		u_int logical;
    592 
    593 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    594 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    595 
    596 		logical = 0x00200000;	/* offset of kernel in RAM */
    597 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    598 		    physical_start + logical, textsize,
    599 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    600 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    601 		    physical_start + logical, totalsize - textsize,
    602 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    603 	}
    604 
    605 #ifdef VERBOSE_INIT_ARM
    606 	printf("Constructing L2 page tables\n");
    607 #endif
    608 
    609 	/* Map the stack pages */
    610 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    611 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    612 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    613 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    614 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    615 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    616 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    617 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    618 
    619 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    620 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    621 
    622 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    623 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    624 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    625 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    626 	}
    627 
    628 	/* Map the vector page. */
    629 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    630 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    631 
    632 	/* Map the statically mapped devices. */
    633 	pmap_devmap_bootstrap(l1pagetable, tsarm_devmap);
    634 
    635 	/*
    636 	 * Update the physical_freestart/physical_freeend/free_pages
    637 	 * variables.
    638 	 */
    639 	{
    640 		extern char _end[];
    641 
    642 		physical_freestart = physical_start +
    643 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    644 		     KERNEL_BASE);
    645 		physical_freeend = physical_end;
    646 		free_pages =
    647 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    648 	}
    649 
    650 	/*
    651 	 * Now we have the real page tables in place so we can switch to them.
    652 	 * Once this is done we will be running with the REAL kernel page
    653 	 * tables.
    654 	 */
    655 
    656 	/* Switch tables */
    657 #ifdef VERBOSE_INIT_ARM
    658 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    659 	       physical_freestart, free_pages, free_pages);
    660 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    661 #endif
    662 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    663 	cpu_setttb(kernel_l1pt.pv_pa);
    664 	cpu_tlb_flushID();
    665 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    666 
    667 	/*
    668 	 * Moved from cpu_startup() as data_abort_handler() references
    669 	 * this during uvm init
    670 	 */
    671 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    672 
    673 #ifdef VERBOSE_INIT_ARM
    674 	printf("done!\n");
    675 #endif
    676 
    677 #ifdef VERBOSE_INIT_ARM
    678 	printf("bootstrap done.\n");
    679 #endif
    680 
    681 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    682 
    683 	/*
    684 	 * Pages were allocated during the secondary bootstrap for the
    685 	 * stacks for different CPU modes.
    686 	 * We must now set the r13 registers in the different CPU modes to
    687 	 * point to these stacks.
    688 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    689 	 * of the stack memory.
    690 	 */
    691 #ifdef VERBOSE_INIT_ARM
    692 	printf("init subsystems: stacks ");
    693 #endif
    694 
    695 	set_stackptr(PSR_IRQ32_MODE,
    696 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    697 	set_stackptr(PSR_ABT32_MODE,
    698 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    699 	set_stackptr(PSR_UND32_MODE,
    700 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    701 
    702 	/*
    703 	 * Well we should set a data abort handler.
    704 	 * Once things get going this will change as we will need a proper
    705 	 * handler.
    706 	 * Until then we will use a handler that just panics but tells us
    707 	 * why.
    708 	 * Initialisation of the vectors will just panic on a data abort.
    709 	 * This just fills in a slightly better one.
    710 	 */
    711 #ifdef VERBOSE_INIT_ARM
    712 	printf("vectors ");
    713 #endif
    714 	data_abort_handler_address = (u_int)data_abort_handler;
    715 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    716 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    717 
    718 	/* Initialise the undefined instruction handlers */
    719 #ifdef VERBOSE_INIT_ARM
    720 	printf("undefined ");
    721 #endif
    722 	undefined_init();
    723 
    724 	/* Load memory into UVM. */
    725 #ifdef VERBOSE_INIT_ARM
    726 	printf("page ");
    727 #endif
    728 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    729 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    730 	    atop(physical_freestart), atop(physical_freeend),
    731 	    VM_FREELIST_DEFAULT);
    732 	uvm_page_physload(0, atop(physical_freeend_low),
    733 	    0, atop(physical_freeend_low),
    734 	    VM_FREELIST_DEFAULT);
    735 	/*
    736 	 * There is 32 Mb of memory on the TS-7200 in 4 8Mb chunks, so far
    737 	 * we've only been working with the first one mapped at 0x0.  Tell
    738 	 * UVM about the others.
    739 	 */
    740 	uvm_page_physload(atop(0x1000000), atop(0x1800000),
    741 	    atop(0x1000000), atop(0x1800000),
    742 	    VM_FREELIST_DEFAULT);
    743 	uvm_page_physload(atop(0x4000000), atop(0x4800000),
    744 	    atop(0x4000000), atop(0x4800000),
    745 	    VM_FREELIST_DEFAULT);
    746 	uvm_page_physload(atop(0x5000000), atop(0x5800000),
    747 	    atop(0x5000000), atop(0x5800000),
    748 	    VM_FREELIST_DEFAULT);
    749 
    750 	physmem = 0x2000000 / PAGE_SIZE;
    751 
    752 
    753 	/* Boot strap pmap telling it where the kernel page table is */
    754 #ifdef VERBOSE_INIT_ARM
    755 	printf("pmap ");
    756 #endif
    757 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    758 
    759 	/* Setup the IRQ system */
    760 #ifdef VERBOSE_INIT_ARM
    761 	printf("irq ");
    762 #endif
    763 	ep93xx_intr_init();
    764 #if NISA > 0
    765 	isa_intr_init();
    766 
    767 #ifdef VERBOSE_INIT_ARM
    768 	printf("isa ");
    769 #endif
    770 	isa_tsarm_init(TS7XXX_IO16_VBASE + TS7XXX_ISAIO,
    771 		TS7XXX_IO16_VBASE + TS7XXX_ISAMEM);
    772 #endif
    773 
    774 #ifdef VERBOSE_INIT_ARM
    775 	printf("done.\n");
    776 #endif
    777 
    778 #ifdef BOOTHOWTO
    779 	boothowto = BOOTHOWTO;
    780 #endif
    781 
    782 #ifdef DDB
    783 	db_machine_init();
    784 	if (boothowto & RB_KDB)
    785 		Debugger();
    786 #endif
    787 
    788 	/* We return the new stack pointer address */
    789 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    790 }
    791 
    792 void
    793 consinit(void)
    794 {
    795 	static int consinit_called;
    796 	bus_space_handle_t ioh;
    797 
    798 	if (consinit_called != 0)
    799 		return;
    800 
    801 	consinit_called = 1;
    802 
    803 	/*
    804 	 * Console devices are already mapped in VA.  Our devmap reflects
    805 	 * this, so register it now so drivers can map the console
    806 	 * device.
    807 	 */
    808 	pmap_devmap_register(tsarm_devmap);
    809 #if 0
    810 	isa_tsarm_init(TS7XXX_IO16_VBASE + TS7XXX_ISAIO,
    811 		TS7XXX_IO16_VBASE + TS7XXX_ISAMEM);
    812 
    813         if (comcnattach(&isa_io_bs_tag, 0x3e8, comcnspeed,
    814             COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    815         {
    816                 panic("can't init serial console");
    817         }
    818 #endif
    819 
    820 #if NEPCOM > 0
    821 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_UART1,
    822 		EP93XX_APB_UART_SIZE, 0, &ioh);
    823         if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_UART1,
    824 		ioh, comcnspeed, comcnmode))
    825 	{
    826 		panic("can't init serial console");
    827 	}
    828 #else
    829 	panic("serial console not configured");
    830 #endif
    831 #if KGDB
    832 #if NEPCOM > 0
    833 	if (strcmp(kgdb_devname, "epcom") == 0) {
    834 		com_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
    835 			kgdb_devmode);
    836 	}
    837 #endif	/* NEPCOM > 0 */
    838 #endif	/* KGDB */
    839 }
    840 
    841 
    842 bus_dma_tag_t
    843 ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    844 {
    845 	int i;
    846 	struct arm32_bus_dma_tag *dmat;
    847 
    848 	for (i = 0; i < bootconfig.dramblocks; i++) {
    849 		tsarm_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    850 		tsarm_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    851 		tsarm_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    852 			PAGE_SIZE;
    853 	}
    854 
    855 	dmat = dma_tag_template;
    856 
    857 	dmat->_ranges = tsarm_dma_ranges;
    858 	dmat->_nranges = bootconfig.dramblocks;
    859 
    860 	return dmat;
    861 }
    862