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