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