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