Home | History | Annotate | Line # | Download | only in gumstix
gumstix_machdep.c revision 1.39
      1 /*	$NetBSD: gumstix_machdep.c,v 1.39 2012/07/29 00:07:08 matt Exp $ */
      2 /*
      3  * Copyright (C) 2005, 2006, 2007  WIDE Project and SOUM Corporation.
      4  * All rights reserved.
      5  *
      6  * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
      7  * 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. Neither the name of the project nor the name of SOUM Corporation
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT and SOUM CORPORATION ``AS IS''
     22  * AND 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 THE PROJECT AND SOUM 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 /*
     34  * Copyright (c) 2002, 2003, 2004, 2005  Genetec Corporation.
     35  * All rights reserved.
     36  *
     37  * Written by Hiroyuki Bessho for Genetec Corporation.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. The name of Genetec Corporation may not be used to endorse or
     48  *    promote products derived from this software without specific prior
     49  *    written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61  * POSSIBILITY OF SUCH DAMAGE.
     62  *
     63  * Machine dependent functions for kernel setup for Genetec G4250EBX
     64  * evaluation board.
     65  *
     66  * Based on iq80310_machhdep.c
     67  */
     68 /*
     69  * Copyright (c) 2001 Wasabi Systems, Inc.
     70  * All rights reserved.
     71  *
     72  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     73  *
     74  * Redistribution and use in source and binary forms, with or without
     75  * modification, are permitted provided that the following conditions
     76  * are met:
     77  * 1. Redistributions of source code must retain the above copyright
     78  *    notice, this list of conditions and the following disclaimer.
     79  * 2. Redistributions in binary form must reproduce the above copyright
     80  *    notice, this list of conditions and the following disclaimer in the
     81  *    documentation and/or other materials provided with the distribution.
     82  * 3. All advertising materials mentioning features or use of this software
     83  *    must display the following acknowledgement:
     84  *	This product includes software developed for the NetBSD Project by
     85  *	Wasabi Systems, Inc.
     86  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     87  *    or promote products derived from this software without specific prior
     88  *    written permission.
     89  *
     90  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     91  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     92  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     93  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     94  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     95  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     96  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     97  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     98  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     99  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    100  * POSSIBILITY OF SUCH DAMAGE.
    101  */
    102 
    103 /*
    104  * Copyright (c) 1997,1998 Mark Brinicombe.
    105  * Copyright (c) 1997,1998 Causality Limited.
    106  * All rights reserved.
    107  *
    108  * Redistribution and use in source and binary forms, with or without
    109  * modification, are permitted provided that the following conditions
    110  * are met:
    111  * 1. Redistributions of source code must retain the above copyright
    112  *    notice, this list of conditions and the following disclaimer.
    113  * 2. Redistributions in binary form must reproduce the above copyright
    114  *    notice, this list of conditions and the following disclaimer in the
    115  *    documentation and/or other materials provided with the distribution.
    116  * 3. All advertising materials mentioning features or use of this software
    117  *    must display the following acknowledgement:
    118  *	This product includes software developed by Mark Brinicombe
    119  *	for the NetBSD Project.
    120  * 4. The name of the company nor the name of the author may be used to
    121  *    endorse or promote products derived from this software without specific
    122  *    prior written permission.
    123  *
    124  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
    125  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    126  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    127  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    128  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    129  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    130  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    131  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    132  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    133  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    134  * SUCH DAMAGE.
    135  *
    136  * Machine dependent functions for kernel setup for Intel IQ80310 evaluation
    137  * boards using RedBoot firmware.
    138  */
    139 
    140 #include "opt_evbarm_boardtype.h"
    141 #include "opt_cputypes.h"
    142 #include "opt_gumstix.h"
    143 #ifdef OVERO
    144 #include "opt_omap.h"
    145 #include "prcm.h"
    146 #endif
    147 #include "opt_ddb.h"
    148 #include "opt_kgdb.h"
    149 #include "opt_pmap_debug.h"
    150 #include "opt_md.h"
    151 #include "opt_modular.h"
    152 #include "opt_com.h"
    153 
    154 #include <sys/param.h>
    155 #include <sys/conf.h>
    156 #include <sys/device.h>
    157 #include <sys/exec.h>
    158 #include <sys/kernel.h>
    159 #include <sys/ksyms.h>
    160 #include <sys/msgbuf.h>
    161 #include <sys/proc.h>
    162 #include <sys/reboot.h>
    163 #include <sys/systm.h>
    164 #include <sys/termios.h>
    165 
    166 #include <machine/autoconf.h>
    167 #include <machine/bootconfig.h>
    168 #include <sys/bus.h>
    169 #include <machine/cpu.h>
    170 #include <machine/db_machdep.h>
    171 #include <machine/frame.h>
    172 
    173 #include <arm/arm32/machdep.h>
    174 #include <arm/omap/omap2_gpmcreg.h>
    175 #include <arm/omap/omap2_prcm.h>
    176 #include <arm/omap/omap2_reg.h>
    177 #include <arm/omap/omap_var.h>
    178 #include <arm/omap/omap_com.h>
    179 #include <arm/undefined.h>
    180 #include <arm/xscale/pxa2x0reg.h>
    181 #include <arm/xscale/pxa2x0var.h>
    182 #include <arm/xscale/pxa2x0_gpio.h>
    183 #include <evbarm/gumstix/gumstixreg.h>
    184 #include <evbarm/gumstix/gumstixvar.h>
    185 
    186 #include <uvm/uvm_extern.h>
    187 
    188 #include <dev/cons.h>
    189 #include <dev/md.h>
    190 
    191 #include <ddb/db_sym.h>
    192 #include <ddb/db_extern.h>
    193 #ifdef KGDB
    194 #include <sys/kgdb.h>
    195 #endif
    196 
    197 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    198 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    199 #ifndef KERNEL_VM_BASE
    200 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    201 #endif
    202 
    203 /*
    204  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    205  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    206  */
    207 #define KERNEL_VM_SIZE		0x0C000000
    208 
    209 
    210 /*
    211  * Address to call from cpu_reset() to reset the machine.
    212  * This is machine architecture dependent as it varies depending
    213  * on where the ROM appears when you turn the MMU off.
    214  */
    215 
    216 u_int cpu_reset_address = 0;
    217 
    218 /* Define various stack sizes in pages */
    219 #define IRQ_STACK_SIZE	1
    220 #define ABT_STACK_SIZE	1
    221 #define UND_STACK_SIZE	1
    222 
    223 BootConfig bootconfig;		/* Boot config storage */
    224 static char bootargs[MAX_BOOT_STRING];
    225 const size_t bootargs_len = sizeof(bootargs) - 1;	/* without nul */
    226 char *boot_args = NULL;
    227 
    228 uint32_t system_serial_high;
    229 uint32_t system_serial_low;
    230 
    231 vm_offset_t physical_start;
    232 vm_offset_t physical_freestart;
    233 vm_offset_t physical_freeend;
    234 vm_offset_t physical_end;
    235 u_int free_pages;
    236 
    237 /*int debug_flags;*/
    238 #ifndef PMAP_STATIC_L1S
    239 int max_processes = 64;			/* Default number */
    240 #endif	/* !PMAP_STATIC_L1S */
    241 
    242 vm_offset_t msgbufphys;
    243 
    244 #ifdef PMAP_DEBUG
    245 extern int pmap_debug_level;
    246 #endif
    247 
    248 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    249 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    250 #define	KERNEL_PT_KERNEL_NUM	((KERNEL_VM_BASE - KERNEL_BASE) >> 22)
    251 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
    252 				        /* Page tables for mapping kernel VM */
    253 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    254 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    255 
    256 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    257 
    258 /* Prototypes */
    259 #if defined(GUMSTIX)
    260 static void	read_system_serial(void);
    261 #endif
    262 static void	process_kernel_args(int, char *[]);
    263 static void	process_kernel_args_liner(char *);
    264 #ifdef KGDB
    265 static void	kgdb_port_init(void);
    266 #endif
    267 static void	gumstix_device_register(device_t, 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 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    278 #include "lcd.h"
    279 #endif
    280 
    281 #ifndef CONSPEED
    282 #define CONSPEED B115200	/* It's a setting of the default of u-boot */
    283 #endif
    284 #ifndef CONMODE
    285 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    286 #endif
    287 
    288 int comcnspeed = CONSPEED;
    289 int comcnmode = CONMODE;
    290 
    291 #ifdef GUMSTIX_NETBSD_ARGS_CONSOLE
    292 static char console[16];
    293 #endif
    294 
    295 extern void gxio_config_pin(void);
    296 extern void gxio_config_expansion(char *);
    297 
    298 /*
    299  * void cpu_reboot(int howto, char *bootstr)
    300  *
    301  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    302  * then reset the CPU.
    303  */
    304 void
    305 cpu_reboot(int howto, char *bootstr)
    306 {
    307 
    308 #ifdef DIAGNOSTIC
    309 	/* info */
    310 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    311 #endif
    312 
    313 	/*
    314 	 * If we are still cold then hit the air brakes
    315 	 * and crash to earth fast
    316 	 */
    317 	if (cold) {
    318 		doshutdownhooks();
    319 		pmf_system_shutdown(boothowto);
    320 		printf("The operating system has halted.\n");
    321 		printf("Please press any key to reboot.\n\n");
    322 		cngetc();
    323 		printf("rebooting...\n");
    324 #if defined(OMAP_3530) && NPRCM > 0
    325 		prcm_cold_reset();
    326 #endif
    327 		cpu_reset();
    328 		/*NOTREACHED*/
    329 	}
    330 
    331 	/*
    332 	 * If RB_NOSYNC was not specified sync the discs.
    333 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    334 	 * unmount.  It looks like syslogd is getting woken up only to find
    335 	 * that it cannot page part of the binary in as the filesystem has
    336 	 * been unmounted.
    337 	 */
    338 	if (!(howto & RB_NOSYNC))
    339 		bootsync();
    340 
    341 	/* Say NO to interrupts */
    342 	splhigh();
    343 
    344 	/* Do a dump if requested. */
    345 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    346 		dumpsys();
    347 
    348 	/* Run any shutdown hooks */
    349 	doshutdownhooks();
    350 
    351 	pmf_system_shutdown(boothowto);
    352 
    353 	/* Make sure IRQ's are disabled */
    354 	IRQdisable;
    355 
    356 	if (howto & RB_HALT) {
    357 		printf("The operating system has halted.\n");
    358 		printf("Please press any key to reboot.\n\n");
    359 		cngetc();
    360 	}
    361 
    362 	printf("rebooting...\n");
    363 #if defined(OMAP_3530) && NPRCM > 0
    364 	prcm_cold_reset();
    365 #endif
    366 	cpu_reset();
    367 	/*NOTREACHED*/
    368 }
    369 
    370 static inline pd_entry_t *
    371 read_ttb(void)
    372 {
    373 	long ttb;
    374 
    375 	__asm volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r" (ttb));
    376 
    377 	return (pd_entry_t *)(ttb & ~((1<<14)-1));
    378 }
    379 
    380 /*
    381  * Static device mappings. These peripheral registers are mapped at
    382  * fixed virtual addresses very early in initarm() so that we can use
    383  * them while booting the kernel, and stay at the same address
    384  * throughout whole kernel's life time.
    385  *
    386  * We use this table twice; once with bootstrap page table, and once
    387  * with kernel's page table which we build up in initarm().
    388  *
    389  * Since we map these registers into the bootstrap page table using
    390  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    391  * registers segment-aligned and segment-rounded in order to avoid
    392  * using the 2nd page tables.
    393  */
    394 
    395 #define	_A(a)	((a) & ~L1_S_OFFSET)
    396 #define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    397 
    398 static const struct pmap_devmap gumstix_devmap[] = {
    399 #if defined(GUMSTIX)
    400 	{
    401 		GUMSTIX_GPIO_VBASE,
    402 		_A(PXA2X0_GPIO_BASE),
    403 		_S(PXA250_GPIO_SIZE),
    404 		VM_PROT_READ | VM_PROT_WRITE,
    405 		PTE_NOCACHE,
    406 	},
    407 	{
    408 		GUMSTIX_CLKMAN_VBASE,
    409 		_A(PXA2X0_CLKMAN_BASE),
    410 		_S(PXA2X0_CLKMAN_SIZE),
    411 		VM_PROT_READ | VM_PROT_WRITE,
    412 		PTE_NOCACHE,
    413 	},
    414 	{
    415 		GUMSTIX_INTCTL_VBASE,
    416 		_A(PXA2X0_INTCTL_BASE),
    417 		_S(PXA2X0_INTCTL_SIZE),
    418 		VM_PROT_READ | VM_PROT_WRITE,
    419 		PTE_NOCACHE,
    420 	},
    421 	{
    422 		GUMSTIX_FFUART_VBASE,
    423 		_A(PXA2X0_FFUART_BASE),
    424 		_S(4 * COM_NPORTS),
    425 		VM_PROT_READ | VM_PROT_WRITE,
    426 		PTE_NOCACHE,
    427 	},
    428 	{
    429 		GUMSTIX_STUART_VBASE,
    430 		_A(PXA2X0_STUART_BASE),
    431 		_S(4 * COM_NPORTS),
    432 		VM_PROT_READ | VM_PROT_WRITE,
    433 		PTE_NOCACHE,
    434 	},
    435 	{
    436 		GUMSTIX_BTUART_VBASE,
    437 		_A(PXA2X0_BTUART_BASE),
    438 		_S(4 * COM_NPORTS),
    439 		VM_PROT_READ | VM_PROT_WRITE,
    440 		PTE_NOCACHE,
    441 	},
    442 	{
    443 		GUMSTIX_HWUART_VBASE,
    444 		_A(PXA2X0_HWUART_BASE),
    445 		_S(4 * COM_NPORTS),
    446 		VM_PROT_READ | VM_PROT_WRITE,
    447 		PTE_NOCACHE,
    448 	},
    449 	{
    450 		GUMSTIX_LCDC_VBASE,
    451 		_A(PXA2X0_LCDC_BASE),
    452 		_S(4 * COM_NPORTS),
    453 		VM_PROT_READ | VM_PROT_WRITE,
    454 		PTE_NOCACHE,
    455 	},
    456 #elif defined(OVERO)
    457 	{
    458 		OVERO_L4_PERIPHERAL_VBASE,
    459 		_A(OMAP3530_L4_PERIPHERAL_BASE),
    460 		_S(OMAP3530_L4_PERIPHERAL_SIZE),
    461 		VM_PROT_READ | VM_PROT_WRITE,
    462 		PTE_NOCACHE
    463 	},
    464 	{
    465 		OVERO_GPMC_VBASE,
    466 		_A(GPMC_BASE),
    467 		_S(GPMC_SIZE),
    468 		VM_PROT_READ | VM_PROT_WRITE,
    469 		PTE_NOCACHE
    470 	},
    471 #endif
    472 	{ 0, 0, 0, 0, 0 }
    473 };
    474 
    475 #undef	_A
    476 #undef	_S
    477 
    478 
    479 /*
    480  * u_int initarm(...)
    481  *
    482  * Initial entry point on startup. This gets called before main() is
    483  * entered.
    484  * It should be responsible for setting up everything that must be
    485  * in place when main is called.
    486  * This includes
    487  *   Taking a copy of the boot configuration structure.
    488  *   Initialising the physical console so characters can be printed.
    489  *   Setting up page tables for the kernel
    490  *   Relocating the kernel to the bottom of physical memory
    491  */
    492 u_int
    493 initarm(void *arg)
    494 {
    495 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    496 #ifdef DIAGNOSTIC
    497 	extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
    498 #endif
    499 	extern vaddr_t xscale_cache_clean_addr;
    500 #endif
    501 	extern uint32_t *u_boot_args[];
    502 	extern uint32_t ram_size;
    503 	enum { r0 = 0, r1 = 1, r2 = 2, r3 = 3 }; /* args from u-boot */
    504 	int loop;
    505 	int loop1;
    506 	u_int l1pagetable;
    507 	paddr_t memstart;
    508 	psize_t memsize;
    509 
    510 	/*
    511 	 * U-Boot doesn't use the virtual memory.
    512 	 *
    513 	 * Gumstix (basix, connex, verdex, verdex-pro):
    514 	 * Physical Address Range     Description
    515 	 * -----------------------    ----------------------------------
    516 	 * 0x00000000 - 0x00ffffff    flash Memory   (16MB or 4MB)
    517 	 * 0x40000000 - 0x480fffff    Processor Registers
    518 	 * 0xa0000000 - 0xa3ffffff    SDRAM Bank 0 (64MB or 128MB)
    519 	 *
    520 	 * Overo:
    521 	 * Physical Address Range     Description
    522 	 * -----------------------    ----------------------------------
    523 	 */
    524 
    525 	/*
    526 	 * Heads up ... Setup the CPU / MMU / TLB functions
    527 	 */
    528 	if (set_cpufuncs())
    529 		panic("cpu not recognized!");
    530 
    531 	/* map some peripheral registers at static I/O area */
    532 	pmap_devmap_bootstrap((vaddr_t)read_ttb(), gumstix_devmap);
    533 
    534 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    535 	/* start 32.768kHz OSC */
    536 	ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_OSCC, OSCC_OON);
    537 
    538 	/* Get ready for splfoo() */
    539 	pxa2x0_intr_bootstrap(GUMSTIX_INTCTL_VBASE);
    540 
    541 	/* setup GPIO for {FF,ST,HW}UART. */
    542 	pxa2x0_gpio_bootstrap(GUMSTIX_GPIO_VBASE);
    543 
    544 	pxa2x0_clkman_bootstrap(GUMSTIX_CLKMAN_VBASE);
    545 #elif defined(CPU_CORTEXA8)
    546 	{
    547 		void cortexa8_pmc_ccnt_init(void);
    548 
    549 		cortexa8_pmc_ccnt_init();
    550 	}
    551 #endif
    552 
    553 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    554 
    555 	/* configure GPIOs. */
    556 	gxio_config_pin();
    557 
    558 
    559 #ifndef GUMSTIX_NETBSD_ARGS_CONSOLE
    560 	consinit();
    561 #endif
    562 #ifdef KGDB
    563 	kgdb_port_init();
    564 #endif
    565 
    566         /*
    567 	 * Examine the boot args string for options we need to know about
    568 	 * now.
    569 	 */
    570 #if defined(GUMSTIX)
    571 #define SDRAM_START	0xa0000000UL
    572 #elif defined(OVERO)
    573 #define SDRAM_START	0x80000000UL
    574 #endif
    575 	if (((uint32_t)u_boot_args[r0] & 0xf0000000) != SDRAM_START)
    576 		/* Maybe r0 is 'argc'.  We are booted by command 'go'. */
    577 		process_kernel_args((int)u_boot_args[r0],
    578 		    (char **)u_boot_args[r1]);
    579 	else
    580 		/*
    581 		 * Maybe r3 is 'boot args string' of 'bootm'.  This string is
    582 		 * linely.
    583 		 */
    584 		process_kernel_args_liner((char *)u_boot_args[r3]);
    585 #ifdef GUMSTIX_NETBSD_ARGS_CONSOLE
    586 	consinit();
    587 #endif
    588 
    589 	/* Talk to the user */
    590 #define BDSTR(s)	_BDSTR(s)
    591 #define _BDSTR(s)	#s
    592 	printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
    593 
    594 	/* Read system serial */
    595 #if defined(GUMSTIX)
    596 	read_system_serial();
    597 #endif
    598 
    599 	memstart = SDRAM_START;
    600 	memsize = ram_size;
    601 
    602 #ifdef VERBOSE_INIT_ARM
    603 	printf("initarm: Configuring system ...\n");
    604 #endif
    605 
    606 	/* Fake bootconfig structure for the benefit of pmap.c */
    607 	/* XXX must make the memory description h/w independent */
    608 	bootconfig.dramblocks = 1;
    609 	bootconfig.dram[0].address = memstart;
    610 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    611 
    612 	/*
    613 	 * Set up the variables that define the availablilty of
    614 	 * physical memory.  For now, we're going to set
    615 	 * physical_freestart to 0xa0200000 (where the kernel
    616 	 * was loaded), and allocate the memory we need downwards.
    617 	 * If we get too close to the L1 table that we set up, we
    618 	 * will panic.  We will update physical_freestart and
    619 	 * physical_freeend later to reflect what pmap_bootstrap()
    620 	 * wants to see.
    621 	 *
    622 	 * XXX pmap_bootstrap() needs an enema.
    623 	 */
    624 	physical_start = bootconfig.dram[0].address;
    625 	physical_end = physical_start + memsize;
    626 
    627 #if defined(GUMSTIX)
    628 	physical_freestart = 0xa0009000UL;
    629 	physical_freeend = 0xa0200000UL;
    630 #elif defined(OVERO)
    631 	physical_freestart = 0x80009000UL;
    632 	physical_freeend = 0x80200000UL;
    633 #endif
    634 
    635 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    636 
    637 #ifdef VERBOSE_INIT_ARM
    638 	/* Tell the user about the memory */
    639 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    640 	    physical_start, physical_end - 1);
    641 #endif
    642 
    643 	/*
    644 	 * Okay, the kernel starts 2MB in from the bottom of physical
    645 	 * memory.  We are going to allocate our bootstrap pages downwards
    646 	 * from there.
    647 	 *
    648 	 * We need to allocate some fixed page tables to get the kernel
    649 	 * going.  We allocate one page directory and a number of page
    650 	 * tables and store the physical addresses in the kernel_pt_table
    651 	 * array.
    652 	 *
    653 	 * The kernel page directory must be on a 16K boundary.  The page
    654 	 * tables must be on 4K bounaries.  What we do is allocate the
    655 	 * page directory on the first 16K boundary that we encounter, and
    656 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    657 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    658 	 * least one 16K aligned region.
    659 	 */
    660 
    661 #ifdef VERBOSE_INIT_ARM
    662 	printf("Allocating page tables\n");
    663 #endif
    664 
    665 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    666 
    667 #ifdef VERBOSE_INIT_ARM
    668 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    669 	       physical_freestart, free_pages, free_pages);
    670 #endif
    671 
    672 	/* Define a macro to simplify memory allocation */
    673 #define	valloc_pages(var, np)				\
    674 	alloc_pages((var).pv_pa, (np));			\
    675 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    676 
    677 #define alloc_pages(var, np)				\
    678 	physical_freeend -= ((np) * PAGE_SIZE);		\
    679 	if (physical_freeend < physical_freestart)	\
    680 		panic("initarm: out of memory");	\
    681 	(var) = physical_freeend;			\
    682 	free_pages -= (np);				\
    683 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    684 
    685 	loop1 = 0;
    686 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    687 		/* Are we 16KB aligned for an L1 ? */
    688 		if ((physical_freeend & (L1_TABLE_SIZE - 1)) == 0 &&
    689 		    kernel_l1pt.pv_pa == 0) {
    690 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    691 		} else {
    692 			valloc_pages(kernel_pt_table[loop1],
    693 			    L2_TABLE_SIZE / PAGE_SIZE);
    694 			++loop1;
    695 		}
    696 	}
    697 
    698 	/* This should never be able to happen but better confirm that. */
    699 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    700 		panic("initarm: Failed to align the kernel page directory");
    701 
    702 	/*
    703 	 * Allocate a page for the system page mapped to V0x00000000
    704 	 * This page will just contain the system vectors and can be
    705 	 * shared by all processes.
    706 	 */
    707 	alloc_pages(systempage.pv_pa, 1);
    708 #if defined(CPU_CORTEXA8)
    709 	systempage.pv_va = ARM_VECTORS_HIGH;
    710 #endif
    711 
    712 	/* Allocate stacks for all modes */
    713 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    714 	valloc_pages(abtstack, ABT_STACK_SIZE);
    715 	valloc_pages(undstack, UND_STACK_SIZE);
    716 	valloc_pages(kernelstack, UPAGES);
    717 
    718 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    719 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    720 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    721 #endif
    722 	valloc_pages(minidataclean, 1);
    723 
    724 #ifdef VERBOSE_INIT_ARM
    725 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    726 	    irqstack.pv_va);
    727 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    728 	    abtstack.pv_va);
    729 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    730 	    undstack.pv_va);
    731 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    732 	    kernelstack.pv_va);
    733 #endif
    734 
    735 	/*
    736 	 * XXX Defer this to later so that we can reclaim the memory
    737 	 * XXX used by the RedBoot page tables.
    738 	 */
    739 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    740 
    741 	/*
    742 	 * Ok we have allocated physical pages for the primary kernel
    743 	 * page tables
    744 	 */
    745 
    746 #ifdef VERBOSE_INIT_ARM
    747 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    748 #endif
    749 
    750 	/*
    751 	 * Now we start construction of the L1 page table
    752 	 * We start by mapping the L2 page tables into the L1.
    753 	 * This means that we can replace L1 mappings later on if necessary
    754 	 */
    755 	l1pagetable = kernel_l1pt.pv_va;
    756 
    757 	/* Map the L2 pages tables in the L1 page table */
    758 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    759 	pmap_link_l2pt(l1pagetable, 0x00000000,
    760 	    &kernel_pt_table[KERNEL_PT_SYS]);
    761 #elif defined(CPU_CORTEXA8)
    762 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    763 	    &kernel_pt_table[KERNEL_PT_SYS]);
    764 #endif
    765 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    766 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    767 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    768 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    769 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    770 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    771 
    772 	/* update the top of the kernel VM */
    773 	pmap_curmaxkvaddr =
    774 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    775 
    776 #ifdef VERBOSE_INIT_ARM
    777 	printf("Mapping kernel\n");
    778 #endif
    779 
    780 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    781 	{
    782 		extern char etext[], _end[];
    783 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    784 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    785 		u_int logical;
    786 
    787 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    788 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    789 
    790 		logical = 0x00200000;	/* offset of kernel in RAM */
    791 
    792 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    793 		    physical_start + logical, textsize,
    794 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    795 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    796 		    physical_start + logical, totalsize - textsize,
    797 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    798 	}
    799 
    800 #ifdef VERBOSE_INIT_ARM
    801 	printf("Constructing L2 page tables\n");
    802 #endif
    803 
    804 	/* Map the stack pages */
    805 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    806 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    807 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    808 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    809 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    810 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    811 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    812 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    813 
    814 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    815 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    816 
    817 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    818 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    819 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    820 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    821 	}
    822 
    823 	/* Map the Mini-Data cache clean area. */
    824 #if defined(GUMSTIX)
    825 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    826 	    minidataclean.pv_pa);
    827 #endif
    828 
    829 	/* Map the vector page. */
    830 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    831 #if 1
    832 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
    833 	 * cache-clean code there.  */
    834 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    835 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    836 #else
    837 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    838 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    839 #endif
    840 #elif defined(CPU_CORTEXA8)
    841 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    842 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    843 #endif
    844 
    845 	/*
    846 	 * map integrated peripherals at same address in l1pagetable
    847 	 * so that we can continue to use console.
    848 	 */
    849 	pmap_devmap_bootstrap(l1pagetable, gumstix_devmap);
    850 
    851 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    852 	/*
    853 	 * Give the XScale global cache clean code an appropriately
    854 	 * sized chunk of unmapped VA space starting at 0xff000000
    855 	 * (our device mappings end before this address).
    856 	 */
    857 	xscale_cache_clean_addr = 0xff000000U;
    858 #endif
    859 
    860 	/*
    861 	 * Now we have the real page tables in place so we can switch to them.
    862 	 * Once this is done we will be running with the REAL kernel page
    863 	 * tables.
    864 	 */
    865 
    866 	/*
    867 	 * Update the physical_freestart/physical_freeend/free_pages
    868 	 * variables.
    869 	 */
    870 	{
    871 		extern char _end[];
    872 
    873 		physical_freestart = physical_start +
    874 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    875 		     KERNEL_BASE);
    876 		physical_freeend = physical_end;
    877 		free_pages =
    878 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    879 	}
    880 
    881 	/* Switch tables */
    882 #ifdef VERBOSE_INIT_ARM
    883 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    884 	       physical_freestart, free_pages, free_pages);
    885 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    886 #endif
    887 
    888 	cpu_setttb(kernel_l1pt.pv_pa);
    889 	cpu_tlb_flushID();
    890 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    891 
    892 	/*
    893 	 * Moved from cpu_startup() as data_abort_handler() references
    894 	 * this during uvm init
    895 	 */
    896 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    897 
    898 #ifdef VERBOSE_INIT_ARM
    899 	printf("bootstrap done.\n");
    900 #endif
    901 
    902 #if defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
    903 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    904 #elif defined(CPU_CORTEXA8)
    905 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    906 #endif
    907 
    908 	/*
    909 	 * Pages were allocated during the secondary bootstrap for the
    910 	 * stacks for different CPU modes.
    911 	 * We must now set the r13 registers in the different CPU modes to
    912 	 * point to these stacks.
    913 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    914 	 * of the stack memory.
    915 	 */
    916 #ifdef	VERBOSE_INIT_ARM
    917 	printf("init subsystems: stacks ");
    918 #endif
    919 
    920 	set_stackptr(PSR_IRQ32_MODE,
    921 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    922 	set_stackptr(PSR_ABT32_MODE,
    923 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    924 	set_stackptr(PSR_UND32_MODE,
    925 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    926 
    927 	/*
    928 	 * Well we should set a data abort handler.
    929 	 * Once things get going this will change as we will need a proper
    930 	 * handler.
    931 	 * Until then we will use a handler that just panics but tells us
    932 	 * why.
    933 	 * Initialisation of the vectors will just panic on a data abort.
    934 	 * This just fills in a slighly better one.
    935 	 */
    936 #ifdef	VERBOSE_INIT_ARM
    937 	printf("vectors ");
    938 #endif
    939 	data_abort_handler_address = (u_int)data_abort_handler;
    940 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    941 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    942 
    943 	/* Initialise the undefined instruction handlers */
    944 #ifdef	VERBOSE_INIT_ARM
    945 	printf("undefined ");
    946 #endif
    947 	undefined_init();
    948 
    949 	/* Load memory into UVM. */
    950 #ifdef	VERBOSE_INIT_ARM
    951 	printf("page ");
    952 #endif
    953 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    954 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    955 	    atop(physical_freestart), atop(physical_freeend),
    956 	    VM_FREELIST_DEFAULT);
    957 
    958 	/* Boot strap pmap telling it where the kernel page table is */
    959 #ifdef	VERBOSE_INIT_ARM
    960 	printf("pmap ");
    961 #endif
    962 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    963 
    964 #ifdef __HAVE_MEMORY_DISK__
    965 	md_root_setconf(memory_disk, sizeof memory_disk);
    966 #endif
    967 
    968 #ifdef BOOTHOWTO
    969 	boothowto |= BOOTHOWTO;
    970 #endif
    971 
    972 #ifdef KGDB
    973 	if (boothowto & RB_KDB) {
    974 		kgdb_debug_init = 1;
    975 		kgdb_connect(1);
    976 	}
    977 #endif
    978 
    979 #if NKSYMS || defined(DDB) || defined(MODULAR)
    980 	/* Firmware doesn't load symbols. */
    981 	ddb_init(0, NULL, NULL);
    982 #endif
    983 
    984 #ifdef DDB
    985 	db_machine_init();
    986 	if (boothowto & RB_KDB)
    987 		Debugger();
    988 #endif
    989 
    990 	/* We have our own device_register() */
    991 	evbarm_device_register = gumstix_device_register;
    992 
    993 	/* We return the new stack pointer address */
    994 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    995 }
    996 
    997 #if defined(GUMSTIX)
    998 static void
    999 read_system_serial(void)
   1000 {
   1001 #define GUMSTIX_SYSTEM_SERIAL_ADDR	0
   1002 #define GUMSTIX_SYSTEM_SERIAL_SIZE	8
   1003 #define FLASH_OFFSET_INTEL_PROTECTION	0x81
   1004 #define FLASH_OFFSET_USER_PROTECTION	0x85
   1005 #define FLASH_CMD_READ_ID		0x90
   1006 #define FLASH_CMD_RESET			0xff
   1007 	int i;
   1008 	char system_serial[GUMSTIX_SYSTEM_SERIAL_SIZE], *src;
   1009 	char x;
   1010 
   1011 	src = (char *)(FLASH_OFFSET_USER_PROTECTION * 2 /*word*/);
   1012 	*(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
   1013 	memcpy(system_serial,
   1014 	    src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
   1015 	*(volatile uint16_t *)0 = FLASH_CMD_RESET;
   1016 
   1017 	for (i = 1, x = system_serial[0]; i < sizeof (system_serial); i++)
   1018 		x &= system_serial[i];
   1019 	if (x == 0xff) {
   1020 		src = (char *)(FLASH_OFFSET_INTEL_PROTECTION * 2 /*word*/);
   1021 		*(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
   1022 		memcpy(system_serial,
   1023 		    src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
   1024 		*(volatile uint16_t *)0 = FLASH_CMD_RESET;
   1025 
   1026 		/*
   1027 		 * XXXX: Don't need ???
   1028 		 * gumstix_serial_hash(system_serial);
   1029 		 */
   1030 	}
   1031 	system_serial_high = system_serial[0] << 24 | system_serial[1] << 16 |
   1032 	    system_serial[2] << 8 | system_serial[3];
   1033 	system_serial_low = system_serial[4] << 24 | system_serial[5] << 16 |
   1034 	    system_serial[6] << 8 | system_serial[7];
   1035 
   1036 	printf("system serial: 0x");
   1037 	for (i = 0; i < sizeof (system_serial); i++)
   1038 		printf("%02x", system_serial[i]);
   1039 	printf("\n");
   1040 }
   1041 #endif
   1042 
   1043 #ifdef GUMSTIX_NETBSD_ARGS_BUSHEADER
   1044 static const char busheader_name[] = "busheader=";
   1045 #endif
   1046 #if defined(GUMSTIX_NETBSD_ARGS_BUSHEADER) || \
   1047     defined(GUMSTIX_NETBSD_ARGS_EXPANSION)
   1048 static const char expansion_name[] = "expansion=";
   1049 #endif
   1050 #ifdef GUMSTIX_NETBSD_ARGS_CONSOLE
   1051 static const char console_name[] = "console=";
   1052 #endif
   1053 static void
   1054 process_kernel_args(int argc, char *argv[])
   1055 {
   1056 	int gxio_configured = 0, i, j;
   1057 
   1058 	boothowto = 0;
   1059 
   1060 	for (i = 1, j = 0; i < argc; i++) {
   1061 #ifdef GUMSTIX_NETBSD_ARGS_BUSHEADER
   1062 		if (!strncmp(argv[i], busheader_name, strlen(busheader_name))) {
   1063 			/* Configure for GPIOs of busheader side */
   1064 			gxio_config_expansion(argv[i] + strlen(busheader_name));
   1065 			gxio_configured = 1;
   1066 			continue;
   1067 		}
   1068 #endif
   1069 #if defined(GUMSTIX_NETBSD_ARGS_BUSHEADER) || \
   1070     defined(GUMSTIX_NETBSD_ARGS_EXPANSION)
   1071 		if (!strncmp(argv[i], expansion_name, strlen(expansion_name))) {
   1072 			/* Configure expansion */
   1073 			gxio_config_expansion(argv[i] + strlen(expansion_name));
   1074 			gxio_configured = 1;
   1075 			continue;
   1076 		}
   1077 #endif
   1078 #ifdef GUMSTIX_NETBSD_ARGS_CONSOLE
   1079 		if (!strncmp(argv[i], console_name, strlen(console_name))) {
   1080 			strncpy(console, argv[i] + strlen(console_name),
   1081 			    sizeof(console));
   1082 			consinit();
   1083 		}
   1084 #endif
   1085 		if (j == bootargs_len) {
   1086 			*(bootargs + j) = '\0';
   1087 			continue;
   1088 		}
   1089 		if (j != 0)
   1090 			*(bootargs + j++) = ' ';
   1091 		strncpy(bootargs + j, argv[i], bootargs_len - j);
   1092 		bootargs[bootargs_len] = '\0';
   1093 		j += strlen(argv[i]);
   1094 	}
   1095 	boot_args = bootargs;
   1096 
   1097 	parse_mi_bootargs(boot_args);
   1098 
   1099 	if (!gxio_configured)
   1100 		gxio_config_expansion(NULL);
   1101 }
   1102 
   1103 static void
   1104 process_kernel_args_liner(char *args)
   1105 {
   1106 	int i = 0;
   1107 	char *p = NULL;
   1108 
   1109 	boothowto = 0;
   1110 
   1111 	strncpy(bootargs, args, sizeof(bootargs));
   1112 #if defined(GUMSTIX_NETBSD_ARGS_BUSHEADER) || \
   1113     defined(GUMSTIX_NETBSD_ARGS_EXPANSION)
   1114 	{
   1115 		char *q;
   1116 
   1117 		if ((p = strstr(bootargs, expansion_name)))
   1118 			q = p + strlen(expansion_name);
   1119 #ifdef GUMSTIX_NETBSD_ARGS_BUSHEADER
   1120 		else if ((p = strstr(bootargs, busheader_name)))
   1121 			q = p + strlen(busheader_name);
   1122 #endif
   1123 		if (p) {
   1124 			char expansion[256], c;
   1125 
   1126 			i = 0;
   1127 			do {
   1128 				c = *(q + i);
   1129 				if (c == ' ')
   1130 					c = '\0';
   1131 				expansion[i++] = c;
   1132 			} while (c != '\0' && i < sizeof(expansion));
   1133 			gxio_config_expansion(expansion);
   1134 			strcpy(p, q + i);
   1135 		}
   1136 	}
   1137 #endif
   1138 	if (p == NULL)
   1139 		gxio_config_expansion(NULL);
   1140 #ifdef GUMSTIX_NETBSD_ARGS_CONSOLE
   1141 	p = strstr(bootargs, console_name);
   1142 	if (p != NULL) {
   1143 		char c;
   1144 
   1145 		i = 0;
   1146 		do {
   1147 			c = *(p + strlen(console_name) + i);
   1148 			if (c == ' ')
   1149 				c = '\0';
   1150 			console[i++] = c;
   1151 		} while (c != '\0' && i < sizeof(console));
   1152 		consinit();
   1153 		strcpy(p, p + strlen(console_name) + i);
   1154 	}
   1155 #endif
   1156 	boot_args = bootargs;
   1157 
   1158 	parse_mi_bootargs(boot_args);
   1159 }
   1160 
   1161 #ifdef KGDB
   1162 #ifndef KGDB_DEVNAME
   1163 #define KGDB_DEVNAME	"ffuart"
   1164 #endif
   1165 const char kgdb_devname[] = KGDB_DEVNAME;
   1166 
   1167 #ifndef KGDB_DEVRATE
   1168 #define KGDB_DEVRATE	CONSPEED
   1169 #endif
   1170 int kgdb_devrate = KGDB_DEVRATE;
   1171 
   1172 #if (NCOM > 0)
   1173 #ifndef KGDB_DEVMODE
   1174 #define KGDB_DEVMODE	CONMODE
   1175 #endif
   1176 int comkgdbmode = KGDB_DEVMODE;
   1177 #endif /* NCOM */
   1178 
   1179 #endif /* KGDB */
   1180 
   1181 
   1182 void
   1183 consinit(void)
   1184 {
   1185 	static int consinit_called = 0;
   1186 
   1187 	if (consinit_called != 0)
   1188 		return;
   1189 
   1190 	consinit_called = 1;
   1191 
   1192 #if NCOM > 0
   1193 
   1194 #ifdef GUMSTIX_NETBSD_ARGS_CONSOLE
   1195 	/* Maybe passed Linux's bootargs 'console=ttyS?,<speed>...' */
   1196 	if (strncmp(console, "ttyS", 4) == 0 && console[5] == ',') {
   1197 		int i;
   1198 
   1199 		comcnspeed = 0;
   1200 		for (i = 6; i < strlen(console) && isdigit(console[i]); i++)
   1201 			comcnspeed = comcnspeed * 10 + (console[i] - '0');
   1202 	}
   1203 #endif
   1204 
   1205 #if defined(GUMSTIX)
   1206 
   1207 #ifdef FFUARTCONSOLE
   1208 #ifdef KGDB
   1209 	if (strcmp(kgdb_devname, "ffuart") == 0){
   1210 		/* port is reserved for kgdb */
   1211 	} else
   1212 #endif
   1213 #if defined(GUMSTIX_NETBSD_ARGS_CONSOLE)
   1214 	if (console[0] == '\0' || strcasecmp(console, "ffuart") == 0 ||
   1215 	    strncmp(console, "ttyS0,", 6) == 0)
   1216 #endif
   1217 	{
   1218 		int rv;
   1219 
   1220 		rv = comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
   1221 		    comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode);
   1222 		if (rv == 0) {
   1223 			pxa2x0_clkman_config(CKEN_FFUART, 1);
   1224 			return;
   1225 		}
   1226 	}
   1227 #endif /* FFUARTCONSOLE */
   1228 
   1229 #ifdef STUARTCONSOLE
   1230 #ifdef KGDB
   1231 	if (strcmp(kgdb_devname, "stuart") == 0) {
   1232 		/* port is reserved for kgdb */
   1233 	} else
   1234 #endif
   1235 #if defined(GUMSTIX_NETBSD_ARGS_CONSOLE)
   1236 	if (console[0] == '\0' || strcasecmp(console, "stuart") == 0)
   1237 #endif
   1238 	{
   1239 		int rv;
   1240 
   1241 		rv = comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_STUART_BASE,
   1242 		    comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode);
   1243 		if (rv == 0) {
   1244 			pxa2x0_clkman_config(CKEN_STUART, 1);
   1245 			return;
   1246 		}
   1247 	}
   1248 #endif /* STUARTCONSOLE */
   1249 
   1250 #ifdef BTUARTCONSOLE
   1251 #ifdef KGDB
   1252 	if (strcmp(kgdb_devname, "btuart") == 0) {
   1253 		/* port is reserved for kgdb */
   1254 	} else
   1255 #endif
   1256 #if defined(GUMSTIX_NETBSD_ARGS_CONSOLE)
   1257 	if (console[0] == '\0' || strcasecmp(console, "btuart") == 0)
   1258 #endif
   1259 	{
   1260 		int rv;
   1261 
   1262 		rv = comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
   1263 		    comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode);
   1264 		if (rv == 0) {
   1265 			pxa2x0_clkman_config(CKEN_BTUART, 1);
   1266 			return;
   1267 		}
   1268 	}
   1269 #endif /* BTUARTCONSOLE */
   1270 
   1271 #ifdef HWUARTCONSOLE
   1272 #ifdef KGDB
   1273 	if (strcmp(kgdb_devname, "hwuart") == 0) {
   1274 		/* port is reserved for kgdb */
   1275 	} else
   1276 #endif
   1277 #if defined(GUMSTIX_NETBSD_ARGS_CONSOLE)
   1278 	if (console[0] == '\0' || strcasecmp(console, "hwuart") == 0)
   1279 #endif
   1280 	{
   1281 		rv = comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_HWUART_BASE,
   1282 		    comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode);
   1283 		if (rv == 0) {
   1284 			pxa2x0_clkman_config(CKEN_HWUART, 1);
   1285 			return;
   1286 		}
   1287 	}
   1288 #endif /* HWUARTCONSOLE */
   1289 
   1290 #elif defined(OVERO)
   1291 
   1292 	if (comcnattach(&omap_a4x_bs_tag, 0x49020000, comcnspeed,
   1293 	    OMAP_COM_FREQ, COM_TYPE_NORMAL, comcnmode) == 0)
   1294 		return;
   1295 
   1296 #endif /* GUMSTIX or OVERO */
   1297 
   1298 #endif /* NCOM */
   1299 
   1300 #if NLCD > 0
   1301 #if defined(GUMSTIX_NETBSD_ARGS_CONSOLE)
   1302 	if (console[0] == '\0' || strcasecmp(console, "lcd") == 0)
   1303 #endif
   1304 	{
   1305 		gxlcd_cnattach();
   1306 	}
   1307 #endif
   1308 }
   1309 
   1310 #ifdef KGDB
   1311 static void
   1312 kgdb_port_init(void)
   1313 {
   1314 #if (NCOM > 0) && defined(COM_PXA2X0)
   1315 	paddr_t paddr = 0;
   1316 	int cken = 0;
   1317 
   1318 	if (0 == strcmp(kgdb_devname, "ffuart")) {
   1319 		paddr = PXA2X0_FFUART_BASE;
   1320 		cken = CKEN_FFUART;
   1321 	} else if (0 == strcmp(kgdb_devname, "stuart")) {
   1322 		paddr = PXA2X0_STUART_BASE;
   1323 		cken = CKEN_STUART;
   1324 	} else if (0 == strcmp(kgdb_devname, "btuart")) {
   1325 		paddr = PXA2X0_BTUART_BASE;
   1326 		cken = CKEN_BTUART;
   1327 	} else if (0 == strcmp(kgdb_devname, "hwuart")) {
   1328 		paddr = PXA2X0_HWUART_BASE;
   1329 		cken = CKEN_HWUART;
   1330 	}
   1331 
   1332 	if (paddr &&
   1333 	    0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
   1334 		kgdb_devrate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
   1335 
   1336 		pxa2x0_clkman_config(cken, 1);
   1337 	}
   1338 
   1339 #endif
   1340 }
   1341 #endif
   1342 
   1343 static void
   1344 gumstix_device_register(device_t dev, void *aux)
   1345 {
   1346 
   1347 	if (device_is_a(dev, "ohci")) {
   1348 		if (prop_dictionary_set_bool(device_properties(dev),
   1349 		    "Ganged-power-mask-on-port1", 1) == false) {
   1350 			printf("WARNING: unable to set power-mask for port1"
   1351 			    " property for %s\n", dev->dv_xname);
   1352 		}
   1353 		if (prop_dictionary_set_bool(device_properties(dev),
   1354 		    "Ganged-power-mask-on-port2", 1) == false) {
   1355 			printf("WARNING: unable to set power-mask for port2"
   1356 			    " property for %s\n", dev->dv_xname);
   1357 		}
   1358 		if (prop_dictionary_set_bool(device_properties(dev),
   1359 		    "Ganged-power-mask-on-port3", 1) == false) {
   1360 			printf("WARNING: unable to set power-mask for port3"
   1361 			    " property for %s\n", dev->dv_xname);
   1362 		}
   1363 	}
   1364 }
   1365