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