1/*	$NetBSD: tsarm_machdep.c,v 1.15 2011/06/30 20:09:28 wiz Exp $ */
2
3/*
4 * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Based on code written by Jason R. Thorpe and Steve C. Woodford for
8 * Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed for the NetBSD Project by
21 *	Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 *    or promote products derived from this software without specific prior
24 *    written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * Copyright (c) 1997,1998 Mark Brinicombe.
41 * Copyright (c) 1997,1998 Causality Limited.
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *	This product includes software developed by Mark Brinicombe
55 *	for the NetBSD Project.
56 * 4. The name of the company nor the name of the author may be used to
57 *    endorse or promote products derived from this software without specific
58 *    prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * Machine dependent functions for kernel setup for Iyonix.
73 */
74
75#include <sys/cdefs.h>
76__KERNEL_RCSID(0, "$NetBSD: tsarm_machdep.c,v 1.15 2011/06/30 20:09:28 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 <acorn32/include/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/ep93xx/ep93xxreg.h>
110#include <arm/ep93xx/ep93xxvar.h>
111
112#include <dev/ic/comreg.h>
113#include <dev/ic/comvar.h>
114
115#include "epcom.h"
116#if NEPCOM > 0
117#include <arm/ep93xx/epcomvar.h>
118#endif
119
120#include "isa.h"
121#if NISA > 0
122#include <dev/isa/isareg.h>
123#include <dev/isa/isavar.h>
124#endif
125
126#include <machine/isa_machdep.h>
127
128#include <evbarm/tsarm/tsarmreg.h>
129
130#include "ksyms.h"
131
132/* Kernel text starts 2MB in from the bottom of the kernel address space. */
133#define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
134#define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
135
136/*
137 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
138 * Core-logic registers and I/O mappings occupy 0xf0000000 - 0xffffffff
139 */
140#define KERNEL_VM_SIZE		0x0C000000
141
142/*
143 * Address to call from cpu_reset() to reset the machine.
144 * This is machine architecture dependent as it varies depending
145 * on where the ROM appears when you turn the MMU off.
146 */
147
148u_int cpu_reset_address = 0x00000000;
149
150/* Define various stack sizes in pages */
151#define IRQ_STACK_SIZE	8
152#define ABT_STACK_SIZE	8
153#define UND_STACK_SIZE	8
154
155struct bootconfig bootconfig;		/* Boot config storage */
156char *boot_args = NULL;
157char *boot_file = NULL;
158
159vm_offset_t physical_start;
160vm_offset_t physical_freestart;
161vm_offset_t physical_freeend;
162vm_offset_t physical_freeend_low;
163vm_offset_t physical_end;
164u_int free_pages;
165
166/* Physical and virtual addresses for some global pages */
167pv_addr_t irqstack;
168pv_addr_t undstack;
169pv_addr_t abtstack;
170pv_addr_t kernelstack;
171
172vm_offset_t msgbufphys;
173
174static struct arm32_dma_range tsarm_dma_ranges[4];
175
176#if NISA > 0
177extern void isa_tsarm_init(u_int, u_int);
178#endif
179
180extern u_int data_abort_handler_address;
181extern u_int prefetch_abort_handler_address;
182extern u_int undefined_handler_address;
183
184#ifdef PMAP_DEBUG
185extern int pmap_debug_level;
186#endif
187
188#define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
189
190#define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
191#define	KERNEL_PT_KERNEL_NUM	4
192					/* L2 tables for mapping kernel VM */
193#define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
194
195#define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
196#define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
197
198pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
199
200/* Prototypes */
201
202void	consinit(void);
203/*
204 * Define the default console speed for the machine.
205 */
206#ifndef CONSPEED
207#define CONSPEED B115200
208#endif /* ! CONSPEED */
209
210#ifndef CONMODE
211#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
212#endif
213
214int comcnspeed = CONSPEED;
215int comcnmode = CONMODE;
216
217#if KGDB
218#ifndef KGDB_DEVNAME
219#error Must define KGDB_DEVNAME
220#endif
221const char kgdb_devname[] = KGDB_DEVNAME;
222
223#ifndef KGDB_DEVADDR
224#error Must define KGDB_DEVADDR
225#endif
226unsigned long kgdb_devaddr = KGDB_DEVADDR;
227
228#ifndef KGDB_DEVRATE
229#define KGDB_DEVRATE	CONSPEED
230#endif
231int kgdb_devrate = KGDB_DEVRATE;
232
233#ifndef KGDB_DEVMODE
234#define KGDB_DEVMODE	CONMODE
235#endif
236int kgdb_devmode = KGDB_DEVMODE;
237#endif /* KGDB */
238
239/*
240 * void cpu_reboot(int howto, char *bootstr)
241 *
242 * Reboots the system
243 *
244 * Deal with any syncing, unmounting, dumping and shutdown hooks,
245 * then reset the CPU.
246 */
247void
248cpu_reboot(int howto, char *bootstr)
249{
250
251	/*
252	 * If we are still cold then hit the air brakes
253	 * and crash to earth fast
254	 */
255	if (cold) {
256		doshutdownhooks();
257		pmf_system_shutdown(boothowto);
258		printf("\r\n");
259		printf("The operating system has halted.\r\n");
260		printf("Please press any key to reboot.\r\n");
261		cngetc();
262		printf("\r\nrebooting...\r\n");
263		goto reset;
264	}
265
266	/* Disable console buffering */
267
268	/*
269	 * If RB_NOSYNC was not specified sync the discs.
270	 * Note: Unless cold is set to 1 here, syslogd will die during the
271	 * unmount.  It looks like syslogd is getting woken up only to find
272	 * that it cannot page part of the binary in as the filesystem has
273	 * been unmounted.
274	 */
275	if (!(howto & RB_NOSYNC))
276		bootsync();
277
278	/* Say NO to interrupts */
279	splhigh();
280
281	/* Do a dump if requested. */
282	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
283		dumpsys();
284
285	/* Run any shutdown hooks */
286	doshutdownhooks();
287
288	pmf_system_shutdown(boothowto);
289
290	/* Make sure IRQ's are disabled */
291	IRQdisable;
292
293	if (howto & RB_HALT) {
294		printf("\r\n");
295		printf("The operating system has halted.\r\n");
296		printf("Please press any key to reboot.\r\n");
297		cngetc();
298	}
299
300	printf("\r\nrebooting...\r\n");
301 reset:
302	/*
303	 * Make really really sure that all interrupts are disabled,
304	 * and poke the Internal Bus and Peripheral Bus reset lines.
305	 */
306	(void) disable_interrupts(I32_bit|F32_bit);
307
308	{
309		u_int32_t feed, ctrl;
310
311		feed = TS7XXX_IO16_VBASE + TS7XXX_WDOGFEED;
312		ctrl = TS7XXX_IO16_VBASE + TS7XXX_WDOGCTRL;
313
314		__asm volatile (
315			"mov r0, #0x5\n"
316			"mov r1, #0x1\n"
317			"strh r0, [%0]\n"
318			"strh r1, [%1]\n"
319			:
320			: "r" (feed), "r" (ctrl)
321			: "r0", "r1"
322		);
323	}
324
325	for (;;);
326}
327
328/* Static device mappings. */
329static const struct pmap_devmap tsarm_devmap[] = {
330    {
331	EP93XX_AHB_VBASE,
332	EP93XX_AHB_HWBASE,
333	EP93XX_AHB_SIZE,
334	VM_PROT_READ|VM_PROT_WRITE,
335	PTE_NOCACHE,
336    },
337
338    {
339	EP93XX_APB_VBASE,
340	EP93XX_APB_HWBASE,
341	EP93XX_APB_SIZE,
342	VM_PROT_READ|VM_PROT_WRITE,
343	PTE_NOCACHE,
344    },
345
346	/*
347	 * IO8 and IO16 space *must* be mapped contiguously with
348	 * IO8_VA == IO16_VA - 64 Mbytes.  ISA busmap driver depends
349	 * on that!
350	 */
351    {
352	TS7XXX_IO8_VBASE,
353	TS7XXX_IO8_HWBASE,
354	TS7XXX_IO8_SIZE,
355	VM_PROT_READ|VM_PROT_WRITE,
356	PTE_NOCACHE,
357    },
358
359    {
360	TS7XXX_IO16_VBASE,
361	TS7XXX_IO16_HWBASE,
362	TS7XXX_IO16_SIZE,
363	VM_PROT_READ|VM_PROT_WRITE,
364	PTE_NOCACHE,
365    },
366
367   {
368	0,
369	0,
370	0,
371	0,
372	0,
373    }
374};
375
376/*
377 * u_int initarm(...)
378 *
379 * Initial entry point on startup. This gets called before main() is
380 * entered.
381 * It should be responsible for setting up everything that must be
382 * in place when main is called.
383 * This includes
384 *   Taking a copy of the boot configuration structure.
385 *   Initialising the physical console so characters can be printed.
386 *   Setting up page tables for the kernel
387 *   Initialising interrupt controllers to a sane default state
388 */
389u_int
390initarm(void *arg)
391{
392#ifdef FIXME
393	struct bootconfig *passed_bootconfig = arg;
394	extern char _end[];
395#endif
396	int loop;
397	int loop1;
398	u_int l1pagetable;
399	paddr_t memstart;
400	psize_t memsize;
401
402#ifdef FIXME
403	/* Calibrate the delay loop. */
404	i80321_calibrate_delay();
405#endif
406
407	/*
408	 * Since we map the on-board devices VA==PA, and the kernel
409	 * is running VA==PA, it's possible for us to initialize
410	 * the console now.
411	 */
412	consinit();
413
414#ifdef VERBOSE_INIT_ARM
415	/* Talk to the user */
416	printf("\nNetBSD/tsarm booting ...\n");
417#endif
418
419	/*
420	 * Heads up ... Setup the CPU / MMU / TLB functions
421	 */
422	if (set_cpufuncs())
423		panic("cpu not recognized!");
424
425	/*
426	 * We are currently running with the MMU enabled
427	 */
428
429#ifdef FIXME
430	/*
431	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
432	 * registers.
433	 */
434	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
435	    &memstart, &memsize);
436#else
437	memstart = 0x0;
438	memsize = 0x2000000;
439#endif
440
441#ifdef VERBOSE_INIT_ARM
442	printf("initarm: Configuring system ...\n");
443#endif
444
445	/* Fake bootconfig structure for the benefit of pmap.c */
446	/* XXX must make the memory description h/w independent */
447	bootconfig.dramblocks = 4;
448	bootconfig.dram[0].address = 0x0UL;
449	bootconfig.dram[0].pages = 0x800000UL / PAGE_SIZE;
450	bootconfig.dram[1].address = 0x1000000UL;
451	bootconfig.dram[1].pages = 0x800000UL / PAGE_SIZE;
452	bootconfig.dram[2].address = 0x4000000UL;
453	bootconfig.dram[2].pages = 0x800000UL / PAGE_SIZE;
454	bootconfig.dram[3].address = 0x5000000UL;
455	bootconfig.dram[3].pages = 0x800000UL / PAGE_SIZE;
456
457	/*
458	 * Set up the variables that define the availablilty of
459	 * physical memory.  For now, we're going to set
460	 * physical_freestart to 0x00200000 (where the kernel
461	 * was loaded), and allocate the memory we need downwards.
462	 * If we get too close to the L1 table that we set up, we
463	 * will panic.  We will update physical_freestart and
464	 * physical_freeend later to reflect what pmap_bootstrap()
465	 * wants to see.
466	 *
467	 * XXX pmap_bootstrap() needs an enema.
468	 */
469	physical_start = bootconfig.dram[0].address;
470	physical_end = bootconfig.dram[0].address +
471		(bootconfig.dram[0].pages * PAGE_SIZE);
472
473	physical_freestart = 0x00009000UL;
474	physical_freeend = 0x00200000UL;
475
476	physmem = (physical_end - physical_start) / PAGE_SIZE;
477
478#ifdef VERBOSE_INIT_ARM
479	/* Tell the user about the memory */
480	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
481	    physical_start, physical_end - 1);
482#endif
483
484	/*
485	 * Okay, the kernel starts 2MB in from the bottom of physical
486	 * memory.  We are going to allocate our bootstrap pages downwards
487	 * from there.
488	 *
489	 * We need to allocate some fixed page tables to get the kernel
490	 * going.  We allocate one page directory and a number of page
491	 * tables and store the physical addresses in the kernel_pt_table
492	 * array.
493	 *
494	 * The kernel page directory must be on a 16K boundary.  The page
495	 * tables must be on 4K bounaries.  What we do is allocate the
496	 * page directory on the first 16K boundary that we encounter, and
497	 * the page tables on 4K boundaries otherwise.  Since we allocate
498	 * at least 3 L2 page tables, we are guaranteed to encounter at
499	 * least one 16K aligned region.
500	 */
501
502#ifdef VERBOSE_INIT_ARM
503	printf("Allocating page tables\n");
504#endif
505
506	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
507
508#ifdef VERBOSE_INIT_ARM
509	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
510	       physical_freestart, free_pages, free_pages);
511#endif
512
513	/* Define a macro to simplify memory allocation */
514#define	valloc_pages(var, np)				\
515	alloc_pages((var).pv_pa, (np));			\
516	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
517
518#define alloc_pages(var, np)				\
519	physical_freeend -= ((np) * PAGE_SIZE);		\
520	if (physical_freeend < physical_freestart)	\
521		panic("initarm: out of memory");	\
522	(var) = physical_freeend;			\
523	free_pages -= (np);				\
524	memset((char *)(var), 0, ((np) * PAGE_SIZE));
525
526	loop1 = 0;
527	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
528		/* Are we 16KB aligned for an L1 ? */
529		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
530		    && kernel_l1pt.pv_pa == 0) {
531			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
532		} else {
533			valloc_pages(kernel_pt_table[loop1],
534			    L2_TABLE_SIZE / PAGE_SIZE);
535			++loop1;
536		}
537	}
538
539	/* This should never be able to happen but better confirm that. */
540	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
541		panic("initarm: Failed to align the kernel page directory");
542
543	/*
544	 * Allocate a page for the system vectors page
545	 */
546	alloc_pages(systempage.pv_pa, 1);
547
548	/* Allocate stacks for all modes */
549	valloc_pages(irqstack, IRQ_STACK_SIZE);
550	valloc_pages(abtstack, ABT_STACK_SIZE);
551	valloc_pages(undstack, UND_STACK_SIZE);
552	valloc_pages(kernelstack, UPAGES);
553
554#ifdef VERBOSE_INIT_ARM
555	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
556	    irqstack.pv_va);
557	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
558	    abtstack.pv_va);
559	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
560	    undstack.pv_va);
561	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
562	    kernelstack.pv_va);
563#endif
564
565	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
566
567	/*
568	 * Ok we have allocated physical pages for the primary kernel
569	 * page tables.  Save physical_freeend for when we give whats left
570	 * of memory below 2Mbyte to UVM.
571	 */
572
573	physical_freeend_low = physical_freeend;
574
575#ifdef VERBOSE_INIT_ARM
576	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
577#endif
578
579	/*
580	 * Now we start construction of the L1 page table
581	 * We start by mapping the L2 page tables into the L1.
582	 * This means that we can replace L1 mappings later on if necessary
583	 */
584	l1pagetable = kernel_l1pt.pv_pa;
585
586	/* Map the L2 pages tables in the L1 page table */
587	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
588	    &kernel_pt_table[KERNEL_PT_SYS]);
589	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
590		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
591		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
592	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
593		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
594		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
595
596	/* update the top of the kernel VM */
597	pmap_curmaxkvaddr =
598	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
599
600#ifdef VERBOSE_INIT_ARM
601	printf("Mapping kernel\n");
602#endif
603
604	/* Now we fill in the L2 pagetable for the kernel static code/data */
605	{
606		extern char etext[], _end[];
607		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
608		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
609		u_int logical;
610
611		textsize = (textsize + PGOFSET) & ~PGOFSET;
612		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
613
614		logical = 0x00200000;	/* offset of kernel in RAM */
615		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
616		    physical_start + logical, textsize,
617		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
618		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
619		    physical_start + logical, totalsize - textsize,
620		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
621	}
622
623#ifdef VERBOSE_INIT_ARM
624	printf("Constructing L2 page tables\n");
625#endif
626
627	/* Map the stack pages */
628	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
629	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
630	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
631	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
632	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
633	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
634	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
635	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
636
637	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
638	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
639
640	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
641		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
642		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
643		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
644	}
645
646	/* Map the vector page. */
647	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
648	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
649
650	/* Map the statically mapped devices. */
651	pmap_devmap_bootstrap(l1pagetable, tsarm_devmap);
652
653	/*
654	 * Update the physical_freestart/physical_freeend/free_pages
655	 * variables.
656	 */
657	{
658		extern char _end[];
659
660		physical_freestart = physical_start +
661		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
662		     KERNEL_BASE);
663		physical_freeend = physical_end;
664		free_pages =
665		    (physical_freeend - physical_freestart) / PAGE_SIZE;
666	}
667
668	/*
669	 * Now we have the real page tables in place so we can switch to them.
670	 * Once this is done we will be running with the REAL kernel page
671	 * tables.
672	 */
673
674	/* Switch tables */
675#ifdef VERBOSE_INIT_ARM
676	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
677	       physical_freestart, free_pages, free_pages);
678	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
679#endif
680	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
681	cpu_setttb(kernel_l1pt.pv_pa);
682	cpu_tlb_flushID();
683	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
684
685	/*
686	 * Moved from cpu_startup() as data_abort_handler() references
687	 * this during uvm init
688	 */
689	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
690
691#ifdef VERBOSE_INIT_ARM
692	printf("done!\n");
693#endif
694
695#ifdef VERBOSE_INIT_ARM
696	printf("bootstrap done.\n");
697#endif
698
699	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
700
701	/*
702	 * Pages were allocated during the secondary bootstrap for the
703	 * stacks for different CPU modes.
704	 * We must now set the r13 registers in the different CPU modes to
705	 * point to these stacks.
706	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
707	 * of the stack memory.
708	 */
709#ifdef VERBOSE_INIT_ARM
710	printf("init subsystems: stacks ");
711#endif
712
713	set_stackptr(PSR_IRQ32_MODE,
714	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
715	set_stackptr(PSR_ABT32_MODE,
716	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
717	set_stackptr(PSR_UND32_MODE,
718	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
719
720	/*
721	 * Well we should set a data abort handler.
722	 * Once things get going this will change as we will need a proper
723	 * handler.
724	 * Until then we will use a handler that just panics but tells us
725	 * why.
726	 * Initialisation of the vectors will just panic on a data abort.
727	 * This just fills in a slightly better one.
728	 */
729#ifdef VERBOSE_INIT_ARM
730	printf("vectors ");
731#endif
732	data_abort_handler_address = (u_int)data_abort_handler;
733	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
734	undefined_handler_address = (u_int)undefinedinstruction_bounce;
735
736	/* Initialise the undefined instruction handlers */
737#ifdef VERBOSE_INIT_ARM
738	printf("undefined ");
739#endif
740	undefined_init();
741
742	/* Load memory into UVM. */
743#ifdef VERBOSE_INIT_ARM
744	printf("page ");
745#endif
746	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
747	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
748	    atop(physical_freestart), atop(physical_freeend),
749	    VM_FREELIST_DEFAULT);
750	uvm_page_physload(0, atop(physical_freeend_low),
751	    0, atop(physical_freeend_low),
752	    VM_FREELIST_DEFAULT);
753	/*
754	 * There is 32 Mb of memory on the TS-7200 in 4 8Mb chunks, so far
755	 * we've only been working with the first one mapped at 0x0.  Tell
756	 * UVM about the others.
757	 */
758	uvm_page_physload(atop(0x1000000), atop(0x1800000),
759	    atop(0x1000000), atop(0x1800000),
760	    VM_FREELIST_DEFAULT);
761	uvm_page_physload(atop(0x4000000), atop(0x4800000),
762	    atop(0x4000000), atop(0x4800000),
763	    VM_FREELIST_DEFAULT);
764	uvm_page_physload(atop(0x5000000), atop(0x5800000),
765	    atop(0x5000000), atop(0x5800000),
766	    VM_FREELIST_DEFAULT);
767
768	physmem = 0x2000000 / PAGE_SIZE;
769
770
771	/* Boot strap pmap telling it where the kernel page table is */
772#ifdef VERBOSE_INIT_ARM
773	printf("pmap ");
774#endif
775	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
776
777	/* Setup the IRQ system */
778#ifdef VERBOSE_INIT_ARM
779	printf("irq ");
780#endif
781	ep93xx_intr_init();
782#if NISA > 0
783	isa_intr_init();
784
785#ifdef VERBOSE_INIT_ARM
786	printf("isa ");
787#endif
788	isa_tsarm_init(TS7XXX_IO16_VBASE + TS7XXX_ISAIO,
789		TS7XXX_IO16_VBASE + TS7XXX_ISAMEM);
790#endif
791
792#ifdef VERBOSE_INIT_ARM
793	printf("done.\n");
794#endif
795
796#ifdef BOOTHOWTO
797	boothowto = BOOTHOWTO;
798#endif
799
800#ifdef DDB
801	db_machine_init();
802	if (boothowto & RB_KDB)
803		Debugger();
804#endif
805
806	/* We return the new stack pointer address */
807	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
808}
809
810void
811consinit(void)
812{
813	static int consinit_called;
814	bus_space_handle_t ioh;
815
816	if (consinit_called != 0)
817		return;
818
819	consinit_called = 1;
820
821	/*
822	 * Console devices are already mapped in VA.  Our devmap reflects
823	 * this, so register it now so drivers can map the console
824	 * device.
825	 */
826	pmap_devmap_register(tsarm_devmap);
827#if 0
828	isa_tsarm_init(TS7XXX_IO16_VBASE + TS7XXX_ISAIO,
829		TS7XXX_IO16_VBASE + TS7XXX_ISAMEM);
830
831        if (comcnattach(&isa_io_bs_tag, 0x3e8, comcnspeed,
832            COM_FREQ, COM_TYPE_NORMAL, comcnmode))
833        {
834                panic("can't init serial console");
835        }
836#endif
837
838#if NEPCOM > 0
839	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_UART1,
840		EP93XX_APB_UART_SIZE, 0, &ioh);
841        if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_UART1,
842		ioh, comcnspeed, comcnmode))
843	{
844		panic("can't init serial console");
845	}
846#else
847	panic("serial console not configured");
848#endif
849#if KGDB
850#if NEPCOM > 0
851	if (strcmp(kgdb_devname, "epcom") == 0) {
852		com_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
853			kgdb_devmode);
854	}
855#endif	/* NEPCOM > 0 */
856#endif	/* KGDB */
857}
858
859
860bus_dma_tag_t
861ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
862{
863	int i;
864	struct arm32_bus_dma_tag *dmat;
865
866	for (i = 0; i < bootconfig.dramblocks; i++) {
867		tsarm_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
868		tsarm_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
869		tsarm_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
870			PAGE_SIZE;
871	}
872
873	dmat = dma_tag_template;
874
875	dmat->_ranges = tsarm_dma_ranges;
876	dmat->_nranges = bootconfig.dramblocks;
877
878	return dmat;
879}
880