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