xref: /netbsd-src/sys/arch/evbarm/imx31/imx31lk_machdep.c (revision 6dffe8d42bd46273f674d7ab834e7be9b1af990e)
1 /* $NetBSD: imx31lk_machdep.c,v 1.4 2008/11/11 06:46:41 dyoung Exp $ */
2 
3 /*
4  * Startup routines for the ZOOM iMX31 LITEKIT.
5  * Below you can trace the increasingly impressive lineage ;)
6  */
7 
8 /*
9  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
10  * Written by Hiroyuki Bessho for Genetec Corporation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of Genetec Corporation may not be used to endorse or
21  *    promote products derived from this software without specific prior
22  *    written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Machine dependant functions for kernel setup for
37  * Intel DBPXA250 evaluation board (a.k.a. Lubbock).
38  * Based on iq80310_machhdep.c
39  */
40 /*
41  * Copyright (c) 2001 Wasabi Systems, Inc.
42  * All rights reserved.
43  *
44  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed for the NetBSD Project by
57  *	Wasabi Systems, Inc.
58  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
59  *    or promote products derived from this software without specific prior
60  *    written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
64  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
65  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
66  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72  * POSSIBILITY OF SUCH DAMAGE.
73  */
74 
75 /*
76  * Copyright (c) 1997,1998 Mark Brinicombe.
77  * Copyright (c) 1997,1998 Causality Limited.
78  * All rights reserved.
79  *
80  * Redistribution and use in source and binary forms, with or without
81  * modification, are permitted provided that the following conditions
82  * are met:
83  * 1. Redistributions of source code must retain the above copyright
84  *    notice, this list of conditions and the following disclaimer.
85  * 2. Redistributions in binary form must reproduce the above copyright
86  *    notice, this list of conditions and the following disclaimer in the
87  *    documentation and/or other materials provided with the distribution.
88  * 3. All advertising materials mentioning features or use of this software
89  *    must display the following acknowledgement:
90  *	This product includes software developed by Mark Brinicombe
91  *	for the NetBSD Project.
92  * 4. The name of the company nor the name of the author may be used to
93  *    endorse or promote products derived from this software without specific
94  *    prior written permission.
95  *
96  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
97  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
98  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
99  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
100  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
101  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
102  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
104  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
105  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106  * SUCH DAMAGE.
107  *
108  * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
109  * boards using RedBoot firmware.
110  */
111 
112 #include <sys/cdefs.h>
113 __KERNEL_RCSID(0, "$NetBSD: imx31lk_machdep.c,v 1.4 2008/11/11 06:46:41 dyoung Exp $");
114 
115 #include "opt_ddb.h"
116 #include "opt_kgdb.h"
117 #include "opt_ipkdb.h"
118 #include "opt_pmap_debug.h"
119 #include "opt_md.h"
120 #include "opt_com.h"
121 #include "md.h"
122 
123 #include <sys/param.h>
124 #include <sys/device.h>
125 #include <sys/systm.h>
126 #include <sys/kernel.h>
127 #include <sys/exec.h>
128 #include <sys/proc.h>
129 #include <sys/msgbuf.h>
130 #include <sys/reboot.h>
131 #include <sys/termios.h>
132 #include <sys/ksyms.h>
133 
134 #include <uvm/uvm_extern.h>
135 
136 #include <sys/conf.h>
137 #include <dev/cons.h>
138 #include <dev/md.h>
139 
140 #include <machine/db_machdep.h>
141 #include <ddb/db_sym.h>
142 #include <ddb/db_extern.h>
143 #ifdef KGDB
144 #include <sys/kgdb.h>
145 #endif
146 
147 #include <machine/bootconfig.h>
148 #include <machine/bus.h>
149 #include <machine/cpu.h>
150 #include <machine/frame.h>
151 #include <arm/undefined.h>
152 
153 #include <arm/arm32/pte.h>
154 #include <arm/arm32/machdep.h>
155 
156 #include <arm/imx/imxuartreg.h>
157 #include <arm/imx/imxuartvar.h>
158 #include <evbarm/imx31/imx31lk_reg.h>
159 
160 /* Kernel text starts 1MB in from the bottom of the kernel address space. */
161 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00100000)
162 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
163 
164 /*
165  * The range 0x81000000 - 0x8cffffff is available for kernel VM space
166  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
167  */
168 #define KERNEL_VM_SIZE		0x0C000000
169 
170 
171 /*
172  * Address to call from cpu_reset() to reset the machine.
173  * This is machine architecture dependant as it varies depending
174  * on where the ROM appears when you turn the MMU off.
175  */
176 
177 u_int cpu_reset_address = 0;
178 
179 /* Define various stack sizes in pages */
180 #define IRQ_STACK_SIZE	1
181 #define ABT_STACK_SIZE	1
182 #ifdef IPKDB
183 #define UND_STACK_SIZE	2
184 #else
185 #define UND_STACK_SIZE	1
186 #endif
187 
188 BootConfig bootconfig;		/* Boot config storage */
189 char *boot_args = NULL;
190 char *boot_file = NULL;
191 
192 vm_offset_t physical_start;
193 vm_offset_t physical_freestart;
194 vm_offset_t physical_freeend;
195 vm_offset_t physical_end;
196 u_int free_pages;
197 vm_offset_t pagetables_start;
198 int physmem = 0;
199 
200 /*int debug_flags;*/
201 #ifndef PMAP_STATIC_L1S
202 int max_processes = 64;			/* Default number */
203 #endif	/* !PMAP_STATIC_L1S */
204 
205 /* Physical and virtual addresses for some global pages */
206 pv_addr_t irqstack;
207 pv_addr_t undstack;
208 pv_addr_t abtstack;
209 pv_addr_t kernelstack;
210 
211 vm_offset_t msgbufphys;
212 
213 extern u_int data_abort_handler_address;
214 extern u_int prefetch_abort_handler_address;
215 extern u_int undefined_handler_address;
216 
217 #ifdef PMAP_DEBUG
218 extern int pmap_debug_level;
219 #endif
220 
221 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
222 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
223 #define	KERNEL_PT_KERNEL_NUM	4
224 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
225 				        /* Page tables for mapping kernel VM */
226 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
227 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
228 
229 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
230 
231 struct user *proc0paddr;
232 
233 /* Prototypes */
234 
235 #if 0
236 void	process_kernel_args(char *);
237 #endif
238 
239 void	imx31lk_consinit(int);
240 void	kgdb_port_init(void);
241 void	change_clock(uint32_t v);
242 
243 bs_protos(bs_notimpl);
244 
245 #include "com.h"
246 #if NCOM > 0
247 #include <dev/ic/comreg.h>
248 #include <dev/ic/comvar.h>
249 #endif
250 
251 #ifndef CONSPEED
252 #define CONSPEED B115200	/* What RedBoot uses */
253 #endif
254 #ifndef CONMODE
255 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
256 #endif
257 
258 int comcnspeed = CONSPEED;
259 int comcnmode = CONMODE;
260 
261 /*
262  * void cpu_reboot(int howto, char *bootstr)
263  *
264  * Reboots the system
265  *
266  * Deal with any syncing, unmounting, dumping and shutdown hooks,
267  * then reset the CPU.
268  */
269 void
270 cpu_reboot(int howto, char *bootstr)
271 {
272 #ifdef DIAGNOSTIC
273 	/* info */
274 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
275 #endif
276 
277 	/*
278 	 * If we are still cold then hit the air brakes
279 	 * and crash to earth fast
280 	 */
281 	if (cold) {
282 		doshutdownhooks();
283 		pmf_system_shutdown(boothowto);
284 		printf("The operating system has halted.\n");
285 		printf("Please press any key to reboot.\n\n");
286 		cngetc();
287 		printf("rebooting...\n");
288 		cpu_reset();
289 		/*NOTREACHED*/
290 	}
291 
292 	/* Disable console buffering */
293 /*	cnpollc(1);*/
294 
295 	/*
296 	 * If RB_NOSYNC was not specified sync the discs.
297 	 * Note: Unless cold is set to 1 here, syslogd will die during the
298 	 * unmount.  It looks like syslogd is getting woken up only to find
299 	 * that it cannot page part of the binary in as the filesystem has
300 	 * been unmounted.
301 	 */
302 	if (!(howto & RB_NOSYNC))
303 		bootsync();
304 
305 	/* Say NO to interrupts */
306 	splhigh();
307 
308 	/* Do a dump if requested. */
309 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
310 		dumpsys();
311 
312 	/* Run any shutdown hooks */
313 	doshutdownhooks();
314 
315 	pmf_system_shutdown(boothowto);
316 
317 	/* Make sure IRQ's are disabled */
318 	IRQdisable;
319 
320 	if (howto & RB_HALT) {
321 		printf("The operating system has halted.\n");
322 		printf("Please press any key to reboot.\n\n");
323 		cngetc();
324 	}
325 
326 	printf("rebooting...\n");
327 	cpu_reset();
328 	/*NOTREACHED*/
329 }
330 
331 /*
332  * Static device mappings. These peripheral registers are mapped at
333  * fixed virtual addresses very early in imx31lk_start() so that we
334  * can use them while booting the kernel, and stay at the same address
335  * throughout whole kernel's life time.
336  *
337  * We use this table twice; once with bootstrap page table, and once
338  * with kernel's page table which we build up in initarm().
339  */
340 
341 #define _A(a)   ((a) & ~L1_S_OFFSET)
342 #define _S(s)   (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
343 
344 static const struct pmap_devmap imx31lk_devmap[] = {
345     {
346 	IMX31LITEKIT_UART1_VBASE,
347 	_A(IMX_UART1_BASE),
348 	_S(L1_S_SIZE),
349 	VM_PROT_READ|VM_PROT_WRITE,
350 	PTE_NOCACHE,
351     },
352 	{0, 0, 0, 0, 0 }
353 };
354 
355 #ifndef MEMSTART
356 #define MEMSTART	0x80000000
357 #endif
358 #ifndef MEMSIZE
359 #define MEMSIZE		0x8000000
360 #endif
361 
362 /*
363  * u_int initarm(...)
364  *
365  * Initial entry point on startup. This gets called before main() is
366  * entered.
367  * It should be responsible for setting up everything that must be
368  * in place when main is called.
369  * This includes
370  *   Taking a copy of the boot configuration structure.
371  *   Initialising the physical console so characters can be printed.
372  *   Setting up page tables for the kernel
373  *   Relocating the kernel to the bottom of physical memory
374  */
375 u_int
376 initarm(void *arg)
377 {
378 	int loop;
379 	int loop1;
380 	vaddr_t l1pagetable;
381 
382 	disable_interrupts(I32_bit|F32_bit);
383 		/* XXX move to imx31lk_start.S */
384 
385 	/* Register devmap for devices we mapped in start */
386 	pmap_devmap_register(imx31lk_devmap);
387 
388 #ifdef NOTYET
389 	/* start 32.768 kHz OSC */
390 	ioreg_write(VIPER_CLKMAN_VBASE + 0x08, 2);
391 	/* Get ready for splfoo() */
392 	imx31_intr_bootstrap(IMX31_INTCTL_VBASE);
393 #endif
394 
395 	/*
396 	 * Heads up ... Setup the CPU / MMU / TLB functions
397 	 */
398 	if (set_cpufuncs())
399 		panic("cpu not recognized!");
400 
401 #if 0
402 	/* Calibrate the delay loop. */
403 #endif
404 
405 	imx31lk_consinit(1);
406 
407 #ifdef KGDB
408 	kgdb_port_init();
409 #endif
410 	/* Talk to the user */
411 	printf("\nNetBSD/evbarm (imx31lk) booting ...\n");
412 
413 #if 0
414 	/*
415 	 * Examine the boot args string for options we need to know about
416 	 * now.
417 	 */
418 	process_kernel_args((char *)nwbootinfo.bt_args);
419 #endif
420 
421 	printf("initarm: Configuring system ...\n");
422 
423 	/* Fake bootconfig structure for the benefit of pmap.c */
424 	/* XXX must make the memory description h/w independent */
425 	bootconfig.dramblocks = 1;
426 	bootconfig.dram[0].address = MEMSTART;
427 	bootconfig.dram[0].pages = MEMSIZE / PAGE_SIZE;
428 
429 	/*
430 	 * Set up the variables that define the availablilty of
431 	 * physical memory.  For now, we're going to set
432 	 * physical_freeend to 0x80100000UL (where the kernel
433 	 * was loaded) and allocate the memory we need downwards.
434 	 * If we get too close to the page tables that LoLo
435 	 * set up, we will panic.  We will update physical_freestart
436 	 * and physical_freeend later to reflect what pmap_bootstrap()
437 	 * wants to see.
438 	 *
439 	 * XXX pmap_bootstrap() needs an enema.
440 	 * (now that would be truly hardcore XXX)
441 	 */
442 	physical_start = bootconfig.dram[0].address;
443 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
444 
445 	physical_freestart = 0x800c0000UL;	/* top of LoLo */
446 	physical_freeend =   0x80100000UL;	/* base of kernel */
447 
448 	physmem = (physical_end - physical_start) / PAGE_SIZE;
449 
450 #ifdef VERBOSE_INIT_ARM
451 	/* Tell the user about the memory */
452 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
453 	    physical_start, physical_end - 1);
454 #endif
455 
456 	/*
457 	 * Okay, the kernel starts 1MB in from the bottom of physical
458 	 * memory.  We are going to allocate our bootstrap pages downwards
459 	 * from there.
460 	 *
461 	 * We need to allocate some fixed page tables to get the kernel
462 	 * going.  We allocate one page directory and a number of page
463 	 * tables and store the physical addresses in the kernel_pt_table
464 	 * array.
465 	 *
466 	 * The kernel page directory must be on a 16K boundary.  The page
467 	 * tables must be on 4K boundaries.  What we do is allocate the
468 	 * page directory on the first 16K boundary that we encounter, and
469 	 * the page tables on 4K boundaries otherwise.  Since we allocate
470 	 * at least 3 L2 page tables, we are guaranteed to encounter at
471 	 * least one 16K aligned region.
472 	 */
473 
474 #ifdef VERBOSE_INIT_ARM
475 	printf("Allocating page tables\n");
476 #endif
477 
478 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
479 
480 #ifdef VERBOSE_INIT_ARM
481 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
482 	       physical_freestart, free_pages, free_pages);
483 #endif
484 
485 	/* Define a macro to simplify memory allocation */
486 #define	valloc_pages(var, np)				\
487 	alloc_pages((var).pv_pa, (np));			\
488 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
489 
490 #define alloc_pages(var, np)				\
491 	physical_freeend -= ((np) * PAGE_SIZE);		\
492 	if (physical_freeend < physical_freestart)	\
493 		panic("initarm: out of memory");	\
494 	(var) = physical_freeend;			\
495 	free_pages -= (np);				\
496 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
497 
498 	loop1 = 0;
499 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
500 		/* Are we 16KB aligned for an L1 ? */
501 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
502 		    && kernel_l1pt.pv_pa == 0) {
503 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
504 		} else {
505 			valloc_pages(kernel_pt_table[loop1],
506 			    L2_TABLE_SIZE / PAGE_SIZE);
507 			++loop1;
508 		}
509 	}
510 
511 	/* This should never be able to happen but better confirm that. */
512 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
513 		panic("initarm: Failed to align the kernel page directory");
514 
515 	/*
516 	 * Allocate a page for the system page mapped to V0x00000000
517 	 * This page will just contain the system vectors and can be
518 	 * shared by all processes.
519 	 */
520 	alloc_pages(systempage.pv_pa, 1);
521 
522 	/* Allocate stacks for all modes */
523 	valloc_pages(irqstack, IRQ_STACK_SIZE);
524 	valloc_pages(abtstack, ABT_STACK_SIZE);
525 	valloc_pages(undstack, UND_STACK_SIZE);
526 	valloc_pages(kernelstack, UPAGES);
527 
528 #ifdef VERBOSE_INIT_ARM
529 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
530 	    irqstack.pv_va);
531 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
532 	    abtstack.pv_va);
533 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
534 	    undstack.pv_va);
535 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
536 	    kernelstack.pv_va);
537 #endif
538 
539 	/*
540 	 * XXX Defer this to later so that we can reclaim the memory
541 	 * XXX used by the LoLo page tables.
542 	 */
543 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
544 
545 	/*
546 	 * Ok we have allocated physical pages for the primary kernel
547 	 * page tables
548 	 */
549 
550 #ifdef VERBOSE_INIT_ARM
551 	printf("Creating L1 page table at p0x%08lx v0x%08lx\n",
552 		kernel_l1pt.pv_pa, kernel_l1pt.pv_va);
553 #endif
554 
555 	/*
556 	 * Now we start construction of the L1 page table
557 	 * We start by mapping the L2 page tables into the L1.
558 	 * This means that we can replace L1 mappings later on if necessary
559 	 */
560 	l1pagetable = kernel_l1pt.pv_pa;
561 
562 	/* Map the L2 pages tables in the L1 page table */
563 	pmap_link_l2pt(l1pagetable, 0x00000000,
564 	    &kernel_pt_table[KERNEL_PT_SYS]);
565 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
566 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
567 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
568 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
569 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
570 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
571 
572 	/* update the top of the kernel VM */
573 	pmap_curmaxkvaddr =
574 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
575 
576 #ifdef VERBOSE_INIT_ARM
577 	printf("Mapping kernel\n");
578 #endif
579 
580 	/* Now we fill in the L2 pagetable for the kernel static code/data */
581 #define round_L_page(x) (((x) + L2_L_OFFSET) & L2_L_FRAME)
582 	{
583 		extern char etext[], _end[];
584 		size_t textsize = round_L_page((size_t)etext - KERNEL_TEXT_BASE);
585 		size_t totalsize = round_L_page((size_t)_end - KERNEL_TEXT_BASE);
586 		u_int logical;
587 
588 
589 printf("%s: etext %lx, _end %lx\n",
590 	__func__, (uintptr_t)etext, (uintptr_t)_end);
591 printf("%s: textsize %#lx, totalsize %#lx\n",
592 	__func__, textsize, totalsize);
593 
594 		logical = 0x00100000;	/* offset of kernel in RAM */
595 
596 		/* Map text section read-only. */
597 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
598 		    physical_start + logical, textsize,
599 		    VM_PROT_READ|VM_PROT_EXECUTE, PTE_CACHE);
600 
601 		/* Map data and bss sections read-write. */
602 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
603 		    physical_start + logical, totalsize - textsize,
604 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
605 	}
606 
607 #ifdef VERBOSE_INIT_ARM
608 	printf("Constructing L2 page tables\n");
609 #endif
610 
611 	/* Map the stack pages */
612 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
613 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
614 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
615 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
616 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
617 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
618 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
619 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
620 
621 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
622 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
623 
624 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
625 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
626 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
627 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
628 	}
629 
630 	/* Map the vector page. */
631 #if 1
632 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
633 	 * cache-clean code there.  */
634 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
635 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
636 #else
637 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
638 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
639 #endif
640 
641 	/*
642 	 * map integrated peripherals at same address in l1pagetable
643 	 * so that we can continue to use console.
644 	 */
645 	pmap_devmap_bootstrap(l1pagetable, imx31lk_devmap);
646 
647 	/*
648 	 * Now we have the real page tables in place so we can switch to them.
649 	 * Once this is done we will be running with the REAL kernel page
650 	 * tables.
651 	 */
652 
653 	/*
654 	 * Update the physical_freestart/physical_freeend/free_pages
655 	 * variables.
656 	 */
657 	{
658 		extern char _end[];
659 
660 		physical_freestart = physical_start +
661 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
662 		     KERNEL_BASE);
663 		physical_freeend = physical_end;
664 		free_pages =
665 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
666 	}
667 
668 	/* Switch tables */
669 #ifdef VERBOSE_INIT_ARM
670 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
671 	       physical_freestart, free_pages, free_pages);
672 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
673 #endif
674 
675 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
676 	setttb(kernel_l1pt.pv_pa);
677 	cpu_tlb_flushID();
678 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
679 	imx31lk_consinit(2);
680 
681 	/*
682 	 * Moved from cpu_startup() as data_abort_handler() references
683 	 * this during uvm init
684 	 */
685 	proc0paddr = (struct user *)kernelstack.pv_va;
686 	lwp0.l_addr = proc0paddr;
687 
688 #ifdef VERBOSE_INIT_ARM
689 	printf("bootstrap done.\n");
690 #endif
691 
692 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
693 
694 	/*
695 	 * Pages were allocated during the secondary bootstrap for the
696 	 * stacks for different CPU modes.
697 	 * We must now set the r13 registers in the different CPU modes to
698 	 * point to these stacks.
699 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
700 	 * of the stack memory.
701 	 */
702 	printf("init subsystems: stacks ");
703 
704 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
705 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
706 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
707 
708 	/*
709 	 * Well we should set a data abort handler.
710 	 * Once things get going this will change as we will need a proper
711 	 * handler.
712 	 * Until then we will use a handler that just panics but tells us
713 	 * why.
714 	 * Initialisation of the vectors will just panic on a data abort.
715 	 * This just fills in a slightly better one.
716 	 */
717 	printf("vectors ");
718 	data_abort_handler_address = (u_int)data_abort_handler;
719 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
720 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
721 
722 	/* Initialise the undefined instruction handlers */
723 	printf("undefined ");
724 	undefined_init();
725 
726 	/* Load memory into UVM. */
727 	printf("page ");
728 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
729 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
730 	    atop(physical_freestart), atop(physical_freeend),
731 	    VM_FREELIST_DEFAULT);
732 
733 	/* Boot strap pmap telling it where the kernel page table is */
734 	printf("pmap ");
735 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
736 
737 #ifdef __HAVE_MEMORY_DISK__
738 	md_root_setconf(memory_disk, sizeof memory_disk);
739 #endif
740 
741 #ifdef IPKDB
742 	/* Initialise ipkdb */
743 	ipkdb_init();
744 	if (boothowto & RB_KDB)
745 		ipkdb_connect(0);
746 #endif
747 
748 #ifdef KGDB
749 	if (boothowto & RB_KDB) {
750 		kgdb_debug_init = 1;
751 		kgdb_connect(1);
752 	}
753 #endif
754 
755 #ifdef DDB
756 	printf("ddb ");
757 	db_machine_init();
758 
759 	/* Firmware doesn't load symbols. */
760 	ddb_init(0, NULL, NULL);
761 
762 	if (boothowto & RB_KDB)
763 		Debugger();
764 #endif
765 	/* We return the new stack pointer address */
766 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
767 }
768 
769 #if 0
770 void
771 process_kernel_args(char *args)
772 {
773 
774 	boothowto = 0;
775 
776 	/* Make a local copy of the bootargs */
777 	strncpy(bootargs, args, MAX_BOOT_STRING);
778 
779 	args = bootargs;
780 	boot_file = bootargs;
781 
782 	/* Skip the kernel image filename */
783 	while (*args != ' ' && *args != 0)
784 		++args;
785 
786 	if (*args != 0)
787 		*args++ = 0;
788 
789 	while (*args == ' ')
790 		++args;
791 
792 	boot_args = args;
793 
794 	printf("bootfile: %s\n", boot_file);
795 	printf("bootargs: %s\n", boot_args);
796 
797 	parse_mi_bootargs(boot_args);
798 }
799 #endif
800 
801 #ifdef KGDB
802 #ifndef KGDB_DEVNAME
803 #define KGDB_DEVNAME "ffuart"
804 #endif
805 const char kgdb_devname[] = KGDB_DEVNAME;
806 
807 #if (NCOM > 0)
808 #ifndef KGDB_DEVMODE
809 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
810 #endif
811 int comkgdbmode = KGDB_DEVMODE;
812 #endif /* NCOM */
813 
814 #endif /* KGDB */
815 
816 
817 void
818 imx31lk_consinit(int phase)
819 {
820 	static int ophase = 0;
821 	intptr_t bh;
822 
823 	if (ophase != phase) {
824 		ophase = phase;
825 		switch (phase) {
826 		case 1:
827 			imxuart_init(0, IMX_UART1_BASE);
828 			break;
829 		case 2:
830 			bh = IMX31LITEKIT_UART1_VBASE;
831 			bh |= (IMX_UART1_BASE & ~_A(IMX_UART1_BASE));
832 			imxuart_init(0, bh);
833 			break;
834 		}
835 	}
836 }
837 
838 void
839 consinit(void)
840 {
841 	imx31lk_consinit(2);
842 }
843 
844 void consinit_test(void);
845 void
846 consinit_test(void)
847 {
848 	imxuart_softc_t *sc, softc;
849 	extern int imxuart_puts(imxuart_softc_t *sc, const char *s);
850 
851 	printf("\n%s start\n", __func__);
852 	sc = &softc;
853 	sc->sc_init_cnt = 0;
854 	imxuart_init(sc, IMX_UART1_BASE);
855 	imxuart_puts(sc, "test1\r\n");
856 	imxuart_init(sc,
857 	    IMX31LITEKIT_UART1_VBASE|(IMX_UART1_BASE & ~_A(IMX_UART1_BASE)));
858 	imxuart_puts(sc, "test2\r\n");
859 	printf("%s done\n", __func__);
860 }
861 
862 #ifdef KGDB
863 void
864 kgdb_port_init(void)
865 {
866 #if (NCOM > 0) && defined(COM_PXA2X0)
867 	paddr_t paddr = 0;
868 	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
869 
870 	if (0 == strcmp(kgdb_devname, "ffuart")) {
871 		paddr = PXA2X0_FFUART_BASE;
872 		ckenreg |= CKEN_FFUART;
873 	}
874 	else if (0 == strcmp(kgdb_devname, "btuart")) {
875 		paddr = PXA2X0_BTUART_BASE;
876 		ckenreg |= CKEN_BTUART;
877 	}
878 
879 	if (paddr &&
880 	    0 == com_kgdb_attach(&imx31_a4x_bs_tag, paddr,
881 		kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
882 
883 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
884 	}
885 #endif
886 }
887 #endif
888