1 /*	$NetBSD: netwinder_machdep.c,v 1.31 2002/04/12 19:12:32 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1997,1998 Mark Brinicombe.
5  * Copyright (c) 1997,1998 Causality Limited.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Mark Brinicombe
19  *	for the NetBSD Project.
20  * 4. The name of the company nor the name of the author may be used to
21  *    endorse or promote products derived from this software without specific
22  *    prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * Machine dependant functions for kernel setup for EBSA285 core architecture
37  * using Netwinder firmware
38  *
39  * Created      : 24/11/97
40  */
41 
42 #include "opt_ddb.h"
43 #include "opt_pmap_debug.h"
44 
45 #include <sys/param.h>
46 #include <sys/device.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/exec.h>
50 #include <sys/proc.h>
51 #include <sys/msgbuf.h>
52 #include <sys/reboot.h>
53 #include <sys/termios.h>
54 
55 #include <dev/cons.h>
56 
57 #include <machine/db_machdep.h>
58 #include <ddb/db_sym.h>
59 #include <ddb/db_extern.h>
60 
61 #include <machine/bootconfig.h>
62 #include <machine/bus.h>
63 #include <machine/cpu.h>
64 #include <machine/frame.h>
65 #include <machine/intr.h>
66 #include <arm/undefined.h>
67 
68 #include <machine/netwinder_boot.h>
69 #include <arm/footbridge/dc21285mem.h>
70 #include <arm/footbridge/dc21285reg.h>
71 
72 #include "opt_ipkdb.h"
73 
74 #include "isa.h"
75 #if NISA > 0
76 #include <dev/isa/isareg.h>
77 #include <dev/isa/isavar.h>
78 #endif
79 
80 static bus_space_handle_t isa_base = (bus_space_handle_t) DC21285_PCI_IO_VBASE;
81 
82 bs_protos(generic);
83 
84 #define	ISA_GETBYTE(r)		generic_bs_r_1(0, isa_base, (r))
85 #define	ISA_PUTBYTE(r,v)	generic_bs_w_1(0, isa_base, (r), (v))
86 
87 /*
88  * Address to call from cpu_reset() to reset the machine.
89  * This is machine architecture dependant as it varies depending
90  * on where the ROM appears when you turn the MMU off.
91  */
92 static void netwinder_reset(void);
93 u_int cpu_reset_address = (u_int) netwinder_reset;
94 
95 u_int dc21285_fclk = 63750000;
96 
97 /* Define various stack sizes in pages */
98 #define IRQ_STACK_SIZE	1
99 #define ABT_STACK_SIZE	1
100 #ifdef IPKDB
101 #define UND_STACK_SIZE	2
102 #else
103 #define UND_STACK_SIZE	1
104 #endif
105 
106 struct nwbootinfo nwbootinfo;
107 BootConfig bootconfig;		/* Boot config storage */
108 static char bootargs[MAX_BOOT_STRING + 1];
109 char *boot_args = NULL;
110 char *boot_file = NULL;
111 
112 vm_offset_t physical_start;
113 vm_offset_t physical_freestart;
114 vm_offset_t physical_freeend;
115 vm_offset_t physical_end;
116 u_int free_pages;
117 vm_offset_t pagetables_start;
118 int physmem = 0;
119 
120 /*int debug_flags;*/
121 #ifndef PMAP_STATIC_L1S
122 int max_processes = 64;			/* Default number */
123 #endif	/* !PMAP_STATIC_L1S */
124 
125 /* Physical and virtual addresses for some global pages */
126 pv_addr_t systempage;
127 pv_addr_t irqstack;
128 pv_addr_t undstack;
129 pv_addr_t abtstack;
130 pv_addr_t kernelstack;
131 
132 vm_offset_t msgbufphys;
133 
134 extern u_int data_abort_handler_address;
135 extern u_int prefetch_abort_handler_address;
136 extern u_int undefined_handler_address;
137 
138 #ifdef PMAP_DEBUG
139 extern int pmap_debug_level;
140 #endif
141 
142 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
143 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
144 #define KERNEL_PT_VMDATA	2	/* Page tables for mapping kernel VM */
145 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
146 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
147 
148 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
149 
150 struct user *proc0paddr;
151 
152 /* Prototypes */
153 
154 void consinit		__P((void));
155 
156 int fcomcnattach __P((u_int iobase, int rate,tcflag_t cflag));
157 int fcomcndetach __P((void));
158 
159 void isa_netwinder_init __P((u_int iobase, u_int membase));
160 
161 void process_kernel_args	__P((char *));
162 void data_abort_handler		__P((trapframe_t *frame));
163 void prefetch_abort_handler	__P((trapframe_t *frame));
164 void undefinedinstruction_bounce	__P((trapframe_t *frame));
165 extern void configure		__P((void));
166 extern void parse_mi_bootargs	__P((char *args));
167 extern void dumpsys		__P((void));
168 
169 /* A load of console goo. */
170 #include "vga.h"
171 #if (NVGA > 0)
172 #include <dev/ic/mc6845reg.h>
173 #include <dev/ic/pcdisplayvar.h>
174 #include <dev/ic/vgareg.h>
175 #include <dev/ic/vgavar.h>
176 #endif
177 
178 #include "pckbc.h"
179 #if (NPCKBC > 0)
180 #include <dev/ic/i8042reg.h>
181 #include <dev/ic/pckbcvar.h>
182 #endif
183 
184 #include "com.h"
185 #if (NCOM > 0)
186 #include <dev/ic/comreg.h>
187 #include <dev/ic/comvar.h>
188 #ifndef CONCOMADDR
189 #define CONCOMADDR 0x3f8
190 #endif
191 #endif
192 
193 #ifndef CONSDEVNAME
194 #define CONSDEVNAME "com"
195 #endif
196 
197 #define CONSPEED B115200
198 #ifndef CONSPEED
199 #define CONSPEED B9600	/* TTYDEF_SPEED */
200 #endif
201 #ifndef CONMODE
202 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
203 #endif
204 
205 int comcnspeed = CONSPEED;
206 int comcnmode = CONMODE;
207 
208 extern struct consdev kcomcons;
209 static void kcomcnputc(dev_t, int);
210 
211 /*
212  * void cpu_reboot(int howto, char *bootstr)
213  *
214  * Reboots the system
215  *
216  * Deal with any syncing, unmounting, dumping and shutdown hooks,
217  * then reset the CPU.
218  */
219 
220 void
221 cpu_reboot(howto, bootstr)
222 	int howto;
223 	char *bootstr;
224 {
225 #ifdef DIAGNOSTIC
226 	/* info */
227 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
228 #endif
229 
230 	/*
231 	 * If we are still cold then hit the air brakes
232 	 * and crash to earth fast
233 	 */
234 	if (cold) {
235 		doshutdownhooks();
236 		printf("The operating system has halted.\n");
237 		printf("Please press any key to reboot.\n\n");
238 		cngetc();
239 		printf("rebooting...\n");
240 		cpu_reset();
241 		/*NOTREACHED*/
242 	}
243 
244 	/* Disable console buffering */
245 /*	cnpollc(1);*/
246 
247 	/*
248 	 * If RB_NOSYNC was not specified sync the discs.
249 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
250 	 * It looks like syslogd is getting woken up only to find that it cannot
251 	 * page part of the binary in as the filesystem has been unmounted.
252 	 */
253 	if (!(howto & RB_NOSYNC))
254 		bootsync();
255 
256 	/* Say NO to interrupts */
257 	splhigh();
258 
259 	/* Do a dump if requested. */
260 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
261 		dumpsys();
262 
263 	/* Run any shutdown hooks */
264 	doshutdownhooks();
265 
266 	/* Make sure IRQ's are disabled */
267 	IRQdisable;
268 
269 	if (howto & RB_HALT) {
270 		printf("The operating system has halted.\n");
271 		printf("Please press any key to reboot.\n\n");
272 		cngetc();
273 	}
274 
275 	printf("rebooting...\n");
276 	cpu_reset();
277 	/*NOTREACHED*/
278 }
279 
280 static void
281 netwinder_reset(void)
282 {
283 	ISA_PUTBYTE(0x370, 0x07); 	/* Select Logical Dev 7 (GPIO) */
284 	ISA_PUTBYTE(0x371, 0x07);
285 	ISA_PUTBYTE(0x370, 0xe6);	/* Select GP16 Control Reg */
286 	ISA_PUTBYTE(0x371, 0x00);	/* Make GP16 an output */
287 	ISA_PUTBYTE(0x338, 0xc4);	/* Set GP17/GP16 & GP12 */
288 }
289 
290 /*
291  * Mapping table for core kernel memory. This memory is mapped at init
292  * time with section mappings.
293  */
294 struct l1_sec_map {
295 	vm_offset_t	va;
296 	vm_offset_t	pa;
297 	vm_size_t	size;
298 	vm_prot_t	prot;
299 	int		cache;
300 } l1_sec_table[] = {
301 	/* Map 1MB for CSR space */
302 	{ DC21285_ARMCSR_VBASE,			DC21285_ARMCSR_BASE,
303 	    DC21285_ARMCSR_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
304 	    PTE_NOCACHE },
305 
306 	/* Map 1MB for fast cache cleaning space */
307 	{ DC21285_CACHE_FLUSH_VBASE,		DC21285_SA_CACHE_FLUSH_BASE,
308 	    DC21285_CACHE_FLUSH_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
309 	    PTE_CACHE },
310 
311 	/* Map 1MB for PCI IO space */
312 	{ DC21285_PCI_IO_VBASE,			DC21285_PCI_IO_BASE,
313 	    DC21285_PCI_IO_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
314 	    PTE_NOCACHE },
315 
316 	/* Map 1MB for PCI IACK space */
317 	{ DC21285_PCI_IACK_VBASE,		DC21285_PCI_IACK_SPECIAL,
318 	    DC21285_PCI_IACK_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
319 	    PTE_NOCACHE },
320 
321 	/* Map 16MB of type 1 PCI config access */
322 	{ DC21285_PCI_TYPE_1_CONFIG_VBASE,	DC21285_PCI_TYPE_1_CONFIG,
323 	    DC21285_PCI_TYPE_1_CONFIG_VSIZE,	VM_PROT_READ|VM_PROT_WRITE,
324 	    PTE_NOCACHE },
325 
326 	/* Map 16MB of type 0 PCI config access */
327 	{ DC21285_PCI_TYPE_0_CONFIG_VBASE,	DC21285_PCI_TYPE_0_CONFIG,
328 	    DC21285_PCI_TYPE_0_CONFIG_VSIZE,	VM_PROT_READ|VM_PROT_WRITE,
329 	    PTE_NOCACHE },
330 
331 	/* Map 1MB of 32 bit PCI address space for ISA MEM accesses via PCI */
332 	{ DC21285_PCI_ISA_MEM_VBASE,		DC21285_PCI_MEM_BASE,
333 	    DC21285_PCI_ISA_MEM_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
334 	    PTE_NOCACHE },
335 
336 	{ 0, 0, 0, 0, 0 }
337 };
338 
339 /*
340  * u_int initarm(void);
341  *
342  * Initial entry point on startup. This gets called before main() is
343  * entered.
344  * It should be responsible for setting up everything that must be
345  * in place when main is called.
346  * This includes
347  *   Taking a copy of the boot configuration structure.
348  *   Initialising the physical console so characters can be printed.
349  *   Setting up page tables for the kernel
350  *   Relocating the kernel to the bottom of physical memory
351  */
352 
353 u_int
354 initarm(void)
355 {
356 	int loop;
357 	int loop1;
358 	u_int l1pagetable;
359 	extern char _end[];
360 	pv_addr_t kernel_l1pt;
361 	pv_addr_t kernel_ptpt;
362 
363 	/*
364 	 * Set up a diagnostic console so we can see what's going
365 	 * on.
366 	 */
367 	cn_tab = &kcomcons;
368 
369 	/* Talk to the user */
370 	printf("\nNetBSD/netwinder booting ...\n");
371 
372 	/*
373 	 * Heads up ... Setup the CPU / MMU / TLB functions
374 	 */
375 	if (set_cpufuncs())
376 		panic("cpu not recognized!");
377 
378 	/*
379 	 * We are currently running with the MMU enabled and the
380 	 * entire address space mapped VA==PA, except for the
381 	 * first 64MB of RAM is also double-mapped at 0xf0000000.
382 	 * There is an L1 page table at 0x00008000.
383 	 *
384 	 * We also have the 21285's PCI I/O space mapped where
385 	 * we expect it.
386 	 */
387 
388 	printf("initarm: Configuring system ...\n");
389 
390 	/*
391 	 * Copy out the boot info passed by the firmware.  Note that
392 	 * early versions of NeTTrom fill this in with bogus values,
393 	 * so we need to sanity check it.
394 	 */
395 	memcpy(&nwbootinfo, (caddr_t)(KERNEL_BASE + 0x100),
396 	    sizeof(nwbootinfo));
397 #ifdef VERBOSE_INIT_ARM
398 	printf("NeTTrom boot info:\n");
399 	printf("\tpage size = 0x%08lx\n", nwbootinfo.bi_pagesize);
400 	printf("\tnpages = %ld (0x%08lx)\n", nwbootinfo.bi_nrpages,
401 	    nwbootinfo.bi_nrpages);
402 	printf("\trootdev = 0x%08lx\n", nwbootinfo.bi_rootdev);
403 	printf("\tcmdline = %s\n", nwbootinfo.bi_cmdline);
404 #endif
405 	if (nwbootinfo.bi_nrpages != 0x02000 &&
406 	    nwbootinfo.bi_nrpages != 0x04000 &&
407 	    nwbootinfo.bi_nrpages != 0x08000 &&
408 	    nwbootinfo.bi_nrpages != 0x10000) {
409 		nwbootinfo.bi_pagesize = 0xdeadbeef;
410 		nwbootinfo.bi_nrpages = 0x01000;	/* 16MB */
411 		nwbootinfo.bi_rootdev = 0;
412 	}
413 
414 	/* Fake bootconfig structure for the benefit of pmap.c */
415 	/* XXX must make the memory description h/w independant */
416 	bootconfig.dramblocks = 1;
417 	bootconfig.dram[0].address = 0;
418 	bootconfig.dram[0].pages = nwbootinfo.bi_nrpages;
419 
420 	/*
421 	 * Set up the variables that define the availablilty of
422 	 * physical memory.
423 	 *
424 	 * Since the NetWinder NeTTrom doesn't load ELF symbols
425 	 * for us, we can safely assume that everything after end[]
426 	 * is free.  We start there and allocate upwards.
427 	 */
428 	physical_start = bootconfig.dram[0].address;
429 	physical_end = physical_start + (bootconfig.dram[0].pages * NBPG);
430 
431 	physical_freestart = ((((vaddr_t) _end) + PGOFSET) & ~PGOFSET) -
432 	    KERNEL_BASE;
433 	physical_freeend = physical_end;
434 	free_pages = (physical_freeend - physical_freestart) / NBPG;
435 
436 #ifdef VERBOSE_INIT_ARM
437 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
438 	       physical_freestart, free_pages, free_pages);
439 #endif
440 
441 	physmem = (physical_end - physical_start) / NBPG;
442 
443 	/* Tell the user about the memory */
444 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
445 	    physical_start, physical_end - 1);
446 
447 	/*
448 	 * Okay, we need to allocate some fixed page tables to get the
449 	 * kernel going.  We allocate one page directory and a number
450 	 * of page tables and store the physical addresses in the
451 	 * kernel_pt_table array.
452 	 *
453 	 * The kernel page directory must be on a 16K boundary.  The page
454 	 * tables must be on 4K boundaries.  What we do is allocate the
455 	 * page directory on the first 16K boundary that we encounter,
456 	 * and the page tables on 4K boundaries otherwise.  Since we
457 	 * allocate at least 3 L2 page tables, we are guaranteed to
458 	 * encounter at least one 16K aligned region.
459 	 */
460 
461 #ifdef VERBOSE_INIT_ARM
462 	printf("Allocating page tables\n");
463 #endif
464 
465 	/* Define a macro to simplify memory allocation */
466 #define	valloc_pages(var, np)			\
467 	alloc_pages((var).pv_pa, (np));		\
468 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
469 
470 #define alloc_pages(var, np)			\
471 	(var) = physical_freestart;		\
472 	physical_freestart += ((np) * NBPG);	\
473 	free_pages -= (np);			\
474 	memset((char *)(var), 0, ((np) * NBPG));
475 
476 	loop1 = 0;
477 	kernel_l1pt.pv_pa = 0;
478 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
479 		/* Are we 16KB aligned for an L1 ? */
480 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
481 		    && kernel_l1pt.pv_pa == 0) {
482 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
483 		} else {
484 			alloc_pages(kernel_pt_table[loop1].pv_pa,
485 			    L2_TABLE_SIZE / NBPG);
486 			kernel_pt_table[loop1].pv_va =
487 			    kernel_pt_table[loop1].pv_pa;
488 			++loop1;
489 		}
490 	}
491 
492 	/* This should never be able to happen but better confirm that. */
493 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
494 		panic("initarm: Failed to align the kernel page directory\n");
495 
496 	/*
497 	 * Allocate a page for the system page mapped to V0x00000000
498 	 * This page will just contain the system vectors and can be
499 	 * shared by all processes.
500 	 */
501 	alloc_pages(systempage.pv_pa, 1);
502 
503 	/* Allocate a page for the page table to map kernel page tables*/
504 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
505 
506 	/* Allocate stacks for all modes */
507 	valloc_pages(irqstack, IRQ_STACK_SIZE);
508 	valloc_pages(abtstack, ABT_STACK_SIZE);
509 	valloc_pages(undstack, UND_STACK_SIZE);
510 	valloc_pages(kernelstack, UPAGES);
511 
512 #ifdef VERBOSE_INIT_ARM
513 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
514 	    irqstack.pv_va);
515 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
516 	    abtstack.pv_va);
517 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
518 	    undstack.pv_va);
519 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
520 	    kernelstack.pv_va);
521 #endif
522 
523 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
524 
525 	/*
526 	 * Ok we have allocated physical pages for the primary kernel
527 	 * page tables
528 	 */
529 
530 #ifdef VERBOSE_INIT_ARM
531 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
532 #endif
533 
534 	/*
535 	 * Now we start consturction of the L1 page table
536 	 * We start by mapping the L2 page tables into the L1.
537 	 * This means that we can replace L1 mappings later on if necessary
538 	 */
539 	l1pagetable = kernel_l1pt.pv_pa;
540 
541 	/* Map the L2 pages tables in the L1 page table */
542 	pmap_link_l2pt(l1pagetable, 0x00000000,
543 	    &kernel_pt_table[KERNEL_PT_SYS]);
544 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
545 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
546 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
547 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
548 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
549 	pmap_link_l2pt(l1pagetable, PTE_BASE, &kernel_ptpt);
550 
551 	/* update the top of the kernel VM */
552 	pmap_curmaxkvaddr =
553 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
554 
555 #ifdef VERBOSE_INIT_ARM
556 	printf("Mapping kernel\n");
557 #endif
558 
559 	/* Now we fill in the L2 pagetable for the kernel static code/data */
560 	{
561 		/*
562 		 * The kernel starts in the first 1MB of RAM, and we'd
563 		 * like to use a section mapping for text, so we'll just
564 		 * map from KERNEL_BASE to etext[] to _end[].
565 		 */
566 
567 		extern char etext[];
568 		size_t textsize = (uintptr_t) etext - KERNEL_BASE;
569 		size_t totalsize = (uintptr_t) _end - KERNEL_BASE;
570 		u_int logical;
571 
572 		textsize = (textsize + PGOFSET) & ~PGOFSET;
573 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
574 
575 		textsize = textsize & ~PGOFSET;
576 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
577 
578 		logical = 0;		/* offset into RAM */
579 
580 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
581 		    physical_start + logical, textsize,
582 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
583 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
584 		    physical_start + logical, totalsize - textsize,
585 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
586 	}
587 
588 #ifdef VERBOSE_INIT_ARM
589 	printf("Constructing L2 page tables\n");
590 #endif
591 
592 	/* Map the stack pages */
593 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
594 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
595 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
596 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
597 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
598 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
599 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
600 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
601 
602 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
603 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
604 
605 	/* Map the page table that maps the kernel pages */
606 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
607 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
608 
609 	/*
610 	 * Map entries in the page table used to map PTE's
611 	 * Basically every kernel page table gets mapped here
612 	 */
613 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
614 	pmap_map_entry(l1pagetable,
615 	    PTE_BASE + (KERNEL_BASE >> (PGSHIFT-2)),
616 	    kernel_pt_table[KERNEL_PT_KERNEL].pv_pa,
617 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
618 	pmap_map_entry(l1pagetable,
619 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
620 	    kernel_ptpt.pv_pa,
621 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
622 	pmap_map_entry(l1pagetable,
623 	    PTE_BASE + (0x00000000 >> (PGSHIFT-2)),
624 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa,
625 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
626 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
627 		pmap_map_entry(l1pagetable,
628 		    PTE_BASE + ((KERNEL_VM_BASE +
629 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
630 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
631 		    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
632 
633 	/* Map the vector page. */
634 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
635 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
636 
637 	/*
638 	 * Map devices we can map w/ section mappings.
639 	 */
640 	loop = 0;
641 	while (l1_sec_table[loop].size) {
642 		vm_size_t sz;
643 
644 #ifdef VERBOSE_INIT_ARM
645 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
646 		    l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
647 		    l1_sec_table[loop].va);
648 #endif
649 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
650 			pmap_map_section(l1pagetable,
651 			    l1_sec_table[loop].va + sz,
652 			    l1_sec_table[loop].pa + sz,
653 			    l1_sec_table[loop].prot,
654 			    l1_sec_table[loop].cache);
655 		++loop;
656 	}
657 
658 	/*
659 	 * Now we have the real page tables in place so we can switch to them.
660 	 * Once this is done we will be running with the REAL kernel page
661 	 * tables.
662 	 */
663 
664 	/* Switch tables */
665 #ifdef VERBOSE_INIT_ARM
666 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
667 	       physical_freestart, free_pages, free_pages);
668 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
669 #endif
670 
671 	setttb(kernel_l1pt.pv_pa);
672 
673 #ifdef VERBOSE_INIT_ARM
674 	printf("done!\n");
675 #endif
676 
677 	/*
678 	 * XXX this should only be done in main() but it useful to
679 	 * have output earlier ...
680 	 */
681 	consinit();
682 
683 #ifdef VERBOSE_INIT_ARM
684 	printf("bootstrap done.\n");
685 #endif
686 
687 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
688 
689 	/*
690 	 * Pages were allocated during the secondary bootstrap for the
691 	 * stacks for different CPU modes.
692 	 * We must now set the r13 registers in the different CPU modes to
693 	 * point to these stacks.
694 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
695 	 * of the stack memory.
696 	 */
697 	printf("init subsystems: stacks ");
698 
699 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
700 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
701 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
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 slighly better one.
711 	 */
712 	printf("vectors ");
713 	data_abort_handler_address = (u_int)data_abort_handler;
714 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
715 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
716 
717 	/* Initialise the undefined instruction handlers */
718 	printf("undefined ");
719 	undefined_init();
720 
721 	/* Boot strap pmap telling it where the kernel page table is */
722 	printf("pmap ");
723 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
724 
725 	/* Setup the IRQ system */
726 	printf("irq ");
727 	irq_init();
728 	printf("done.\n");
729 
730 	/*
731 	 * Warn the user if the bootinfo was bogus.  We already
732 	 * faked up some safe values.
733 	 */
734 	if (nwbootinfo.bi_pagesize == 0xdeadbeef)
735 		printf("WARNING: NeTTrom boot info corrupt\n");
736 
737 #ifdef IPKDB
738 	/* Initialise ipkdb */
739 	ipkdb_init();
740 	if (boothowto & RB_KDB)
741 		ipkdb_connect(0);
742 #endif
743 
744 #ifdef DDB
745 	db_machine_init();
746 
747 	/* Firmware doesn't load symbols. */
748 	ddb_init(0, NULL, NULL);
749 
750 	if (boothowto & RB_KDB)
751 		Debugger();
752 #endif
753 
754 	/* We return the new stack pointer address */
755 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
756 }
757 
758 void
759 process_kernel_args(args)
760 	char *args;
761 {
762 
763 	boothowto = 0;
764 
765 	/* Make a local copy of the bootargs */
766 	strncpy(bootargs, args, MAX_BOOT_STRING);
767 
768 	args = bootargs;
769 	boot_file = bootargs;
770 
771 	/* Skip the kernel image filename */
772 	while (*args != ' ' && *args != 0)
773 		++args;
774 
775 	if (*args != 0)
776 		*args++ = 0;
777 
778 	while (*args == ' ')
779 		++args;
780 
781 	boot_args = args;
782 
783 	printf("bootfile: %s\n", boot_file);
784 	printf("bootargs: %s\n", boot_args);
785 
786 	parse_mi_bootargs(boot_args);
787 }
788 
789 extern struct bus_space footbridge_pci_io_bs_tag;
790 extern struct bus_space footbridge_pci_mem_bs_tag;
791 void footbridge_pci_bs_tag_init __P((void));
792 
793 void
794 consinit(void)
795 {
796 	static int consinit_called = 0;
797 	char *console = CONSDEVNAME;
798 
799 	if (consinit_called != 0)
800 		return;
801 
802 	consinit_called = 1;
803 
804 #if NISA > 0
805 	/* Initialise the ISA subsystem early ... */
806 	isa_netwinder_init(DC21285_PCI_IO_VBASE, DC21285_PCI_ISA_MEM_VBASE);
807 #endif
808 
809 	footbridge_pci_bs_tag_init();
810 
811 	if (strncmp(console, "vga", 3) == 0) {
812 #if (NVGA > 0)
813 		vga_cnattach(&footbridge_pci_io_bs_tag,
814 		    &footbridge_pci_mem_bs_tag, - 1, 0);
815 #if (NPCKBC > 0)
816 		pckbc_cnattach(&isa_io_bs_tag, IO_KBD, KBCMDP, PCKBC_KBD_SLOT);
817 #endif	/* NPCKBC */
818 #else
819 		panic("vga console not configured");
820 #endif	/* NVGA */
821 	} else {
822 #if (NCOM > 0)
823 		if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
824 		    COM_FREQ, comcnmode))
825 			panic("can't init serial console @%x", CONCOMADDR);
826 #else
827 			panic("serial console @%x not configured", CONCOMADDR);
828 #endif
829 	}
830 }
831 
832 static bus_space_handle_t kcom_base = (bus_space_handle_t) (DC21285_PCI_IO_VBASE + CONCOMADDR);
833 
834 #define	KCOM_GETBYTE(r)		generic_bs_r_1(0, kcom_base, (r))
835 #define	KCOM_PUTBYTE(r,v)	generic_bs_w_1(0, kcom_base, (r), (v))
836 
837 static int
838 kcomcngetc(dev_t dev)
839 {
840 	int stat, c;
841 
842 	/* block until a character becomes available */
843 	while (!ISSET(stat = KCOM_GETBYTE(com_lsr), LSR_RXRDY))
844 		;
845 
846 	c = KCOM_GETBYTE(com_data);
847 	stat = KCOM_GETBYTE(com_iir);
848 	return c;
849 }
850 
851 /*
852  * Console kernel output character routine.
853  */
854 static void
855 kcomcnputc(dev_t dev, int c)
856 {
857 	int timo;
858 
859 	/* wait for any pending transmission to finish */
860 	timo = 150000;
861 	while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
862 		continue;
863 
864 	KCOM_PUTBYTE(com_data, c);
865 
866 	/* wait for this transmission to complete */
867 	timo = 1500000;
868 	while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
869 		continue;
870 }
871 
872 static void
873 kcomcnpollc(dev_t dev, int on)
874 {
875 }
876 
877 struct consdev kcomcons = {
878 	NULL, NULL, kcomcngetc, kcomcnputc, kcomcnpollc, NULL,
879 	NODEV, CN_NORMAL
880 };
881