1/*	$NetBSD: netwinder_machdep.c,v 1.75 2011/06/30 20:09:34 wiz Exp $	*/
2
3/*
4 * Copyright (c) 1997,1998 Mark Brinicombe.
5 * Copyright (c) 1997,1998 Causality Limited.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Mark Brinicombe
19 *	for the NetBSD Project.
20 * 4. The name of the company nor the name of the author may be used to
21 *    endorse or promote products derived from this software without specific
22 *    prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * Machine dependent functions for kernel setup for EBSA285 core architecture
37 * using Netwinder firmware
38 *
39 * Created      : 24/11/97
40 */
41
42#include <sys/cdefs.h>
43__KERNEL_RCSID(0, "$NetBSD: netwinder_machdep.c,v 1.75 2011/06/30 20:09:34 wiz Exp $");
44
45#include "opt_ddb.h"
46#include "opt_pmap_debug.h"
47
48#include <sys/param.h>
49#include <sys/device.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/exec.h>
53#include <sys/proc.h>
54#include <sys/msgbuf.h>
55#include <sys/reboot.h>
56#include <sys/termios.h>
57#include <sys/ksyms.h>
58
59#include <uvm/uvm_extern.h>
60
61#include <dev/cons.h>
62
63#include <machine/db_machdep.h>
64#include <ddb/db_sym.h>
65#include <ddb/db_extern.h>
66
67#include <arm/arm32/machdep.h>
68
69#include <machine/bootconfig.h>
70#define	_ARM32_BUS_DMA_PRIVATE
71#include <sys/bus.h>
72#include <machine/cpu.h>
73#include <machine/frame.h>
74#include <machine/intr.h>
75#include <arm/undefined.h>
76
77#include <machine/netwinder_boot.h>
78#include <arm/footbridge/dc21285mem.h>
79#include <arm/footbridge/dc21285reg.h>
80
81#include "isa.h"
82#include "isadma.h"
83#if NISA > 0
84#include <dev/isa/isareg.h>
85#include <dev/isa/isavar.h>
86#endif
87
88#include "igsfb.h"
89#if NIGSFB > 0
90#include <dev/pci/pcivar.h>
91#include <dev/pci/igsfb_pcivar.h>
92#endif
93
94#include "pckbc.h"
95#if NPCKBC > 0
96#include <dev/ic/i8042reg.h>
97#include <dev/ic/pckbcvar.h>
98#endif
99
100#include "com.h"
101#include <dev/ic/comreg.h>
102#include <dev/ic/comvar.h>
103
104#include "ksyms.h"
105
106static bus_space_handle_t isa_base = (bus_space_handle_t) DC21285_PCI_IO_VBASE;
107
108bs_protos(generic);
109
110#define	ISA_GETBYTE(r)		generic_bs_r_1(0, isa_base, (r))
111#define	ISA_PUTBYTE(r,v)	generic_bs_w_1(0, isa_base, (r), (v))
112
113/*
114 * Address to call from cpu_reset() to reset the machine.
115 * This is machine architecture dependent as it varies depending
116 * on where the ROM appears when you turn the MMU off.
117 */
118static void netwinder_reset(void);
119u_int cpu_reset_address;
120
121u_int dc21285_fclk = 63750000;
122
123/* Define various stack sizes in pages */
124#define IRQ_STACK_SIZE	1
125#define ABT_STACK_SIZE	1
126#define UND_STACK_SIZE	1
127
128struct nwbootinfo nwbootinfo;
129BootConfig bootconfig;		/* Boot config storage */
130static char bootargs[MAX_BOOT_STRING + 1];
131char *boot_args = NULL;
132char *boot_file = NULL;
133
134vm_offset_t physical_start;
135vm_offset_t physical_freestart;
136vm_offset_t physical_freeend;
137vm_offset_t physical_end;
138u_int free_pages;
139vm_offset_t pagetables_start;
140
141/*int debug_flags;*/
142#ifndef PMAP_STATIC_L1S
143int max_processes = 64;			/* Default number */
144#endif	/* !PMAP_STATIC_L1S */
145
146/* Physical and virtual addresses for some global pages */
147pv_addr_t irqstack;
148pv_addr_t undstack;
149pv_addr_t abtstack;
150extern pv_addr_t kernelstack;	/* in arm32_machdep.c */
151
152vm_offset_t msgbufphys;
153
154extern u_int data_abort_handler_address;
155extern u_int prefetch_abort_handler_address;
156extern u_int undefined_handler_address;
157
158#ifdef PMAP_DEBUG
159extern int pmap_debug_level;
160#endif
161
162#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
163#define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
164#define KERNEL_PT_VMDATA	2	/* Page tables for mapping kernel VM */
165#define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
166#define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
167
168pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
169
170#define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
171/*
172 * The range 0xf1000000 - 0xfcffffff is available for kernel VM space
173 * Footbridge registers and I/O mappings occupy 0xfd000000 - 0xffffffff
174 */
175#if NIGSFB > 0
176/* XXX: uwe: map 16 megs at 0xfc000000 for igsfb(4) */
177#define KERNEL_VM_SIZE		0x0B000000
178#else
179#define KERNEL_VM_SIZE		0x0C000000
180#endif
181
182/* Prototypes */
183
184void consinit(void);
185void process_kernel_args(char *);
186void data_abort_handler(trapframe_t *);
187void prefetch_abort_handler(trapframe_t *);
188void undefinedinstruction_bounce(trapframe_t *);
189
190
191/* A load of console goo. */
192#ifndef CONSDEVNAME
193#  if (NIGSFB > 0) && (NPCKBC > 0)
194#    define CONSDEVNAME "igsfb"
195#  elif NCOM > 0
196#    define CONSDEVNAME "com"
197#  else
198#    error CONSDEVNAME not defined and no known console device configured
199#  endif
200#endif /* !CONSDEVNAME */
201
202#ifndef CONCOMADDR
203#define CONCOMADDR 0x3f8
204#endif
205
206#ifndef CONSPEED
207#define CONSPEED B115200	/* match NeTTrom */
208#endif
209
210#ifndef CONMODE
211#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
212#endif
213
214int comcnspeed = CONSPEED;
215int comcnmode = CONMODE;
216
217extern struct consdev kcomcons;
218static void kcomcnputc(dev_t, int);
219
220#if NIGSFB > 0
221/* XXX: uwe */
222#define IGS_PCI_MEM_VBASE		0xfc000000
223#define IGS_PCI_MEM_VSIZE		0x01000000
224#define IGS_PCI_MEM_BASE		0x08000000
225
226extern struct arm32_pci_chipset footbridge_pci_chipset;
227extern struct bus_space footbridge_pci_io_bs_tag;
228extern struct bus_space footbridge_pci_mem_bs_tag;
229extern void footbridge_pci_bs_tag_init(void);
230
231/* standard methods */
232extern bs_map_proto(footbridge_mem);
233extern bs_unmap_proto(footbridge_mem);
234
235/* our hooks */
236static bs_map_proto(nw_footbridge_mem);
237static bs_unmap_proto(nw_footbridge_mem);
238#endif
239
240
241/*
242 * void cpu_reboot(int howto, char *bootstr)
243 *
244 * Reboots the system
245 *
246 * Deal with any syncing, unmounting, dumping and shutdown hooks,
247 * then reset the CPU.
248 */
249
250void
251cpu_reboot(int howto, char *bootstr)
252{
253#ifdef DIAGNOSTIC
254	/* info */
255	printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
256#endif
257
258	/*
259	 * If we are still cold then hit the air brakes
260	 * and crash to earth fast
261	 */
262	if (cold) {
263		doshutdownhooks();
264		pmf_system_shutdown(boothowto);
265		printf("The operating system has halted.\n");
266		printf("Please press any key to reboot.\n\n");
267		cngetc();
268		printf("rebooting...\n");
269		cpu_reset();
270		/*NOTREACHED*/
271	}
272
273	/* Disable console buffering */
274/*	cnpollc(1);*/
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
279	 * the unmount.  It looks like syslogd is getting woken up
280	 * only to find that it cannot page part of the binary in as
281	 * the filesystem has 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	pmf_system_shutdown(boothowto);
297
298	/* Make sure IRQ's are disabled */
299	IRQdisable;
300
301	if (howto & RB_HALT) {
302		printf("The operating system has halted.\n");
303		printf("Please press any key to reboot.\n\n");
304		cngetc();
305	}
306
307	printf("rebooting...\n");
308	cpu_reset();
309	/*NOTREACHED*/
310}
311
312/*
313 * NB: this function runs with MMU disabled!
314 */
315static void
316netwinder_reset(void)
317{
318	register u_int base = DC21285_PCI_IO_BASE;
319
320#define PUTBYTE(reg, val) \
321	*((volatile u_int8_t *)(base + (reg))) = (val)
322
323	PUTBYTE(0x338, 0x84);	/* Red led(GP17), fan on(GP12) */
324	PUTBYTE(0x370, 0x87);	/* Enter the extended function mode */
325	PUTBYTE(0x370, 0x87);	/* (need to write the magic twice) */
326	PUTBYTE(0x370, 0x07); 	/* Select Logical Device Number reg */
327	PUTBYTE(0x371, 0x07);	/* Select Logical Device 7 (GPIO) */
328	PUTBYTE(0x370, 0xe6);	/* Select GP16 Control Reg */
329	PUTBYTE(0x371, 0x00);	/* Make GP16 an output */
330	PUTBYTE(0x338, 0xc4);	/* RESET(GP16), red led, fan on */
331}
332
333/*
334 * Mapping table for core kernel memory. This memory is mapped at init
335 * time with section mappings.
336 */
337struct l1_sec_map {
338	vm_offset_t	va;
339	vm_offset_t	pa;
340	vm_size_t	size;
341	vm_prot_t	prot;
342	int		cache;
343} l1_sec_table[] = {
344	/* Map 1MB for CSR space */
345	{ DC21285_ARMCSR_VBASE,			DC21285_ARMCSR_BASE,
346	    DC21285_ARMCSR_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
347	    PTE_NOCACHE },
348
349	/* Map 1MB for fast cache cleaning space */
350	{ DC21285_CACHE_FLUSH_VBASE,		DC21285_SA_CACHE_FLUSH_BASE,
351	    DC21285_CACHE_FLUSH_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
352	    PTE_CACHE },
353
354	/* Map 1MB for PCI IO space */
355	{ DC21285_PCI_IO_VBASE,			DC21285_PCI_IO_BASE,
356	    DC21285_PCI_IO_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
357	    PTE_NOCACHE },
358
359	/* Map 1MB for PCI IACK space */
360	{ DC21285_PCI_IACK_VBASE,		DC21285_PCI_IACK_SPECIAL,
361	    DC21285_PCI_IACK_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
362	    PTE_NOCACHE },
363
364	/* Map 16MB of type 1 PCI config access */
365	{ DC21285_PCI_TYPE_1_CONFIG_VBASE,	DC21285_PCI_TYPE_1_CONFIG,
366	    DC21285_PCI_TYPE_1_CONFIG_VSIZE,	VM_PROT_READ|VM_PROT_WRITE,
367	    PTE_NOCACHE },
368
369	/* Map 16MB of type 0 PCI config access */
370	{ DC21285_PCI_TYPE_0_CONFIG_VBASE,	DC21285_PCI_TYPE_0_CONFIG,
371	    DC21285_PCI_TYPE_0_CONFIG_VSIZE,	VM_PROT_READ|VM_PROT_WRITE,
372	    PTE_NOCACHE },
373
374	/* Map 1MB of 32 bit PCI address space for ISA MEM accesses via PCI */
375	{ DC21285_PCI_ISA_MEM_VBASE,		DC21285_PCI_MEM_BASE,
376	    DC21285_PCI_ISA_MEM_VSIZE,		VM_PROT_READ|VM_PROT_WRITE,
377	    PTE_NOCACHE },
378
379#if NIGSFB > 0
380	/* XXX: uwe: Map 16MB of PCI address space for CyberPro as console */
381	{ IGS_PCI_MEM_VBASE,	DC21285_PCI_MEM_BASE + IGS_PCI_MEM_BASE,
382	    IGS_PCI_MEM_VSIZE,			VM_PROT_READ|VM_PROT_WRITE,
383	    PTE_NOCACHE },
384#endif
385
386	{ 0, 0, 0, 0, 0 }
387};
388
389/*
390 * u_int initarm(...);
391 *
392 * Initial entry point on startup. This gets called before main() is
393 * entered.
394 * It should be responsible for setting up everything that must be
395 * in place when main is called.
396 * This includes
397 *   Taking a copy of the boot configuration structure.
398 *   Initialising the physical console so characters can be printed.
399 *   Setting up page tables for the kernel
400 *   Relocating the kernel to the bottom of physical memory
401 */
402
403u_int
404initarm(void *arg)
405{
406	int loop;
407	int loop1;
408	u_int l1pagetable;
409	extern char _end[];
410
411	/*
412	 * Turn the led off, then turn it yellow.
413	 * 0x80 - red; 0x04 - fan; 0x02 - green.
414	 */
415	ISA_PUTBYTE(0x338, 0x04);
416	ISA_PUTBYTE(0x338, 0x86);
417
418	/*
419	 * Set up a diagnostic console so we can see what's going
420	 * on.
421	 */
422	cn_tab = &kcomcons;
423
424	/* Talk to the user */
425	printf("\nNetBSD/netwinder booting ...\n");
426
427	/*
428	 * Heads up ... Setup the CPU / MMU / TLB functions
429	 */
430	if (set_cpufuncs())
431		panic("CPU not recognized!");
432
433	/*
434	 * We are currently running with the MMU enabled and the
435	 * entire address space mapped VA==PA, except for the
436	 * first 64MB of RAM is also double-mapped at 0xf0000000.
437	 * There is an L1 page table at 0x00008000.
438	 *
439	 * We also have the 21285's PCI I/O space mapped where
440	 * we expect it.
441	 */
442
443	printf("initarm: Configuring system ...\n");
444
445	/*
446	 * Copy out the boot info passed by the firmware.  Note that
447	 * early versions of NeTTrom fill this in with bogus values,
448	 * so we need to sanity check it.
449	 */
450	memcpy(&nwbootinfo, (void *)(KERNEL_BASE + 0x100),
451	    sizeof(nwbootinfo));
452#ifdef VERBOSE_INIT_ARM
453	printf("NeTTrom boot info:\n");
454	printf("\tpage size = 0x%08lx\n", nwbootinfo.bi_pagesize);
455	printf("\tnpages = %ld (0x%08lx)\n", nwbootinfo.bi_nrpages,
456	    nwbootinfo.bi_nrpages);
457	printf("\trootdev = 0x%08lx\n", nwbootinfo.bi_rootdev);
458	printf("\tcmdline = %s\n", nwbootinfo.bi_cmdline);
459#endif
460	if (nwbootinfo.bi_nrpages != 0x02000 &&
461	    nwbootinfo.bi_nrpages != 0x04000 &&
462	    nwbootinfo.bi_nrpages != 0x08000 &&
463	    nwbootinfo.bi_nrpages != 0x10000) {
464		nwbootinfo.bi_pagesize = 0xdeadbeef;
465		nwbootinfo.bi_nrpages = 0x01000;	/* 16MB */
466		nwbootinfo.bi_rootdev = 0;
467	}
468
469	/* Fake bootconfig structure for the benefit of pmap.c */
470	/* XXX must make the memory description h/w independent */
471	bootconfig.dramblocks = 1;
472	bootconfig.dram[0].address = 0;
473	bootconfig.dram[0].pages = nwbootinfo.bi_nrpages;
474
475	/*
476	 * Set up the variables that define the availablilty of
477	 * physical memory.
478	 *
479	 * Since the NetWinder NeTTrom doesn't load ELF symbols
480	 * for us, we can safely assume that everything after end[]
481	 * is free.  We start there and allocate upwards.
482	 */
483	physical_start = bootconfig.dram[0].address;
484	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
485
486	physical_freestart = ((((vaddr_t) _end) + PGOFSET) & ~PGOFSET) -
487	    KERNEL_BASE;
488	physical_freeend = physical_end;
489	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
490
491#ifdef VERBOSE_INIT_ARM
492	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
493	       physical_freestart, free_pages, free_pages);
494#endif
495
496	physmem = (physical_end - physical_start) / PAGE_SIZE;
497
498	/* Tell the user about the memory */
499	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
500	    physical_start, physical_end - 1);
501
502	/*
503	 * Okay, we need to allocate some fixed page tables to get the
504	 * kernel going.  We allocate one page directory and a number
505	 * of page tables and store the physical addresses in the
506	 * kernel_pt_table array.
507	 *
508	 * The kernel page directory must be on a 16K boundary.  The page
509	 * tables must be on 4K boundaries.  What we do is allocate the
510	 * page directory on the first 16K boundary that we encounter,
511	 * and the page tables on 4K boundaries otherwise.  Since we
512	 * allocate at least 3 L2 page tables, we are guaranteed to
513	 * encounter at least one 16K aligned region.
514	 */
515
516#ifdef VERBOSE_INIT_ARM
517	printf("Allocating page tables\n");
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	(var) = physical_freestart;		\
527	physical_freestart += ((np) * PAGE_SIZE);\
528	free_pages -= (np);			\
529	memset((char *)(var), 0, ((np) * PAGE_SIZE));
530
531	loop1 = 0;
532	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
533		/* Are we 16KB aligned for an L1 ? */
534		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
535		    && kernel_l1pt.pv_pa == 0) {
536			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
537		} else {
538			valloc_pages(kernel_pt_table[loop1],
539			    L2_TABLE_SIZE / PAGE_SIZE);
540			++loop1;
541		}
542	}
543
544	/* This should never be able to happen but better confirm that. */
545	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
546		panic("initarm: Failed to align the kernel page directory");
547
548	/*
549	 * Allocate a page for the system page mapped to V0x00000000
550	 * This page will just contain the system vectors and can be
551	 * shared by all processes.
552	 */
553	alloc_pages(systempage.pv_pa, 1);
554
555	/* Allocate stacks for all modes */
556	valloc_pages(irqstack, IRQ_STACK_SIZE);
557	valloc_pages(abtstack, ABT_STACK_SIZE);
558	valloc_pages(undstack, UND_STACK_SIZE);
559	valloc_pages(kernelstack, UPAGES);
560
561#ifdef VERBOSE_INIT_ARM
562	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
563	    irqstack.pv_va);
564	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
565	    abtstack.pv_va);
566	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
567	    undstack.pv_va);
568	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
569	    kernelstack.pv_va);
570#endif
571
572	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
573
574	/*
575	 * Ok we have allocated physical pages for the primary kernel
576	 * page tables
577	 */
578
579#ifdef VERBOSE_INIT_ARM
580	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
581#endif
582
583	/*
584	 * Now we start consturction of the L1 page table
585	 * We start by mapping the L2 page tables into the L1.
586	 * This means that we can replace L1 mappings later on if necessary
587	 */
588	l1pagetable = kernel_l1pt.pv_pa;
589
590	/* Map the L2 pages tables in the L1 page table */
591	pmap_link_l2pt(l1pagetable, 0x00000000,
592	    &kernel_pt_table[KERNEL_PT_SYS]);
593	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
594	    &kernel_pt_table[KERNEL_PT_KERNEL]);
595	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
596		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
597		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
598
599	/* update the top of the kernel VM */
600	pmap_curmaxkvaddr =
601	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
602
603#ifdef VERBOSE_INIT_ARM
604	printf("Mapping kernel\n");
605#endif
606
607	/* Now we fill in the L2 pagetable for the kernel static code/data */
608	{
609		/*
610		 * The kernel starts in the first 1MB of RAM, and we'd
611		 * like to use a section mapping for text, so we'll just
612		 * map from KERNEL_BASE to etext[] to _end[].
613		 */
614
615		extern char etext[];
616		size_t textsize = (uintptr_t) etext - KERNEL_BASE;
617		size_t totalsize = (uintptr_t) _end - KERNEL_BASE;
618		u_int logical;
619
620		textsize = (textsize + PGOFSET) & ~PGOFSET;
621		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
622
623		textsize = textsize & ~PGOFSET;
624		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
625
626		logical = 0;		/* offset into RAM */
627
628		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
629		    physical_start + logical, textsize,
630		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
631		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
632		    physical_start + logical, totalsize - textsize,
633		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
634	}
635
636#ifdef VERBOSE_INIT_ARM
637	printf("Constructing L2 page tables\n");
638#endif
639
640	/* Map the stack pages */
641	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
642	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
643	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
644	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
645	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
646	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
647	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
648	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
649
650	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
651	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
652
653	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
654		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
655		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
656		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
657	}
658
659	/* Map the vector page. */
660	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
661	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
662
663	/*
664	 * Map devices we can map w/ section mappings.
665	 */
666	loop = 0;
667	while (l1_sec_table[loop].size) {
668		vm_size_t sz;
669
670#ifdef VERBOSE_INIT_ARM
671		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
672		    l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
673		    l1_sec_table[loop].va);
674#endif
675		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
676			pmap_map_section(l1pagetable,
677			    l1_sec_table[loop].va + sz,
678			    l1_sec_table[loop].pa + sz,
679			    l1_sec_table[loop].prot,
680			    l1_sec_table[loop].cache);
681		++loop;
682	}
683
684	/*
685	 * Now we have the real page tables in place so we can switch to them.
686	 * Once this is done we will be running with the REAL kernel page
687	 * tables.
688	 */
689
690	/* Switch tables */
691#ifdef VERBOSE_INIT_ARM
692	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
693	       physical_freestart, free_pages, free_pages);
694	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
695#endif
696
697	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
698	cpu_setttb(kernel_l1pt.pv_pa);
699	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
700
701	/*
702	 * Moved from cpu_startup() as data_abort_handler() references
703	 * this during uvm init
704	 */
705	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
706
707#ifdef VERBOSE_INIT_ARM
708	printf("done!\n");
709#endif
710
711	/*
712	 * XXX this should only be done in main() but it useful to
713	 * have output earlier ...
714	 */
715	consinit();
716
717#ifdef VERBOSE_INIT_ARM
718	printf("bootstrap done.\n");
719#endif
720
721	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
722
723	/*
724	 * Pages were allocated during the secondary bootstrap for the
725	 * stacks for different CPU modes.
726	 * We must now set the r13 registers in the different CPU modes to
727	 * point to these stacks.
728	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
729	 * of the stack memory.
730	 */
731	printf("init subsystems: stacks ");
732
733	set_stackptr(PSR_IRQ32_MODE,
734	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
735	set_stackptr(PSR_ABT32_MODE,
736	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
737	set_stackptr(PSR_UND32_MODE,
738	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
739
740	/*
741	 * Well we should set a data abort handler.
742	 * Once things get going this will change as we will need a proper
743	 * handler.
744	 * Until then we will use a handler that just panics but tells us
745	 * why.
746	 * Initialisation of the vectors will just panic on a data abort.
747	 * This just fills in a slightly better one.
748	 */
749	printf("vectors ");
750	data_abort_handler_address = (u_int)data_abort_handler;
751	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
752	undefined_handler_address = (u_int)undefinedinstruction_bounce;
753
754	/* Initialise the undefined instruction handlers */
755	printf("undefined ");
756	undefined_init();
757
758	/* Load memory into UVM. */
759	printf("page ");
760	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
761
762	/* XXX Always one RAM block -- nuke the loop. */
763	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
764		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
765		paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
766#if NISADMA > 0
767		paddr_t istart, isize;
768		extern struct arm32_dma_range *footbridge_isa_dma_ranges;
769		extern int footbridge_isa_dma_nranges;
770#endif
771
772		if (start < physical_freestart)
773			start = physical_freestart;
774		if (end > physical_freeend)
775			end = physical_freeend;
776
777#if 0
778		printf("%d: %lx -> %lx\n", loop, start, end - 1);
779#endif
780
781#if NISADMA > 0
782		if (arm32_dma_range_intersect(footbridge_isa_dma_ranges,
783					      footbridge_isa_dma_nranges,
784					      start, end - start,
785					      &istart, &isize)) {
786			/*
787			 * Place the pages that intersect with the
788			 * ISA DMA range onto the ISA DMA free list.
789			 */
790#if 0
791			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
792			    istart + isize - 1);
793#endif
794			uvm_page_physload(atop(istart),
795			    atop(istart + isize), atop(istart),
796			    atop(istart + isize), VM_FREELIST_ISADMA);
797
798			/*
799			 * Load the pieces that come before the
800			 * intersection onto the default free list.
801			 */
802			if (start < istart) {
803#if 0
804				printf("    BEFORE 0x%lx -> 0x%lx\n",
805				    start, istart - 1);
806#endif
807				uvm_page_physload(atop(start),
808				    atop(istart), atop(start),
809				    atop(istart), VM_FREELIST_DEFAULT);
810			}
811
812			/*
813			 * Load the pieces that come after the
814			 * intersection onto the default free list.
815			 */
816			if ((istart + isize) < end) {
817#if 0
818				printf("     AFTER 0x%lx -> 0x%lx\n",
819				    (istart + isize), end - 1);
820#endif
821				uvm_page_physload(atop(istart + isize),
822				    atop(end), atop(istart + isize),
823				    atop(end), VM_FREELIST_DEFAULT);
824			}
825		} else {
826			uvm_page_physload(atop(start), atop(end),
827			    atop(start), atop(end), VM_FREELIST_DEFAULT);
828		}
829#else /* NISADMA > 0 */
830		uvm_page_physload(atop(start), atop(end),
831		    atop(start), atop(end), VM_FREELIST_DEFAULT);
832#endif /* NISADMA > 0 */
833	}
834
835	/* Boot strap pmap telling it where the kernel page table is */
836	printf("pmap ");
837	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
838
839	/* Now that pmap is inited, we can set cpu_reset_address */
840	cpu_reset_address = (u_int)vtophys((vaddr_t)netwinder_reset);
841
842	/* Setup the IRQ system */
843	printf("irq ");
844	footbridge_intr_init();
845	printf("done.\n");
846
847	/*
848	 * Warn the user if the bootinfo was bogus.  We already
849	 * faked up some safe values.
850	 */
851	if (nwbootinfo.bi_pagesize == 0xdeadbeef)
852		printf("WARNING: NeTTrom boot info corrupt\n");
853
854#ifdef DDB
855	db_machine_init();
856	if (boothowto & RB_KDB)
857		Debugger();
858#endif
859
860	/* Turn the led green */
861	ISA_PUTBYTE(0x338, 0x06);
862
863	/* We return the new stack pointer address */
864	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
865}
866
867void
868process_kernel_args(char *args)
869{
870
871	boothowto = 0;
872
873	/* Make a local copy of the bootargs */
874	strncpy(bootargs, args, MAX_BOOT_STRING);
875
876	args = bootargs;
877	boot_file = bootargs;
878
879	/* Skip the kernel image filename */
880	while (*args != ' ' && *args != 0)
881		++args;
882
883	if (*args != 0)
884		*args++ = 0;
885
886	while (*args == ' ')
887		++args;
888
889	boot_args = args;
890
891	printf("bootfile: %s\n", boot_file);
892	printf("bootargs: %s\n", boot_args);
893
894	parse_mi_bootargs(boot_args);
895}
896
897void
898consinit(void)
899{
900	static int consinit_called = 0;
901	const char *console = CONSDEVNAME;
902
903	if (consinit_called != 0)
904		return;
905
906	consinit_called = 1;
907
908#ifdef DIAGNOSTIC
909	printf("consinit(\"%s\")\n", console);
910#endif
911
912#if NISA > 0
913	/* Initialise the ISA subsystem early ... */
914	isa_footbridge_init(DC21285_PCI_IO_VBASE, DC21285_PCI_ISA_MEM_VBASE);
915#endif
916
917	if (strncmp(console, "igsfb", 5) == 0) {
918#if NIGSFB > 0
919		int res;
920
921		footbridge_pci_bs_tag_init();
922
923		/*
924		 * XXX: uwe: special case mapping for the igsfb memory space.
925		 *
926		 * The problem with this is that when footbridge is
927		 * attached during normal autoconfiguration the bus
928		 * space tags will be reinited and these hooks lost.
929		 * However, since igsfb(4) don't unmap memory during
930		 * normal operation, this is ok.  But if the igsfb is
931		 * configured but is not a console, we waste 16M of
932		 * kernel VA space.
933		 */
934		footbridge_pci_mem_bs_tag.bs_map = nw_footbridge_mem_bs_map;
935		footbridge_pci_mem_bs_tag.bs_unmap = nw_footbridge_mem_bs_unmap;
936
937		igsfb_pci_cnattach(&footbridge_pci_io_bs_tag,
938				   &footbridge_pci_mem_bs_tag,
939				   &footbridge_pci_chipset,
940				   0, 8, 0);
941#if NPCKBC > 0
942		res = pckbc_cnattach(&isa_io_bs_tag,
943				     IO_KBD, KBCMDP, PCKBC_KBD_SLOT);
944		if (res)
945			printf("pckbc_cnattach: %d!\n", res);
946#endif
947#else
948		panic("igsfb console not configured");
949#endif /* NIGSFB */
950	} else {
951#ifdef DIAGNOSTIC
952		if (strncmp(console, "com", 3) != 0) {
953			printf("consinit: unknown CONSDEVNAME=\"%s\","
954			       " falling back to \"com\"\n", console);
955		}
956#endif
957#if NCOM > 0
958		if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
959				COM_FREQ, COM_TYPE_NORMAL, comcnmode))
960			panic("can't init serial console @%x", CONCOMADDR);
961#else
962		panic("serial console @%x not configured", CONCOMADDR);
963#endif
964	}
965}
966
967
968#if NIGSFB > 0
969static int
970nw_footbridge_mem_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int cacheable, bus_space_handle_t *bshp)
971{
972	bus_addr_t startpa, endpa;
973
974	/* Round the allocation to page boundries */
975	startpa = trunc_page(bpa);
976	endpa = round_page(bpa + size);
977
978	/*
979	 * Check for mappings of the igsfb(4) memory space as we have
980	 * this space already mapped.
981	 */
982	if (startpa >= IGS_PCI_MEM_BASE
983	    && endpa < (IGS_PCI_MEM_BASE + IGS_PCI_MEM_VSIZE)) {
984		/* Store the bus space handle */
985		*bshp =  IGS_PCI_MEM_VBASE
986			+ (bpa - IGS_PCI_MEM_BASE);
987#ifdef DEBUG
988		printf("nw/mem_bs_map: %08x+%08x: %08x..%08x -> %08x\n",
989		       (u_int32_t)bpa, (u_int32_t)size,
990		       (u_int32_t)startpa, (u_int32_t)endpa,
991		       (u_int32_t)*bshp);
992#endif
993		return 0;
994	}
995
996	return (footbridge_mem_bs_map(t, bpa, size, cacheable, bshp));
997}
998
999
1000static void
1001nw_footbridge_mem_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
1002{
1003
1004	/*
1005	 * Check for mappings of the igsfb(4) memory space as we have
1006	 * this space already mapped.
1007	 */
1008	if (bsh >= IGS_PCI_MEM_VBASE
1009	    && bsh < (IGS_PCI_MEM_VBASE + IGS_PCI_MEM_VSIZE)) {
1010#ifdef DEBUG
1011		printf("nw/bs_unmap: 0x%08x\n", (u_int32_t)bsh);
1012#endif
1013		return;
1014	}
1015
1016	footbridge_mem_bs_unmap(t, bsh, size);
1017}
1018#endif /* NIGSFB */
1019
1020
1021static bus_space_handle_t kcom_base = (bus_space_handle_t) (DC21285_PCI_IO_VBASE + CONCOMADDR);
1022
1023#define	KCOM_GETBYTE(r)		generic_bs_r_1(0, kcom_base, (r))
1024#define	KCOM_PUTBYTE(r,v)	generic_bs_w_1(0, kcom_base, (r), (v))
1025
1026static int
1027kcomcngetc(dev_t dev)
1028{
1029	int stat, c;
1030
1031	/* block until a character becomes available */
1032	while (!ISSET(stat = KCOM_GETBYTE(com_lsr), LSR_RXRDY))
1033		;
1034
1035	c = KCOM_GETBYTE(com_data);
1036	stat = KCOM_GETBYTE(com_iir);
1037	return c;
1038}
1039
1040/*
1041 * Console kernel output character routine.
1042 */
1043static void
1044kcomcnputc(dev_t dev, int c)
1045{
1046	int timo;
1047
1048	/* wait for any pending transmission to finish */
1049	timo = 150000;
1050	while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
1051		continue;
1052
1053	KCOM_PUTBYTE(com_data, c);
1054
1055	/* wait for this transmission to complete */
1056	timo = 1500000;
1057	while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
1058		continue;
1059}
1060
1061static void
1062kcomcnpollc(dev_t dev, int on)
1063{
1064}
1065
1066struct consdev kcomcons = {
1067	NULL, NULL, kcomcngetc, kcomcnputc, kcomcnpollc, NULL,
1068	NULL, NULL, NODEV, CN_NORMAL
1069};
1070