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