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