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