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