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