1/*	$NetBSD: viper_machdep.c,v 1.35 2024/02/20 23:36:01 andvar Exp $ */
2
3/*
4 * Startup routines for the Arcom Viper.  Below you can trace the
5 * impressive lineage ;)
6 *
7 * Modified for the Viper by Antti Kantee <pooka@netbsd.org>
8 */
9
10/*
11 * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
12 * Written by Hiroyuki Bessho for Genetec Corporation.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. The name of Genetec Corporation may not be used to endorse or
23 *    promote products derived from this software without specific prior
24 *    written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``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 GENETEC CORPORATION
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 * Machine dependent functions for kernel setup for
39 * Intel DBPXA250 evaluation board (a.k.a. Lubbock).
40 * Based on iq80310_machhdep.c
41 */
42/*
43 * Copyright (c) 2001 Wasabi Systems, Inc.
44 * All rights reserved.
45 *
46 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 *    notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 *    notice, this list of conditions and the following disclaimer in the
55 *    documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 *    must display the following acknowledgement:
58 *	This product includes software developed for the NetBSD Project by
59 *	Wasabi Systems, Inc.
60 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
61 *    or promote products derived from this software without specific prior
62 *    written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
66 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
68 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74 * POSSIBILITY OF SUCH DAMAGE.
75 */
76
77/*
78 * Copyright (c) 1997,1998 Mark Brinicombe.
79 * Copyright (c) 1997,1998 Causality Limited.
80 * All rights reserved.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 *    notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 *    notice, this list of conditions and the following disclaimer in the
89 *    documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 *    must display the following acknowledgement:
92 *	This product includes software developed by Mark Brinicombe
93 *	for the NetBSD Project.
94 * 4. The name of the company nor the name of the author may be used to
95 *    endorse or promote products derived from this software without specific
96 *    prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
99 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
100 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
101 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
102 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
103 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
104 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * Machine dependent functions for kernel setup for Intel IQ80310 evaluation
111 * boards using RedBoot firmware.
112 */
113
114#include <sys/cdefs.h>
115__KERNEL_RCSID(0, "$NetBSD: viper_machdep.c,v 1.35 2024/02/20 23:36:01 andvar Exp $");
116
117#include "opt_arm_debug.h"
118#include "opt_console.h"
119#include "opt_ddb.h"
120#include "opt_kgdb.h"
121#include "opt_md.h"
122#include "opt_com.h"
123#include "lcd.h"
124
125#include <sys/param.h>
126#include <sys/device.h>
127#include <sys/systm.h>
128#include <sys/kernel.h>
129#include <sys/exec.h>
130#include <sys/proc.h>
131#include <sys/msgbuf.h>
132#include <sys/reboot.h>
133#include <sys/termios.h>
134#include <sys/ksyms.h>
135#include <sys/bus.h>
136#include <sys/cpu.h>
137
138#include <uvm/uvm_extern.h>
139
140#include <sys/conf.h>
141#include <dev/cons.h>
142#include <dev/md.h>
143#include <dev/ic/smc91cxxreg.h>
144
145#include <machine/db_machdep.h>
146#include <ddb/db_sym.h>
147#include <ddb/db_extern.h>
148#ifdef KGDB
149#include <sys/kgdb.h>
150#endif
151
152#include <machine/bootconfig.h>
153#include <arm/locore.h>
154#include <arm/undefined.h>
155
156#include <arm/arm32/machdep.h>
157
158#include <arm/xscale/pxa2x0reg.h>
159#include <arm/xscale/pxa2x0var.h>
160#include <arm/xscale/pxa2x0_gpio.h>
161#include <arm/sa11x0/sa1111_reg.h>
162#include <evbarm/viper/viper_reg.h>
163
164/* Kernel text starts 2MB in from the bottom of the kernel address space. */
165#define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
166#define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
167
168/*
169 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
170 * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
171 */
172#define KERNEL_VM_SIZE		0x0C000000
173
174BootConfig bootconfig;		/* Boot config storage */
175char *boot_args = NULL;
176char *boot_file = NULL;
177
178vaddr_t physical_start;
179vaddr_t physical_freestart;
180vaddr_t physical_freeend;
181vaddr_t physical_end;
182u_int free_pages;
183
184/*int debug_flags;*/
185#ifndef PMAP_STATIC_L1S
186int max_processes = 64;			/* Default number */
187#endif	/* !PMAP_STATIC_L1S */
188
189/* Physical and virtual addresses for some global pages */
190pv_addr_t minidataclean;
191
192paddr_t msgbufphys;
193
194#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
195#define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
196#define	KERNEL_PT_KERNEL_NUM	4
197#define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
198				        /* Page tables for mapping kernel VM */
199#define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
200#define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
201
202pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
203
204/* Prototypes */
205
206#if 0
207void	process_kernel_args(char *);
208#endif
209
210void	consinit(void);
211void	kgdb_port_init(void);
212void	change_clock(uint32_t v);
213
214bs_protos(bs_notimpl);
215
216#include "com.h"
217#if NCOM > 0
218#include <dev/ic/comreg.h>
219#include <dev/ic/comvar.h>
220#endif
221
222#ifndef CONSPEED
223#define CONSPEED B115200	/* What RedBoot uses */
224#endif
225#ifndef CONMODE
226#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
227#endif
228
229int comcnspeed = CONSPEED;
230int comcnmode = CONMODE;
231
232static struct pxa2x0_gpioconf boarddep_gpioconf[] = {
233	{ 44, GPIO_ALT_FN_1_IN },	/* BTCST */
234	{ 45, GPIO_ALT_FN_2_OUT },	/* BTRST */
235
236	{ -1 }
237};
238static struct pxa2x0_gpioconf *viper_gpioconf[] = {
239	pxa25x_com_btuart_gpioconf,
240	pxa25x_com_ffuart_gpioconf,
241	pxa25x_com_stuart_gpioconf,
242	boarddep_gpioconf,
243	NULL
244};
245
246/*
247 * void cpu_reboot(int howto, char *bootstr)
248 *
249 * Reboots the system
250 *
251 * Deal with any syncing, unmounting, dumping and shutdown hooks,
252 * then reset the CPU.
253 */
254void
255cpu_reboot(int howto, char *bootstr)
256{
257#ifdef DIAGNOSTIC
258	/* info */
259	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
260#endif
261
262	/*
263	 * If we are still cold then hit the air brakes
264	 * and crash to earth fast
265	 */
266	if (cold) {
267		doshutdownhooks();
268		pmf_system_shutdown(boothowto);
269		printf("The operating system has halted.\n");
270		printf("Please press any key to reboot.\n\n");
271		cngetc();
272		printf("rebooting...\n");
273		cpu_reset();
274		/*NOTREACHED*/
275	}
276
277	/* Disable console buffering */
278/*	cnpollc(1);*/
279
280	/*
281	 * If RB_NOSYNC was not specified sync the discs.
282	 * Note: Unless cold is set to 1 here, syslogd will die during the
283	 * unmount.  It looks like syslogd is getting woken up only to find
284	 * that it cannot page part of the binary in as the filesystem has
285	 * been unmounted.
286	 */
287	if (!(howto & RB_NOSYNC))
288		bootsync();
289
290	/* Say NO to interrupts */
291	splhigh();
292
293	/* Do a dump if requested. */
294	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
295		dumpsys();
296
297	/* Run any shutdown hooks */
298	doshutdownhooks();
299
300	pmf_system_shutdown(boothowto);
301
302	/* Make sure IRQ's are disabled */
303	IRQdisable;
304
305	if (howto & RB_HALT) {
306		printf("The operating system has halted.\n");
307		printf("Please press any key to reboot.\n\n");
308		cngetc();
309	}
310
311	printf("rebooting...\n");
312	cpu_reset();
313	/*NOTREACHED*/
314}
315
316/*
317 * Static device mappings. These peripheral registers are mapped at
318 * fixed virtual addresses very early in viper_start() so that we
319 * can use them while booting the kernel, and stay at the same address
320 * throughout whole kernel's life time.
321 *
322 * We use this table twice; once with bootstrap page table, and once
323 * with kernel's page table which we build up in initarm().
324 */
325
326static const struct pmap_devmap viper_devmap[] = {
327    {
328	    VIPER_GPIO_VBASE,
329	    PXA2X0_GPIO_BASE,
330	    L1_S_SIZE,
331	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
332    },
333    {
334	    VIPER_CLKMAN_VBASE,
335	    PXA2X0_CLKMAN_BASE,
336	    L1_S_SIZE,
337	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
338    },
339    {
340	    VIPER_INTCTL_VBASE,
341	    PXA2X0_INTCTL_BASE,
342	    L1_S_SIZE,
343	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
344    },
345    {
346	    VIPER_FFUART_VBASE,
347	    PXA2X0_FFUART_BASE,
348	    L1_S_SIZE,
349	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
350    },
351    {
352	    VIPER_BTUART_VBASE,
353	    PXA2X0_BTUART_BASE,
354	    L1_S_SIZE,
355	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
356    },
357
358    {0, 0, 0, 0,}
359};
360
361#ifndef MEMSTART
362#define MEMSTART 0xa0000000
363#endif
364#ifndef MEMSIZE
365#define MEMSIZE 0x4000000
366#endif
367
368/*
369 * vaddr_t initarm(...)
370 *
371 * Initial entry point on startup. This gets called before main() is
372 * entered.
373 * It should be responsible for setting up everything that must be
374 * in place when main is called.
375 * This includes
376 *   Taking a copy of the boot configuration structure.
377 *   Initialising the physical console so characters can be printed.
378 *   Setting up page tables for the kernel
379 *   Relocating the kernel to the bottom of physical memory
380 */
381vaddr_t
382initarm(void *arg)
383{
384	int loop;
385	int loop1;
386	u_int l1pagetable;
387
388	/* Register devmap for devices we mapped in start */
389	pmap_devmap_register(viper_devmap);
390
391	/* start 32.768 kHz OSC */
392	ioreg_write(VIPER_CLKMAN_VBASE + 0x08, 2);
393	/* Get ready for splfoo() */
394	pxa2x0_intr_bootstrap(VIPER_INTCTL_VBASE);
395
396	/*
397	 * Heads up ... Setup the CPU / MMU / TLB functions
398	 */
399	if (set_cpufuncs())
400		panic("cpu not recognized!");
401
402#if 0
403	/* Calibrate the delay loop. */
404#endif
405
406	/* setup GPIO for BTUART, in case bootloader doesn't take care of it */
407	pxa2x0_gpio_bootstrap(VIPER_GPIO_VBASE);
408	pxa2x0_gpio_config(viper_gpioconf);
409
410	/* turn on clock to UART block.
411	   XXX: this should not be done here. */
412	ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, CKEN_FFUART|CKEN_BTUART |
413	    ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN));
414
415	consinit();
416#ifdef KGDB
417	kgdb_port_init();
418#endif
419	/* Talk to the user */
420	printf("\nNetBSD/evbarm (viper) booting ...\n");
421
422#if 0
423	/*
424	 * Examine the boot args string for options we need to know about
425	 * now.
426	 */
427	process_kernel_args((char *)nwbootinfo.bt_args);
428#endif
429
430	printf("initarm: Configuring system ...\n");
431
432	/* Fake bootconfig structure for the benefit of pmap.c */
433	/* XXX must make the memory description h/w independent */
434	bootconfig.dramblocks = 1;
435	bootconfig.dram[0].address = MEMSTART;
436	bootconfig.dram[0].pages = MEMSIZE / PAGE_SIZE;
437
438	/*
439	 * Set up the variables that define the availability of
440	 * physical memory.  For now, we're going to set
441	 * physical_freestart to 0xa0200000 (where the kernel
442	 * was loaded), and allocate the memory we need downwards.
443	 * If we get too close to the page tables that RedBoot
444	 * set up, we will panic.  We will update physical_freestart
445	 * and physical_freeend later to reflect what pmap_bootstrap()
446	 * wants to see.
447	 *
448	 * XXX pmap_bootstrap() needs an enema.
449	 * (now that would be truly hardcore XXX)
450	 */
451	physical_start = bootconfig.dram[0].address;
452	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
453
454	physical_freestart = 0xa0009000UL;
455	physical_freeend = 0xa0200000UL;
456
457	physmem = (physical_end - physical_start) / PAGE_SIZE;
458
459#ifdef VERBOSE_INIT_ARM
460	/* Tell the user about the memory */
461	printf("physmemory: 0x%"PRIxPSIZE" pages at 0x%08lx -> 0x%08lx\n", physmem,
462	    physical_start, physical_end - 1);
463#endif
464
465	/*
466	 * Okay, the kernel starts 2MB in from the bottom of physical
467	 * memory.  We are going to allocate our bootstrap pages downwards
468	 * from there.
469	 *
470	 * We need to allocate some fixed page tables to get the kernel
471	 * going.  We allocate one page directory and a number of page
472	 * tables and store the physical addresses in the kernel_pt_table
473	 * array.
474	 *
475	 * The kernel page directory must be on a 16K boundary.  The page
476	 * tables must be on 4K boundaries.  What we do is allocate the
477	 * page directory on the first 16K boundary that we encounter, and
478	 * the page tables on 4K boundaries otherwise.  Since we allocate
479	 * at least 3 L2 page tables, we are guaranteed to encounter at
480	 * least one 16K aligned region.
481	 */
482
483#ifdef VERBOSE_INIT_ARM
484	printf("Allocating page tables\n");
485#endif
486
487	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
488
489#ifdef VERBOSE_INIT_ARM
490	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
491	       physical_freestart, free_pages, free_pages);
492#endif
493
494	/* Define a macro to simplify memory allocation */
495#define	valloc_pages(var, np)				\
496	alloc_pages((var).pv_pa, (np));			\
497	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
498
499#define alloc_pages(var, np)				\
500	physical_freeend -= ((np) * PAGE_SIZE);		\
501	if (physical_freeend < physical_freestart)	\
502		panic("initarm: out of memory");	\
503	(var) = physical_freeend;			\
504	free_pages -= (np);				\
505	memset((char *)(var), 0, ((np) * PAGE_SIZE));
506
507	loop1 = 0;
508	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
509		/* Are we 16KB aligned for an L1 ? */
510		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
511		    && kernel_l1pt.pv_pa == 0) {
512			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
513		} else {
514			valloc_pages(kernel_pt_table[loop1],
515			    L2_TABLE_SIZE / PAGE_SIZE);
516			++loop1;
517		}
518	}
519
520	/* This should never be able to happen but better confirm that. */
521	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
522		panic("initarm: Failed to align the kernel page directory");
523
524	/*
525	 * Allocate a page for the system page mapped to V0x00000000
526	 * This page will just contain the system vectors and can be
527	 * shared by all processes.
528	 */
529	alloc_pages(systempage.pv_pa, 1);
530
531	/* Allocate stacks for all modes */
532	valloc_pages(irqstack, IRQ_STACK_SIZE);
533	valloc_pages(abtstack, ABT_STACK_SIZE);
534	valloc_pages(undstack, UND_STACK_SIZE);
535	valloc_pages(kernelstack, UPAGES);
536
537	/* Allocate enough pages for cleaning the Mini-Data cache. */
538	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
539	valloc_pages(minidataclean, 1);
540
541#ifdef VERBOSE_INIT_ARM
542	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
543	    irqstack.pv_va);
544	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
545	    abtstack.pv_va);
546	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
547	    undstack.pv_va);
548	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
549	    kernelstack.pv_va);
550#endif
551
552	/*
553	 * XXX Defer this to later so that we can reclaim the memory
554	 * XXX used by the RedBoot page tables.
555	 */
556	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
557
558	/*
559	 * Ok we have allocated physical pages for the primary kernel
560	 * page tables
561	 */
562
563#ifdef VERBOSE_INIT_ARM
564	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
565#endif
566
567	/*
568	 * Now we start construction of the L1 page table
569	 * We start by mapping the L2 page tables into the L1.
570	 * This means that we can replace L1 mappings later on if necessary
571	 */
572	l1pagetable = kernel_l1pt.pv_pa;
573
574	/* Map the L2 pages tables in the L1 page table */
575	pmap_link_l2pt(l1pagetable, 0x00000000,
576	    &kernel_pt_table[KERNEL_PT_SYS]);
577	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
578		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
579		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
580	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
581		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
582		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
583
584	/* update the top of the kernel VM */
585	pmap_curmaxkvaddr =
586	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
587
588#ifdef VERBOSE_INIT_ARM
589	printf("Mapping kernel\n");
590#endif
591
592	/* Now we fill in the L2 pagetable for the kernel static code/data */
593	{
594		extern char etext[], _end[];
595		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
596		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
597		u_int logical;
598
599		textsize = (textsize + PGOFSET) & ~PGOFSET;
600		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
601
602		logical = 0x00200000;	/* offset of kernel in RAM */
603
604		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
605		    physical_start + logical, textsize,
606		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
607		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
608		    physical_start + logical, totalsize - textsize,
609		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
610	}
611
612#ifdef VERBOSE_INIT_ARM
613	printf("Constructing L2 page tables\n");
614#endif
615
616	/* Map the stack pages */
617	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
618	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
619	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
620	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
621	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
622	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
623	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
624	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
625
626	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
627	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
628
629	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
630		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
631		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
632		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
633	}
634
635	/* Map the Mini-Data cache clean area. */
636	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
637	    minidataclean.pv_pa);
638
639	/* Map the vector page. */
640#if 1
641	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
642	 * cache-clean code there.  */
643	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
644	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
645#else
646	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
647	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
648#endif
649
650	/*
651	 * map integrated peripherals at same address in l1pagetable
652	 * so that we can continue to use console.
653	 */
654	pmap_devmap_bootstrap(l1pagetable, viper_devmap);
655
656	/*
657	 * Give the XScale global cache clean code an appropriately
658	 * sized chunk of unmapped VA space starting at 0xff000000
659	 * (our device mappings end before this address).
660	 */
661	xscale_cache_clean_addr = 0xff000000U;
662
663	/*
664	 * Now we have the real page tables in place so we can switch to them.
665	 * Once this is done we will be running with the REAL kernel page
666	 * tables.
667	 */
668
669	/*
670	 * Update the physical_freestart/physical_freeend/free_pages
671	 * variables.
672	 */
673	{
674		extern char _end[];
675
676		physical_freestart = physical_start +
677		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
678		     KERNEL_BASE);
679		physical_freeend = physical_end;
680		free_pages =
681		    (physical_freeend - physical_freestart) / PAGE_SIZE;
682	}
683
684	/* Switch tables */
685#ifdef VERBOSE_INIT_ARM
686	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
687	       physical_freestart, free_pages, free_pages);
688	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
689#endif
690
691	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
692	cpu_setttb(kernel_l1pt.pv_pa, true);
693	cpu_tlb_flushID();
694	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
695
696	/*
697	 * Moved from cpu_startup() as data_abort_handler() references
698	 * this during uvm init
699	 */
700	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
701
702#ifdef VERBOSE_INIT_ARM
703	printf("bootstrap done.\n");
704#endif
705
706	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
707
708	/*
709	 * Pages were allocated during the secondary bootstrap for the
710	 * stacks for different CPU modes.
711	 * We must now set the r13 registers in the different CPU modes to
712	 * point to these stacks.
713	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
714	 * of the stack memory.
715	 */
716	printf("init subsystems: stacks ");
717
718	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
719	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
720	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
721
722	/*
723	 * Well we should set a data abort handler.
724	 * Once things get going this will change as we will need a proper
725	 * handler.
726	 * Until then we will use a handler that just panics but tells us
727	 * why.
728	 * Initialisation of the vectors will just panic on a data abort.
729	 * This just fills in a slightly better one.
730	 */
731	printf("vectors ");
732	data_abort_handler_address = (u_int)data_abort_handler;
733	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
734	undefined_handler_address = (u_int)undefinedinstruction_bounce;
735
736	/* Initialise the undefined instruction handlers */
737	printf("undefined ");
738	undefined_init();
739
740	/* Load memory into UVM. */
741	printf("page ");
742	uvm_md_init();
743	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
744	    atop(physical_freestart), atop(physical_freeend),
745	    VM_FREELIST_DEFAULT);
746
747	/* Boot strap pmap telling it where managed kernel virtual memory is */
748	printf("pmap ");
749	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
750
751#ifdef __HAVE_MEMORY_DISK__
752	md_root_setconf(memory_disk, sizeof memory_disk);
753#endif
754
755#ifdef KGDB
756	if (boothowto & RB_KDB) {
757		kgdb_debug_init = 1;
758		kgdb_connect(1);
759	}
760#endif
761
762#ifdef DDB
763	db_machine_init();
764
765	/* Firmware doesn't load symbols. */
766	ddb_init(0, NULL, NULL);
767
768	if (boothowto & RB_KDB)
769		Debugger();
770#endif
771
772	/* We return the new stack pointer address */
773	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
774}
775
776#if 0
777void
778process_kernel_args(char *args)
779{
780
781	boothowto = 0;
782
783	/* Make a local copy of the bootargs */
784	strncpy(bootargs, args, MAX_BOOT_STRING);
785
786	args = bootargs;
787	boot_file = bootargs;
788
789	/* Skip the kernel image filename */
790	while (*args != ' ' && *args != 0)
791		++args;
792
793	if (*args != 0)
794		*args++ = 0;
795
796	while (*args == ' ')
797		++args;
798
799	boot_args = args;
800
801	printf("bootfile: %s\n", boot_file);
802	printf("bootargs: %s\n", boot_args);
803
804	parse_mi_bootargs(boot_args);
805}
806#endif
807
808#ifdef KGDB
809#ifndef KGDB_DEVNAME
810#define KGDB_DEVNAME "ffuart"
811#endif
812const char kgdb_devname[] = KGDB_DEVNAME;
813
814#if (NCOM > 0)
815#ifndef KGDB_DEVMODE
816#define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
817#endif
818int comkgdbmode = KGDB_DEVMODE;
819#endif /* NCOM */
820
821#endif /* KGDB */
822
823
824void
825consinit(void)
826{
827	static int consinit_called = 0;
828	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
829#if 0
830	char *console = CONSDEVNAME;
831#endif
832
833	if (consinit_called != 0)
834		return;
835	consinit_called = 1;
836
837#if NCOM > 0
838
839#ifdef FFUARTCONSOLE
840#ifdef KGDB
841	if (0 == strcmp(kgdb_devname, "ffuart")) {
842		/* port is reserved for kgdb */
843	} else
844#endif
845	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
846		     comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
847
848#if 0
849		/* XXX: can't call pxa2x0_clkman_config yet */
850		pxa2x0_clkman_config(CKEN_FFUART, 1);
851#else
852		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN,
853		    ckenreg|CKEN_FFUART);
854#endif
855
856		return;
857	}
858
859#endif /* FFUARTCONSOLE */
860
861#ifdef BTUARTCONSOLE
862#ifdef KGDB
863	if (0 == strcmp(kgdb_devname, "btuart")) {
864		/* port is reserved for kgdb */
865	} else
866#endif
867	if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
868		comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
869		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN,
870		    ckenreg|CKEN_BTUART);
871		return;
872	}
873#endif /* BTUARTCONSOLE */
874
875	/* no console, guess we're flying blind */
876
877#endif /* NCOM */
878
879}
880
881#ifdef KGDB
882void
883kgdb_port_init(void)
884{
885#if (NCOM > 0) && defined(COM_PXA2X0)
886	paddr_t paddr = 0;
887	uint32_t ckenreg = ioreg_read(VIPER_CLKMAN_VBASE+CLKMAN_CKEN);
888
889	if (0 == strcmp(kgdb_devname, "ffuart")) {
890		paddr = PXA2X0_FFUART_BASE;
891		ckenreg |= CKEN_FFUART;
892	}
893	else if (0 == strcmp(kgdb_devname, "btuart")) {
894		paddr = PXA2X0_BTUART_BASE;
895		ckenreg |= CKEN_BTUART;
896	}
897
898	if (paddr &&
899	    0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
900		kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
901
902		ioreg_write(VIPER_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
903	}
904#endif
905}
906#endif
907