No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / evbarm / gumstix / gumstix_machdep.c
blobb244565f20df0f4206c500564d5c450ef6384263
1 /* $NetBSD$ */
2 /*
3 * Copyright (C) 2005, 2006, 2007 WIDE Project and SOUM Corporation.
4 * All rights reserved.
6 * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
7 * Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the name of SOUM Corporation
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT and SOUM CORPORATION ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT AND SOUM CORPORATION
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 2002, 2003, 2004, 2005 Genetec Corporation.
35 * All rights reserved.
37 * Written by Hiroyuki Bessho for Genetec Corporation.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The name of Genetec Corporation may not be used to endorse or
48 * promote products derived from this software without specific prior
49 * written permission.
51 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
63 * Machine dependant functions for kernel setup for Genetec G4250EBX
64 * evaluation board.
66 * Based on iq80310_machhdep.c
69 * Copyright (c) 2001 Wasabi Systems, Inc.
70 * All rights reserved.
72 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
76 * are met:
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 * notice, this list of conditions and the following disclaimer in the
81 * documentation and/or other materials provided with the distribution.
82 * 3. All advertising materials mentioning features or use of this software
83 * must display the following acknowledgement:
84 * This product includes software developed for the NetBSD Project by
85 * Wasabi Systems, Inc.
86 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
87 * or promote products derived from this software without specific prior
88 * written permission.
90 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
91 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
92 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
93 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
94 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
95 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
96 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
97 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
98 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
99 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
100 * POSSIBILITY OF SUCH DAMAGE.
104 * Copyright (c) 1997,1998 Mark Brinicombe.
105 * Copyright (c) 1997,1998 Causality Limited.
106 * All rights reserved.
108 * Redistribution and use in source and binary forms, with or without
109 * modification, are permitted provided that the following conditions
110 * are met:
111 * 1. Redistributions of source code must retain the above copyright
112 * notice, this list of conditions and the following disclaimer.
113 * 2. Redistributions in binary form must reproduce the above copyright
114 * notice, this list of conditions and the following disclaimer in the
115 * documentation and/or other materials provided with the distribution.
116 * 3. All advertising materials mentioning features or use of this software
117 * must display the following acknowledgement:
118 * This product includes software developed by Mark Brinicombe
119 * for the NetBSD Project.
120 * 4. The name of the company nor the name of the author may be used to
121 * endorse or promote products derived from this software without specific
122 * prior written permission.
124 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
125 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
126 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
127 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
128 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
129 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
130 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
131 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
132 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
133 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
134 * SUCH DAMAGE.
136 * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
137 * boards using RedBoot firmware.
140 #include "opt_ddb.h"
141 #include "opt_kgdb.h"
142 #include "opt_pmap_debug.h"
143 #include "opt_md.h"
144 #include "opt_modular.h"
145 #include "opt_com.h"
146 #include "md.h"
148 #include <sys/param.h>
149 #include <sys/device.h>
150 #include <sys/systm.h>
151 #include <sys/kernel.h>
152 #include <sys/exec.h>
153 #include <sys/proc.h>
154 #include <sys/msgbuf.h>
155 #include <sys/reboot.h>
156 #include <sys/termios.h>
157 #include <sys/ksyms.h>
159 #include <uvm/uvm_extern.h>
161 #include <sys/conf.h>
162 #include <dev/cons.h>
163 #include <dev/md.h>
165 #include <machine/db_machdep.h>
166 #include <ddb/db_sym.h>
167 #include <ddb/db_extern.h>
168 #ifdef KGDB
169 #include <sys/kgdb.h>
170 #endif
172 #include <machine/bootconfig.h>
173 #include <machine/bus.h>
174 #include <machine/cpu.h>
175 #include <machine/frame.h>
176 #include <arm/undefined.h>
178 #include <arm/arm32/machdep.h>
180 #include <arm/xscale/pxa2x0reg.h>
181 #include <arm/xscale/pxa2x0var.h>
182 #include <arm/xscale/pxa2x0_gpio.h>
183 #include <evbarm/gumstix/gumstixreg.h>
184 #include <evbarm/gumstix/gumstixvar.h>
186 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
187 #define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00200000)
188 #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
191 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
192 * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
194 #define KERNEL_VM_SIZE 0x0C000000
198 * Address to call from cpu_reset() to reset the machine.
199 * This is machine architecture dependant as it varies depending
200 * on where the ROM appears when you turn the MMU off.
203 u_int cpu_reset_address = 0;
205 /* Define various stack sizes in pages */
206 #define IRQ_STACK_SIZE 1
207 #define ABT_STACK_SIZE 1
208 #define UND_STACK_SIZE 1
210 BootConfig bootconfig; /* Boot config storage */
211 static char bootargs[MAX_BOOT_STRING];
212 char *boot_args = NULL;
214 uint32_t system_serial_high;
215 uint32_t system_serial_low;
217 vm_offset_t physical_start;
218 vm_offset_t physical_freestart;
219 vm_offset_t physical_freeend;
220 vm_offset_t physical_end;
221 u_int free_pages;
223 /*int debug_flags;*/
224 #ifndef PMAP_STATIC_L1S
225 int max_processes = 64; /* Default number */
226 #endif /* !PMAP_STATIC_L1S */
228 /* Physical and virtual addresses for some global pages */
229 pv_addr_t irqstack;
230 pv_addr_t undstack;
231 pv_addr_t abtstack;
232 pv_addr_t kernelstack;
233 pv_addr_t minidataclean;
235 vm_offset_t msgbufphys;
237 extern u_int data_abort_handler_address;
238 extern u_int prefetch_abort_handler_address;
239 extern u_int undefined_handler_address;
241 #ifdef PMAP_DEBUG
242 extern int pmap_debug_level;
243 #endif
245 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
246 #define KERNEL_PT_KERNEL 1 /* Page table for mapping kernel */
247 #define KERNEL_PT_KERNEL_NUM 4
248 #define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
249 /* Page tables for mapping kernel VM */
250 #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
251 #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
253 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
255 /* Prototypes */
256 static void read_system_serial(void);
257 static void process_kernel_args(int, char *[]);
258 static void process_kernel_args_line(char *);
259 #ifdef KGDB
260 static void kgdb_port_init(void);
261 #endif
263 bs_protos(bs_notimpl);
265 #include "com.h"
266 #if NCOM > 0
267 #include <dev/ic/comreg.h>
268 #include <dev/ic/comvar.h>
269 #endif
271 #include "lcd.h"
273 #ifndef CONSPEED
274 #define CONSPEED B115200 /* It's a setting of the default of u-boot */
275 #endif
276 #ifndef CONMODE
277 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
278 #endif
280 int comcnspeed = CONSPEED;
281 int comcnmode = CONMODE;
283 extern void gxio_config_pin(void);
284 extern void gxio_config_expansion(char *);
287 * void cpu_reboot(int howto, char *bootstr)
289 * Deal with any syncing, unmounting, dumping and shutdown hooks,
290 * then reset the CPU.
292 void
293 cpu_reboot(int howto, char *bootstr)
296 #ifdef DIAGNOSTIC
297 /* info */
298 printf("boot: howto=%08x curproc=%p\n", howto, curproc);
299 #endif
302 * If we are still cold then hit the air brakes
303 * and crash to earth fast
305 if (cold) {
306 doshutdownhooks();
307 pmf_system_shutdown(boothowto);
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*/
317 * If RB_NOSYNC was not specified sync the discs.
318 * Note: Unless cold is set to 1 here, syslogd will die during the
319 * unmount. It looks like syslogd is getting woken up only to find
320 * that it cannot page part of the binary in as the filesystem has
321 * been unmounted.
323 if (!(howto & RB_NOSYNC))
324 bootsync();
326 /* Say NO to interrupts */
327 splhigh();
329 /* Do a dump if requested. */
330 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
331 dumpsys();
333 /* Run any shutdown hooks */
334 doshutdownhooks();
336 pmf_system_shutdown(boothowto);
338 /* Make sure IRQ's are disabled */
339 IRQdisable;
341 if (howto & RB_HALT) {
342 printf("The operating system has halted.\n");
343 printf("Please press any key to reboot.\n\n");
344 cngetc();
347 printf("rebooting...\n");
348 cpu_reset();
349 /*NOTREACHED*/
352 static inline
353 pd_entry_t *
354 read_ttb(void)
356 long ttb;
358 __asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
361 return (pd_entry_t *)(ttb & ~((1<<14)-1));
365 * Static device mappings. These peripheral registers are mapped at
366 * fixed virtual addresses very early in initarm() so that we can use
367 * them while booting the kernel, and stay at the same address
368 * throughout whole kernel's life time.
370 * We use this table twice; once with bootstrap page table, and once
371 * with kernel's page table which we build up in initarm().
373 * Since we map these registers into the bootstrap page table using
374 * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
375 * registers segment-aligned and segment-rounded in order to avoid
376 * using the 2nd page tables.
379 #define _A(a) ((a) & ~L1_S_OFFSET)
380 #define _S(s) (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
382 static const struct pmap_devmap gumstix_devmap[] = {
384 GUMSTIX_GPIO_VBASE,
385 _A(PXA2X0_GPIO_BASE),
386 _S(PXA250_GPIO_SIZE),
387 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
390 GUMSTIX_CLKMAN_VBASE,
391 _A(PXA2X0_CLKMAN_BASE),
392 _S(PXA2X0_CLKMAN_SIZE),
393 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
396 GUMSTIX_INTCTL_VBASE,
397 _A(PXA2X0_INTCTL_BASE),
398 _S(PXA2X0_INTCTL_SIZE),
399 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
402 GUMSTIX_FFUART_VBASE,
403 _A(PXA2X0_FFUART_BASE),
404 _S(4 * COM_NPORTS),
405 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
408 GUMSTIX_STUART_VBASE,
409 _A(PXA2X0_STUART_BASE),
410 _S(4 * COM_NPORTS),
411 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
414 GUMSTIX_BTUART_VBASE,
415 _A(PXA2X0_BTUART_BASE),
416 _S(4 * COM_NPORTS),
417 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
420 GUMSTIX_HWUART_VBASE,
421 _A(PXA2X0_HWUART_BASE),
422 _S(4 * COM_NPORTS),
423 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
426 GUMSTIX_LCDC_VBASE,
427 _A(PXA2X0_LCDC_BASE),
428 _S(4 * COM_NPORTS),
429 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
431 {0, 0, 0, 0, 0}
434 #undef _A
435 #undef _S
439 * u_int initarm(...)
441 * Initial entry point on startup. This gets called before main() is
442 * entered.
443 * It should be responsible for setting up everything that must be
444 * in place when main is called.
445 * This includes
446 * Taking a copy of the boot configuration structure.
447 * Initialising the physical console so characters can be printed.
448 * Setting up page tables for the kernel
449 * Relocating the kernel to the bottom of physical memory
451 u_int
452 initarm(void *arg)
454 extern vaddr_t xscale_cache_clean_addr;
455 extern uint32_t *u_boot_args[];
456 extern uint32_t ram_size;
457 enum { r0 = 0, r1 = 1, r2 = 2, r3 = 3 }; /* args from u-boot */
458 int loop;
459 int loop1;
460 u_int l1pagetable;
461 paddr_t memstart;
462 psize_t memsize;
463 #ifdef DIAGNOSTIC
464 extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
465 #endif
467 /* map some peripheral registers at static I/O area */
468 pmap_devmap_bootstrap((vaddr_t)read_ttb(), gumstix_devmap);
470 /* start 32.768kHz OSC */
471 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_OSCC, OSCC_OON);
473 /* Get ready for splfoo() */
474 pxa2x0_intr_bootstrap(GUMSTIX_INTCTL_VBASE);
477 * Heads up ... Setup the CPU / MMU / TLB functions
479 if (set_cpufuncs())
480 panic("cpu not recognized!");
483 * U-Boot doesn't use the virtual memory.
485 * Physical Address Range Description
486 * ----------------------- ----------------------------------
487 * 0x00000000 - 0x00ffffff flash Memory (16MB or 4MB)
488 * 0x40000000 - 0x480fffff Processor Registers
489 * 0xa0000000 - 0xa3ffffff SDRAM Bank 0 (64MB)
492 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
494 /* setup GPIO for {FF,ST,HW}UART. */
495 pxa2x0_gpio_bootstrap(GUMSTIX_GPIO_VBASE);
497 /* configure GPIOs. */
498 gxio_config_pin();
500 pxa2x0_clkman_bootstrap(GUMSTIX_CLKMAN_VBASE);
502 consinit();
503 #ifdef KGDB
504 kgdb_port_init();
505 #endif
507 /* Talk to the user */
508 printf("\nNetBSD/evbarm (gumstix) booting ...\n");
510 /* Read system serial */
511 read_system_serial();
514 * Examine the boot args string for options we need to know about
515 * now.
517 #define SDRAM_START 0xa0000000UL
518 if (((uint32_t)u_boot_args[r0] & 0xf0000000) != SDRAM_START)
519 /* Maybe r0 is 'argc'. We are booted by command 'go'. */
520 process_kernel_args((int)u_boot_args[r0],
521 (char **)u_boot_args[r1]);
522 else
524 * Maybe r3 is 'boot args string' of 'bootm'. This string is
525 * linely.
527 process_kernel_args_line((char *)u_boot_args[r3]);
529 memstart = SDRAM_START;
530 memsize = ram_size;
532 #ifdef VERBOSE_INIT_ARM
533 printf("initarm: Configuring system ...\n");
534 #endif
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 = memstart;
540 bootconfig.dram[0].pages = memsize / PAGE_SIZE;
543 * Set up the variables that define the availablilty of
544 * physical memory. For now, we're going to set
545 * physical_freestart to 0xa0200000 (where the kernel
546 * was loaded), and allocate the memory we need downwards.
547 * If we get too close to the L1 table that we set up, we
548 * will panic. We will update physical_freestart and
549 * physical_freeend later to reflect what pmap_bootstrap()
550 * wants to see.
552 * XXX pmap_bootstrap() needs an enema.
554 physical_start = bootconfig.dram[0].address;
555 physical_end = physical_start + memsize;
557 physical_freestart = 0xa0009000UL;
558 physical_freeend = 0xa0200000UL;
560 physmem = (physical_end - physical_start) / PAGE_SIZE;
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 #endif
569 * Okay, the kernel starts 2MB in from the bottom of physical
570 * memory. We are going to allocate our bootstrap pages downwards
571 * from there.
573 * We need to allocate some fixed page tables to get the kernel
574 * going. We allocate one page directory and a number of page
575 * tables and store the physical addresses in the kernel_pt_table
576 * array.
578 * The kernel page directory must be on a 16K boundary. The page
579 * tables must be on 4K bounaries. What we do is allocate the
580 * page directory on the first 16K boundary that we encounter, and
581 * the page tables on 4K boundaries otherwise. Since we allocate
582 * at least 3 L2 page tables, we are guaranteed to encounter at
583 * least one 16K aligned region.
586 #ifdef VERBOSE_INIT_ARM
587 printf("Allocating page tables\n");
588 #endif
590 free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
592 #ifdef VERBOSE_INIT_ARM
593 printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
594 physical_freestart, free_pages, free_pages);
595 #endif
597 /* Define a macro to simplify memory allocation */
598 #define valloc_pages(var, np) \
599 alloc_pages((var).pv_pa, (np)); \
600 (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
602 #define alloc_pages(var, np) \
603 physical_freeend -= ((np) * PAGE_SIZE); \
604 if (physical_freeend < physical_freestart) \
605 panic("initarm: out of memory"); \
606 (var) = physical_freeend; \
607 free_pages -= (np); \
608 memset((char *)(var), 0, ((np) * PAGE_SIZE));
610 loop1 = 0;
611 for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
612 /* Are we 16KB aligned for an L1 ? */
613 if ((physical_freeend & (L1_TABLE_SIZE - 1)) == 0 &&
614 kernel_l1pt.pv_pa == 0) {
615 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
616 } else {
617 valloc_pages(kernel_pt_table[loop1],
618 L2_TABLE_SIZE / PAGE_SIZE);
619 ++loop1;
623 /* This should never be able to happen but better confirm that. */
624 if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
625 panic("initarm: Failed to align the kernel page directory");
628 * Allocate a page for the system page mapped to V0x00000000
629 * This page will just contain the system vectors and can be
630 * shared by all processes.
632 alloc_pages(systempage.pv_pa, 1);
634 /* Allocate stacks for all modes */
635 valloc_pages(irqstack, IRQ_STACK_SIZE);
636 valloc_pages(abtstack, ABT_STACK_SIZE);
637 valloc_pages(undstack, UND_STACK_SIZE);
638 valloc_pages(kernelstack, UPAGES);
640 /* Allocate enough pages for cleaning the Mini-Data cache. */
641 KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
642 valloc_pages(minidataclean, 1);
644 #ifdef VERBOSE_INIT_ARM
645 printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
646 irqstack.pv_va);
647 printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
648 abtstack.pv_va);
649 printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
650 undstack.pv_va);
651 printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
652 kernelstack.pv_va);
653 #endif
656 * XXX Defer this to later so that we can reclaim the memory
657 * XXX used by the RedBoot page tables.
659 alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
662 * Ok we have allocated physical pages for the primary kernel
663 * page tables
666 #ifdef VERBOSE_INIT_ARM
667 printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
668 #endif
671 * Now we start construction of the L1 page table
672 * We start by mapping the L2 page tables into the L1.
673 * This means that we can replace L1 mappings later on if necessary
675 l1pagetable = kernel_l1pt.pv_va;
677 /* Map the L2 pages tables in the L1 page table */
678 pmap_link_l2pt(l1pagetable, 0x00000000,
679 &kernel_pt_table[KERNEL_PT_SYS]);
680 for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
681 pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
682 &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
683 for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
684 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
685 &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
687 /* update the top of the kernel VM */
688 pmap_curmaxkvaddr =
689 KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
691 #ifdef VERBOSE_INIT_ARM
692 printf("Mapping kernel\n");
693 #endif
695 /* Now we fill in the L2 pagetable for the kernel static code/data */
697 extern char etext[], _end[];
698 size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
699 size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
700 u_int logical;
702 textsize = (textsize + PGOFSET) & ~PGOFSET;
703 totalsize = (totalsize + PGOFSET) & ~PGOFSET;
705 logical = 0x00200000; /* offset of kernel in RAM */
707 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
708 physical_start + logical, textsize,
709 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
710 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
711 physical_start + logical, totalsize - textsize,
712 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
715 #ifdef VERBOSE_INIT_ARM
716 printf("Constructing L2 page tables\n");
717 #endif
719 /* Map the stack pages */
720 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
721 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
722 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
723 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
724 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
725 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
726 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
727 UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
729 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
730 L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
732 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
733 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
734 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
735 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
738 /* Map the Mini-Data cache clean area. */
739 xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
740 minidataclean.pv_pa);
742 /* Map the vector page. */
743 #if 1
744 /* MULTI-ICE requires that page 0 is NC/NB so that it can download the
745 * cache-clean code there. */
746 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
747 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
748 #else
749 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
750 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
751 #endif
754 * map integrated peripherals at same address in l1pagetable
755 * so that we can continue to use console.
757 pmap_devmap_bootstrap(l1pagetable, gumstix_devmap);
760 * Give the XScale global cache clean code an appropriately
761 * sized chunk of unmapped VA space starting at 0xff000000
762 * (our device mappings end before this address).
764 xscale_cache_clean_addr = 0xff000000U;
767 * Now we have the real page tables in place so we can switch to them.
768 * Once this is done we will be running with the REAL kernel page
769 * tables.
773 * Update the physical_freestart/physical_freeend/free_pages
774 * variables.
777 extern char _end[];
779 physical_freestart = physical_start +
780 (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
781 KERNEL_BASE);
782 physical_freeend = physical_end;
783 free_pages =
784 (physical_freeend - physical_freestart) / PAGE_SIZE;
787 /* Switch tables */
788 #ifdef VERBOSE_INIT_ARM
789 printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
790 physical_freestart, free_pages, free_pages);
791 printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
792 #endif
794 cpu_setttb(kernel_l1pt.pv_pa);
795 cpu_tlb_flushID();
796 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
799 * Moved from cpu_startup() as data_abort_handler() references
800 * this during uvm init
802 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
804 #ifdef VERBOSE_INIT_ARM
805 printf("bootstrap done.\n");
806 #endif
808 arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
811 * Pages were allocated during the secondary bootstrap for the
812 * stacks for different CPU modes.
813 * We must now set the r13 registers in the different CPU modes to
814 * point to these stacks.
815 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
816 * of the stack memory.
818 #ifdef VERBOSE_INIT_ARM
819 printf("init subsystems: stacks ");
820 #endif
822 set_stackptr(PSR_IRQ32_MODE,
823 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
824 set_stackptr(PSR_ABT32_MODE,
825 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
826 set_stackptr(PSR_UND32_MODE,
827 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
830 * Well we should set a data abort handler.
831 * Once things get going this will change as we will need a proper
832 * handler.
833 * Until then we will use a handler that just panics but tells us
834 * why.
835 * Initialisation of the vectors will just panic on a data abort.
836 * This just fills in a slighly better one.
838 #ifdef VERBOSE_INIT_ARM
839 printf("vectors ");
840 #endif
841 data_abort_handler_address = (u_int)data_abort_handler;
842 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
843 undefined_handler_address = (u_int)undefinedinstruction_bounce;
845 /* Initialise the undefined instruction handlers */
846 #ifdef VERBOSE_INIT_ARM
847 printf("undefined ");
848 #endif
849 undefined_init();
851 /* Load memory into UVM. */
852 #ifdef VERBOSE_INIT_ARM
853 printf("page ");
854 #endif
855 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
856 uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
857 atop(physical_freestart), atop(physical_freeend),
858 VM_FREELIST_DEFAULT);
860 /* Boot strap pmap telling it where the kernel page table is */
861 #ifdef VERBOSE_INIT_ARM
862 printf("pmap ");
863 #endif
864 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
866 #ifdef __HAVE_MEMORY_DISK__
867 md_root_setconf(memory_disk, sizeof memory_disk);
868 #endif
870 #ifdef BOOTHOWTO
871 boothowto |= BOOTHOWTO;
872 #endif
874 #ifdef KGDB
875 if (boothowto & RB_KDB) {
876 kgdb_debug_init = 1;
877 kgdb_connect(1);
879 #endif
881 #if NKSYMS || defined(DDB) || defined(MODULAR)
882 /* Firmware doesn't load symbols. */
883 ddb_init(0, NULL, NULL);
884 #endif
886 #ifdef DDB
887 db_machine_init();
888 if (boothowto & RB_KDB)
889 Debugger();
890 #endif
892 /* We return the new stack pointer address */
893 return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
896 static void
897 read_system_serial(void)
899 #define GUMSTIX_SYSTEM_SERIAL_ADDR 0
900 #define GUMSTIX_SYSTEM_SERIAL_SIZE 8
901 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
902 #define FLASH_OFFSET_USER_PROTECTION 0x85
903 #define FLASH_CMD_READ_ID 0x90
904 #define FLASH_CMD_RESET 0xff
905 int i;
906 char system_serial[GUMSTIX_SYSTEM_SERIAL_SIZE], *src;
907 char x;
909 src = (char *)(FLASH_OFFSET_USER_PROTECTION * 2 /*word*/);
910 *(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
911 memcpy(system_serial,
912 src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
913 *(volatile uint16_t *)0 = FLASH_CMD_RESET;
915 for (i = 1, x = system_serial[0]; i < sizeof (system_serial); i++)
916 x &= system_serial[i];
917 if (x == 0xff) {
918 src = (char *)(FLASH_OFFSET_INTEL_PROTECTION * 2 /*word*/);
919 *(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
920 memcpy(system_serial,
921 src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
922 *(volatile uint16_t *)0 = FLASH_CMD_RESET;
925 * XXXX: Don't need ???
926 * gumstix_serial_hash(system_serial);
929 system_serial_high = system_serial[0] << 24 | system_serial[1] << 16 |
930 system_serial[2] << 8 | system_serial[3];
931 system_serial_low = system_serial[4] << 24 | system_serial[5] << 16 |
932 system_serial[6] << 8 | system_serial[7];
934 printf("system serial: 0x");
935 for (i = 0; i < sizeof (system_serial); i++)
936 printf("%02x", system_serial[i]);
937 printf("\n");
940 static const char busheader_name[] = "busheader=";
941 static void
942 process_kernel_args(int argc, char *argv[])
944 int gxio_configured = 0, i, j;
946 boothowto = 0;
948 for (i = 1, j = 0; i < argc; i++) {
949 if (!strncmp(argv[i], busheader_name, strlen(busheader_name))) {
950 /* configure for GPIOs of busheader side */
951 gxio_config_expansion(argv[i] + strlen(busheader_name));
952 gxio_configured = 1;
953 continue;
955 if (j == MAX_BOOT_STRING) {
956 *(bootargs + j) = '\0';
957 continue;
959 if (j != 0)
960 *(bootargs + j++) = ' ';
961 strncpy(bootargs + j, argv[i], MAX_BOOT_STRING - j);
962 j += strlen(argv[i]);
964 boot_args = bootargs;
966 parse_mi_bootargs(boot_args);
968 if (!gxio_configured)
969 gxio_config_expansion(NULL);
972 static void
973 process_kernel_args_line(char *args)
975 int i;
976 char expansion[256], *p, c;
978 boothowto = 0;
980 strncpy(bootargs, args, sizeof(bootargs));
981 p = strstr(bootargs, busheader_name);
982 if (p == NULL)
983 gxio_config_expansion(NULL);
984 else {
985 i = 0;
986 do {
987 c = *(p + strlen(busheader_name) + i);
988 if (c == ' ')
989 c = '\0';
990 expansion[i++] = c;
991 } while (c != '\0');
992 gxio_config_expansion(expansion);
993 strcpy(p, p + i);
995 boot_args = bootargs;
997 parse_mi_bootargs(boot_args);
1000 #ifdef KGDB
1001 #ifndef KGDB_DEVNAME
1002 #define KGDB_DEVNAME "ffuart"
1003 #endif
1004 const char kgdb_devname[] = KGDB_DEVNAME;
1006 #ifndef KGDB_DEVRATE
1007 #define KGDB_DEVRATE CONSPEED
1008 #endif
1009 int kgdb_devrate = KGDB_DEVRATE;
1011 #if (NCOM > 0)
1012 #ifndef KGDB_DEVMODE
1013 #define KGDB_DEVMODE CONMODE
1014 #endif
1015 int comkgdbmode = KGDB_DEVMODE;
1016 #endif /* NCOM */
1018 #endif /* KGDB */
1021 void
1022 consinit(void)
1024 static int consinit_called = 0;
1026 if (consinit_called != 0)
1027 return;
1029 consinit_called = 1;
1031 #if NCOM > 0
1033 #ifdef FFUARTCONSOLE
1034 #ifdef KGDB
1035 if (0 == strcmp(kgdb_devname, "ffuart")){
1036 /* port is reserved for kgdb */
1037 } else
1038 #endif
1040 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
1041 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1042 pxa2x0_clkman_config(CKEN_FFUART, 1);
1043 return;
1046 #endif /* FFUARTCONSOLE */
1048 #ifdef STUARTCONSOLE
1049 #ifdef KGDB
1050 if (0 == strcmp(kgdb_devname, "stuart")) {
1051 /* port is reserved for kgdb */
1052 } else
1053 #endif
1055 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_STUART_BASE,
1056 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1057 pxa2x0_clkman_config(CKEN_STUART, 1);
1058 return;
1061 #endif /* STUARTCONSOLE */
1063 #ifdef BTUARTCONSOLE
1064 #ifdef KGDB
1065 if (0 == strcmp(kgdb_devname, "btuart")) {
1066 /* port is reserved for kgdb */
1067 } else
1068 #endif
1070 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
1071 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1072 pxa2x0_clkman_config(CKEN_BTUART, 1);
1073 return;
1076 #endif /* BTUARTCONSOLE */
1078 #ifdef HWUARTCONSOLE
1079 #ifdef KGDB
1080 if (0 == strcmp(kgdb_devname, "hwuart")) {
1081 /* port is reserved for kgdb */
1082 } else
1083 #endif
1085 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_HWUART_BASE,
1086 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1087 pxa2x0_clkman_config(CKEN_HWUART, 1);
1088 return;
1091 #endif /* HWUARTCONSOLE */
1093 #endif /* NCOM */
1095 #if NLCD > 0
1096 gxlcd_cnattach();
1097 #endif
1100 #ifdef KGDB
1101 static void
1102 kgdb_port_init(void)
1104 #if (NCOM > 0) && defined(COM_PXA2X0)
1105 paddr_t paddr = 0;
1106 int cken = 0;
1108 if (0 == strcmp(kgdb_devname, "ffuart")) {
1109 paddr = PXA2X0_FFUART_BASE;
1110 cken = CKEN_FFUART;
1111 } else if (0 == strcmp(kgdb_devname, "stuart")) {
1112 paddr = PXA2X0_STUART_BASE;
1113 cken = CKEN_STUART;
1114 } else if (0 == strcmp(kgdb_devname, "btuart")) {
1115 paddr = PXA2X0_BTUART_BASE;
1116 cken = CKEN_BTUART;
1117 } else if (0 == strcmp(kgdb_devname, "hwuart")) {
1118 paddr = PXA2X0_HWUART_BASE;
1119 cken = CKEN_HWUART;
1122 if (paddr &&
1123 0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
1124 kgdb_devrate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
1126 pxa2x0_clkman_config(cken, 1);
1129 #endif
1131 #endif