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