xref: /netbsd/sys/arch/cats/cats/cats_machdep.c (revision c4a72b64)
1 /*	$NetBSD: cats_machdep.c,v 1.37 2002/11/03 21:43:31 chris 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 cyclone firmware
38  *
39  * Created      : 24/11/97
40  */
41 
42 #include "opt_ddb.h"
43 #include "opt_pmap_debug.h"
44 
45 #include "isadma.h"
46 
47 #include <sys/param.h>
48 #include <sys/device.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/exec.h>
52 #include <sys/proc.h>
53 #include <sys/msgbuf.h>
54 #include <sys/reboot.h>
55 #include <sys/termios.h>
56 
57 #include <dev/cons.h>
58 
59 #include <machine/db_machdep.h>
60 #include <ddb/db_sym.h>
61 #include <ddb/db_extern.h>
62 
63 #include <machine/bootconfig.h>
64 #define	_ARM32_BUS_DMA_PRIVATE
65 #include <machine/bus.h>
66 #include <machine/cpu.h>
67 #include <machine/frame.h>
68 #include <machine/intr.h>
69 #include <arm/undefined.h>
70 #include <arm/arm32/machdep.h>
71 
72 #include <machine/cyclone_boot.h>
73 #include <arm/footbridge/dc21285mem.h>
74 #include <arm/footbridge/dc21285reg.h>
75 
76 #include "opt_ipkdb.h"
77 
78 #include "isa.h"
79 #if NISA > 0
80 #include <dev/isa/isareg.h>
81 #include <dev/isa/isavar.h>
82 #endif
83 
84 
85 /*
86  * Address to call from cpu_reset() to reset the machine.
87  * This is machine architecture dependant as it varies depending
88  * on where the ROM appears when you turn the MMU off.
89  */
90 
91 u_int cpu_reset_address = DC21285_ROM_BASE;
92 
93 u_int dc21285_fclk = FCLK;
94 
95 /* Define various stack sizes in pages */
96 #define IRQ_STACK_SIZE	1
97 #define ABT_STACK_SIZE	1
98 #ifdef IPKDB
99 #define UND_STACK_SIZE	2
100 #else
101 #define UND_STACK_SIZE	1
102 #endif
103 
104 struct ebsaboot ebsabootinfo;
105 BootConfig bootconfig;		/* Boot config storage */
106 static char bootargs[MAX_BOOT_STRING + 1];
107 char *boot_args = NULL;
108 char *boot_file = NULL;
109 
110 vm_offset_t physical_start;
111 vm_offset_t physical_freestart;
112 vm_offset_t physical_freeend;
113 vm_offset_t physical_end;
114 u_int free_pages;
115 vm_offset_t pagetables_start;
116 int physmem = 0;
117 
118 /*int debug_flags;*/
119 #ifndef PMAP_STATIC_L1S
120 int max_processes = 64;			/* Default number */
121 #endif	/* !PMAP_STATIC_L1S */
122 
123 /* Physical and virtual addresses for some global pages */
124 pv_addr_t systempage;
125 pv_addr_t irqstack;
126 pv_addr_t undstack;
127 pv_addr_t abtstack;
128 pv_addr_t kernelstack;
129 
130 vm_offset_t msgbufphys;
131 
132 extern u_int data_abort_handler_address;
133 extern u_int prefetch_abort_handler_address;
134 extern u_int undefined_handler_address;
135 
136 #ifdef PMAP_DEBUG
137 extern int pmap_debug_level;
138 #endif
139 
140 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
141 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
142 #define	KERNEL_PT_KERNEL_NUM	2
143 
144 /* now this could move into something more generic */
145 					/* L2 tables for mapping kernel VM */
146 #define	KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
147 #define	KERNEL_PT_VMDATA_NUM	4	/* 16MB kernel VM !*/
148 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
149 
150 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
151 
152 struct user *proc0paddr;
153 
154 /* Prototypes */
155 
156 void consinit		__P((void));
157 
158 int fcomcnattach __P((u_int iobase, int rate,tcflag_t cflag));
159 int fcomcndetach __P((void));
160 
161 static void process_kernel_args	__P((char *));
162 extern void configure		__P((void));
163 
164 /* A load of console goo. */
165 #include "vga.h"
166 #if (NVGA > 0)
167 #include <dev/ic/mc6845reg.h>
168 #include <dev/ic/pcdisplayvar.h>
169 #include <dev/ic/vgareg.h>
170 #include <dev/ic/vgavar.h>
171 #endif
172 
173 #include "pckbc.h"
174 #if (NPCKBC > 0)
175 #include <dev/ic/i8042reg.h>
176 #include <dev/ic/pckbcvar.h>
177 #endif
178 
179 #include "com.h"
180 #if (NCOM > 0)
181 #include <dev/ic/comreg.h>
182 #include <dev/ic/comvar.h>
183 #ifndef CONCOMADDR
184 #define CONCOMADDR 0x3f8
185 #endif
186 #endif
187 
188 #ifndef CONSDEVNAME
189 #define CONSDEVNAME "vga"
190 #endif
191 
192 #define CONSPEED B38400
193 #ifndef CONSPEED
194 #define CONSPEED B9600	/* TTYDEF_SPEED */
195 #endif
196 #ifndef CONMODE
197 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
198 #endif
199 
200 int comcnspeed = CONSPEED;
201 int comcnmode = CONMODE;
202 
203 
204 /*
205  * void cpu_reboot(int howto, char *bootstr)
206  *
207  * Reboots the system
208  *
209  * Deal with any syncing, unmounting, dumping and shutdown hooks,
210  * then reset the CPU.
211  */
212 
213 void
214 cpu_reboot(howto, bootstr)
215 	int howto;
216 	char *bootstr;
217 {
218 #ifdef DIAGNOSTIC
219 	/* info */
220 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
221 #endif
222 
223 	/*
224 	 * If we are still cold then hit the air brakes
225 	 * and crash to earth fast
226 	 */
227 	if (cold) {
228 		doshutdownhooks();
229 		printf("The operating system has halted.\n");
230 		printf("Please press any key to reboot.\n\n");
231 		cngetc();
232 		printf("rebooting...\n");
233 		cpu_reset();
234 		/*NOTREACHED*/
235 	}
236 
237 	/* Disable console buffering */
238 /*	cnpollc(1);*/
239 
240 	/*
241 	 * If RB_NOSYNC was not specified sync the discs.
242 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
243 	 * It looks like syslogd is getting woken up only to find that it cannot
244 	 * page part of the binary in as the filesystem has been unmounted.
245 	 */
246 	if (!(howto & RB_NOSYNC))
247 		bootsync();
248 
249 	/* Say NO to interrupts */
250 	splhigh();
251 
252 	/* Do a dump if requested. */
253 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
254 		dumpsys();
255 
256 	/* Run any shutdown hooks */
257 	doshutdownhooks();
258 
259 	/* Make sure IRQ's are disabled */
260 	IRQdisable;
261 
262 	if (howto & RB_HALT) {
263 		printf("The operating system has halted.\n");
264 		printf("Please press any key to reboot.\n\n");
265 		cngetc();
266 	}
267 
268 	printf("rebooting...\n");
269 	cpu_reset();
270 	/*NOTREACHED*/
271 }
272 
273 /*
274  * Mapping table for core kernel memory. This memory is mapped at init
275  * time with section mappings.
276  */
277 struct l1_sec_map {
278 	vm_offset_t	va;
279 	vm_offset_t	pa;
280 	vm_size_t	size;
281 	vm_prot_t	prot;
282 	int		cache;
283 } l1_sec_table[] = {
284 	/* Map 1MB for CSR space */
285 	{ DC21285_ARMCSR_VBASE,			DC21285_ARMCSR_BASE,
286 	    DC21285_ARMCSR_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
287 	    PTE_NOCACHE },
288 
289 	/* Map 1MB for fast cache cleaning space */
290 	{ DC21285_CACHE_FLUSH_VBASE,		DC21285_SA_CACHE_FLUSH_BASE,
291 	    DC21285_CACHE_FLUSH_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
292 	    PTE_CACHE },
293 
294 	/* Map 1MB for PCI IO space */
295 	{ DC21285_PCI_IO_VBASE,			DC21285_PCI_IO_BASE,
296 	    DC21285_PCI_IO_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
297 	    PTE_NOCACHE },
298 
299 	/* Map 1MB for PCI IACK space */
300 	{ DC21285_PCI_IACK_VBASE,		DC21285_PCI_IACK_SPECIAL,
301 	    DC21285_PCI_IACK_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
302 	    PTE_NOCACHE },
303 
304 	/* Map 16MB of type 1 PCI config access */
305 	{ DC21285_PCI_TYPE_1_CONFIG_VBASE,	DC21285_PCI_TYPE_1_CONFIG,
306 	    DC21285_PCI_TYPE_1_CONFIG_VSIZE,	VM_PROT_READ|VM_PROT_WRITE,
307 	    PTE_NOCACHE },
308 
309 	/* Map 16MB of type 0 PCI config access */
310 	{ DC21285_PCI_TYPE_0_CONFIG_VBASE,	DC21285_PCI_TYPE_0_CONFIG,
311 	    DC21285_PCI_TYPE_0_CONFIG_VSIZE,	VM_PROT_READ|VM_PROT_WRITE,
312 	    PTE_NOCACHE },
313 
314 	/* Map 1MB of 32 bit PCI address space for ISA MEM accesses via PCI */
315 	{ DC21285_PCI_ISA_MEM_VBASE,		DC21285_PCI_MEM_BASE,
316 	    DC21285_PCI_ISA_MEM_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
317 	    PTE_NOCACHE },
318 
319 	{ 0, 0, 0, 0, 0 }
320 };
321 
322 /*
323  * u_int initarm(struct ebsaboot *bootinfo)
324  *
325  * Initial entry point on startup. This gets called before main() is
326  * entered.
327  * It should be responsible for setting up everything that must be
328  * in place when main is called.
329  * This includes
330  *   Taking a copy of the boot configuration structure.
331  *   Initialising the physical console so characters can be printed.
332  *   Setting up page tables for the kernel
333  *   Relocating the kernel to the bottom of physical memory
334  */
335 
336 u_int
337 initarm(bootargs)
338 	void *bootargs;
339 {
340 	struct ebsaboot *bootinfo = bootargs;
341 	int loop;
342 	int loop1;
343 	u_int logical;
344 	u_int l1pagetable;
345 	struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
346 	pv_addr_t kernel_l1pt;
347 	pv_addr_t kernel_ptpt;
348 
349 	/*
350 	 * Heads up ... Setup the CPU / MMU / TLB functions
351 	 */
352 	set_cpufuncs();
353 
354 	/* Copy the boot configuration structure */
355 	ebsabootinfo = *bootinfo;
356 
357 	if (ebsabootinfo.bt_fclk >= 50000000
358 	    && ebsabootinfo.bt_fclk <= 66000000)
359 		dc21285_fclk = ebsabootinfo.bt_fclk;
360 
361 	/* Fake bootconfig structure for the benefit of pmap.c */
362 	/* XXX must make the memory description h/w independant */
363 	bootconfig.dramblocks = 1;
364 	bootconfig.dram[0].address = ebsabootinfo.bt_memstart;
365 	bootconfig.dram[0].pages = (ebsabootinfo.bt_memend
366 	    - ebsabootinfo.bt_memstart) / NBPG;
367 
368 	/*
369 	 * Initialise the diagnostic serial console
370 	 * This allows a means of generating output during initarm().
371 	 * Once all the memory map changes are complete we can call consinit()
372 	 * and not have to worry about things moving.
373 	 */
374 /*	fcomcnattach(DC21285_ARMCSR_BASE, comcnspeed, comcnmode);*/
375 
376 	/* Talk to the user */
377 	printf("NetBSD/cats booting ...\n");
378 
379 	if (ebsabootinfo.bt_magic != BT_MAGIC_NUMBER_EBSA
380 	    && ebsabootinfo.bt_magic != BT_MAGIC_NUMBER_CATS)
381 		panic("Incompatible magic number passed in boot args");
382 
383 /*	{
384 	int loop;
385 	for (loop = 0; loop < 8; ++loop) {
386 		printf("%08x\n", *(((int *)bootinfo)+loop));
387 	}
388 	}*/
389 
390 	/*
391 	 * Ok we have the following memory map
392 	 *
393 	 * virtual address == physical address apart from the areas:
394 	 * 0x00000000 -> 0x000fffff which is mapped to
395 	 * top 1MB of physical memory
396 	 * 0x00100000 -> 0x0fffffff which is mapped to
397 	 * physical addresses 0x00100000 -> 0x0fffffff
398 	 * 0x10000000 -> 0x1fffffff which is mapped to
399 	 * physical addresses 0x00000000 -> 0x0fffffff
400 	 * 0x20000000 -> 0xefffffff which is mapped to
401 	 * physical addresses 0x20000000 -> 0xefffffff
402 	 * 0xf0000000 -> 0xf03fffff which is mapped to
403 	 * physical addresses 0x00000000 -> 0x003fffff
404 	 *
405 	 * This means that the kernel is mapped suitably for continuing
406 	 * execution, all I/O is mapped 1:1 virtual to physical and
407 	 * physical memory is accessible.
408 	 *
409 	 * The initarm() has the responsibility for creating the kernel
410 	 * page tables.
411 	 * It must also set up various memory pointers that are used
412 	 * by pmap etc.
413 	 */
414 
415 	/*
416 	 * Examine the boot args string for options we need to know about
417 	 * now.
418 	 */
419 	process_kernel_args((char *)ebsabootinfo.bt_args);
420 
421 	printf("initarm: Configuring system ...\n");
422 
423 	/*
424 	 * Set up the variables that define the availablilty of
425 	 * physical memory
426 	 */
427 	physical_start = ebsabootinfo.bt_memstart;
428 	physical_freestart = physical_start;
429 	physical_end = ebsabootinfo.bt_memend;
430 	physical_freeend = physical_end;
431 	free_pages = (physical_end - physical_start) / NBPG;
432 
433 	physmem = (physical_end - physical_start) / NBPG;
434 
435 	/* Tell the user about the memory */
436 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
437 	    physical_start, physical_end - 1);
438 
439 	/*
440 	 * Ok the kernel occupies the bottom of physical memory.
441 	 * The first free page after the kernel can be found in
442 	 * ebsabootinfo->bt_memavail
443 	 * We now need to allocate some fixed page tables to get the kernel
444 	 * going.
445 	 * We allocate one page directory and a number page tables and store
446 	 * the physical addresses in the kernel_pt_table array.
447 	 *
448 	 * Ok the next bit of physical allocation may look complex but it is
449 	 * simple really. I have done it like this so that no memory gets
450 	 * wasted during the allocation of various pages and tables that are
451 	 * all different sizes.
452 	 * The start addresses will be page aligned.
453 	 * We allocate the kernel page directory on the first free 16KB boundry
454 	 * we find.
455 	 * We allocate the kernel page tables on the first 4KB boundry we find.
456 	 * Since we allocate at least 3 L2 pagetables we know that we must
457 	 * encounter at least one 16KB aligned address.
458 	 */
459 
460 #ifdef VERBOSE_INIT_ARM
461 	printf("Allocating page tables\n");
462 #endif
463 
464 	/* Update the address of the first free page of physical memory */
465 	physical_freestart = ebsabootinfo.bt_memavail;
466 	free_pages -= (physical_freestart - physical_start) / NBPG;
467 
468 	/* Define a macro to simplify memory allocation */
469 #define	valloc_pages(var, np)			\
470 	alloc_pages((var).pv_pa, (np));	\
471 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
472 
473 #define alloc_pages(var, np)			\
474 	(var) = physical_freestart;		\
475 	physical_freestart += ((np) * NBPG);	\
476 	free_pages -= (np);			\
477 	memset((char *)(var), 0, ((np) * NBPG));
478 
479 	loop1 = 0;
480 	kernel_l1pt.pv_pa = 0;
481 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
482 		/* Are we 16KB aligned for an L1 ? */
483 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
484 		    && kernel_l1pt.pv_pa == 0) {
485 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
486 		} else {
487 			alloc_pages(kernel_pt_table[loop1].pv_pa,
488 			    L2_TABLE_SIZE / NBPG);
489 			kernel_pt_table[loop1].pv_va =
490 			    kernel_pt_table[loop1].pv_pa;
491 			++loop1;
492 		}
493 	}
494 
495 #ifdef DIAGNOSTIC
496 	/* This should never be able to happen but better confirm that. */
497 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
498 		panic("initarm: Failed to align the kernel page directory");
499 #endif
500 
501 	/*
502 	 * Allocate a page for the system page mapped to V0x00000000
503 	 * This page will just contain the system vectors and can be
504 	 * shared by all processes.
505 	 */
506 	alloc_pages(systempage.pv_pa, 1);
507 
508 	/* Allocate a page for the page table to map kernel page tables*/
509 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
510 
511 	/* Allocate stacks for all modes */
512 	valloc_pages(irqstack, IRQ_STACK_SIZE);
513 	valloc_pages(abtstack, ABT_STACK_SIZE);
514 	valloc_pages(undstack, UND_STACK_SIZE);
515 	valloc_pages(kernelstack, UPAGES);
516 
517 #ifdef VERBOSE_INIT_ARM
518 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
519 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
520 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
521 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
522 #endif
523 
524 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
525 
526 	/*
527 	 * Ok we have allocated physical pages for the primary kernel
528 	 * page tables
529 	 */
530 
531 #ifdef VERBOSE_INIT_ARM
532 	printf("Creating L1 page table\n");
533 #endif
534 
535 	/*
536 	 * Now we start consturction of the L1 page table
537 	 * We start by mapping the L2 page tables into the L1.
538 	 * This means that we can replace L1 mappings later on if necessary
539 	 */
540 	l1pagetable = kernel_l1pt.pv_pa;
541 
542 	/* Map the L2 pages tables in the L1 page table */
543 	pmap_link_l2pt(l1pagetable, 0x00000000,
544 	    &kernel_pt_table[KERNEL_PT_SYS]);
545 
546 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
547 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
548 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
549 
550 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
551 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
552 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
553 
554 	/* update the top of the kernel VM */
555 	pmap_curmaxkvaddr =
556 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
557 
558 	pmap_link_l2pt(l1pagetable, PTE_BASE, &kernel_ptpt);
559 
560 #ifdef VERBOSE_INIT_ARM
561 	printf("Mapping kernel\n");
562 #endif
563 
564 	/* Now we fill in the L2 pagetable for the kernel static code/data */
565 
566 	if (N_GETMAGIC(kernexec[0]) != ZMAGIC)
567 		panic("Illegal kernel format");
568 	else {
569 		extern int end;
570 
571 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
572 			physical_start, kernexec->a_text,
573 			VM_PROT_READ, PTE_CACHE);
574 		logical += pmap_map_chunk(l1pagetable,
575 			KERNEL_TEXT_BASE + logical,
576 			physical_start + logical, kernexec->a_data,
577 			VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
578 		logical += pmap_map_chunk(l1pagetable,
579 			KERNEL_TEXT_BASE + logical,
580 			physical_start + logical, kernexec->a_bss,
581 			VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
582 		logical += pmap_map_chunk(l1pagetable,
583 			KERNEL_TEXT_BASE + logical,
584 			physical_start + logical, kernexec->a_syms + sizeof(int)
585 			+ *(u_int *)((int)&end + kernexec->a_syms + sizeof(int)),
586 			VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
587 	}
588 
589 	/*
590 	 * PATCH PATCH ...
591 	 *
592 	 * Fixup the first word of the kernel to be the instruction
593 	 * add pc, pc, #0x41000000
594 	 *
595 	 * This traps the case where the CPU core resets due to bus contention
596 	 * on a prototype CATS system and will reboot into the firmware.
597 	 */
598 	*((u_int *)KERNEL_TEXT_BASE) = 0xe28ff441;
599 
600 #ifdef VERBOSE_INIT_ARM
601 	printf("Constructing L2 page tables\n");
602 #endif
603 
604 	/* Map the boot arguments page */
605 	pmap_map_entry(l1pagetable, ebsabootinfo.bt_vargp,
606 	    ebsabootinfo.bt_pargp, VM_PROT_READ, PTE_CACHE);
607 
608 	/* Map the stack pages */
609 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
610 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
611 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
612 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
613 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
614 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
615 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
616 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
617 
618 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
619 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
620 
621 	/* Map the page table that maps the kernel pages */
622 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
623 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
624 
625 	/*
626 	 * Map entries in the page table used to map PTE's
627 	 * Basically every kernel page table gets mapped here
628 	 */
629 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
630 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
631 		pmap_map_entry(l1pagetable,
632 		    PTE_BASE + ((KERNEL_BASE +
633 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
634 		    kernel_pt_table[KERNEL_PT_KERNEL + loop].pv_pa,
635 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
636 
637 	pmap_map_entry(l1pagetable,
638 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
639 	    kernel_ptpt.pv_pa,
640 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
641 	pmap_map_entry(l1pagetable,
642 	    PTE_BASE + (0x00000000 >> (PGSHIFT-2)),
643 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa,
644 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
645 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
646 		pmap_map_entry(l1pagetable,
647 		    PTE_BASE + ((KERNEL_VM_BASE +
648 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
649 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
650 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
651 
652 	/* Map the vector page. */
653 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
654 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
655 
656 	/* Map the core memory needed before autoconfig */
657 	loop = 0;
658 	while (l1_sec_table[loop].size) {
659 		vm_size_t sz;
660 
661 #ifdef VERBOSE_INIT_ARM
662 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
663 		    l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
664 		    l1_sec_table[loop].va);
665 #endif
666 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
667 			pmap_map_section(l1pagetable,
668 			    l1_sec_table[loop].va + sz,
669 			    l1_sec_table[loop].pa + sz,
670 			    l1_sec_table[loop].prot,
671 			    l1_sec_table[loop].cache);
672 		++loop;
673 	}
674 
675 	/*
676 	 * Now we have the real page tables in place so we can switch to them.
677 	 * Once this is done we will be running with the REAL kernel page tables.
678 	 */
679 
680 	/* Switch tables */
681 #ifdef VERBOSE_INIT_ARM
682 	printf("switching to new L1 page table\n");
683 #endif
684 
685 	setttb(kernel_l1pt.pv_pa);
686 
687 	/*
688 	 * Ok the DC21285 CSR registers have just moved.
689 	 * Detach the diagnostic serial port and reattach at the new address.
690 	 */
691 /*	fcomcndetach();*/
692 
693 	/*
694 	 * XXX this should only be done in main() but it useful to
695 	 * have output earlier ...
696 	 */
697 	consinit();
698 
699 #ifdef VERBOSE_INIT_ARM
700 	printf("bootstrap done.\n");
701 #endif
702 
703 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
704 
705 	/*
706 	 * Pages were allocated during the secondary bootstrap for the
707 	 * stacks for different CPU modes.
708 	 * We must now set the r13 registers in the different CPU modes to
709 	 * point to these stacks.
710 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
711 	 * of the stack memory.
712 	 */
713 	printf("init subsystems: stacks ");
714 
715 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
716 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
717 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
718 
719 	/*
720 	 * Well we should set a data abort handler.
721 	 * Once things get going this will change as we will need a proper handler.
722 	 * Until then we will use a handler that just panics but tells us
723 	 * why.
724 	 * Initialisation of the vectors will just panic on a data abort.
725 	 * This just fills in a slighly better one.
726 	 */
727 	printf("vectors ");
728 	data_abort_handler_address = (u_int)data_abort_handler;
729 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
730 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
731 
732 	/* At last !
733 	 * We now have the kernel in physical memory from the bottom upwards.
734 	 * Kernel page tables are physically above this.
735 	 * The kernel is mapped to KERNEL_TEXT_BASE
736 	 * The kernel data PTs will handle the mapping of 0xf1000000-0xf3ffffff
737 	 * The page tables are mapped to 0xefc00000
738 	 */
739 
740 	/* Initialise the undefined instruction handlers */
741 	printf("undefined ");
742 	undefined_init();
743 
744 	/* Load memory into UVM. */
745 	printf("page ");
746 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
747 
748 	/* XXX Always one RAM block -- nuke the loop. */
749 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
750 		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
751 		paddr_t end = start + (bootconfig.dram[loop].pages * NBPG);
752 #if NISADMA > 0
753 		paddr_t istart, isize;
754 		extern struct arm32_dma_range *footbridge_isa_dma_ranges;
755 		extern int footbridge_isa_dma_nranges;
756 #endif
757 
758 		if (start < physical_freestart)
759 			start = physical_freestart;
760 		if (end > physical_freeend)
761 			end = physical_freeend;
762 
763 #if 0
764 		printf("%d: %lx -> %lx\n", loop, start, end - 1);
765 #endif
766 
767 #if NISADMA > 0
768 		if (arm32_dma_range_intersect(footbridge_isa_dma_ranges,
769 					      footbridge_isa_dma_nranges,
770 					      start, end - start,
771 					      &istart, &isize)) {
772 			/*
773 			 * Place the pages that intersect with the
774 			 * ISA DMA range onto the ISA DMA free list.
775 			 */
776 #if 0
777 			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
778 			    istart + isize - 1);
779 #endif
780 			uvm_page_physload(atop(istart),
781 			    atop(istart + isize), atop(istart),
782 			    atop(istart + isize), VM_FREELIST_ISADMA);
783 
784 			/*
785 			 * Load the pieces that come before the
786 			 * intersection onto the default free list.
787 			 */
788 			if (start < istart) {
789 #if 0
790 				printf("    BEFORE 0x%lx -> 0x%lx\n",
791 				    start, istart - 1);
792 #endif
793 				uvm_page_physload(atop(start),
794 				    atop(istart), atop(start),
795 				    atop(istart), VM_FREELIST_DEFAULT);
796 			}
797 
798 			/*
799 			 * Load the pieces that come after the
800 			 * intersection onto the default free list.
801 			 */
802 			if ((istart + isize) < end) {
803 #if 0
804 				printf("     AFTER 0x%lx -> 0x%lx\n",
805 				    (istart + isize), end - 1);
806 #endif
807 				uvm_page_physload(atop(istart + isize),
808 				    atop(end), atop(istart + isize),
809 				    atop(end), VM_FREELIST_DEFAULT);
810 			}
811 		} else {
812 			uvm_page_physload(atop(start), atop(end),
813 			    atop(start), atop(end), VM_FREELIST_DEFAULT);
814 		}
815 #else /* NISADMA > 0 */
816 		uvm_page_physload(atop(start), atop(end),
817 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
818 #endif /* NISADMA > 0 */
819 	}
820 
821 	/* Boot strap pmap telling it where the kernel page table is */
822 	printf("pmap ");
823 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
824 
825 	/* Setup the IRQ system */
826 	printf("irq ");
827 	footbridge_intr_init();
828 	printf("done.\n");
829 
830 #ifdef IPKDB
831 	/* Initialise ipkdb */
832 	ipkdb_init();
833 	if (boothowto & RB_KDB)
834 		ipkdb_connect(0);
835 #endif
836 
837 #ifdef DDB
838 	db_machine_init();
839 #ifdef __ELF__
840 	/* ok this is really rather sick, in ELF what happens is that the
841 	 * ELF symbol table is added after the text section.
842 	 */
843 	ddb_init(0, NULL, NULL);	/* XXX */
844 #else
845 	{
846 		extern int end;
847 		extern int *esym;
848 
849 		ddb_init(*(int *)&end, ((int *)&end) + 1, esym);
850 	}
851 #endif /* __ELF__ */
852 
853 	if (boothowto & RB_KDB)
854 		Debugger();
855 #endif
856 
857 	/* We return the new stack pointer address */
858 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
859 }
860 
861 static void
862 process_kernel_args(args)
863 	char *args;
864 {
865 
866 	boothowto = 0;
867 
868 	/* Make a local copy of the bootargs */
869 	strncpy(bootargs, args, MAX_BOOT_STRING);
870 
871 	args = bootargs;
872 	boot_file = bootargs;
873 
874 	/* Skip the kernel image filename */
875 	while (*args != ' ' && *args != 0)
876 		++args;
877 
878 	if (*args != 0)
879 		*args++ = 0;
880 
881 	while (*args == ' ')
882 		++args;
883 
884 	boot_args = args;
885 
886 	printf("bootfile: %s\n", boot_file);
887 	printf("bootargs: %s\n", boot_args);
888 
889 	parse_mi_bootargs(boot_args);
890 }
891 
892 extern struct bus_space footbridge_pci_io_bs_tag;
893 extern struct bus_space footbridge_pci_mem_bs_tag;
894 void footbridge_pci_bs_tag_init __P((void));
895 
896 void
897 consinit(void)
898 {
899 	static int consinit_called = 0;
900 	char *console = CONSDEVNAME;
901 
902 	if (consinit_called != 0)
903 		return;
904 
905 	consinit_called = 1;
906 
907 #if NISA > 0
908 	/* Initialise the ISA subsystem early ... */
909 	isa_footbridge_init(DC21285_PCI_IO_VBASE, DC21285_PCI_ISA_MEM_VBASE);
910 #endif
911 
912 	footbridge_pci_bs_tag_init();
913 
914 	get_bootconf_option(boot_args, "console", BOOTOPT_TYPE_STRING,
915 	    &console);
916 
917 	if (strncmp(console, "fcom", 4) == 0
918 	    || strncmp(console, "diag", 4) == 0)
919 		fcomcnattach(DC21285_ARMCSR_VBASE, comcnspeed, comcnmode);
920 #if (NVGA > 0)
921 	else if (strncmp(console, "vga", 3) == 0) {
922 		vga_cnattach(&footbridge_pci_io_bs_tag,
923 		    &footbridge_pci_mem_bs_tag, - 1, 0);
924 #if (NPCKBC > 0)
925 		pckbc_cnattach(&isa_io_bs_tag, IO_KBD, KBCMDP, PCKBC_KBD_SLOT);
926 #endif	/* NPCKBC */
927 	}
928 #endif	/* NVGA */
929 #if (NCOM > 0)
930 	else if (strncmp(console, "com", 3) == 0) {
931 		if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
932 		    COM_FREQ, comcnmode))
933 			panic("can't init serial console @%x", CONCOMADDR);
934 	}
935 #endif
936 	/* Don't know what console was requested so use the fall back. */
937 	else
938 		fcomcnattach(DC21285_ARMCSR_VBASE, comcnspeed, comcnmode);
939 }
940 
941 /* End of ebsa285_machdep.c */
942