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