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