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