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