Home | History | Annotate | Line # | Download | only in g42xxeb
g42xxeb_machdep.c revision 1.21
      1 /*	$NetBSD: g42xxeb_machdep.c,v 1.21 2009/12/28 03:22:20 uebayasi Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2003, 2004, 2005  Genetec Corporation.
      5  * All rights reserved.
      6  *
      7  * Written by Hiroyuki Bessho for Genetec Corporation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Genetec Corporation may not be used to endorse or
     18  *    promote products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  * Machine dependant functions for kernel setup for Genetec G4250EBX
     34  * evaluation board.
     35  *
     36  * Based on iq80310_machhdep.c
     37  */
     38 /*
     39  * Copyright (c) 2001 Wasabi Systems, Inc.
     40  * All rights reserved.
     41  *
     42  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed for the NetBSD Project by
     55  *	Wasabi Systems, Inc.
     56  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     57  *    or promote products derived from this software without specific prior
     58  *    written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     62  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     63  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     64  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     65  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     66  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     67  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     68  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     69  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     70  * POSSIBILITY OF SUCH DAMAGE.
     71  */
     72 
     73 /*
     74  * Copyright (c) 1997,1998 Mark Brinicombe.
     75  * Copyright (c) 1997,1998 Causality Limited.
     76  * All rights reserved.
     77  *
     78  * Redistribution and use in source and binary forms, with or without
     79  * modification, are permitted provided that the following conditions
     80  * are met:
     81  * 1. Redistributions of source code must retain the above copyright
     82  *    notice, this list of conditions and the following disclaimer.
     83  * 2. Redistributions in binary form must reproduce the above copyright
     84  *    notice, this list of conditions and the following disclaimer in the
     85  *    documentation and/or other materials provided with the distribution.
     86  * 3. All advertising materials mentioning features or use of this software
     87  *    must display the following acknowledgement:
     88  *	This product includes software developed by Mark Brinicombe
     89  *	for the NetBSD Project.
     90  * 4. The name of the company nor the name of the author may be used to
     91  *    endorse or promote products derived from this software without specific
     92  *    prior written permission.
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     95  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     96  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     97  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     98  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     99  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    100  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  * SUCH DAMAGE.
    105  *
    106  * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
    107  * boards using RedBoot firmware.
    108  */
    109 
    110 #include "opt_ddb.h"
    111 #include "opt_kgdb.h"
    112 #include "opt_pmap_debug.h"
    113 #include "opt_md.h"
    114 #include "opt_com.h"
    115 #include "md.h"
    116 #include "lcd.h"
    117 
    118 #include <sys/param.h>
    119 #include <sys/device.h>
    120 #include <sys/systm.h>
    121 #include <sys/kernel.h>
    122 #include <sys/exec.h>
    123 #include <sys/proc.h>
    124 #include <sys/msgbuf.h>
    125 #include <sys/reboot.h>
    126 #include <sys/termios.h>
    127 #include <sys/ksyms.h>
    128 
    129 #include <uvm/uvm_extern.h>
    130 
    131 #include <sys/conf.h>
    132 #include <dev/cons.h>
    133 #include <dev/md.h>
    134 
    135 #include <machine/db_machdep.h>
    136 #include <ddb/db_sym.h>
    137 #include <ddb/db_extern.h>
    138 #ifdef KGDB
    139 #include <sys/kgdb.h>
    140 #endif
    141 
    142 #include <machine/bootconfig.h>
    143 #include <machine/bus.h>
    144 #include <machine/cpu.h>
    145 #include <machine/frame.h>
    146 #include <arm/undefined.h>
    147 
    148 #include <arm/arm32/machdep.h>
    149 
    150 #include <arm/xscale/pxa2x0reg.h>
    151 #include <arm/xscale/pxa2x0var.h>
    152 #include <arm/xscale/pxa2x0_gpio.h>
    153 #include <evbarm/g42xxeb/g42xxeb_reg.h>
    154 #include <evbarm/g42xxeb/g42xxeb_var.h>
    155 
    156 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    157 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    158 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    159 
    160 /*
    161  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    162  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    163  */
    164 #define KERNEL_VM_SIZE		0x0C000000
    165 
    166 
    167 /*
    168  * Address to call from cpu_reset() to reset the machine.
    169  * This is machine architecture dependant as it varies depending
    170  * on where the ROM appears when you turn the MMU off.
    171  */
    172 
    173 u_int cpu_reset_address = 0;
    174 
    175 /* Define various stack sizes in pages */
    176 #define IRQ_STACK_SIZE	1
    177 #define ABT_STACK_SIZE	1
    178 #define UND_STACK_SIZE	1
    179 
    180 BootConfig bootconfig;		/* Boot config storage */
    181 char *boot_args = NULL;
    182 char *boot_file = NULL;
    183 
    184 vm_offset_t physical_start;
    185 vm_offset_t physical_freestart;
    186 vm_offset_t physical_freeend;
    187 vm_offset_t physical_end;
    188 u_int free_pages;
    189 
    190 /*int debug_flags;*/
    191 #ifndef PMAP_STATIC_L1S
    192 int max_processes = 64;			/* Default number */
    193 #endif	/* !PMAP_STATIC_L1S */
    194 
    195 /* Physical and virtual addresses for some global pages */
    196 pv_addr_t irqstack;
    197 pv_addr_t undstack;
    198 pv_addr_t abtstack;
    199 pv_addr_t kernelstack;
    200 pv_addr_t minidataclean;
    201 
    202 vm_offset_t msgbufphys;
    203 
    204 extern u_int data_abort_handler_address;
    205 extern u_int prefetch_abort_handler_address;
    206 extern u_int undefined_handler_address;
    207 
    208 #ifdef PMAP_DEBUG
    209 extern int pmap_debug_level;
    210 #endif
    211 
    212 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    213 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    214 #define	KERNEL_PT_KERNEL_NUM	4
    215 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
    216 				        /* Page tables for mapping kernel VM */
    217 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    218 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    219 
    220 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    221 
    222 /* Prototypes */
    223 
    224 #if 0
    225 void	process_kernel_args(char *);
    226 #endif
    227 
    228 void	consinit(void);
    229 void	kgdb_port_init(void);
    230 void	change_clock(uint32_t v);
    231 
    232 bs_protos(bs_notimpl);
    233 
    234 #include "com.h"
    235 #if NCOM > 0
    236 #include <dev/ic/comreg.h>
    237 #include <dev/ic/comvar.h>
    238 #endif
    239 
    240 #ifndef CONSPEED
    241 #define CONSPEED B115200	/* What RedBoot uses */
    242 #endif
    243 #ifndef CONMODE
    244 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    245 #endif
    246 
    247 int comcnspeed = CONSPEED;
    248 int comcnmode = CONMODE;
    249 
    250 static struct pxa2x0_gpioconf boarddep_gpioconf[] = {
    251 	{ 44, GPIO_ALT_FN_1_IN },	/* BTCST */
    252 	{ 45, GPIO_ALT_FN_2_OUT },	/* BTRST */
    253 
    254 	{ -1 }
    255 };
    256 static struct pxa2x0_gpioconf *g42xxeb_gpioconf[] = {
    257 	pxa25x_com_btuart_gpioconf,
    258 	pxa25x_com_ffuart_gpioconf,
    259 #if 0
    260 	pxa25x_com_stuart_gpioconf,
    261 	pxa25x_pxaacu_gpioconf,
    262 #endif
    263 	boarddep_gpioconf,
    264 	NULL
    265 };
    266 
    267 /*
    268  * void cpu_reboot(int howto, char *bootstr)
    269  *
    270  * Reboots the system
    271  *
    272  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    273  * then reset the CPU.
    274  */
    275 void
    276 cpu_reboot(int howto, char *bootstr)
    277 {
    278 #ifdef DIAGNOSTIC
    279 	/* info */
    280 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    281 #endif
    282 
    283 	/*
    284 	 * If we are still cold then hit the air brakes
    285 	 * and crash to earth fast
    286 	 */
    287 	if (cold) {
    288 		doshutdownhooks();
    289 		pmf_system_shutdown(boothowto);
    290 		printf("The operating system has halted.\n");
    291 		printf("Please press any key to reboot.\n\n");
    292 		cngetc();
    293 		printf("rebooting...\n");
    294 		cpu_reset();
    295 		/*NOTREACHED*/
    296 	}
    297 
    298 	/* Disable console buffering */
    299 /*	cnpollc(1);*/
    300 
    301 	/*
    302 	 * If RB_NOSYNC was not specified sync the discs.
    303 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    304 	 * unmount.  It looks like syslogd is getting woken up only to find
    305 	 * that it cannot page part of the binary in as the filesystem has
    306 	 * been unmounted.
    307 	 */
    308 	if (!(howto & RB_NOSYNC))
    309 		bootsync();
    310 
    311 	/* Say NO to interrupts */
    312 	splhigh();
    313 
    314 	/* Do a dump if requested. */
    315 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    316 		dumpsys();
    317 
    318 	/* Run any shutdown hooks */
    319 	doshutdownhooks();
    320 
    321 	pmf_system_shutdown(boothowto);
    322 
    323 	/* Make sure IRQ's are disabled */
    324 	IRQdisable;
    325 
    326 	if (howto & RB_HALT) {
    327 		printf("The operating system has halted.\n");
    328 		printf("Please press any key to reboot.\n\n");
    329 		cngetc();
    330 	}
    331 
    332 	printf("rebooting...\n");
    333 	cpu_reset();
    334 	/*NOTREACHED*/
    335 }
    336 
    337 static inline
    338 pd_entry_t *
    339 read_ttb(void)
    340 {
    341   long ttb;
    342 
    343   __asm volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r" (ttb));
    344 
    345 
    346   return (pd_entry_t *)(ttb & ~((1<<14)-1));
    347 }
    348 
    349 /*
    350  * Static device mappings. These peripheral registers are mapped at
    351  * fixed virtual addresses very early in initarm() so that we can use
    352  * them while booting the kernel, and stay at the same address
    353  * throughout whole kernel's life time.
    354  *
    355  * We use this table twice; once with bootstrap page table, and once
    356  * with kernel's page table which we build up in initarm().
    357  *
    358  * Since we map these registers into the bootstrap page table using
    359  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    360  * registers segment-aligned and segment-rounded in order to avoid
    361  * using the 2nd page tables.
    362  */
    363 
    364 #define	_A(a)	((a) & ~L1_S_OFFSET)
    365 #define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    366 
    367 static const struct pmap_devmap g42xxeb_devmap[] = {
    368     {
    369 	    G42XXEB_PLDREG_VBASE,
    370 	    _A(G42XXEB_PLDREG_BASE),
    371 	    _S(G42XXEB_PLDREG_SIZE),
    372 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    373     },
    374     {
    375 	    G42XXEB_GPIO_VBASE,
    376 	    _A(PXA2X0_GPIO_BASE),
    377 	    _S(PXA250_GPIO_SIZE),
    378 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    379     },
    380     {
    381 	    G42XXEB_CLKMAN_VBASE,
    382 	    _A(PXA2X0_CLKMAN_BASE),
    383 	    _S(PXA2X0_CLKMAN_SIZE),
    384 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    385     },
    386     {
    387 	    G42XXEB_INTCTL_VBASE,
    388 	    _A(PXA2X0_INTCTL_BASE),
    389 	    _S(PXA2X0_INTCTL_SIZE),
    390 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    391     },
    392     {
    393 	    G42XXEB_FFUART_VBASE,
    394 	    _A(PXA2X0_FFUART_BASE),
    395 	    _S(4 * COM_NPORTS),
    396 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    397     },
    398     {
    399 	    G42XXEB_BTUART_VBASE,
    400 	    _A(PXA2X0_BTUART_BASE),
    401 	    _S(4 * COM_NPORTS),
    402 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
    403     },
    404     {0, 0, 0, 0,}
    405 };
    406 
    407 #undef	_A
    408 #undef	_S
    409 
    410 
    411 /*
    412  * u_int initarm(...)
    413  *
    414  * Initial entry point on startup. This gets called before main() is
    415  * entered.
    416  * It should be responsible for setting up everything that must be
    417  * in place when main is called.
    418  * This includes
    419  *   Taking a copy of the boot configuration structure.
    420  *   Initialising the physical console so characters can be printed.
    421  *   Setting up page tables for the kernel
    422  *   Relocating the kernel to the bottom of physical memory
    423  */
    424 u_int
    425 initarm(void *arg)
    426 {
    427 	extern vaddr_t xscale_cache_clean_addr;
    428 	int loop;
    429 	int loop1;
    430 	u_int l1pagetable;
    431 	paddr_t memstart;
    432 	psize_t memsize;
    433 	int led_data = 1;
    434 #ifdef DIAGNOSTIC
    435 	extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
    436 #endif
    437 
    438 #define LEDSTEP_P() ioreg8_write(G42XXEB_PLDREG_BASE+G42XXEB_LED, led_data++)
    439 #define LEDSTEP() pldreg8_write(G42XXEB_LED, led_data++);
    440 
    441 	/* use physical address until pagetable is set */
    442 	LEDSTEP_P();
    443 
    444 	/* map some peripheral registers at static I/O area */
    445 	pmap_devmap_bootstrap((vaddr_t)read_ttb(), g42xxeb_devmap);
    446 
    447 	LEDSTEP_P();
    448 
    449 	/* start 32.768 kHz OSC */
    450 	ioreg_write(G42XXEB_CLKMAN_VBASE + 0x08, 2);
    451 	/* Get ready for splfoo() */
    452 	pxa2x0_intr_bootstrap(G42XXEB_INTCTL_VBASE);
    453 
    454 	LEDSTEP();
    455 
    456 	/*
    457 	 * Heads up ... Setup the CPU / MMU / TLB functions
    458 	 */
    459 	if (set_cpufuncs())
    460 		panic("cpu not recognized!");
    461 
    462 	LEDSTEP();
    463 
    464 	/*
    465 	 * Okay, RedBoot has provided us with the following memory map:
    466 	 *
    467 	 * Physical Address Range     Description
    468 	 * -----------------------    ----------------------------------
    469 	 * 0x00000000 - 0x01ffffff    flash Memory   (32MB)
    470 	 * 0x04000000 - 0x05ffffff    Application flash Memory  (32MB)
    471 	 * 0x08000000 - 0x080000ff    I/O baseboard registers
    472 	 * 0x0c000000 - 0x0c0fffff    Ethernet Controller
    473 	 * 0x14000000 - 0x17ffffff    Expansion Card (64MB)
    474 	 * 0x40000000 - 0x480fffff    Processor Registers
    475 	 * 0xa0000000 - 0xa3ffffff    SDRAM Bank 0 (64MB)
    476 	 *
    477 	 *
    478 	 * Virtual Address Range    X C B  Description
    479 	 * -----------------------  - - -  ----------------------------------
    480 	 * 0x00000000 - 0x00003fff  N Y Y  SDRAM
    481 	 * 0x00004000 - 0x01ffffff  N Y N  ROM
    482 	 * 0x08000000 - 0x080fffff  N N N  I/O baseboard registers
    483 	 * 0x0a000000 - 0x0a0fffff  N N N  SRAM
    484 	 * 0x40000000 - 0x480fffff  N N N  Processor Registers
    485 	 * 0xa0000000 - 0xa000ffff  N Y N  RedBoot SDRAM
    486 	 * 0xa0017000 - 0xa3ffffff  Y Y Y  SDRAM
    487 	 * 0xc0000000 - 0xcfffffff  Y Y Y  Cache Flush Region
    488 	 * (done by this routine)
    489 	 * 0xfd000000 - 0xfd0000ff  N N N  I/O baseboard registers
    490 	 * 0xfd100000 - 0xfd3fffff  N N N  Processor Registers.
    491 	 * 0xfd400000 - 0xfd4fffff  N N N  FF-UART
    492 	 * 0xfd500000 - 0xfd5fffff  N N N  BT-UART
    493 	 *
    494 	 * RedBoot's first level page table is at 0xa0004000.  There
    495 	 * are also 2 second-level tables at 0xa0008000 and
    496 	 * 0xa0008400.  We will continue to use them until we switch to
    497 	 * our pagetable by cpu_setttb().
    498 	 */
    499 
    500 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    501 
    502 	LEDSTEP();
    503 
    504 	/* setup GPIO for BTUART, in case bootloader doesn't take care of it */
    505 	pxa2x0_gpio_bootstrap(G42XXEB_GPIO_VBASE);
    506 	pxa2x0_gpio_config(g42xxeb_gpioconf);
    507 
    508 	LEDSTEP();
    509 
    510 	consinit();
    511 #ifdef KGDB
    512 	LEDSTEP();
    513 	kgdb_port_init();
    514 #endif
    515 
    516 	LEDSTEP();
    517 
    518 	/* Talk to the user */
    519 	printf("\nNetBSD/evbarm (g42xxeb) booting ...\n");
    520 
    521 #if 0
    522 	/*
    523 	 * Examine the boot args string for options we need to know about
    524 	 * now.
    525 	 */
    526 	process_kernel_args((char *)nwbootinfo.bt_args);
    527 #endif
    528 
    529 	memstart = 0xa0000000;
    530 	memsize = 0x04000000;		/* 64MB */
    531 
    532 	printf("initarm: Configuring system ...\n");
    533 
    534 	/* Fake bootconfig structure for the benefit of pmap.c */
    535 	/* XXX must make the memory description h/w independent */
    536 	bootconfig.dramblocks = 1;
    537 	bootconfig.dram[0].address = memstart;
    538 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    539 
    540 	/*
    541 	 * Set up the variables that define the availablilty of
    542 	 * physical memory.  For now, we're going to set
    543 	 * physical_freestart to 0xa0200000 (where the kernel
    544 	 * was loaded), and allocate the memory we need downwards.
    545 	 * If we get too close to the L1 table that we set up, we
    546 	 * will panic.  We will update physical_freestart and
    547 	 * physical_freeend later to reflect what pmap_bootstrap()
    548 	 * wants to see.
    549 	 *
    550 	 * XXX pmap_bootstrap() needs an enema.
    551 	 */
    552 	physical_start = bootconfig.dram[0].address;
    553 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    554 
    555 	physical_freestart = 0xa0009000UL;
    556 	physical_freeend = 0xa0200000UL;
    557 
    558 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    559 
    560 #ifdef VERBOSE_INIT_ARM
    561 	/* Tell the user about the memory */
    562 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    563 	    physical_start, physical_end - 1);
    564 #endif
    565 
    566 	/*
    567 	 * Okay, the kernel starts 2MB in from the bottom of physical
    568 	 * memory.  We are going to allocate our bootstrap pages downwards
    569 	 * from there.
    570 	 *
    571 	 * We need to allocate some fixed page tables to get the kernel
    572 	 * going.  We allocate one page directory and a number of page
    573 	 * tables and store the physical addresses in the kernel_pt_table
    574 	 * array.
    575 	 *
    576 	 * The kernel page directory must be on a 16K boundary.  The page
    577 	 * tables must be on 4K bounaries.  What we do is allocate the
    578 	 * page directory on the first 16K boundary that we encounter, and
    579 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    580 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    581 	 * least one 16K aligned region.
    582 	 */
    583 
    584 #ifdef VERBOSE_INIT_ARM
    585 	printf("Allocating page tables\n");
    586 #endif
    587 
    588 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    589 
    590 #ifdef VERBOSE_INIT_ARM
    591 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    592 	       physical_freestart, free_pages, free_pages);
    593 #endif
    594 
    595 	/* Define a macro to simplify memory allocation */
    596 #define	valloc_pages(var, np)				\
    597 	alloc_pages((var).pv_pa, (np));			\
    598 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    599 
    600 #define alloc_pages(var, np)				\
    601 	physical_freeend -= ((np) * PAGE_SIZE);		\
    602 	if (physical_freeend < physical_freestart)	\
    603 		panic("initarm: out of memory");	\
    604 	(var) = physical_freeend;			\
    605 	free_pages -= (np);				\
    606 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    607 
    608 	loop1 = 0;
    609 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    610 		/* Are we 16KB aligned for an L1 ? */
    611 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    612 		    && kernel_l1pt.pv_pa == 0) {
    613 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    614 		} else {
    615 			valloc_pages(kernel_pt_table[loop1],
    616 			    L2_TABLE_SIZE / PAGE_SIZE);
    617 			++loop1;
    618 		}
    619 	}
    620 
    621 	/* This should never be able to happen but better confirm that. */
    622 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    623 		panic("initarm: Failed to align the kernel page directory");
    624 
    625 	LEDSTEP();
    626 
    627 	/*
    628 	 * Allocate a page for the system page mapped to V0x00000000
    629 	 * This page will just contain the system vectors and can be
    630 	 * shared by all processes.
    631 	 */
    632 	alloc_pages(systempage.pv_pa, 1);
    633 
    634 	/* Allocate stacks for all modes */
    635 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    636 	valloc_pages(abtstack, ABT_STACK_SIZE);
    637 	valloc_pages(undstack, UND_STACK_SIZE);
    638 	valloc_pages(kernelstack, UPAGES);
    639 
    640 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    641 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    642 	valloc_pages(minidataclean, 1);
    643 
    644 #ifdef VERBOSE_INIT_ARM
    645 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    646 	    irqstack.pv_va);
    647 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    648 	    abtstack.pv_va);
    649 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    650 	    undstack.pv_va);
    651 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    652 	    kernelstack.pv_va);
    653 #endif
    654 
    655 	/*
    656 	 * XXX Defer this to later so that we can reclaim the memory
    657 	 * XXX used by the RedBoot page tables.
    658 	 */
    659 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    660 
    661 	/*
    662 	 * Ok we have allocated physical pages for the primary kernel
    663 	 * page tables
    664 	 */
    665 
    666 #ifdef VERBOSE_INIT_ARM
    667 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    668 #endif
    669 
    670 	/*
    671 	 * Now we start construction of the L1 page table
    672 	 * We start by mapping the L2 page tables into the L1.
    673 	 * This means that we can replace L1 mappings later on if necessary
    674 	 */
    675 	l1pagetable = kernel_l1pt.pv_pa;
    676 
    677 	/* Map the L2 pages tables in the L1 page table */
    678 	pmap_link_l2pt(l1pagetable, 0x00000000,
    679 	    &kernel_pt_table[KERNEL_PT_SYS]);
    680 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    681 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    682 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    683 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    684 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    685 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    686 
    687 	/* update the top of the kernel VM */
    688 	pmap_curmaxkvaddr =
    689 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    690 
    691 #ifdef VERBOSE_INIT_ARM
    692 	printf("Mapping kernel\n");
    693 #endif
    694 
    695 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    696 	{
    697 		extern char etext[], _end[];
    698 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    699 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    700 		u_int logical;
    701 
    702 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    703 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    704 
    705 		logical = 0x00200000;	/* offset of kernel in RAM */
    706 
    707 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    708 		    physical_start + logical, textsize,
    709 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    710 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    711 		    physical_start + logical, totalsize - textsize,
    712 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    713 	}
    714 
    715 #ifdef VERBOSE_INIT_ARM
    716 	printf("Constructing L2 page tables\n");
    717 #endif
    718 
    719 	/* Map the stack pages */
    720 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    721 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    722 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    723 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    724 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    725 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    726 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    727 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    728 
    729 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    730 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    731 
    732 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    733 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    734 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    735 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    736 	}
    737 
    738 	/* Map the Mini-Data cache clean area. */
    739 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    740 	    minidataclean.pv_pa);
    741 
    742 	/* Map the vector page. */
    743 #if 1
    744 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
    745 	 * cache-clean code there.  */
    746 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    747 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    748 #else
    749 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    750 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    751 #endif
    752 
    753 	/*
    754 	 * map integrated peripherals at same address in l1pagetable
    755 	 * so that we can continue to use console.
    756 	 */
    757 	pmap_devmap_bootstrap(l1pagetable, g42xxeb_devmap);
    758 
    759 	/*
    760 	 * Give the XScale global cache clean code an appropriately
    761 	 * sized chunk of unmapped VA space starting at 0xff000000
    762 	 * (our device mappings end before this address).
    763 	 */
    764 	xscale_cache_clean_addr = 0xff000000U;
    765 
    766 	/*
    767 	 * Now we have the real page tables in place so we can switch to them.
    768 	 * Once this is done we will be running with the REAL kernel page
    769 	 * tables.
    770 	 */
    771 
    772 	/*
    773 	 * Update the physical_freestart/physical_freeend/free_pages
    774 	 * variables.
    775 	 */
    776 	{
    777 		extern char _end[];
    778 
    779 		physical_freestart = physical_start +
    780 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    781 		     KERNEL_BASE);
    782 		physical_freeend = physical_end;
    783 		free_pages =
    784 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    785 	}
    786 
    787 	/* Switch tables */
    788 #ifdef VERBOSE_INIT_ARM
    789 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    790 	       physical_freestart, free_pages, free_pages);
    791 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    792 #endif
    793 	LEDSTEP();
    794 
    795 	cpu_setttb(kernel_l1pt.pv_pa);
    796 	cpu_tlb_flushID();
    797 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    798 	LEDSTEP();
    799 
    800 	/*
    801 	 * Moved from cpu_startup() as data_abort_handler() references
    802 	 * this during uvm init
    803 	 */
    804 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    805 
    806 #ifdef VERBOSE_INIT_ARM
    807 	printf("bootstrap done.\n");
    808 #endif
    809 
    810 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    811 
    812 	/*
    813 	 * Pages were allocated during the secondary bootstrap for the
    814 	 * stacks for different CPU modes.
    815 	 * We must now set the r13 registers in the different CPU modes to
    816 	 * point to these stacks.
    817 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    818 	 * of the stack memory.
    819 	 */
    820 #ifdef	VERBOSE_INIT_ARM
    821 	printf("init subsystems: stacks ");
    822 #endif
    823 
    824 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    825 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    826 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    827 
    828 	/*
    829 	 * Well we should set a data abort handler.
    830 	 * Once things get going this will change as we will need a proper
    831 	 * handler.
    832 	 * Until then we will use a handler that just panics but tells us
    833 	 * why.
    834 	 * Initialisation of the vectors will just panic on a data abort.
    835 	 * This just fills in a slighly better one.
    836 	 */
    837 #ifdef	VERBOSE_INIT_ARM
    838 	printf("vectors ");
    839 #endif
    840 	data_abort_handler_address = (u_int)data_abort_handler;
    841 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    842 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    843 
    844 	/* Initialise the undefined instruction handlers */
    845 #ifdef	VERBOSE_INIT_ARM
    846 	printf("undefined ");
    847 #endif
    848 	undefined_init();
    849 
    850 	/* Load memory into UVM. */
    851 #ifdef	VERBOSE_INIT_ARM
    852 	printf("page ");
    853 #endif
    854 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    855 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    856 	    atop(physical_freestart), atop(physical_freeend),
    857 	    VM_FREELIST_DEFAULT);
    858 
    859 	/* Boot strap pmap telling it where the kernel page table is */
    860 #ifdef	VERBOSE_INIT_ARM
    861 	printf("pmap ");
    862 #endif
    863 	LEDSTEP();
    864 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    865 	LEDSTEP();
    866 
    867 #ifdef __HAVE_MEMORY_DISK__
    868 	md_root_setconf(memory_disk, sizeof memory_disk);
    869 #endif
    870 
    871 #ifdef BOOTHOWTO
    872 	boothowto |= BOOTHOWTO;
    873 #endif
    874 
    875 	{
    876 		uint8_t sw = pldreg8_read(G42XXEB_DIPSW);
    877 
    878 		if (0 == (sw & (1<<0)))
    879 			boothowto ^= RB_KDB;
    880 		if (0 == (sw & (1<<1)))
    881 			boothowto ^= RB_SINGLE;
    882 	}
    883 
    884 	LEDSTEP();
    885 
    886 #ifdef KGDB
    887 	if (boothowto & RB_KDB) {
    888 		kgdb_debug_init = 1;
    889 		kgdb_connect(1);
    890 	}
    891 #endif
    892 
    893 #ifdef DDB
    894 	db_machine_init();
    895 
    896 	/* Firmware doesn't load symbols. */
    897 	ddb_init(0, NULL, NULL);
    898 
    899 	if (boothowto & RB_KDB)
    900 		Debugger();
    901 #endif
    902 
    903 	pldreg8_write(G42XXEB_LED, 0);
    904 
    905 	/* We return the new stack pointer address */
    906 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    907 }
    908 
    909 #if 0
    910 void
    911 process_kernel_args(char *args)
    912 {
    913 
    914 	boothowto = 0;
    915 
    916 	/* Make a local copy of the bootargs */
    917 	strncpy(bootargs, args, MAX_BOOT_STRING);
    918 
    919 	args = bootargs;
    920 	boot_file = bootargs;
    921 
    922 	/* Skip the kernel image filename */
    923 	while (*args != ' ' && *args != 0)
    924 		++args;
    925 
    926 	if (*args != 0)
    927 		*args++ = 0;
    928 
    929 	while (*args == ' ')
    930 		++args;
    931 
    932 	boot_args = args;
    933 
    934 	printf("bootfile: %s\n", boot_file);
    935 	printf("bootargs: %s\n", boot_args);
    936 
    937 	parse_mi_bootargs(boot_args);
    938 }
    939 #endif
    940 
    941 #ifdef KGDB
    942 #ifndef KGDB_DEVNAME
    943 #define KGDB_DEVNAME "ffuart"
    944 #endif
    945 const char kgdb_devname[] = KGDB_DEVNAME;
    946 
    947 #if (NCOM > 0)
    948 #ifndef KGDB_DEVMODE
    949 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    950 #endif
    951 int comkgdbmode = KGDB_DEVMODE;
    952 #endif /* NCOM */
    953 
    954 #endif /* KGDB */
    955 
    956 
    957 void
    958 consinit(void)
    959 {
    960 	static int consinit_called = 0;
    961 	uint32_t ckenreg = ioreg_read(G42XXEB_CLKMAN_VBASE+CLKMAN_CKEN);
    962 #if 0
    963 	char *console = CONSDEVNAME;
    964 #endif
    965 
    966 	if (consinit_called != 0)
    967 		return;
    968 
    969 	consinit_called = 1;
    970 
    971 #if NCOM > 0
    972 
    973 #ifdef FFUARTCONSOLE
    974 #ifdef KGDB
    975 	if (0 == strcmp(kgdb_devname, "ffuart")){
    976 		/* port is reserved for kgdb */
    977 	} else
    978 #endif
    979 	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
    980 		comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
    981 #if 0
    982 		pxa2x0_clkman_config(CKEN_FFUART, 1);
    983 #else
    984 		ioreg_write(G42XXEB_CLKMAN_VBASE+CLKMAN_CKEN,
    985 		    ckenreg|CKEN_FFUART);
    986 #endif
    987 
    988 		return;
    989 	}
    990 #endif /* FFUARTCONSOLE */
    991 
    992 #ifdef BTUARTCONSOLE
    993 #ifdef KGDB
    994 	if (0 == strcmp(kgdb_devname, "btuart")) {
    995 		/* port is reserved for kgdb */
    996 	} else
    997 #endif
    998 	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
    999 		comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
   1000 		ioreg_write(G42XXEB_CLKMAN_VBASE+CLKMAN_CKEN,
   1001 		    ckenreg|CKEN_BTUART);
   1002 		return;
   1003 	}
   1004 #endif /* BTUARTCONSOLE */
   1005 
   1006 
   1007 #endif /* NCOM */
   1008 
   1009 }
   1010 
   1011 #ifdef KGDB
   1012 void
   1013 kgdb_port_init(void)
   1014 {
   1015 #if (NCOM > 0) && defined(COM_PXA2X0)
   1016 	paddr_t paddr = 0;
   1017 	uint32_t ckenreg = ioreg_read(G42XXEB_CLKMAN_VBASE+CLKMAN_CKEN);
   1018 
   1019 	if (0 == strcmp(kgdb_devname, "ffuart")) {
   1020 		paddr = PXA2X0_FFUART_BASE;
   1021 		ckenreg |= CKEN_FFUART;
   1022 	}
   1023 	else if (0 == strcmp(kgdb_devname, "btuart")) {
   1024 		paddr = PXA2X0_BTUART_BASE;
   1025 		ckenreg |= CKEN_BTUART;
   1026 	}
   1027 
   1028 	if (paddr &&
   1029 	    0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
   1030 		kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
   1031 
   1032 		ioreg_write(G42XXEB_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
   1033 
   1034 	}
   1035 
   1036 #endif
   1037 }
   1038 #endif
   1039 
   1040