xref: /netbsd-src/sys/arch/evbarm/imx31/imx31lk_machdep.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /* $NetBSD: imx31lk_machdep.c,v 1.2 2008/04/27 18:58:46 matt 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.2 2008/04/27 18:58:46 matt 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 		printf("The operating system has halted.\n");
284 		printf("Please press any key to reboot.\n\n");
285 		cngetc();
286 		printf("rebooting...\n");
287 		cpu_reset();
288 		/*NOTREACHED*/
289 	}
290 
291 	/* Disable console buffering */
292 /*	cnpollc(1);*/
293 
294 	/*
295 	 * If RB_NOSYNC was not specified sync the discs.
296 	 * Note: Unless cold is set to 1 here, syslogd will die during the
297 	 * unmount.  It looks like syslogd is getting woken up only to find
298 	 * that it cannot page part of the binary in as the filesystem has
299 	 * been unmounted.
300 	 */
301 	if (!(howto & RB_NOSYNC))
302 		bootsync();
303 
304 	/* Say NO to interrupts */
305 	splhigh();
306 
307 	/* Do a dump if requested. */
308 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
309 		dumpsys();
310 
311 	/* Run any shutdown hooks */
312 	doshutdownhooks();
313 
314 	/* Make sure IRQ's are disabled */
315 	IRQdisable;
316 
317 	if (howto & RB_HALT) {
318 		printf("The operating system has halted.\n");
319 		printf("Please press any key to reboot.\n\n");
320 		cngetc();
321 	}
322 
323 	printf("rebooting...\n");
324 	cpu_reset();
325 	/*NOTREACHED*/
326 }
327 
328 /*
329  * Static device mappings. These peripheral registers are mapped at
330  * fixed virtual addresses very early in imx31lk_start() so that we
331  * can use them while booting the kernel, and stay at the same address
332  * throughout whole kernel's life time.
333  *
334  * We use this table twice; once with bootstrap page table, and once
335  * with kernel's page table which we build up in initarm().
336  */
337 
338 #define _A(a)   ((a) & ~L1_S_OFFSET)
339 #define _S(s)   (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
340 
341 static const struct pmap_devmap imx31lk_devmap[] = {
342     {
343 	IMX31LITEKIT_UART1_VBASE,
344 	_A(IMX_UART1_BASE),
345 	_S(L1_S_SIZE),
346 	VM_PROT_READ|VM_PROT_WRITE,
347 	PTE_NOCACHE,
348     },
349 	{0, 0, 0, 0, 0 }
350 };
351 
352 #ifndef MEMSTART
353 #define MEMSTART	0x80000000
354 #endif
355 #ifndef MEMSIZE
356 #define MEMSIZE		0x8000000
357 #endif
358 
359 /*
360  * u_int initarm(...)
361  *
362  * Initial entry point on startup. This gets called before main() is
363  * entered.
364  * It should be responsible for setting up everything that must be
365  * in place when main is called.
366  * This includes
367  *   Taking a copy of the boot configuration structure.
368  *   Initialising the physical console so characters can be printed.
369  *   Setting up page tables for the kernel
370  *   Relocating the kernel to the bottom of physical memory
371  */
372 u_int
373 initarm(void *arg)
374 {
375 	int loop;
376 	int loop1;
377 	vaddr_t l1pagetable;
378 
379 	disable_interrupts(I32_bit|F32_bit);
380 		/* XXX move to imx31lk_start.S */
381 
382 	/* Register devmap for devices we mapped in start */
383 	pmap_devmap_register(imx31lk_devmap);
384 
385 #ifdef NOTYET
386 	/* start 32.768 kHz OSC */
387 	ioreg_write(VIPER_CLKMAN_VBASE + 0x08, 2);
388 	/* Get ready for splfoo() */
389 	imx31_intr_bootstrap(IMX31_INTCTL_VBASE);
390 #endif
391 
392 	/*
393 	 * Heads up ... Setup the CPU / MMU / TLB functions
394 	 */
395 	if (set_cpufuncs())
396 		panic("cpu not recognized!");
397 
398 #if 0
399 	/* Calibrate the delay loop. */
400 #endif
401 
402 	imx31lk_consinit(1);
403 
404 #ifdef KGDB
405 	kgdb_port_init();
406 #endif
407 	/* Talk to the user */
408 	printf("\nNetBSD/evbarm (imx31lk) booting ...\n");
409 
410 #if 0
411 	/*
412 	 * Examine the boot args string for options we need to know about
413 	 * now.
414 	 */
415 	process_kernel_args((char *)nwbootinfo.bt_args);
416 #endif
417 
418 	printf("initarm: Configuring system ...\n");
419 
420 	/* Fake bootconfig structure for the benefit of pmap.c */
421 	/* XXX must make the memory description h/w independent */
422 	bootconfig.dramblocks = 1;
423 	bootconfig.dram[0].address = MEMSTART;
424 	bootconfig.dram[0].pages = MEMSIZE / PAGE_SIZE;
425 
426 	/*
427 	 * Set up the variables that define the availablilty of
428 	 * physical memory.  For now, we're going to set
429 	 * physical_freeend to 0x80100000UL (where the kernel
430 	 * was loaded) and allocate the memory we need downwards.
431 	 * If we get too close to the page tables that LoLo
432 	 * set up, we will panic.  We will update physical_freestart
433 	 * and physical_freeend later to reflect what pmap_bootstrap()
434 	 * wants to see.
435 	 *
436 	 * XXX pmap_bootstrap() needs an enema.
437 	 * (now that would be truly hardcore XXX)
438 	 */
439 	physical_start = bootconfig.dram[0].address;
440 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
441 
442 	physical_freestart = 0x800c0000UL;	/* top of LoLo */
443 	physical_freeend =   0x80100000UL;	/* base of kernel */
444 
445 	physmem = (physical_end - physical_start) / PAGE_SIZE;
446 
447 #ifdef VERBOSE_INIT_ARM
448 	/* Tell the user about the memory */
449 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
450 	    physical_start, physical_end - 1);
451 #endif
452 
453 	/*
454 	 * Okay, the kernel starts 1MB in from the bottom of physical
455 	 * memory.  We are going to allocate our bootstrap pages downwards
456 	 * from there.
457 	 *
458 	 * We need to allocate some fixed page tables to get the kernel
459 	 * going.  We allocate one page directory and a number of page
460 	 * tables and store the physical addresses in the kernel_pt_table
461 	 * array.
462 	 *
463 	 * The kernel page directory must be on a 16K boundary.  The page
464 	 * tables must be on 4K boundaries.  What we do is allocate the
465 	 * page directory on the first 16K boundary that we encounter, and
466 	 * the page tables on 4K boundaries otherwise.  Since we allocate
467 	 * at least 3 L2 page tables, we are guaranteed to encounter at
468 	 * least one 16K aligned region.
469 	 */
470 
471 #ifdef VERBOSE_INIT_ARM
472 	printf("Allocating page tables\n");
473 #endif
474 
475 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
476 
477 #ifdef VERBOSE_INIT_ARM
478 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
479 	       physical_freestart, free_pages, free_pages);
480 #endif
481 
482 	/* Define a macro to simplify memory allocation */
483 #define	valloc_pages(var, np)				\
484 	alloc_pages((var).pv_pa, (np));			\
485 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
486 
487 #define alloc_pages(var, np)				\
488 	physical_freeend -= ((np) * PAGE_SIZE);		\
489 	if (physical_freeend < physical_freestart)	\
490 		panic("initarm: out of memory");	\
491 	(var) = physical_freeend;			\
492 	free_pages -= (np);				\
493 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
494 
495 	loop1 = 0;
496 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
497 		/* Are we 16KB aligned for an L1 ? */
498 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
499 		    && kernel_l1pt.pv_pa == 0) {
500 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
501 		} else {
502 			valloc_pages(kernel_pt_table[loop1],
503 			    L2_TABLE_SIZE / PAGE_SIZE);
504 			++loop1;
505 		}
506 	}
507 
508 	/* This should never be able to happen but better confirm that. */
509 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
510 		panic("initarm: Failed to align the kernel page directory");
511 
512 	/*
513 	 * Allocate a page for the system page mapped to V0x00000000
514 	 * This page will just contain the system vectors and can be
515 	 * shared by all processes.
516 	 */
517 	alloc_pages(systempage.pv_pa, 1);
518 
519 	/* Allocate stacks for all modes */
520 	valloc_pages(irqstack, IRQ_STACK_SIZE);
521 	valloc_pages(abtstack, ABT_STACK_SIZE);
522 	valloc_pages(undstack, UND_STACK_SIZE);
523 	valloc_pages(kernelstack, UPAGES);
524 
525 #ifdef VERBOSE_INIT_ARM
526 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
527 	    irqstack.pv_va);
528 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
529 	    abtstack.pv_va);
530 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
531 	    undstack.pv_va);
532 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
533 	    kernelstack.pv_va);
534 #endif
535 
536 	/*
537 	 * XXX Defer this to later so that we can reclaim the memory
538 	 * XXX used by the LoLo page tables.
539 	 */
540 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
541 
542 	/*
543 	 * Ok we have allocated physical pages for the primary kernel
544 	 * page tables
545 	 */
546 
547 #ifdef VERBOSE_INIT_ARM
548 	printf("Creating L1 page table at p0x%08lx v0x%08lx\n",
549 		kernel_l1pt.pv_pa, kernel_l1pt.pv_va);
550 #endif
551 
552 	/*
553 	 * Now we start construction of the L1 page table
554 	 * We start by mapping the L2 page tables into the L1.
555 	 * This means that we can replace L1 mappings later on if necessary
556 	 */
557 	l1pagetable = kernel_l1pt.pv_pa;
558 
559 	/* Map the L2 pages tables in the L1 page table */
560 	pmap_link_l2pt(l1pagetable, 0x00000000,
561 	    &kernel_pt_table[KERNEL_PT_SYS]);
562 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
563 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
564 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
565 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
566 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
567 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
568 
569 	/* update the top of the kernel VM */
570 	pmap_curmaxkvaddr =
571 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
572 
573 #ifdef VERBOSE_INIT_ARM
574 	printf("Mapping kernel\n");
575 #endif
576 
577 	/* Now we fill in the L2 pagetable for the kernel static code/data */
578 #define round_L_page(x) (((x) + L2_L_OFFSET) & L2_L_FRAME)
579 	{
580 		extern char etext[], _end[];
581 		size_t textsize = round_L_page((size_t)etext - KERNEL_TEXT_BASE);
582 		size_t totalsize = round_L_page((size_t)_end - KERNEL_TEXT_BASE);
583 		u_int logical;
584 
585 
586 printf("%s: etext %lx, _end %lx\n",
587 	__FUNCTION__, (uintptr_t)etext, (uintptr_t)_end);
588 printf("%s: textsize %#lx, totalsize %#lx\n",
589 	__FUNCTION__, textsize, totalsize);
590 
591 		logical = 0x00100000;	/* offset of kernel in RAM */
592 
593 		/* Map text section read-only. */
594 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
595 		    physical_start + logical, textsize,
596 		    VM_PROT_READ|VM_PROT_EXECUTE, PTE_CACHE);
597 
598 		/* Map data and bss sections read-write. */
599 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
600 		    physical_start + logical, totalsize - textsize,
601 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
602 	}
603 
604 #ifdef VERBOSE_INIT_ARM
605 	printf("Constructing L2 page tables\n");
606 #endif
607 
608 	/* Map the stack pages */
609 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
610 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
611 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
612 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
613 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
614 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
615 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
616 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
617 
618 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
619 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
620 
621 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
622 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
623 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
624 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
625 	}
626 
627 	/* Map the vector page. */
628 #if 1
629 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
630 	 * cache-clean code there.  */
631 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
632 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
633 #else
634 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
635 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
636 #endif
637 
638 	/*
639 	 * map integrated peripherals at same address in l1pagetable
640 	 * so that we can continue to use console.
641 	 */
642 	pmap_devmap_bootstrap(l1pagetable, imx31lk_devmap);
643 
644 	/*
645 	 * Now we have the real page tables in place so we can switch to them.
646 	 * Once this is done we will be running with the REAL kernel page
647 	 * tables.
648 	 */
649 
650 	/*
651 	 * Update the physical_freestart/physical_freeend/free_pages
652 	 * variables.
653 	 */
654 	{
655 		extern char _end[];
656 
657 		physical_freestart = physical_start +
658 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
659 		     KERNEL_BASE);
660 		physical_freeend = physical_end;
661 		free_pages =
662 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
663 	}
664 
665 	/* Switch tables */
666 #ifdef VERBOSE_INIT_ARM
667 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
668 	       physical_freestart, free_pages, free_pages);
669 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
670 #endif
671 
672 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
673 	setttb(kernel_l1pt.pv_pa);
674 	cpu_tlb_flushID();
675 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
676 	imx31lk_consinit(2);
677 
678 	/*
679 	 * Moved from cpu_startup() as data_abort_handler() references
680 	 * this during uvm init
681 	 */
682 	proc0paddr = (struct user *)kernelstack.pv_va;
683 	lwp0.l_addr = proc0paddr;
684 
685 #ifdef VERBOSE_INIT_ARM
686 	printf("bootstrap done.\n");
687 #endif
688 
689 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
690 
691 	/*
692 	 * Pages were allocated during the secondary bootstrap for the
693 	 * stacks for different CPU modes.
694 	 * We must now set the r13 registers in the different CPU modes to
695 	 * point to these stacks.
696 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
697 	 * of the stack memory.
698 	 */
699 	printf("init subsystems: stacks ");
700 
701 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
702 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
703 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
704 
705 	/*
706 	 * Well we should set a data abort handler.
707 	 * Once things get going this will change as we will need a proper
708 	 * handler.
709 	 * Until then we will use a handler that just panics but tells us
710 	 * why.
711 	 * Initialisation of the vectors will just panic on a data abort.
712 	 * This just fills in a slightly better one.
713 	 */
714 	printf("vectors ");
715 	data_abort_handler_address = (u_int)data_abort_handler;
716 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
717 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
718 
719 	/* Initialise the undefined instruction handlers */
720 	printf("undefined ");
721 	undefined_init();
722 
723 	/* Load memory into UVM. */
724 	printf("page ");
725 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
726 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
727 	    atop(physical_freestart), atop(physical_freeend),
728 	    VM_FREELIST_DEFAULT);
729 
730 	/* Boot strap pmap telling it where the kernel page table is */
731 	printf("pmap ");
732 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
733 
734 #ifdef __HAVE_MEMORY_DISK__
735 	md_root_setconf(memory_disk, sizeof memory_disk);
736 #endif
737 
738 #ifdef IPKDB
739 	/* Initialise ipkdb */
740 	ipkdb_init();
741 	if (boothowto & RB_KDB)
742 		ipkdb_connect(0);
743 #endif
744 
745 #ifdef KGDB
746 	if (boothowto & RB_KDB) {
747 		kgdb_debug_init = 1;
748 		kgdb_connect(1);
749 	}
750 #endif
751 
752 #ifdef DDB
753 	printf("ddb ");
754 	db_machine_init();
755 
756 	/* Firmware doesn't load symbols. */
757 	ddb_init(0, NULL, NULL);
758 
759 	if (boothowto & RB_KDB)
760 		Debugger();
761 #endif
762 	/* We return the new stack pointer address */
763 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
764 }
765 
766 #if 0
767 void
768 process_kernel_args(char *args)
769 {
770 
771 	boothowto = 0;
772 
773 	/* Make a local copy of the bootargs */
774 	strncpy(bootargs, args, MAX_BOOT_STRING);
775 
776 	args = bootargs;
777 	boot_file = bootargs;
778 
779 	/* Skip the kernel image filename */
780 	while (*args != ' ' && *args != 0)
781 		++args;
782 
783 	if (*args != 0)
784 		*args++ = 0;
785 
786 	while (*args == ' ')
787 		++args;
788 
789 	boot_args = args;
790 
791 	printf("bootfile: %s\n", boot_file);
792 	printf("bootargs: %s\n", boot_args);
793 
794 	parse_mi_bootargs(boot_args);
795 }
796 #endif
797 
798 #ifdef KGDB
799 #ifndef KGDB_DEVNAME
800 #define KGDB_DEVNAME "ffuart"
801 #endif
802 const char kgdb_devname[] = KGDB_DEVNAME;
803 
804 #if (NCOM > 0)
805 #ifndef KGDB_DEVMODE
806 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
807 #endif
808 int comkgdbmode = KGDB_DEVMODE;
809 #endif /* NCOM */
810 
811 #endif /* KGDB */
812 
813 
814 void
815 imx31lk_consinit(int phase)
816 {
817 	static int ophase = 0;
818 	intptr_t bh;
819 
820 	if (ophase != phase) {
821 		ophase = phase;
822 		switch (phase) {
823 		case 1:
824 			imxuart_init(0, IMX_UART1_BASE);
825 			break;
826 		case 2:
827 			bh = IMX31LITEKIT_UART1_VBASE;
828 			bh |= (IMX_UART1_BASE & ~_A(IMX_UART1_BASE));
829 			imxuart_init(0, bh);
830 			break;
831 		}
832 	}
833 }
834 
835 void
836 consinit(void)
837 {
838 	imx31lk_consinit(2);
839 }
840 
841 void consinit_test(void);
842 void
843 consinit_test(void)
844 {
845 	imxuart_softc_t *sc, softc;
846 	extern int imxuart_puts(imxuart_softc_t *sc, const char *s);
847 
848 	printf("\n%s start\n", __FUNCTION__);
849 	sc = &softc;
850 	sc->sc_init_cnt = 0;
851 	imxuart_init(sc, IMX_UART1_BASE);
852 	imxuart_puts(sc, "test1\r\n");
853 	imxuart_init(sc,
854 	    IMX31LITEKIT_UART1_VBASE|(IMX_UART1_BASE & ~_A(IMX_UART1_BASE)));
855 	imxuart_puts(sc, "test2\r\n");
856 	printf("%s done\n", __FUNCTION__);
857 }
858 
859 #ifdef KGDB
860 void
861 kgdb_port_init(void)
862 {
863 #if (NCOM > 0) && defined(COM_PXA2X0)
864 	paddr_t paddr = 0;
865 	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
866 
867 	if (0 == strcmp(kgdb_devname, "ffuart")) {
868 		paddr = PXA2X0_FFUART_BASE;
869 		ckenreg |= CKEN_FFUART;
870 	}
871 	else if (0 == strcmp(kgdb_devname, "btuart")) {
872 		paddr = PXA2X0_BTUART_BASE;
873 		ckenreg |= CKEN_BTUART;
874 	}
875 
876 	if (paddr &&
877 	    0 == com_kgdb_attach(&imx31_a4x_bs_tag, paddr,
878 		kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
879 
880 		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
881 	}
882 #endif
883 }
884 #endif
885