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