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