1/*	$NetBSD: iyonix_machdep.c,v 1.6 2024/02/20 23:36:02 andvar 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.6 2024/02/20 23:36:02 andvar 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
131struct vcons_screen rascons_console_screen;
132
133struct 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
147struct bootconfig bootconfig;		/* Boot config storage */
148
149char *boot_args;
150
151vaddr_t physical_start;
152vaddr_t physical_freestart;
153vaddr_t physical_freeend;
154vaddr_t physical_end;
155u_int free_pages;
156vaddr_t pagetables_start;
157
158/*int debug_flags;*/
159#ifndef PMAP_STATIC_L1S
160int max_processes = 64;			/* Default number */
161#endif	/* !PMAP_STATIC_L1S */
162
163/* Physical and virtual addresses for some global pages */
164pv_addr_t minidataclean;
165
166paddr_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
181pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
182
183char iyonix_macaddr[ETHER_ADDR_LEN];
184
185char boot_consdev[16];
186
187/* Prototypes */
188
189void	iyonix_pic_init(void);
190void	iyonix_read_machineid(void);
191
192void	consinit(void);
193
194static void consinit_com(const char *consdev);
195static void consinit_genfb(const char *consdev);
196static void process_kernel_args(void);
197static 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
230int comcnspeed = CONSPEED;
231int comcnmode = CONMODE;
232int comcnunit = CONUNIT;
233
234#if KGDB
235#ifndef KGDB_DEVNAME
236#error Must define KGDB_DEVNAME
237#endif
238const char kgdb_devname[] = KGDB_DEVNAME;
239
240#ifndef KGDB_DEVADDR
241#error Must define KGDB_DEVADDR
242#endif
243unsigned long kgdb_devaddr = KGDB_DEVADDR;
244
245#ifndef KGDB_DEVRATE
246#define KGDB_DEVRATE	CONSPEED
247#endif
248int kgdb_devrate = KGDB_DEVRATE;
249
250#ifndef KGDB_DEVMODE
251#define KGDB_DEVMODE	CONMODE
252#endif
253int 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 */
264void
265cpu_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. */
331static 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
379void
380iyonix_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
413void
414iyonix_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 */
443vaddr_t
444initarm(void *arg)
445{
446	struct bootconfig *passed_bootconfig = arg;
447	extern char _end[];
448	int loop;
449	int loop1;
450	u_int l1pagetable;
451	paddr_t memstart = 0;
452	psize_t memsize = 0;
453
454	/* Calibrate the delay loop. */
455	i80321_calibrate_delay();
456
457	/* Ensure bootconfig has valid magic */
458	if (passed_bootconfig->magic != BOOTCONFIG_MAGIC)
459		printf("Bad bootconfig magic: %x\n", bootconfig.magic);
460
461	bootconfig = *passed_bootconfig;
462
463	/* Fake bootconfig structure for anything that still needs it */
464	/* XXX must make the memory description h/w independent */
465	bootconfig.dram[0].address = memstart;
466	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
467	bootconfig.dramblocks = 1;
468
469	/* process arguments - can update boothowto */
470	process_kernel_args();
471
472	/*
473	 * Since we map the on-board devices VA==PA, and the kernel
474	 * is running VA==PA, it's possible for us to initialize
475	 * the console now.
476	 */
477	consinit();
478
479#ifdef VERBOSE_INIT_ARM
480	/* Talk to the user */
481	printf("\nNetBSD/iyonix booting ...\n");
482#endif
483
484	/*
485	 * Heads up ... Setup the CPU / MMU / TLB functions
486	 */
487	if (set_cpufuncs())
488		panic("cpu not recognized!");
489
490	/*
491	 * We are currently running with the MMU enabled and the
492	 * entire address space mapped VA==PA.
493	 */
494
495	/*
496	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
497	 * registers.
498	 */
499	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
500	    &memstart, &memsize);
501
502#ifdef VERBOSE_INIT_ARM
503	printf("initarm: Configuring system ...\n");
504#endif
505
506	/*
507	 * Set up the variables that define the availability of
508	 * physical memory.
509	 */
510	physical_start = memstart;
511	physical_end = physical_start + memsize;
512
513	physical_freestart = physical_start +
514	    (((uintptr_t) _end - KERNEL_TEXT_BASE + PGOFSET) & ~PGOFSET);
515	physical_freeend = physical_end;
516
517	physmem = (physical_end - physical_start) / PAGE_SIZE;
518
519#ifdef VERBOSE_INIT_ARM
520	/* Tell the user about the memory */
521	printf("physmemory: 0x%"PRIxPSIZE" pages at 0x%08lx -> 0x%08lx\n", physmem,
522	    physical_start, physical_end - 1);
523#endif
524
525	/*
526	 * The kernel is loaded at the base of physical memory. We allocate
527	 * pages upwards from the top of the kernel.
528	 *
529	 * We need to allocate some fixed page tables to get the kernel
530	 * going.  We allocate one page directory and a number of page
531	 * tables and store the physical addresses in the kernel_pt_table
532	 * array.
533	 *
534	 * The kernel page directory must be on a 16K boundary.  The page
535	 * tables must be on 4K boundaries.  What we do is allocate the
536	 * page directory on the first 16K boundary that we encounter, and
537	 * the page tables on 4K boundaries otherwise.  Since we allocate
538	 * at least 3 L2 page tables, we are guaranteed to encounter at
539	 * least one 16K aligned region.
540	 */
541
542#ifdef VERBOSE_INIT_ARM
543	printf("Allocating page tables\n");
544#endif
545
546	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
547
548#ifdef VERBOSE_INIT_ARM
549	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
550	       physical_freestart, free_pages, free_pages);
551#endif
552
553	/* Define a macro to simplify memory allocation */
554#define	valloc_pages(var, np)				\
555	alloc_pages((var).pv_pa, (np));			\
556	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
557
558#define alloc_pages(var, np)				\
559	(var) = physical_freestart;			\
560	physical_freestart += ((np) * PAGE_SIZE);	\
561	if (physical_freeend < physical_freestart)	\
562		panic("initarm: out of memory");	\
563	free_pages -= (np);				\
564	memset((char *)(var), 0, ((np) * PAGE_SIZE));
565
566	loop1 = 0;
567	kernel_l1pt.pv_pa = kernel_l1pt.pv_va = 0;
568	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
569		/* Are we 16KB aligned for an L1 ? */
570		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
571		    && kernel_l1pt.pv_pa == 0) {
572			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
573		} else {
574			valloc_pages(kernel_pt_table[loop1],
575			    L2_TABLE_SIZE / PAGE_SIZE);
576			++loop1;
577		}
578	}
579
580	/* This should never be able to happen but better confirm that. */
581	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
582		panic("initarm: Failed to align the kernel page directory");
583
584	/*
585	 * Allocate a page for the system page mapped to V0x00000000
586	 * This page will just contain the system vectors and can be
587	 * shared by all processes.
588	 */
589	alloc_pages(systempage.pv_pa, 1);
590
591	/* Allocate stacks for all modes */
592	valloc_pages(irqstack, IRQ_STACK_SIZE);
593	valloc_pages(abtstack, ABT_STACK_SIZE);
594	valloc_pages(undstack, UND_STACK_SIZE);
595	valloc_pages(kernelstack, UPAGES);
596
597	/* Allocate enough pages for cleaning the Mini-Data cache. */
598	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
599	valloc_pages(minidataclean, 1);
600
601#ifdef VERBOSE_INIT_ARM
602	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
603	    irqstack.pv_va);
604	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
605	    abtstack.pv_va);
606	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
607	    undstack.pv_va);
608	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
609	    kernelstack.pv_va);
610#endif
611
612	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
613
614	/*
615	 * Ok we have allocated physical pages for the primary kernel
616	 * page tables
617	 */
618
619#ifdef VERBOSE_INIT_ARM
620	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
621#endif
622
623	/*
624	 * Now we start construction of the L1 page table
625	 * We start by mapping the L2 page tables into the L1.
626	 * This means that we can replace L1 mappings later on if necessary
627	 */
628	l1pagetable = kernel_l1pt.pv_pa;
629
630	/* Map the L2 pages tables in the L1 page table */
631	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
632	    &kernel_pt_table[KERNEL_PT_SYS]);
633	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
634		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
635		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
636	pmap_link_l2pt(l1pagetable, IYONIX_IOPXS_VBASE,
637	    &kernel_pt_table[KERNEL_PT_IOPXS]);
638	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
639		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
640		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
641
642	/* update the top of the kernel VM */
643	pmap_curmaxkvaddr =
644	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
645
646#ifdef VERBOSE_INIT_ARM
647	printf("Mapping kernel\n");
648#endif
649
650	/* Now we fill in the L2 pagetable for the kernel static code/data */
651	{
652		extern char etext[], _end[];
653		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
654		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
655		u_int logical;
656
657		textsize = (textsize + PGOFSET) & ~PGOFSET;
658		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
659
660		logical = 0;	/* offset of kernel in RAM */
661		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
662		    physical_start + logical, textsize,
663		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
664		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
665		    physical_start + logical, totalsize - textsize,
666		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
667	}
668
669#ifdef VERBOSE_INIT_ARM
670	printf("Constructing L2 page tables\n");
671#endif
672
673	/* Map the stack pages */
674	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
675	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
676	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
677	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
678	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
679	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
680	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
681	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
682
683	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
684	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
685
686	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
687		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
688		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
689		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
690	}
691
692	/* Map the Mini-Data cache clean area. */
693	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
694	    minidataclean.pv_pa);
695
696	/* Map the vector page. */
697	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
698	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
699
700	/* Map the statically mapped devices. */
701	pmap_devmap_bootstrap(l1pagetable, iyonix_devmap);
702
703	/*
704	 * Give the XScale global cache clean code an appropriately
705	 * sized chunk of unmapped VA space starting at 0xff000000
706	 * (our device mappings end before this address).
707	 */
708	xscale_cache_clean_addr = 0xff000000U;
709
710	/*
711	 * Now we have the real page tables in place so we can switch to them.
712	 * Once this is done we will be running with the REAL kernel page
713	 * tables.
714	 */
715
716	/* Switch tables */
717#ifdef VERBOSE_INIT_ARM
718	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
719	       physical_freestart, free_pages, free_pages);
720	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
721#endif
722	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
723	cpu_setttb(kernel_l1pt.pv_pa, true);
724	cpu_tlb_flushID();
725	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
726
727	iyonix_read_machineid();
728
729	/*
730	 * Moved from cpu_startup() as data_abort_handler() references
731	 * this during uvm init
732	 */
733	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
734
735#ifdef VERBOSE_INIT_ARM
736	printf("done!\n");
737#endif
738
739#ifdef VERBOSE_INIT_ARM
740	printf("bootstrap done.\n");
741#endif
742
743	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
744
745	/*
746	 * Pages were allocated during the secondary bootstrap for the
747	 * stacks for different CPU modes.
748	 * We must now set the r13 registers in the different CPU modes to
749	 * point to these stacks.
750	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
751	 * of the stack memory.
752	 */
753#ifdef VERBOSE_INIT_ARM
754	printf("init subsystems: stacks ");
755#endif
756
757	set_stackptr(PSR_IRQ32_MODE,
758	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
759	set_stackptr(PSR_ABT32_MODE,
760	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
761	set_stackptr(PSR_UND32_MODE,
762	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
763
764	/*
765	 * Well we should set a data abort handler.
766	 * Once things get going this will change as we will need a proper
767	 * handler.
768	 * Until then we will use a handler that just panics but tells us
769	 * why.
770	 * Initialisation of the vectors will just panic on a data abort.
771	 * This just fills in a slightly better one.
772	 */
773#ifdef VERBOSE_INIT_ARM
774	printf("vectors ");
775#endif
776	data_abort_handler_address = (u_int)data_abort_handler;
777	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
778	undefined_handler_address = (u_int)undefinedinstruction_bounce;
779
780	/* Initialise the undefined instruction handlers */
781#ifdef VERBOSE_INIT_ARM
782	printf("undefined ");
783#endif
784	undefined_init();
785
786	/* Load memory into UVM. */
787#ifdef VERBOSE_INIT_ARM
788	printf("page ");
789#endif
790	uvm_md_init();
791	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
792	    atop(physical_freestart), atop(physical_freeend),
793	    VM_FREELIST_DEFAULT);
794
795	/* Boot strap pmap telling it where managed kernel virtual memory is */
796#ifdef VERBOSE_INIT_ARM
797	printf("pmap ");
798#endif
799	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
800
801	/* Setup the IRQ system */
802#ifdef VERBOSE_INIT_ARM
803	printf("irq ");
804#endif
805	i80321_intr_init();
806
807#ifdef VERBOSE_INIT_ARM
808	printf("done.\n");
809#endif
810
811#ifdef DDB
812	db_machine_init();
813	if (boothowto & RB_KDB)
814		Debugger();
815#endif
816
817	iyonix_pic_init();
818
819	printf("args: %s\n", bootconfig.args);
820	printf("howto: %x\n", boothowto);
821
822	/* We return the new stack pointer address */
823	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
824}
825
826void
827consinit(void)
828{
829	static int consinit_called;
830
831	if (consinit_called != 0)
832		return;
833
834	consinit_called = 1;
835
836	/* We let consinit_<foo> worry about device numbers */
837	if (strncmp(boot_consdev, "genfb", 5) &&
838	    strncmp(boot_consdev, "com", 3))
839	        strcpy(boot_consdev, DEFAULT_CONSDEV);
840
841	if (!strncmp(boot_consdev, "com", 3))
842		consinit_com(boot_consdev);
843	else
844		consinit_genfb(boot_consdev);
845}
846
847static void
848consinit_com(const char *consdev)
849{
850	static const bus_addr_t comcnaddrs[] = {
851		IYONIX_UART1,		/* com0 */
852	};
853	/*
854	 * Console devices are mapped VA==PA.  Our devmap reflects
855	 * this, so register it now so drivers can map the console
856	 * device.
857	 */
858	pmap_devmap_register(iyonix_devmap);
859
860	/* When we support more than the first serial port as console,
861	 * we should check consdev for a number.
862	 */
863#if NCOM > 0
864	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
865	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
866	{
867		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
868	}
869#else
870	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
871#endif
872
873#if KGDB
874#if NCOM > 0
875	if (strcmp(kgdb_devname, "com") == 0) {
876		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
877		    COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
878	}
879#endif	/* NCOM > 0 */
880#endif	/* KGDB */
881}
882
883static void
884consinit_genfb(const char *consdev)
885{
886	/* NOTYET */
887}
888
889static void
890process_kernel_args(void)
891{
892	char *args;
893
894	/* Ok now we will check the arguments for interesting parameters. */
895	args = bootconfig.args;
896
897#ifdef BOOTHOWTO
898	boothowto = BOOTHOWTO;
899#else
900	boothowto = 0;
901#endif
902
903	/* Only arguments itself are passed from the bootloader */
904	while (*args == ' ')
905		++args;
906
907	boot_args = args;
908	parse_mi_bootargs(boot_args);
909	parse_iyonix_bootargs(boot_args);
910}
911
912static void
913parse_iyonix_bootargs(char *args)
914{
915	char *ptr;
916
917	if (get_bootconf_option(args, "consdev", BOOTOPT_TYPE_STRING, &ptr))
918	{
919		/* ptr may have trailing clutter */
920		strlcpy(boot_consdev, ptr, sizeof(boot_consdev));
921		if ( (ptr = strchr(boot_consdev, ' ')) )
922			*ptr = 0;
923	}
924}
925