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