iyonix_machdep.c revision 1.3 1 /* $NetBSD: iyonix_machdep.c,v 1.3 2020/04/18 10:55:45 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Based on code written by Jason R. Thorpe and Steve C. Woodford for
8 * Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1997,1998 Mark Brinicombe.
41 * Copyright (c) 1997,1998 Causality Limited.
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Mark Brinicombe
55 * for the NetBSD Project.
56 * 4. The name of the company nor the name of the author may be used to
57 * endorse or promote products derived from this software without specific
58 * prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * Machine dependent functions for kernel setup for Iyonix.
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: iyonix_machdep.c,v 1.3 2020/04/18 10:55:45 skrll Exp $");
77
78 #include "opt_ddb.h"
79 #include "opt_kgdb.h"
80 #include "opt_pmap_debug.h"
81
82 #include <sys/param.h>
83 #include <sys/device.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/exec.h>
87 #include <sys/proc.h>
88 #include <sys/msgbuf.h>
89 #include <sys/reboot.h>
90 #include <sys/termios.h>
91 #include <sys/ksyms.h>
92 #include <sys/bus.h>
93 #include <sys/cpu.h>
94
95 #include <uvm/uvm_extern.h>
96
97 #include <dev/cons.h>
98
99 #include <dev/pci/ppbreg.h>
100 #include <dev/ic/i8259reg.h>
101
102 #include <net/if.h>
103 #include <net/if_ether.h>
104
105 #include <machine/db_machdep.h>
106 #include <ddb/db_sym.h>
107 #include <ddb/db_extern.h>
108
109 #include <acorn32/include/bootconfig.h>
110 #include <arm/locore.h>
111 #include <arm/undefined.h>
112
113 #include <arm/arm32/machdep.h>
114
115 #include <arm/xscale/i80321reg.h>
116 #include <arm/xscale/i80321var.h>
117
118 #include <evbarm/iyonix/iyonixreg.h>
119 #include <evbarm/iyonix/obiovar.h>
120
121 #include <dev/wscons/wsconsio.h>
122 #include <dev/wscons/wsdisplayvar.h>
123 #include <dev/rasops/rasops.h>
124 #include <dev/wscons/wsdisplay_vconsvar.h>
125 #include <dev/wsfont/wsfont.h>
126
127 #include "ksyms.h"
128
129 #define KERNEL_TEXT_BASE KERNEL_BASE
130 #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
131
132 struct vcons_screen rascons_console_screen;
133
134 struct wsscreen_descr rascons_stdscreen = {
135 "std",
136 0, 0, /* will be filled in -- XXX shouldn't, it's global */
137 0,
138 0, 0,
139 WSSCREEN_REVERSE
140 };
141
142 /*
143 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
144 * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
145 */
146 #define KERNEL_VM_SIZE 0x0C000000
147
148 struct bootconfig bootconfig; /* Boot config storage */
149
150 char *boot_args;
151
152 vaddr_t physical_start;
153 vaddr_t physical_freestart;
154 vaddr_t physical_freeend;
155 vaddr_t physical_end;
156 u_int free_pages;
157 vaddr_t pagetables_start;
158
159 /*int debug_flags;*/
160 #ifndef PMAP_STATIC_L1S
161 int max_processes = 64; /* Default number */
162 #endif /* !PMAP_STATIC_L1S */
163
164 /* Physical and virtual addresses for some global pages */
165 pv_addr_t minidataclean;
166
167 paddr_t msgbufphys;
168
169 #ifdef PMAP_DEBUG
170 extern int pmap_debug_level;
171 #endif
172
173 #define KERNEL_PT_SYS 0 /* L2 table for mapping zero page */
174
175 #define KERNEL_PT_KERNEL 1 /* L2 table for mapping kernel */
176 #define KERNEL_PT_KERNEL_NUM 4
177
178 /* L2 table for mapping i80321 */
179 #define KERNEL_PT_IOPXS (KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
180
181 /* L2 tables for mapping kernel VM */
182 #define KERNEL_PT_VMDATA (KERNEL_PT_IOPXS + 1)
183 #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
184 #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
185
186 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
187
188 char iyonix_macaddr[ETHER_ADDR_LEN];
189
190 char boot_consdev[16];
191
192 /* Prototypes */
193
194 void iyonix_pic_init(void);
195 void iyonix_read_machineid(void);
196
197 void consinit(void);
198
199 static void consinit_com(const char *consdev);
200 static void consinit_genfb(const char *consdev);
201 static void process_kernel_args(void);
202 static void parse_iyonix_bootargs(char *args);
203
204 #include "com.h"
205 #if NCOM > 0
206 #include <dev/ic/comreg.h>
207 #include <dev/ic/comvar.h>
208 #endif
209
210 #include "genfb.h"
211
212 #if (NGENFB == 0) && (NCOM == 0)
213 # error "No valid console device (com or genfb)"
214 #elif defined(COMCONSOLE) || (NGENFB == 0)
215 # define DEFAULT_CONSDEV "com"
216 #else
217 # define DEFAULT_CONSDEV "genfb"
218 #endif
219
220 /*
221 * Define the default console speed for the machine.
222 */
223 #ifndef CONSPEED
224 #define CONSPEED B9600
225 #endif /* ! CONSPEED */
226
227 #ifndef CONUNIT
228 #define CONUNIT 0
229 #endif
230
231 #ifndef CONMODE
232 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
233 #endif
234
235 int comcnspeed = CONSPEED;
236 int comcnmode = CONMODE;
237 int comcnunit = CONUNIT;
238
239 #if KGDB
240 #ifndef KGDB_DEVNAME
241 #error Must define KGDB_DEVNAME
242 #endif
243 const char kgdb_devname[] = KGDB_DEVNAME;
244
245 #ifndef KGDB_DEVADDR
246 #error Must define KGDB_DEVADDR
247 #endif
248 unsigned long kgdb_devaddr = KGDB_DEVADDR;
249
250 #ifndef KGDB_DEVRATE
251 #define KGDB_DEVRATE CONSPEED
252 #endif
253 int kgdb_devrate = KGDB_DEVRATE;
254
255 #ifndef KGDB_DEVMODE
256 #define KGDB_DEVMODE CONMODE
257 #endif
258 int kgdb_devmode = KGDB_DEVMODE;
259 #endif /* KGDB */
260
261 /*
262 * void cpu_reboot(int howto, char *bootstr)
263 *
264 * Reboots the system
265 *
266 * Deal with any syncing, unmounting, dumping and shutdown hooks,
267 * then reset the CPU.
268 */
269 void
270 cpu_reboot(int howto, char *bootstr)
271 {
272
273 /*
274 * If we are still cold then hit the air brakes
275 * and crash to earth fast
276 */
277 if (cold) {
278 doshutdownhooks();
279 pmf_system_shutdown(boothowto);
280 printf("The operating system has halted.\n");
281 printf("Please press any key to reboot.\n\n");
282 cngetc();
283 printf("rebooting...\n");
284 goto reset;
285 }
286
287 /* Disable console buffering */
288
289 /*
290 * If RB_NOSYNC was not specified sync the discs.
291 * Note: Unless cold is set to 1 here, syslogd will die during the
292 * unmount. It looks like syslogd is getting woken up only to find
293 * that it cannot page part of the binary in as the filesystem has
294 * been unmounted.
295 */
296 if (!(howto & RB_NOSYNC))
297 bootsync();
298
299 /* Say NO to interrupts */
300 splhigh();
301
302 /* Do a dump if requested. */
303 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
304 dumpsys();
305
306 /* Run any shutdown hooks */
307 doshutdownhooks();
308
309 pmf_system_shutdown(boothowto);
310
311 /* Make sure IRQ's are disabled */
312 IRQdisable;
313
314 if (howto & RB_HALT) {
315 printf("The operating system has halted.\n");
316 printf("Please press any key to reboot.\n\n");
317 cngetc();
318 }
319
320 printf("rebooting...\n\r");
321 reset:
322 /*
323 * Make really really sure that all interrupts are disabled,
324 * and poke the Internal Bus and Peripheral Bus reset lines.
325 */
326 (void) disable_interrupts(I32_bit|F32_bit);
327 *(volatile uint32_t *)(IYONIX_80321_VBASE + VERDE_ATU_BASE +
328 ATU_PCSR) = PCSR_RIB | PCSR_RPB;
329
330 /* ...and if that didn't work, just croak. */
331 printf("RESET FAILED!\n");
332 for (;;);
333 }
334
335 /* Static device mappings. */
336 static const struct pmap_devmap iyonix_devmap[] = {
337 /*
338 * Map the on-board devices VA == PA so that we can access them
339 * with the MMU on or off.
340 */
341 {
342 IYONIX_OBIO_BASE,
343 IYONIX_OBIO_BASE,
344 IYONIX_OBIO_SIZE,
345 VM_PROT_READ|VM_PROT_WRITE,
346 PTE_NOCACHE,
347 },
348
349 {
350 IYONIX_IOW_VBASE,
351 VERDE_OUT_XLATE_IO_WIN0_BASE,
352 VERDE_OUT_XLATE_IO_WIN_SIZE,
353 VM_PROT_READ|VM_PROT_WRITE,
354 PTE_NOCACHE,
355 },
356
357 {
358 IYONIX_80321_VBASE,
359 VERDE_PMMR_BASE,
360 VERDE_PMMR_SIZE,
361 VM_PROT_READ|VM_PROT_WRITE,
362 PTE_NOCACHE,
363 },
364
365 {
366 IYONIX_FLASH_BASE,
367 IYONIX_FLASH_BASE,
368 IYONIX_FLASH_SIZE,
369 VM_PROT_READ|VM_PROT_WRITE,
370 PTE_NOCACHE,
371 },
372
373 {
374 0,
375 0,
376 0,
377 0,
378 0,
379 }
380 };
381
382 /* Read out the Machine ID from the flash, and stash it away for later use. */
383
384 void
385 iyonix_read_machineid(void)
386 {
387 volatile uint32_t *flashbase = (uint32_t *)IYONIX_FLASH_BASE;
388 volatile uint16_t *flashword = (uint16_t *)IYONIX_FLASH_BASE;
389 union {
390 uint32_t w[2];
391 uint8_t b[8];
392 } machid;
393
394 /* Enter SecSi Sector Region */
395 flashword[0x555] = 0xAA;
396 flashword[0x2AA] = 0x55;
397 flashword[0x555] = 0x88;
398
399 machid.w[0] = flashbase[0];
400 machid.w[1] = flashbase[1];
401
402 iyonix_macaddr[0] = machid.b[6];
403 iyonix_macaddr[1] = machid.b[5];
404 iyonix_macaddr[2] = machid.b[4];
405 iyonix_macaddr[3] = machid.b[3];
406 iyonix_macaddr[4] = machid.b[2];
407 iyonix_macaddr[5] = machid.b[1];
408
409 /* Exit SecSi Sector Region */
410 flashword[0x555] = 0xAA;
411 flashword[0x2AA] = 0x55;
412 flashword[0x555] = 0x90;
413 flashword[0x555] = 0x00;
414 }
415
416 #define IYONIX_PIC_WRITE(a,v) (*((char *)IYONIX_OBIO_BASE + (a)) = (v))
417
418 void
419 iyonix_pic_init(void)
420 {
421 IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT);
422 IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW2, ICW2_IRL(0));
423 IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW3, ICW3_CASCADE(2));
424 IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW4, ICW4_8086);
425 IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_OCW1, 0x0); /* Unmask */
426
427 IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT);
428 IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW2, ICW2_IRL(0));
429 IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW3, ICW3_CASCADE(1));
430 IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW4, ICW4_8086);
431 IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_OCW1, 0x0); /* Unmask */
432
433 }
434
435 /*
436 * vaddr_t initarm(...)
437 *
438 * Initial entry point on startup. This gets called before main() is
439 * entered.
440 * It should be responsible for setting up everything that must be
441 * in place when main is called.
442 * This includes
443 * Taking a copy of the boot configuration structure.
444 * Initialising the physical console so characters can be printed.
445 * Setting up page tables for the kernel
446 * Initialising interrupt controllers to a sane default state
447 */
448 vaddr_t
449 initarm(void *arg)
450 {
451 struct bootconfig *passed_bootconfig = arg;
452 extern vaddr_t xscale_cache_clean_addr;
453 #ifdef DIAGNOSTIC
454 extern vsize_t xscale_minidata_clean_size;
455 #endif
456 extern char _end[];
457 int loop;
458 int loop1;
459 u_int l1pagetable;
460 paddr_t memstart = 0;
461 psize_t memsize = 0;
462
463 /* Calibrate the delay loop. */
464 i80321_calibrate_delay();
465
466 /* Ensure bootconfig has valid magic */
467 if (passed_bootconfig->magic != BOOTCONFIG_MAGIC)
468 printf("Bad bootconfig magic: %x\n", bootconfig.magic);
469
470 bootconfig = *passed_bootconfig;
471
472 /* Fake bootconfig structure for anything that still needs it */
473 /* XXX must make the memory description h/w independent */
474 bootconfig.dram[0].address = memstart;
475 bootconfig.dram[0].pages = memsize / PAGE_SIZE;
476 bootconfig.dramblocks = 1;
477
478 /* process arguments - can update boothowto */
479 process_kernel_args();
480
481 /*
482 * Since we map the on-board devices VA==PA, and the kernel
483 * is running VA==PA, it's possible for us to initialize
484 * the console now.
485 */
486 consinit();
487
488 #ifdef VERBOSE_INIT_ARM
489 /* Talk to the user */
490 printf("\nNetBSD/iyonix booting ...\n");
491 #endif
492
493 /*
494 * Heads up ... Setup the CPU / MMU / TLB functions
495 */
496 if (set_cpufuncs())
497 panic("cpu not recognized!");
498
499 /*
500 * We are currently running with the MMU enabled and the
501 * entire address space mapped VA==PA.
502 */
503
504 /*
505 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
506 * registers.
507 */
508 i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
509 &memstart, &memsize);
510
511 #ifdef VERBOSE_INIT_ARM
512 printf("initarm: Configuring system ...\n");
513 #endif
514
515 /*
516 * Set up the variables that define the availability of
517 * physical memory.
518 */
519 physical_start = memstart;
520 physical_end = physical_start + memsize;
521
522 physical_freestart = physical_start +
523 (((uintptr_t) _end - KERNEL_TEXT_BASE + PGOFSET) & ~PGOFSET);
524 physical_freeend = physical_end;
525
526 physmem = (physical_end - physical_start) / PAGE_SIZE;
527
528 #ifdef VERBOSE_INIT_ARM
529 /* Tell the user about the memory */
530 printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
531 physical_start, physical_end - 1);
532 #endif
533
534 /*
535 * The kernel is loaded at the base of physical memory. We allocate
536 * pages upwards from the top of the kernel.
537 *
538 * We need to allocate some fixed page tables to get the kernel
539 * going. We allocate one page directory and a number of page
540 * tables and store the physical addresses in the kernel_pt_table
541 * array.
542 *
543 * The kernel page directory must be on a 16K boundary. The page
544 * tables must be on 4K boundaries. What we do is allocate the
545 * page directory on the first 16K boundary that we encounter, and
546 * the page tables on 4K boundaries otherwise. Since we allocate
547 * at least 3 L2 page tables, we are guaranteed to encounter at
548 * least one 16K aligned region.
549 */
550
551 #ifdef VERBOSE_INIT_ARM
552 printf("Allocating page tables\n");
553 #endif
554
555 free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
556
557 #ifdef VERBOSE_INIT_ARM
558 printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
559 physical_freestart, free_pages, free_pages);
560 #endif
561
562 /* Define a macro to simplify memory allocation */
563 #define valloc_pages(var, np) \
564 alloc_pages((var).pv_pa, (np)); \
565 (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
566
567 #define alloc_pages(var, np) \
568 (var) = physical_freestart; \
569 physical_freestart += ((np) * PAGE_SIZE); \
570 if (physical_freeend < physical_freestart) \
571 panic("initarm: out of memory"); \
572 free_pages -= (np); \
573 memset((char *)(var), 0, ((np) * PAGE_SIZE));
574
575 loop1 = 0;
576 kernel_l1pt.pv_pa = kernel_l1pt.pv_va = 0;
577 for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
578 /* Are we 16KB aligned for an L1 ? */
579 if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
580 && kernel_l1pt.pv_pa == 0) {
581 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
582 } else {
583 valloc_pages(kernel_pt_table[loop1],
584 L2_TABLE_SIZE / PAGE_SIZE);
585 ++loop1;
586 }
587 }
588
589 /* This should never be able to happen but better confirm that. */
590 if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
591 panic("initarm: Failed to align the kernel page directory");
592
593 /*
594 * Allocate a page for the system page mapped to V0x00000000
595 * This page will just contain the system vectors and can be
596 * shared by all processes.
597 */
598 alloc_pages(systempage.pv_pa, 1);
599
600 /* Allocate stacks for all modes */
601 valloc_pages(irqstack, IRQ_STACK_SIZE);
602 valloc_pages(abtstack, ABT_STACK_SIZE);
603 valloc_pages(undstack, UND_STACK_SIZE);
604 valloc_pages(kernelstack, UPAGES);
605
606 /* Allocate enough pages for cleaning the Mini-Data cache. */
607 KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
608 valloc_pages(minidataclean, 1);
609
610 #ifdef VERBOSE_INIT_ARM
611 printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
612 irqstack.pv_va);
613 printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
614 abtstack.pv_va);
615 printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
616 undstack.pv_va);
617 printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
618 kernelstack.pv_va);
619 #endif
620
621 alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
622
623 /*
624 * Ok we have allocated physical pages for the primary kernel
625 * page tables
626 */
627
628 #ifdef VERBOSE_INIT_ARM
629 printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
630 #endif
631
632 /*
633 * Now we start construction of the L1 page table
634 * We start by mapping the L2 page tables into the L1.
635 * This means that we can replace L1 mappings later on if necessary
636 */
637 l1pagetable = kernel_l1pt.pv_pa;
638
639 /* Map the L2 pages tables in the L1 page table */
640 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
641 &kernel_pt_table[KERNEL_PT_SYS]);
642 for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
643 pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
644 &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
645 pmap_link_l2pt(l1pagetable, IYONIX_IOPXS_VBASE,
646 &kernel_pt_table[KERNEL_PT_IOPXS]);
647 for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
648 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
649 &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
650
651 /* update the top of the kernel VM */
652 pmap_curmaxkvaddr =
653 KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
654
655 #ifdef VERBOSE_INIT_ARM
656 printf("Mapping kernel\n");
657 #endif
658
659 /* Now we fill in the L2 pagetable for the kernel static code/data */
660 {
661 extern char etext[], _end[];
662 size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
663 size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
664 u_int logical;
665
666 textsize = (textsize + PGOFSET) & ~PGOFSET;
667 totalsize = (totalsize + PGOFSET) & ~PGOFSET;
668
669 logical = 0; /* offset of kernel in RAM */
670 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
671 physical_start + logical, textsize,
672 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
673 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
674 physical_start + logical, totalsize - textsize,
675 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
676 }
677
678 #ifdef VERBOSE_INIT_ARM
679 printf("Constructing L2 page tables\n");
680 #endif
681
682 /* Map the stack pages */
683 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
684 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
685 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
686 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
687 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
688 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
689 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
690 UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
691
692 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
693 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
694
695 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
696 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
697 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
698 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
699 }
700
701 /* Map the Mini-Data cache clean area. */
702 xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
703 minidataclean.pv_pa);
704
705 /* Map the vector page. */
706 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
707 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
708
709 /* Map the statically mapped devices. */
710 pmap_devmap_bootstrap(l1pagetable, iyonix_devmap);
711
712 /*
713 * Give the XScale global cache clean code an appropriately
714 * sized chunk of unmapped VA space starting at 0xff000000
715 * (our device mappings end before this address).
716 */
717 xscale_cache_clean_addr = 0xff000000U;
718
719 /*
720 * Now we have the real page tables in place so we can switch to them.
721 * Once this is done we will be running with the REAL kernel page
722 * tables.
723 */
724
725 /* Switch tables */
726 #ifdef VERBOSE_INIT_ARM
727 printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
728 physical_freestart, free_pages, free_pages);
729 printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
730 #endif
731 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
732 cpu_setttb(kernel_l1pt.pv_pa, true);
733 cpu_tlb_flushID();
734 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
735
736 iyonix_read_machineid();
737
738 /*
739 * Moved from cpu_startup() as data_abort_handler() references
740 * this during uvm init
741 */
742 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
743
744 #ifdef VERBOSE_INIT_ARM
745 printf("done!\n");
746 #endif
747
748 #ifdef VERBOSE_INIT_ARM
749 printf("bootstrap done.\n");
750 #endif
751
752 arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
753
754 /*
755 * Pages were allocated during the secondary bootstrap for the
756 * stacks for different CPU modes.
757 * We must now set the r13 registers in the different CPU modes to
758 * point to these stacks.
759 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
760 * of the stack memory.
761 */
762 #ifdef VERBOSE_INIT_ARM
763 printf("init subsystems: stacks ");
764 #endif
765
766 set_stackptr(PSR_IRQ32_MODE,
767 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
768 set_stackptr(PSR_ABT32_MODE,
769 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
770 set_stackptr(PSR_UND32_MODE,
771 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
772
773 /*
774 * Well we should set a data abort handler.
775 * Once things get going this will change as we will need a proper
776 * handler.
777 * Until then we will use a handler that just panics but tells us
778 * why.
779 * Initialisation of the vectors will just panic on a data abort.
780 * This just fills in a slightly better one.
781 */
782 #ifdef VERBOSE_INIT_ARM
783 printf("vectors ");
784 #endif
785 data_abort_handler_address = (u_int)data_abort_handler;
786 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
787 undefined_handler_address = (u_int)undefinedinstruction_bounce;
788
789 /* Initialise the undefined instruction handlers */
790 #ifdef VERBOSE_INIT_ARM
791 printf("undefined ");
792 #endif
793 undefined_init();
794
795 /* Load memory into UVM. */
796 #ifdef VERBOSE_INIT_ARM
797 printf("page ");
798 #endif
799 uvm_md_init();
800 uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
801 atop(physical_freestart), atop(physical_freeend),
802 VM_FREELIST_DEFAULT);
803
804 /* Boot strap pmap telling it where managed kernel virtual memory is */
805 #ifdef VERBOSE_INIT_ARM
806 printf("pmap ");
807 #endif
808 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
809
810 /* Setup the IRQ system */
811 #ifdef VERBOSE_INIT_ARM
812 printf("irq ");
813 #endif
814 i80321_intr_init();
815
816 #ifdef VERBOSE_INIT_ARM
817 printf("done.\n");
818 #endif
819
820 #ifdef DDB
821 db_machine_init();
822 if (boothowto & RB_KDB)
823 Debugger();
824 #endif
825
826 iyonix_pic_init();
827
828 printf("args: %s\n", bootconfig.args);
829 printf("howto: %x\n", boothowto);
830
831 /* We return the new stack pointer address */
832 return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
833 }
834
835 void
836 consinit(void)
837 {
838 static int consinit_called;
839
840 if (consinit_called != 0)
841 return;
842
843 consinit_called = 1;
844
845 /* We let consinit_<foo> worry about device numbers */
846 if (strncmp(boot_consdev, "genfb", 5) &&
847 strncmp(boot_consdev, "com", 3))
848 strcpy(boot_consdev, DEFAULT_CONSDEV);
849
850 if (!strncmp(boot_consdev, "com", 3))
851 consinit_com(boot_consdev);
852 else
853 consinit_genfb(boot_consdev);
854 }
855
856 static void
857 consinit_com(const char *consdev)
858 {
859 static const bus_addr_t comcnaddrs[] = {
860 IYONIX_UART1, /* com0 */
861 };
862 /*
863 * Console devices are mapped VA==PA. Our devmap reflects
864 * this, so register it now so drivers can map the console
865 * device.
866 */
867 pmap_devmap_register(iyonix_devmap);
868
869 /* When we support more than the first serial port as console,
870 * we should check consdev for a number.
871 */
872 #if NCOM > 0
873 if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
874 COM_FREQ, COM_TYPE_NORMAL, comcnmode))
875 {
876 panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
877 }
878 #else
879 panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
880 #endif
881
882 #if KGDB
883 #if NCOM > 0
884 if (strcmp(kgdb_devname, "com") == 0) {
885 com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
886 COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
887 }
888 #endif /* NCOM > 0 */
889 #endif /* KGDB */
890 }
891
892 static void
893 consinit_genfb(const char *consdev)
894 {
895 /* NOTYET */
896 }
897
898 static void
899 process_kernel_args(void)
900 {
901 char *args;
902
903 /* Ok now we will check the arguments for interesting parameters. */
904 args = bootconfig.args;
905
906 #ifdef BOOTHOWTO
907 boothowto = BOOTHOWTO;
908 #else
909 boothowto = 0;
910 #endif
911
912 /* Only arguments itself are passed from the bootloader */
913 while (*args == ' ')
914 ++args;
915
916 boot_args = args;
917 parse_mi_bootargs(boot_args);
918 parse_iyonix_bootargs(boot_args);
919 }
920
921 static void
922 parse_iyonix_bootargs(char *args)
923 {
924 char *ptr;
925
926 if (get_bootconf_option(args, "consdev", BOOTOPT_TYPE_STRING, &ptr))
927 {
928 /* ptr may have trailing clutter */
929 strlcpy(boot_consdev, ptr, sizeof(boot_consdev));
930 if ( (ptr = strchr(boot_consdev, ' ')) )
931 *ptr = 0;
932 }
933 }
934