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