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