xref: /netbsd-src/sys/arch/evbarm/smdk2xx0/smdk2410_machdep.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: smdk2410_machdep.c,v 1.34 2014/02/27 22:57:22 joerg Exp $ */
2 
3 /*
4  * Copyright (c) 2002, 2003 Fujitsu Component Limited
5  * Copyright (c) 2002, 2003, 2005 Genetec Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The Fujitsu Component Limited nor the name of
17  *    Genetec corporation may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
21  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
25  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * Copyright (c) 2001,2002 ARM Ltd
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. The name of the company may not be used to endorse or promote
47  *    products derived from this software without specific prior written
48  *    permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ARM LTD
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  *
62  */
63 
64 /*
65  * Copyright (c) 1997,1998 Mark Brinicombe.
66  * Copyright (c) 1997,1998 Causality Limited.
67  * All rights reserved.
68  *
69  * Redistribution and use in source and binary forms, with or without
70  * modification, are permitted provided that the following conditions
71  * are met:
72  * 1. Redistributions of source code must retain the above copyright
73  *    notice, this list of conditions and the following disclaimer.
74  * 2. Redistributions in binary form must reproduce the above copyright
75  *    notice, this list of conditions and the following disclaimer in the
76  *    documentation and/or other materials provided with the distribution.
77  * 3. All advertising materials mentioning features or use of this software
78  *    must display the following acknowledgement:
79  *	This product includes software developed by Mark Brinicombe
80  *	for the NetBSD Project.
81  * 4. The name of the company nor the name of the author may be used to
82  *    endorse or promote products derived from this software without specific
83  *    prior written permission.
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
86  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
87  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
88  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
89  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
90  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
91  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95  * SUCH DAMAGE.
96  *
97  * Machine dependent functions for kernel setup for integrator board
98  *
99  * Created      : 24/11/97
100  */
101 
102 /*
103  * Machine dependent functions for kernel setup for Samsung SMDK2410
104  * derived from integrator_machdep.c
105  */
106 
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: smdk2410_machdep.c,v 1.34 2014/02/27 22:57:22 joerg Exp $");
109 
110 #include "opt_ddb.h"
111 #include "opt_kgdb.h"
112 #include "opt_pmap_debug.h"
113 #include "opt_md.h"
114 
115 #include <sys/param.h>
116 #include <sys/device.h>
117 #include <sys/systm.h>
118 #include <sys/kernel.h>
119 #include <sys/exec.h>
120 #include <sys/proc.h>
121 #include <sys/msgbuf.h>
122 #include <sys/reboot.h>
123 #include <sys/termios.h>
124 #include <sys/ksyms.h>
125 #include <sys/bus.h>
126 #include <sys/cpu.h>
127 #include <sys/intr.h>
128 
129 #include <uvm/uvm_extern.h>
130 
131 #include <dev/cons.h>
132 #include <dev/md.h>
133 
134 #include <machine/db_machdep.h>
135 #include <ddb/db_sym.h>
136 #include <ddb/db_extern.h>
137 #ifdef KGDB
138 #include <sys/kgdb.h>
139 #endif
140 
141 #include <machine/bootconfig.h>
142 #include <arm/locore.h>
143 #include <arm/undefined.h>
144 
145 #include <arm/arm32/machdep.h>
146 
147 #include <arm/s3c2xx0/s3c2410reg.h>
148 #include <arm/s3c2xx0/s3c2410var.h>
149 
150 #include "ksyms.h"
151 
152 #ifndef	SDRAM_START
153 #define	SDRAM_START	S3C2410_SDRAM_START
154 #endif
155 #ifndef	SDRAM_SIZE
156 #define	SDRAM_SIZE	(32*1024*1024)
157 #endif
158 
159 /*
160  * Address to map I/O registers in early initialize stage.
161  */
162 #define SMDK2410_IO_VBASE	0xfd000000
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 /* Memory disk support */
175 #if defined(MEMORY_DISK_DYNAMIC) && defined(MEMORY_DISK_ROOT_ADDR)
176 #define DO_MEMORY_DISK
177 /* We have memory disk image outside of the kernel on ROM. */
178 #ifdef MEMORY_DISK_ROOT_ROM
179 /* map the image directory and use read-only */
180 #else
181 /* copy the image to RAM */
182 #endif
183 #endif
184 
185 BootConfig bootconfig;		/* Boot config storage */
186 char *boot_args = NULL;
187 char *boot_file = NULL;
188 
189 vm_offset_t physical_start;
190 vm_offset_t physical_freestart;
191 vm_offset_t physical_freeend;
192 vm_offset_t physical_end;
193 u_int free_pages;
194 
195 /*int debug_flags;*/
196 #ifndef PMAP_STATIC_L1S
197 int max_processes = 64;		/* Default number */
198 #endif				/* !PMAP_STATIC_L1S */
199 
200 vm_offset_t msgbufphys;
201 
202 #ifdef PMAP_DEBUG
203 extern int pmap_debug_level;
204 #endif
205 
206 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
207 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
208 #define	KERNEL_PT_KERNEL_NUM	2	/* L2 tables for mapping kernel VM */
209 
210 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
211 
212 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
213 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
214 
215 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
216 
217 /* Prototypes */
218 
219 void consinit(void);
220 void kgdb_port_init(void);
221 
222 
223 #include "com.h"
224 #if NCOM > 0
225 #include <dev/ic/comreg.h>
226 #include <dev/ic/comvar.h>
227 #endif
228 
229 #include "sscom.h"
230 #if NSSCOM > 0
231 #include "opt_sscom.h"
232 #include <arm/s3c2xx0/sscom_var.h>
233 #endif
234 
235 /*
236  * Define the default console speed for the board.  This is generally
237  * what the firmware provided with the board defaults to.
238  */
239 #ifndef CONSPEED
240 #define CONSPEED B115200	/* TTYDEF_SPEED */
241 #endif
242 #ifndef CONMODE
243 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)   /* 8N1 */
244 #endif
245 
246 int comcnspeed = CONSPEED;
247 int comcnmode = CONMODE;
248 
249 
250 /*
251  * void cpu_reboot(int howto, char *bootstr)
252  *
253  * Reboots the system
254  *
255  * Deal with any syncing, unmounting, dumping and shutdown hooks,
256  * then reset the CPU.
257  */
258 void
259 cpu_reboot(int howto, char *bootstr)
260 {
261 #ifdef DIAGNOSTIC
262 	/* info */
263 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
264 #endif
265 
266 	cpu_reset_address_paddr = vtophys((u_int)s3c2410_softreset);
267 
268 	/*
269 	 * If we are still cold then hit the air brakes
270 	 * and crash to earth fast
271 	 */
272 	if (cold) {
273 		doshutdownhooks();
274 		pmf_system_shutdown(boothowto);
275 		printf("The operating system has halted.\n");
276 		printf("Please press any key to reboot.\n\n");
277 		cngetc();
278 		printf("rebooting...\n");
279 		cpu_reset();
280 		/* NOTREACHED */
281 	}
282 	/* Disable console buffering */
283 
284 	/*
285 	 * If RB_NOSYNC was not specified sync the discs.
286 	 * Note: Unless cold is set to 1 here, syslogd will die during the
287 	 * unmount.  It looks like syslogd is getting woken up only to find
288 	 * that it cannot page part of the binary in as the filesystem has
289 	 * been unmounted.
290 	 */
291 	if (!(howto & RB_NOSYNC))
292 		bootsync();
293 
294 	/* Say NO to interrupts */
295 	splhigh();
296 
297 	/* Do a dump if requested. */
298 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
299 		dumpsys();
300 
301 	/* Run any shutdown hooks */
302 	doshutdownhooks();
303 
304 	pmf_system_shutdown(boothowto);
305 
306 	/* Make sure IRQ's are disabled */
307 	IRQdisable;
308 
309 	if (howto & RB_HALT) {
310 		printf("The operating system has halted.\n");
311 		printf("Please press any key to reboot.\n\n");
312 		cngetc();
313 	}
314 	printf("rebooting...\n");
315 	cpu_reset();
316 	/* NOTREACHED */
317 }
318 
319 /*
320  * Static device mappings. These peripheral registers are mapped at
321  * fixed virtual addresses very early in initarm() so that we can use
322  * them while booting the kernel , and stay at the same address
323  * throughout whole kernel's life time.
324  *
325  * We use this table twice; once with bootstrap page table, and once
326  * with kernel's page table which we build up in initarm().
327  *
328  * Since we map these registers into the bootstrap page table using
329  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
330  * registers segment-aligned and segment-rounded in order to avoid
331  * using the 2nd page tables.
332  */
333 
334 #define	_A(a)	((a) & ~L1_S_OFFSET)
335 #define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
336 
337 #define	_V(n)	(SMDK2410_IO_VBASE + (n) * L1_S_SIZE)
338 
339 #define	GPIO_VBASE	_V(0)
340 #define	INTCTL_VBASE	_V(1)
341 #define	CLKMAN_VBASE	_V(2)
342 #define	UART_VBASE	_V(3)
343 #ifdef	MEMORY_DISK_DYNAMIC
344 #define	MEMORY_DISK_VADDR	_V(4)
345 #endif
346 
347 static const struct pmap_devmap smdk2410_devmap[] = {
348 	/* GPIO registers */
349 	{
350 		GPIO_VBASE,
351 		_A(S3C2410_GPIO_BASE),
352 		_S(S3C2410_GPIO_SIZE),
353 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
354 	},
355 	{
356 		INTCTL_VBASE,
357 		_A(S3C2410_INTCTL_BASE),
358 		_S(S3C2410_INTCTL_SIZE),
359 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
360 	},
361 	{
362 		CLKMAN_VBASE,
363 		_A(S3C2410_CLKMAN_BASE),
364 		_S(S3C24X0_CLKMAN_SIZE),
365 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
366 	},
367 	{	/* UART registers for UART0, 1, 2. */
368 		UART_VBASE,
369 		_A(S3C2410_UART0_BASE),
370 		_S(S3C2410_UART_BASE(3) - S3C2410_UART0_BASE),
371 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
372 	},
373 
374 	{ 0, 0, 0, 0 }
375 };
376 
377 #undef	_A
378 #undef	_S
379 
380 static inline	pd_entry_t *
381 read_ttb(void)
382 {
383 	long ttb;
384 
385 	__asm volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r"(ttb));
386 
387 
388 	return (pd_entry_t *)(ttb & ~((1 << 14) - 1));
389 }
390 
391 
392 #define	ioreg_read8(a)  	(*(volatile uint8_t *)(a))
393 #define	ioreg_write8(a,v)	(*(volatile uint8_t *)(a)=(v))
394 #define	ioreg_read32(a)  	(*(volatile uint32_t *)(a))
395 #define	ioreg_write32(a,v)  	(*(volatile uint32_t *)(a)=(v))
396 
397 /*
398  * u_int initarm(...)
399  *
400  * Initial entry point on startup. This gets called before main() is
401  * entered.
402  * It should be responsible for setting up everything that must be
403  * in place when main is called.
404  * This includes
405  *   Taking a copy of the boot configuration structure.
406  *   Initialising the physical console so characters can be printed.
407  *   Setting up page tables for the kernel
408  *   Relocating the kernel to the bottom of physical memory
409  */
410 
411 u_int
412 initarm(void *arg)
413 {
414 	int loop;
415 	int loop1;
416 	u_int l1pagetable;
417 	extern int etext __asm("_etext");
418 	extern int end __asm("_end");
419 	int progress_counter = 0;
420 
421 #ifdef DO_MEMORY_DISK
422 	vm_offset_t md_root_start;
423 #define MD_ROOT_SIZE (MEMORY_DISK_ROOT_SIZE * DEV_BSIZE)
424 #endif
425 
426 #define gpio_read8(reg) ioreg_read8(GPIO_VBASE + (reg))
427 
428 #define LEDSTEP()  __LED(progress_counter++)
429 
430 #define pdatf (*(volatile uint8_t *)(S3C2410_GPIO_BASE+GPIO_PFDAT))
431 #define __LED(x)  (pdatf = (pdatf & ~0xf0) | (~(x) & 0xf0))
432 
433 	LEDSTEP();
434 
435 	/* CS8900A on CS3 and CL-PD7610 need nBE1 signal. make sure
436 	 * memory controller is set correctly.  (USB download firmware
437 	 * doesn't do this right) Also, we use WAIT signal for them.
438 	 */
439 	ioreg_write32(S3C2410_MEMCTL_BASE + MEMCTL_BWSCON,
440 	    (BWSCON_ST|BWSCON_WS) << BWSCON_BANK_SHIFT(2) |
441 	    (BWSCON_ST|BWSCON_WS) << BWSCON_BANK_SHIFT(3) |
442 	    ioreg_read32(S3C2410_MEMCTL_BASE + MEMCTL_BWSCON));
443 	/* tweak access timing for CS8900A */
444 	ioreg_write32(S3C2410_MEMCTL_BASE + MEMCTL_BANKCON(3),
445 	    (0<<BANKCON_TACS_SHIFT)|(1<<BANKCON_TCOS_SHIFT)|
446 	    (7<<BANKCON_TACC_SHIFT)|(0<<BANKCON_TOCH_SHIFT)|
447 	    (0<<BANKCON_TCAH_SHIFT));
448 
449 	/*
450 	 * Heads up ... Setup the CPU / MMU / TLB functions
451 	 */
452 	if (set_cpufuncs())
453 		panic("cpu not recognized!");
454 
455 	LEDSTEP();
456 
457 	/*
458 	 * Map I/O registers that are used in startup.  Now we are
459 	 * still using page table prepared by bootloader.  Later we'll
460 	 * map those registers at the same address in the kernel page
461 	 * table.
462 	 */
463 	pmap_devmap_bootstrap((vaddr_t)read_ttb(), smdk2410_devmap);
464 
465 #undef	pdatf
466 #define pdatf (*(volatile uint8_t *)(GPIO_VBASE+GPIO_PFDAT))
467 
468 
469 	LEDSTEP();
470 
471 	/* Disable all peripheral interrupts */
472 	ioreg_write32(INTCTL_VBASE + INTCTL_INTMSK, ~0);
473 
474 	/* initialize some variables so that splfoo() doesn't
475 	   touch illegal address.  */
476 	s3c2xx0_intr_bootstrap(INTCTL_VBASE);
477 
478 	consinit();
479 #ifdef VERBOSE_INIT_ARM
480 	printf("consinit done\n");
481 #endif
482 
483 #ifdef KGDB
484 	LEDSTEP();
485 	kgdb_port_init();
486 #endif
487 	LEDSTEP();
488 
489 #ifdef VERBOSE_INIT_ARM
490 	/* Talk to the user */
491 	printf("\nNetBSD/evbarm (SMDK2410) booting ...\n");
492 #endif
493 	/*
494 	 * Ok we have the following memory map
495 	 *
496 	 * Physical Address Range     Description
497 	 * -----------------------    ----------------------------------
498 	 * 0x00000000 - 0x00ffffff    Intel flash Memory   (16MB)
499 	 * 0x02000000 - 0x020fffff    AMD flash Memory   (1MB)
500 	 * or 			       (depend on DIPSW setting)
501 	 * 0x00000000 - 0x000fffff    AMD flash Memory   (1MB)
502 	 * 0x02000000 - 0x02ffffff    Intel flash Memory   (16MB)
503 	 *
504 	 * 0x30000000 - 0x31ffffff    SDRAM (32MB)
505 	 *
506 	 * The initarm() has the responsibility for creating the kernel
507 	 * page tables.
508 	 * It must also set up various memory pointers that are used
509 	 * by pmap etc.
510 	 */
511 
512 	/* Fake bootconfig structure for the benefit of pmap.c */
513 	/* XXX must make the memory description h/w independent */
514 	bootconfig.dramblocks = 1;
515 	bootconfig.dram[0].address = SDRAM_START;
516 	bootconfig.dram[0].pages = SDRAM_SIZE / PAGE_SIZE;
517 
518 	/*
519 	 * Set up the variables that define the availablilty of
520 	 * physical memory.  For now, we're going to set
521 	 * physical_freestart to 0x08200000 (where the kernel
522 	 * was loaded), and allocate the memory we need downwards.
523 	 * If we get too close to the bottom of SDRAM, we
524 	 * will panic.  We will update physical_freestart and
525 	 * physical_freeend later to reflect what pmap_bootstrap()
526 	 * wants to see.
527 	 *
528 	 * XXX pmap_bootstrap() needs an enema.
529 	 */
530 	physical_start = bootconfig.dram[0].address;
531 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
532 
533 #ifdef DO_MEMORY_DISK
534 #ifdef MEMORY_DISK_ROOT_ROM
535 	md_root_start = MEMORY_DISK_ROOT_ADDR;
536 	boothowto |= RB_RDONLY;
537 #else
538 	/* Reserve physmem for ram disk */
539 	md_root_start = ((physical_end - MD_ROOT_SIZE) & ~(L1_S_SIZE-1));
540 	printf("Reserve %ld bytes for memory disk\n",
541 	    physical_end - md_root_start);
542 	/* copy fs contents */
543 	memcpy((void *)md_root_start, (void *)MEMORY_DISK_ROOT_ADDR,
544 	    MD_ROOT_SIZE);
545 	physical_end = md_root_start;
546 #endif
547 #endif
548 
549 	physical_freestart = SDRAM_START;	/* XXX */
550 	physical_freeend = SDRAM_START + 0x00200000;
551 
552 	physmem = (physical_end - physical_start) / PAGE_SIZE;
553 
554 #ifdef VERBOSE_INIT_ARM
555 	/* Tell the user about the memory */
556 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
557 	    physical_start, physical_end - 1);
558 #endif
559 
560 	/*
561 	 * XXX
562 	 * Okay, the kernel starts 2MB in from the bottom of physical
563 	 * memory.  We are going to allocate our bootstrap pages downwards
564 	 * from there.
565 	 *
566 	 * We need to allocate some fixed page tables to get the kernel
567 	 * going.  We allocate one page directory and a number of page
568 	 * tables and store the physical addresses in the kernel_pt_table
569 	 * array.
570 	 *
571 	 * The kernel page directory must be on a 16K boundary.  The page
572 	 * tables must be on 4K boundaries.  What we do is allocate the
573 	 * page directory on the first 16K boundary that we encounter, and
574 	 * the page tables on 4K boundaries otherwise.  Since we allocate
575 	 * at least 3 L2 page tables, we are guaranteed to encounter at
576 	 * least one 16K aligned region.
577 	 */
578 
579 #ifdef VERBOSE_INIT_ARM
580 	printf("Allocating page tables\n");
581 #endif
582 
583 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
584 
585 #ifdef VERBOSE_INIT_ARM
586 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
587 	    physical_freestart, free_pages, free_pages);
588 #endif
589 
590 	/* Define a macro to simplify memory allocation */
591 #define	valloc_pages(var, np)				\
592 	alloc_pages((var).pv_pa, (np));			\
593 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
594 
595 #define alloc_pages(var, np)				\
596 	physical_freeend -= ((np) * PAGE_SIZE);		\
597 	if (physical_freeend < physical_freestart)	\
598 		panic("initarm: out of memory");	\
599 	(var) = physical_freeend;			\
600 	free_pages -= (np);				\
601 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
602 
603 	loop1 = 0;
604 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
605 		/* Are we 16KB aligned for an L1 ? */
606 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
607 		    && kernel_l1pt.pv_pa == 0) {
608 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
609 		} else {
610 			valloc_pages(kernel_pt_table[loop1],
611 			    L2_TABLE_SIZE / PAGE_SIZE);
612 			++loop1;
613 		}
614 	}
615 
616 	/* This should never be able to happen but better confirm that. */
617 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
618 		panic("initarm: Failed to align the kernel page directory\n");
619 
620 	/*
621 	 * Allocate a page for the system page mapped to V0x00000000
622 	 * This page will just contain the system vectors and can be
623 	 * shared by all processes.
624 	 */
625 	alloc_pages(systempage.pv_pa, 1);
626 
627 	/* Allocate stacks for all modes */
628 	valloc_pages(irqstack, IRQ_STACK_SIZE);
629 	valloc_pages(abtstack, ABT_STACK_SIZE);
630 	valloc_pages(undstack, UND_STACK_SIZE);
631 	valloc_pages(kernelstack, UPAGES);
632 
633 #ifdef VERBOSE_INIT_ARM
634 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
635 	    irqstack.pv_va);
636 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
637 	    abtstack.pv_va);
638 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
639 	    undstack.pv_va);
640 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
641 	    kernelstack.pv_va);
642 #endif
643 
644 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
645 
646 	LEDSTEP();
647 
648 	/*
649 	 * Ok we have allocated physical pages for the primary kernel
650 	 * page tables
651 	 */
652 
653 #ifdef VERBOSE_INIT_ARM
654 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
655 #endif
656 
657 	/*
658 	 * Now we start construction of the L1 page table
659 	 * We start by mapping the L2 page tables into the L1.
660 	 * This means that we can replace L1 mappings later on if necessary
661 	 */
662 	l1pagetable = kernel_l1pt.pv_pa;
663 
664 	/* Map the L2 pages tables in the L1 page table */
665 	pmap_link_l2pt(l1pagetable, 0x00000000,
666 	    &kernel_pt_table[KERNEL_PT_SYS]);
667 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
668 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
669 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
670 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
671 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
672 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
673 
674 	/* update the top of the kernel VM */
675 	pmap_curmaxkvaddr =
676 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
677 
678 #ifdef VERBOSE_INIT_ARM
679 	printf("Mapping kernel\n");
680 #endif
681 
682 	/* Now we fill in the L2 pagetable for the kernel static code/data */
683 	{
684 		size_t textsize = (uintptr_t)&etext - KERNEL_TEXT_BASE;
685 		size_t totalsize = (uintptr_t)&end - KERNEL_TEXT_BASE;
686 		u_int logical;
687 
688 		textsize = (textsize + PGOFSET) & ~PGOFSET;
689 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
690 
691 		logical = 0x00200000;	/* offset of kernel in RAM */
692 
693 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
694 		    physical_start + logical, textsize,
695 		    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
696 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
697 		    physical_start + logical, totalsize - textsize,
698 		    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
699 	}
700 
701 #ifdef VERBOSE_INIT_ARM
702 	printf("Constructing L2 page tables\n");
703 #endif
704 
705 	/* Map the stack pages */
706 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
707 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
708 	    PTE_CACHE);
709 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
710 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
711 	    PTE_CACHE);
712 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
713 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE,
714 	    PTE_CACHE);
715 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
716 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
717 
718 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
719 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
720 
721 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
722 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
723 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
724 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
725 	}
726 
727 	/* Map the vector page. */
728 #if 1
729 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download the
730 	 * cache-clean code there.  */
731 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
732 	    VM_PROT_READ | VM_PROT_WRITE, PTE_NOCACHE);
733 #else
734 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
735 	    VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
736 #endif
737 
738 #ifdef MEMORY_DISK_DYNAMIC
739 	/* map MD root image */
740 	pmap_map_chunk(l1pagetable, MEMORY_DISK_VADDR, md_root_start,
741 	    MD_ROOT_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
742 
743 	md_root_setconf((void *)md_root_start, MD_ROOT_SIZE);
744 #endif /* MEMORY_DISK_DYNAMIC */
745 	/*
746 	 * map integrated peripherals at same address in l1pagetable
747 	 * so that we can continue to use console.
748 	 */
749 	pmap_devmap_bootstrap(l1pagetable, smdk2410_devmap);
750 
751 	/*
752 	 * Now we have the real page tables in place so we can switch to them.
753 	 * Once this is done we will be running with the REAL kernel page
754 	 * tables.
755 	 */
756 
757 	/*
758 	 * Update the physical_freestart/physical_freeend/free_pages
759 	 * variables.
760 	 */
761 	{
762 		physical_freestart = physical_start +
763 		    (((((uintptr_t)&end) + PGOFSET) & ~PGOFSET) - KERNEL_BASE);
764 		physical_freeend = physical_end;
765 		free_pages =
766 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
767 	}
768 
769 	/* Switch tables */
770 #ifdef VERBOSE_INIT_ARM
771 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
772 	    physical_freestart, free_pages, free_pages);
773 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
774 #endif
775 	LEDSTEP();
776 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
777 	cpu_setttb(kernel_l1pt.pv_pa, true);
778 	cpu_tlb_flushID();
779 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
780 
781 	/*
782 	 * Moved from cpu_startup() as data_abort_handler() references
783 	 * this during uvm init
784 	 */
785 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
786 
787 #ifdef VERBOSE_INIT_ARM
788 	printf("done!\n");
789 #endif
790 
791 	LEDSTEP();
792 #ifdef VERBOSE_INIT_ARM
793 	printf("bootstrap done.\n");
794 #endif
795 
796 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
797 
798 	/*
799 	 * Pages were allocated during the secondary bootstrap for the
800 	 * stacks for different CPU modes.
801 	 * We must now set the r13 registers in the different CPU modes to
802 	 * point to these stacks.
803 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
804 	 * of the stack memory.
805 	 */
806 #ifdef VERBOSE_INIT_ARM
807 	printf("init subsystems: stacks ");
808 #endif
809 
810 	set_stackptr(PSR_IRQ32_MODE,
811 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
812 	set_stackptr(PSR_ABT32_MODE,
813 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
814 	set_stackptr(PSR_UND32_MODE,
815 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
816 
817 	LEDSTEP();
818 
819 	/*
820 	 * Well we should set a data abort handler.
821 	 * Once things get going this will change as we will need a proper
822 	 * handler.
823 	 * Until then we will use a handler that just panics but tells us
824 	 * why.
825 	 * Initialisation of the vectors will just panic on a data abort.
826 	 * This just fills in a slightly better one.
827 	 */
828 #ifdef VERBOSE_INIT_ARM
829 	printf("vectors ");
830 #endif
831 	data_abort_handler_address = (u_int)data_abort_handler;
832 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
833 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
834 
835 	/* Initialise the undefined instruction handlers */
836 #ifdef VERBOSE_INIT_ARM
837 	printf("undefined ");
838 #endif
839 	undefined_init();
840 
841 	LEDSTEP();
842 
843 	/* Load memory into UVM. */
844 #ifdef VERBOSE_INIT_ARM
845 	printf("page ");
846 #endif
847 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
848 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
849 	    atop(physical_freestart), atop(physical_freeend),
850 	    VM_FREELIST_DEFAULT);
851 
852 	LEDSTEP();
853 	/* Boot strap pmap telling it where the kernel page table is */
854 #ifdef VERBOSE_INIT_ARM
855 	printf("pmap ");
856 #endif
857 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
858 
859 	LEDSTEP();
860 
861 	/* Setup the IRQ system */
862 #ifdef VERBOSE_INIT_ARM
863 	printf("irq ");
864 #endif
865 	/* XXX irq_init(); */
866 
867 #ifdef VERBOSE_INIT_ARM
868 	printf("done.\n");
869 #endif
870 
871 #ifdef BOOTHOWTO
872 	boothowto |= BOOTHOWTO;
873 #endif
874 	{
875 		uint8_t  gpio = ~gpio_read8(GPIO_PFDAT);
876 
877 		if (gpio & (1<<0)) /* SW1 (EINT0) */
878 			boothowto ^= RB_SINGLE;
879 		if (gpio & (1<<2)) /* SW2 (EINT2) */
880 			boothowto ^= RB_KDB;
881 #ifdef VERBOSE_INIT_ARM
882 		printf( "sw: %x boothowto: %x\n", gpio, boothowto );
883 #endif
884 	}
885 
886 #ifdef KGDB
887 	if (boothowto & RB_KDB) {
888 		kgdb_debug_init = 1;
889 		kgdb_connect(1);
890 	}
891 #endif
892 
893 #ifdef DDB
894 	db_machine_init();
895 	if (boothowto & RB_KDB)
896 		Debugger();
897 #endif
898 
899 	/* We return the new stack pointer address */
900 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
901 }
902 
903 void
904 consinit(void)
905 {
906 	static int consinit_done = 0;
907 	bus_space_tag_t iot = &s3c2xx0_bs_tag;
908 	int pclk;
909 
910 	if (consinit_done != 0)
911 		return;
912 
913 	consinit_done = 1;
914 
915 	s3c24x0_clock_freq2(CLKMAN_VBASE, NULL, NULL, &pclk);
916 
917 #if NSSCOM > 0
918 #ifdef SSCOM0CONSOLE
919 	if (0 == s3c2410_sscom_cnattach(iot, 0, comcnspeed,
920 		pclk, comcnmode))
921 		return;
922 #endif
923 #ifdef SSCOM1CONSOLE
924 	if (0 == s3c2410_sscom_cnattach(iot, 1, comcnspeed,
925 		pclk, comcnmode))
926 		return;
927 #endif
928 #endif				/* NSSCOM */
929 #if NCOM>0 && defined(CONCOMADDR)
930 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
931 		COM_FREQ, COM_TYPE_NORMAL, comcnmode))
932 		panic("can't init serial console @%x", CONCOMADDR);
933 	return;
934 #endif
935 
936 	consinit_done = 0;
937 }
938 
939 
940 #ifdef KGDB
941 
942 #if (NSSCOM > 0)
943 
944 #ifdef KGDB_DEVNAME
945 const char kgdb_devname[] = KGDB_DEVNAME;
946 #else
947 const char kgdb_devname[] = "";
948 #endif
949 
950 #ifndef KGDB_DEVMODE
951 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8) /* 8N1 */
952 #endif
953 int kgdb_sscom_mode = KGDB_DEVMODE;
954 
955 #endif				/* NSSCOM */
956 
957 void
958 kgdb_port_init(void)
959 {
960 #if (NSSCOM > 0)
961 	int unit = -1;
962 	int pclk;
963 
964 	if (strcmp(kgdb_devname, "sscom0") == 0)
965 		unit = 0;
966 	else if (strcmp(kgdb_devname, "sscom1") == 0)
967 		unit = 1;
968 
969 	if (unit >= 0) {
970 		s3c24x0_clock_freq2(CLKMAN_VBASE, NULL, NULL, &pclk);
971 
972 		s3c2410_sscom_kgdb_attach(&s3c2xx0_bs_tag,
973 		    unit, kgdb_rate, pclk, kgdb_sscom_mode);
974 	}
975 #endif
976 }
977 #endif
978 
979 static struct arm32_dma_range smdk2410_dma_ranges[1];
980 
981 bus_dma_tag_t
982 s3c2xx0_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
983 {
984 	extern paddr_t physical_start, physical_end;
985 	struct arm32_bus_dma_tag *dmat;
986 
987 	smdk2410_dma_ranges[0].dr_sysbase = physical_start;
988 	smdk2410_dma_ranges[0].dr_busbase = physical_start;
989 	smdk2410_dma_ranges[0].dr_len = physical_end - physical_start;
990 
991 #if 1
992 	dmat = dma_tag_template;
993 #else
994 	dmat = malloc(sizeof *dmat, M_DEVBUF, M_NOWAIT);
995 	if (dmat == NULL)
996 		return NULL;
997 	*dmat =  *dma_tag_template;
998 #endif
999 
1000 	dmat->_ranges = smdk2410_dma_ranges;
1001 	dmat->_nranges = 1;
1002 
1003 	return dmat;
1004 }
1005