xref: /netbsd/sys/arch/evbarm/iyonix/iyonix_machdep.c (revision c6e78f35)
1 /*	$NetBSD: iyonix_machdep.c,v 1.4 2020/04/18 11:00:40 skrll 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: iyonix_machdep.c,v 1.4 2020/04/18 11:00:40 skrll Exp $");
77 
78 #include "opt_ddb.h"
79 #include "opt_kgdb.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 #include <sys/bus.h>
92 #include <sys/cpu.h>
93 
94 #include <uvm/uvm_extern.h>
95 
96 #include <dev/cons.h>
97 
98 #include <dev/pci/ppbreg.h>
99 #include <dev/ic/i8259reg.h>
100 
101 #include <net/if.h>
102 #include <net/if_ether.h>
103 
104 #include <machine/db_machdep.h>
105 #include <ddb/db_sym.h>
106 #include <ddb/db_extern.h>
107 
108 #include <acorn32/include/bootconfig.h>
109 #include <arm/locore.h>
110 #include <arm/undefined.h>
111 
112 #include <arm/arm32/machdep.h>
113 
114 #include <arm/xscale/i80321reg.h>
115 #include <arm/xscale/i80321var.h>
116 
117 #include <evbarm/iyonix/iyonixreg.h>
118 #include <evbarm/iyonix/obiovar.h>
119 
120 #include <dev/wscons/wsconsio.h>
121 #include <dev/wscons/wsdisplayvar.h>
122 #include <dev/rasops/rasops.h>
123 #include <dev/wscons/wsdisplay_vconsvar.h>
124 #include <dev/wsfont/wsfont.h>
125 
126 #include "ksyms.h"
127 
128 #define	KERNEL_TEXT_BASE	KERNEL_BASE
129 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
130 
131 struct vcons_screen rascons_console_screen;
132 
133 struct wsscreen_descr rascons_stdscreen = {
134 	"std",
135 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
136 	0,
137 	0, 0,
138 	WSSCREEN_REVERSE
139 };
140 
141 /*
142  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
143  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
144  */
145 #define KERNEL_VM_SIZE		0x0C000000
146 
147 struct bootconfig bootconfig;		/* Boot config storage */
148 
149 char *boot_args;
150 
151 vaddr_t physical_start;
152 vaddr_t physical_freestart;
153 vaddr_t physical_freeend;
154 vaddr_t physical_end;
155 u_int free_pages;
156 vaddr_t pagetables_start;
157 
158 /*int debug_flags;*/
159 #ifndef PMAP_STATIC_L1S
160 int max_processes = 64;			/* Default number */
161 #endif	/* !PMAP_STATIC_L1S */
162 
163 /* Physical and virtual addresses for some global pages */
164 pv_addr_t minidataclean;
165 
166 paddr_t msgbufphys;
167 
168 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
169 
170 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
171 #define	KERNEL_PT_KERNEL_NUM	4
172 
173 					/* L2 table for mapping i80321 */
174 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
175 
176 					/* L2 tables for mapping kernel VM */
177 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
178 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
179 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
180 
181 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
182 
183 char iyonix_macaddr[ETHER_ADDR_LEN];
184 
185 char boot_consdev[16];
186 
187 /* Prototypes */
188 
189 void	iyonix_pic_init(void);
190 void	iyonix_read_machineid(void);
191 
192 void	consinit(void);
193 
194 static void consinit_com(const char *consdev);
195 static void consinit_genfb(const char *consdev);
196 static void process_kernel_args(void);
197 static void parse_iyonix_bootargs(char *args);
198 
199 #include "com.h"
200 #if NCOM > 0
201 #include <dev/ic/comreg.h>
202 #include <dev/ic/comvar.h>
203 #endif
204 
205 #include "genfb.h"
206 
207 #if (NGENFB == 0) && (NCOM == 0)
208 # error "No valid console device (com or genfb)"
209 #elif defined(COMCONSOLE) || (NGENFB == 0)
210 # define DEFAULT_CONSDEV "com"
211 #else
212 # define DEFAULT_CONSDEV "genfb"
213 #endif
214 
215 /*
216  * Define the default console speed for the machine.
217  */
218 #ifndef CONSPEED
219 #define CONSPEED B9600
220 #endif /* ! CONSPEED */
221 
222 #ifndef CONUNIT
223 #define	CONUNIT	0
224 #endif
225 
226 #ifndef CONMODE
227 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
228 #endif
229 
230 int comcnspeed = CONSPEED;
231 int comcnmode = CONMODE;
232 int comcnunit = CONUNIT;
233 
234 #if KGDB
235 #ifndef KGDB_DEVNAME
236 #error Must define KGDB_DEVNAME
237 #endif
238 const char kgdb_devname[] = KGDB_DEVNAME;
239 
240 #ifndef KGDB_DEVADDR
241 #error Must define KGDB_DEVADDR
242 #endif
243 unsigned long kgdb_devaddr = KGDB_DEVADDR;
244 
245 #ifndef KGDB_DEVRATE
246 #define KGDB_DEVRATE	CONSPEED
247 #endif
248 int kgdb_devrate = KGDB_DEVRATE;
249 
250 #ifndef KGDB_DEVMODE
251 #define KGDB_DEVMODE	CONMODE
252 #endif
253 int kgdb_devmode = KGDB_DEVMODE;
254 #endif /* KGDB */
255 
256 /*
257  * void cpu_reboot(int howto, char *bootstr)
258  *
259  * Reboots the system
260  *
261  * Deal with any syncing, unmounting, dumping and shutdown hooks,
262  * then reset the CPU.
263  */
264 void
cpu_reboot(int howto,char * bootstr)265 cpu_reboot(int howto, char *bootstr)
266 {
267 
268 	/*
269 	 * If we are still cold then hit the air brakes
270 	 * and crash to earth fast
271 	 */
272 	if (cold) {
273 		doshutdownhooks();
274 		pmf_system_shutdown(boothowto);
275 		printf("The operating system has halted.\n");
276 		printf("Please press any key to reboot.\n\n");
277 		cngetc();
278 		printf("rebooting...\n");
279 		goto reset;
280 	}
281 
282 	/* Disable console buffering */
283 
284 	/*
285 	 * If RB_NOSYNC was not specified sync the discs.
286 	 * Note: Unless cold is set to 1 here, syslogd will die during the
287 	 * unmount.  It looks like syslogd is getting woken up only to find
288 	 * that it cannot page part of the binary in as the filesystem has
289 	 * been unmounted.
290 	 */
291 	if (!(howto & RB_NOSYNC))
292 		bootsync();
293 
294 	/* Say NO to interrupts */
295 	splhigh();
296 
297 	/* Do a dump if requested. */
298 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
299 		dumpsys();
300 
301 	/* Run any shutdown hooks */
302 	doshutdownhooks();
303 
304 	pmf_system_shutdown(boothowto);
305 
306 	/* Make sure IRQ's are disabled */
307 	IRQdisable;
308 
309 	if (howto & RB_HALT) {
310 		printf("The operating system has halted.\n");
311 		printf("Please press any key to reboot.\n\n");
312 		cngetc();
313 	}
314 
315 	printf("rebooting...\n\r");
316  reset:
317 	/*
318 	 * Make really really sure that all interrupts are disabled,
319 	 * and poke the Internal Bus and Peripheral Bus reset lines.
320 	 */
321 	(void) disable_interrupts(I32_bit|F32_bit);
322 	*(volatile uint32_t *)(IYONIX_80321_VBASE + VERDE_ATU_BASE +
323 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
324 
325 	/* ...and if that didn't work, just croak. */
326 	printf("RESET FAILED!\n");
327 	for (;;);
328 }
329 
330 /* Static device mappings. */
331 static const struct pmap_devmap iyonix_devmap[] = {
332     /*
333      * Map the on-board devices VA == PA so that we can access them
334      * with the MMU on or off.
335      */
336     {
337 	IYONIX_OBIO_BASE,
338 	IYONIX_OBIO_BASE,
339 	IYONIX_OBIO_SIZE,
340 	VM_PROT_READ|VM_PROT_WRITE,
341 	PTE_NOCACHE,
342     },
343 
344     {
345 	IYONIX_IOW_VBASE,
346 	VERDE_OUT_XLATE_IO_WIN0_BASE,
347 	VERDE_OUT_XLATE_IO_WIN_SIZE,
348 	VM_PROT_READ|VM_PROT_WRITE,
349 	PTE_NOCACHE,
350    },
351 
352    {
353 	IYONIX_80321_VBASE,
354 	VERDE_PMMR_BASE,
355 	VERDE_PMMR_SIZE,
356 	VM_PROT_READ|VM_PROT_WRITE,
357 	PTE_NOCACHE,
358    },
359 
360    {
361 	IYONIX_FLASH_BASE,
362 	IYONIX_FLASH_BASE,
363 	IYONIX_FLASH_SIZE,
364 	VM_PROT_READ|VM_PROT_WRITE,
365 	PTE_NOCACHE,
366    },
367 
368    {
369 	0,
370 	0,
371 	0,
372 	0,
373 	0,
374     }
375 };
376 
377 /* Read out the Machine ID from the flash, and stash it away for later use. */
378 
379 void
iyonix_read_machineid(void)380 iyonix_read_machineid(void)
381 {
382 	volatile uint32_t *flashbase = (uint32_t *)IYONIX_FLASH_BASE;
383 	volatile uint16_t *flashword = (uint16_t *)IYONIX_FLASH_BASE;
384 	union {
385 		uint32_t w[2];
386 		uint8_t  b[8];
387 	} machid;
388 
389 	/* Enter SecSi Sector Region */
390 	flashword[0x555] = 0xAA;
391 	flashword[0x2AA] = 0x55;
392 	flashword[0x555] = 0x88;
393 
394 	machid.w[0] = flashbase[0];
395 	machid.w[1] = flashbase[1];
396 
397 	iyonix_macaddr[0] = machid.b[6];
398 	iyonix_macaddr[1] = machid.b[5];
399 	iyonix_macaddr[2] = machid.b[4];
400 	iyonix_macaddr[3] = machid.b[3];
401 	iyonix_macaddr[4] = machid.b[2];
402 	iyonix_macaddr[5] = machid.b[1];
403 
404 	/* Exit SecSi Sector Region */
405 	flashword[0x555] = 0xAA;
406 	flashword[0x2AA] = 0x55;
407 	flashword[0x555] = 0x90;
408 	flashword[0x555] = 0x00;
409 }
410 
411 #define IYONIX_PIC_WRITE(a,v) (*((char *)IYONIX_OBIO_BASE + (a)) = (v))
412 
413 void
iyonix_pic_init(void)414 iyonix_pic_init(void)
415 {
416 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT);
417 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW2, ICW2_IRL(0));
418 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW3, ICW3_CASCADE(2));
419 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW4, ICW4_8086);
420 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_OCW1, 0x0); /* Unmask */
421 
422 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT);
423 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW2, ICW2_IRL(0));
424 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW3, ICW3_CASCADE(1));
425 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW4, ICW4_8086);
426 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_OCW1, 0x0); /* Unmask */
427 
428 }
429 
430 /*
431  * vaddr_t initarm(...)
432  *
433  * Initial entry point on startup. This gets called before main() is
434  * entered.
435  * It should be responsible for setting up everything that must be
436  * in place when main is called.
437  * This includes
438  *   Taking a copy of the boot configuration structure.
439  *   Initialising the physical console so characters can be printed.
440  *   Setting up page tables for the kernel
441  *   Initialising interrupt controllers to a sane default state
442  */
443 vaddr_t
initarm(void * arg)444 initarm(void *arg)
445 {
446 	struct bootconfig *passed_bootconfig = arg;
447 	extern vaddr_t xscale_cache_clean_addr;
448 #ifdef DIAGNOSTIC
449 	extern vsize_t xscale_minidata_clean_size;
450 #endif
451 	extern char _end[];
452 	int loop;
453 	int loop1;
454 	u_int l1pagetable;
455 	paddr_t memstart = 0;
456 	psize_t memsize = 0;
457 
458 	/* Calibrate the delay loop. */
459 	i80321_calibrate_delay();
460 
461 	/* Ensure bootconfig has valid magic */
462 	if (passed_bootconfig->magic != BOOTCONFIG_MAGIC)
463 		printf("Bad bootconfig magic: %x\n", bootconfig.magic);
464 
465 	bootconfig = *passed_bootconfig;
466 
467 	/* Fake bootconfig structure for anything that still needs it */
468 	/* XXX must make the memory description h/w independent */
469 	bootconfig.dram[0].address = memstart;
470 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
471 	bootconfig.dramblocks = 1;
472 
473 	/* process arguments - can update boothowto */
474 	process_kernel_args();
475 
476 	/*
477 	 * Since we map the on-board devices VA==PA, and the kernel
478 	 * is running VA==PA, it's possible for us to initialize
479 	 * the console now.
480 	 */
481 	consinit();
482 
483 #ifdef VERBOSE_INIT_ARM
484 	/* Talk to the user */
485 	printf("\nNetBSD/iyonix booting ...\n");
486 #endif
487 
488 	/*
489 	 * Heads up ... Setup the CPU / MMU / TLB functions
490 	 */
491 	if (set_cpufuncs())
492 		panic("cpu not recognized!");
493 
494 	/*
495 	 * We are currently running with the MMU enabled and the
496 	 * entire address space mapped VA==PA.
497 	 */
498 
499 	/*
500 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
501 	 * registers.
502 	 */
503 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
504 	    &memstart, &memsize);
505 
506 #ifdef VERBOSE_INIT_ARM
507 	printf("initarm: Configuring system ...\n");
508 #endif
509 
510 	/*
511 	 * Set up the variables that define the availability of
512 	 * physical memory.
513 	 */
514 	physical_start = memstart;
515 	physical_end = physical_start + memsize;
516 
517 	physical_freestart = physical_start +
518 	    (((uintptr_t) _end - KERNEL_TEXT_BASE + PGOFSET) & ~PGOFSET);
519 	physical_freeend = physical_end;
520 
521 	physmem = (physical_end - physical_start) / PAGE_SIZE;
522 
523 #ifdef VERBOSE_INIT_ARM
524 	/* Tell the user about the memory */
525 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
526 	    physical_start, physical_end - 1);
527 #endif
528 
529 	/*
530 	 * The kernel is loaded at the base of physical memory. We allocate
531 	 * pages upwards from the top of the kernel.
532 	 *
533 	 * We need to allocate some fixed page tables to get the kernel
534 	 * going.  We allocate one page directory and a number of page
535 	 * tables and store the physical addresses in the kernel_pt_table
536 	 * array.
537 	 *
538 	 * The kernel page directory must be on a 16K boundary.  The page
539 	 * tables must be on 4K boundaries.  What we do is allocate the
540 	 * page directory on the first 16K boundary that we encounter, and
541 	 * the page tables on 4K boundaries otherwise.  Since we allocate
542 	 * at least 3 L2 page tables, we are guaranteed to encounter at
543 	 * least one 16K aligned region.
544 	 */
545 
546 #ifdef VERBOSE_INIT_ARM
547 	printf("Allocating page tables\n");
548 #endif
549 
550 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
551 
552 #ifdef VERBOSE_INIT_ARM
553 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
554 	       physical_freestart, free_pages, free_pages);
555 #endif
556 
557 	/* Define a macro to simplify memory allocation */
558 #define	valloc_pages(var, np)				\
559 	alloc_pages((var).pv_pa, (np));			\
560 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
561 
562 #define alloc_pages(var, np)				\
563 	(var) = physical_freestart;			\
564 	physical_freestart += ((np) * PAGE_SIZE);	\
565 	if (physical_freeend < physical_freestart)	\
566 		panic("initarm: out of memory");	\
567 	free_pages -= (np);				\
568 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
569 
570 	loop1 = 0;
571 	kernel_l1pt.pv_pa = kernel_l1pt.pv_va = 0;
572 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
573 		/* Are we 16KB aligned for an L1 ? */
574 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
575 		    && kernel_l1pt.pv_pa == 0) {
576 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
577 		} else {
578 			valloc_pages(kernel_pt_table[loop1],
579 			    L2_TABLE_SIZE / PAGE_SIZE);
580 			++loop1;
581 		}
582 	}
583 
584 	/* This should never be able to happen but better confirm that. */
585 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
586 		panic("initarm: Failed to align the kernel page directory");
587 
588 	/*
589 	 * Allocate a page for the system page mapped to V0x00000000
590 	 * This page will just contain the system vectors and can be
591 	 * shared by all processes.
592 	 */
593 	alloc_pages(systempage.pv_pa, 1);
594 
595 	/* Allocate stacks for all modes */
596 	valloc_pages(irqstack, IRQ_STACK_SIZE);
597 	valloc_pages(abtstack, ABT_STACK_SIZE);
598 	valloc_pages(undstack, UND_STACK_SIZE);
599 	valloc_pages(kernelstack, UPAGES);
600 
601 	/* Allocate enough pages for cleaning the Mini-Data cache. */
602 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
603 	valloc_pages(minidataclean, 1);
604 
605 #ifdef VERBOSE_INIT_ARM
606 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
607 	    irqstack.pv_va);
608 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
609 	    abtstack.pv_va);
610 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
611 	    undstack.pv_va);
612 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
613 	    kernelstack.pv_va);
614 #endif
615 
616 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
617 
618 	/*
619 	 * Ok we have allocated physical pages for the primary kernel
620 	 * page tables
621 	 */
622 
623 #ifdef VERBOSE_INIT_ARM
624 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
625 #endif
626 
627 	/*
628 	 * Now we start construction of the L1 page table
629 	 * We start by mapping the L2 page tables into the L1.
630 	 * This means that we can replace L1 mappings later on if necessary
631 	 */
632 	l1pagetable = kernel_l1pt.pv_pa;
633 
634 	/* Map the L2 pages tables in the L1 page table */
635 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
636 	    &kernel_pt_table[KERNEL_PT_SYS]);
637 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
638 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
639 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
640 	pmap_link_l2pt(l1pagetable, IYONIX_IOPXS_VBASE,
641 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
642 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
643 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
644 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
645 
646 	/* update the top of the kernel VM */
647 	pmap_curmaxkvaddr =
648 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
649 
650 #ifdef VERBOSE_INIT_ARM
651 	printf("Mapping kernel\n");
652 #endif
653 
654 	/* Now we fill in the L2 pagetable for the kernel static code/data */
655 	{
656 		extern char etext[], _end[];
657 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
658 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
659 		u_int logical;
660 
661 		textsize = (textsize + PGOFSET) & ~PGOFSET;
662 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
663 
664 		logical = 0;	/* offset of kernel in RAM */
665 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
666 		    physical_start + logical, textsize,
667 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
668 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
669 		    physical_start + logical, totalsize - textsize,
670 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
671 	}
672 
673 #ifdef VERBOSE_INIT_ARM
674 	printf("Constructing L2 page tables\n");
675 #endif
676 
677 	/* Map the stack pages */
678 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
679 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
680 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
681 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
682 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
683 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
684 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
685 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
686 
687 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
688 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
689 
690 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
691 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
692 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
693 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
694 	}
695 
696 	/* Map the Mini-Data cache clean area. */
697 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
698 	    minidataclean.pv_pa);
699 
700 	/* Map the vector page. */
701 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
702 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
703 
704 	/* Map the statically mapped devices. */
705 	pmap_devmap_bootstrap(l1pagetable, iyonix_devmap);
706 
707 	/*
708 	 * Give the XScale global cache clean code an appropriately
709 	 * sized chunk of unmapped VA space starting at 0xff000000
710 	 * (our device mappings end before this address).
711 	 */
712 	xscale_cache_clean_addr = 0xff000000U;
713 
714 	/*
715 	 * Now we have the real page tables in place so we can switch to them.
716 	 * Once this is done we will be running with the REAL kernel page
717 	 * tables.
718 	 */
719 
720 	/* Switch tables */
721 #ifdef VERBOSE_INIT_ARM
722 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
723 	       physical_freestart, free_pages, free_pages);
724 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
725 #endif
726 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
727 	cpu_setttb(kernel_l1pt.pv_pa, true);
728 	cpu_tlb_flushID();
729 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
730 
731 	iyonix_read_machineid();
732 
733 	/*
734 	 * Moved from cpu_startup() as data_abort_handler() references
735 	 * this during uvm init
736 	 */
737 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
738 
739 #ifdef VERBOSE_INIT_ARM
740 	printf("done!\n");
741 #endif
742 
743 #ifdef VERBOSE_INIT_ARM
744 	printf("bootstrap done.\n");
745 #endif
746 
747 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
748 
749 	/*
750 	 * Pages were allocated during the secondary bootstrap for the
751 	 * stacks for different CPU modes.
752 	 * We must now set the r13 registers in the different CPU modes to
753 	 * point to these stacks.
754 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
755 	 * of the stack memory.
756 	 */
757 #ifdef VERBOSE_INIT_ARM
758 	printf("init subsystems: stacks ");
759 #endif
760 
761 	set_stackptr(PSR_IRQ32_MODE,
762 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
763 	set_stackptr(PSR_ABT32_MODE,
764 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
765 	set_stackptr(PSR_UND32_MODE,
766 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
767 
768 	/*
769 	 * Well we should set a data abort handler.
770 	 * Once things get going this will change as we will need a proper
771 	 * handler.
772 	 * Until then we will use a handler that just panics but tells us
773 	 * why.
774 	 * Initialisation of the vectors will just panic on a data abort.
775 	 * This just fills in a slightly better one.
776 	 */
777 #ifdef VERBOSE_INIT_ARM
778 	printf("vectors ");
779 #endif
780 	data_abort_handler_address = (u_int)data_abort_handler;
781 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
782 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
783 
784 	/* Initialise the undefined instruction handlers */
785 #ifdef VERBOSE_INIT_ARM
786 	printf("undefined ");
787 #endif
788 	undefined_init();
789 
790 	/* Load memory into UVM. */
791 #ifdef VERBOSE_INIT_ARM
792 	printf("page ");
793 #endif
794 	uvm_md_init();
795 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
796 	    atop(physical_freestart), atop(physical_freeend),
797 	    VM_FREELIST_DEFAULT);
798 
799 	/* Boot strap pmap telling it where managed kernel virtual memory is */
800 #ifdef VERBOSE_INIT_ARM
801 	printf("pmap ");
802 #endif
803 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
804 
805 	/* Setup the IRQ system */
806 #ifdef VERBOSE_INIT_ARM
807 	printf("irq ");
808 #endif
809 	i80321_intr_init();
810 
811 #ifdef VERBOSE_INIT_ARM
812 	printf("done.\n");
813 #endif
814 
815 #ifdef DDB
816 	db_machine_init();
817 	if (boothowto & RB_KDB)
818 		Debugger();
819 #endif
820 
821 	iyonix_pic_init();
822 
823 	printf("args: %s\n", bootconfig.args);
824 	printf("howto: %x\n", boothowto);
825 
826 	/* We return the new stack pointer address */
827 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
828 }
829 
830 void
consinit(void)831 consinit(void)
832 {
833 	static int consinit_called;
834 
835 	if (consinit_called != 0)
836 		return;
837 
838 	consinit_called = 1;
839 
840 	/* We let consinit_<foo> worry about device numbers */
841 	if (strncmp(boot_consdev, "genfb", 5) &&
842 	    strncmp(boot_consdev, "com", 3))
843 	        strcpy(boot_consdev, DEFAULT_CONSDEV);
844 
845 	if (!strncmp(boot_consdev, "com", 3))
846 		consinit_com(boot_consdev);
847 	else
848 		consinit_genfb(boot_consdev);
849 }
850 
851 static void
consinit_com(const char * consdev)852 consinit_com(const char *consdev)
853 {
854 	static const bus_addr_t comcnaddrs[] = {
855 		IYONIX_UART1,		/* com0 */
856 	};
857 	/*
858 	 * Console devices are mapped VA==PA.  Our devmap reflects
859 	 * this, so register it now so drivers can map the console
860 	 * device.
861 	 */
862 	pmap_devmap_register(iyonix_devmap);
863 
864 	/* When we support more than the first serial port as console,
865 	 * we should check consdev for a number.
866 	 */
867 #if NCOM > 0
868 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
869 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
870 	{
871 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
872 	}
873 #else
874 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
875 #endif
876 
877 #if KGDB
878 #if NCOM > 0
879 	if (strcmp(kgdb_devname, "com") == 0) {
880 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
881 		    COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
882 	}
883 #endif	/* NCOM > 0 */
884 #endif	/* KGDB */
885 }
886 
887 static void
consinit_genfb(const char * consdev)888 consinit_genfb(const char *consdev)
889 {
890 	/* NOTYET */
891 }
892 
893 static void
process_kernel_args(void)894 process_kernel_args(void)
895 {
896 	char *args;
897 
898 	/* Ok now we will check the arguments for interesting parameters. */
899 	args = bootconfig.args;
900 
901 #ifdef BOOTHOWTO
902 	boothowto = BOOTHOWTO;
903 #else
904 	boothowto = 0;
905 #endif
906 
907 	/* Only arguments itself are passed from the bootloader */
908 	while (*args == ' ')
909 		++args;
910 
911 	boot_args = args;
912 	parse_mi_bootargs(boot_args);
913 	parse_iyonix_bootargs(boot_args);
914 }
915 
916 static void
parse_iyonix_bootargs(char * args)917 parse_iyonix_bootargs(char *args)
918 {
919 	char *ptr;
920 
921 	if (get_bootconf_option(args, "consdev", BOOTOPT_TYPE_STRING, &ptr))
922 	{
923 		/* ptr may have trailing clutter */
924 		strlcpy(boot_consdev, ptr, sizeof(boot_consdev));
925 		if ( (ptr = strchr(boot_consdev, ' ')) )
926 			*ptr = 0;
927 	}
928 }
929