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