Home | History | Annotate | Line # | Download | only in smdk2xx0
smdk2800_machdep.c revision 1.23
      1 /*	$NetBSD: smdk2800_machdep.c,v 1.23 2005/12/24 22:45:34 perry Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2003, 2005 Fujitsu Component Limited
      5  * Copyright (c) 2002, 2003, 2005 Genetec Corporation
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     17  *    Genetec corporation may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     21  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     25  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 2001,2002 ARM Ltd
     37  * All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. The name of the company may not be used to endorse or promote
     48  *    products derived from this software without specific prior written
     49  *    permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ARM LTD
     55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61  * POSSIBILITY OF SUCH DAMAGE.
     62  *
     63  */
     64 
     65 /*
     66  * Copyright (c) 1997,1998 Mark Brinicombe.
     67  * Copyright (c) 1997,1998 Causality Limited.
     68  * All rights reserved.
     69  *
     70  * Redistribution and use in source and binary forms, with or without
     71  * modification, are permitted provided that the following conditions
     72  * are met:
     73  * 1. Redistributions of source code must retain the above copyright
     74  *    notice, this list of conditions and the following disclaimer.
     75  * 2. Redistributions in binary form must reproduce the above copyright
     76  *    notice, this list of conditions and the following disclaimer in the
     77  *    documentation and/or other materials provided with the distribution.
     78  * 3. All advertising materials mentioning features or use of this software
     79  *    must display the following acknowledgement:
     80  *	This product includes software developed by Mark Brinicombe
     81  *	for the NetBSD Project.
     82  * 4. The name of the company nor the name of the author may be used to
     83  *    endorse or promote products derived from this software without specific
     84  *    prior written permission.
     85  *
     86  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     87  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     88  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     89  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     90  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     91  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     92  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     93  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     94  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     95  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     96  * SUCH DAMAGE.
     97  *
     98  * Machine dependant functions for kernel setup for integrator board
     99  *
    100  * Created      : 24/11/97
    101  */
    102 
    103 /*
    104  * Machine dependant functions for kernel setup for Samsung SMDK2800
    105  * derived from integrator_machdep.c
    106  */
    107 
    108 #include <sys/cdefs.h>
    109 __KERNEL_RCSID(0, "$NetBSD: smdk2800_machdep.c,v 1.23 2005/12/24 22:45:34 perry Exp $");
    110 
    111 #include "opt_ddb.h"
    112 #include "opt_kgdb.h"
    113 #include "opt_ipkdb.h"
    114 #include "opt_pmap_debug.h"
    115 #include "opt_md.h"
    116 #include "pci.h"
    117 
    118 #include <sys/param.h>
    119 #include <sys/device.h>
    120 #include <sys/systm.h>
    121 #include <sys/kernel.h>
    122 #include <sys/exec.h>
    123 #include <sys/proc.h>
    124 #include <sys/msgbuf.h>
    125 #include <sys/reboot.h>
    126 #include <sys/termios.h>
    127 #include <sys/ksyms.h>
    128 
    129 #include <uvm/uvm_extern.h>
    130 
    131 #include <dev/cons.h>
    132 #include <dev/md.h>
    133 
    134 #include <machine/db_machdep.h>
    135 #include <ddb/db_sym.h>
    136 #include <ddb/db_extern.h>
    137 #ifdef KGDB
    138 #include <sys/kgdb.h>
    139 #endif
    140 
    141 #include <machine/bootconfig.h>
    142 #include <machine/bus.h>
    143 #include <machine/cpu.h>
    144 #include <machine/frame.h>
    145 #include <machine/intr.h>
    146 #include <arm/undefined.h>
    147 
    148 #include <arm/arm32/machdep.h>
    149 
    150 #include <arm/s3c2xx0/s3c2800reg.h>
    151 #include <arm/s3c2xx0/s3c2800var.h>
    152 #include <evbarm/smdk2xx0/smdk2800var.h>
    153 
    154 #include "ksyms.h"
    155 
    156 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    157 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    158 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    159 
    160 /*
    161  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    162  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    163  */
    164 #define KERNEL_VM_SIZE		0x0C000000
    165 
    166 /* Memory disk support */
    167 #if defined(MEMORY_DISK_DYNAMIC) && defined(MEMORY_DISK_ROOT_ADDR)
    168 #define DO_MEMORY_DISK
    169 /* We have memory disk image outside of the kernel on ROM. */
    170 #ifdef MEMORY_DISK_ROOT_ROM
    171 /* map the image directory and use read-only */
    172 #else
    173 /* copy the image to RAM */
    174 #endif
    175 #endif
    176 
    177 
    178 /*
    179  * Address to call from cpu_reset() to reset the machine.
    180  * This is machine architecture dependant as it varies depending
    181  * on where the ROM appears when you turn the MMU off.
    182  */
    183 u_int cpu_reset_address = (u_int)0;
    184 
    185 /* Define various stack sizes in pages */
    186 #define IRQ_STACK_SIZE	1
    187 #define ABT_STACK_SIZE	1
    188 #ifdef IPKDB
    189 #define UND_STACK_SIZE	2
    190 #else
    191 #define UND_STACK_SIZE	1
    192 #endif
    193 
    194 BootConfig bootconfig;		/* Boot config storage */
    195 char *boot_args = NULL;
    196 char *boot_file = NULL;
    197 
    198 vm_offset_t physical_start;
    199 vm_offset_t physical_freestart;
    200 vm_offset_t physical_freeend;
    201 vm_offset_t physical_end;
    202 u_int free_pages;
    203 vm_offset_t pagetables_start;
    204 int physmem = 0;
    205 
    206 /*int debug_flags;*/
    207 #ifndef PMAP_STATIC_L1S
    208 int max_processes = 64;		/* Default number */
    209 #endif				/* !PMAP_STATIC_L1S */
    210 
    211 /* Physical and virtual addresses for some global pages */
    212 pv_addr_t systempage;
    213 pv_addr_t irqstack;
    214 pv_addr_t undstack;
    215 pv_addr_t abtstack;
    216 pv_addr_t kernelstack;
    217 
    218 vm_offset_t msgbufphys;
    219 
    220 extern u_int data_abort_handler_address;
    221 extern u_int prefetch_abort_handler_address;
    222 extern u_int undefined_handler_address;
    223 
    224 #ifdef PMAP_DEBUG
    225 extern int pmap_debug_level;
    226 #endif
    227 
    228 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    229 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    230 #define	KERNEL_PT_KERNEL_NUM	2	/* L2 tables for mapping kernel VM */
    231 
    232 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    233 
    234 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    235 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    236 
    237 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    238 
    239 struct user *proc0paddr;
    240 
    241 /* Prototypes */
    242 
    243 void consinit(void);
    244 void kgdb_port_init(void);
    245 
    246 /* A load of console goo. */
    247 #include "vga.h"
    248 #if NVGA > 0
    249 #include <dev/ic/mc6845reg.h>
    250 #include <dev/ic/pcdisplayvar.h>
    251 #include <dev/ic/vgareg.h>
    252 #include <dev/ic/vgavar.h>
    253 #endif
    254 
    255 #include "com.h"
    256 #if NCOM > 0
    257 #include <dev/ic/comreg.h>
    258 #include <dev/ic/comvar.h>
    259 #endif
    260 
    261 #include "sscom.h"
    262 #if NSSCOM > 0
    263 #include "opt_sscom.h"
    264 #include <arm/s3c2xx0/sscom_var.h>
    265 #endif
    266 
    267 /*
    268  * Define the default console speed for the board.  This is generally
    269  * what the firmware provided with the board defaults to.
    270  */
    271 #ifndef CONSPEED
    272 #define CONSPEED B115200	/* TTYDEF_SPEED */
    273 #endif
    274 #ifndef CONMODE
    275 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)   /* 8N1 */
    276 #endif
    277 
    278 int comcnspeed = CONSPEED;
    279 int comcnmode = CONMODE;
    280 
    281 /*
    282  * void cpu_reboot(int howto, char *bootstr)
    283  *
    284  * Reboots the system
    285  *
    286  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    287  * then reset the CPU.
    288  */
    289 void
    290 cpu_reboot(int howto, char *bootstr)
    291 {
    292 
    293 	cpu_reset_address = vtophys((u_int)s3c2800_softreset);
    294 
    295 	/*
    296 	 * If we are still cold then hit the air brakes
    297 	 * and crash to earth fast
    298 	 */
    299 	if (cold) {
    300 		doshutdownhooks();
    301 		printf("The operating system has halted.\n");
    302 		printf("Please press any key to reboot.\n\n");
    303 		cngetc();
    304 		printf("rebooting...\n");
    305 		cpu_reset();
    306 		/* NOTREACHED */
    307 	}
    308 	/* Disable console buffering */
    309 
    310 	/*
    311 	 * If RB_NOSYNC was not specified sync the discs.
    312 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    313 	 * unmount.  It looks like syslogd is getting woken up only to find
    314 	 * that it cannot page part of the binary in as the filesystem has
    315 	 * been unmounted.
    316 	 */
    317 	if (!(howto & RB_NOSYNC))
    318 		bootsync();
    319 
    320 	/* Say NO to interrupts */
    321 	splhigh();
    322 
    323 	/* Do a dump if requested. */
    324 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    325 		dumpsys();
    326 
    327 	/* Run any shutdown hooks */
    328 	doshutdownhooks();
    329 
    330 	/* Make sure IRQ's are disabled */
    331 	IRQdisable;
    332 
    333 	if (howto & RB_HALT) {
    334 		printf("The operating system has halted.\n");
    335 		printf("Please press any key to reboot.\n\n");
    336 		cngetc();
    337 	}
    338 	printf("rebooting...\n");
    339 	cpu_reset();
    340 	/* NOTREACHED */
    341 }
    342 
    343 /*
    344  * All built-in peripheral registers are statically mapped in start up
    345  * routine.  This table tells pmap subsystem about it, and to map them
    346  * at the same position.
    347  */
    348 static const struct pmap_devmap smdk2800_devmap[] = {
    349 	{
    350 		SMDK2800_IO_AREA_VBASE,
    351 		S3C2800_PERIPHERALS,
    352 		S3C2800_PERIPHERALS_SIZE,
    353 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    354 	},
    355 	{ 0, 0, 0, 0 }
    356 };
    357 
    358 #define ioreg_vaddr(pa)	((pa) - S3C2800_PERIPHERALS + SMDK2800_IO_AREA_VBASE)
    359 #define	ioreg32(pa)	(*(volatile uint32_t *)ioreg_vaddr(pa))
    360 
    361 /*
    362  * u_int initarm(...)
    363  *
    364  * Initial entry point on startup. This gets called before main() is
    365  * entered.
    366  * It should be responsible for setting up everything that must be
    367  * in place when main is called.
    368  * This includes
    369  *   Taking a copy of the boot configuration structure.
    370  *   Initialising the physical console so characters can be printed.
    371  *   Setting up page tables for the kernel
    372  *   Relocating the kernel to the bottom of physical memory
    373  */
    374 
    375 u_int
    376 initarm(void *arg)
    377 {
    378 	int loop;
    379 	int loop1;
    380 	u_int l1pagetable;
    381 	extern int etext __asm("_etext");
    382 	extern int end __asm("_end");
    383 	pv_addr_t kernel_l1pt;
    384 	int progress_counter = 0;
    385 
    386 #ifdef DO_MEMORY_DISK
    387 	vm_offset_t md_root_start;
    388 #define MD_ROOT_SIZE (MEMORY_DISK_ROOT_SIZE * DEV_BSIZE)
    389 #endif
    390 
    391 #define gpio8(reg) (*(volatile uint8_t *)(ioreg_vaddr(S3C2800_GPIO_BASE) + (reg)))
    392 
    393 #define LEDSTEP()  __LED(progress_counter++)
    394 
    395 #define pdatc gpio8(GPIO_PDATC)
    396 #define __LED(x)  (pdatc = (pdatc & ~0x07) | (~(x) & 0x07))
    397 
    398 	LEDSTEP();
    399 	/*
    400 	 * Heads up ... Setup the CPU / MMU / TLB functions
    401 	 */
    402 	if (set_cpufuncs())
    403 		panic("CPU not recognized!");
    404 
    405 	LEDSTEP();
    406 
    407 
    408 	/* Disable all peripheral interrupts */
    409 	ioreg32(S3C2800_INTCTL_BASE + INTCTL_INTMSK) = 0;
    410 
    411 	consinit();
    412 #ifdef VERBOSE_INIT_ARM
    413 	printf("consinit done\n");
    414 #endif
    415 
    416 #ifdef KGDB
    417 	LEDSTEP();
    418 	kgdb_port_init();
    419 #endif
    420 	LEDSTEP();
    421 
    422 #ifdef VERBOSE_INIT_ARM
    423 	/* Talk to the user */
    424 	printf("\nNetBSD/evbarm (SMDK2800) booting ...\n");
    425 #endif
    426 
    427 	/*
    428 	 * Ok we have the following memory map
    429 	 *
    430 	 * Physical Address Range     Description
    431 	 * -----------------------    ----------------------------------
    432 	 * 0x00000000 - 0x00ffffff    Intel flash Memory   (16MB)
    433 	 * 0x02000000 - 0x020fffff    AMD flash Memory   (1MB)
    434 	 * or 			       (depend on DIPSW setting)
    435 	 * 0x00000000 - 0x000fffff    AMD flash Memory   (1MB)
    436 	 * 0x02000000 - 0x02ffffff    Intel flash Memory   (16MB)
    437 	 *
    438 	 * 0x08000000 - 0x09ffffff    SDRAM (32MB)
    439 	 * 0x20000000 - 0x3fffffff    PCI space
    440 	 *
    441 	 * The initarm() has the responsibility for creating the kernel
    442 	 * page tables.
    443 	 * It must also set up various memory pointers that are used
    444 	 * by pmap etc.
    445 	 */
    446 
    447 	/* Fake bootconfig structure for the benefit of pmap.c */
    448 	/* XXX must make the memory description h/w independent */
    449 	bootconfig.dramblocks = 1;
    450 	bootconfig.dram[0].address = SDRAM_START;
    451 	bootconfig.dram[0].pages = SDRAM_SIZE / PAGE_SIZE;
    452 
    453 	/*
    454 	 * Set up the variables that define the availablilty of
    455 	 * physical memory.  For now, we're going to set
    456 	 * physical_freestart to 0x08200000 (where the kernel
    457 	 * was loaded), and allocate the memory we need downwards.
    458 	 * If we get too close to the bottom of SDRAM, we
    459 	 * will panic.  We will update physical_freestart and
    460 	 * physical_freeend later to reflect what pmap_bootstrap()
    461 	 * wants to see.
    462 	 *
    463 	 * XXX pmap_bootstrap() needs an enema.
    464 	 */
    465 	physical_start = bootconfig.dram[0].address;
    466 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    467 
    468 #if DO_MEMORY_DISK
    469 #ifdef MEMORY_DISK_ROOT_ROM
    470 	md_root_start = MEMORY_DISK_ROOT_ADDR;
    471 	boothowto |= RB_RDONLY;
    472 #else
    473 	/* Reserve physmem for ram disk */
    474 	md_root_start = ((physical_end - MD_ROOT_SIZE) & ~(L1_S_SIZE-1));
    475 	printf("Reserve %ld bytes for memory disk\n",
    476 	    physical_end - md_root_start);
    477 	/* copy fs contents */
    478 	memcpy((void *)md_root_start, (void *)MEMORY_DISK_ROOT_ADDR,
    479 	    MD_ROOT_SIZE);
    480 	physical_end = md_root_start;
    481 #endif
    482 #endif
    483 
    484 	physical_freestart = 0x08000000UL;	/* XXX */
    485 	physical_freeend = 0x08200000UL;
    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 	 * XXX
    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 	kernel_l1pt.pv_pa = 0;
    540 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    541 		/* Are we 16KB aligned for an L1 ? */
    542 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    543 		    && kernel_l1pt.pv_pa == 0) {
    544 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    545 		} else {
    546 			valloc_pages(kernel_pt_table[loop1],
    547 			    L2_TABLE_SIZE / PAGE_SIZE);
    548 			++loop1;
    549 		}
    550 	}
    551 
    552 	/* This should never be able to happen but better confirm that. */
    553 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    554 		panic("initarm: Failed to align the kernel page directory\n");
    555 
    556 	/*
    557 	 * Allocate a page for the system page mapped to V0x00000000
    558 	 * This page will just contain the system vectors and can be
    559 	 * shared by all processes.
    560 	 */
    561 	alloc_pages(systempage.pv_pa, 1);
    562 
    563 	/* Allocate stacks for all modes */
    564 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    565 	valloc_pages(abtstack, ABT_STACK_SIZE);
    566 	valloc_pages(undstack, UND_STACK_SIZE);
    567 	valloc_pages(kernelstack, UPAGES);
    568 
    569 #ifdef VERBOSE_INIT_ARM
    570 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    571 	    irqstack.pv_va);
    572 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    573 	    abtstack.pv_va);
    574 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    575 	    undstack.pv_va);
    576 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    577 	    kernelstack.pv_va);
    578 #endif
    579 
    580 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    581 
    582 	LEDSTEP();
    583 
    584 	/*
    585 	 * Ok we have allocated physical pages for the primary kernel
    586 	 * page tables
    587 	 */
    588 
    589 #ifdef VERBOSE_INIT_ARM
    590 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    591 #endif
    592 
    593 	/*
    594 	 * Now we start construction of the L1 page table
    595 	 * We start by mapping the L2 page tables into the L1.
    596 	 * This means that we can replace L1 mappings later on if necessary
    597 	 */
    598 	l1pagetable = kernel_l1pt.pv_pa;
    599 
    600 	/* Map the L2 pages tables in the L1 page table */
    601 	pmap_link_l2pt(l1pagetable, 0x00000000,
    602 	    &kernel_pt_table[KERNEL_PT_SYS]);
    603 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    604 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    605 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    606 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    607 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    608 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    609 
    610 	/* update the top of the kernel VM */
    611 	pmap_curmaxkvaddr =
    612 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    613 
    614 #ifdef VERBOSE_INIT_ARM
    615 	printf("Mapping kernel\n");
    616 #endif
    617 
    618 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    619 	{
    620 		size_t textsize = (uintptr_t)&etext - KERNEL_TEXT_BASE;
    621 		size_t totalsize = (uintptr_t)&end - KERNEL_TEXT_BASE;
    622 		u_int logical;
    623 
    624 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    625 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    626 
    627 		logical = 0x00200000;	/* offset of kernel in RAM */
    628 
    629 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    630 		    physical_start + logical, textsize,
    631 		    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    632 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    633 		    physical_start + logical, totalsize - textsize,
    634 		    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    635 	}
    636 
    637 #ifdef VERBOSE_INIT_ARM
    638 	printf("Constructing L2 page tables\n");
    639 #endif
    640 
    641 	/* Map the stack pages */
    642 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    643 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
    644 	    PTE_CACHE);
    645 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    646 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
    647 	    PTE_CACHE);
    648 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    649 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
    650 	    PTE_CACHE);
    651 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    652 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    653 
    654 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    655 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    656 
    657 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    658 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    659 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    660 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    661 	}
    662 
    663 	/* Map the vector page. */
    664 #if 1
    665 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
    666 	 * cache-clean code there.  */
    667 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    668 	    VM_PROT_READ | VM_PROT_WRITE, PTE_NOCACHE);
    669 #else
    670 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    671 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    672 #endif
    673 
    674 #ifdef MEMORY_DISK_DYNAMIC
    675 	/* map MD root image */
    676 	pmap_map_chunk(l1pagetable, SMDK2800_MEMORY_DISK_VADDR, md_root_start,
    677 	    MD_ROOT_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    678 
    679 	md_root_setconf((void *)md_root_start, MD_ROOT_SIZE);
    680 #endif /* MEMORY_DISK_DYNAMIC */
    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, smdk2800_devmap);
    686 
    687 	/*
    688 	 * Now we have the real page tables in place so we can switch to them.
    689 	 * Once this is done we will be running with the REAL kernel page
    690 	 * tables.
    691 	 */
    692 
    693 	/*
    694 	 * Update the physical_freestart/physical_freeend/free_pages
    695 	 * variables.
    696 	 */
    697 	{
    698 		physical_freestart = physical_start +
    699 		    (((((uintptr_t)&end) + PGOFSET) & ~PGOFSET) - KERNEL_BASE);
    700 		physical_freeend = physical_end;
    701 		free_pages =
    702 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    703 	}
    704 
    705 	/* Switch tables */
    706 #ifdef VERBOSE_INIT_ARM
    707 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    708 	    physical_freestart, free_pages, free_pages);
    709 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    710 #endif
    711 	LEDSTEP();
    712 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    713 	setttb(kernel_l1pt.pv_pa);
    714 	cpu_tlb_flushID();
    715 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    716 
    717 	/*
    718 	 * Moved from cpu_startup() as data_abort_handler() references
    719 	 * this during uvm init
    720 	 */
    721 	proc0paddr = (struct user *)kernelstack.pv_va;
    722 	lwp0.l_addr = proc0paddr;
    723 
    724 #ifdef VERBOSE_INIT_ARM
    725 	printf("done!\n");
    726 #endif
    727 
    728 #if 0
    729 	/*
    730 	 * The IFPGA registers have just moved.
    731 	 * Detach the diagnostic serial port and reattach at the new address.
    732 	 */
    733 	plcomcndetach();
    734 	/*
    735 	 * XXX this should only be done in main() but it useful to
    736 	 * have output earlier ...
    737 	 */
    738 	consinit();
    739 #endif
    740 
    741 	LEDSTEP();
    742 #ifdef VERBOSE_INIT_ARM
    743 	printf("bootstrap done.\n");
    744 #endif
    745 
    746 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    747 
    748 	/*
    749 	 * Pages were allocated during the secondary bootstrap for the
    750 	 * stacks for different CPU modes.
    751 	 * We must now set the r13 registers in the different CPU modes to
    752 	 * point to these stacks.
    753 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    754 	 * of the stack memory.
    755 	 */
    756 #ifdef VERBOSE_INIT_ARM
    757 	printf("init subsystems: stacks ");
    758 #endif
    759 
    760 	set_stackptr(PSR_IRQ32_MODE,
    761 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    762 	set_stackptr(PSR_ABT32_MODE,
    763 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    764 	set_stackptr(PSR_UND32_MODE,
    765 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    766 
    767 	LEDSTEP();
    768 
    769 	/*
    770 	 * Well we should set a data abort handler.
    771 	 * Once things get going this will change as we will need a proper
    772 	 * handler.
    773 	 * Until then we will use a handler that just panics but tells us
    774 	 * why.
    775 	 * Initialisation of the vectors will just panic on a data abort.
    776 	 * This just fills in a slightly better one.
    777 	 */
    778 #ifdef VERBOSE_INIT_ARM
    779 	printf("vectors ");
    780 #endif
    781 	data_abort_handler_address = (u_int)data_abort_handler;
    782 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    783 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    784 
    785 	/* Initialise the undefined instruction handlers */
    786 #ifdef VERBOSE_INIT_ARM
    787 	printf("undefined ");
    788 #endif
    789 	undefined_init();
    790 
    791 	LEDSTEP();
    792 
    793 	/* Load memory into UVM. */
    794 #ifdef VERBOSE_INIT_ARM
    795 	printf("page ");
    796 #endif
    797 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    798 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    799 	    atop(physical_freestart), atop(physical_freeend),
    800 	    VM_FREELIST_DEFAULT);
    801 
    802 	LEDSTEP();
    803 	/* Boot strap pmap telling it where the kernel page table is */
    804 #ifdef VERBOSE_INIT_ARM
    805 	printf("pmap ");
    806 #endif
    807 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    808 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    809 
    810 	LEDSTEP();
    811 
    812 	/* Setup the IRQ system */
    813 #ifdef VERBOSE_INIT_ARM
    814 	printf("irq ");
    815 #endif
    816 	/* XXX irq_init(); */
    817 
    818 #ifdef VERBOSE_INIT_ARM
    819 	printf("done.\n");
    820 #endif
    821 
    822 #ifdef BOOTHOWTO_INIT
    823 	boothowto |= BOOTHOWTO_INIT;
    824 #endif
    825 	{
    826 		uint8_t  gpio = ~gpio8(GPIO_PDATF);
    827 
    828 		if (gpio & (1<<5)) /* SW3 */
    829 			boothowto ^= RB_SINGLE;
    830 		if (gpio & (1<<7)) /* SW7 */
    831 			boothowto ^= RB_KDB;
    832 #ifdef VERBOSE_INIT_ARM
    833 		printf( "sw: %x boothowto: %x\n", gpio, boothowto );
    834 #endif
    835 	}
    836 
    837 #ifdef IPKDB
    838 	/* Initialise ipkdb */
    839 	ipkdb_init();
    840 	if (boothowto & RB_KDB)
    841 		ipkdb_connect(0);
    842 #endif
    843 
    844 #ifdef KGDB
    845 	if (boothowto & RB_KDB) {
    846 		kgdb_debug_init = 1;
    847 		kgdb_connect(1);
    848 	}
    849 #endif
    850 
    851 #if NKSYMS || defined(DDB) || defined(LKM)
    852 	/* Firmware doesn't load symbols. */
    853 	ksyms_init(0, NULL, NULL);
    854 #endif
    855 
    856 #ifdef DDB
    857 	db_machine_init();
    858 	if (boothowto & RB_KDB)
    859 		Debugger();
    860 #endif
    861 
    862 	/* We return the new stack pointer address */
    863 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    864 }
    865 
    866 void
    867 consinit(void)
    868 {
    869 	static int consinit_done = 0;
    870 	bus_space_tag_t iot = &s3c2xx0_bs_tag;
    871 	int pclk;
    872 
    873 	if (consinit_done != 0)
    874 		return;
    875 
    876 	consinit_done = 1;
    877 
    878 	pmap_devmap_register(smdk2800_devmap);
    879 
    880 	s3c2800_clock_freq2(ioreg_vaddr(S3C2800_CLKMAN_BASE), NULL, NULL, &pclk);
    881 
    882 #if NSSCOM > 0
    883 #ifdef SSCOM0CONSOLE
    884 	if (0 == s3c2800_sscom_cnattach(iot, 0, comcnspeed,
    885 		pclk, comcnmode))
    886 		return;
    887 #endif
    888 #ifdef SSCOM1CONSOLE
    889 	if (0 == s3c2800_sscom_cnattach(iot, 1, comcnspeed,
    890 		pclk, comcnmode))
    891 		return;
    892 #endif
    893 #endif				/* NSSCOM */
    894 #if NCOM>0 && defined(CONCOMADDR)
    895 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
    896 		COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    897 		panic("can't init serial console @%x", CONCOMADDR);
    898 	return;
    899 #endif
    900 
    901 	consinit_done = 0;
    902 }
    903 
    904 
    905 #ifdef KGDB
    906 
    907 #if (NSSCOM > 0)
    908 
    909 #ifdef KGDB_DEVNAME
    910 const char kgdb_devname[] = KGDB_DEVNAME;
    911 #else
    912 const char kgdb_devname[] = "";
    913 #endif
    914 
    915 #ifndef KGDB_DEVMODE
    916 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8) /* 8N1 */
    917 #endif
    918 int kgdb_sscom_mode = KGDB_DEVMODE;
    919 
    920 #endif				/* NSSCOM */
    921 
    922 void
    923 kgdb_port_init(void)
    924 {
    925 #if (NSSCOM > 0)
    926 	int unit = -1;
    927 	int pclk;
    928 
    929 	if (strcmp(kgdb_devname, "sscom0") == 0)
    930 		unit = 0;
    931 	else if (strcmp(kgdb_devname, "sscom1") == 0)
    932 		unit = 1;
    933 
    934 	if (unit >= 0) {
    935 		s3c2800_clock_freq2(ioreg_vaddr(S3C2800_CLKMAN_BASE),
    936 		    NULL, NULL, &pclk);
    937 
    938 		s3c2800_sscom_kgdb_attach(&s3c2xx0_bs_tag,
    939 		    unit, kgdb_rate, pclk, kgdb_sscom_mode);
    940 	}
    941 #endif
    942 }
    943 #endif
    944