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