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