Home | History | Annotate | Line # | Download | only in gemini
gemini_machdep.c revision 1.4
      1 /*	$NetBSD: gemini_machdep.c,v 1.4 2008/11/01 07:58:33 cliff Exp $	*/
      2 
      3 /* adapted from:
      4  *	NetBSD: sdp24xx_machdep.c,v 1.4 2008/08/27 11:03:10 matt Exp
      5  */
      6 
      7 /*
      8  * Machine dependent functions for kernel setup for TI OSK5912 board.
      9  * Based on lubbock_machdep.c which in turn was based on iq80310_machhdep.c
     10  *
     11  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
     12  * Written by Hiroyuki Bessho for Genetec Corporation.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. The name of Genetec Corporation may not be used to endorse or
     23  *    promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  *
     38  * Copyright (c) 2001 Wasabi Systems, Inc.
     39  * All rights reserved.
     40  *
     41  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed for the NetBSD Project by
     54  *	Wasabi Systems, Inc.
     55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     56  *    or promote products derived from this software without specific prior
     57  *    written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  *
     71  * Copyright (c) 1997,1998 Mark Brinicombe.
     72  * Copyright (c) 1997,1998 Causality Limited.
     73  * All rights reserved.
     74  *
     75  * Redistribution and use in source and binary forms, with or without
     76  * modification, are permitted provided that the following conditions
     77  * are met:
     78  * 1. Redistributions of source code must retain the above copyright
     79  *    notice, this list of conditions and the following disclaimer.
     80  * 2. Redistributions in binary form must reproduce the above copyright
     81  *    notice, this list of conditions and the following disclaimer in the
     82  *    documentation and/or other materials provided with the distribution.
     83  * 3. All advertising materials mentioning features or use of this software
     84  *    must display the following acknowledgement:
     85  *	This product includes software developed by Mark Brinicombe
     86  *	for the NetBSD Project.
     87  * 4. The name of the company nor the name of the author may be used to
     88  *    endorse or promote products derived from this software without specific
     89  *    prior written permission.
     90  *
     91  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     92  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     93  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     94  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     95  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     96  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     97  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     98  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     99  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    100  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    101  * SUCH DAMAGE.
    102  *
    103  * Copyright (c) 2007 Microsoft
    104  * All rights reserved.
    105  *
    106  * Redistribution and use in source and binary forms, with or without
    107  * modification, are permitted provided that the following conditions
    108  * are met:
    109  * 1. Redistributions of source code must retain the above copyright
    110  *    notice, this list of conditions and the following disclaimer.
    111  * 2. Redistributions in binary form must reproduce the above copyright
    112  *    notice, this list of conditions and the following disclaimer in the
    113  *    documentation and/or other materials provided with the distribution.
    114  * 3. All advertising materials mentioning features or use of this software
    115  *    must display the following acknowledgement:
    116  *	This product includes software developed by Microsoft
    117  *
    118  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
    119  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    120  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    121  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
    122  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    123  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    124  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    125  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    126  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    127  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    128  * SUCH DAMAGE.
    129  */
    130 
    131 #include <sys/cdefs.h>
    132 __KERNEL_RCSID(0, "$NetBSD: gemini_machdep.c,v 1.4 2008/11/01 07:58:33 cliff Exp $");
    133 
    134 #include "opt_machdep.h"
    135 #include "opt_ddb.h"
    136 #include "opt_kgdb.h"
    137 #include "opt_ipkdb.h"
    138 #include "opt_md.h"
    139 #include "opt_com.h"
    140 #include "opt_gemini.h"
    141 #include "geminiwdt.h"
    142 #include "md.h"
    143 
    144 #include <sys/param.h>
    145 #include <sys/device.h>
    146 #include <sys/systm.h>
    147 #include <sys/kernel.h>
    148 #include <sys/exec.h>
    149 #include <sys/proc.h>
    150 #include <sys/msgbuf.h>
    151 #include <sys/reboot.h>
    152 #include <sys/termios.h>
    153 #include <sys/ksyms.h>
    154 
    155 #include <uvm/uvm_extern.h>
    156 
    157 #include <sys/conf.h>
    158 #include <dev/cons.h>
    159 #include <dev/md.h>
    160 
    161 #include <machine/db_machdep.h>
    162 #include <ddb/db_sym.h>
    163 #include <ddb/db_extern.h>
    164 #ifdef KGDB
    165 #include <sys/kgdb.h>
    166 #endif
    167 
    168 #include <machine/bootconfig.h>
    169 #include <machine/bus.h>
    170 #include <machine/cpu.h>
    171 #include <machine/frame.h>
    172 #include <arm/armreg.h>
    173 #include <arm/undefined.h>
    174 
    175 #include <arm/arm32/machdep.h>
    176 
    177 #include <arm/gemini/gemini_reg.h>
    178 #include <arm/gemini/gemini_var.h>
    179 #include <arm/gemini/gemini_wdtvar.h>
    180 #include <arm/gemini/gemini_com.h>
    181 
    182 #include <evbarm/gemini/gemini.h>
    183 
    184 /*
    185  * Address to call from cpu_reset() to reset the machine.
    186  * This is machine architecture dependant as it varies depending
    187  * on where the ROM appears when you turn the MMU off.
    188  */
    189 
    190 u_int cpu_reset_address = 0;
    191 
    192 /* Define various stack sizes in pages */
    193 #define IRQ_STACK_SIZE	1
    194 #define FIQ_STACK_SIZE	1
    195 #define ABT_STACK_SIZE	1
    196 #ifdef IPKDB
    197 #define UND_STACK_SIZE	2
    198 #else
    199 #define UND_STACK_SIZE	1
    200 #endif
    201 
    202 BootConfig bootconfig;		/* Boot config storage */
    203 char *boot_args = NULL;
    204 char *boot_file = NULL;
    205 
    206 /* Physical address of the beginning of SDRAM. */
    207 paddr_t physical_start;
    208 /* Physical address of the first byte after the end of SDRAM. */
    209 paddr_t physical_end;
    210 /* Number of pages of memory. */
    211 int physmem = 0;
    212 
    213 /* Same things, but for the free (unused by the kernel) memory. */
    214 static paddr_t physical_freestart, physical_freeend;
    215 static u_int free_pages;
    216 
    217 /* Physical and virtual addresses for some global pages */
    218 pv_addr_t fiqstack;
    219 pv_addr_t irqstack;
    220 pv_addr_t undstack;
    221 pv_addr_t abtstack;
    222 pv_addr_t kernelstack;	/* stack for SVC mode */
    223 
    224 /* Physical address of the message buffer. */
    225 paddr_t msgbufphys;
    226 
    227 extern u_int data_abort_handler_address;
    228 extern u_int prefetch_abort_handler_address;
    229 extern u_int undefined_handler_address;
    230 extern char KERNEL_BASE_phys[];
    231 extern char KERNEL_BASE_virt[];
    232 extern char etext[], __data_start[], _edata[], __bss_start[], __bss_end__[];
    233 extern char _end[];
    234 
    235 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    236 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    237 #define	KERNEL_PT_KERNEL_NUM	4
    238 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
    239 				        /* Page tables for mapping kernel VM */
    240 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    241 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    242 
    243 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    244 
    245 extern struct user *proc0paddr;
    246 
    247 /*
    248  * Macros to translate between physical and virtual for a subset of the
    249  * kernel address space.  *Not* for general use.
    250  */
    251 #define KERNEL_BASE_PHYS ((paddr_t)&KERNEL_BASE_phys)
    252 
    253 #define KERN_VTOPHYS(va) \
    254 	((paddr_t)((vaddr_t)va - KERNEL_BASE + GEMINI_DRAM_BASE))
    255 #define KERN_PHYSTOV(pa) \
    256 	((vaddr_t)((paddr_t)pa - GEMINI_DRAM_BASE + KERNEL_BASE))
    257 
    258 /* Prototypes */
    259 
    260 void gemini_intr_init(bus_space_tag_t);
    261 void consinit(void);
    262 #ifdef KGDB
    263 static void kgdb_port_init(void);
    264 #endif
    265 
    266 static void setup_real_page_tables(void);
    267 static void init_clocks(void);
    268 
    269 bs_protos(bs_notimpl);
    270 
    271 #include "com.h"
    272 #if NCOM > 0
    273 #include <dev/ic/comreg.h>
    274 #include <dev/ic/comvar.h>
    275 #endif
    276 
    277 
    278 static void gemini_global_reset(void) __attribute__ ((noreturn));
    279 
    280 static void
    281 gemini_global_reset(void)
    282 {
    283 	volatile uint32_t *rp;
    284 	uint32_t r;
    285 
    286 	rp = (volatile uint32_t *)
    287 		(GEMINI_GLOBAL_VBASE + GEMINI_GLOBAL_RESET_CTL);
    288 	r = *rp;
    289 	r |= GLOBAL_RESET_GLOBAL;
    290 	*rp = r;
    291 	for(;;);
    292 	/* NOTREACHED */
    293 }
    294 
    295 /*
    296  * void cpu_reboot(int howto, char *bootstr)
    297  *
    298  * Reboots the system
    299  *
    300  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    301  * then reset the CPU.
    302  */
    303 void
    304 cpu_reboot(int howto, char *bootstr)
    305 {
    306 #ifdef DIAGNOSTIC
    307 	/* info */
    308 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    309 #endif
    310 
    311 	/*
    312 	 * If we are still cold then hit the air brakes
    313 	 * and crash to earth fast
    314 	 */
    315 	if (cold) {
    316 		doshutdownhooks();
    317 		printf("The operating system has halted.\n");
    318 		printf("Please press any key to reboot.\n\n");
    319 		cngetc();
    320 		printf("rebooting...\n");
    321 		delay(2000);			/* cnflush(); */
    322 		gemini_global_reset();
    323 		/*NOTREACHED*/
    324 	}
    325 
    326 	/* Disable console buffering */
    327 /*	cnpollc(1);*/
    328 
    329 	/*
    330 	 * If RB_NOSYNC was not specified sync the discs.
    331 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    332 	 * unmount.  It looks like syslogd is getting woken up only to find
    333 	 * that it cannot page part of the binary in as the filesystem has
    334 	 * been unmounted.
    335 	 */
    336 	if (!(howto & RB_NOSYNC))
    337 		bootsync();
    338 
    339 	/* Say NO to interrupts */
    340 	splhigh();
    341 
    342 	/* Do a dump if requested. */
    343 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    344 		dumpsys();
    345 
    346 	/* Run any shutdown hooks */
    347 	doshutdownhooks();
    348 
    349 	/* Make sure IRQ's are disabled */
    350 	IRQdisable;
    351 
    352 	if (howto & RB_HALT) {
    353 		printf("The operating system has halted.\n");
    354 		printf("Please press any key to reboot.\n\n");
    355 		cngetc();
    356 	}
    357 
    358 	printf("rebooting...\n");
    359 	delay(2000);			/* cnflush(); */
    360 	gemini_global_reset();
    361 	/*NOTREACHED*/
    362 }
    363 
    364 /*
    365  * Static device mappings. These peripheral registers are mapped at
    366  * fixed virtual addresses very early in initarm() so that we can use
    367  * them while booting the kernel, and stay at the same address
    368  * throughout whole kernel's life time.
    369  *
    370  * We use this table twice; once with bootstrap page table, and once
    371  * with kernel's page table which we build up in initarm().
    372  *
    373  * Since we map these registers into the bootstrap page table using
    374  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    375  * registers segment-aligned and segment-rounded in order to avoid
    376  * using the 2nd page tables.
    377  */
    378 
    379 #define	_A(a)	((a) & ~L1_S_OFFSET)
    380 #define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    381 
    382 static const struct pmap_devmap devmap[] = {
    383 	/* Global regs */
    384 	{
    385 		.pd_va = _A(GEMINI_GLOBAL_VBASE),
    386 		.pd_pa = _A(GEMINI_GLOBAL_BASE),
    387 		.pd_size = _S(L1_S_SIZE),
    388 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    389 		.pd_cache = PTE_NOCACHE
    390 	},
    391 
    392 	/* Watchdog */
    393 	{
    394 		.pd_va = _A(GEMINI_WATCHDOG_VBASE),
    395 		.pd_pa = _A(GEMINI_WATCHDOG_BASE),
    396 		.pd_size = _S(L1_S_SIZE),
    397 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    398 		.pd_cache = PTE_NOCACHE
    399 	},
    400 
    401 	/* UART */
    402 	{
    403 		.pd_va = _A(GEMINI_CONSOLE_VBASE),
    404 		.pd_pa = _A(CONSADDR),
    405 		.pd_size = _S(L1_S_SIZE),
    406 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    407 		.pd_cache = PTE_NOCACHE
    408 	},
    409 
    410 	/* Timers */
    411 	{
    412 		.pd_va = _A(GEMINI_TIMER_VBASE),
    413 		.pd_pa = _A(GEMINI_TIMER_BASE),
    414 		.pd_size = _S(L1_S_SIZE),
    415 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    416 		.pd_cache = PTE_NOCACHE
    417 	},
    418 
    419 	{0}	/* list terminator */
    420 };
    421 
    422 #undef	_A
    423 #undef	_S
    424 
    425 #ifdef DDB
    426 static void gemini_db_trap(int where)
    427 {
    428 #if  NGEMINIWDT > 0
    429 	static int oldwatchdogstate;
    430 
    431 	if (where) {
    432 		oldwatchdogstate = geminiwdt_enable(0);
    433 	} else {
    434 		geminiwdt_enable(oldwatchdogstate);
    435 	}
    436 #endif
    437 }
    438 #endif
    439 
    440 void gemini_putchar(char c);
    441 void
    442 gemini_putchar(char c)
    443 {
    444 	unsigned char *com0addr = (char *)GEMINI_CONSOLE_VBASE;
    445 	int timo = 150000;
    446 
    447 	while ((com0addr[COM_REG_LSR * 4] & LSR_TXRDY) == 0)
    448 		if (--timo == 0)
    449 			break;
    450 
    451 	com0addr[COM_REG_TXDATA] = c;
    452 
    453 	while ((com0addr[COM_REG_LSR * 4] & LSR_TSRE) == 0)
    454 		if (--timo == 0)
    455 			break;
    456 }
    457 
    458 void gemini_puthex(unsigned int);
    459 void
    460 gemini_puthex(unsigned int val)
    461 {
    462 	char hexc[] = "0123456789abcdef";
    463 
    464 	gemini_putchar('0');
    465 	gemini_putchar('x');
    466 	gemini_putchar(hexc[(val >> 28) & 0xf]);
    467 	gemini_putchar(hexc[(val >> 24) & 0xf]);
    468 	gemini_putchar(hexc[(val >> 20) & 0xf]);
    469 	gemini_putchar(hexc[(val >> 16) & 0xf]);
    470 	gemini_putchar(hexc[(val >> 12) & 0xf]);
    471 	gemini_putchar(hexc[(val >> 8) & 0xf]);
    472 	gemini_putchar(hexc[(val >> 4) & 0xf]);
    473 	gemini_putchar(hexc[(val >> 0) & 0xf]);
    474 }
    475 
    476 /*
    477  * u_int initarm(...)
    478  *
    479  * Initial entry point on startup. This gets called before main() is
    480  * entered.
    481  * It should be responsible for setting up everything that must be
    482  * in place when main is called.
    483  * This includes
    484  *   Taking a copy of the boot configuration structure.
    485  *   Initialising the physical console so characters can be printed.
    486  *   Setting up page tables for the kernel
    487  *   Relocating the kernel to the bottom of physical memory
    488  */
    489 u_int
    490 initarm(void *arg)
    491 {
    492 	gemini_putchar('0');
    493 
    494 	/*
    495 	 * When we enter here, we are using a temporary first level
    496 	 * translation table with section entries in it to cover the OBIO
    497 	 * peripherals and SDRAM.  The temporary first level translation table
    498 	 * is at the end of SDRAM.
    499 	 */
    500 
    501 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    502 	gemini_putchar('1');
    503 	if (set_cpufuncs())
    504 		panic("cpu not recognized!");
    505 
    506 	gemini_putchar('2');
    507 	init_clocks();
    508 	gemini_putchar('3');
    509 
    510 	/* The console is going to try to map things.  Give pmap a devmap. */
    511 	pmap_devmap_register(devmap);
    512 	gemini_putchar('4');
    513 	consinit();
    514 	gemini_putchar('5');
    515 #ifdef KGDB
    516 	kgdb_port_init();
    517 #endif
    518 
    519 #ifdef VERBOSE_INIT_ARM
    520 	/* Talk to the user */
    521 	printf("\nNetBSD/evbarm (gemini) booting ...\n");
    522 #endif
    523 
    524 #ifdef BOOT_ARGS
    525 	char mi_bootargs[] = BOOT_ARGS;
    526 	parse_mi_bootargs(mi_bootargs);
    527 #endif
    528 
    529 #ifdef VERBOSE_INIT_ARM
    530 	printf("initarm: Configuring system ...\n");
    531 #endif
    532 
    533 	/*
    534 	 * Set up the variables that define the availability of physical
    535 	 * memory.
    536 	 */
    537 	physical_start = GEMINI_DRAM_BASE;
    538 #define	MEMSIZE_BYTES 	(MEMSIZE * 1024 * 1024)
    539 	physical_end = (physical_start & ~(0x400000-1)) + MEMSIZE_BYTES;
    540 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    541 
    542 	/* Fake bootconfig structure for the benefit of pmap.c. */
    543 	bootconfig.dramblocks = 1;
    544 	bootconfig.dram[0].address = physical_start;
    545 	bootconfig.dram[0].pages = physmem;
    546 
    547 	/*
    548 	 * Our kernel is at the beginning of memory, so set our free space to
    549 	 * all the memory after the kernel.
    550 	 */
    551 	physical_freestart = KERN_VTOPHYS(round_page((vaddr_t) _end));
    552 	physical_freeend = physical_end;
    553 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    554 
    555 	/*
    556 	 * This is going to do all the hard work of setting up the first and
    557 	 * and second level page tables.  Pages of memory will be allocated
    558 	 * and mapped for other structures that are required for system
    559 	 * operation.  When it returns, physical_freestart and free_pages will
    560 	 * have been updated to reflect the allocations that were made.  In
    561 	 * addition, kernel_l1pt, kernel_pt_table[], systempage, irqstack,
    562 	 * abtstack, undstack, kernelstack, msgbufphys will be set to point to
    563 	 * the memory that was allocated for them.
    564 	 */
    565 	setup_real_page_tables();
    566 
    567 	/*
    568 	 * Moved from cpu_startup() as data_abort_handler() references
    569 	 * this during uvm init.
    570 	 */
    571 	proc0paddr = (struct user *)kernelstack.pv_va;
    572 	lwp0.l_addr = proc0paddr;
    573 
    574 #ifdef VERBOSE_INIT_ARM
    575 	printf("bootstrap done.\n");
    576 #endif
    577 
    578 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    579 
    580 	/*
    581 	 * Pages were allocated during the secondary bootstrap for the
    582 	 * stacks for different CPU modes.
    583 	 * We must now set the r13 registers in the different CPU modes to
    584 	 * point to these stacks.
    585 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    586 	 * of the stack memory.
    587 	 */
    588 #ifdef VERBOSE_INIT_ARM
    589 	printf("init subsystems: stacks ");
    590 #endif
    591 
    592 	set_stackptr(PSR_FIQ32_MODE, fiqstack.pv_va + FIQ_STACK_SIZE * PAGE_SIZE);
    593 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    594 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    595 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    596 
    597 	/*
    598 	 * Well we should set a data abort handler.
    599 	 * Once things get going this will change as we will need a proper
    600 	 * handler.
    601 	 * Until then we will use a handler that just panics but tells us
    602 	 * why.
    603 	 * Initialisation of the vectors will just panic on a data abort.
    604 	 * This just fills in a slightly better one.
    605 	 */
    606 #ifdef VERBOSE_INIT_ARM
    607 	printf("vectors ");
    608 #endif
    609 	data_abort_handler_address = (u_int)data_abort_handler;
    610 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    611 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    612 
    613 	/* Initialise the undefined instruction handlers */
    614 #ifdef VERBOSE_INIT_ARM
    615 	printf("undefined ");
    616 #endif
    617 	undefined_init();
    618 
    619 	/* Load memory into UVM. */
    620 #ifdef VERBOSE_INIT_ARM
    621 	printf("page ");
    622 #endif
    623 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
    624 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    625 	    atop(physical_freestart), atop(physical_freeend),
    626 	    VM_FREELIST_DEFAULT);
    627 	uvm_page_physload(atop(GEMINI_DRAM_BASE), atop(KERNEL_BASE_phys),
    628 	    atop(GEMINI_DRAM_BASE), atop(KERNEL_BASE_phys),
    629 	    VM_FREELIST_DEFAULT);
    630 
    631 	/* Boot strap pmap telling it where the kernel page table is */
    632 #ifdef VERBOSE_INIT_ARM
    633 	printf("pmap ");
    634 #endif
    635 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    636 
    637 #ifdef VERBOSE_INIT_ARM
    638 	printf("done.\n");
    639 #endif
    640 
    641 #ifdef IPKDB
    642 	/* Initialise ipkdb */
    643 	ipkdb_init();
    644 	if (boothowto & RB_KDB)
    645 		ipkdb_connect(0);
    646 #endif
    647 
    648 #ifdef KGDB
    649 	if (boothowto & RB_KDB) {
    650 		kgdb_debug_init = 1;
    651 		kgdb_connect(1);
    652 	}
    653 #endif
    654 
    655 #ifdef DDB
    656 	db_trap_callback = gemini_db_trap;
    657 	db_machine_init();
    658 
    659 	/* Firmware doesn't load symbols. */
    660 	ddb_init(0, NULL, NULL);
    661 
    662 	if (boothowto & RB_KDB)
    663 		Debugger();
    664 #endif
    665 	printf("initarm done.\n");
    666 
    667 	/* We return the new stack pointer address */
    668 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    669 }
    670 
    671 static void
    672 init_clocks(void)
    673 {
    674 }
    675 
    676 #ifndef CONSADDR
    677 #error Specify the address of the console UART with the CONSADDR option.
    678 #endif
    679 #ifndef CONSPEED
    680 #define CONSPEED 19200
    681 #endif
    682 #ifndef CONMODE
    683 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    684 #endif
    685 
    686 static const bus_addr_t consaddr = CONSADDR;
    687 static const int conspeed = CONSPEED;
    688 static const int conmode = CONMODE;
    689 
    690 void
    691 consinit(void)
    692 {
    693 	bus_space_handle_t bh;
    694 	static int consinit_called = 0;
    695 
    696 	if (consinit_called != 0)
    697 		return;
    698 	consinit_called = 1;
    699 
    700 	if (bus_space_map(&gemini_a4x_bs_tag, consaddr,
    701 		GEMINI_UART_SIZE, 0, &bh))
    702 			panic("Serial console can not be mapped.");
    703 
    704 	if (comcnattach(&gemini_a4x_bs_tag, consaddr, conspeed,
    705 		GEMINI_COM_FREQ, COM_TYPE_16550_NOERS, conmode))
    706 			panic("Serial console can not be initialized.");
    707 
    708 	bus_space_unmap(&gemini_a4x_bs_tag, bh, GEMINI_UART_SIZE);
    709 }
    710 
    711 #ifdef KGDB
    712 #ifndef KGDB_DEVADDR
    713 #error Specify the address of the kgdb UART with the KGDB_DEVADDR option.
    714 #endif
    715 #ifndef KGDB_DEVRATE
    716 #define KGDB_DEVRATE 19200
    717 #endif
    718 
    719 #ifndef KGDB_DEVMODE
    720 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    721 #endif
    722 static const vaddr_t comkgdbaddr = KGDB_DEVADDR;
    723 static const int comkgdbspeed = KGDB_DEVRATE;
    724 static const int comkgdbmode = KGDB_DEVMODE;
    725 
    726 void
    727 static kgdb_port_init(void)
    728 {
    729 	static int kgdbsinit_called = 0;
    730 
    731 	if (kgdbsinit_called != 0)
    732 		return;
    733 
    734 	kgdbsinit_called = 1;
    735 
    736 	bus_space_handle_t bh;
    737 	if (bus_space_map(&gemini_a4x_bs_tag, comkgdbaddr,
    738 		GEMINI_UART_SIZE, 0, &bh))
    739 			panic("kgdb port can not be mapped.");
    740 
    741 	if (com_kgdb_attach(&gemini_a4x_bs_tag, comkgdbaddr, comkgdbspeed,
    742 		GEMINI_UART_SIZE, COM_TYPE_16550_NOERS, comkgdbmode))
    743 			panic("KGDB uart can not be initialized.");
    744 
    745 	bus_space_unmap(&gemini_a4x_bs_tag, bh, GEMINI_UART_SIZE);
    746 }
    747 #endif
    748 
    749 static void
    750 setup_real_page_tables(void)
    751 {
    752 	/*
    753 	 * We need to allocate some fixed page tables to get the kernel going.
    754 	 *
    755 	 * We are going to allocate our bootstrap pages from the beginning of
    756 	 * the free space that we just calculated.  We allocate one page
    757 	 * directory and a number of page tables and store the physical
    758 	 * addresses in the kernel_pt_table array.
    759 	 *
    760 	 * The kernel page directory must be on a 16K boundary.  The page
    761 	 * tables must be on 4K boundaries.  What we do is allocate the
    762 	 * page directory on the first 16K boundary that we encounter, and
    763 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    764 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    765 	 * least one 16K aligned region.
    766 	 */
    767 
    768 #ifdef VERBOSE_INIT_ARM
    769 	printf("Allocating page tables\n");
    770 #endif
    771 
    772 	/*
    773 	 * Define a macro to simplify memory allocation.  As we allocate the
    774 	 * memory, make sure that we don't walk over our temporary first level
    775 	 * translation table.
    776 	 */
    777 #define valloc_pages(var, np)						\
    778 	(var).pv_pa = physical_freestart;				\
    779 	physical_freestart += ((np) * PAGE_SIZE);			\
    780 	if (physical_freestart > (physical_freeend - L1_TABLE_SIZE))	\
    781 		panic("initarm: out of memory");			\
    782 	free_pages -= (np);						\
    783 	(var).pv_va = KERN_PHYSTOV((var).pv_pa);			\
    784 	memset((char *)(var).pv_va, 0, ((np) * PAGE_SIZE));
    785 
    786 	int loop, pt_index;
    787 
    788 	pt_index = 0;
    789 	kernel_l1pt.pv_pa = 0;
    790 	kernel_l1pt.pv_va = 0;
    791 printf("%s: physical_freestart %#lx\n", __func__, physical_freestart);
    792 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    793 		/* Are we 16KB aligned for an L1 ? */
    794 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
    795 		    && kernel_l1pt.pv_pa == 0) {
    796 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    797 		} else {
    798 			valloc_pages(kernel_pt_table[pt_index],
    799 			    L2_TABLE_SIZE / PAGE_SIZE);
    800 			++pt_index;
    801 		}
    802 	}
    803 pt_index=0;
    804 printf("%s: kernel_l1pt: %#lx:%#lx\n", __func__, kernel_l1pt.pv_va, kernel_l1pt.pv_pa);
    805 printf("%s: kernel_pt_table:\n", __func__);
    806 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    807 printf("\t%#lx:%#lx\n", kernel_pt_table[pt_index].pv_va, kernel_pt_table[pt_index].pv_pa);
    808 ++pt_index;
    809 }
    810 
    811 	/* This should never be able to happen but better confirm that. */
    812 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    813 		panic("initarm: Failed to align the kernel page directory");
    814 
    815 	/*
    816 	 * Allocate a page for the system page mapped to V0x00000000
    817 	 * This page will just contain the system vectors and can be
    818 	 * shared by all processes.
    819 	 */
    820 	valloc_pages(systempage, 1);
    821 	systempage.pv_va = ARM_VECTORS_HIGH;
    822 
    823 	/* Allocate stacks for all modes */
    824 	valloc_pages(fiqstack, FIQ_STACK_SIZE);
    825 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    826 	valloc_pages(abtstack, ABT_STACK_SIZE);
    827 	valloc_pages(undstack, UND_STACK_SIZE);
    828 	valloc_pages(kernelstack, UPAGES);
    829 
    830 	/* Allocate the message buffer. */
    831 	pv_addr_t msgbuf;
    832 	int msgbuf_pgs = round_page(MSGBUFSIZE) / PAGE_SIZE;
    833 	valloc_pages(msgbuf, msgbuf_pgs);
    834 	msgbufphys = msgbuf.pv_pa;
    835 
    836 	/*
    837 	 * Ok we have allocated physical pages for the primary kernel
    838 	 * page tables
    839 	 */
    840 
    841 #ifdef VERBOSE_INIT_ARM
    842 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    843 #endif
    844 
    845 	/*
    846 	 * Now we start construction of the L1 page table
    847 	 * We start by mapping the L2 page tables into the L1.
    848 	 * This means that we can replace L1 mappings later on if necessary
    849 	 */
    850 	vaddr_t l1_va = kernel_l1pt.pv_va;
    851 	paddr_t l1_pa = kernel_l1pt.pv_pa;
    852 
    853 	/* Map the L2 pages tables in the L1 page table */
    854 	pmap_link_l2pt(l1_va, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    855 		       &kernel_pt_table[KERNEL_PT_SYS]);
    856 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    857 		pmap_link_l2pt(l1_va, KERNEL_BASE + loop * 0x00400000,
    858 			       &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    859 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    860 		pmap_link_l2pt(l1_va, KERNEL_VM_BASE + loop * 0x00400000,
    861 			       &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    862 
    863 	/* update the top of the kernel VM */
    864 	pmap_curmaxkvaddr =
    865 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    866 
    867 #ifdef VERBOSE_INIT_ARM
    868 	printf("Mapping kernel\n");
    869 #endif
    870 
    871 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    872 #define round_L_page(x) (((x) + L2_L_OFFSET) & L2_L_FRAME)
    873 	size_t textsize = round_L_page(etext - KERNEL_BASE_virt);
    874 	size_t totalsize = round_L_page(_end - KERNEL_BASE_virt);
    875 	/* offset of kernel in RAM */
    876 	u_int offset = (u_int)KERNEL_BASE_virt - KERNEL_BASE;
    877 
    878 	/* Map text section read-only. */
    879 	offset += pmap_map_chunk(l1_va,
    880 				(vaddr_t)KERNEL_BASE + offset,
    881 				 physical_start + offset, textsize,
    882 				 VM_PROT_READ|VM_PROT_EXECUTE, PTE_CACHE);
    883 	/* Map data and bss sections read-write. */
    884 	offset += pmap_map_chunk(l1_va,
    885 				(vaddr_t)KERNEL_BASE + offset,
    886 				 physical_start + offset, totalsize - textsize,
    887 				 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    888 
    889 #ifdef VERBOSE_INIT_ARM
    890 	printf("Constructing L2 page tables\n");
    891 #endif
    892 
    893 	/* Map the stack pages */
    894 	pmap_map_chunk(l1_va, fiqstack.pv_va, fiqstack.pv_pa,
    895 	    FIQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    896 	pmap_map_chunk(l1_va, irqstack.pv_va, irqstack.pv_pa,
    897 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    898 	pmap_map_chunk(l1_va, abtstack.pv_va, abtstack.pv_pa,
    899 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    900 	pmap_map_chunk(l1_va, undstack.pv_va, undstack.pv_pa,
    901 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    902 	pmap_map_chunk(l1_va, kernelstack.pv_va, kernelstack.pv_pa,
    903 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    904 
    905 	pmap_map_chunk(l1_va, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    906 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    907 
    908 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    909 		pmap_map_chunk(l1_va, kernel_pt_table[loop].pv_va,
    910 			       kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    911 			       VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    912 	}
    913 
    914 	/* Map the vector page. */
    915 	pmap_map_entry(l1_va, ARM_VECTORS_HIGH, systempage.pv_pa,
    916 		       VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    917 
    918 	/*
    919 	 * Map integrated peripherals at same address in first level page
    920 	 * table so that we can continue to use console.
    921 	 */
    922 	pmap_devmap_bootstrap(l1_va, devmap);
    923 
    924 
    925 #ifdef VERBOSE_INIT_ARM
    926 	/* Tell the user about where all the bits and pieces live. */
    927 	printf("%22s       Physical              Virtual        Num\n", " ");
    928 	printf("%22s Starting    Ending    Starting    Ending   Pages\n", " ");
    929 
    930 	static const char mem_fmt[] =
    931 	    "%20s: 0x%08lx 0x%08lx 0x%08lx 0x%08lx %d\n";
    932 	static const char mem_fmt_nov[] =
    933 	    "%20s: 0x%08lx 0x%08lx                       %d\n";
    934 
    935 	printf(mem_fmt, "SDRAM", physical_start, physical_end-1,
    936 	    KERN_PHYSTOV(physical_start), KERN_PHYSTOV(physical_end-1),
    937 	    physmem);
    938 	printf(mem_fmt, "text section",
    939 	       KERN_VTOPHYS(KERNEL_BASE_virt), KERN_VTOPHYS(etext-1),
    940 	       (vaddr_t)KERNEL_BASE_virt, (vaddr_t)etext-1,
    941 	       (int)(textsize / PAGE_SIZE));
    942 	printf(mem_fmt, "data section",
    943 	       KERN_VTOPHYS(__data_start), KERN_VTOPHYS(_edata),
    944 	       (vaddr_t)__data_start, (vaddr_t)_edata,
    945 	       (int)((round_page((vaddr_t)_edata)
    946 		      - trunc_page((vaddr_t)__data_start)) / PAGE_SIZE));
    947 	printf(mem_fmt, "bss section",
    948 	       KERN_VTOPHYS(__bss_start), KERN_VTOPHYS(__bss_end__),
    949 	       (vaddr_t)__bss_start, (vaddr_t)__bss_end__,
    950 	       (int)((round_page((vaddr_t)__bss_end__)
    951 		      - trunc_page((vaddr_t)__bss_start)) / PAGE_SIZE));
    952 	printf(mem_fmt, "L1 page directory",
    953 	    kernel_l1pt.pv_pa, kernel_l1pt.pv_pa + L1_TABLE_SIZE - 1,
    954 	    kernel_l1pt.pv_va, kernel_l1pt.pv_va + L1_TABLE_SIZE - 1,
    955 	    L1_TABLE_SIZE / PAGE_SIZE);
    956 	printf(mem_fmt, "Exception Vectors",
    957 	    systempage.pv_pa, systempage.pv_pa + PAGE_SIZE - 1,
    958 	    (vaddr_t)ARM_VECTORS_HIGH, (vaddr_t)ARM_VECTORS_HIGH + PAGE_SIZE - 1,
    959 	    1);
    960 	printf(mem_fmt, "FIQ stack",
    961 	    fiqstack.pv_pa, fiqstack.pv_pa + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
    962 	    fiqstack.pv_va, fiqstack.pv_va + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
    963 	    FIQ_STACK_SIZE);
    964 	printf(mem_fmt, "IRQ stack",
    965 	    irqstack.pv_pa, irqstack.pv_pa + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
    966 	    irqstack.pv_va, irqstack.pv_va + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
    967 	    IRQ_STACK_SIZE);
    968 	printf(mem_fmt, "ABT stack",
    969 	    abtstack.pv_pa, abtstack.pv_pa + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
    970 	    abtstack.pv_va, abtstack.pv_va + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
    971 	    ABT_STACK_SIZE);
    972 	printf(mem_fmt, "UND stack",
    973 	    undstack.pv_pa, undstack.pv_pa + (UND_STACK_SIZE * PAGE_SIZE) - 1,
    974 	    undstack.pv_va, undstack.pv_va + (UND_STACK_SIZE * PAGE_SIZE) - 1,
    975 	    UND_STACK_SIZE);
    976 	printf(mem_fmt, "SVC stack",
    977 	    kernelstack.pv_pa, kernelstack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
    978 	    kernelstack.pv_va, kernelstack.pv_va + (UPAGES * PAGE_SIZE) - 1,
    979 	    UPAGES);
    980 	printf(mem_fmt_nov, "Message Buffer",
    981 	    msgbufphys, msgbufphys + msgbuf_pgs * PAGE_SIZE - 1, msgbuf_pgs);
    982 	printf(mem_fmt, "Free Memory", physical_freestart, physical_freeend-1,
    983 	    KERN_PHYSTOV(physical_freestart), KERN_PHYSTOV(physical_freeend-1),
    984 	    free_pages);
    985 #endif
    986 
    987 	/*
    988 	 * Now we have the real page tables in place so we can switch to them.
    989 	 * Once this is done we will be running with the REAL kernel page
    990 	 * tables.
    991 	 */
    992 
    993 	/* Switch tables */
    994 #ifdef VERBOSE_INIT_ARM
    995 	printf("switching to new L1 page table  @%#lx...", l1_pa);
    996 #endif
    997 
    998 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    999 	setttb(l1_pa);
   1000 	cpu_tlb_flushID();
   1001 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
   1002 
   1003 #ifdef VERBOSE_INIT_ARM
   1004 	printf("OK.\n");
   1005 #endif
   1006 }
   1007