xref: /netbsd-src/sys/arch/evbarm/iq80321/iq80321_machdep.c (revision dd75ac5b443e967e26b4d18cc8cd5eb98512bfbf)
1 /*	$NetBSD: iq80321_machdep.c,v 1.65 2023/04/20 08:28:04 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1997,1998 Mark Brinicombe.
40  * Copyright (c) 1997,1998 Causality Limited.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by Mark Brinicombe
54  *	for the NetBSD Project.
55  * 4. The name of the company nor the name of the author may be used to
56  *    endorse or promote products derived from this software without specific
57  *    prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  * Machine dependent functions for kernel setup for Intel IQ80321 evaluation
72  * boards using RedBoot firmware.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: iq80321_machdep.c,v 1.65 2023/04/20 08:28:04 skrll Exp $");
77 
78 #include "opt_console.h"
79 #include "opt_ddb.h"
80 #include "opt_kgdb.h"
81 
82 #include <sys/param.h>
83 #include <sys/device.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/exec.h>
87 #include <sys/proc.h>
88 #include <sys/msgbuf.h>
89 #include <sys/reboot.h>
90 #include <sys/termios.h>
91 #include <sys/ksyms.h>
92 #include <sys/cpu.h>
93 #include <sys/bus.h>
94 
95 #include <uvm/uvm_extern.h>
96 
97 #include <dev/cons.h>
98 
99 #include <dev/pci/ppbreg.h>
100 
101 #include <machine/db_machdep.h>
102 #include <ddb/db_sym.h>
103 #include <ddb/db_extern.h>
104 
105 #include <machine/bootconfig.h>
106 
107 #include <arm/locore.h>
108 #include <arm/undefined.h>
109 
110 #include <arm/arm32/machdep.h>
111 
112 #include <arm/xscale/i80321reg.h>
113 #include <arm/xscale/i80321var.h>
114 
115 #include <evbarm/iq80321/iq80321reg.h>
116 #include <evbarm/iq80321/iq80321var.h>
117 #include <evbarm/iq80321/obiovar.h>
118 
119 #include "ksyms.h"
120 
121 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
122 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
123 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
124 
125 /*
126  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
127  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
128  */
129 #define KERNEL_VM_SIZE		0x0C000000
130 
131 BootConfig bootconfig;		/* Boot config storage */
132 char *boot_args = NULL;
133 char *boot_file = NULL;
134 
135 vaddr_t physical_start;
136 vaddr_t physical_freestart;
137 vaddr_t physical_freeend;
138 vaddr_t physical_end;
139 u_int free_pages;
140 
141 /*int debug_flags;*/
142 #ifndef PMAP_STATIC_L1S
143 int max_processes = 64;			/* Default number */
144 #endif	/* !PMAP_STATIC_L1S */
145 
146 /* Physical and virtual addresses for some global pages */
147 pv_addr_t minidataclean;
148 
149 paddr_t msgbufphys;
150 
151 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
152 
153 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
154 #define	KERNEL_PT_KERNEL_NUM	4
155 
156 					/* L2 table for mapping i80321 */
157 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
158 
159 					/* L2 tables for mapping kernel VM */
160 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
161 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
162 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
163 
164 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
165 
166 /* Prototypes */
167 
168 void	consinit(void);
169 
170 #include "com.h"
171 #if NCOM > 0
172 #include <dev/ic/comreg.h>
173 #include <dev/ic/comvar.h>
174 #endif
175 
176 /*
177  * Define the default console speed for the board.  This is generally
178  * what the firmware provided with the board defaults to.
179  */
180 #ifndef CONSPEED
181 #define CONSPEED B115200
182 #endif /* ! CONSPEED */
183 
184 #ifndef CONUNIT
185 #define	CONUNIT	0
186 #endif
187 
188 #ifndef CONMODE
189 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
190 #endif
191 
192 int comcnspeed = CONSPEED;
193 int comcnmode = CONMODE;
194 int comcnunit = CONUNIT;
195 
196 #if KGDB
197 #ifndef KGDB_DEVNAME
198 #error Must define KGDB_DEVNAME
199 #endif
200 const char kgdb_devname[] = KGDB_DEVNAME;
201 
202 #ifndef KGDB_DEVADDR
203 #error Must define KGDB_DEVADDR
204 #endif
205 unsigned long kgdb_devaddr = KGDB_DEVADDR;
206 
207 #ifndef KGDB_DEVRATE
208 #define KGDB_DEVRATE	CONSPEED
209 #endif
210 int kgdb_devrate = KGDB_DEVRATE;
211 
212 #ifndef KGDB_DEVMODE
213 #define KGDB_DEVMODE	CONMODE
214 #endif
215 int kgdb_devmode = KGDB_DEVMODE;
216 #endif /* KGDB */
217 
218 #if defined(I80321_REBOOT)
219 extern void I80321_REBOOT(int);
220 #endif
221 
222 /*
223  * void cpu_reboot(int howto, char *bootstr)
224  *
225  * Reboots the system
226  *
227  * Deal with any syncing, unmounting, dumping and shutdown hooks,
228  * then reset the CPU.
229  */
230 void
231 cpu_reboot(int howto, char *bootstr)
232 {
233 
234 	/*
235 	 * If we are still cold then hit the air brakes
236 	 * and crash to earth fast
237 	 */
238 	if (cold) {
239 		doshutdownhooks();
240 		pmf_system_shutdown(boothowto);
241 		printf("The operating system has halted.\n");
242 		printf("Please press any key to reboot.\n\n");
243 		cngetc();
244 		printf("rebooting...\n");
245 		goto reset;
246 	}
247 
248 	/* Disable console buffering */
249 
250 	/*
251 	 * If RB_NOSYNC was not specified sync the discs.
252 	 * Note: Unless cold is set to 1 here, syslogd will die during the
253 	 * unmount.  It looks like syslogd is getting woken up only to find
254 	 * that it cannot page part of the binary in as the filesystem has
255 	 * been unmounted.
256 	 */
257 	if (!(howto & RB_NOSYNC))
258 		bootsync();
259 
260 	/* Say NO to interrupts */
261 	splhigh();
262 
263 	/* Do a dump if requested. */
264 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
265 		dumpsys();
266 
267 	/* Run any shutdown hooks */
268 	doshutdownhooks();
269 
270 	pmf_system_shutdown(boothowto);
271 
272 	/* Make sure IRQ's are disabled */
273 	IRQdisable;
274 
275 	if (howto & RB_HALT) {
276 #if defined(I80321_REBOOT)
277 		I80321_REBOOT(howto);
278 #endif
279 		iq80321_7seg('.', '.');
280 		printf("The operating system has halted.\n");
281 		printf("Please press any key to reboot.\n\n");
282 		cngetc();
283 	}
284 
285 	printf("rebooting...\n\r");
286  reset:
287 #if defined(I80321_REBOOT)
288 	I80321_REBOOT(howto);
289 #endif
290 
291 	/*
292 	 * Make really really sure that all interrupts are disabled,
293 	 * and poke the Internal Bus and Peripheral Bus reset lines.
294 	 */
295 	(void) disable_interrupts(I32_bit|F32_bit);
296 	*(volatile uint32_t *)(IQ80321_80321_VBASE + VERDE_ATU_BASE +
297 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
298 
299 	/* ...and if that didn't work, just croak. */
300 	printf("RESET FAILED!\n");
301 	for (;;);
302 }
303 
304 /* Static device mappings. */
305 static const struct pmap_devmap iq80321_devmap[] = {
306 	/*
307 	 * Map the on-board devices VA == PA so that we can access them
308 	 * with the MMU on or off.
309 	 */
310 	DEVMAP_ENTRY(
311 		IQ80321_OBIO_BASE,
312 		IQ80321_OBIO_BASE,
313 		IQ80321_OBIO_SIZE
314 	),
315 
316 	DEVMAP_ENTRY(
317 		IQ80321_IOW_VBASE,
318 		VERDE_OUT_XLATE_IO_WIN0_BASE,
319 		VERDE_OUT_XLATE_IO_WIN_SIZE
320 	),
321 
322 	DEVMAP_ENTRY(
323 		IQ80321_80321_VBASE,
324 		VERDE_PMMR_BASE,
325 		VERDE_PMMR_SIZE
326    	),
327 
328 	DEVMAP_ENTRY_END
329 };
330 
331 static void
332 iq80321_hardclock_hook(void)
333 {
334 	static int snakefreq;
335 
336 	if ((snakefreq++ & 15) == 0)
337 		iq80321_7seg_snake();
338 }
339 
340 /*
341  * vaddr_t initarm(...)
342  *
343  * Initial entry point on startup. This gets called before main() is
344  * entered.
345  * It should be responsible for setting up everything that must be
346  * in place when main is called.
347  * This includes
348  *   Taking a copy of the boot configuration structure.
349  *   Initialising the physical console so characters can be printed.
350  *   Setting up page tables for the kernel
351  *   Relocating the kernel to the bottom of physical memory
352  */
353 vaddr_t
354 initarm(void *arg)
355 {
356 	extern vaddr_t xscale_cache_clean_addr;
357 #ifdef DIAGNOSTIC
358 	extern vsize_t xscale_minidata_clean_size;
359 #endif
360 	int loop;
361 	int loop1;
362 	u_int l1pagetable;
363 	paddr_t memstart;
364 	psize_t memsize;
365 
366 	/*
367 	 * Clear out the 7-segment display.  Whee, the first visual
368 	 * indication that we're running kernel code.
369 	 */
370 	iq80321_7seg(' ', ' ');
371 
372 	/* Calibrate the delay loop. */
373 	i80321_calibrate_delay();
374 	i80321_hardclock_hook = iq80321_hardclock_hook;
375 
376 	/*
377 	 * Since we map the on-board devices VA==PA, and the kernel
378 	 * is running VA==PA, it's possible for us to initialize
379 	 * the console now.
380 	 */
381 	consinit();
382 
383 #ifdef VERBOSE_INIT_ARM
384 	/* Talk to the user */
385 	printf("\nNetBSD/evbarm (IQ80321) booting ...\n");
386 #endif
387 
388 	/*
389 	 * Heads up ... Setup the CPU / MMU / TLB functions
390 	 */
391 	if (set_cpufuncs())
392 		panic("CPU not recognized!");
393 
394 	/*
395 	 * We are currently running with the MMU enabled and the
396 	 * entire address space mapped VA==PA, except for the
397 	 * first 64M of RAM is also double-mapped at 0xc0000000.
398 	 * There is an L1 page table at 0xa0004000.
399 	 */
400 
401 	/*
402 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
403 	 * registers.
404 	 */
405 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
406 	    &memstart, &memsize);
407 
408 #ifdef VERBOSE_INIT_ARM
409 	printf("initarm: Configuring system ...\n");
410 #endif
411 
412 	/* Fake bootconfig structure for the benefit of pmap.c */
413 	/* XXX must make the memory description h/w independent */
414 	bootconfig.dramblocks = 1;
415 	bootconfig.dram[0].address = memstart;
416 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
417 
418 	/*
419 	 * Set up the variables that define the availability of
420 	 * physical memory.  For now, we're going to set
421 	 * physical_freestart to 0xa0200000 (where the kernel
422 	 * was loaded), and allocate the memory we need downwards.
423 	 * If we get too close to the L1 table that we set up, we
424 	 * will panic.  We will update physical_freestart and
425 	 * physical_freeend later to reflect what pmap_bootstrap()
426 	 * wants to see.
427 	 *
428 	 * XXX pmap_bootstrap() needs an enema.
429 	 */
430 	physical_start = bootconfig.dram[0].address;
431 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
432 
433 	physical_freestart = 0xa0009000UL;
434 	physical_freeend = 0xa0200000UL;
435 
436 	physmem = (physical_end - physical_start) / PAGE_SIZE;
437 
438 #ifdef VERBOSE_INIT_ARM
439 	/* Tell the user about the memory */
440 	printf("physmemory: %"PRIuPSIZE" pages at 0x%08lx -> 0x%08lx\n",
441 	    physmem, physical_start, physical_end - 1);
442 #endif
443 
444 	/*
445 	 * Okay, the kernel starts 2MB in from the bottom of physical
446 	 * memory.  We are going to allocate our bootstrap pages downwards
447 	 * from there.
448 	 *
449 	 * We need to allocate some fixed page tables to get the kernel
450 	 * going.  We allocate one page directory and a number of page
451 	 * tables and store the physical addresses in the kernel_pt_table
452 	 * array.
453 	 *
454 	 * The kernel page directory must be on a 16K boundary.  The page
455 	 * tables must be on 4K boundaries.  What we do is allocate the
456 	 * page directory on the first 16K boundary that we encounter, and
457 	 * the page tables on 4K boundaries otherwise.  Since we allocate
458 	 * at least 3 L2 page tables, we are guaranteed to encounter at
459 	 * least one 16K aligned region.
460 	 */
461 
462 #ifdef VERBOSE_INIT_ARM
463 	printf("Allocating page tables\n");
464 #endif
465 
466 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
467 
468 #ifdef VERBOSE_INIT_ARM
469 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
470 	       physical_freestart, free_pages, free_pages);
471 #endif
472 
473 	/* Define a macro to simplify memory allocation */
474 #define	valloc_pages(var, np)				\
475 	alloc_pages((var).pv_pa, (np));			\
476 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
477 
478 #define alloc_pages(var, np)				\
479 	physical_freeend -= ((np) * PAGE_SIZE);		\
480 	if (physical_freeend < physical_freestart)	\
481 		panic("initarm: out of memory");	\
482 	(var) = physical_freeend;			\
483 	free_pages -= (np);				\
484 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
485 
486 	loop1 = 0;
487 	kernel_l1pt.pv_pa = 0;
488 	kernel_l1pt.pv_va = 0;
489 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
490 		/* Are we 16KB aligned for an L1 ? */
491 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
492 		    && kernel_l1pt.pv_pa == 0) {
493 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
494 		} else {
495 			valloc_pages(kernel_pt_table[loop1],
496 			    L2_TABLE_SIZE / PAGE_SIZE);
497 			++loop1;
498 		}
499 	}
500 
501 	/* This should never be able to happen but better confirm that. */
502 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
503 		panic("initarm: Failed to align the kernel page directory");
504 
505 	/*
506 	 * Allocate a page for the system page mapped to V0x00000000
507 	 * This page will just contain the system vectors and can be
508 	 * shared by all processes.
509 	 */
510 	alloc_pages(systempage.pv_pa, 1);
511 
512 	/* Allocate stacks for all modes */
513 	valloc_pages(irqstack, IRQ_STACK_SIZE);
514 	valloc_pages(abtstack, ABT_STACK_SIZE);
515 	valloc_pages(undstack, UND_STACK_SIZE);
516 	valloc_pages(kernelstack, UPAGES);
517 
518 	/* Allocate enough pages for cleaning the Mini-Data cache. */
519 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
520 	valloc_pages(minidataclean, 1);
521 
522 #ifdef VERBOSE_INIT_ARM
523 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
524 	    irqstack.pv_va);
525 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
526 	    abtstack.pv_va);
527 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
528 	    undstack.pv_va);
529 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
530 	    kernelstack.pv_va);
531 #endif
532 
533 	/*
534 	 * XXX Defer this to later so that we can reclaim the memory
535 	 * XXX used by the RedBoot page tables.
536 	 */
537 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
538 
539 	/*
540 	 * Ok we have allocated physical pages for the primary kernel
541 	 * page tables
542 	 */
543 
544 #ifdef VERBOSE_INIT_ARM
545 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
546 #endif
547 
548 	/*
549 	 * Now we start construction of the L1 page table
550 	 * We start by mapping the L2 page tables into the L1.
551 	 * This means that we can replace L1 mappings later on if necessary
552 	 */
553 	l1pagetable = kernel_l1pt.pv_pa;
554 
555 	/* Map the L2 pages tables in the L1 page table */
556 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
557 	    &kernel_pt_table[KERNEL_PT_SYS]);
558 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
559 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
560 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
561 	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
562 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
563 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
564 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
565 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
566 
567 	/* update the top of the kernel VM */
568 	pmap_curmaxkvaddr =
569 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
570 
571 #ifdef VERBOSE_INIT_ARM
572 	printf("Mapping kernel\n");
573 #endif
574 
575 	/* Now we fill in the L2 pagetable for the kernel static code/data */
576 	{
577 		extern char etext[], _end[];
578 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
579 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
580 		u_int logical;
581 
582 		textsize = (textsize + PGOFSET) & ~PGOFSET;
583 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
584 
585 		logical = 0x00200000;	/* offset of kernel in RAM */
586 
587 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
588 		    physical_start + logical, textsize,
589 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
590 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
591 		    physical_start + logical, totalsize - textsize,
592 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
593 	}
594 
595 #ifdef VERBOSE_INIT_ARM
596 	printf("Constructing L2 page tables\n");
597 #endif
598 
599 	/* Map the stack pages */
600 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
601 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
602 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
603 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
604 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
605 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
606 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
607 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
608 
609 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
610 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
611 
612 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
613 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
614 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
615 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
616 	}
617 
618 	/* Map the Mini-Data cache clean area. */
619 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
620 	    minidataclean.pv_pa);
621 
622 	/* Map the vector page. */
623 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
624 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
625 
626 	/* Map the statically mapped devices. */
627 	pmap_devmap_bootstrap(l1pagetable, iq80321_devmap);
628 
629 	/*
630 	 * Give the XScale global cache clean code an appropriately
631 	 * sized chunk of unmapped VA space starting at 0xff000000
632 	 * (our device mappings end before this address).
633 	 */
634 	xscale_cache_clean_addr = 0xff000000U;
635 
636 	/*
637 	 * Now we have the real page tables in place so we can switch to them.
638 	 * Once this is done we will be running with the REAL kernel page
639 	 * tables.
640 	 */
641 
642 	/*
643 	 * Update the physical_freestart/physical_freeend/free_pages
644 	 * variables.
645 	 */
646 	{
647 		extern char _end[];
648 
649 		physical_freestart = physical_start +
650 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
651 		     KERNEL_BASE);
652 		physical_freeend = physical_end;
653 		free_pages =
654 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
655 	}
656 
657 	/* Switch tables */
658 #ifdef VERBOSE_INIT_ARM
659 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
660 	       physical_freestart, free_pages, free_pages);
661 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
662 #endif
663 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
664 	cpu_setttb(kernel_l1pt.pv_pa, true);
665 	cpu_tlb_flushID();
666 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
667 
668 	/*
669 	 * Moved from cpu_startup() as data_abort_handler() references
670 	 * this during uvm init
671 	 */
672 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
673 
674 #ifdef VERBOSE_INIT_ARM
675 	printf("done!\n");
676 #endif
677 
678 #ifdef VERBOSE_INIT_ARM
679 	printf("bootstrap done.\n");
680 #endif
681 
682 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
683 
684 	/*
685 	 * Pages were allocated during the secondary bootstrap for the
686 	 * stacks for different CPU modes.
687 	 * We must now set the r13 registers in the different CPU modes to
688 	 * point to these stacks.
689 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
690 	 * of the stack memory.
691 	 */
692 #ifdef VERBOSE_INIT_ARM
693 	printf("init subsystems: stacks ");
694 #endif
695 
696 	set_stackptr(PSR_IRQ32_MODE,
697 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
698 	set_stackptr(PSR_ABT32_MODE,
699 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
700 	set_stackptr(PSR_UND32_MODE,
701 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
702 
703 	/*
704 	 * Well we should set a data abort handler.
705 	 * Once things get going this will change as we will need a proper
706 	 * handler.
707 	 * Until then we will use a handler that just panics but tells us
708 	 * why.
709 	 * Initialisation of the vectors will just panic on a data abort.
710 	 * This just fills in a slightly better one.
711 	 */
712 #ifdef VERBOSE_INIT_ARM
713 	printf("vectors ");
714 #endif
715 	data_abort_handler_address = (u_int)data_abort_handler;
716 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
717 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
718 
719 	/* Initialise the undefined instruction handlers */
720 #ifdef VERBOSE_INIT_ARM
721 	printf("undefined ");
722 #endif
723 	undefined_init();
724 
725 	/* Load memory into UVM. */
726 #ifdef VERBOSE_INIT_ARM
727 	printf("page ");
728 #endif
729 	uvm_md_init();
730 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
731 	    atop(physical_freestart), atop(physical_freeend),
732 	    VM_FREELIST_DEFAULT);
733 
734 	/* Boot strap pmap telling it where managed kernel virtual memory is */
735 #ifdef VERBOSE_INIT_ARM
736 	printf("pmap ");
737 #endif
738 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
739 
740 	/* Setup the IRQ system */
741 #ifdef VERBOSE_INIT_ARM
742 	printf("irq ");
743 #endif
744 	i80321_intr_init();
745 
746 #ifdef VERBOSE_INIT_ARM
747 	printf("done.\n");
748 #endif
749 
750 #ifdef BOOTHOWTO
751 	boothowto = BOOTHOWTO;
752 #endif
753 
754 #ifdef DDB
755 	db_machine_init();
756 	if (boothowto & RB_KDB)
757 		Debugger();
758 #endif
759 
760 	/* We return the new stack pointer address */
761 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
762 }
763 
764 void
765 consinit(void)
766 {
767 	static const bus_addr_t comcnaddrs[] = {
768 		IQ80321_UART1,		/* com0 */
769 	};
770 	static int consinit_called;
771 
772 	if (consinit_called != 0)
773 		return;
774 
775 	consinit_called = 1;
776 
777 	/*
778 	 * Console devices are mapped VA==PA.  Our devmap reflects
779 	 * this, so register it now so drivers can map the console
780 	 * device.
781 	 */
782 	pmap_devmap_register(iq80321_devmap);
783 
784 #if NCOM > 0
785 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
786 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
787 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
788 #else
789 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
790 #endif
791 #if KGDB
792 #if NCOM > 0
793 	if (strcmp(kgdb_devname, "com") == 0) {
794 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
795 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
796 	}
797 #endif	/* NCOM > 0 */
798 #endif	/* KGDB */
799 }
800