xref: /netbsd/sys/arch/zaurus/zaurus/machdep.c (revision 6550d01e)
1 /*	$NetBSD: machdep.c,v 1.21 2010/11/28 08:23:25 hannken Exp $	*/
2 /*	$OpenBSD: zaurus_machdep.c,v 1.25 2006/06/20 18:24:04 todd Exp $	*/
3 
4 /*
5  * Copyright (c) 2002, 2003  Genetec Corporation.  All rights reserved.
6  * Written by Hiroyuki Bessho for Genetec Corporation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of Genetec Corporation may not be used to endorse or
17  *    promote products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Machine dependant functions for kernel setup for
33  * Intel DBPXA250 evaluation board (a.k.a. Lubbock).
34  * Based on iq80310_machhdep.c
35  */
36 
37 /*
38  * Copyright (c) 2001 Wasabi Systems, Inc.
39  * All rights reserved.
40  *
41  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed for the NetBSD Project by
54  *	Wasabi Systems, Inc.
55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
56  *    or promote products derived from this software without specific prior
57  *    written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69  * POSSIBILITY OF SUCH DAMAGE.
70  */
71 
72 /*
73  * Copyright (c) 1997,1998 Mark Brinicombe.
74  * Copyright (c) 1997,1998 Causality Limited.
75  * All rights reserved.
76  *
77  * Redistribution and use in source and binary forms, with or without
78  * modification, are permitted provided that the following conditions
79  * are met:
80  * 1. Redistributions of source code must retain the above copyright
81  *    notice, this list of conditions and the following disclaimer.
82  * 2. Redistributions in binary form must reproduce the above copyright
83  *    notice, this list of conditions and the following disclaimer in the
84  *    documentation and/or other materials provided with the distribution.
85  * 3. All advertising materials mentioning features or use of this software
86  *    must display the following acknowledgement:
87  *	This product includes software developed by Mark Brinicombe
88  *	for the NetBSD Project.
89  * 4. The name of the company nor the name of the author may be used to
90  *    endorse or promote products derived from this software without specific
91  *    prior written permission.
92  *
93  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
94  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
95  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
96  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
97  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
98  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
99  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103  * SUCH DAMAGE.
104  *
105  * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
106  * boards using RedBoot firmware.
107  */
108 
109 #include <sys/cdefs.h>
110 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.21 2010/11/28 08:23:25 hannken Exp $");
111 
112 #include "opt_ddb.h"
113 #include "opt_kgdb.h"
114 #include "opt_modular.h"
115 #include "opt_pmap_debug.h"
116 #include "opt_md.h"
117 #include "opt_com.h"
118 #include "ksyms.h"
119 
120 #include "opt_kloader.h"
121 #ifndef KLOADER_KERNEL_PATH
122 #define KLOADER_KERNEL_PATH	"/netbsd"
123 #endif
124 
125 #include <sys/param.h>
126 #include <sys/device.h>
127 #include <sys/systm.h>
128 #include <sys/kernel.h>
129 #include <sys/exec.h>
130 #include <sys/proc.h>
131 #include <sys/msgbuf.h>
132 #include <sys/reboot.h>
133 #include <sys/termios.h>
134 #include <sys/boot_flag.h>
135 
136 #include <uvm/uvm_extern.h>
137 
138 #include <dev/cons.h>
139 #include <sys/conf.h>
140 #include <sys/queue.h>
141 
142 #include <machine/db_machdep.h>
143 #include <ddb/db_sym.h>
144 #include <ddb/db_extern.h>
145 #ifdef KGDB
146 #include <sys/kgdb.h>
147 #endif
148 
149 #include <machine/bootconfig.h>
150 #include <machine/bootinfo.h>
151 #include <machine/bus.h>
152 #include <machine/cpu.h>
153 #include <machine/frame.h>
154 #ifdef KLOADER
155 #include <machine/kloader.h>
156 #endif
157 
158 #include <arm/undefined.h>
159 #include <arm/arm32/machdep.h>
160 
161 #include <arm/xscale/pxa2x0cpu.h>
162 #include <arm/xscale/pxa2x0reg.h>
163 #include <arm/xscale/pxa2x0var.h>
164 #include <arm/xscale/pxa2x0_gpio.h>
165 
166 #include <arch/zaurus/zaurus/zaurus_reg.h>
167 #include <arch/zaurus/zaurus/zaurus_var.h>
168 
169 #include <zaurus/dev/scoopreg.h>
170 
171 #include <dev/ic/comreg.h>
172 
173 #if 0	/* XXX */
174 #include "apm.h"
175 #endif	/* XXX */
176 #if NAPM > 0
177 #include <zaurus/dev/zapmvar.h>
178 #endif
179 
180 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
181 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
182 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x04000000)
183 
184 /*
185  * The range 0xc4000000 - 0xcfffffff is available for kernel VM space
186  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
187  */
188 #define KERNEL_VM_SIZE		0x0c000000
189 
190 /*
191  * Address to call from cpu_reset() to reset the machine.
192  * This is machine architecture dependant as it varies depending
193  * on where the ROM appears when you turn the MMU off.
194  */
195 u_int cpu_reset_address = 0;
196 
197 /* Define various stack sizes in pages */
198 #define IRQ_STACK_SIZE	1
199 #define ABT_STACK_SIZE	1
200 #define UND_STACK_SIZE	1
201 
202 int zaurusmod;			/* Zaurus model */
203 
204 BootConfig bootconfig;		/* Boot config storage */
205 char *boot_file = NULL;
206 char *boot_args = NULL;
207 
208 paddr_t physical_start;
209 paddr_t physical_freestart;
210 paddr_t physical_freeend;
211 paddr_t physical_end;
212 u_int free_pages;
213 
214 #ifndef PMAP_STATIC_L1S
215 int max_processes = 64;			/* Default number */
216 #endif	/* !PMAP_STATIC_L1S */
217 
218 /* Physical and virtual addresses for some global pages */
219 pv_addr_t irqstack;
220 pv_addr_t undstack;
221 pv_addr_t abtstack;
222 extern pv_addr_t kernelstack;
223 pv_addr_t minidataclean;
224 
225 paddr_t msgbufphys;
226 
227 extern u_int data_abort_handler_address;
228 extern u_int prefetch_abort_handler_address;
229 extern u_int undefined_handler_address;
230 
231 #ifdef PMAP_DEBUG
232 extern int pmap_debug_level;
233 #endif
234 
235 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
236 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
237 #define	KERNEL_PT_KERNEL_NUM	32
238 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
239 				        /* Page tables for mapping kernel VM */
240 #define	KERNEL_PT_VMDATA_NUM	8	/* start with 32MB of KVM */
241 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
242 
243 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
244 
245 const char *console = "glass";
246 int glass_console = 0;
247 
248 #ifdef KLOADER
249 pv_addr_t bootinfo_pt;
250 pv_addr_t bootinfo_pg;
251 struct kloader_bootinfo kbootinfo;
252 int kloader_howto = 0;
253 #else
254 struct bootinfo _bootinfo;
255 #endif
256 struct bootinfo *bootinfo;
257 struct btinfo_howto *bi_howto;
258 
259 #define	BOOTINFO_PAGE	(0xa0200000UL - PAGE_SIZE)
260 
261 /* Prototypes */
262 void	consinit(void);
263 void	dumpsys(void);
264 #ifdef KGDB
265 void	kgdb_port_init(void);
266 #endif
267 #ifdef KLOADER
268 static int parseboot(char *arg, char **filename, int *howto);
269 static char *gettrailer(char *arg);
270 static int parseopts(const char *opts, int *howto);
271 #endif
272 
273 #if defined(CPU_XSCALE_PXA250)
274 static struct pxa2x0_gpioconf pxa25x_boarddep_gpioconf[] = {
275 	{  34, GPIO_ALT_FN_1_IN },	/* FFRXD */
276 	{  35, GPIO_ALT_FN_1_IN },	/* FFCTS */
277 	{  39, GPIO_ALT_FN_2_OUT },	/* FFTXD */
278 	{  40, GPIO_ALT_FN_2_OUT },	/* FFDTR */
279 	{  41, GPIO_ALT_FN_2_OUT },	/* FFRTS */
280 
281 	{  44, GPIO_ALT_FN_1_IN },	/* BTCST */
282 	{  45, GPIO_ALT_FN_2_OUT },	/* BTRST */
283 
284 	{ -1 }
285 };
286 static struct pxa2x0_gpioconf *pxa25x_zaurus_gpioconf[] = {
287 	pxa25x_com_btuart_gpioconf,
288 	pxa25x_com_ffuart_gpioconf,
289 	pxa25x_com_stuart_gpioconf,
290 	pxa25x_boarddep_gpioconf,
291 	NULL
292 };
293 #else
294 static struct pxa2x0_gpioconf *pxa25x_zaurus_gpioconf[] = {
295 	NULL
296 };
297 #endif
298 #if defined(CPU_XSCALE_PXA270)
299 static struct pxa2x0_gpioconf pxa27x_boarddep_gpioconf[] = {
300 	{  34, GPIO_ALT_FN_1_IN },	/* FFRXD */
301 	{  35, GPIO_ALT_FN_1_IN },	/* FFCTS */
302 	{  39, GPIO_ALT_FN_2_OUT },	/* FFTXD */
303 	{  40, GPIO_ALT_FN_2_OUT },	/* FFDTR */
304 	{  41, GPIO_ALT_FN_2_OUT },	/* FFRTS */
305 
306 	{  44, GPIO_ALT_FN_1_IN },	/* BTCST */
307 	{  45, GPIO_ALT_FN_2_OUT },	/* BTRST */
308 
309 	{ 104, GPIO_ALT_FN_1_OUT },	/* pSKTSEL */
310 
311 	{ -1 }
312 };
313 static struct pxa2x0_gpioconf *pxa27x_zaurus_gpioconf[] = {
314 	pxa27x_com_btuart_gpioconf,
315 	pxa27x_com_ffuart_gpioconf,
316 	pxa27x_com_stuart_gpioconf,
317 	pxa27x_i2c_gpioconf,
318 	pxa27x_i2s_gpioconf,
319 	pxa27x_pxamci_gpioconf,
320 	pxa27x_boarddep_gpioconf,
321 	NULL
322 };
323 #else
324 static struct pxa2x0_gpioconf *pxa27x_zaurus_gpioconf[] = {
325 	NULL
326 };
327 #endif
328 
329 /*
330  * void cpu_reboot(int howto, char *bootstr)
331  *
332  * Reboots the system
333  *
334  * Deal with any syncing, unmounting, dumping and shutdown hooks,
335  * then reset the CPU.
336  */
337 void
338 cpu_reboot(int howto, char *bootstr)
339 {
340 	/*
341 	 * If we are still cold then hit the air brakes
342 	 * and crash to earth fast
343 	 */
344 	if (cold) {
345 		howto |= RB_HALT;
346 		goto haltsys;
347 	}
348 
349 	boothowto = howto;
350 
351 #ifdef KLOADER
352 	if ((howto & RB_HALT) == 0 && panicstr == NULL) {
353 		char *filename = NULL;
354 
355 		if ((howto & RB_STRING) && (bootstr != NULL)) {
356 			if (parseboot(bootstr, &filename, &kloader_howto) == 0){
357 				filename = NULL;
358 				kloader_howto = 0;
359 			}
360 		}
361 		if (kloader_howto != 0) {
362 			printf("howto: 0x%x\n", kloader_howto);
363 		}
364 		if (filename != NULL) {
365 			kloader_reboot_setup(filename);
366 		} else {
367 			kloader_reboot_setup(KLOADER_KERNEL_PATH);
368 		}
369 	}
370 #endif
371 
372 	/*
373 	 * If RB_NOSYNC was not specified sync the discs.
374 	 * Note: Unless cold is set to 1 here, syslogd will die during the
375 	 * unmount.  It looks like syslogd is getting woken up only to find
376 	 * that it cannot page part of the binary in as the filesystem has
377 	 * been unmounted.
378 	 */
379 	if (!(howto & RB_NOSYNC)) {
380 		bootsync();
381 		/*
382 		 * If we've been adjusting the clock, the todr
383 		 * will be out of synch; adjust it now.
384 		 */
385 		resettodr();
386 	}
387 
388 	/* Wait 3s */
389 	delay(3 * 1000 * 1000);
390 
391 	/* Say NO to interrupts */
392 	splhigh();
393 
394 	/* Do a dump if requested. */
395 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
396 		dumpsys();
397 
398 haltsys:
399 	/* Run any shutdown hooks */
400 	doshutdownhooks();
401 
402 	pmf_system_shutdown(boothowto);
403 
404 	/* Make sure IRQ's are disabled */
405 	IRQdisable;
406 
407 	if (howto & RB_HALT) {
408 #if NAPM > 0
409 		if (howto & RB_POWERDOWN) {
410 			printf("\nAttempting to power down...\n");
411 			zapm_poweroff();
412 		}
413 #endif
414 		printf("The operating system has halted.\n");
415 		printf("Please press any key to reboot.\n\n");
416 		cngetc();
417 	}
418 #ifdef KLOADER
419 	else if (panicstr == NULL) {
420 		delay(1 * 1000 * 1000);
421 		kloader_reboot();
422 		printf("\n");
423 		printf("Failed to load a new kernel.\n");
424 		printf("Please press any key to reboot.\n\n");
425 		cngetc();
426 	}
427 #endif
428 
429 	printf("rebooting...\n");
430 	delay(1 * 1000 * 1000);
431 	zaurus_restart();
432 
433 	printf("REBOOT FAILED!!!\n");
434 	for (;;)
435 		continue;
436 	/*NOTREACHED*/
437 }
438 
439 /*
440  * Do a GPIO reset, immediately causing the processor to begin the normal
441  * boot sequence.  See 2.7 Reset in the PXA27x Developer's Manual for the
442  * summary of effects of this kind of reset.
443  */
444 void
445 zaurus_restart(void)
446 {
447 	uint32_t rv;
448 
449 	rv = pxa2x0_memctl_read(MEMCTL_MSC0);
450 	if ((rv & 0xffff0000) == 0x7ff00000) {
451 		pxa2x0_memctl_write(MEMCTL_MSC0, (rv & 0xffff) | 0x7ee00000);
452 	}
453 
454 	/* External reset circuit presumably asserts nRESET_GPIO. */
455 	pxa2x0_gpio_set_function(89, GPIO_OUT | GPIO_SET);
456 	delay(1 * 1000* 1000);	/* wait 1s */
457 }
458 
459 static inline pd_entry_t *
460 read_ttb(void)
461 {
462 	u_long ttb;
463 
464 	__asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
465 
466 	return (pd_entry_t *)(ttb & ~((1 << 14) - 1));
467 }
468 
469 /*
470  * Static device mappings. These peripheral registers are mapped at
471  * fixed virtual addresses very early in initarm() so that we can use
472  * them while booting the kernel, and stay at the same address
473  * throughout whole kernel's life time.
474  *
475  * We use this table twice; once with bootstrap page table, and once
476  * with kernel's page table which we build up in initarm().
477  *
478  * Since we map these registers into the bootstrap page table using
479  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
480  * registers segment-aligned and segment-rounded in order to avoid
481  * using the 2nd page tables.
482  */
483 #define	_A(a)	((a) & ~L1_S_OFFSET)
484 #define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
485 
486 static const struct pmap_devmap zaurus_devmap[] = {
487     {
488 	    ZAURUS_GPIO_VBASE,
489 	    _A(PXA2X0_GPIO_BASE),
490 	    _S(PXA2X0_GPIO_SIZE),
491 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
492     },
493     {
494 	    ZAURUS_CLKMAN_VBASE,
495 	    _A(PXA2X0_CLKMAN_BASE),
496 	    _S(PXA2X0_CLKMAN_SIZE),
497 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
498     },
499     {
500 	    ZAURUS_INTCTL_VBASE,
501 	    _A(PXA2X0_INTCTL_BASE),
502 	    _S(PXA2X0_INTCTL_SIZE),
503 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
504     },
505     {
506 	    ZAURUS_MEMCTL_VBASE,
507 	    _A(PXA2X0_MEMCTL_BASE),
508 	    _S(PXA2X0_MEMCTL_SIZE),
509 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
510     },
511     {
512 	    ZAURUS_SCOOP0_VBASE,
513 	    _A(C3000_SCOOP0_BASE),
514 	    _S(SCOOP_SIZE),
515 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
516     },
517     {
518 	    ZAURUS_SCOOP1_VBASE,
519 	    _A(C3000_SCOOP1_BASE),
520 	    _S(SCOOP_SIZE),
521 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
522     },
523     {
524 	    ZAURUS_FFUART_VBASE,
525 	    _A(PXA2X0_FFUART_BASE),
526 	    _S(4 * COM_NPORTS),
527 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
528     },
529     {
530 	    ZAURUS_BTUART_VBASE,
531 	    _A(PXA2X0_BTUART_BASE),
532 	    _S(4 * COM_NPORTS),
533 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
534     },
535     {
536 	    ZAURUS_STUART_VBASE,
537 	    _A(PXA2X0_STUART_BASE),
538 	    _S(4 * COM_NPORTS),
539 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
540     },
541 
542     {0, 0, 0, 0, 0,}
543 };
544 
545 #undef	_A
546 #undef	_S
547 
548 void green_on(int virt);
549 void
550 green_on(int virt)
551 {
552 	/* clobber green led p */
553 	volatile uint16_t *p;
554 
555 	if (virt) {
556 		p = (volatile uint16_t *)(ZAURUS_SCOOP0_VBASE + SCOOP_GPWR);
557 	} else {
558 		p = (volatile uint16_t *)(C3000_SCOOP0_BASE + SCOOP_GPWR);
559 	}
560 
561 	*p |= (1 << SCOOP0_LED_GREEN);
562 }
563 
564 void irda_on(int virt);
565 void
566 irda_on(int virt)
567 {
568 	/* clobber IrDA led p */
569 	volatile uint16_t *p;
570 
571 	if (virt) {
572 		/* XXX scoop1 registers are not page-aligned! */
573 		int ofs = C3000_SCOOP1_BASE - trunc_page(C3000_SCOOP1_BASE);
574 		p = (volatile uint16_t *)(ZAURUS_SCOOP1_VBASE + ofs + SCOOP_GPWR);
575 	} else {
576 		p = (volatile uint16_t *)(C3000_SCOOP1_BASE + SCOOP_GPWR);
577 	}
578 
579 	*p &= ~(1 << SCOOP1_IR_ON);
580 }
581 
582 /*
583  * u_int initarm(...)
584  *
585  * Initial entry point on startup. This gets called before main() is
586  * entered.
587  * It should be responsible for setting up everything that must be
588  * in place when main is called.
589  * This includes
590  *   Taking a copy of the boot configuration structure.
591  *   Initialising the physical console so characters can be printed.
592  *   Setting up page tables for the kernel
593  *   Relocating the kernel to the bottom of physical memory
594  */
595 u_int
596 initarm(void *arg)
597 {
598 #ifdef DIAGNOSTIC
599 	extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
600 #endif
601 	extern vaddr_t xscale_cache_clean_addr;
602 	int loop;
603 	int loop1;
604 	u_int l1pagetable;
605 	paddr_t memstart;
606 	psize_t memsize;
607 	struct pxa2x0_gpioconf **zaurus_gpioconf;
608 	u_int *magicaddr;
609 
610 	/* Get ready for zaurus_restart() */
611 	pxa2x0_memctl_bootstrap(PXA2X0_MEMCTL_BASE);
612 
613 	/*
614 	 * Heads up ... Setup the CPU / MMU / TLB functions
615 	 */
616 	if (set_cpufuncs())
617 		panic("cpu not recognized!");
618 
619 	/* Get ready for splfoo() */
620 	pxa2x0_intr_bootstrap(PXA2X0_INTCTL_BASE);
621 
622 	/* map some peripheral registers at static I/O area */
623 	pmap_devmap_bootstrap((vaddr_t)read_ttb(), zaurus_devmap);
624 
625 	/* set new memctl register address so that zaurus_restart() doesn't
626 	   touch illegal address. */
627 	pxa2x0_memctl_bootstrap(ZAURUS_MEMCTL_VBASE);
628 
629 	/* set new intc register address so that splfoo() doesn't
630 	   touch illegal address.  */
631 	pxa2x0_intr_bootstrap(ZAURUS_INTCTL_VBASE);
632 
633 	/*
634 	 * Examine the boot args string for options we need to know about
635 	 * now.
636 	 */
637 	magicaddr = (void *)(0xa0200000 - BOOTARGS_BUFSIZ);
638 	if (*magicaddr == BOOTARGS_MAGIC) {
639 #ifdef KLOADER
640 		bootinfo = &kbootinfo.bootinfo;
641 #else
642 		bootinfo = &_bootinfo;
643 #endif
644 		memcpy(bootinfo,
645 		  (char *)0xa0200000 - BOOTINFO_MAXSIZE, BOOTINFO_MAXSIZE);
646 		bi_howto = lookup_bootinfo(BTINFO_HOWTO);
647 		boothowto = (bi_howto != NULL) ? bi_howto->howto : RB_AUTOBOOT;
648 	} else {
649 		boothowto = RB_AUTOBOOT;
650 	}
651 	*magicaddr = 0xdeadbeef;
652 #ifdef RAMDISK_HOOKS
653         boothowto |= RB_DFLTROOT;
654 #endif /* RAMDISK_HOOKS */
655 	if (boothowto & RB_MD1) {
656 		/* serial console */
657 		console = "ffuart";
658 	}
659 
660 	/*
661 	 * This test will work for now but has to be revised when support
662 	 * for other models is added.
663 	 */
664 	if ((cputype & ~CPU_ID_XSCALE_COREREV_MASK) == CPU_ID_PXA27X) {
665 		zaurusmod = ZAURUS_C3000;
666 		zaurus_gpioconf = pxa27x_zaurus_gpioconf;
667 	} else {
668 		zaurusmod = ZAURUS_C860;
669 		zaurus_gpioconf = pxa25x_zaurus_gpioconf;
670 	}
671 
672 	/* setup a serial console for very early boot */
673 	pxa2x0_gpio_bootstrap(ZAURUS_GPIO_VBASE);
674 	pxa2x0_gpio_config(zaurus_gpioconf);
675 	pxa2x0_clkman_bootstrap(ZAURUS_CLKMAN_VBASE);
676 	if (strcmp(console, "glass") != 0)
677 		consinit();
678 #ifdef KGDB
679 	kgdb_port_init();
680 #endif
681 
682 #ifdef VERBOSE_INIT_ARM
683 	/* Talk to the user */
684 	printf("\nNetBSD/zaurus booting ...\n");
685 #endif
686 
687 	{
688 		/* XXX - all Zaurus have this for now, fix memory sizing */
689 		memstart = 0xa0000000;
690 		memsize =  0x04000000; /* 64MB */
691 	}
692 
693 #ifdef KLOADER
694 	/* copy boot parameter for kloader */
695 	kloader_bootinfo_set(&kbootinfo, 0, NULL, NULL, true);
696 #endif
697 
698 #ifdef VERBOSE_INIT_ARM
699 	printf("initarm: Configuring system ...\n");
700 #endif
701 
702 	/* Fake bootconfig structure for the benefit of pmap.c */
703 	/* XXX must make the memory description h/w independant */
704 	bootconfig.dramblocks = 1;
705 	bootconfig.dram[0].address = memstart;
706 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
707 
708 	/*
709 	 * Set up the variables that define the availablilty of
710 	 * physical memory.  For now, we're going to set
711 	 * physical_freestart to 0xa0200000 (where the kernel
712 	 * was loaded), and allocate the memory we need downwards.
713 	 * If we get too close to the page tables that RedBoot
714 	 * set up, we will panic.  We will update physical_freestart
715 	 * and physical_freeend later to reflect what pmap_bootstrap()
716 	 * wants to see.
717 	 *
718 	 * XXX pmap_bootstrap() needs an enema.
719 	 */
720 	physical_start = bootconfig.dram[0].address;
721 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
722 
723 	physical_freestart = 0xa0009000UL;
724 	physical_freeend = BOOTINFO_PAGE;
725 
726 	physmem = (physical_end - physical_start) / PAGE_SIZE;
727 
728 #ifdef VERBOSE_INIT_ARM
729 	/* Tell the user about the memory */
730 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
731 	    physical_start, physical_end - 1);
732 #endif
733 
734 	/*
735 	 * Okay, the kernel starts 2MB in from the bottom of physical
736 	 * memory.  We are going to allocate our bootstrap pages downwards
737 	 * from there.
738 	 *
739 	 * We need to allocate some fixed page tables to get the kernel
740 	 * going.  We allocate one page directory and a number of page
741 	 * tables and store the physical addresses in the kernel_pt_table
742 	 * array.
743 	 *
744 	 * The kernel page directory must be on a 16K boundary.  The page
745 	 * tables must be on 4K bounaries.  What we do is allocate the
746 	 * page directory on the first 16K boundary that we encounter, and
747 	 * the page tables on 4K boundaries otherwise.  Since we allocate
748 	 * at least 3 L2 page tables, we are guaranteed to encounter at
749 	 * least one 16K aligned region.
750 	 */
751 
752 #ifdef VERBOSE_INIT_ARM
753 	printf("Allocating page tables\n");
754 #endif
755 
756 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
757 
758 #ifdef VERBOSE_INIT_ARM
759 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
760 	       physical_freestart, free_pages, free_pages);
761 #endif
762 
763 	/* Define a macro to simplify memory allocation */
764 #define	valloc_pages(var, np)				\
765 	alloc_pages((var).pv_pa, (np));			\
766 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
767 
768 #define alloc_pages(var, np)				\
769 	physical_freeend -= ((np) * PAGE_SIZE);		\
770 	if (physical_freeend < physical_freestart)	\
771 		panic("initarm: out of memory");	\
772 	(var) = physical_freeend;			\
773 	free_pages -= (np);				\
774 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
775 
776 	loop1 = 0;
777 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
778 		/* Are we 16KB aligned for an L1 ? */
779 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
780 		    && kernel_l1pt.pv_pa == 0) {
781 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
782 		} else {
783 			valloc_pages(kernel_pt_table[loop1],
784 			    L2_TABLE_SIZE / PAGE_SIZE);
785 			++loop1;
786 		}
787 	}
788 #ifdef KLOADER
789 	valloc_pages(bootinfo_pt, L2_TABLE_SIZE / PAGE_SIZE);
790 #endif
791 
792 	/* This should never be able to happen but better confirm that. */
793 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
794 		panic("initarm: Failed to align the kernel page directory");
795 
796 	/*
797 	 * Allocate a page for the system page mapped to V0x00000000
798 	 * This page will just contain the system vectors and can be
799 	 * shared by all processes.
800 	 */
801 	alloc_pages(systempage.pv_pa, 1);
802 
803 	/* Allocate stacks for all modes */
804 	valloc_pages(irqstack, IRQ_STACK_SIZE);
805 	valloc_pages(abtstack, ABT_STACK_SIZE);
806 	valloc_pages(undstack, UND_STACK_SIZE);
807 	valloc_pages(kernelstack, UPAGES);
808 
809 	/* Allocate enough pages for cleaning the Mini-Data cache. */
810 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
811 	valloc_pages(minidataclean, 1);
812 
813 #ifdef KLOADER
814 	bootinfo_pg.pv_pa = BOOTINFO_PAGE;
815 	bootinfo_pg.pv_va = KERNEL_BASE + bootinfo_pg.pv_pa - physical_start;
816 #endif
817 
818 #ifdef VERBOSE_INIT_ARM
819 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
820 	    irqstack.pv_va);
821 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
822 	    abtstack.pv_va);
823 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
824 	    undstack.pv_va);
825 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
826 	    kernelstack.pv_va);
827 	printf("minidataclean: p0x%08lx v0x%08lx, size = %ld\n",
828 	    minidataclean.pv_pa, minidataclean.pv_va,
829 	    xscale_minidata_clean_size);
830 #ifdef KLOADER
831 	printf("bootinfo_pg: p0x%08lx v0x%08lx\n", bootinfo_pg.pv_pa,
832 	    bootinfo_pg.pv_va);
833 #endif
834 #endif
835 
836 	/*
837 	 * XXX Defer this to later so that we can reclaim the memory
838 	 * XXX used by the RedBoot page tables.
839 	 */
840 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
841 
842 	/*
843 	 * Ok we have allocated physical pages for the primary kernel
844 	 * page tables
845 	 */
846 
847 #ifdef VERBOSE_INIT_ARM
848 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
849 #endif
850 
851 	/*
852 	 * Now we start construction of the L1 page table
853 	 * We start by mapping the L2 page tables into the L1.
854 	 * This means that we can replace L1 mappings later on if necessary
855 	 */
856 	l1pagetable = kernel_l1pt.pv_pa;
857 
858 	/* Map the L2 pages tables in the L1 page table */
859 	pmap_link_l2pt(l1pagetable, 0x00000000,
860 	    &kernel_pt_table[KERNEL_PT_SYS]);
861 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
862 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
863 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
864 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
865 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
866 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
867 #ifdef KLOADER
868 	pmap_link_l2pt(l1pagetable, 0xa0000000, &bootinfo_pt);
869 #endif
870 
871 	/* update the top of the kernel VM */
872 	pmap_curmaxkvaddr =
873 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
874 
875 #ifdef VERBOSE_INIT_ARM
876 	printf("Mapping kernel\n");
877 #endif
878 
879 	/* Now we fill in the L2 pagetable for the kernel static code/data
880 	 * and the symbol table. */
881 	{
882 		extern char etext[], _end[];
883 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
884 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
885 		u_int logical;
886 
887 		textsize = (textsize + PGOFSET) & ~PGOFSET;
888 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
889 
890 		logical = 0x00200000;	/* offset of kernel in RAM */
891 
892 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
893 		    physical_start + logical, textsize,
894 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
895 		pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
896 		    physical_start + logical, totalsize - textsize,
897 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
898 	}
899 
900 #ifdef VERBOSE_INIT_ARM
901 	printf("Constructing L2 page tables\n");
902 #endif
903 
904 	/* Map the stack pages */
905 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
906 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
907 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
908 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
909 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
910 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
911 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
912 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
913 
914 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
915 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
916 
917 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
918 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
919 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
920 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
921 	}
922 
923 #ifdef KLOADER
924 	pmap_map_chunk(l1pagetable, bootinfo_pt.pv_va, bootinfo_pt.pv_pa,
925 	    L2_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
926 	pmap_map_chunk(l1pagetable, bootinfo_pg.pv_va, bootinfo_pg.pv_pa,
927 	    PAGE_SIZE, VM_PROT_ALL, PTE_CACHE);
928 #endif
929 
930 	/* Map the Mini-Data cache clean area. */
931 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
932 	    minidataclean.pv_pa);
933 
934 	/* Map the vector page. */
935 #if 0
936 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
937 	 * cache-clean code there.  */
938 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
939 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
940 #else
941 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
942 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
943 #endif
944 
945 	/*
946 	 * map integrated peripherals at same address in l1pagetable
947 	 * so that we can continue to use console.
948 	 */
949 	pmap_devmap_bootstrap(l1pagetable, zaurus_devmap);
950 
951 	/*
952 	 * Give the XScale global cache clean code an appropriately
953 	 * sized chunk of unmapped VA space starting at 0xff000000
954 	 * (our device mappings end before this address).
955 	 */
956 	xscale_cache_clean_addr = 0xff000000U;
957 
958 	/*
959 	 * Now we have the real page tables in place so we can switch to them.
960 	 * Once this is done we will be running with the REAL kernel page
961 	 * tables.
962 	 */
963 
964 	/*
965 	 * Update the physical_freestart/physical_freeend/free_pages
966 	 * variables.
967 	 */
968 	{
969 		extern char _end[];
970 
971 		physical_freestart = physical_start +
972 		    ((((uintptr_t) _end + PGOFSET) & ~PGOFSET) - KERNEL_BASE);
973 		physical_freeend = physical_end;
974 		free_pages =
975 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
976 	}
977 
978 	/* Switch tables */
979 #ifdef VERBOSE_INIT_ARM
980 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
981 	       physical_freestart, free_pages, free_pages);
982 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
983 #endif
984 
985 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
986 	cpu_setttb(kernel_l1pt.pv_pa);
987 	cpu_tlb_flushID();
988 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
989 
990 	/*
991 	 * Moved from cpu_startup() as data_abort_handler() references
992 	 * this during uvm init
993 	 */
994 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
995 
996 #ifdef VERBOSE_INIT_ARM
997 	printf("bootstrap done.\n");
998 #endif
999 
1000 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
1001 
1002 	/*
1003 	 * Pages were allocated during the secondary bootstrap for the
1004 	 * stacks for different CPU modes.
1005 	 * We must now set the r13 registers in the different CPU modes to
1006 	 * point to these stacks.
1007 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1008 	 * of the stack memory.
1009 	 */
1010 #ifdef VERBOSE_INIT_ARM
1011 	printf("init subsystems: stacks ");
1012 #endif
1013 
1014 	set_stackptr(PSR_IRQ32_MODE,
1015 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
1016 	set_stackptr(PSR_ABT32_MODE,
1017 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
1018 	set_stackptr(PSR_UND32_MODE,
1019 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
1020 
1021 	/*
1022 	 * Well we should set a data abort handler.
1023 	 * Once things get going this will change as we will need a proper
1024 	 * handler.
1025 	 * Until then we will use a handler that just panics but tells us
1026 	 * why.
1027 	 * Initialisation of the vectors will just panic on a data abort.
1028 	 * This just fills in a slighly better one.
1029 	 */
1030 #ifdef VERBOSE_INIT_ARM
1031 	printf("vectors ");
1032 #endif
1033 	data_abort_handler_address = (u_int)data_abort_handler;
1034 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
1035 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
1036 
1037 	/* Initialise the undefined instruction handlers */
1038 #ifdef VERBOSE_INIT_ARM
1039 	printf("undefined ");
1040 #endif
1041 	undefined_init();
1042 
1043 	/* Load memory into UVM. */
1044 #ifdef VERBOSE_INIT_ARM
1045 	printf("page ");
1046 #endif
1047 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
1048 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
1049 	    atop(physical_freestart), atop(physical_freeend),
1050 	    VM_FREELIST_DEFAULT);
1051 
1052 	/* Boot strap pmap telling it where the kernel page table is */
1053 #ifdef VERBOSE_INIT_ARM
1054 	printf("pmap ");
1055 #endif
1056 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
1057 
1058 #ifdef VERBOSE_INIT_ARM
1059 	printf("\n");
1060 #endif
1061 
1062 #ifdef __HAVE_MEMORY_DISK__
1063 	md_root_setconf(memory_disk, sizeof memory_disk);
1064 #endif
1065 
1066 #if NKSYMS || defined(DDB) || defined(MODULAR)
1067 	/* Firmware doesn't load symbols. */
1068 	ddb_init(0, NULL, NULL);
1069 #endif
1070 
1071 #ifdef KGDB
1072 	if (boothowto & RB_KDB) {
1073 		kgdb_debug_init = 1;
1074 		kgdb_connect(1);
1075 	}
1076 #endif
1077 
1078 #ifdef DDB
1079 	db_machine_init();
1080 	if (boothowto & RB_KDB)
1081 		Debugger();
1082 #endif
1083 
1084 	/* We return the new stack pointer address */
1085 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
1086 }
1087 
1088 void *
1089 lookup_bootinfo(int type)
1090 {
1091 	struct btinfo_common *help;
1092 	int n;
1093 
1094 	if (bootinfo == NULL)
1095 		return (NULL);
1096 
1097 	n = bootinfo->nentries;
1098 	help = (struct btinfo_common *)(bootinfo->info);
1099 	while (n--) {
1100 		if (help->type == type)
1101 			return (help);
1102 		help = (struct btinfo_common *)((char *)help + help->len);
1103 	}
1104 	return (NULL);
1105 }
1106 
1107 #ifdef KLOADER
1108 static int
1109 parseboot(char *arg, char **filename, int *howto)
1110 {
1111 	char *opts = NULL;
1112 
1113 	*filename = NULL;
1114 	*howto = 0;
1115 
1116 	/* if there were no arguments */
1117 	if (arg == NULL || *arg == '\0')
1118 		return 1;
1119 
1120 	/* format is... */
1121 	/* [[xxNx:]filename] [-adqsv] */
1122 
1123 	/* check for just args */
1124 	if (arg[0] == '-') {
1125 		opts = arg;
1126 	} else {
1127 		/* there's a file name */
1128 		*filename = arg;
1129 
1130 		opts = gettrailer(arg);
1131 		if (opts == NULL || *opts == '\0') {
1132 			opts = NULL;
1133 		} else if (*opts != '-') {
1134 			printf("invalid arguments\n");
1135 			return 0;
1136 		}
1137 	}
1138 
1139 	/* at this point, we have dealt with filenames. */
1140 
1141 	/* now, deal with options */
1142 	if (opts) {
1143 		if (parseopts(opts, howto) == 0) {
1144 			return 0;
1145 		}
1146 	}
1147 	return 1;
1148 }
1149 
1150 static char *
1151 gettrailer(char *arg)
1152 {
1153 	static char nullstr[] = "";
1154 	char *options;
1155 
1156 	if ((options = strchr(arg, ' ')) == NULL)
1157 		return nullstr;
1158 	else
1159 		*options++ = '\0';
1160 
1161 	/* trim leading blanks */
1162 	while (*options && *options == ' ')
1163 		options++;
1164 
1165 	return options;
1166 }
1167 
1168 static int
1169 parseopts(const char *opts, int *howto)
1170 {
1171 	int r, tmpopt = *howto;
1172 
1173 	opts++; 	/* skip - */
1174 	while (*opts && *opts != ' ') {
1175 		r = 0;
1176 		BOOT_FLAG(*opts, r);
1177 		if (r == 0) {
1178 			printf("-%c: unknown flag\n", *opts);
1179 			return 0;
1180 		}
1181 		tmpopt |= r;
1182 		opts++;
1183 	}
1184 
1185 	*howto = tmpopt;
1186 	return 1;
1187 }
1188 #endif
1189 
1190 /*
1191  * Console
1192  */
1193 #include "com.h"
1194 #if (NCOM > 0)
1195 #include <dev/ic/comvar.h>
1196 #endif
1197 
1198 #include "lcd.h"
1199 #include "wsdisplay.h"
1200 
1201 #ifndef CONSPEED
1202 #define CONSPEED B9600
1203 #endif
1204 #ifndef CONMODE
1205 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
1206 #endif
1207 
1208 int comcnspeed = CONSPEED;
1209 int comcnmode = CONMODE;
1210 
1211 #ifdef KGDB
1212 #ifndef KGDB_DEVNAME
1213 #define KGDB_DEVNAME	"ffuart"
1214 #endif
1215 const char kgdb_devname[] = KGDB_DEVNAME;
1216 
1217 #if (NCOM > 0)
1218 #ifndef KGDB_DEVMODE
1219 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
1220 #endif
1221 int comkgdbmode = KGDB_DEVMODE;
1222 #endif /* NCOM */
1223 #endif /* KGDB */
1224 
1225 void
1226 consinit(void)
1227 {
1228 	static int consinit_called = 0;
1229 #if (NCOM > 0) && defined(COM_PXA2X0)
1230 	paddr_t paddr;
1231 	u_int cken = 0;
1232 #endif
1233 
1234 	if (consinit_called)
1235 		return;
1236 	consinit_called = 1;
1237 
1238 #if (NCOM > 0) && defined(COM_PXA2X0)
1239 #ifdef KGDB
1240 	if (strcmp(kgdb_devname, console) == 0) {
1241 		/* port is reserved for kgdb */
1242 	} else
1243 #endif
1244 	if (strcmp(console, "ffuart") == 0) {
1245 		paddr = PXA2X0_FFUART_BASE;
1246 		cken = CKEN_FFUART;
1247 	} else if (strcmp(console, "btuart") == 0) {
1248 		paddr = PXA2X0_BTUART_BASE;
1249 		cken = CKEN_BTUART;
1250 	} else if (strcmp(console, "stuart") == 0) {
1251 		paddr = PXA2X0_STUART_BASE;
1252 		cken = CKEN_STUART;
1253 		irda_on(0);
1254 	} else
1255 #endif
1256 	if (strcmp(console, "glass") == 0) {
1257 #if (NLCD > 0) && (NWSDISPLAY > 0)
1258 		extern void lcd_cnattach(void);
1259 
1260 		glass_console = 1;
1261 		lcd_cnattach();
1262 #endif
1263 	}
1264 
1265 #if (NCOM > 0) && defined(COM_PXA2X0)
1266 	if (cken != 0 && comcnattach(&pxa2x0_a4x_bs_tag, paddr, comcnspeed,
1267 	    PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode) == 0) {
1268 		pxa2x0_clkman_config(cken, 1);
1269 	}
1270 #endif
1271 }
1272 
1273 #ifdef KGDB
1274 void
1275 kgdb_port_init(void)
1276 {
1277 #if (NCOM > 0) && defined(COM_PXA2X0)
1278 	paddr_t paddr;
1279 	u_int cken;
1280 
1281 	if (strcmp(kgdb_devname, "ffuart") == 0) {
1282 		paddr = PXA2X0_FFUART_BASE;
1283 		cken = CKEN_FFUART;
1284 	} else if (strcmp(kgdb_devname, "btuart") == 0) {
1285 		paddr = PXA2X0_BTUART_BASE;
1286 		cken = CKEN_BTUART;
1287 	} else if (strcmp(kgdb_devname, "stuart") == 0) {
1288 		paddr = PXA2X0_STUART_BASE;
1289 		cken = CKEN_STUART;
1290 		irda_on(0);
1291 	} else
1292 		return;
1293 
1294 	if (com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
1295 	    kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode) == 0) {
1296 		pxa2x0_clkman_config(cken, 1);
1297 	}
1298 #endif
1299 }
1300 #endif
1301