Home | History | Annotate | Line # | Download | only in smdk2xx0
smdk2410_machdep.c revision 1.1
      1 /*	$NetBSD: smdk2410_machdep.c,v 1.1 2003/07/31 20:11:45 bsh Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2003 Fujitsu Component Limited
      5  * Copyright (c) 2002, 2003 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  * Copyright (c) 2001,2002 ARM Ltd
     36  * All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. The name of the company may not be used to endorse or promote
     47  *    products derived from this software without specific prior written
     48  *    permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ARM LTD
     54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  * POSSIBILITY OF SUCH DAMAGE.
     61  *
     62  */
     63 
     64 /*
     65  * Copyright (c) 1997,1998 Mark Brinicombe.
     66  * Copyright (c) 1997,1998 Causality Limited.
     67  * All rights reserved.
     68  *
     69  * Redistribution and use in source and binary forms, with or without
     70  * modification, are permitted provided that the following conditions
     71  * are met:
     72  * 1. Redistributions of source code must retain the above copyright
     73  *    notice, this list of conditions and the following disclaimer.
     74  * 2. Redistributions in binary form must reproduce the above copyright
     75  *    notice, this list of conditions and the following disclaimer in the
     76  *    documentation and/or other materials provided with the distribution.
     77  * 3. All advertising materials mentioning features or use of this software
     78  *    must display the following acknowledgement:
     79  *	This product includes software developed by Mark Brinicombe
     80  *	for the NetBSD Project.
     81  * 4. The name of the company nor the name of the author may be used to
     82  *    endorse or promote products derived from this software without specific
     83  *    prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     86  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     87  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     88  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     89  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     90  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     91  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  * Machine dependant functions for kernel setup for integrator board
     98  *
     99  * Created      : 24/11/97
    100  */
    101 
    102 /*
    103  * Machine dependant functions for kernel setup for Samsung SMDK2410
    104  * derived from integrator_machdep.c
    105  */
    106 
    107 #include <sys/cdefs.h>
    108 __KERNEL_RCSID(0, "$NetBSD: smdk2410_machdep.c,v 1.1 2003/07/31 20:11:45 bsh Exp $");
    109 
    110 #include "opt_ddb.h"
    111 #include "opt_kgdb.h"
    112 #include "opt_ipkdb.h"
    113 #include "opt_pmap_debug.h"
    114 #include "opt_md.h"
    115 #include "rbt.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/s3c2410reg.h>
    150 #include <arm/s3c2xx0/s3c2410var.h>
    151 
    152 #include "ksyms.h"
    153 
    154 #ifndef	SDRAM_START
    155 #define	SDRAM_START	S3C2410_SDRAM_START
    156 #endif
    157 #ifndef	SDRAM_SIZE
    158 #define	SDRAM_SIZE	(32*1024*1024)
    159 #endif
    160 
    161 /*
    162  * Address to map I/O registers in early initialize stage.
    163  */
    164 #define SMDK2410_VBASE_FREE	0xfd000000
    165 
    166 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    167 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    168 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    169 
    170 /*
    171  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    172  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    173  */
    174 #define KERNEL_VM_SIZE		0x0C000000
    175 
    176 /* Memory disk support */
    177 #if defined(MEMORY_DISK_DYNAMIC) && defined(MEMORY_DISK_ROOT_ADDR)
    178 #define DO_MEMORY_DISK
    179 /* We have memory disk image outside of the kernel on ROM. */
    180 #ifdef MEMORY_DISK_ROOT_ROM
    181 /* map the image directory and use read-only */
    182 #else
    183 /* copy the image to RAM */
    184 #endif
    185 #endif
    186 
    187 
    188 /*
    189  * Address to call from cpu_reset() to reset the machine.
    190  * This is machine architecture dependant as it varies depending
    191  * on where the ROM appears when you turn the MMU off.
    192  */
    193 u_int cpu_reset_address = (u_int)0;
    194 
    195 /* Define various stack sizes in pages */
    196 #define IRQ_STACK_SIZE	1
    197 #define ABT_STACK_SIZE	1
    198 #ifdef IPKDB
    199 #define UND_STACK_SIZE	2
    200 #else
    201 #define UND_STACK_SIZE	1
    202 #endif
    203 
    204 BootConfig bootconfig;		/* Boot config storage */
    205 char *boot_args = NULL;
    206 char *boot_file = NULL;
    207 
    208 vm_offset_t physical_start;
    209 vm_offset_t physical_freestart;
    210 vm_offset_t physical_freeend;
    211 vm_offset_t physical_end;
    212 u_int free_pages;
    213 vm_offset_t pagetables_start;
    214 int physmem = 0;
    215 
    216 /*int debug_flags;*/
    217 #ifndef PMAP_STATIC_L1S
    218 int max_processes = 64;		/* Default number */
    219 #endif				/* !PMAP_STATIC_L1S */
    220 
    221 /* Physical and virtual addresses for some global pages */
    222 pv_addr_t systempage;
    223 pv_addr_t irqstack;
    224 pv_addr_t undstack;
    225 pv_addr_t abtstack;
    226 pv_addr_t kernelstack;
    227 
    228 vm_offset_t msgbufphys;
    229 
    230 extern u_int data_abort_handler_address;
    231 extern u_int prefetch_abort_handler_address;
    232 extern u_int undefined_handler_address;
    233 
    234 #ifdef PMAP_DEBUG
    235 extern int pmap_debug_level;
    236 #endif
    237 
    238 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    239 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    240 #define	KERNEL_PT_KERNEL_NUM	2	/* L2 tables for mapping kernel VM */
    241 
    242 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    243 
    244 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    245 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    246 
    247 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    248 
    249 struct user *proc0paddr;
    250 
    251 /* Prototypes */
    252 
    253 void consinit(void);
    254 void kgdb_port_init(void);
    255 
    256 static int
    257 bootstrap_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
    258     int cacheable, bus_space_handle_t * bshp);
    259 static void copy_io_area_map(pd_entry_t * new_pd);
    260 extern int	s3c24x0_calc_fclk(unsigned int pllcon);
    261 
    262 /* A load of console goo. */
    263 #include "vga.h"
    264 #if NVGA > 0
    265 #include <dev/ic/mc6845reg.h>
    266 #include <dev/ic/pcdisplayvar.h>
    267 #include <dev/ic/vgareg.h>
    268 #include <dev/ic/vgavar.h>
    269 #endif
    270 
    271 #include "com.h"
    272 #if NCOM > 0
    273 #include <dev/ic/comreg.h>
    274 #include <dev/ic/comvar.h>
    275 #endif
    276 
    277 #include "sscom.h"
    278 #if NSSCOM > 0
    279 #include "opt_sscom.h"
    280 #include <arm/s3c2xx0/sscom_var.h>
    281 #endif
    282 
    283 /*
    284  * Define the default console speed for the board.  This is generally
    285  * what the firmware provided with the board defaults to.
    286  */
    287 #ifndef CONSPEED
    288 #define CONSPEED B115200	/* TTYDEF_SPEED */
    289 #endif
    290 #ifndef CONMODE
    291 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)   /* 8N1 */
    292 #endif
    293 
    294 int comcnspeed = CONSPEED;
    295 int comcnmode = CONMODE;
    296 
    297 struct bus_space bootstrap_bs_tag;
    298 
    299 /*
    300  * void cpu_reboot(int howto, char *bootstr)
    301  *
    302  * Reboots the system
    303  *
    304  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    305  * then reset the CPU.
    306  */
    307 void
    308 cpu_reboot(int howto, char *bootstr)
    309 {
    310 #ifdef DIAGNOSTIC
    311 	/* info */
    312 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    313 #endif
    314 
    315 #if	NRBT > 0
    316 #ifndef RB_JUMPTO
    317 #error	rbt pseudo device needs RB_JUMPTO flag
    318 #endif
    319 	if (howto & RB_JUMPTO) {
    320 		/* horrible hack to reboot new kernel loaded to physical memory */
    321 		cpu_reset_address = vtophys((u_int)bootstr);
    322 	}
    323 	else
    324 #endif
    325 	cpu_reset_address = vtophys((u_int)s3c2410_softreset);
    326 
    327 	/*
    328 	 * If we are still cold then hit the air brakes
    329 	 * and crash to earth fast
    330 	 */
    331 	if (cold) {
    332 		doshutdownhooks();
    333 		printf("The operating system has halted.\n");
    334 		printf("Please press any key to reboot.\n\n");
    335 		cngetc();
    336 		printf("rebooting...\n");
    337 		cpu_reset();
    338 		/* NOTREACHED */
    339 	}
    340 	/* Disable console buffering */
    341 
    342 	/*
    343 	 * If RB_NOSYNC was not specified sync the discs.
    344 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    345 	 * unmount.  It looks like syslogd is getting woken up only to find
    346 	 * that it cannot page part of the binary in as the filesystem has
    347 	 * been unmounted.
    348 	 */
    349 	if (!(howto & RB_NOSYNC))
    350 		bootsync();
    351 
    352 	/* Say NO to interrupts */
    353 	splhigh();
    354 
    355 	/* Do a dump if requested. */
    356 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    357 		dumpsys();
    358 
    359 	/* Run any shutdown hooks */
    360 	doshutdownhooks();
    361 
    362 	/* Make sure IRQ's are disabled */
    363 	IRQdisable;
    364 
    365 	if (howto & RB_HALT) {
    366 		printf("The operating system has halted.\n");
    367 		printf("Please press any key to reboot.\n\n");
    368 		cngetc();
    369 	}
    370 	printf("rebooting...\n");
    371 	cpu_reset();
    372 	/* NOTREACHED */
    373 }
    374 #define ioreg_write8(a,v)  (*(volatile uint8_t *)(a)=(v))
    375 
    376 /*
    377  * u_int initarm(...)
    378  *
    379  * Initial entry point on startup. This gets called before main() is
    380  * entered.
    381  * It should be responsible for setting up everything that must be
    382  * in place when main is called.
    383  * This includes
    384  *   Taking a copy of the boot configuration structure.
    385  *   Initialising the physical console so characters can be printed.
    386  *   Setting up page tables for the kernel
    387  *   Relocating the kernel to the bottom of physical memory
    388  */
    389 
    390 u_int
    391 initarm(void *arg)
    392 {
    393 	int loop;
    394 	int loop1;
    395 	u_int l1pagetable;
    396 	extern int etext asm("_etext");
    397 	extern int end asm("_end");
    398 	pv_addr_t kernel_l1pt;
    399 	struct s3c24x0_softc temp_softc;	/* used to initialize IO regs */
    400 	int progress_counter = 0;
    401 
    402 #ifdef DO_MEMORY_DISK
    403 	vm_offset_t md_root_start;
    404 #define MD_ROOT_SIZE (MEMORY_DISK_ROOT_SIZE * DEV_BSIZE)
    405 #endif
    406 
    407 #define gpio_read8(reg) bus_space_read_1(temp_softc.sc_sx.sc_iot,  \
    408 					 temp_softc.sc_sx.sc_gpio_ioh, (reg))
    409 
    410 #define LEDSTEP()  __LED(progress_counter++)
    411 
    412 #define pdatf (*(volatile uint8_t *)(S3C2410_GPIO_BASE+GPIO_PFDAT))
    413 #define __LED(x)  (pdatf = (pdatf & ~0xf0) | (~(x) & 0xf0))
    414 
    415 	LEDSTEP();
    416 	/*
    417 	 * Heads up ... Setup the CPU / MMU / TLB functions
    418 	 */
    419 	if (set_cpufuncs())
    420 		panic("cpu not recognized!");
    421 
    422 	LEDSTEP();
    423 
    424 	/*
    425 	 * prepare fake bus space tag
    426 	 */
    427 	bootstrap_bs_tag = s3c2xx0_bs_tag;
    428 	bootstrap_bs_tag.bs_map = bootstrap_bs_map;
    429 	s3c2xx0_softc = &temp_softc.sc_sx;
    430 	s3c2xx0_softc->sc_iot = &bootstrap_bs_tag;
    431 
    432 	bootstrap_bs_map(&bootstrap_bs_tag, S3C2410_GPIO_BASE,
    433 	    S3C2410_GPIO_SIZE, 0, &temp_softc.sc_sx.sc_gpio_ioh);
    434 	bootstrap_bs_map(&bootstrap_bs_tag, S3C2410_INTCTL_BASE,
    435 	    S3C2410_INTCTL_SIZE, 0, &temp_softc.sc_sx.sc_intctl_ioh);
    436 	bootstrap_bs_map(&bootstrap_bs_tag, S3C2410_CLKMAN_BASE,
    437 	    S3C2410_CLKMAN_SIZE, 0, &temp_softc.sc_sx.sc_clkman_ioh);
    438 
    439 #undef __LED
    440 #define __LED(x) 								\
    441 	bus_space_write_1(&bootstrap_bs_tag, temp_softc.sc_sx.sc_gpio_ioh,	\
    442 	    GPIO_PFDAT, (~((x)<<4) & 0xf0) |					\
    443 	    (gpio_read8(GPIO_PFDAT) & ~0xf0))
    444 
    445 	LEDSTEP();
    446 
    447 	/* Disable all peripheral interrupts */
    448 	bus_space_write_4(&bootstrap_bs_tag, temp_softc.sc_sx.sc_intctl_ioh,
    449 	    INTCTL_INTMSK, 0);
    450 
    451 	s3c24x0_clock_freq(s3c2xx0_softc);
    452 
    453 	consinit();
    454 	printf("consinit done\n");
    455 
    456 #ifdef KGDB
    457 	LEDSTEP();
    458 	kgdb_port_init();
    459 #endif
    460 	LEDSTEP();
    461 
    462 	/* Talk to the user */
    463 	printf("\nNetBSD/evbarm (SMDK2410) booting ...\n");
    464 
    465 	/*
    466 	 * Ok we have the following memory map
    467 	 *
    468 	 * Physical Address Range     Description
    469 	 * -----------------------    ----------------------------------
    470 	 * 0x00000000 - 0x00ffffff    Intel flash Memory   (16MB)
    471 	 * 0x02000000 - 0x020fffff    AMD flash Memory   (1MB)
    472 	 * or 			       (depend on DIPSW setting)
    473 	 * 0x00000000 - 0x000fffff    AMD flash Memory   (1MB)
    474 	 * 0x02000000 - 0x02ffffff    Intel flash Memory   (16MB)
    475 	 *
    476 	 * 0x30000000 - 0x31ffffff    SDRAM (32MB)
    477 	 *
    478 	 * The initarm() has the responsibility for creating the kernel
    479 	 * page tables.
    480 	 * It must also set up various memory pointers that are used
    481 	 * by pmap etc.
    482 	 */
    483 
    484 	/* Fake bootconfig structure for the benefit of pmap.c */
    485 	/* XXX must make the memory description h/w independent */
    486 	bootconfig.dramblocks = 1;
    487 	bootconfig.dram[0].address = SDRAM_START;
    488 	bootconfig.dram[0].pages = SDRAM_SIZE / PAGE_SIZE;
    489 
    490 	/*
    491 	 * Set up the variables that define the availablilty of
    492 	 * physical memory.  For now, we're going to set
    493 	 * physical_freestart to 0x08200000 (where the kernel
    494 	 * was loaded), and allocate the memory we need downwards.
    495 	 * If we get too close to the bottom of SDRAM, we
    496 	 * will panic.  We will update physical_freestart and
    497 	 * physical_freeend later to reflect what pmap_bootstrap()
    498 	 * wants to see.
    499 	 *
    500 	 * XXX pmap_bootstrap() needs an enema.
    501 	 */
    502 	physical_start = bootconfig.dram[0].address;
    503 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    504 
    505 #ifdef DO_MEMORY_DISK
    506 #ifdef MEMORY_DISK_ROOT_ROM
    507 	md_root_start = MEMORY_DISK_ROOT_ADDR;
    508 	boothowto |= RB_RDONLY;
    509 #else
    510 	/* Reserve physmem for ram disk */
    511 	md_root_start = ((physical_end - MD_ROOT_SIZE) & ~(L1_S_SIZE-1));
    512 	printf("Reserve %ld bytes for memory disk\n",
    513 	    physical_end - md_root_start);
    514 	/* copy fs contents */
    515 	memcpy((void *)md_root_start, (void *)MEMORY_DISK_ROOT_ADDR,
    516 	    MD_ROOT_SIZE);
    517 	physical_end = md_root_start;
    518 #endif
    519 #endif
    520 
    521 	physical_freestart = SDRAM_START;	/* XXX */
    522 	physical_freeend = SDRAM_START + 0x00200000;
    523 
    524 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    525 
    526 #ifdef VERBOSE_INIT_ARM
    527 	/* Tell the user about the memory */
    528 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    529 	    physical_start, physical_end - 1);
    530 #endif
    531 
    532 	/*
    533 	 * XXX
    534 	 * Okay, the kernel starts 2MB in from the bottom of physical
    535 	 * memory.  We are going to allocate our bootstrap pages downwards
    536 	 * from there.
    537 	 *
    538 	 * We need to allocate some fixed page tables to get the kernel
    539 	 * going.  We allocate one page directory and a number of page
    540 	 * tables and store the physical addresses in the kernel_pt_table
    541 	 * array.
    542 	 *
    543 	 * The kernel page directory must be on a 16K boundary.  The page
    544 	 * tables must be on 4K bounaries.  What we do is allocate the
    545 	 * page directory on the first 16K boundary that we encounter, and
    546 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    547 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    548 	 * least one 16K aligned region.
    549 	 */
    550 
    551 #ifdef VERBOSE_INIT_ARM
    552 	printf("Allocating page tables\n");
    553 #endif
    554 
    555 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    556 
    557 #ifdef VERBOSE_INIT_ARM
    558 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    559 	    physical_freestart, free_pages, free_pages);
    560 #endif
    561 
    562 	/* Define a macro to simplify memory allocation */
    563 #define	valloc_pages(var, np)				\
    564 	alloc_pages((var).pv_pa, (np));			\
    565 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    566 
    567 #define alloc_pages(var, np)				\
    568 	physical_freeend -= ((np) * PAGE_SIZE);		\
    569 	if (physical_freeend < physical_freestart)	\
    570 		panic("initarm: out of memory");	\
    571 	(var) = physical_freeend;			\
    572 	free_pages -= (np);				\
    573 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    574 
    575 	loop1 = 0;
    576 	kernel_l1pt.pv_pa = 0;
    577 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    578 		/* Are we 16KB aligned for an L1 ? */
    579 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    580 		    && kernel_l1pt.pv_pa == 0) {
    581 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    582 		} else {
    583 			valloc_pages(kernel_pt_table[loop1],
    584 			    L2_TABLE_SIZE / PAGE_SIZE);
    585 			++loop1;
    586 		}
    587 	}
    588 
    589 	/* This should never be able to happen but better confirm that. */
    590 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
    591 		panic("initarm: Failed to align the kernel page directory\n");
    592 
    593 	/*
    594 	 * Allocate a page for the system page mapped to V0x00000000
    595 	 * This page will just contain the system vectors and can be
    596 	 * shared by all processes.
    597 	 */
    598 	alloc_pages(systempage.pv_pa, 1);
    599 
    600 	/* Allocate stacks for all modes */
    601 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    602 	valloc_pages(abtstack, ABT_STACK_SIZE);
    603 	valloc_pages(undstack, UND_STACK_SIZE);
    604 	valloc_pages(kernelstack, UPAGES);
    605 
    606 #ifdef VERBOSE_INIT_ARM
    607 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    608 	    irqstack.pv_va);
    609 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    610 	    abtstack.pv_va);
    611 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    612 	    undstack.pv_va);
    613 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    614 	    kernelstack.pv_va);
    615 #endif
    616 
    617 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    618 
    619 	LEDSTEP();
    620 
    621 	/*
    622 	 * Ok we have allocated physical pages for the primary kernel
    623 	 * page tables
    624 	 */
    625 
    626 #ifdef VERBOSE_INIT_ARM
    627 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    628 #endif
    629 
    630 	/*
    631 	 * Now we start construction of the L1 page table
    632 	 * We start by mapping the L2 page tables into the L1.
    633 	 * This means that we can replace L1 mappings later on if necessary
    634 	 */
    635 	l1pagetable = kernel_l1pt.pv_pa;
    636 
    637 	/* Map the L2 pages tables in the L1 page table */
    638 	pmap_link_l2pt(l1pagetable, 0x00000000,
    639 	    &kernel_pt_table[KERNEL_PT_SYS]);
    640 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    641 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    642 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    643 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    644 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    645 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    646 
    647 	/* update the top of the kernel VM */
    648 	pmap_curmaxkvaddr =
    649 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    650 
    651 #ifdef VERBOSE_INIT_ARM
    652 	printf("Mapping kernel\n");
    653 #endif
    654 
    655 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    656 	{
    657 		size_t textsize = (uintptr_t)&etext - KERNEL_TEXT_BASE;
    658 		size_t totalsize = (uintptr_t)&end - KERNEL_TEXT_BASE;
    659 		u_int logical;
    660 
    661 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    662 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    663 
    664 		logical = 0x00200000;	/* offset of kernel in RAM */
    665 
    666 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    667 		    physical_start + logical, textsize,
    668 		    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    669 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    670 		    physical_start + logical, totalsize - textsize,
    671 		    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    672 	}
    673 
    674 #ifdef VERBOSE_INIT_ARM
    675 	printf("Constructing L2 page tables\n");
    676 #endif
    677 
    678 	/* Map the stack pages */
    679 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    680 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
    681 	    PTE_CACHE);
    682 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    683 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
    684 	    PTE_CACHE);
    685 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    686 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
    687 	    PTE_CACHE);
    688 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    689 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    690 
    691 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    692 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    693 
    694 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    695 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    696 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    697 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    698 	}
    699 
    700 	/* Map the vector page. */
    701 #if 1
    702 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
    703 	 * cache-clean code there.  */
    704 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    705 	    VM_PROT_READ | VM_PROT_WRITE, PTE_NOCACHE);
    706 #else
    707 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    708 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    709 #endif
    710 
    711 #ifdef MEMORY_DISK_DYNAMIC
    712 	/* map MD root image */
    713 	bootstrap_bs_map(&bootstrap_bs_tag, md_root_start, MD_ROOT_SIZE,
    714 			 BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_LINEAR,
    715 			 (bus_space_handle_t *)&md_root_start);
    716 
    717 	md_root_setconf((void *)md_root_start, MD_ROOT_SIZE);
    718 #endif /* MEMORY_DISK_DYNAMIC */
    719 	/*
    720 	 * map integrated peripherals at same address in l1pagetable
    721 	 * so that we can continue to use console.
    722 	 */
    723 	copy_io_area_map((pd_entry_t *)l1pagetable);
    724 
    725 	/*
    726 	 * Now we have the real page tables in place so we can switch to them.
    727 	 * Once this is done we will be running with the REAL kernel page
    728 	 * tables.
    729 	 */
    730 
    731 	/*
    732 	 * Update the physical_freestart/physical_freeend/free_pages
    733 	 * variables.
    734 	 */
    735 	{
    736 		physical_freestart = physical_start +
    737 		    (((((uintptr_t)&end) + PGOFSET) & ~PGOFSET) - KERNEL_BASE);
    738 		physical_freeend = physical_end;
    739 		free_pages =
    740 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    741 	}
    742 
    743 	/* Switch tables */
    744 #ifdef VERBOSE_INIT_ARM
    745 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    746 	    physical_freestart, free_pages, free_pages);
    747 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    748 #endif
    749 	LEDSTEP();
    750 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    751 	setttb(kernel_l1pt.pv_pa);
    752 	cpu_tlb_flushID();
    753 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    754 
    755 	/*
    756 	 * Moved from cpu_startup() as data_abort_handler() references
    757 	 * this during uvm init
    758 	 */
    759 	proc0paddr = (struct user *)kernelstack.pv_va;
    760 	lwp0.l_addr = proc0paddr;
    761 
    762 #ifdef VERBOSE_INIT_ARM
    763 	printf("done!\n");
    764 #endif
    765 
    766 	LEDSTEP();
    767 #ifdef VERBOSE_INIT_ARM
    768 	printf("bootstrap done.\n");
    769 #endif
    770 
    771 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    772 
    773 	/*
    774 	 * Pages were allocated during the secondary bootstrap for the
    775 	 * stacks for different CPU modes.
    776 	 * We must now set the r13 registers in the different CPU modes to
    777 	 * point to these stacks.
    778 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    779 	 * of the stack memory.
    780 	 */
    781 	printf("init subsystems: stacks ");
    782 
    783 	set_stackptr(PSR_IRQ32_MODE,
    784 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    785 	set_stackptr(PSR_ABT32_MODE,
    786 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    787 	set_stackptr(PSR_UND32_MODE,
    788 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    789 
    790 	LEDSTEP();
    791 
    792 	/*
    793 	 * Well we should set a data abort handler.
    794 	 * Once things get going this will change as we will need a proper
    795 	 * handler.
    796 	 * Until then we will use a handler that just panics but tells us
    797 	 * why.
    798 	 * Initialisation of the vectors will just panic on a data abort.
    799 	 * This just fills in a slighly better one.
    800 	 */
    801 #ifdef VERBOSE_INIT_ARM
    802 	printf("vectors ");
    803 #endif
    804 	data_abort_handler_address = (u_int)data_abort_handler;
    805 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    806 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    807 
    808 	/* Initialise the undefined instruction handlers */
    809 	printf("undefined ");
    810 	undefined_init();
    811 
    812 	LEDSTEP();
    813 
    814 	/* Load memory into UVM. */
    815 #ifdef VERBOSE_INIT_ARM
    816 	printf("page ");
    817 #endif
    818 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    819 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    820 	    atop(physical_freestart), atop(physical_freeend),
    821 	    VM_FREELIST_DEFAULT);
    822 
    823 	LEDSTEP();
    824 	/* Boot strap pmap telling it where the kernel page table is */
    825 #ifdef VERBOSE_INIT_ARM
    826 	printf("pmap ");
    827 #endif
    828 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    829 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    830 
    831 	LEDSTEP();
    832 
    833 	/* Setup the IRQ system */
    834 #ifdef VERBOSE_INIT_ARM
    835 	printf("irq ");
    836 #endif
    837 	/* XXX irq_init(); */
    838 
    839 	printf("done.\n");
    840 
    841 #ifdef BOOTHOWTO
    842 	boothowto |= BOOTHOWTO;
    843 #endif
    844 	{
    845 		uint8_t  gpio = ~gpio_read8(GPIO_PFDAT);
    846 
    847 		if (gpio & (1<<0)) /* SW1 (EINT0) */
    848 			boothowto ^= RB_SINGLE;
    849 		if (gpio & (1<<2)) /* SW2 (EINT2) */
    850 			boothowto ^= RB_KDB;
    851 #ifdef VERBOSE_INIT_ARM
    852 		printf( "sw: %x boothowto: %x\n", gpio, boothowto );
    853 #endif
    854 	}
    855 
    856 #ifdef IPKDB
    857 	/* Initialise ipkdb */
    858 	ipkdb_init();
    859 	if (boothowto & RB_KDB)
    860 		ipkdb_connect(0);
    861 #endif
    862 
    863 #ifdef KGDB
    864 	if (boothowto & RB_KDB) {
    865 		kgdb_debug_init = 1;
    866 		kgdb_connect(1);
    867 	}
    868 #endif
    869 
    870 #if NKSYMS || defined(DDB) || defined(LKM)
    871 	/* Firmware doesn't load symbols. */
    872 	ksyms_init(0, NULL, NULL);
    873 #endif
    874 
    875 #ifdef DDB
    876 	db_machine_init();
    877 	if (boothowto & RB_KDB)
    878 		Debugger();
    879 #endif
    880 
    881 	/* We return the new stack pointer address */
    882 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    883 }
    884 
    885 void
    886 consinit(void)
    887 {
    888 	static int consinit_done = 0;
    889 	bus_space_tag_t iot = s3c2xx0_softc->sc_iot;
    890 	int pclk = s3c2xx0_softc->sc_pclk;
    891 
    892 	if (consinit_done != 0)
    893 		return;
    894 
    895 	consinit_done = 1;
    896 
    897 #if NSSCOM > 0
    898 #ifdef SSCOM0CONSOLE
    899 	if (0 == s3c2410_sscom_cnattach(iot, 0, comcnspeed,
    900 		pclk, comcnmode))
    901 		return;
    902 #endif
    903 #ifdef SSCOM1CONSOLE
    904 	if (0 == s3c2410_sscom_cnattach(iot, 1, comcnspeed,
    905 		pclk, comcnmode))
    906 		return;
    907 #endif
    908 #endif				/* NSSCOM */
    909 #if NCOM>0 && defined(CONCOMADDR)
    910 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
    911 		COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    912 		panic("can't init serial console @%x", CONCOMADDR);
    913 	return;
    914 #endif
    915 
    916 	consinit_done = 0;
    917 }
    918 
    919 
    920 #ifdef KGDB
    921 
    922 #if (NSSCOM > 0)
    923 
    924 #ifdef KGDB_DEVNAME
    925 const char kgdb_devname[] = KGDB_DEVNAME;
    926 #else
    927 const char kgdb_devname[] = "";
    928 #endif
    929 
    930 #ifndef KGDB_DEVMODE
    931 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8) /* 8N1 */
    932 #endif
    933 int kgdb_sscom_mode = KGDB_DEVMODE;
    934 
    935 #endif				/* NSSCOM */
    936 
    937 void
    938 kgdb_port_init(void)
    939 {
    940 #if (NSSCOM > 0)
    941 	int unit = -1;
    942 	int pclk = s3c2xx0_softc->sc_pclk;
    943 
    944 	if (strcmp(kgdb_devname, "sscom0") == 0)
    945 		unit = 0;
    946 	else if (strcmp(kgdb_devname, "sscom1") == 0)
    947 		unit = 1;
    948 
    949 	if (unit >= 0) {
    950 		s3c2800_sscom_kgdb_attach(s3c2xx0_softc->sc_iot,
    951 		    unit, kgdb_rate, pclk, kgdb_sscom_mode);
    952 	}
    953 #endif
    954 }
    955 #endif
    956 
    957 static __inline
    958        pd_entry_t *
    959 read_ttb(void)
    960 {
    961 	long ttb;
    962 
    963 	__asm __volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r"(ttb));
    964 
    965 
    966 	return (pd_entry_t *)(ttb & ~((1 << 14) - 1));
    967 }
    968 
    969 
    970 static __inline void
    971 writeback_dcache_line(vaddr_t va)
    972 {
    973 	/* writeback Dcache line */
    974 	/* we can't use cpu_dcache_wb_range() here, because cpufuncs for ARM9
    975 	 * assume write-through cache, and always flush Dcache instead of
    976 	 * cleaning it. Since Boot loader maps page table with write-back
    977 	 * cached, we really need to clean Dcache. */
    978 	asm("mcr	p15, 0, %0, c7, c10, 1"
    979 	    : :	"r"(va));
    980 }
    981 
    982 static __inline void
    983 clean_dcache_line(vaddr_t va)
    984 {
    985 	/* writeback and invalidate Dcache line */
    986 	asm("mcr	p15, 0, %0, c7, c14, 1"
    987 	    : : "r"(va));
    988 }
    989 
    990 static vaddr_t section_free = SMDK2410_VBASE_FREE;
    991 
    992 /*
    993  * simple memory mapping function used in early bootstrap stage
    994  * before pmap is initialized.
    995  * This assumes only peripheral registers to map. they are mapped to
    996  * fixed address with section mapping.
    997  */
    998 static int
    999 bootstrap_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
   1000     int flag, bus_space_handle_t * bshp)
   1001 {
   1002 	long offset;
   1003 	int modified = 0;
   1004 	pd_entry_t *pagedir = read_ttb();
   1005 	/* This assumes PA==VA for page directory */
   1006 
   1007 	if (0) {
   1008 	} else {
   1009 		vaddr_t va;
   1010 		bus_addr_t pa;
   1011 		int cacheable = flag & BUS_SPACE_MAP_CACHEABLE;
   1012 
   1013 
   1014 		size = (size + L1_S_OFFSET) & ~L1_S_OFFSET;
   1015 		pa = bpa & ~L1_S_OFFSET;
   1016 		offset = bpa - pa;
   1017 
   1018 		va = section_free;
   1019 		while (size) {
   1020 			pmap_map_section((vaddr_t)pagedir, va,
   1021 			    pa, VM_PROT_READ | VM_PROT_WRITE,
   1022 			    cacheable ? PTE_CACHE : PTE_NOCACHE);
   1023 			writeback_dcache_line((vaddr_t)& pagedir[va >> L1_S_SHIFT]);
   1024 			va += L1_S_SIZE;
   1025 			pa += L1_S_SIZE;
   1026 			size -= L1_S_SIZE;
   1027 		}
   1028 
   1029 		*bshp = (bus_space_handle_t)(section_free + offset);
   1030 		section_free = va;
   1031 	}
   1032 
   1033 
   1034 	if (modified) {
   1035 
   1036 		cpu_drain_writebuf();
   1037 		cpu_tlb_flushD();
   1038 	}
   1039 	return (0);
   1040 }
   1041 
   1042 static void
   1043 copy_io_area_map(pd_entry_t * new_pd)
   1044 {
   1045 	pd_entry_t *cur_pd = read_ttb();
   1046 	int sec;
   1047 
   1048 	for (sec = SMDK2410_VBASE_FREE >> L1_S_SHIFT;
   1049 	    sec < (section_free >> L1_S_SHIFT); ++sec) {
   1050 		new_pd[sec] = cur_pd[sec];
   1051 		writeback_dcache_line((vaddr_t)&new_pd[sec]);
   1052 	}
   1053 	cpu_drain_writebuf();
   1054 }
   1055