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