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