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