1/*	$NetBSD: hdlg_machdep.c,v 1.15 2011/06/30 20:09:24 wiz Exp $	*/
2
3/*
4 * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe and Steve C. Woodford 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 GigaLANDISK
72 * using RedBoot firmware.
73 */
74
75#include <sys/cdefs.h>
76__KERNEL_RCSID(0, "$NetBSD: hdlg_machdep.c,v 1.15 2011/06/30 20:09:24 wiz Exp $");
77
78#include "opt_ddb.h"
79#include "opt_kgdb.h"
80#include "opt_pmap_debug.h"
81
82#include <sys/param.h>
83#include <sys/device.h>
84#include <sys/systm.h>
85#include <sys/kernel.h>
86#include <sys/exec.h>
87#include <sys/proc.h>
88#include <sys/msgbuf.h>
89#include <sys/reboot.h>
90#include <sys/termios.h>
91#include <sys/ksyms.h>
92
93#include <uvm/uvm_extern.h>
94
95#include <dev/cons.h>
96
97#include <machine/db_machdep.h>
98#include <ddb/db_sym.h>
99#include <ddb/db_extern.h>
100
101#include <machine/bootconfig.h>
102#include <sys/bus.h>
103#include <machine/cpu.h>
104#include <machine/frame.h>
105#include <arm/undefined.h>
106
107#include <arm/arm32/machdep.h>
108
109#include <arm/xscale/i80321reg.h>
110#include <arm/xscale/i80321var.h>
111
112#include <dev/pci/ppbreg.h>
113
114#include <evbarm/hdl_g/hdlgreg.h>
115#include <evbarm/hdl_g/hdlgvar.h>
116#include <evbarm/hdl_g/obiovar.h>
117
118#include "ksyms.h"
119
120/* Kernel text starts 2MB in from the bottom of the kernel address space. */
121#define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
122#define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
123
124/*
125 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
126 * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
127 */
128#define KERNEL_VM_SIZE		0x0C000000
129
130/*
131 * Address to call from cpu_reset() to reset the machine.
132 * This is machine architecture dependent as it varies depending
133 * on where the ROM appears when you turn the MMU off.
134 *
135 * XXX Not actually used on hdlg -- clean up the generic
136 * ARM code.
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	4
181
182					/* L2 table for mapping i80321 */
183#define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
184
185					/* L2 tables for mapping kernel VM */
186#define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
187#define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
188#define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
189
190pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
191
192/* Prototypes */
193void consinit(void);
194
195/* Static device mappings. */
196static const struct pmap_devmap hdlg_devmap[] = {
197    /*
198     * Map the on-board devices VA == PA so that we can access them
199     * with the MMU on or off.
200     */
201    {
202	HDLG_OBIO_BASE,
203	HDLG_OBIO_BASE,
204	HDLG_OBIO_SIZE,
205	VM_PROT_READ|VM_PROT_WRITE,
206	PTE_NOCACHE,
207    },
208
209    {
210	HDLG_IOW_VBASE,
211	VERDE_OUT_XLATE_IO_WIN0_BASE,
212	VERDE_OUT_XLATE_IO_WIN_SIZE,
213	VM_PROT_READ|VM_PROT_WRITE,
214	PTE_NOCACHE,
215   },
216
217   {
218	HDLG_80321_VBASE,
219	VERDE_PMMR_BASE,
220	VERDE_PMMR_SIZE,
221	VM_PROT_READ|VM_PROT_WRITE,
222	PTE_NOCACHE,
223   },
224
225   {
226	0,
227	0,
228	0,
229	0,
230	0,
231    }
232};
233
234static void
235hardclock_hook(void)
236{
237
238	/* Nothing to do */
239}
240
241/*
242 * u_int initarm(...)
243 *
244 * Initial entry point on startup. This gets called before main() is
245 * entered.
246 * It should be responsible for setting up everything that must be
247 * in place when main is called.
248 * This includes
249 *   Taking a copy of the boot configuration structure.
250 *   Initialising the physical console so characters can be printed.
251 *   Setting up page tables for the kernel
252 *   Relocating the kernel to the bottom of physical memory
253 */
254u_int
255initarm(void *arg)
256{
257	extern vaddr_t xscale_cache_clean_addr;
258#ifdef DIAGNOSTIC
259	extern vsize_t xscale_minidata_clean_size;
260#endif
261	int loop;
262	int loop1;
263	u_int l1pagetable;
264	paddr_t memstart;
265	psize_t memsize;
266
267	/* Calibrate the delay loop. */
268	i80321_calibrate_delay();
269	i80321_hardclock_hook = hardclock_hook;
270
271	/*
272	 * Since we map the on-board devices VA==PA, and the kernel
273	 * is running VA==PA, it's possible for us to initialize
274	 * the console now.
275	 */
276	consinit();
277
278#ifdef VERBOSE_INIT_ARM
279	/* Talk to the user */
280	printf("\nNetBSD/evbarm (HDL-G) booting ...\n");
281#endif
282
283	/*
284	 * Heads up ... Setup the CPU / MMU / TLB functions
285	 */
286	if (set_cpufuncs())
287		panic("CPU not recognized!");
288
289	/*
290	 * We are currently running with the MMU enabled and the
291	 * entire address space mapped VA==PA, except for the
292	 * first 64M of RAM is also double-mapped at 0xc0000000.
293	 * There is an L1 page table at 0xa0004000.
294	 */
295
296	/*
297	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
298	 * registers.
299	 */
300	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
301	    &memstart, &memsize);
302
303#ifdef VERBOSE_INIT_ARM
304	printf("initarm: Configuring system ...\n");
305#endif
306
307	/* Fake bootconfig structure for the benefit of pmap.c */
308	/* XXX must make the memory description h/w independent */
309	bootconfig.dramblocks = 1;
310	bootconfig.dram[0].address = memstart;
311	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
312
313	/*
314	 * Set up the variables that define the availablilty of
315	 * physical memory.  For now, we're going to set
316	 * physical_freestart to 0xa0200000 (where the kernel
317	 * was loaded), and allocate the memory we need downwards.
318	 * If we get too close to the L1 table that we set up, we
319	 * will panic.  We will update physical_freestart and
320	 * physical_freeend later to reflect what pmap_bootstrap()
321	 * wants to see.
322	 *
323	 * XXX pmap_bootstrap() needs an enema.
324	 */
325	physical_start = bootconfig.dram[0].address;
326	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
327
328	physical_freestart = 0xa0009000UL;
329	physical_freeend = 0xa0200000UL;
330
331	physmem = (physical_end - physical_start) / PAGE_SIZE;
332
333#ifdef VERBOSE_INIT_ARM
334	/* Tell the user about the memory */
335	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
336	    physical_start, physical_end - 1);
337#endif
338
339	/*
340	 * Okay, the kernel starts 2MB in from the bottom of physical
341	 * memory.  We are going to allocate our bootstrap pages downwards
342	 * from there.
343	 *
344	 * We need to allocate some fixed page tables to get the kernel
345	 * going.  We allocate one page directory and a number of page
346	 * tables and store the physical addresses in the kernel_pt_table
347	 * array.
348	 *
349	 * The kernel page directory must be on a 16K boundary.  The page
350	 * tables must be on 4K boundaries.  What we do is allocate the
351	 * page directory on the first 16K boundary that we encounter, and
352	 * the page tables on 4K boundaries otherwise.  Since we allocate
353	 * at least 3 L2 page tables, we are guaranteed to encounter at
354	 * least one 16K aligned region.
355	 */
356
357#ifdef VERBOSE_INIT_ARM
358	printf("Allocating page tables\n");
359#endif
360
361	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
362
363#ifdef VERBOSE_INIT_ARM
364	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
365	       physical_freestart, free_pages, free_pages);
366#endif
367
368	/* Define a macro to simplify memory allocation */
369#define	valloc_pages(var, np)				\
370	alloc_pages((var).pv_pa, (np));			\
371	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
372
373#define alloc_pages(var, np)				\
374	physical_freeend -= ((np) * PAGE_SIZE);		\
375	if (physical_freeend < physical_freestart)	\
376		panic("initarm: out of memory");	\
377	(var) = physical_freeend;			\
378	free_pages -= (np);				\
379	memset((char *)(var), 0, ((np) * PAGE_SIZE));
380
381	loop1 = 0;
382	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
383		/* Are we 16KB aligned for an L1 ? */
384		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
385		    && kernel_l1pt.pv_pa == 0) {
386			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
387		} else {
388			valloc_pages(kernel_pt_table[loop1],
389			    L2_TABLE_SIZE / PAGE_SIZE);
390			++loop1;
391		}
392	}
393
394	/* This should never be able to happen but better confirm that. */
395	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
396		panic("initarm: Failed to align the kernel page directory");
397
398	/*
399	 * Allocate a page for the system page mapped to V0x00000000
400	 * This page will just contain the system vectors and can be
401	 * shared by all processes.
402	 */
403	alloc_pages(systempage.pv_pa, 1);
404
405	/* Allocate stacks for all modes */
406	valloc_pages(irqstack, IRQ_STACK_SIZE);
407	valloc_pages(abtstack, ABT_STACK_SIZE);
408	valloc_pages(undstack, UND_STACK_SIZE);
409	valloc_pages(kernelstack, UPAGES);
410
411	/* Allocate enough pages for cleaning the Mini-Data cache. */
412	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
413	valloc_pages(minidataclean, 1);
414
415#ifdef VERBOSE_INIT_ARM
416	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
417	    irqstack.pv_va);
418	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
419	    abtstack.pv_va);
420	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
421	    undstack.pv_va);
422	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
423	    kernelstack.pv_va);
424#endif
425
426	/*
427	 * XXX Defer this to later so that we can reclaim the memory
428	 * XXX used by the RedBoot page tables.
429	 */
430	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
431
432	/*
433	 * Ok we have allocated physical pages for the primary kernel
434	 * page tables
435	 */
436
437#ifdef VERBOSE_INIT_ARM
438	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
439#endif
440
441	/*
442	 * Now we start construction of the L1 page table
443	 * We start by mapping the L2 page tables into the L1.
444	 * This means that we can replace L1 mappings later on if necessary
445	 */
446	l1pagetable = kernel_l1pt.pv_pa;
447
448	/* Map the L2 pages tables in the L1 page table */
449	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
450	    &kernel_pt_table[KERNEL_PT_SYS]);
451	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
452		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
453		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
454	pmap_link_l2pt(l1pagetable, HDLG_IOPXS_VBASE,
455	    &kernel_pt_table[KERNEL_PT_IOPXS]);
456	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
457		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
458		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
459
460	/* update the top of the kernel VM */
461	pmap_curmaxkvaddr =
462	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
463
464#ifdef VERBOSE_INIT_ARM
465	printf("Mapping kernel\n");
466#endif
467
468	/* Now we fill in the L2 pagetable for the kernel static code/data */
469	{
470		extern char etext[], _end[];
471		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
472		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
473		u_int logical;
474
475		textsize = (textsize + PGOFSET) & ~PGOFSET;
476		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
477
478		logical = 0x00200000;	/* offset of kernel in RAM */
479
480		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
481		    physical_start + logical, textsize,
482		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
483		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
484		    physical_start + logical, totalsize - textsize,
485		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
486	}
487
488#ifdef VERBOSE_INIT_ARM
489	printf("Constructing L2 page tables\n");
490#endif
491
492	/* Map the stack pages */
493	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
494	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
495	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
496	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
497	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
498	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
499	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
500	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
501
502	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
503	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
504
505	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
506		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
507		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
508		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
509	}
510
511	/* Map the Mini-Data cache clean area. */
512	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
513	    minidataclean.pv_pa);
514
515	/* Map the vector page. */
516	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
517	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
518
519	/* Map the statically mapped devices. */
520	pmap_devmap_bootstrap(l1pagetable, hdlg_devmap);
521
522	/*
523	 * Give the XScale global cache clean code an appropriately
524	 * sized chunk of unmapped VA space starting at 0xff000000
525	 * (our device mappings end before this address).
526	 */
527	xscale_cache_clean_addr = 0xff000000U;
528
529	/*
530	 * Now we have the real page tables in place so we can switch to them.
531	 * Once this is done we will be running with the REAL kernel page
532	 * tables.
533	 */
534
535	/*
536	 * Update the physical_freestart/physical_freeend/free_pages
537	 * variables.
538	 */
539	{
540		extern char _end[];
541
542		physical_freestart = physical_start +
543		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
544		     KERNEL_BASE);
545		physical_freeend = physical_end;
546		free_pages =
547		    (physical_freeend - physical_freestart) / PAGE_SIZE;
548	}
549
550	/* Switch tables */
551#ifdef VERBOSE_INIT_ARM
552	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
553	       physical_freestart, free_pages, free_pages);
554	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
555#endif
556	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
557	cpu_setttb(kernel_l1pt.pv_pa);
558	cpu_tlb_flushID();
559	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
560
561	/*
562	 * Moved from cpu_startup() as data_abort_handler() references
563	 * this during uvm init
564	 */
565	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
566
567#ifdef VERBOSE_INIT_ARM
568	printf("done!\n");
569#endif
570
571#ifdef VERBOSE_INIT_ARM
572	printf("bootstrap done.\n");
573#endif
574
575	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
576
577	/*
578	 * Pages were allocated during the secondary bootstrap for the
579	 * stacks for different CPU modes.
580	 * We must now set the r13 registers in the different CPU modes to
581	 * point to these stacks.
582	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
583	 * of the stack memory.
584	 */
585#ifdef VERBOSE_INIT_ARM
586	printf("init subsystems: stacks ");
587#endif
588
589	set_stackptr(PSR_IRQ32_MODE,
590	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
591	set_stackptr(PSR_ABT32_MODE,
592	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
593	set_stackptr(PSR_UND32_MODE,
594	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
595
596	/*
597	 * Well we should set a data abort handler.
598	 * Once things get going this will change as we will need a proper
599	 * handler.
600	 * Until then we will use a handler that just panics but tells us
601	 * why.
602	 * Initialisation of the vectors will just panic on a data abort.
603	 * This just fills in a slightly better one.
604	 */
605#ifdef VERBOSE_INIT_ARM
606	printf("vectors ");
607#endif
608	data_abort_handler_address = (u_int)data_abort_handler;
609	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
610	undefined_handler_address = (u_int)undefinedinstruction_bounce;
611
612	/* Initialise the undefined instruction handlers */
613#ifdef VERBOSE_INIT_ARM
614	printf("undefined ");
615#endif
616	undefined_init();
617
618	/* Load memory into UVM. */
619#ifdef VERBOSE_INIT_ARM
620	printf("page ");
621#endif
622	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
623	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
624	    atop(physical_freestart), atop(physical_freeend),
625	    VM_FREELIST_DEFAULT);
626
627	/* Boot strap pmap telling it where the kernel page table is */
628#ifdef VERBOSE_INIT_ARM
629	printf("pmap ");
630#endif
631	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
632
633	/* Setup the IRQ system */
634#ifdef VERBOSE_INIT_ARM
635	printf("irq ");
636#endif
637	i80321_intr_init();
638
639#ifdef VERBOSE_INIT_ARM
640	printf("done.\n");
641#endif
642
643#ifdef BOOTHOWTO
644	boothowto = BOOTHOWTO;
645#endif
646
647#ifdef DDB
648	db_machine_init();
649	if (boothowto & RB_KDB)
650		Debugger();
651#endif
652
653	/* We return the new stack pointer address */
654	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
655}
656
657/*
658 * void cpu_reboot(int howto, char *bootstr)
659 *
660 * Reboots the system
661 *
662 * Deal with any syncing, unmounting, dumping and shutdown hooks,
663 * then reset the CPU.
664 */
665void
666cpu_reboot(int howto, char *bootstr)
667{
668
669	/*
670	 * If we are still cold then hit the air brakes
671	 * and crash to earth fast
672	 */
673	if (cold) {
674		*(volatile uint8_t *)HDLG_LEDCTRL |= LEDCTRL_STAT_RED;
675		howto |= RB_HALT;
676		goto haltsys;
677	}
678
679	/* Disable console buffering */
680
681	/*
682	 * If RB_NOSYNC was not specified sync the discs.
683	 * Note: Unless cold is set to 1 here, syslogd will die during the
684	 * unmount.  It looks like syslogd is getting woken up only to find
685	 * that it cannot page part of the binary in as the filesystem has
686	 * been unmounted.
687	 */
688	if ((howto & RB_NOSYNC) == 0) {
689		bootsync();
690		/*resettodr();*/
691	}
692
693	/* wait 1s */
694	delay(1 * 1000 * 1000);
695
696	/* Say NO to interrupts */
697	splhigh();
698
699	/* Do a dump if requested. */
700	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
701		dumpsys();
702	}
703
704haltsys:
705	/* Run any shutdown hooks */
706	doshutdownhooks();
707
708	pmf_system_shutdown(boothowto);
709
710	/* Make sure IRQ's are disabled */
711	IRQdisable;
712
713	if (howto & RB_HALT) {
714		*(volatile uint8_t *)HDLG_PWRMNG = PWRMNG_POWOFF;
715		delay(3 * 1000 * 1000);	/* wait 3s */
716
717		printf("SHUTDOWN FAILED!\n");
718		printf("The operating system has halted.\n");
719		printf("Please press any key to reboot.\n\n");
720		cngetc();
721	}
722
723	printf("rebooting...\n\r");
724
725	(void)disable_interrupts(I32_bit|F32_bit);
726	cpu_idcache_wbinv_all();
727	cpu_drain_writebuf();
728
729	*(volatile uint8_t *)HDLG_PWRMNG = PWRMNG_RESET;
730	delay(1 * 1000 * 1000);	/* wait 1s */
731
732	/* ...and if that didn't work, just croak. */
733	printf("RESET FAILED!\n");
734	for (;;) {
735		continue;
736	}
737}
738
739/*
740 * console
741 */
742#include "com.h"
743#if NCOM > 0
744#include <dev/ic/comreg.h>
745#include <dev/ic/comvar.h>
746#endif
747
748/*
749 * Define the default console speed for the board.  This is generally
750 * what the firmware provided with the board defaults to.
751 */
752#ifndef CONSPEED
753#define CONSPEED B115200
754#endif /* ! CONSPEED */
755
756#ifndef CONUNIT
757#define	CONUNIT	0
758#endif
759
760#ifndef CONMODE
761#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
762#endif
763
764int comcnspeed = CONSPEED;
765int comcnmode = CONMODE;
766int comcnunit = CONUNIT;
767
768#if KGDB
769#ifndef KGDB_DEVNAME
770#error Must define KGDB_DEVNAME
771#endif
772const char kgdb_devname[] = KGDB_DEVNAME;
773
774#ifndef KGDB_DEVADDR
775#error Must define KGDB_DEVADDR
776#endif
777unsigned long kgdb_devaddr = KGDB_DEVADDR;
778
779#ifndef KGDB_DEVRATE
780#define KGDB_DEVRATE	CONSPEED
781#endif
782int kgdb_devrate = KGDB_DEVRATE;
783
784#ifndef KGDB_DEVMODE
785#define KGDB_DEVMODE	CONMODE
786#endif
787int kgdb_devmode = KGDB_DEVMODE;
788#endif /* KGDB */
789
790void
791consinit(void)
792{
793	static const bus_addr_t comcnaddrs[] = {
794		HDLG_UART1,		/* com0 */
795	};
796	static int consinit_called;
797
798	if (consinit_called)
799		return;
800	consinit_called = 1;
801
802	/*
803	 * Console devices are mapped VA==PA.  Our devmap reflects
804	 * this, so register it now so drivers can map the console
805	 * device.
806	 */
807	pmap_devmap_register(hdlg_devmap);
808
809#if NCOM > 0
810	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
811	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
812		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
813#else
814	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
815#endif
816#if KGDB
817#if NCOM > 0
818	if (strcmp(kgdb_devname, "com") == 0) {
819		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
820				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
821	}
822#endif	/* NCOM > 0 */
823#endif	/* KGDB */
824}
825