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