Home | History | Annotate | Line # | Download | only in adi_brh
brh_machdep.c revision 1.1
      1 /*	$NetBSD: brh_machdep.c,v 1.1 2003/01/25 02:00:16 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1997,1998 Mark Brinicombe.
     40  * Copyright (c) 1997,1998 Causality Limited.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Mark Brinicombe
     54  *	for the NetBSD Project.
     55  * 4. The name of the company nor the name of the author may be used to
     56  *    endorse or promote products derived from this software without specific
     57  *    prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * Machine dependant functions for kernel setup for the ADI Engineering
     72  * BRH i80200 evaluation platform.
     73  */
     74 
     75 #include "opt_ddb.h"
     76 #include "opt_pmap_debug.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/device.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/exec.h>
     83 #include <sys/proc.h>
     84 #include <sys/msgbuf.h>
     85 #include <sys/reboot.h>
     86 #include <sys/termios.h>
     87 
     88 #include <dev/cons.h>
     89 
     90 #include <machine/db_machdep.h>
     91 #include <ddb/db_sym.h>
     92 #include <ddb/db_extern.h>
     93 
     94 #include <machine/bootconfig.h>
     95 #include <machine/bus.h>
     96 #include <machine/cpu.h>
     97 #include <machine/frame.h>
     98 #include <arm/undefined.h>
     99 
    100 #include <arm/arm32/machdep.h>
    101 
    102 #include <arm/xscale/i80200reg.h>
    103 #include <arm/xscale/i80200var.h>
    104 
    105 #include <dev/pci/ppbreg.h>
    106 
    107 #include <arm/xscale/beccreg.h>
    108 #include <arm/xscale/beccvar.h>
    109 
    110 #include <evbarm/adi_brh/brhreg.h>
    111 #include <evbarm/adi_brh/brhvar.h>
    112 #include <evbarm/adi_brh/obiovar.h>
    113 
    114 #include "opt_ipkdb.h"
    115 
    116 /*
    117  * Address to call from cpu_reset() to reset the machine.
    118  * This is machine architecture dependant as it varies depending
    119  * on where the ROM appears when you turn the MMU off.
    120  */
    121 
    122 u_int cpu_reset_address = 0x00000000;
    123 
    124 /* Define various stack sizes in pages */
    125 #define IRQ_STACK_SIZE	1
    126 #define ABT_STACK_SIZE	1
    127 #ifdef IPKDB
    128 #define UND_STACK_SIZE	2
    129 #else
    130 #define UND_STACK_SIZE	1
    131 #endif
    132 
    133 BootConfig bootconfig;		/* Boot config storage */
    134 char *boot_args = NULL;
    135 char *boot_file = NULL;
    136 
    137 vm_offset_t physical_start;
    138 vm_offset_t physical_freestart;
    139 vm_offset_t physical_freeend;
    140 vm_offset_t physical_end;
    141 u_int free_pages;
    142 vm_offset_t pagetables_start;
    143 int physmem = 0;
    144 
    145 /*int debug_flags;*/
    146 #ifndef PMAP_STATIC_L1S
    147 int max_processes = 64;			/* Default number */
    148 #endif	/* !PMAP_STATIC_L1S */
    149 
    150 /* Physical and virtual addresses for some global pages */
    151 pv_addr_t systempage;
    152 pv_addr_t irqstack;
    153 pv_addr_t undstack;
    154 pv_addr_t abtstack;
    155 pv_addr_t kernelstack;
    156 pv_addr_t minidataclean;
    157 
    158 vm_offset_t msgbufphys;
    159 
    160 extern u_int data_abort_handler_address;
    161 extern u_int prefetch_abort_handler_address;
    162 extern u_int undefined_handler_address;
    163 
    164 #ifdef PMAP_DEBUG
    165 extern int pmap_debug_level;
    166 #endif
    167 
    168 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    169 
    170 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    171 #define	KERNEL_PT_KERNEL_NUM	2
    172 
    173 					/* L2 tables for mapping kernel VM */
    174 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    175 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    176 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    177 
    178 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    179 
    180 struct user *proc0paddr;
    181 
    182 /* Prototypes */
    183 
    184 void	consinit(void);
    185 
    186 #include "com.h"
    187 #if NCOM > 0
    188 #include <dev/ic/comreg.h>
    189 #include <dev/ic/comvar.h>
    190 #endif
    191 
    192 /*
    193  * Define the default console speed for the board.  This is generally
    194  * what the firmware provided with the board defaults to.
    195  */
    196 #ifndef CONSPEED
    197 #define CONSPEED B57600
    198 #endif /* ! CONSPEED */
    199 
    200 #ifndef CONUNIT
    201 #define	CONUNIT	0
    202 #endif
    203 
    204 #ifndef CONMODE
    205 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    206 #endif
    207 
    208 int comcnspeed = CONSPEED;
    209 int comcnmode = CONMODE;
    210 int comcnunit = CONUNIT;
    211 
    212 /*
    213  * void cpu_reboot(int howto, char *bootstr)
    214  *
    215  * Reboots the system
    216  *
    217  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    218  * then reset the CPU.
    219  */
    220 void
    221 cpu_reboot(int howto, char *bootstr)
    222 {
    223 #ifdef DIAGNOSTIC
    224 	/* info */
    225 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    226 #endif
    227 
    228 	/*
    229 	 * If we are still cold then hit the air brakes
    230 	 * and crash to earth fast
    231 	 */
    232 	if (cold) {
    233 		doshutdownhooks();
    234 		printf("The operating system has halted.\n");
    235 		printf("Please press any key to reboot.\n\n");
    236 		cngetc();
    237 		printf("rebooting...\n");
    238 		goto reset;
    239 	}
    240 
    241 	/* Disable console buffering */
    242 
    243 	/*
    244 	 * If RB_NOSYNC was not specified sync the discs.
    245 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    246 	 * unmount.  It looks like syslogd is getting woken up only to find
    247 	 * that it cannot page part of the binary in as the filesystem has
    248 	 * been unmounted.
    249 	 */
    250 	if (!(howto & RB_NOSYNC))
    251 		bootsync();
    252 
    253 	/* Say NO to interrupts */
    254 	splhigh();
    255 
    256 	/* Do a dump if requested. */
    257 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    258 		dumpsys();
    259 
    260 	/* Run any shutdown hooks */
    261 	doshutdownhooks();
    262 
    263 	/* Make sure IRQ's are disabled */
    264 	IRQdisable;
    265 
    266 	if (howto & RB_HALT) {
    267 		brh_7seg('8');
    268 		printf("The operating system has halted.\n");
    269 		printf("Please press any key to reboot.\n\n");
    270 		cngetc();
    271 	}
    272 
    273 	printf("rebooting...\n\r");
    274  reset:
    275 	cpu_reset();
    276 }
    277 
    278 /*
    279  * Mapping table for core kernel memory. This memory is mapped at init
    280  * time with section mappings.
    281  */
    282 struct l1_sec_map {
    283 	vaddr_t	va;
    284 	vaddr_t	pa;
    285 	vsize_t	size;
    286 	vm_prot_t prot;
    287 	int cache;
    288 } l1_sec_table[] = {
    289     {
    290 	BRH_PCI_CONF_VBASE,
    291 	BECC_PCI_CONF_BASE,
    292 	BRH_PCI_CONF_VSIZE,
    293 	VM_PROT_READ|VM_PROT_WRITE,
    294 	PTE_NOCACHE,
    295     },
    296     {
    297 	BRH_PCI_MEM1_VBASE,
    298 	BECC_PCI_MEM1_BASE,
    299 	BRH_PCI_MEM1_VSIZE,
    300 	VM_PROT_READ|VM_PROT_WRITE,
    301 	PTE_NOCACHE,
    302     },
    303     {
    304 	BRH_PCI_MEM2_VBASE,
    305 	BECC_PCI_MEM2_BASE,
    306 	BRH_PCI_MEM2_VSIZE,
    307 	VM_PROT_READ|VM_PROT_WRITE,
    308 	PTE_NOCACHE,
    309     },
    310     {
    311 	BRH_UART1_VBASE,
    312 	BRH_UART1_BASE,
    313 	BRH_UART1_VSIZE,
    314 	VM_PROT_READ|VM_PROT_WRITE,
    315 	PTE_NOCACHE,
    316     },
    317     {
    318 	BRH_UART2_VBASE,
    319 	BRH_UART2_BASE,
    320 	BRH_UART2_VSIZE,
    321 	VM_PROT_READ|VM_PROT_WRITE,
    322 	PTE_NOCACHE,
    323     },
    324     {
    325 	BRH_LED_VBASE,
    326 	BRH_LED_BASE,
    327 	BRH_LED_VSIZE,
    328 	VM_PROT_READ|VM_PROT_WRITE,
    329 	PTE_NOCACHE,
    330     },
    331     {
    332 	BRH_PCI_IO_VBASE,
    333 	BECC_PCI_IO_BASE,
    334 	BRH_PCI_IO_VSIZE,
    335 	VM_PROT_READ|VM_PROT_WRITE,
    336 	PTE_NOCACHE,
    337     },
    338     {
    339 	BRH_BECC_VBASE,
    340 	BECC_REG_BASE,
    341 	BRH_BECC_VSIZE,
    342 	VM_PROT_READ|VM_PROT_WRITE,
    343 	PTE_NOCACHE,
    344     },
    345     {
    346 	0,
    347 	0,
    348 	0,
    349 	0,
    350 	0,
    351     }
    352 };
    353 
    354 static void
    355 brh_hardclock_hook(void)
    356 {
    357 	static int snakefreq;
    358 
    359 	if ((snakefreq++ & 15) == 0)
    360 		brh_7seg_snake();
    361 }
    362 
    363 /*
    364  * u_int initarm(...)
    365  *
    366  * Initial entry point on startup. This gets called before main() is
    367  * entered.
    368  * It should be responsible for setting up everything that must be
    369  * in place when main is called.
    370  * This includes
    371  *   Taking a copy of the boot configuration structure.
    372  *   Initialising the physical console so characters can be printed.
    373  *   Setting up page tables for the kernel
    374  *   Relocating the kernel to the bottom of physical memory
    375  */
    376 u_int
    377 initarm(void *arg)
    378 {
    379 	extern vaddr_t xscale_cache_clean_addr;
    380 	extern vsize_t xscale_minidata_clean_size;
    381 	int loop;
    382 	int loop1;
    383 	u_int l1pagetable;
    384 	pv_addr_t kernel_l1pt;
    385 	pv_addr_t kernel_ptpt;
    386 	paddr_t memstart;
    387 	psize_t memsize;
    388 
    389 	/*
    390 	 * Clear out the 7-segment display.  Whee, the first visual
    391 	 * indication that we're running kernel code.
    392 	 */
    393 	brh_7seg(' ');
    394 
    395 	/*
    396 	 * Since we have mapped the on-board devices at their permanent
    397 	 * locations already, it is possible for us to initialize
    398 	 * the console now.
    399 	 */
    400 	consinit();
    401 
    402 	/* Talk to the user */
    403 	printf("\nNetBSD/evbarm (ADI BRH) booting ...\n");
    404 
    405 	/* Calibrate the delay loop. */
    406 	becc_calibrate_delay();
    407 	becc_hardclock_hook = brh_hardclock_hook;
    408 
    409 	/*
    410 	 * Heads up ... Setup the CPU / MMU / TLB functions
    411 	 */
    412 	if (set_cpufuncs())
    413 		panic("cpu not recognized!");
    414 
    415 	/*
    416 	 * We are currently running with the MMU enabled and the
    417 	 * entire address space mapped VA==PA.  Memory conveniently
    418 	 * starts at 0xc0000000, which is where we want it.  Certain
    419 	 * on-board devices have already been mapped where we want
    420 	 * them to be.  There is an L1 page table at 0xc0004000.
    421 	 */
    422 
    423 	becc_icu_init();
    424 
    425 	/*
    426 	 * Memory always starts at 0xc0000000 on a BRH, and the
    427 	 * memory size is always 128M.
    428 	 */
    429 	memstart = 0xc0000000UL;
    430 	memsize = (128UL * 1024 * 1024);
    431 
    432 	printf("initarm: Configuring system ...\n");
    433 
    434 	/* Fake bootconfig structure for the benefit of pmap.c */
    435 	/* XXX must make the memory description h/w independant */
    436 	bootconfig.dramblocks = 1;
    437 	bootconfig.dram[0].address = memstart;
    438 	bootconfig.dram[0].pages = memsize / NBPG;
    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 0xc0200000 (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 = physical_start + (bootconfig.dram[0].pages * NBPG);
    454 
    455 	physical_freestart = 0xc0009000UL;
    456 	physical_freeend = 0xc0200000UL;
    457 
    458 	/* Tell the user about the memory */
    459 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    460 	    physical_start, physical_end - 1);
    461 
    462 	/*
    463 	 * Okay, the kernel starts 2MB in from the bottom of physical
    464 	 * memory.  We are going to allocate our bootstrap pages downwards
    465 	 * from there.
    466 	 *
    467 	 * We need to allocate some fixed page tables to get the kernel
    468 	 * going.  We allocate one page directory and a number of page
    469 	 * tables and store the physical addresses in the kernel_pt_table
    470 	 * array.
    471 	 *
    472 	 * The kernel page directory must be on a 16K boundary.  The page
    473 	 * tables must be on 4K bounaries.  What we do is allocate the
    474 	 * page directory on the first 16K boundary that we encounter, and
    475 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    476 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    477 	 * least one 16K aligned region.
    478 	 */
    479 
    480 #ifdef VERBOSE_INIT_ARM
    481 	printf("Allocating page tables\n");
    482 #endif
    483 
    484 	free_pages = (physical_freeend - physical_freestart) / NBPG;
    485 
    486 #ifdef VERBOSE_INIT_ARM
    487 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    488 	       physical_freestart, free_pages, free_pages);
    489 #endif
    490 
    491 	/* Define a macro to simplify memory allocation */
    492 #define	valloc_pages(var, np)				\
    493 	alloc_pages((var).pv_pa, (np));			\
    494 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    495 
    496 #define alloc_pages(var, np)				\
    497 	physical_freeend -= ((np) * NBPG);		\
    498 	if (physical_freeend < physical_freestart)	\
    499 		panic("initarm: out of memory");	\
    500 	(var) = physical_freeend;			\
    501 	free_pages -= (np);				\
    502 	memset((char *)(var), 0, ((np) * NBPG));
    503 
    504 	loop1 = 0;
    505 	kernel_l1pt.pv_pa = 0;
    506 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    507 		/* Are we 16KB aligned for an L1 ? */
    508 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    509 		    && kernel_l1pt.pv_pa == 0) {
    510 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
    511 		} else {
    512 			alloc_pages(kernel_pt_table[loop1].pv_pa,
    513 			    L2_TABLE_SIZE / NBPG);
    514 			kernel_pt_table[loop1].pv_va =
    515 			    kernel_pt_table[loop1].pv_pa;
    516 			++loop1;
    517 		}
    518 	}
    519 
    520 	/* This should never be able to happen but better confirm that. */
    521 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    522 		panic("initarm: Failed to align the kernel page directory\n");
    523 
    524 	/*
    525 	 * Allocate a page for the system page mapped to V0x00000000
    526 	 * This page will just contain the system vectors and can be
    527 	 * shared by all processes.
    528 	 */
    529 	alloc_pages(systempage.pv_pa, 1);
    530 
    531 	/* Allocate a page for the page table to map kernel page tables. */
    532 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
    533 
    534 	/* Allocate stacks for all modes */
    535 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    536 	valloc_pages(abtstack, ABT_STACK_SIZE);
    537 	valloc_pages(undstack, UND_STACK_SIZE);
    538 	valloc_pages(kernelstack, UPAGES);
    539 
    540 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    541 	KASSERT(xscale_minidata_clean_size <= NBPG);
    542 	valloc_pages(minidataclean, 1);
    543 
    544 #ifdef VERBOSE_INIT_ARM
    545 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    546 	    irqstack.pv_va);
    547 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    548 	    abtstack.pv_va);
    549 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    550 	    undstack.pv_va);
    551 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    552 	    kernelstack.pv_va);
    553 #endif
    554 
    555 	/*
    556 	 * XXX Defer this to later so that we can reclaim the memory
    557 	 * XXX used by the RedBoot page tables.
    558 	 */
    559 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
    560 
    561 	/*
    562 	 * Ok we have allocated physical pages for the primary kernel
    563 	 * page tables
    564 	 */
    565 
    566 #ifdef VERBOSE_INIT_ARM
    567 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    568 #endif
    569 
    570 	/*
    571 	 * Now we start construction of the L1 page table
    572 	 * We start by mapping the L2 page tables into the L1.
    573 	 * This means that we can replace L1 mappings later on if necessary
    574 	 */
    575 	l1pagetable = kernel_l1pt.pv_pa;
    576 
    577 	/* Map the L2 pages tables in the L1 page table */
    578 	pmap_link_l2pt(l1pagetable, 0x00000000,
    579 	    &kernel_pt_table[KERNEL_PT_SYS]);
    580 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    581 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    582 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    583 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    584 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    585 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    586 	pmap_link_l2pt(l1pagetable, PTE_BASE, &kernel_ptpt);
    587 
    588 	/* update the top of the kernel VM */
    589 	pmap_curmaxkvaddr =
    590 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    591 
    592 #ifdef VERBOSE_INIT_ARM
    593 	printf("Mapping kernel\n");
    594 #endif
    595 
    596 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    597 	{
    598 		extern char etext[], _end[];
    599 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    600 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    601 		u_int logical;
    602 
    603 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    604 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    605 
    606 		logical = 0x00200000;	/* offset of kernel in RAM */
    607 
    608 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    609 		    physical_start + logical, textsize,
    610 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    611 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    612 		    physical_start + logical, totalsize - textsize,
    613 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    614 	}
    615 
    616 #ifdef VERBOSE_INIT_ARM
    617 	printf("Constructing L2 page tables\n");
    618 #endif
    619 
    620 	/* Map the stack pages */
    621 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    622 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    623 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    624 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    625 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    626 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    627 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    628 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    629 
    630 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    631 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    632 
    633 	/* Map the Mini-Data cache clean area. */
    634 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    635 	    minidataclean.pv_pa);
    636 
    637 	/* Map the page table that maps the kernel pages */
    638 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
    639 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    640 
    641 	/*
    642 	 * Map entries in the page table used to map PTE's
    643 	 * Basically every kernel page table gets mapped here
    644 	 */
    645 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
    646 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++) {
    647 		pmap_map_entry(l1pagetable,
    648 		    PTE_BASE + ((KERNEL_BASE +
    649 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
    650 		    kernel_pt_table[KERNEL_PT_KERNEL + loop].pv_pa,
    651 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    652 	}
    653 	pmap_map_entry(l1pagetable,
    654 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
    655 	    kernel_ptpt.pv_pa, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    656 	pmap_map_entry(l1pagetable,
    657 	    PTE_BASE + (0x00000000 >> (PGSHIFT-2)),
    658 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa,
    659 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    660 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    661 		pmap_map_entry(l1pagetable,
    662 		    PTE_BASE + ((KERNEL_VM_BASE +
    663 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
    664 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
    665 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    666 
    667 	/* Map the vector page. */
    668 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    669 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    670 
    671 	/*
    672 	 * Map devices we can map w/ section mappings.
    673 	 */
    674 	loop = 0;
    675 	while (l1_sec_table[loop].size) {
    676 		vm_size_t sz;
    677 
    678 #ifdef VERBOSE_INIT_ARM
    679 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
    680 		    l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
    681 		    l1_sec_table[loop].va);
    682 #endif
    683 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
    684 			pmap_map_section(l1pagetable,
    685 			    l1_sec_table[loop].va + sz,
    686 			    l1_sec_table[loop].pa + sz,
    687 			    l1_sec_table[loop].prot,
    688 			    l1_sec_table[loop].cache);
    689 		++loop;
    690 	}
    691 
    692 	/*
    693 	 * Give the XScale global cache clean code an appropriately
    694 	 * sized chunk of unmapped VA space starting at 0xff500000
    695 	 * (our device mappings end before this address).
    696 	 */
    697 	xscale_cache_clean_addr = 0xff500000U;
    698 
    699 	/*
    700 	 * Now we have the real page tables in place so we can switch to them.
    701 	 * Once this is done we will be running with the REAL kernel page
    702 	 * tables.
    703 	 */
    704 
    705 	/* Switch tables */
    706 #ifdef VERBOSE_INIT_ARM
    707 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    708 #endif
    709 	setttb(kernel_l1pt.pv_pa);
    710 	cpu_tlb_flushID();
    711 
    712 #ifdef VERBOSE_INIT_ARM
    713 	printf("done!\n");
    714 #endif
    715 
    716 #ifdef VERBOSE_INIT_ARM
    717 	printf("bootstrap done.\n");
    718 #endif
    719 
    720 	/*
    721 	 * Inform the BECC code where the BECC is mapped.
    722 	 */
    723 	becc_vaddr = BRH_BECC_VBASE;
    724 
    725 	/*
    726 	 * BECC <= Rev7 can only address 64M through the inbound
    727 	 * PCI windows.  Limit memory to 64M on those revs.  (This
    728 	 * problem was fixed in Rev8 of the BECC; get an FPGA upgrade.)
    729 	 */
    730 	{
    731 		vaddr_t va = BRH_PCI_CONF_VBASE | (1U << BECC_IDSEL_BIT) |
    732 		    PCI_CLASS_REG;
    733 		uint32_t reg;
    734 
    735 		reg = *(__volatile uint32_t *) va;
    736 		becc_rev = PCI_REVISION(reg);
    737 		if (becc_rev <= BECC_REV_V7 &&
    738 		    memsize > (64UL * 1024 * 1024)) {
    739 			memsize = (64UL * 1024 * 1024);
    740 			bootconfig.dram[0].pages = memsize / NBPG;
    741 			physical_end = physical_start +
    742 			    (bootconfig.dram[0].pages * NBPG);
    743 			printf("BECC <= Rev7: memory truncated to 64M\n");
    744 		}
    745 	}
    746 
    747 	/*
    748 	 * Update the physical_freestart/physical_freeend/free_pages
    749 	 * variables.
    750 	 */
    751 	{
    752 		extern char _end[];
    753 
    754 		physical_freestart = physical_start +
    755 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    756 		     KERNEL_BASE);
    757 		physical_freeend = physical_end;
    758 		free_pages = (physical_freeend - physical_freestart) / NBPG;
    759 	}
    760 #ifdef VERBOSE_INIT_ARM
    761 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    762 	       physical_freestart, free_pages, free_pages);
    763 #endif
    764 
    765 	physmem = (physical_end - physical_start) / NBPG;
    766 
    767 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    768 
    769 	/*
    770 	 * Pages were allocated during the secondary bootstrap for the
    771 	 * stacks for different CPU modes.
    772 	 * We must now set the r13 registers in the different CPU modes to
    773 	 * point to these stacks.
    774 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    775 	 * of the stack memory.
    776 	 */
    777 	printf("init subsystems: stacks ");
    778 
    779 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
    780 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
    781 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
    782 
    783 	/*
    784 	 * Well we should set a data abort handler.
    785 	 * Once things get going this will change as we will need a proper
    786 	 * handler.
    787 	 * Until then we will use a handler that just panics but tells us
    788 	 * why.
    789 	 * Initialisation of the vectors will just panic on a data abort.
    790 	 * This just fills in a slighly better one.
    791 	 */
    792 	printf("vectors ");
    793 	data_abort_handler_address = (u_int)data_abort_handler;
    794 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    795 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    796 
    797 	/* Initialise the undefined instruction handlers */
    798 	printf("undefined ");
    799 	undefined_init();
    800 
    801 	/* Load memory into UVM. */
    802 	printf("page ");
    803 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    804 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    805 	    atop(physical_freestart), atop(physical_freeend),
    806 	    VM_FREELIST_DEFAULT);
    807 
    808 	/* Boot strap pmap telling it where the kernel page table is */
    809 	printf("pmap ");
    810 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
    811 
    812 	/* Setup the IRQ system */
    813 	printf("irq ");
    814 	becc_intr_init();
    815 	printf("done.\n");
    816 
    817 #ifdef IPKDB
    818 	/* Initialise ipkdb */
    819 	ipkdb_init();
    820 	if (boothowto & RB_KDB)
    821 		ipkdb_connect(0);
    822 #endif
    823 
    824 #ifdef DDB
    825 	db_machine_init();
    826 
    827 	/* Firmware doesn't load symbols. */
    828 	ddb_init(0, NULL, NULL);
    829 
    830 	if (boothowto & RB_KDB)
    831 		Debugger();
    832 #endif
    833 
    834 	/* We return the new stack pointer address */
    835 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    836 }
    837 
    838 void
    839 consinit(void)
    840 {
    841 	static const bus_addr_t comcnaddrs[] = {
    842 		BRH_UART1_BASE,		/* com0 */
    843 		BRH_UART2_BASE,		/* com1 */
    844 	};
    845 	static int consinit_called;
    846 
    847 	if (consinit_called != 0)
    848 		return;
    849 
    850 	consinit_called = 1;
    851 
    852 #if NCOM > 0
    853 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    854 	    BECC_PERIPH_CLOCK, comcnmode))
    855 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    856 #else
    857 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    858 #endif
    859 }
    860