Home | History | Annotate | Line # | Download | only in viper
viper_machdep.c revision 1.26
      1 /*	$NetBSD: viper_machdep.c,v 1.26 2018/07/31 06:46:28 skrll Exp $ */
      2 
      3 /*
      4  * Startup routines for the Arcom Viper.  Below you can trace the
      5  * impressive lineage ;)
      6  *
      7  * Modified for the Viper by Antti Kantee <pooka (at) netbsd.org>
      8  */
      9 
     10 /*
     11  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
     12  * Written by Hiroyuki Bessho for Genetec Corporation.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. The name of Genetec Corporation may not be used to endorse or
     23  *    promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``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 GENETEC CORPORATION
     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  * Machine dependent functions for kernel setup for
     39  * Intel DBPXA250 evaluation board (a.k.a. Lubbock).
     40  * Based on iq80310_machhdep.c
     41  */
     42 /*
     43  * Copyright (c) 2001 Wasabi Systems, Inc.
     44  * All rights reserved.
     45  *
     46  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed for the NetBSD Project by
     59  *	Wasabi Systems, Inc.
     60  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     61  *    or promote products derived from this software without specific prior
     62  *    written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     66  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     67  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     68  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     69  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     70  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     71  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     72  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     73  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     74  * POSSIBILITY OF SUCH DAMAGE.
     75  */
     76 
     77 /*
     78  * Copyright (c) 1997,1998 Mark Brinicombe.
     79  * Copyright (c) 1997,1998 Causality Limited.
     80  * All rights reserved.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *	This product includes software developed by Mark Brinicombe
     93  *	for the NetBSD Project.
     94  * 4. The name of the company nor the name of the author may be used to
     95  *    endorse or promote products derived from this software without specific
     96  *    prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     99  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    100  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    101  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    102  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    103  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    104  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  * Machine dependent functions for kernel setup for Intel IQ80310 evaluation
    111  * boards using RedBoot firmware.
    112  */
    113 
    114 #include <sys/cdefs.h>
    115 __KERNEL_RCSID(0, "$NetBSD: viper_machdep.c,v 1.26 2018/07/31 06:46:28 skrll Exp $");
    116 
    117 #include "opt_arm_debug.h"
    118 #include "opt_ddb.h"
    119 #include "opt_kgdb.h"
    120 #include "opt_pmap_debug.h"
    121 #include "opt_md.h"
    122 #include "opt_com.h"
    123 #include "lcd.h"
    124 
    125 #include <sys/param.h>
    126 #include <sys/device.h>
    127 #include <sys/systm.h>
    128 #include <sys/kernel.h>
    129 #include <sys/exec.h>
    130 #include <sys/proc.h>
    131 #include <sys/msgbuf.h>
    132 #include <sys/reboot.h>
    133 #include <sys/termios.h>
    134 #include <sys/ksyms.h>
    135 #include <sys/bus.h>
    136 #include <sys/cpu.h>
    137 
    138 #include <uvm/uvm_extern.h>
    139 
    140 #include <sys/conf.h>
    141 #include <dev/cons.h>
    142 #include <dev/md.h>
    143 #include <dev/ic/smc91cxxreg.h>
    144 
    145 #include <machine/db_machdep.h>
    146 #include <ddb/db_sym.h>
    147 #include <ddb/db_extern.h>
    148 #ifdef KGDB
    149 #include <sys/kgdb.h>
    150 #endif
    151 
    152 #include <machine/bootconfig.h>
    153 #include <arm/locore.h>
    154 #include <arm/undefined.h>
    155 
    156 #include <arm/arm32/machdep.h>
    157 
    158 #include <arm/xscale/pxa2x0reg.h>
    159 #include <arm/xscale/pxa2x0var.h>
    160 #include <arm/xscale/pxa2x0_gpio.h>
    161 #include <arm/sa11x0/sa1111_reg.h>
    162 #include <evbarm/viper/viper_reg.h>
    163 
    164 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    165 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    166 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    167 
    168 /*
    169  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    170  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    171  */
    172 #define KERNEL_VM_SIZE		0x0C000000
    173 
    174 BootConfig bootconfig;		/* Boot config storage */
    175 char *boot_args = NULL;
    176 char *boot_file = NULL;
    177 
    178 vaddr_t physical_start;
    179 vaddr_t physical_freestart;
    180 vaddr_t physical_freeend;
    181 vaddr_t physical_end;
    182 u_int free_pages;
    183 
    184 /*int debug_flags;*/
    185 #ifndef PMAP_STATIC_L1S
    186 int max_processes = 64;			/* Default number */
    187 #endif	/* !PMAP_STATIC_L1S */
    188 
    189 /* Physical and virtual addresses for some global pages */
    190 pv_addr_t minidataclean;
    191 
    192 paddr_t msgbufphys;
    193 
    194 #ifdef PMAP_DEBUG
    195 extern int pmap_debug_level;
    196 #endif
    197 
    198 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    199 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    200 #define	KERNEL_PT_KERNEL_NUM	4
    201 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
    202 				        /* Page tables for mapping kernel VM */
    203 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    204 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    205 
    206 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    207 
    208 /* Prototypes */
    209 
    210 #if 0
    211 void	process_kernel_args(char *);
    212 #endif
    213 
    214 void	consinit(void);
    215 void	kgdb_port_init(void);
    216 void	change_clock(uint32_t v);
    217 
    218 bs_protos(bs_notimpl);
    219 
    220 #include "com.h"
    221 #if NCOM > 0
    222 #include <dev/ic/comreg.h>
    223 #include <dev/ic/comvar.h>
    224 #endif
    225 
    226 #ifndef CONSPEED
    227 #define CONSPEED B115200	/* What RedBoot uses */
    228 #endif
    229 #ifndef CONMODE
    230 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    231 #endif
    232 
    233 int comcnspeed = CONSPEED;
    234 int comcnmode = CONMODE;
    235 
    236 static struct pxa2x0_gpioconf boarddep_gpioconf[] = {
    237 	{ 44, GPIO_ALT_FN_1_IN },	/* BTCST */
    238 	{ 45, GPIO_ALT_FN_2_OUT },	/* BTRST */
    239 
    240 	{ -1 }
    241 };
    242 static struct pxa2x0_gpioconf *viper_gpioconf[] = {
    243 	pxa25x_com_btuart_gpioconf,
    244 	pxa25x_com_ffuart_gpioconf,
    245 	pxa25x_com_stuart_gpioconf,
    246 	boarddep_gpioconf,
    247 	NULL
    248 };
    249 
    250 /*
    251  * void cpu_reboot(int howto, char *bootstr)
    252  *
    253  * Reboots the system
    254  *
    255  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    256  * then reset the CPU.
    257  */
    258 void
    259 cpu_reboot(int howto, char *bootstr)
    260 {
    261 #ifdef DIAGNOSTIC
    262 	/* info */
    263 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    264 #endif
    265 
    266 	/*
    267 	 * If we are still cold then hit the air brakes
    268 	 * and crash to earth fast
    269 	 */
    270 	if (cold) {
    271 		doshutdownhooks();
    272 		pmf_system_shutdown(boothowto);
    273 		printf("The operating system has halted.\n");
    274 		printf("Please press any key to reboot.\n\n");
    275 		cngetc();
    276 		printf("rebooting...\n");
    277 		cpu_reset();
    278 		/*NOTREACHED*/
    279 	}
    280 
    281 	/* Disable console buffering */
    282 /*	cnpollc(1);*/
    283 
    284 	/*
    285 	 * If RB_NOSYNC was not specified sync the discs.
    286 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    287 	 * unmount.  It looks like syslogd is getting woken up only to find
    288 	 * that it cannot page part of the binary in as the filesystem has
    289 	 * been unmounted.
    290 	 */
    291 	if (!(howto & RB_NOSYNC))
    292 		bootsync();
    293 
    294 	/* Say NO to interrupts */
    295 	splhigh();
    296 
    297 	/* Do a dump if requested. */
    298 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    299 		dumpsys();
    300 
    301 	/* Run any shutdown hooks */
    302 	doshutdownhooks();
    303 
    304 	pmf_system_shutdown(boothowto);
    305 
    306 	/* Make sure IRQ's are disabled */
    307 	IRQdisable;
    308 
    309 	if (howto & RB_HALT) {
    310 		printf("The operating system has halted.\n");
    311 		printf("Please press any key to reboot.\n\n");
    312 		cngetc();
    313 	}
    314 
    315 	printf("rebooting...\n");
    316 	cpu_reset();
    317 	/*NOTREACHED*/
    318 }
    319 
    320 /*
    321  * Static device mappings. These peripheral registers are mapped at
    322  * fixed virtual addresses very early in viper_start() so that we
    323  * can use them while booting the kernel, and stay at the same address
    324  * throughout whole kernel's life time.
    325  *
    326  * We use this table twice; once with bootstrap page table, and once
    327  * with kernel's page table which we build up in initarm().
    328  */
    329 
    330 static const struct pmap_devmap viper_devmap[] = {
    331     {
    332 	    VIPER_GPIO_VBASE,
    333 	    PXA2X0_GPIO_BASE,
    334 	    L1_S_SIZE,
    335 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    336     },
    337     {
    338 	    VIPER_CLKMAN_VBASE,
    339 	    PXA2X0_CLKMAN_BASE,
    340 	    L1_S_SIZE,
    341 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    342     },
    343     {
    344 	    VIPER_INTCTL_VBASE,
    345 	    PXA2X0_INTCTL_BASE,
    346 	    L1_S_SIZE,
    347 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    348     },
    349     {
    350 	    VIPER_FFUART_VBASE,
    351 	    PXA2X0_FFUART_BASE,
    352 	    L1_S_SIZE,
    353 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    354     },
    355     {
    356 	    VIPER_BTUART_VBASE,
    357 	    PXA2X0_BTUART_BASE,
    358 	    L1_S_SIZE,
    359 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    360     },
    361 
    362     {0, 0, 0, 0,}
    363 };
    364 
    365 #ifndef MEMSTART
    366 #define MEMSTART 0xa0000000
    367 #endif
    368 #ifndef MEMSIZE
    369 #define MEMSIZE 0x4000000
    370 #endif
    371 
    372 /*
    373  * u_int initarm(...)
    374  *
    375  * Initial entry point on startup. This gets called before main() is
    376  * entered.
    377  * It should be responsible for setting up everything that must be
    378  * in place when main is called.
    379  * This includes
    380  *   Taking a copy of the boot configuration structure.
    381  *   Initialising the physical console so characters can be printed.
    382  *   Setting up page tables for the kernel
    383  *   Relocating the kernel to the bottom of physical memory
    384  */
    385 u_int
    386 initarm(void *arg)
    387 {
    388 	extern vaddr_t xscale_cache_clean_addr;
    389 	int loop;
    390 	int loop1;
    391 	u_int l1pagetable;
    392 #ifdef DIAGNOSTIC
    393 	extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
    394 #endif
    395 
    396 	/* Register devmap for devices we mapped in start */
    397 	pmap_devmap_register(viper_devmap);
    398 
    399 	/* start 32.768 kHz OSC */
    400 	ioreg_write(VIPER_CLKMAN_VBASE + 0x08, 2);
    401 	/* Get ready for splfoo() */
    402 	pxa2x0_intr_bootstrap(VIPER_INTCTL_VBASE);
    403 
    404 	/*
    405 	 * Heads up ... Setup the CPU / MMU / TLB functions
    406 	 */
    407 	if (set_cpufuncs())
    408 		panic("cpu not recognized!");
    409 
    410 #if 0
    411 	/* Calibrate the delay loop. */
    412 #endif
    413 
    414 	/* setup GPIO for BTUART, in case bootloader doesn't take care of it */
    415 	pxa2x0_gpio_bootstrap(VIPER_GPIO_VBASE);
    416 	pxa2x0_gpio_config(viper_gpioconf);
    417 
    418 	/* turn on clock to UART block.
    419 	   XXX: this should not be done here. */
    420 	ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, CKEN_FFUART|CKEN_BTUART |
    421 	    ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN));
    422 
    423 	consinit();
    424 #ifdef KGDB
    425 	kgdb_port_init();
    426 #endif
    427 	/* Talk to the user */
    428 	printf("\nNetBSD/evbarm (viper) booting ...\n");
    429 
    430 #if 0
    431 	/*
    432 	 * Examine the boot args string for options we need to know about
    433 	 * now.
    434 	 */
    435 	process_kernel_args((char *)nwbootinfo.bt_args);
    436 #endif
    437 
    438 	printf("initarm: Configuring system ...\n");
    439 
    440 	/* Fake bootconfig structure for the benefit of pmap.c */
    441 	/* XXX must make the memory description h/w independent */
    442 	bootconfig.dramblocks = 1;
    443 	bootconfig.dram[0].address = MEMSTART;
    444 	bootconfig.dram[0].pages = MEMSIZE / PAGE_SIZE;
    445 
    446 	/*
    447 	 * Set up the variables that define the availablilty of
    448 	 * physical memory.  For now, we're going to set
    449 	 * physical_freestart to 0xa0200000 (where the kernel
    450 	 * was loaded), and allocate the memory we need downwards.
    451 	 * If we get too close to the page tables that RedBoot
    452 	 * set up, we will panic.  We will update physical_freestart
    453 	 * and physical_freeend later to reflect what pmap_bootstrap()
    454 	 * wants to see.
    455 	 *
    456 	 * XXX pmap_bootstrap() needs an enema.
    457 	 * (now that would be truly hardcore XXX)
    458 	 */
    459 	physical_start = bootconfig.dram[0].address;
    460 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    461 
    462 	physical_freestart = 0xa0009000UL;
    463 	physical_freeend = 0xa0200000UL;
    464 
    465 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    466 
    467 #ifdef VERBOSE_INIT_ARM
    468 	/* Tell the user about the memory */
    469 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    470 	    physical_start, physical_end - 1);
    471 #endif
    472 
    473 	/*
    474 	 * Okay, the kernel starts 2MB in from the bottom of physical
    475 	 * memory.  We are going to allocate our bootstrap pages downwards
    476 	 * from there.
    477 	 *
    478 	 * We need to allocate some fixed page tables to get the kernel
    479 	 * going.  We allocate one page directory and a number of page
    480 	 * tables and store the physical addresses in the kernel_pt_table
    481 	 * array.
    482 	 *
    483 	 * The kernel page directory must be on a 16K boundary.  The page
    484 	 * tables must be on 4K boundaries.  What we do is allocate the
    485 	 * page directory on the first 16K boundary that we encounter, and
    486 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    487 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    488 	 * least one 16K aligned region.
    489 	 */
    490 
    491 #ifdef VERBOSE_INIT_ARM
    492 	printf("Allocating page tables\n");
    493 #endif
    494 
    495 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    496 
    497 #ifdef VERBOSE_INIT_ARM
    498 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    499 	       physical_freestart, free_pages, free_pages);
    500 #endif
    501 
    502 	/* Define a macro to simplify memory allocation */
    503 #define	valloc_pages(var, np)				\
    504 	alloc_pages((var).pv_pa, (np));			\
    505 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    506 
    507 #define alloc_pages(var, np)				\
    508 	physical_freeend -= ((np) * PAGE_SIZE);		\
    509 	if (physical_freeend < physical_freestart)	\
    510 		panic("initarm: out of memory");	\
    511 	(var) = physical_freeend;			\
    512 	free_pages -= (np);				\
    513 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    514 
    515 	loop1 = 0;
    516 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    517 		/* Are we 16KB aligned for an L1 ? */
    518 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    519 		    && kernel_l1pt.pv_pa == 0) {
    520 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    521 		} else {
    522 			valloc_pages(kernel_pt_table[loop1],
    523 			    L2_TABLE_SIZE / PAGE_SIZE);
    524 			++loop1;
    525 		}
    526 	}
    527 
    528 	/* This should never be able to happen but better confirm that. */
    529 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    530 		panic("initarm: Failed to align the kernel page directory");
    531 
    532 	/*
    533 	 * Allocate a page for the system page mapped to V0x00000000
    534 	 * This page will just contain the system vectors and can be
    535 	 * shared by all processes.
    536 	 */
    537 	alloc_pages(systempage.pv_pa, 1);
    538 
    539 	/* Allocate stacks for all modes */
    540 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    541 	valloc_pages(abtstack, ABT_STACK_SIZE);
    542 	valloc_pages(undstack, UND_STACK_SIZE);
    543 	valloc_pages(kernelstack, UPAGES);
    544 
    545 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    546 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    547 	valloc_pages(minidataclean, 1);
    548 
    549 #ifdef VERBOSE_INIT_ARM
    550 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    551 	    irqstack.pv_va);
    552 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    553 	    abtstack.pv_va);
    554 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    555 	    undstack.pv_va);
    556 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    557 	    kernelstack.pv_va);
    558 #endif
    559 
    560 	/*
    561 	 * XXX Defer this to later so that we can reclaim the memory
    562 	 * XXX used by the RedBoot page tables.
    563 	 */
    564 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    565 
    566 	/*
    567 	 * Ok we have allocated physical pages for the primary kernel
    568 	 * page tables
    569 	 */
    570 
    571 #ifdef VERBOSE_INIT_ARM
    572 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    573 #endif
    574 
    575 	/*
    576 	 * Now we start construction of the L1 page table
    577 	 * We start by mapping the L2 page tables into the L1.
    578 	 * This means that we can replace L1 mappings later on if necessary
    579 	 */
    580 	l1pagetable = kernel_l1pt.pv_pa;
    581 
    582 	/* Map the L2 pages tables in the L1 page table */
    583 	pmap_link_l2pt(l1pagetable, 0x00000000,
    584 	    &kernel_pt_table[KERNEL_PT_SYS]);
    585 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    586 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    587 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    588 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    589 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    590 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    591 
    592 	/* update the top of the kernel VM */
    593 	pmap_curmaxkvaddr =
    594 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    595 
    596 #ifdef VERBOSE_INIT_ARM
    597 	printf("Mapping kernel\n");
    598 #endif
    599 
    600 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    601 	{
    602 		extern char etext[], _end[];
    603 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    604 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    605 		u_int logical;
    606 
    607 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    608 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    609 
    610 		logical = 0x00200000;	/* offset of kernel in RAM */
    611 
    612 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    613 		    physical_start + logical, textsize,
    614 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    615 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    616 		    physical_start + logical, totalsize - textsize,
    617 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    618 	}
    619 
    620 #ifdef VERBOSE_INIT_ARM
    621 	printf("Constructing L2 page tables\n");
    622 #endif
    623 
    624 	/* Map the stack pages */
    625 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    626 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    627 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    628 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    629 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    630 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    631 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    632 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    633 
    634 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    635 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    636 
    637 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    638 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    639 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    640 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    641 	}
    642 
    643 	/* Map the Mini-Data cache clean area. */
    644 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    645 	    minidataclean.pv_pa);
    646 
    647 	/* Map the vector page. */
    648 #if 1
    649 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
    650 	 * cache-clean code there.  */
    651 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    652 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    653 #else
    654 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    655 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    656 #endif
    657 
    658 	/*
    659 	 * map integrated peripherals at same address in l1pagetable
    660 	 * so that we can continue to use console.
    661 	 */
    662 	pmap_devmap_bootstrap(l1pagetable, viper_devmap);
    663 
    664 	/*
    665 	 * Give the XScale global cache clean code an appropriately
    666 	 * sized chunk of unmapped VA space starting at 0xff000000
    667 	 * (our device mappings end before this address).
    668 	 */
    669 	xscale_cache_clean_addr = 0xff000000U;
    670 
    671 	/*
    672 	 * Now we have the real page tables in place so we can switch to them.
    673 	 * Once this is done we will be running with the REAL kernel page
    674 	 * tables.
    675 	 */
    676 
    677 	/*
    678 	 * Update the physical_freestart/physical_freeend/free_pages
    679 	 * variables.
    680 	 */
    681 	{
    682 		extern char _end[];
    683 
    684 		physical_freestart = physical_start +
    685 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    686 		     KERNEL_BASE);
    687 		physical_freeend = physical_end;
    688 		free_pages =
    689 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    690 	}
    691 
    692 	/* Switch tables */
    693 #ifdef VERBOSE_INIT_ARM
    694 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    695 	       physical_freestart, free_pages, free_pages);
    696 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    697 #endif
    698 
    699 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    700 	cpu_setttb(kernel_l1pt.pv_pa, true);
    701 	cpu_tlb_flushID();
    702 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    703 
    704 	/*
    705 	 * Moved from cpu_startup() as data_abort_handler() references
    706 	 * this during uvm init
    707 	 */
    708 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    709 
    710 #ifdef VERBOSE_INIT_ARM
    711 	printf("bootstrap done.\n");
    712 #endif
    713 
    714 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    715 
    716 	/*
    717 	 * Pages were allocated during the secondary bootstrap for the
    718 	 * stacks for different CPU modes.
    719 	 * We must now set the r13 registers in the different CPU modes to
    720 	 * point to these stacks.
    721 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    722 	 * of the stack memory.
    723 	 */
    724 	printf("init subsystems: stacks ");
    725 
    726 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    727 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    728 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    729 
    730 	/*
    731 	 * Well we should set a data abort handler.
    732 	 * Once things get going this will change as we will need a proper
    733 	 * handler.
    734 	 * Until then we will use a handler that just panics but tells us
    735 	 * why.
    736 	 * Initialisation of the vectors will just panic on a data abort.
    737 	 * This just fills in a slightly better one.
    738 	 */
    739 	printf("vectors ");
    740 	data_abort_handler_address = (u_int)data_abort_handler;
    741 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    742 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    743 
    744 	/* Initialise the undefined instruction handlers */
    745 	printf("undefined ");
    746 	undefined_init();
    747 
    748 	/* Load memory into UVM. */
    749 	printf("page ");
    750 	uvm_md_init();
    751 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    752 	    atop(physical_freestart), atop(physical_freeend),
    753 	    VM_FREELIST_DEFAULT);
    754 
    755 	/* Boot strap pmap telling it where the kernel page table is */
    756 	printf("pmap ");
    757 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    758 
    759 #ifdef __HAVE_MEMORY_DISK__
    760 	md_root_setconf(memory_disk, sizeof memory_disk);
    761 #endif
    762 
    763 #ifdef KGDB
    764 	if (boothowto & RB_KDB) {
    765 		kgdb_debug_init = 1;
    766 		kgdb_connect(1);
    767 	}
    768 #endif
    769 
    770 #ifdef DDB
    771 	db_machine_init();
    772 
    773 	/* Firmware doesn't load symbols. */
    774 	ddb_init(0, NULL, NULL);
    775 
    776 	if (boothowto & RB_KDB)
    777 		Debugger();
    778 #endif
    779 
    780 	/* We return the new stack pointer address */
    781 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    782 }
    783 
    784 #if 0
    785 void
    786 process_kernel_args(char *args)
    787 {
    788 
    789 	boothowto = 0;
    790 
    791 	/* Make a local copy of the bootargs */
    792 	strncpy(bootargs, args, MAX_BOOT_STRING);
    793 
    794 	args = bootargs;
    795 	boot_file = bootargs;
    796 
    797 	/* Skip the kernel image filename */
    798 	while (*args != ' ' && *args != 0)
    799 		++args;
    800 
    801 	if (*args != 0)
    802 		*args++ = 0;
    803 
    804 	while (*args == ' ')
    805 		++args;
    806 
    807 	boot_args = args;
    808 
    809 	printf("bootfile: %s\n", boot_file);
    810 	printf("bootargs: %s\n", boot_args);
    811 
    812 	parse_mi_bootargs(boot_args);
    813 }
    814 #endif
    815 
    816 #ifdef KGDB
    817 #ifndef KGDB_DEVNAME
    818 #define KGDB_DEVNAME "ffuart"
    819 #endif
    820 const char kgdb_devname[] = KGDB_DEVNAME;
    821 
    822 #if (NCOM > 0)
    823 #ifndef KGDB_DEVMODE
    824 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    825 #endif
    826 int comkgdbmode = KGDB_DEVMODE;
    827 #endif /* NCOM */
    828 
    829 #endif /* KGDB */
    830 
    831 
    832 void
    833 consinit(void)
    834 {
    835 	static int consinit_called = 0;
    836 	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
    837 #if 0
    838 	char *console = CONSDEVNAME;
    839 #endif
    840 
    841 	if (consinit_called != 0)
    842 		return;
    843 	consinit_called = 1;
    844 
    845 #if NCOM > 0
    846 
    847 #ifdef FFUARTCONSOLE
    848 #ifdef KGDB
    849 	if (0 == strcmp(kgdb_devname, "ffuart")) {
    850 		/* port is reserved for kgdb */
    851 	} else
    852 #endif
    853 	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
    854 		     comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
    855 
    856 #if 0
    857 		/* XXX: can't call pxa2x0_clkman_config yet */
    858 		pxa2x0_clkman_config(CKEN_FFUART, 1);
    859 #else
    860 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN,
    861 		    ckenreg|CKEN_FFUART);
    862 #endif
    863 
    864 		return;
    865 	}
    866 
    867 #endif /* FFUARTCONSOLE */
    868 
    869 #ifdef BTUARTCONSOLE
    870 #ifdef KGDB
    871 	if (0 == strcmp(kgdb_devname, "btuart")) {
    872 		/* port is reserved for kgdb */
    873 	} else
    874 #endif
    875 	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
    876 		comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
    877 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN,
    878 		    ckenreg|CKEN_BTUART);
    879 		return;
    880 	}
    881 #endif /* BTUARTCONSOLE */
    882 
    883 	/* no console, guess we're flying blind */
    884 
    885 #endif /* NCOM */
    886 
    887 }
    888 
    889 #ifdef KGDB
    890 void
    891 kgdb_port_init(void)
    892 {
    893 #if (NCOM > 0) && defined(COM_PXA2X0)
    894 	paddr_t paddr = 0;
    895 	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
    896 
    897 	if (0 == strcmp(kgdb_devname, "ffuart")) {
    898 		paddr = PXA2X0_FFUART_BASE;
    899 		ckenreg |= CKEN_FFUART;
    900 	}
    901 	else if (0 == strcmp(kgdb_devname, "btuart")) {
    902 		paddr = PXA2X0_BTUART_BASE;
    903 		ckenreg |= CKEN_BTUART;
    904 	}
    905 
    906 	if (paddr &&
    907 	    0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
    908 		kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
    909 
    910 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
    911 	}
    912 #endif
    913 }
    914 #endif
    915