xref: /netbsd-src/sys/arch/evbarm/integrator/integrator_machdep.c (revision 326b2259b73e878289ebd80cd9d20bc5aee35e99)
1 /*	$NetBSD: integrator_machdep.c,v 1.43 2003/07/15 00:25:00 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 2001,2002 ARM Ltd
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the company may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND
20  * 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 ARM LTD
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 /*
33  * Copyright (c) 1997,1998 Mark Brinicombe.
34  * Copyright (c) 1997,1998 Causality Limited.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by Mark Brinicombe
48  *	for the NetBSD Project.
49  * 4. The name of the company nor the name of the author may be used to
50  *    endorse or promote products derived from this software without specific
51  *    prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  * Machine dependant functions for kernel setup for integrator board
66  *
67  * Created      : 24/11/97
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: integrator_machdep.c,v 1.43 2003/07/15 00:25:00 lukem Exp $");
72 
73 #include "opt_ddb.h"
74 #include "opt_pmap_debug.h"
75 
76 #include <sys/param.h>
77 #include <sys/device.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/exec.h>
81 #include <sys/proc.h>
82 #include <sys/msgbuf.h>
83 #include <sys/reboot.h>
84 #include <sys/termios.h>
85 #include <sys/ksyms.h>
86 
87 #include <uvm/uvm_extern.h>
88 
89 #include <dev/cons.h>
90 
91 #include <machine/db_machdep.h>
92 #include <ddb/db_sym.h>
93 #include <ddb/db_extern.h>
94 
95 #include <machine/bootconfig.h>
96 #include <machine/bus.h>
97 #include <machine/cpu.h>
98 #include <machine/frame.h>
99 #include <machine/intr.h>
100 #include <evbarm/ifpga/irqhandler.h>	/* XXX XXX XXX */
101 #include <arm/undefined.h>
102 
103 #include <arm/arm32/machdep.h>
104 
105 #include <evbarm/integrator/integrator_boot.h>
106 
107 #include "opt_ipkdb.h"
108 #include "pci.h"
109 #include "ksyms.h"
110 
111 void ifpga_reset(void) __attribute__((noreturn));
112 
113 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
114 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
115 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
116 
117 /*
118  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
119  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
120  */
121 #define KERNEL_VM_SIZE		0x0C000000
122 
123 /*
124  * Address to call from cpu_reset() to reset the machine.
125  * This is machine architecture dependant as it varies depending
126  * on where the ROM appears when you turn the MMU off.
127  */
128 
129 u_int cpu_reset_address = (u_int) ifpga_reset;
130 
131 /* Define various stack sizes in pages */
132 #define IRQ_STACK_SIZE	1
133 #define ABT_STACK_SIZE	1
134 #ifdef IPKDB
135 #define UND_STACK_SIZE	2
136 #else
137 #define UND_STACK_SIZE	1
138 #endif
139 
140 BootConfig bootconfig;		/* Boot config storage */
141 char *boot_args = NULL;
142 char *boot_file = NULL;
143 
144 vm_offset_t physical_start;
145 vm_offset_t physical_freestart;
146 vm_offset_t physical_freeend;
147 vm_offset_t physical_end;
148 u_int free_pages;
149 vm_offset_t pagetables_start;
150 int physmem = 0;
151 
152 /*int debug_flags;*/
153 #ifndef PMAP_STATIC_L1S
154 int max_processes = 64;			/* Default number */
155 #endif	/* !PMAP_STATIC_L1S */
156 
157 /* Physical and virtual addresses for some global pages */
158 pv_addr_t systempage;
159 pv_addr_t irqstack;
160 pv_addr_t undstack;
161 pv_addr_t abtstack;
162 pv_addr_t kernelstack;
163 
164 vm_offset_t msgbufphys;
165 
166 extern u_int data_abort_handler_address;
167 extern u_int prefetch_abort_handler_address;
168 extern u_int undefined_handler_address;
169 
170 #ifdef PMAP_DEBUG
171 extern int pmap_debug_level;
172 #endif
173 
174 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
175 
176 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
177 #define	KERNEL_PT_KERNEL_NUM	2
178 					/* L2 tables for mapping kernel VM */
179 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
180 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
181 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
182 
183 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
184 
185 struct user *proc0paddr;
186 
187 /* Prototypes */
188 
189 static void	integrator_sdram_bounds	(paddr_t *, psize_t *);
190 
191 void	consinit(void);
192 
193 /* A load of console goo. */
194 #include "vga.h"
195 #if NVGA > 0
196 #include <dev/ic/mc6845reg.h>
197 #include <dev/ic/pcdisplayvar.h>
198 #include <dev/ic/vgareg.h>
199 #include <dev/ic/vgavar.h>
200 #endif
201 
202 #include "pckbc.h"
203 #if NPCKBC > 0
204 #include <dev/ic/i8042reg.h>
205 #include <dev/ic/pckbcvar.h>
206 #endif
207 
208 #include "com.h"
209 #if NCOM > 0
210 #include <dev/ic/comreg.h>
211 #include <dev/ic/comvar.h>
212 #ifndef CONCOMADDR
213 #define CONCOMADDR 0x3f8
214 #endif
215 #endif
216 
217 /*
218  * Define the default console speed for the board.  This is generally
219  * what the firmware provided with the board defaults to.
220  */
221 #ifndef CONSPEED
222 #define CONSPEED B115200
223 #endif
224 #ifndef CONMODE
225 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
226 #endif
227 
228 int comcnspeed = CONSPEED;
229 int comcnmode = CONMODE;
230 
231 #include "plcom.h"
232 #if (NPLCOM > 0)
233 #include <evbarm/dev/plcomreg.h>
234 #include <evbarm/dev/plcomvar.h>
235 
236 #include <evbarm/ifpga/ifpgamem.h>
237 #include <evbarm/ifpga/ifpgareg.h>
238 #include <evbarm/ifpga/ifpgavar.h>
239 #endif
240 
241 #ifndef CONSDEVNAME
242 #define CONSDEVNAME "plcom"
243 #endif
244 
245 #ifndef PLCONSPEED
246 #define PLCONSPEED B38400
247 #endif
248 #ifndef PLCONMODE
249 #define PLCONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
250 #endif
251 #ifndef PLCOMCNUNIT
252 #define PLCOMCNUNIT -1
253 #endif
254 
255 int plcomcnspeed = PLCONSPEED;
256 int plcomcnmode = PLCONMODE;
257 
258 #if 0
259 extern struct consdev kcomcons;
260 static void kcomcnputc(dev_t, int);
261 #endif
262 
263 /*
264  * void cpu_reboot(int howto, char *bootstr)
265  *
266  * Reboots the system
267  *
268  * Deal with any syncing, unmounting, dumping and shutdown hooks,
269  * then reset the CPU.
270  */
271 void
272 cpu_reboot(int howto, char *bootstr)
273 {
274 
275 	/*
276 	 * If we are still cold then hit the air brakes
277 	 * and crash to earth fast
278 	 */
279 	if (cold) {
280 		doshutdownhooks();
281 		printf("The operating system has halted.\n");
282 		printf("Please press any key to reboot.\n\n");
283 		cngetc();
284 		printf("rebooting...\n");
285 		ifpga_reset();
286 		/*NOTREACHED*/
287 	}
288 
289 	/* Disable console buffering */
290 
291 	/*
292 	 * If RB_NOSYNC was not specified sync the discs.
293 	 * Note: Unless cold is set to 1 here, syslogd will die during the
294 	 * unmount.  It looks like syslogd is getting woken up only to find
295 	 * that it cannot page part of the binary in as the filesystem has
296 	 * been unmounted.
297 	 */
298 	if (!(howto & RB_NOSYNC))
299 		bootsync();
300 
301 	/* Say NO to interrupts */
302 	splhigh();
303 
304 	/* Do a dump if requested. */
305 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
306 		dumpsys();
307 
308 	/* Run any shutdown hooks */
309 	doshutdownhooks();
310 
311 	/* Make sure IRQ's are disabled */
312 	IRQdisable;
313 
314 	if (howto & RB_HALT) {
315 		printf("The operating system has halted.\n");
316 		printf("Please press any key to reboot.\n\n");
317 		cngetc();
318 	}
319 
320 	printf("rebooting...\n");
321 	ifpga_reset();
322 	/*NOTREACHED*/
323 }
324 
325 /* Statically mapped devices. */
326 static const struct pmap_devmap integrator_devmap[] = {
327 #if NPLCOM > 0 && defined(PLCONSOLE)
328 	{
329 		UART0_BOOT_BASE,
330 		IFPGA_IO_BASE + IFPGA_UART0,
331 		1024 * 1024,
332 		VM_PROT_READ|VM_PROT_WRITE,
333 		PTE_NOCACHE
334 	},
335 
336 	{
337 		UART1_BOOT_BASE,
338 		IFPGA_IO_BASE + IFPGA_UART1,
339 		1024 * 1024,
340 		VM_PROT_READ|VM_PROT_WRITE,
341 		PTE_NOCACHE
342 	},
343 #endif
344 #if NPCI > 0
345 	{
346 		IFPGA_PCI_IO_VBASE,
347 		IFPGA_PCI_IO_BASE,
348 		IFPGA_PCI_IO_VSIZE,
349 		VM_PROT_READ|VM_PROT_WRITE,
350 		PTE_NOCACHE
351 	},
352 
353 	{
354 		IFPGA_PCI_CONF_VBASE,
355 		IFPGA_PCI_CONF_BASE,
356 		IFPGA_PCI_CONF_VSIZE,
357 		VM_PROT_READ|VM_PROT_WRITE,
358 		PTE_NOCACHE
359 	},
360 #endif
361 
362 	{
363 		0,
364 		0,
365 		0,
366 		0,
367 		0
368 	}
369 };
370 
371 /*
372  * u_int initarm(...)
373  *
374  * Initial entry point on startup. This gets called before main() is
375  * entered.
376  * It should be responsible for setting up everything that must be
377  * in place when main is called.
378  * This includes
379  *   Taking a copy of the boot configuration structure.
380  *   Initialising the physical console so characters can be printed.
381  *   Setting up page tables for the kernel
382  *   Relocating the kernel to the bottom of physical memory
383  */
384 
385 u_int
386 initarm(void *arg)
387 {
388 	int loop;
389 	int loop1;
390 	u_int l1pagetable;
391 	extern int etext asm ("_etext");
392 	extern int end asm ("_end");
393 	pv_addr_t kernel_l1pt;
394 	paddr_t memstart;
395 	psize_t memsize;
396 #if NPLCOM > 0 && defined(PLCONSOLE)
397 	static struct bus_space plcom_bus_space;
398 #endif
399 
400 	/*
401 	 * Heads up ... Setup the CPU / MMU / TLB functions
402 	 */
403 	if (set_cpufuncs())
404 		panic("cpu not recognized!");
405 
406 #if NPLCOM > 0 && defined(PLCONSOLE)
407 	/*
408 	 * Initialise the diagnostic serial console
409 	 * This allows a means of generating output during initarm().
410 	 * Once all the memory map changes are complete we can call consinit()
411 	 * and not have to worry about things moving.
412 	 */
413 
414 	if (PLCOMCNUNIT == 0) {
415 		ifpga_create_io_bs_tag(&plcom_bus_space, (void*)0xfd600000);
416 		plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
417 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT);
418 	} else if (PLCOMCNUNIT == 1) {
419 		ifpga_create_io_bs_tag(&plcom_bus_space, (void*)0xfd700000);
420 		plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
421 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT);
422 	}
423 #endif
424 
425 #ifdef VERBOSE_INIT_ARM
426 	/* Talk to the user */
427 	printf("\nNetBSD/evbarm (Integrator) booting ...\n");
428 #endif
429 
430 	/*
431 	 * Ok we have the following memory map
432 	 *
433 	 * XXX NO WE DON'T
434 	 *
435 	 * virtual address == physical address apart from the areas:
436 	 * 0x00000000 -> 0x000fffff which is mapped to
437 	 * top 1MB of physical memory
438 	 * 0x00100000 -> 0x0fffffff which is mapped to
439 	 * physical addresses 0x00100000 -> 0x0fffffff
440 	 * 0x10000000 -> 0x1fffffff which is mapped to
441 	 * physical addresses 0x00000000 -> 0x0fffffff
442 	 * 0x20000000 -> 0xefffffff which is mapped to
443 	 * physical addresses 0x20000000 -> 0xefffffff
444 	 * 0xf0000000 -> 0xf03fffff which is mapped to
445 	 * physical addresses 0x00000000 -> 0x003fffff
446 	 *
447 	 * This means that the kernel is mapped suitably for continuing
448 	 * execution, all I/O is mapped 1:1 virtual to physical and
449 	 * physical memory is accessible.
450 	 *
451 	 * The initarm() has the responsibility for creating the kernel
452 	 * page tables.
453 	 * It must also set up various memory pointers that are used
454 	 * by pmap etc.
455 	 */
456 
457 	/*
458 	 * Fetch the SDRAM start/size from the CM configuration registers.
459 	 */
460 	integrator_sdram_bounds(&memstart, &memsize);
461 
462 #ifdef VERBOSE_INIT_ARM
463 	printf("initarm: Configuring system ...\n");
464 #endif
465 
466 	/* Fake bootconfig structure for the benefit of pmap.c */
467 	/* XXX must make the memory description h/w independent */
468 	bootconfig.dramblocks = 1;
469 	bootconfig.dram[0].address = memstart;
470 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
471 
472 	/*
473 	 * Set up the variables that define the availablilty of
474 	 * physical memory.  For now, we're going to set
475 	 * physical_freestart to 0x00200000 (where the kernel
476 	 * was loaded), and allocate the memory we need downwards.
477 	 * If we get too close to the L1 table that we set up, we
478 	 * will panic.  We will update physical_freestart and
479 	 * physical_freeend later to reflect what pmap_bootstrap()
480 	 * wants to see.
481 	 *
482 	 * XXX pmap_bootstrap() needs an enema.
483 	 */
484 	physical_start = bootconfig.dram[0].address;
485 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
486 
487 	physical_freestart = 0x00009000UL;
488 	physical_freeend = 0x00200000UL;
489 
490 	physmem = (physical_end - physical_start) / PAGE_SIZE;
491 
492 #ifdef VERBOSE_INIT_ARM
493 	/* Tell the user about the memory */
494 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
495 	    physical_start, physical_end - 1);
496 #endif
497 
498 	/*
499 	 * Okay, the kernel starts 2MB in from the bottom of physical
500 	 * memory.  We are going to allocate our bootstrap pages downwards
501 	 * from there.
502 	 *
503 	 * We need to allocate some fixed page tables to get the kernel
504 	 * going.  We allocate one page directory and a number of page
505 	 * tables and store the physical addresses in the kernel_pt_table
506 	 * array.
507 	 *
508 	 * The kernel page directory must be on a 16K boundary.  The page
509 	 * tables must be on 4K bounaries.  What we do is allocate the
510 	 * page directory on the first 16K boundary that we encounter, and
511 	 * the page tables on 4K boundaries otherwise.  Since we allocate
512 	 * at least 3 L2 page tables, we are guaranteed to encounter at
513 	 * least one 16K aligned region.
514 	 */
515 
516 #ifdef VERBOSE_INIT_ARM
517 	printf("Allocating page tables\n");
518 #endif
519 
520 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
521 
522 #ifdef VERBOSE_INIT_ARM
523 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
524 	       physical_freestart, free_pages, free_pages);
525 #endif
526 
527 	/* Define a macro to simplify memory allocation */
528 #define	valloc_pages(var, np)				\
529 	alloc_pages((var).pv_pa, (np));			\
530 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
531 
532 #define alloc_pages(var, np)				\
533 	physical_freeend -= ((np) * PAGE_SIZE);		\
534 	if (physical_freeend < physical_freestart)	\
535 		panic("initarm: out of memory");	\
536 	(var) = physical_freeend;			\
537 	free_pages -= (np);				\
538 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
539 
540 	loop1 = 0;
541 	kernel_l1pt.pv_pa = 0;
542 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
543 		/* Are we 16KB aligned for an L1 ? */
544 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
545 		    && kernel_l1pt.pv_pa == 0) {
546 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
547 		} else {
548 			valloc_pages(kernel_pt_table[loop1],
549 			    L2_TABLE_SIZE / PAGE_SIZE);
550 			++loop1;
551 		}
552 	}
553 
554 	/* This should never be able to happen but better confirm that. */
555 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
556 		panic("initarm: Failed to align the kernel page directory");
557 
558 	/*
559 	 * Allocate a page for the system page mapped to V0x00000000
560 	 * This page will just contain the system vectors and can be
561 	 * shared by all processes.
562 	 */
563 	alloc_pages(systempage.pv_pa, 1);
564 
565 	/* Allocate stacks for all modes */
566 	valloc_pages(irqstack, IRQ_STACK_SIZE);
567 	valloc_pages(abtstack, ABT_STACK_SIZE);
568 	valloc_pages(undstack, UND_STACK_SIZE);
569 	valloc_pages(kernelstack, UPAGES);
570 
571 #ifdef VERBOSE_INIT_ARM
572 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
573 	    irqstack.pv_va);
574 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
575 	    abtstack.pv_va);
576 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
577 	    undstack.pv_va);
578 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
579 	    kernelstack.pv_va);
580 #endif
581 
582 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
583 
584 	/*
585 	 * Ok we have allocated physical pages for the primary kernel
586 	 * page tables
587 	 */
588 
589 #ifdef VERBOSE_INIT_ARM
590 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
591 #endif
592 
593 	/*
594 	 * Now we start construction of the L1 page table
595 	 * We start by mapping the L2 page tables into the L1.
596 	 * This means that we can replace L1 mappings later on if necessary
597 	 */
598 	l1pagetable = kernel_l1pt.pv_pa;
599 
600 	/* Map the L2 pages tables in the L1 page table */
601 	pmap_link_l2pt(l1pagetable, 0x00000000,
602 	    &kernel_pt_table[KERNEL_PT_SYS]);
603 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
604 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
605 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
606 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
607 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
608 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
609 
610 	/* update the top of the kernel VM */
611 	pmap_curmaxkvaddr =
612 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
613 
614 #ifdef VERBOSE_INIT_ARM
615 	printf("Mapping kernel\n");
616 #endif
617 
618 	/* Now we fill in the L2 pagetable for the kernel static code/data */
619 	{
620 		size_t textsize = (uintptr_t) &etext - KERNEL_TEXT_BASE;
621 		size_t totalsize = (uintptr_t) &end - KERNEL_TEXT_BASE;
622 		u_int logical;
623 
624 		textsize = (textsize + PGOFSET) & ~PGOFSET;
625 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
626 
627 		logical = 0x00200000;	/* offset of kernel in RAM */
628 
629 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
630 		    physical_start + logical, textsize,
631 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
632 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
633 		    physical_start + logical, totalsize - textsize,
634 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
635 	}
636 
637 #ifdef VERBOSE_INIT_ARM
638 	printf("Constructing L2 page tables\n");
639 #endif
640 
641 	/* Map the stack pages */
642 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
643 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
644 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
645 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
646 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
647 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
648 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
649 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
650 
651 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
652 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
653 
654 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
655 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
656 		    kernel_pt_table[loop].pv_va, L2_TABLE_SIZE,
657 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
658 	}
659 
660 	/* Map the vector page. */
661 #if 1
662 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download
663 	   the cache-clean code there.  */
664 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
665 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
666 #else
667 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
668 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
669 #endif
670 
671 	/* Map the statically mapped devices. */
672 	pmap_devmap_bootstrap(l1pagetable, integrator_devmap);
673 
674 	/*
675 	 * Now we have the real page tables in place so we can switch to them.
676 	 * Once this is done we will be running with the REAL kernel page
677 	 * tables.
678 	 */
679 
680 	/*
681 	 * Update the physical_freestart/physical_freeend/free_pages
682 	 * variables.
683 	 */
684 	{
685 		physical_freestart = physical_start +
686 		    (((((uintptr_t) &end) + PGOFSET) & ~PGOFSET) -
687 		     KERNEL_BASE);
688 		physical_freeend = physical_end;
689 		free_pages =
690 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
691 	}
692 
693 	/* Switch tables */
694 #ifdef VERBOSE_INIT_ARM
695 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
696 	       physical_freestart, free_pages, free_pages);
697 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
698 #endif
699 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
700 	setttb(kernel_l1pt.pv_pa);
701 	cpu_tlb_flushID();
702 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
703 
704 	/*
705 	 * Moved from cpu_startup() as data_abort_handler() references
706 	 * this during uvm init
707 	 */
708 	proc0paddr = (struct user *)kernelstack.pv_va;
709 	lwp0.l_addr = proc0paddr;
710 
711 #ifdef VERBOSE_INIT_ARM
712 	printf("done!\n");
713 #endif
714 
715 #ifdef PLCONSOLE
716 	/*
717 	 * The IFPGA registers have just moved.
718 	 * Detach the diagnostic serial port and reattach at the new address.
719 	 */
720 	plcomcndetach();
721 #endif
722 
723 	/*
724 	 * XXX this should only be done in main() but it useful to
725 	 * have output earlier ...
726 	 */
727 	consinit();
728 
729 #ifdef VERBOSE_INIT_ARM
730 	printf("bootstrap done.\n");
731 #endif
732 
733 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
734 
735 	/*
736 	 * Pages were allocated during the secondary bootstrap for the
737 	 * stacks for different CPU modes.
738 	 * We must now set the r13 registers in the different CPU modes to
739 	 * point to these stacks.
740 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
741 	 * of the stack memory.
742 	 */
743 #ifdef VERBOSE_INIT_ARM
744 	printf("init subsystems: stacks ");
745 #endif
746 
747 	set_stackptr(PSR_IRQ32_MODE,
748 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
749 	set_stackptr(PSR_ABT32_MODE,
750 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
751 	set_stackptr(PSR_UND32_MODE,
752 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
753 
754 	/*
755 	 * Well we should set a data abort handler.
756 	 * Once things get going this will change as we will need a proper
757 	 * handler.
758 	 * Until then we will use a handler that just panics but tells us
759 	 * why.
760 	 * Initialisation of the vectors will just panic on a data abort.
761 	 * This just fills in a slighly better one.
762 	 */
763 #ifdef VERBOSE_INIT_ARM
764 	printf("vectors ");
765 #endif
766 	data_abort_handler_address = (u_int)data_abort_handler;
767 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
768 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
769 
770 	/* Initialise the undefined instruction handlers */
771 #ifdef VERBOSE_INIT_ARM
772 	printf("undefined ");
773 #endif
774 	undefined_init();
775 
776 	/* Load memory into UVM. */
777 #ifdef VERBOSE_INIT_ARM
778 	printf("page ");
779 #endif
780 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
781 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
782 	    atop(physical_freestart), atop(physical_freeend),
783 	    VM_FREELIST_DEFAULT);
784 
785 	/* Boot strap pmap telling it where the kernel page table is */
786 #ifdef VERBOSE_INIT_ARM
787 	printf("pmap ");
788 #endif
789 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
790 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
791 
792 	/* Setup the IRQ system */
793 #ifdef VERBOSE_INIT_ARM
794 	printf("irq ");
795 #endif
796 	irq_init();
797 
798 #ifdef VERBOSE_INIT_ARM
799 	printf("done.\n");
800 #endif
801 
802 #ifdef IPKDB
803 	/* Initialise ipkdb */
804 	ipkdb_init();
805 	if (boothowto & RB_KDB)
806 		ipkdb_connect(0);
807 #endif
808 
809 #if NKSYMS || defined(DDB) || defined(LKM)
810 	/* Firmware doesn't load symbols. */
811 	ksyms_init(0, NULL, NULL);
812 #endif
813 
814 #ifdef DDB
815 	db_machine_init();
816 	if (boothowto & RB_KDB)
817 		Debugger();
818 #endif
819 
820 	/* We return the new stack pointer address */
821 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
822 }
823 
824 void
825 consinit(void)
826 {
827 	static int consinit_called = 0;
828 #if NPLCOM > 0 && defined(PLCONSOLE)
829 	static struct bus_space plcom_bus_space;
830 #endif
831 #if 0
832 	char *console = CONSDEVNAME;
833 #endif
834 
835 	if (consinit_called != 0)
836 		return;
837 
838 	consinit_called = 1;
839 
840 #if NPLCOM > 0 && defined(PLCONSOLE)
841 	if (PLCOMCNUNIT == 0) {
842 		ifpga_create_io_bs_tag(&plcom_bus_space,
843 		    (void*)UART0_BOOT_BASE);
844 		if (plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
845 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT))
846 			panic("can't init serial console");
847 		return;
848 	} else if (PLCOMCNUNIT == 1) {
849 		ifpga_create_io_bs_tag(&plcom_bus_space,
850 		    (void*)UART0_BOOT_BASE);
851 		if (plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
852 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT))
853 			panic("can't init serial console");
854 		return;
855 	}
856 #endif
857 #if (NCOM > 0)
858 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
859 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
860 		panic("can't init serial console @%x", CONCOMADDR);
861 	return;
862 #endif
863 	panic("No serial console configured");
864 }
865 
866 static void
867 integrator_sdram_bounds(paddr_t *memstart, psize_t *memsize)
868 {
869 	volatile unsigned long *cm_sdram
870 	    = (volatile unsigned long *)0x10000020;
871 
872 	*memstart = 0;
873 
874 	switch ((*cm_sdram >> 2) & 0x7)
875 	{
876 	case 0:
877 		*memsize = 16 * 1024 * 1024;
878 		break;
879 	case 1:
880 		*memsize = 32 * 1024 * 1024;
881 		break;
882 	case 2:
883 		*memsize = 64 * 1024 * 1024;
884 		break;
885 	case 3:
886 		*memsize = 128 * 1024 * 1024;
887 		break;
888 	case 4:
889 		*memsize = 256 * 1024 * 1024;
890 		break;
891 	default:
892 		printf("CM_SDRAM retuns unknown value, using 16M\n");
893 		*memsize = 16 * 1024 * 1024;
894 		break;
895 	}
896 }
897