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