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