Home | History | Annotate | Line # | Download | only in tsarm
tsarm_machdep.c revision 1.23
      1 /*	$NetBSD: tsarm_machdep.c,v 1.23 2014/09/13 18:08:39 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.23 2014/09/13 18:08:39 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 #include <sys/bus.h>
     93 #include <sys/cpu.h>
     94 
     95 #include <uvm/uvm_extern.h>
     96 
     97 #include <dev/cons.h>
     98 
     99 #include <machine/db_machdep.h>
    100 #include <ddb/db_sym.h>
    101 #include <ddb/db_extern.h>
    102 
    103 #include <acorn32/include/bootconfig.h>
    104 #include <arm/locore.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 vaddr_t physical_start;
    152 vaddr_t physical_freestart;
    153 vaddr_t physical_freeend;
    154 vaddr_t physical_freeend_low;
    155 vaddr_t physical_end;
    156 u_int free_pages;
    157 
    158 paddr_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 		uint32_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 
    382 #ifdef FIXME
    383 	paddr_t memstart;
    384 	psize_t memsize;
    385 
    386 	/* Calibrate the delay loop. */
    387 	i80321_calibrate_delay();
    388 #endif
    389 
    390 	/*
    391 	 * Since we map the on-board devices VA==PA, and the kernel
    392 	 * is running VA==PA, it's possible for us to initialize
    393 	 * the console now.
    394 	 */
    395 	consinit();
    396 
    397 #ifdef VERBOSE_INIT_ARM
    398 	/* Talk to the user */
    399 	printf("\nNetBSD/tsarm booting ...\n");
    400 #endif
    401 
    402 	/*
    403 	 * Heads up ... Setup the CPU / MMU / TLB functions
    404 	 */
    405 	if (set_cpufuncs())
    406 		panic("cpu not recognized!");
    407 
    408 	/*
    409 	 * We are currently running with the MMU enabled
    410 	 */
    411 
    412 #ifdef FIXME
    413 	/*
    414 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
    415 	 * registers.
    416 	 */
    417 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    418 	    &memstart, &memsize);
    419 
    420 	memstart = 0x0;
    421 	memsize = 0x2000000;
    422 #endif
    423 
    424 #ifdef VERBOSE_INIT_ARM
    425 	printf("initarm: Configuring system ...\n");
    426 #endif
    427 
    428 	/* Fake bootconfig structure for the benefit of pmap.c */
    429 	/* XXX must make the memory description h/w independent */
    430 	bootconfig.dramblocks = 4;
    431 	bootconfig.dram[0].address = 0x0UL;
    432 	bootconfig.dram[0].pages = 0x800000UL / PAGE_SIZE;
    433 	bootconfig.dram[1].address = 0x1000000UL;
    434 	bootconfig.dram[1].pages = 0x800000UL / PAGE_SIZE;
    435 	bootconfig.dram[2].address = 0x4000000UL;
    436 	bootconfig.dram[2].pages = 0x800000UL / PAGE_SIZE;
    437 	bootconfig.dram[3].address = 0x5000000UL;
    438 	bootconfig.dram[3].pages = 0x800000UL / PAGE_SIZE;
    439 
    440 	/*
    441 	 * Set up the variables that define the availablilty of
    442 	 * physical memory.  For now, we're going to set
    443 	 * physical_freestart to 0x00200000 (where the kernel
    444 	 * was loaded), and allocate the memory we need downwards.
    445 	 * If we get too close to the L1 table that we set up, we
    446 	 * will panic.  We will update physical_freestart and
    447 	 * physical_freeend later to reflect what pmap_bootstrap()
    448 	 * wants to see.
    449 	 *
    450 	 * XXX pmap_bootstrap() needs an enema.
    451 	 */
    452 	physical_start = bootconfig.dram[0].address;
    453 	physical_end = bootconfig.dram[0].address +
    454 		(bootconfig.dram[0].pages * PAGE_SIZE);
    455 
    456 	physical_freestart = 0x00009000UL;
    457 	physical_freeend = 0x00200000UL;
    458 
    459 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    460 
    461 #ifdef VERBOSE_INIT_ARM
    462 	/* Tell the user about the memory */
    463 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    464 	    physical_start, physical_end - 1);
    465 #endif
    466 
    467 	/*
    468 	 * Okay, the kernel starts 2MB in from the bottom of physical
    469 	 * memory.  We are going to allocate our bootstrap pages downwards
    470 	 * from there.
    471 	 *
    472 	 * We need to allocate some fixed page tables to get the kernel
    473 	 * going.  We allocate one page directory and a number of page
    474 	 * tables and store the physical addresses in the kernel_pt_table
    475 	 * array.
    476 	 *
    477 	 * The kernel page directory must be on a 16K boundary.  The page
    478 	 * tables must be on 4K bounaries.  What we do is allocate the
    479 	 * page directory on the first 16K boundary that we encounter, and
    480 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    481 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    482 	 * least one 16K aligned region.
    483 	 */
    484 
    485 #ifdef VERBOSE_INIT_ARM
    486 	printf("Allocating page tables\n");
    487 #endif
    488 
    489 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    490 
    491 #ifdef VERBOSE_INIT_ARM
    492 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    493 	       physical_freestart, free_pages, free_pages);
    494 #endif
    495 
    496 	/* Define a macro to simplify memory allocation */
    497 #define	valloc_pages(var, np)				\
    498 	alloc_pages((var).pv_pa, (np));			\
    499 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    500 
    501 #define alloc_pages(var, np)				\
    502 	physical_freeend -= ((np) * PAGE_SIZE);		\
    503 	if (physical_freeend < physical_freestart)	\
    504 		panic("initarm: out of memory");	\
    505 	(var) = physical_freeend;			\
    506 	free_pages -= (np);				\
    507 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    508 
    509 	loop1 = 0;
    510 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    511 		/* Are we 16KB aligned for an L1 ? */
    512 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    513 		    && kernel_l1pt.pv_pa == 0) {
    514 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    515 		} else {
    516 			valloc_pages(kernel_pt_table[loop1],
    517 			    L2_TABLE_SIZE / PAGE_SIZE);
    518 			++loop1;
    519 		}
    520 	}
    521 
    522 	/* This should never be able to happen but better confirm that. */
    523 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    524 		panic("initarm: Failed to align the kernel page directory");
    525 
    526 	/*
    527 	 * Allocate a page for the system vectors page
    528 	 */
    529 	alloc_pages(systempage.pv_pa, 1);
    530 
    531 	/* Allocate stacks for all modes */
    532 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    533 	valloc_pages(abtstack, ABT_STACK_SIZE);
    534 	valloc_pages(undstack, UND_STACK_SIZE);
    535 	valloc_pages(kernelstack, UPAGES);
    536 
    537 #ifdef VERBOSE_INIT_ARM
    538 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    539 	    irqstack.pv_va);
    540 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    541 	    abtstack.pv_va);
    542 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    543 	    undstack.pv_va);
    544 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    545 	    kernelstack.pv_va);
    546 #endif
    547 
    548 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    549 
    550 	/*
    551 	 * Ok we have allocated physical pages for the primary kernel
    552 	 * page tables.  Save physical_freeend for when we give whats left
    553 	 * of memory below 2Mbyte to UVM.
    554 	 */
    555 
    556 	physical_freeend_low = physical_freeend;
    557 
    558 #ifdef VERBOSE_INIT_ARM
    559 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    560 #endif
    561 
    562 	/*
    563 	 * Now we start construction of the L1 page table
    564 	 * We start by mapping the L2 page tables into the L1.
    565 	 * This means that we can replace L1 mappings later on if necessary
    566 	 */
    567 	l1pagetable = kernel_l1pt.pv_pa;
    568 
    569 	/* Map the L2 pages tables in the L1 page table */
    570 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    571 	    &kernel_pt_table[KERNEL_PT_SYS]);
    572 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    573 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    574 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    575 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    576 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    577 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    578 
    579 	/* update the top of the kernel VM */
    580 	pmap_curmaxkvaddr =
    581 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    582 
    583 #ifdef VERBOSE_INIT_ARM
    584 	printf("Mapping kernel\n");
    585 #endif
    586 
    587 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    588 	{
    589 		extern char etext[], _end[];
    590 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    591 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    592 		u_int logical;
    593 
    594 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    595 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    596 
    597 		logical = 0x00200000;	/* offset of kernel in RAM */
    598 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    599 		    physical_start + logical, textsize,
    600 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    601 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    602 		    physical_start + logical, totalsize - textsize,
    603 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    604 	}
    605 
    606 #ifdef VERBOSE_INIT_ARM
    607 	printf("Constructing L2 page tables\n");
    608 #endif
    609 
    610 	/* Map the stack pages */
    611 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    612 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    613 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    614 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    615 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    616 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    617 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    618 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    619 
    620 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    621 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    622 
    623 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    624 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    625 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    626 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    627 	}
    628 
    629 	/* Map the vector page. */
    630 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    631 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    632 
    633 	/* Map the statically mapped devices. */
    634 	pmap_devmap_bootstrap(l1pagetable, tsarm_devmap);
    635 
    636 	/*
    637 	 * Update the physical_freestart/physical_freeend/free_pages
    638 	 * variables.
    639 	 */
    640 	{
    641 		extern char _end[];
    642 
    643 		physical_freestart = physical_start +
    644 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    645 		     KERNEL_BASE);
    646 		physical_freeend = physical_end;
    647 		free_pages =
    648 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    649 	}
    650 
    651 	/*
    652 	 * Now we have the real page tables in place so we can switch to them.
    653 	 * Once this is done we will be running with the REAL kernel page
    654 	 * tables.
    655 	 */
    656 
    657 	/* Switch tables */
    658 #ifdef VERBOSE_INIT_ARM
    659 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    660 	       physical_freestart, free_pages, free_pages);
    661 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    662 #endif
    663 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    664 	cpu_setttb(kernel_l1pt.pv_pa, true);
    665 	cpu_tlb_flushID();
    666 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    667 
    668 	/*
    669 	 * Moved from cpu_startup() as data_abort_handler() references
    670 	 * this during uvm init
    671 	 */
    672 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    673 
    674 #ifdef VERBOSE_INIT_ARM
    675 	printf("done!\n");
    676 #endif
    677 
    678 #ifdef VERBOSE_INIT_ARM
    679 	printf("bootstrap done.\n");
    680 #endif
    681 
    682 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    683 
    684 	/*
    685 	 * Pages were allocated during the secondary bootstrap for the
    686 	 * stacks for different CPU modes.
    687 	 * We must now set the r13 registers in the different CPU modes to
    688 	 * point to these stacks.
    689 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    690 	 * of the stack memory.
    691 	 */
    692 #ifdef VERBOSE_INIT_ARM
    693 	printf("init subsystems: stacks ");
    694 #endif
    695 
    696 	set_stackptr(PSR_IRQ32_MODE,
    697 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    698 	set_stackptr(PSR_ABT32_MODE,
    699 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    700 	set_stackptr(PSR_UND32_MODE,
    701 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    702 
    703 	/*
    704 	 * Well we should set a data abort handler.
    705 	 * Once things get going this will change as we will need a proper
    706 	 * handler.
    707 	 * Until then we will use a handler that just panics but tells us
    708 	 * why.
    709 	 * Initialisation of the vectors will just panic on a data abort.
    710 	 * This just fills in a slightly better one.
    711 	 */
    712 #ifdef VERBOSE_INIT_ARM
    713 	printf("vectors ");
    714 #endif
    715 	data_abort_handler_address = (u_int)data_abort_handler;
    716 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    717 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    718 
    719 	/* Initialise the undefined instruction handlers */
    720 #ifdef VERBOSE_INIT_ARM
    721 	printf("undefined ");
    722 #endif
    723 	undefined_init();
    724 
    725 	/* Load memory into UVM. */
    726 #ifdef VERBOSE_INIT_ARM
    727 	printf("page ");
    728 #endif
    729 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    730 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    731 	    atop(physical_freestart), atop(physical_freeend),
    732 	    VM_FREELIST_DEFAULT);
    733 	uvm_page_physload(0, atop(physical_freeend_low),
    734 	    0, atop(physical_freeend_low),
    735 	    VM_FREELIST_DEFAULT);
    736 	/*
    737 	 * There is 32 Mb of memory on the TS-7200 in 4 8Mb chunks, so far
    738 	 * we've only been working with the first one mapped at 0x0.  Tell
    739 	 * UVM about the others.
    740 	 */
    741 	uvm_page_physload(atop(0x1000000), atop(0x1800000),
    742 	    atop(0x1000000), atop(0x1800000),
    743 	    VM_FREELIST_DEFAULT);
    744 	uvm_page_physload(atop(0x4000000), atop(0x4800000),
    745 	    atop(0x4000000), atop(0x4800000),
    746 	    VM_FREELIST_DEFAULT);
    747 	uvm_page_physload(atop(0x5000000), atop(0x5800000),
    748 	    atop(0x5000000), atop(0x5800000),
    749 	    VM_FREELIST_DEFAULT);
    750 
    751 	physmem = 0x2000000 / PAGE_SIZE;
    752 
    753 
    754 	/* Boot strap pmap telling it where the kernel page table is */
    755 #ifdef VERBOSE_INIT_ARM
    756 	printf("pmap ");
    757 #endif
    758 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    759 
    760 	/* Setup the IRQ system */
    761 #ifdef VERBOSE_INIT_ARM
    762 	printf("irq ");
    763 #endif
    764 	ep93xx_intr_init();
    765 #if NISA > 0
    766 	isa_intr_init();
    767 
    768 #ifdef VERBOSE_INIT_ARM
    769 	printf("isa ");
    770 #endif
    771 	isa_tsarm_init(TS7XXX_IO16_VBASE + TS7XXX_ISAIO,
    772 		TS7XXX_IO16_VBASE + TS7XXX_ISAMEM);
    773 #endif
    774 
    775 #ifdef VERBOSE_INIT_ARM
    776 	printf("done.\n");
    777 #endif
    778 
    779 #ifdef BOOTHOWTO
    780 	boothowto = BOOTHOWTO;
    781 #endif
    782 
    783 #ifdef DDB
    784 	db_machine_init();
    785 	if (boothowto & RB_KDB)
    786 		Debugger();
    787 #endif
    788 
    789 	/* We return the new stack pointer address */
    790 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    791 }
    792 
    793 void
    794 consinit(void)
    795 {
    796 	static int consinit_called;
    797 	bus_space_handle_t ioh;
    798 
    799 	if (consinit_called != 0)
    800 		return;
    801 
    802 	consinit_called = 1;
    803 
    804 	/*
    805 	 * Console devices are already mapped in VA.  Our devmap reflects
    806 	 * this, so register it now so drivers can map the console
    807 	 * device.
    808 	 */
    809 	pmap_devmap_register(tsarm_devmap);
    810 #if 0
    811 	isa_tsarm_init(TS7XXX_IO16_VBASE + TS7XXX_ISAIO,
    812 		TS7XXX_IO16_VBASE + TS7XXX_ISAMEM);
    813 
    814         if (comcnattach(&isa_io_bs_tag, 0x3e8, comcnspeed,
    815             COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    816         {
    817                 panic("can't init serial console");
    818         }
    819 #endif
    820 
    821 #if NEPCOM > 0
    822 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_UART1,
    823 		EP93XX_APB_UART_SIZE, 0, &ioh);
    824         if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_UART1,
    825 		ioh, comcnspeed, comcnmode))
    826 	{
    827 		panic("can't init serial console");
    828 	}
    829 #else
    830 	panic("serial console not configured");
    831 #endif
    832 #if KGDB
    833 #if NEPCOM > 0
    834 	if (strcmp(kgdb_devname, "epcom") == 0) {
    835 		com_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
    836 			kgdb_devmode);
    837 	}
    838 #endif	/* NEPCOM > 0 */
    839 #endif	/* KGDB */
    840 }
    841 
    842 
    843 bus_dma_tag_t
    844 ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    845 {
    846 	int i;
    847 	struct arm32_bus_dma_tag *dmat;
    848 
    849 	for (i = 0; i < bootconfig.dramblocks; i++) {
    850 		tsarm_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    851 		tsarm_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    852 		tsarm_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    853 			PAGE_SIZE;
    854 	}
    855 
    856 	dmat = dma_tag_template;
    857 
    858 	dmat->_ranges = tsarm_dma_ranges;
    859 	dmat->_nranges = bootconfig.dramblocks;
    860 
    861 	return dmat;
    862 }
    863