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