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