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