Home | History | Annotate | Line # | Download | only in netwalker
netwalker_machdep.c revision 1.1
      1 /*	$NetBSD: netwalker_machdep.c,v 1.1 2010/11/13 07:31:32 bsh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002, 2003, 2005, 2010  Genetec Corporation.
      5  * All rights reserved.
      6  * Written by Hiroyuki Bessho for Genetec Corporation.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  * Machine dependant functions for kernel setup for Sharp Netwalker.
     30  * Based on iq80310_machhdep.c
     31  */
     32 /*
     33  * Copyright (c) 2001 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed for the NetBSD Project by
     49  *	Wasabi Systems, Inc.
     50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     51  *    or promote products derived from this software without specific prior
     52  *    written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 /*
     68  * Copyright (c) 1997,1998 Mark Brinicombe.
     69  * Copyright (c) 1997,1998 Causality Limited.
     70  * All rights reserved.
     71  *
     72  * Redistribution and use in source and binary forms, with or without
     73  * modification, are permitted provided that the following conditions
     74  * are met:
     75  * 1. Redistributions of source code must retain the above copyright
     76  *    notice, this list of conditions and the following disclaimer.
     77  * 2. Redistributions in binary form must reproduce the above copyright
     78  *    notice, this list of conditions and the following disclaimer in the
     79  *    documentation and/or other materials provided with the distribution.
     80  * 3. All advertising materials mentioning features or use of this software
     81  *    must display the following acknowledgement:
     82  *	This product includes software developed by Mark Brinicombe
     83  *	for the NetBSD Project.
     84  * 4. The name of the company nor the name of the author may be used to
     85  *    endorse or promote products derived from this software without specific
     86  *    prior written permission.
     87  *
     88  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     90  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     91  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     92  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     93  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     94  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     95  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     96  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     97  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     98  * SUCH DAMAGE.
     99  *
    100  * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
    101  * boards using RedBoot firmware.
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: netwalker_machdep.c,v 1.1 2010/11/13 07:31:32 bsh Exp $");
    106 
    107 #include "opt_ddb.h"
    108 #include "opt_kgdb.h"
    109 #include "opt_ipkdb.h"
    110 #include "opt_pmap_debug.h"
    111 #include "opt_md.h"
    112 #include "opt_com.h"
    113 #include "md.h"
    114 #include "imxuart.h"
    115 #include "opt_imxuart.h"
    116 #include "opt_imx.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/pte.h>
    149 #include <arm/arm32/machdep.h>
    150 
    151 #include <arm/imx/imx51reg.h>
    152 #include <arm/imx/imx51var.h>
    153 #include <arm/imx/imxgpioreg.h>
    154 #include <arm/imx/imxwdogreg.h>
    155 #include <arm/imx/imxuartreg.h>
    156 #include <arm/imx/imxuartvar.h>
    157 #include <arm/imx/imx51_iomuxreg.h>
    158 #include <evbarm/netwalker/netwalker_reg.h>
    159 
    160 /* Kernel text starts 1MB in from the bottom of the kernel address space. */
    161 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00100000)
    162 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    163 
    164 /*
    165  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    166  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    167  */
    168 #define KERNEL_VM_SIZE		0x0C000000
    169 
    170 
    171 /*
    172  * Address to call from cpu_reset() to reset the machine.
    173  * This is machine architecture dependant as it varies depending
    174  * on where the ROM appears when you turn the MMU off.
    175  */
    176 
    177 u_int cpu_reset_address = 0;
    178 
    179 /* Define various stack sizes in pages */
    180 #define FIQ_STACK_SIZE	1
    181 #define IRQ_STACK_SIZE	1
    182 #define ABT_STACK_SIZE	1
    183 #ifdef IPKDB
    184 #define UND_STACK_SIZE	2
    185 #else
    186 #define UND_STACK_SIZE	1
    187 #endif
    188 
    189 BootConfig bootconfig;		/* Boot config storage */
    190 char *boot_args = NULL;
    191 char *boot_file = NULL;
    192 
    193 vm_offset_t physical_start;
    194 vm_offset_t physical_freestart;
    195 vm_offset_t physical_freeend;
    196 vm_offset_t physical_end;
    197 u_int free_pages;
    198 vm_offset_t pagetables_start;
    199 
    200 /*int debug_flags;*/
    201 #ifndef PMAP_STATIC_L1S
    202 int max_processes = 64;			/* Default number */
    203 #endif	/* !PMAP_STATIC_L1S */
    204 
    205 /* Physical and virtual addresses for some global pages */
    206 pv_addr_t fiqstack;
    207 pv_addr_t irqstack;
    208 pv_addr_t undstack;
    209 pv_addr_t abtstack;
    210 pv_addr_t kernelstack;
    211 
    212 vm_offset_t msgbufphys;
    213 
    214 extern u_int data_abort_handler_address;
    215 extern u_int prefetch_abort_handler_address;
    216 extern u_int undefined_handler_address;
    217 extern char KERNEL_BASE_phys[];
    218 extern char KERNEL_BASE_virt[];
    219 extern char etext[], __data_start[], _edata[], __bss_start[], __bss_end__[];
    220 extern char _end[];
    221 extern int cpu_do_powersave;
    222 
    223 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    224 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    225 #define	KERNEL_PT_KERNEL_NUM	4
    226 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
    227 				        /* Page tables for mapping kernel VM */
    228 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    229 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    230 
    231 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    232 
    233 /*
    234  * Macros to translate between physical and virtual for a subset of the
    235  * kernel address space.  *Not* for general use.
    236  */
    237 #define KERNEL_BASE_PHYS ((paddr_t)&KERNEL_BASE_phys)
    238 #define KERNEL_BASE_VIRT ((vaddr_t)&KERNEL_BASE_virt)
    239 #define KERN_VTOPHYS(va) \
    240 	((paddr_t)((vaddr_t)va - KERNEL_BASE_VIRT + KERNEL_BASE_PHYS))
    241 #define KERN_PHYSTOV(pa) \
    242 	((vaddr_t)((paddr_t)pa - KERNEL_BASE_PHYS + KERNEL_BASE_VIRT))
    243 
    244 
    245 /* Prototypes */
    246 
    247 void consinit(void);
    248 #if 0
    249 void	process_kernel_args(char *);
    250 #endif
    251 
    252 #ifdef KGDB
    253 void	kgdb_port_init(void);
    254 #endif
    255 void	change_clock(uint32_t v);
    256 
    257 static void init_clocks(void);
    258 static void setup_ioports(void);
    259 #ifdef DEBUG_IOPORTS
    260 void dump_registers(void);
    261 #endif
    262 
    263 bs_protos(bs_notimpl);
    264 
    265 #ifndef CONSPEED
    266 #define CONSPEED B115200	/* What RedBoot uses */
    267 #endif
    268 #ifndef CONMODE
    269 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    270 #endif
    271 
    272 int comcnspeed = CONSPEED;
    273 int comcnmode = CONMODE;
    274 
    275 /*
    276  * void cpu_reboot(int howto, char *bootstr)
    277  *
    278  * Reboots the system
    279  *
    280  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    281  * then reset the CPU.
    282  */
    283 void
    284 cpu_reboot(int howto, char *bootstr)
    285 {
    286 #ifdef DIAGNOSTIC
    287 	/* info */
    288 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    289 #endif
    290 
    291 	/*
    292 	 * If we are still cold then hit the air brakes
    293 	 * and crash to earth fast
    294 	 */
    295 	if (cold) {
    296 		doshutdownhooks();
    297 		pmf_system_shutdown(boothowto);
    298 		printf("The operating system has halted.\n");
    299 		printf("Please press any key to reboot.\n\n");
    300 		cngetc();
    301 		printf("rebooting...\n");
    302 		cpu_reset();
    303 		/*NOTREACHED*/
    304 	}
    305 
    306 	/* Disable console buffering */
    307 /*	cnpollc(1);*/
    308 
    309 	/*
    310 	 * If RB_NOSYNC was not specified sync the discs.
    311 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    312 	 * unmount.  It looks like syslogd is getting woken up only to find
    313 	 * that it cannot page part of the binary in as the filesystem has
    314 	 * been unmounted.
    315 	 */
    316 	if (!(howto & RB_NOSYNC))
    317 		bootsync();
    318 
    319 	/* Say NO to interrupts */
    320 	splhigh();
    321 
    322 	/* Do a dump if requested. */
    323 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    324 		dumpsys();
    325 
    326 	/* Run any shutdown hooks */
    327 	doshutdownhooks();
    328 
    329 	pmf_system_shutdown(boothowto);
    330 
    331 	/* Make sure IRQ's are disabled */
    332 	IRQdisable;
    333 
    334 	if (howto & RB_HALT) {
    335 		printf("The operating system has halted.\n");
    336 		printf("Please press any key to reboot.\n\n");
    337 		cngetc();
    338 	}
    339 
    340 	printf("rebooting...\n");
    341 	cpu_reset();
    342 	/*NOTREACHED*/
    343 }
    344 
    345 /*
    346  * Static device mappings. These peripheral registers are mapped at
    347  * fixed virtual addresses very early in netwalker_start() so that we
    348  * can use them while booting the kernel, and stay at the same address
    349  * throughout whole kernel's life time.
    350  *
    351  * We use this table twice; once with bootstrap page table, and once
    352  * with kernel's page table which we build up in initarm().
    353  */
    354 
    355 #define _A(a)   ((a) & ~L1_S_OFFSET)
    356 #define _S(s)   (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    357 
    358 static const struct pmap_devmap netwalker_devmap[] = {
    359 	{
    360 		/* for UART1, IOMUXC */
    361 		NETWALKER_IO_VBASE0,
    362 		_A(NETWALKER_IO_PBASE0),
    363 		L1_S_SIZE * 4,
    364 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE
    365 	},
    366 	{0, 0, 0, 0, 0 }
    367 };
    368 
    369 #ifndef MEMSTART
    370 #define MEMSTART	0x90000000
    371 #endif
    372 #ifndef MEMSIZE
    373 #define MEMSIZE		512
    374 #endif
    375 
    376 /*
    377  * u_int initarm(...)
    378  *
    379  * Initial entry point on startup. This gets called before main() is
    380  * entered.
    381  * It should be responsible for setting up everything that must be
    382  * in place when main is called.
    383  * This includes
    384  *   Taking a copy of the boot configuration structure.
    385  *   Initialising the physical console so characters can be printed.
    386  *   Setting up page tables for the kernel
    387  *   Relocating the kernel to the bottom of physical memory
    388  */
    389 u_int
    390 initarm(void *arg)
    391 {
    392 	int loop;
    393 	int loop1;
    394 	vaddr_t l1pagetable;
    395 
    396 #ifdef	RBFLAGS
    397 	boothowto |= RBFLAGS;
    398 #endif
    399 
    400 	disable_interrupts(I32_bit|F32_bit);
    401 	/* XXX move to netwalker_start.S */
    402 
    403 	/* Register devmap for devices we mapped in start */
    404 	pmap_devmap_register(netwalker_devmap);
    405 
    406 	setup_ioports();
    407 
    408 	consinit();
    409 
    410 #ifdef	DEBUG_IOPORTS
    411 	dump_registers();
    412 #endif
    413 
    414 	/*
    415 	 * Heads up ... Setup the CPU / MMU / TLB functions
    416 	 */
    417 	if (set_cpufuncs())
    418 		panic("cpu not recognized!");
    419 
    420 #ifdef	NO_POWERSAVE
    421 	cpu_do_powersave=0;
    422 #endif
    423 
    424 	init_clocks();
    425 
    426 #ifdef KGDB
    427 	kgdb_port_init();
    428 #endif
    429 
    430 	/* Talk to the user */
    431 	printf("\nNetBSD/evbarm (netwalker) booting ...\n");
    432 
    433 	/*
    434 	 * Ok we have the following memory map
    435 	 *
    436 	 * Physical Address Range     Description
    437 	 * -----------------------    ----------------------------------
    438 	 *
    439 	 * 0x90000000 - 0x97FFFFFF    DDR SDRAM (128MByte)
    440 	 *
    441 	 * The initarm() has the responsibility for creating the kernel
    442 	 * page tables.
    443 	 * It must also set up various memory pointers that are used
    444 	 * by pmap etc.
    445 	 */
    446 
    447 #if 0
    448 	/*
    449 	 * Examine the boot args string for options we need to know about
    450 	 * now.
    451 	 */
    452 	process_kernel_args((char *)nwbootinfo.bt_args);
    453 #endif
    454 
    455 #ifdef VERBOSE_INIT_ARM
    456 	printf("initarm: Configuring system ...\n");
    457 #endif
    458 	/* Fake bootconfig structure for the benefit of pmap.c */
    459 	/* XXX must make the memory description h/w independent */
    460 	bootconfig.dramblocks = 1;
    461 	bootconfig.dram[0].address = MEMSTART;
    462 	bootconfig.dram[0].pages = (MEMSIZE * 1024 * 1024)/ PAGE_SIZE;
    463 
    464 	/*
    465 	 * Set up the variables that define the availablilty of
    466 	 * physical memory.  For now, we're going to set
    467 	 * physical_freestart to 0x80100000 (where the kernel
    468 	 * was loaded), and allocate the memory we need downwards.
    469 	 * If we get too close to the bottom of SDRAM, we
    470 	 * will panic.  We will update physical_freestart and
    471 	 * physical_freeend later to reflect what pmap_bootstrap()
    472 	 * wants to see.
    473 	 *
    474 	 * XXX pmap_bootstrap() needs an enema.
    475 	 */
    476 	physical_start = bootconfig.dram[0].address;
    477 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    478 
    479 	physical_freestart = 0x90000000UL;	/* top of loadaddres */
    480 	physical_freeend =   0x90100000UL;	/* base of kernel */
    481 
    482 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    483 
    484 #ifdef VERBOSE_INIT_ARM
    485 	/* Tell the user about the memory */
    486 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    487 	    physical_start, physical_end - 1);
    488 #endif
    489 
    490 	/*
    491 	 * Okay, the kernel starts 1MB in from the bottom of physical
    492 	 * memory.  We are going to allocate our bootstrap pages downwards
    493 	 * from there.
    494 	 *
    495 	 * We need to allocate some fixed page tables to get the kernel
    496 	 * going.  We allocate one page directory and a number of page
    497 	 * tables and store the physical addresses in the kernel_pt_table
    498 	 * array.
    499 	 *
    500 	 * The kernel page directory must be on a 16K boundary.  The page
    501 	 * tables must be on 4K boundaries.  What we do is allocate the
    502 	 * page directory on the first 16K boundary that we encounter, and
    503 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    504 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    505 	 * least one 16K aligned region.
    506 	 */
    507 
    508 #ifdef VERBOSE_INIT_ARM
    509 	printf("Allocating page tables\n");
    510 #endif
    511 
    512 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    513 
    514 #ifdef VERBOSE_INIT_ARM
    515 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    516 	       physical_freestart, free_pages, free_pages);
    517 #endif
    518 
    519 	/* Define a macro to simplify memory allocation */
    520 #define	valloc_pages(var, np)				\
    521 	alloc_pages((var).pv_pa, (np));			\
    522 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    523 
    524 #define alloc_pages(var, np)				\
    525 	physical_freeend -= ((np) * PAGE_SIZE);		\
    526 	if (physical_freeend < physical_freestart)	\
    527 		panic("initarm: out of memory");	\
    528 	(var) = physical_freeend;			\
    529 	free_pages -= (np);				\
    530 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    531 
    532 	loop1 = 0;
    533 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    534 		/* Are we 16KB aligned for an L1 ? */
    535 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    536 		    && kernel_l1pt.pv_pa == 0) {
    537 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    538 		} else {
    539 			valloc_pages(kernel_pt_table[loop1],
    540 			    L2_TABLE_SIZE / PAGE_SIZE);
    541 			++loop1;
    542 		}
    543 	}
    544 
    545 	/* This should never be able to happen but better confirm that. */
    546 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    547 		panic("initarm: Failed to align the kernel page directory");
    548 
    549 	/*
    550 	 * Allocate a page for the system page mapped to V0x00000000
    551 	 * This page will just contain the system vectors and can be
    552 	 * shared by all processes.
    553 	 */
    554 	valloc_pages(systempage, 1);
    555 	systempage.pv_va = ARM_VECTORS_HIGH;
    556 
    557 	/* Allocate stacks for all modes */
    558 	valloc_pages(fiqstack, FIQ_STACK_SIZE);
    559 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    560 	valloc_pages(abtstack, ABT_STACK_SIZE);
    561 	valloc_pages(undstack, UND_STACK_SIZE);
    562 	valloc_pages(kernelstack, UPAGES);
    563 
    564 #ifdef VERBOSE_INIT_ARM
    565 	printf("FIQ stack: p0x%08lx v0x%08lx\n", fiqstack.pv_pa,
    566 	    fiqstack.pv_va);
    567 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    568 	    irqstack.pv_va);
    569 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    570 	    abtstack.pv_va);
    571 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    572 	    undstack.pv_va);
    573 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    574 	    kernelstack.pv_va);
    575 #endif
    576 
    577 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    578 
    579 	/*
    580 	 * Ok we have allocated physical pages for the primary kernel
    581 	 * page tables
    582 	 */
    583 
    584 #ifdef VERBOSE_INIT_ARM
    585 	printf("Creating L1 page table at p0x%08lx v0x%08lx\n",
    586 		kernel_l1pt.pv_pa, kernel_l1pt.pv_va);
    587 #endif
    588 
    589 	/*
    590 	 * Now we start construction of the L1 page table
    591 	 * We start by mapping the L2 page tables into the L1.
    592 	 * This means that we can replace L1 mappings later on if necessary
    593 	 */
    594 	l1pagetable = kernel_l1pt.pv_pa;
    595 
    596 	/* Map the L2 pages tables in the L1 page table */
    597 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    598 		       &kernel_pt_table[KERNEL_PT_SYS]);
    599 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    600 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    601 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    602 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    603 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    604 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    605 
    606 	/* update the top of the kernel VM */
    607 	pmap_curmaxkvaddr =
    608 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    609 
    610 #ifdef VERBOSE_INIT_ARM
    611 	printf("Mapping kernel\n");
    612 #endif
    613 
    614 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    615 #define round_L_page(x) (((x) + L2_L_OFFSET) & L2_L_FRAME)
    616 	{
    617 		size_t textsize = round_L_page((size_t)etext - KERNEL_TEXT_BASE);
    618 		size_t totalsize = round_L_page((size_t)_end - KERNEL_TEXT_BASE);
    619 		u_int logical;
    620 
    621 
    622 #ifdef VERBOSE_INIT_ARM
    623 		printf("%s: etext %lx, _end %lx\n",
    624 		       __func__, (uintptr_t)etext, (uintptr_t)_end);
    625 		printf("%s: textsize %#lx, totalsize %#lx\n",
    626 		       __func__, textsize, totalsize);
    627 #endif
    628 		logical = 0x00100000;	/* offset of kernel in RAM */
    629 
    630 		/* Map text section read-only. */
    631 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    632 					  physical_start + logical, textsize,
    633 					  VM_PROT_READ|VM_PROT_EXECUTE, PTE_CACHE);
    634 
    635 		/* Map data and bss sections read-write. */
    636 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    637 					  physical_start + logical, totalsize - textsize,
    638 					  VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    639 	}
    640 
    641 #ifdef VERBOSE_INIT_ARM
    642 	printf("Constructing L2 page tables\n");
    643 #endif
    644 
    645 	/* Map the stack pages */
    646 	pmap_map_chunk(l1pagetable, fiqstack.pv_va, fiqstack.pv_pa,
    647 	    FIQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    648 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    649 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    650 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    651 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    652 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    653 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    654 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    655 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    656 
    657 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    658 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    659 
    660 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    661 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    662 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    663 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    664 	}
    665 
    666 	/* Map the vector page. */
    667 #if 0
    668 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
    669 	 * cache-clean code there.  */
    670 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    671 		       VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    672 #else
    673 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    674 		       VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    675 #endif
    676 
    677 	/*
    678 	 * map integrated peripherals at same address in l1pagetable
    679 	 * so that we can continue to use console.
    680 	 */
    681 	pmap_devmap_bootstrap(l1pagetable, netwalker_devmap);
    682 
    683 	/*
    684 	 * Now we have the real page tables in place so we can switch to them.
    685 	 * Once this is done we will be running with the REAL kernel page
    686 	 * tables.
    687 	 */
    688 
    689 	/*
    690 	 * Update the physical_freestart/physical_freeend/free_pages
    691 	 * variables.
    692 	 */
    693 	physical_freestart = physical_start +
    694 		(((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) - KERNEL_BASE);
    695 	physical_freeend = physical_end;
    696 	free_pages =
    697 		(physical_freeend - physical_freestart) / PAGE_SIZE;
    698 
    699 #ifdef VERBOSE_INIT_ARM
    700 	/* Tell the user about where all the bits and pieces live. */
    701 	printf("%22s       Physical              Virtual        Num\n", " ");
    702 	printf("%22s Starting    Ending    Starting    Ending   Pages\n", " ");
    703 
    704 	static const char mem_fmt[] =
    705 	    "%20s: 0x%08lx 0x%08lx 0x%08lx 0x%08lx %d\n";
    706 	static const char mem_fmt_nov[] =
    707 	    "%20s: 0x%08lx 0x%08lx                       %d\n";
    708 
    709 	printf(mem_fmt, "SDRAM", physical_start, physical_end-1,
    710 	    KERN_PHYSTOV(physical_start), KERN_PHYSTOV(physical_end-1),
    711 	    physmem);
    712 	printf(mem_fmt, "text section",
    713 	       (paddr_t)KERNEL_BASE_phys, KERN_VTOPHYS(etext-1),
    714 	       (vaddr_t)KERNEL_BASE_virt, (vaddr_t)etext-1,
    715 	       (int)(round_L_page((size_t)etext - KERNEL_TEXT_BASE) / PAGE_SIZE));
    716 	printf(mem_fmt, "data section",
    717 	       KERN_VTOPHYS(__data_start), KERN_VTOPHYS(_edata),
    718 	       (vaddr_t)__data_start, (vaddr_t)_edata,
    719 	       (int)((round_page((vaddr_t)_edata)
    720 		      - trunc_page((vaddr_t)__data_start)) / PAGE_SIZE));
    721 	printf(mem_fmt, "bss section",
    722 	       KERN_VTOPHYS(__bss_start), KERN_VTOPHYS(__bss_end__),
    723 	       (vaddr_t)__bss_start, (vaddr_t)__bss_end__,
    724 	       (int)((round_page((vaddr_t)__bss_end__)
    725 		      - trunc_page((vaddr_t)__bss_start)) / PAGE_SIZE));
    726 	printf(mem_fmt, "L1 page directory",
    727 	    kernel_l1pt.pv_pa, kernel_l1pt.pv_pa + L1_TABLE_SIZE - 1,
    728 	    kernel_l1pt.pv_va, kernel_l1pt.pv_va + L1_TABLE_SIZE - 1,
    729 	    L1_TABLE_SIZE / PAGE_SIZE);
    730 	printf(mem_fmt, "Exception Vectors",
    731 	    systempage.pv_pa, systempage.pv_pa + PAGE_SIZE - 1,
    732 	    systempage.pv_va, systempage.pv_va + PAGE_SIZE - 1,
    733 	    1);
    734 	printf(mem_fmt, "FIQ stack",
    735 	    fiqstack.pv_pa, fiqstack.pv_pa + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
    736 	    fiqstack.pv_va, fiqstack.pv_va + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
    737 	    FIQ_STACK_SIZE);
    738 	printf(mem_fmt, "IRQ stack",
    739 	    irqstack.pv_pa, irqstack.pv_pa + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
    740 	    irqstack.pv_va, irqstack.pv_va + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
    741 	    IRQ_STACK_SIZE);
    742 	printf(mem_fmt, "ABT stack",
    743 	    abtstack.pv_pa, abtstack.pv_pa + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
    744 	    abtstack.pv_va, abtstack.pv_va + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
    745 	    ABT_STACK_SIZE);
    746 	printf(mem_fmt, "UND stack",
    747 	    undstack.pv_pa, undstack.pv_pa + (UND_STACK_SIZE * PAGE_SIZE) - 1,
    748 	    undstack.pv_va, undstack.pv_va + (UND_STACK_SIZE * PAGE_SIZE) - 1,
    749 	    UND_STACK_SIZE);
    750 	printf(mem_fmt, "SVC stack",
    751 	    kernelstack.pv_pa, kernelstack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
    752 	    kernelstack.pv_va, kernelstack.pv_va + (UPAGES * PAGE_SIZE) - 1,
    753 	    UPAGES);
    754 	printf(mem_fmt_nov, "Message Buffer",
    755 	    msgbufphys, msgbufphys + round_page(MSGBUFSIZE) - 1, round_page(MSGBUFSIZE) / PAGE_SIZE);
    756 	printf(mem_fmt, "Free Memory", physical_freestart, physical_freeend-1,
    757 	    KERN_PHYSTOV(physical_freestart), KERN_PHYSTOV(physical_freeend-1),
    758 	    free_pages);
    759 #endif
    760 
    761 	/* Switch tables */
    762 #ifdef VERBOSE_INIT_ARM
    763 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    764 	       physical_freestart, free_pages, free_pages);
    765 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    766 #endif
    767 
    768 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    769 	cpu_setttb(kernel_l1pt.pv_pa);
    770 	cpu_tlb_flushID();
    771 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    772 
    773 	/*
    774 	 * Moved from cpu_startup() as data_abort_handler() references
    775 	 * this during uvm init
    776 	 */
    777 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    778 
    779 #ifdef VERBOSE_INIT_ARM
    780 	printf("bootstrap done.\n");
    781 #endif
    782 
    783 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    784 
    785 	/*
    786 	 * Pages were allocated during the secondary bootstrap for the
    787 	 * stacks for different CPU modes.
    788 	 * We must now set the r13 registers in the different CPU modes to
    789 	 * point to these stacks.
    790 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    791 	 * of the stack memory.
    792 	 */
    793 #ifdef VERBOSE_INIT_ARM
    794 	printf("init subsystems: stacks ");
    795 #endif
    796 	set_stackptr(PSR_FIQ32_MODE, fiqstack.pv_va + FIQ_STACK_SIZE * PAGE_SIZE);
    797 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    798 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    799 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    800 
    801 	/*
    802 	 * Well we should set a data abort handler.
    803 	 * Once things get going this will change as we will need a proper
    804 	 * handler.
    805 	 * Until then we will use a handler that just panics but tells us
    806 	 * why.
    807 	 * Initialisation of the vectors will just panic on a data abort.
    808 	 * This just fills in a slightly better one.
    809 	 */
    810 #ifdef VERBOSE_INIT_ARM
    811 	printf("vectors ");
    812 #endif
    813 	data_abort_handler_address = (u_int)data_abort_handler;
    814 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    815 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    816 
    817 	/* Initialise the undefined instruction handlers */
    818 #ifdef VERBOSE_INIT_ARM
    819 	printf("undefined ");
    820 #endif
    821 	undefined_init();
    822 
    823 	/* Load memory into UVM. */
    824 #ifdef VERBOSE_INIT_ARM
    825 	printf("page ");
    826 #endif
    827 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
    828 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    829 	    atop(physical_freestart), atop(physical_freeend),
    830 	    VM_FREELIST_DEFAULT);
    831 
    832 	/* Boot strap pmap telling it where the kernel page table is */
    833 #ifdef VERBOSE_INIT_ARM
    834 	printf("pmap ");
    835 #endif
    836 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    837 
    838 #ifdef __HAVE_MEMORY_DISK__
    839 	md_root_setconf(memory_disk, sizeof memory_disk);
    840 #endif
    841 
    842 #ifdef VERBOSE_INIT_ARM
    843 	printf("done.\n");
    844 #endif
    845 
    846 	/* disable power down counter in watch dog,
    847 	   This must be done within 16 seconds of start-up. */
    848 	ioreg16_write(NETWALKER_WDOG_VBASE + IMX_WDOG_WMCR, 0);
    849 
    850 #ifdef IPKDB
    851 	/* Initialise ipkdb */
    852 	ipkdb_init();
    853 	if (boothowto & RB_KDB)
    854 		ipkdb_connect(0);
    855 #endif
    856 
    857 #ifdef KGDB
    858 	if (boothowto & RB_KDB) {
    859 		kgdb_debug_init = 1;
    860 		kgdb_connect(1);
    861 	}
    862 #endif
    863 
    864 #ifdef DDB
    865 #ifdef VERBOSE_INIT_ARM
    866 	printf("ddb ");
    867 #endif
    868 	db_machine_init();
    869 
    870 	/* Firmware doesn't load symbols. */
    871 	ddb_init(0, NULL, NULL);
    872 
    873 	if (boothowto & RB_KDB)
    874 		Debugger();
    875 #endif
    876 
    877 
    878 
    879 	printf("initarm done.\n");
    880 
    881 	/* We return the new stack pointer address */
    882 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    883 }
    884 
    885 #if 0
    886 void
    887 process_kernel_args(char *args)
    888 {
    889 
    890 	boothowto = 0;
    891 
    892 	/* Make a local copy of the bootargs */
    893 	strncpy(bootargs, args, MAX_BOOT_STRING);
    894 
    895 	args = bootargs;
    896 	boot_file = bootargs;
    897 
    898 	/* Skip the kernel image filename */
    899 	while (*args != ' ' && *args != 0)
    900 		++args;
    901 
    902 	if (*args != 0)
    903 		*args++ = 0;
    904 
    905 	while (*args == ' ')
    906 		++args;
    907 
    908 	boot_args = args;
    909 
    910 	printf("bootfile: %s\n", boot_file);
    911 	printf("bootargs: %s\n", boot_args);
    912 
    913 	parse_mi_bootargs(boot_args);
    914 }
    915 #endif
    916 
    917 static void
    918 init_clocks(void)
    919 {
    920 	extern void cortexa8_pmc_ccnt_init(void);
    921 
    922 	cortexa8_pmc_ccnt_init();
    923 }
    924 
    925 struct iomux_setup {
    926 	size_t  	pad_ctl_reg;
    927 	uint32_t	pad_ctl_val;
    928 	size_t  	mux_ctl_reg;
    929 	uint32_t	mux_ctl_val;
    930 };
    931 
    932 #define	IOMUX_DATA(padname, mux, pad)	\
    933 	IOMUX_DATA2(__CONCAT(IOMUXC_SW_MUX_CTL_PAD_,padname), mux, \
    934 		    __CONCAT(IOMUXC_SW_PAD_CTL_PAD_,padname), pad)
    935 
    936 
    937 #define	IOMUX_DATA2(muxreg, muxval, padreg, padval)	\
    938 	{						\
    939 		.pad_ctl_reg = (padreg),		\
    940 		.pad_ctl_val = (padval),		\
    941 		.mux_ctl_reg = (muxreg),		\
    942 		.mux_ctl_val = (muxval)			\
    943 	}
    944 
    945 
    946 const struct iomux_setup iomux_setup_data[] = {
    947 
    948 	/* left buttons */
    949 	IOMUX_DATA(EIM_EB2, IOMUX_CONFIG_ALT1,
    950 		   PAD_CTL_HYS_ENABLE),
    951 	/* right buttons */
    952 	IOMUX_DATA(EIM_EB3, IOMUX_CONFIG_ALT1,
    953 		   PAD_CTL_HYS_ENABLE),
    954 
    955 
    956 #if 0
    957 	/* UART1 */
    958 	IOMUX_DATA(UART1_RXD, IOMUX_CONFIG_ALT0,
    959 		   (PAD_CTL_HYS_ENABLE | PAD_CTL_PKE_ENABLE |
    960 		    PAD_CTL_PUE_PULL	 | PAD_CTL_DSE_HIGH   |
    961 		    PAD_CTL_SRE_FAST)),
    962 	IOMUX_DATA(UART1_TXD, IOMUX_CONFIG_ALT0,
    963 		   (PAD_CTL_HYS_ENABLE | PAD_CTL_PKE_ENABLE |
    964 		    PAD_CTL_PUE_PULL	 | PAD_CTL_DSE_HIGH   |
    965 		    PAD_CTL_SRE_FAST)),
    966 	IOMUX_DATA(UART1_RTS, IOMUX_CONFIG_ALT0,
    967 		   (PAD_CTL_HYS_ENABLE | PAD_CTL_PKE_ENABLE |
    968 		    PAD_CTL_PUE_PULL	 | PAD_CTL_DSE_HIGH)),
    969 	IOMUX_DATA(UART1_CTS, IOMUX_CONFIG_ALT0,
    970 		   (PAD_CTL_HYS_ENABLE | PAD_CTL_PKE_ENABLE |
    971 		    PAD_CTL_PUE_PULL	 | PAD_CTL_DSE_HIGH)),
    972 #else
    973 	/* UART1 */
    974 #if 1
    975 	IOMUX_DATA(UART1_RXD, IOMUX_CONFIG_ALT0,
    976 		   PAD_CTL_DSE_HIGH | PAD_CTL_SRE_FAST),
    977 #else
    978 	IOMUX_DATA(UART1_RXD, IOMUX_CONFIG_ALT3,	/* gpio4[28] */
    979 		   PAD_CTL_DSE_HIGH | PAD_CTL_SRE_FAST),
    980 #endif
    981 	IOMUX_DATA(UART1_TXD, IOMUX_CONFIG_ALT0,
    982 		   PAD_CTL_DSE_HIGH | PAD_CTL_SRE_FAST),
    983 	IOMUX_DATA(UART1_RTS, IOMUX_CONFIG_ALT0,
    984 		   PAD_CTL_DSE_HIGH),
    985 	IOMUX_DATA(UART1_CTS, IOMUX_CONFIG_ALT0,
    986 		   PAD_CTL_DSE_HIGH),
    987 #endif
    988 
    989 };
    990 
    991 static void
    992 setup_ioports(void)
    993 {
    994 	int i;
    995 	const struct iomux_setup *p;
    996 
    997 #if 0	/* These are all done already by Netwalker's bootloader. */
    998 	/* set IO multiplexor for UART1 */
    999 	uint32_t reg;
   1000 	uint32_t addr;
   1001 
   1002 	/* input */
   1003 	addr = NETWALKER_IOMUXC_VBASE + MUX_IN_UART1_IPP_UART_RXD_MUX;
   1004 	reg = INPUT_DAISY_0;
   1005 	ioreg_write(addr, reg);
   1006 	addr = NETWALKER_IOMUXC_VBASE + MUX_IN_UART1_IPP_UART_RTS_B;
   1007 	reg = INPUT_DAISY_0;
   1008 	ioreg_write(addr, reg);
   1009 #endif
   1010 
   1011 	for (i=0; i < __arraycount(iomux_setup_data); ++i) {
   1012 		p = iomux_setup_data + i;
   1013 
   1014 		ioreg_write(NETWALKER_IOMUXC_VBASE +
   1015 			    p->pad_ctl_reg,
   1016 			    p->pad_ctl_val);
   1017 		ioreg_write(NETWALKER_IOMUXC_VBASE +
   1018 			    p->mux_ctl_reg,
   1019 			    p->mux_ctl_val);
   1020 	}
   1021 
   1022 
   1023 #if 0	/* already done by bootloader */
   1024 	/* GPIO2[22,23]: input (left/right button)
   1025 	   GPIO2[21]: input (power button) */
   1026 	ioreg_write(NETWALKER_GPIO_VBASE(2) + GPIO_DIR,
   1027 		    ~__BITS(21,23) &
   1028 		    ioreg_read(NETWALKER_GPIO_VBASE(2) + GPIO_DIR));
   1029 #endif
   1030 
   1031 #if 0	/* already done by bootloader */
   1032 	/* GPIO4[12]: input  (cover switch) */
   1033 	ioreg_write(NETWALKER_GPIO_VBASE(4) + GPIO_DIR,
   1034 		    ~__BIT(12) &
   1035 		    ioreg_read(NETWALKER_GPIO_VBASE(4) + GPIO_DIR));
   1036 #endif
   1037 }
   1038 
   1039 
   1040 #ifdef	CONSDEVNAME
   1041 const char consdevname[] = CONSDEVNAME;
   1042 
   1043 #ifndef	CONMODE
   1044 #define	CONMODE	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
   1045 #endif
   1046 #ifndef	CONSPEED
   1047 #define	CONSPEED	115200
   1048 #endif
   1049 
   1050 int consmode = CONMODE;
   1051 int consrate = CONSPEED;
   1052 
   1053 #endif	/* CONSDEVNAME */
   1054 
   1055 #ifndef	IMXUART_FREQ
   1056 #define	IMXUART_FREQ	66355200
   1057 #endif
   1058 
   1059 void
   1060 consinit(void)
   1061 {
   1062 	static int consinit_called = 0;
   1063 
   1064 	if (consinit_called)
   1065 		return;
   1066 
   1067 	consinit_called = 1;
   1068 
   1069 #ifdef	CONSDEVNAME
   1070 
   1071 #if NIMXUART > 0
   1072 	imxuart_set_frequency(IMXUART_FREQ, 2);
   1073 #endif
   1074 
   1075 #if (NIMXUART > 0) && defined(IMXUARTCONSOLE)
   1076 	if (strcmp(consdevname, "imxuart") == 0) {
   1077 		paddr_t consaddr;
   1078 #ifdef	CONADDR
   1079 		consaddr = CONADDR;
   1080 #else
   1081 		consaddr = IMX51_UART1_BASE;
   1082 #endif
   1083 		imxuart_cons_attach(&imx_bs_tag, consaddr, consrate, consmode);
   1084 	    return;
   1085 	}
   1086 #endif
   1087 
   1088 #endif
   1089 
   1090 #if (NWSDISPLAY > 0) && defined(IMXLCDCONSOLE)
   1091 	{
   1092 		extern void netwalker_cnattach(void);
   1093 		netwalker_cnattach();
   1094 	}
   1095 #endif
   1096 }
   1097 
   1098 #ifdef KGDB
   1099 #ifndef KGDB_DEVNAME
   1100 #define KGDB_DEVNAME "imxuart"
   1101 #endif
   1102 #ifndef KGDB_DEVMODE
   1103 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
   1104 #endif
   1105 
   1106 const char kgdb_devname[20] = KGDB_DEVNAME;
   1107 int kgdb_mode = KGDB_DEVMODE;
   1108 int kgdb_addr = KGDB_DEVADDR;
   1109 extern int kgdb_rate;	/* defined in kgdb_stub.c */
   1110 
   1111 void
   1112 kgdb_port_init(void)
   1113 {
   1114 #if (NIMXUART > 0)
   1115 	if (strcmp(kgdb_devname, "imxuart") == 0) {
   1116 		imxuart_kgdb_attach(&imx_bs_tag, kgdb_addr,
   1117 		kgdb_rate, kgdb_mode);
   1118 	    return;
   1119 	}
   1120 
   1121 #endif
   1122 }
   1123 #endif
   1124 
   1125 
   1126 #ifdef DEBUG_IOPORTS
   1127 static void dump_sub(paddr_t addr, size_t size)
   1128 {
   1129 	paddr_t end = addr + size;
   1130 
   1131 	for (; addr < end; addr += 4) {
   1132 		if (addr % 16 == 0)
   1133 			printf("%08x: ", (u_int)addr);
   1134 		printf("%08x ", ioreg_read(addr));
   1135 
   1136 		if (addr % 16 == 12)
   1137 			printf("\n");
   1138 	}
   1139 	printf("\n");
   1140 }
   1141 
   1142 void
   1143 dump_registers(void)
   1144 {
   1145 	paddr_t pa;
   1146 	int i;
   1147 
   1148 	dump_sub(IOMUXC_BASE, IOMUXC_USBOH3_IPP_IND_UH3_STP_SELECT_INPUT + 4);
   1149 
   1150 	for (i = 1; i <= 4; ++i) {
   1151 		dump_sub(GPIO_BASE(i), GPIO_SIZE);
   1152 	}
   1153 
   1154 	printf("\nwatchdog: ");
   1155 	for (pa = WDOG1_BASE; pa <= WDOG1_BASE + IMX_WDOG_WMCR;
   1156 	     pa += 2) {
   1157 		printf("%04x ", *(volatile uint16_t *)pa);
   1158 	}
   1159 	printf("\n");
   1160 
   1161 #if 0
   1162 	/* disable power down counter in watch dog,
   1163 	   This must be done within 16 seconds of start-up. */
   1164 	ioreg16_write(NETWALKER_WDOG_VBASE + IMX_WDOG_WMCR, 0);
   1165 
   1166 	/* read left/right buttons */
   1167 	for (;;) {
   1168 		uint32_t reg;
   1169 
   1170 		reg = ioreg_read(GPIO_BASE(2) + GPIO_DR);
   1171 		printf("\r%08x", reg);
   1172 		reg = ioreg_read(GPIO_BASE(4) + GPIO_DR);
   1173 		printf("  %08x", reg);
   1174 
   1175 #if 0
   1176 		ioreg16_write(WDOG1_BASE + IMX_WDOG_WSR, WSR_MAGIC1);
   1177 		ioreg16_write(WDOG1_BASE + IMX_WDOG_WSR, WSR_MAGIC2);
   1178 #endif
   1179 
   1180 	}
   1181 #endif
   1182 
   1183 }
   1184 #endif
   1185