gumstix_machdep.c revision 1.20 1 /* $NetBSD: gumstix_machdep.c,v 1.20 2009/11/27 03:23:06 rmind Exp $ */
2 /*
3 * Copyright (C) 2005, 2006, 2007 WIDE Project and SOUM Corporation.
4 * All rights reserved.
5 *
6 * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
7 * Corporation.
8 *
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.
20 *
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.
32 */
33 /*
34 * Copyright (c) 2002, 2003, 2004, 2005 Genetec Corporation.
35 * All rights reserved.
36 *
37 * Written by Hiroyuki Bessho for Genetec Corporation.
38 *
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.
50 *
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.
62 *
63 * Machine dependant functions for kernel setup for Genetec G4250EBX
64 * evaluation board.
65 *
66 * Based on iq80310_machhdep.c
67 */
68 /*
69 * Copyright (c) 2001 Wasabi Systems, Inc.
70 * All rights reserved.
71 *
72 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
73 *
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.
89 *
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.
101 */
102
103 /*
104 * Copyright (c) 1997,1998 Mark Brinicombe.
105 * Copyright (c) 1997,1998 Causality Limited.
106 * All rights reserved.
107 *
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.
123 *
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.
135 *
136 * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
137 * boards using RedBoot firmware.
138 */
139
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"
147
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>
158
159 #include <uvm/uvm_extern.h>
160
161 #include <sys/conf.h>
162 #include <dev/cons.h>
163 #include <dev/md.h>
164
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
171
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>
177
178 #include <arm/arm32/machdep.h>
179
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>
185
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)
189
190 /*
191 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
192 * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
193 */
194 #define KERNEL_VM_SIZE 0x0C000000
195
196
197 /*
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.
201 */
202
203 u_int cpu_reset_address = 0;
204
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
209
210 BootConfig bootconfig; /* Boot config storage */
211 static char bootargs[MAX_BOOT_STRING];
212 char *boot_args = NULL;
213
214 uint32_t system_serial_high;
215 uint32_t system_serial_low;
216
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;
222 vm_offset_t pagetables_start;
223
224 /*int debug_flags;*/
225 #ifndef PMAP_STATIC_L1S
226 int max_processes = 64; /* Default number */
227 #endif /* !PMAP_STATIC_L1S */
228
229 /* Physical and virtual addresses for some global pages */
230 pv_addr_t irqstack;
231 pv_addr_t undstack;
232 pv_addr_t abtstack;
233 pv_addr_t kernelstack;
234 pv_addr_t minidataclean;
235
236 vm_offset_t msgbufphys;
237
238 extern u_int data_abort_handler_address;
239 extern u_int prefetch_abort_handler_address;
240 extern u_int undefined_handler_address;
241
242 #ifdef PMAP_DEBUG
243 extern int pmap_debug_level;
244 #endif
245
246 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
247 #define KERNEL_PT_KERNEL 1 /* Page table for mapping kernel */
248 #define KERNEL_PT_KERNEL_NUM 4
249 #define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
250 /* Page tables for mapping kernel VM */
251 #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
252 #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
253
254 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
255
256 /* Prototypes */
257 static void read_system_serial(void);
258 static void process_kernel_args(int, char *[]);
259 static void process_kernel_args_line(char *);
260 #ifdef KGDB
261 static void kgdb_port_init(void);
262 #endif
263
264 bs_protos(bs_notimpl);
265
266 #include "com.h"
267 #if NCOM > 0
268 #include <dev/ic/comreg.h>
269 #include <dev/ic/comvar.h>
270 #endif
271
272 #include "lcd.h"
273
274 #ifndef CONSPEED
275 #define CONSPEED B115200 /* It's a setting of the default of u-boot */
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 extern void gxio_config_pin(void);
285 extern void gxio_config_expansion(char *);
286
287 /*
288 * void cpu_reboot(int howto, char *bootstr)
289 *
290 * Deal with any syncing, unmounting, dumping and shutdown hooks,
291 * then reset the CPU.
292 */
293 void
294 cpu_reboot(int howto, char *bootstr)
295 {
296
297 #ifdef DIAGNOSTIC
298 /* info */
299 printf("boot: howto=%08x curproc=%p\n", howto, curproc);
300 #endif
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 pmf_system_shutdown(boothowto);
309 printf("The operating system has halted.\n");
310 printf("Please press any key to reboot.\n\n");
311 cngetc();
312 printf("rebooting...\n");
313 cpu_reset();
314 /*NOTREACHED*/
315 }
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 pmf_system_shutdown(boothowto);
338
339 /* Make sure IRQ's are disabled */
340 IRQdisable;
341
342 if (howto & RB_HALT) {
343 printf("The operating system has halted.\n");
344 printf("Please press any key to reboot.\n\n");
345 cngetc();
346 }
347
348 printf("rebooting...\n");
349 cpu_reset();
350 /*NOTREACHED*/
351 }
352
353 static inline
354 pd_entry_t *
355 read_ttb(void)
356 {
357 long ttb;
358
359 __asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
360
361
362 return (pd_entry_t *)(ttb & ~((1<<14)-1));
363 }
364
365 /*
366 * Static device mappings. These peripheral registers are mapped at
367 * fixed virtual addresses very early in initarm() so that we can use
368 * them while booting the kernel, and stay at the same address
369 * throughout whole kernel's life time.
370 *
371 * We use this table twice; once with bootstrap page table, and once
372 * with kernel's page table which we build up in initarm().
373 *
374 * Since we map these registers into the bootstrap page table using
375 * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
376 * registers segment-aligned and segment-rounded in order to avoid
377 * using the 2nd page tables.
378 */
379
380 #define _A(a) ((a) & ~L1_S_OFFSET)
381 #define _S(s) (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
382
383 static const struct pmap_devmap gumstix_devmap[] = {
384 {
385 GUMSTIX_GPIO_VBASE,
386 _A(PXA2X0_GPIO_BASE),
387 _S(PXA250_GPIO_SIZE),
388 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
389 },
390 {
391 GUMSTIX_CLKMAN_VBASE,
392 _A(PXA2X0_CLKMAN_BASE),
393 _S(PXA2X0_CLKMAN_SIZE),
394 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
395 },
396 {
397 GUMSTIX_INTCTL_VBASE,
398 _A(PXA2X0_INTCTL_BASE),
399 _S(PXA2X0_INTCTL_SIZE),
400 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
401 },
402 {
403 GUMSTIX_FFUART_VBASE,
404 _A(PXA2X0_FFUART_BASE),
405 _S(4 * COM_NPORTS),
406 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
407 },
408 {
409 GUMSTIX_STUART_VBASE,
410 _A(PXA2X0_STUART_BASE),
411 _S(4 * COM_NPORTS),
412 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
413 },
414 {
415 GUMSTIX_BTUART_VBASE,
416 _A(PXA2X0_BTUART_BASE),
417 _S(4 * COM_NPORTS),
418 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
419 },
420 {
421 GUMSTIX_HWUART_VBASE,
422 _A(PXA2X0_HWUART_BASE),
423 _S(4 * COM_NPORTS),
424 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
425 },
426 {
427 GUMSTIX_LCDC_VBASE,
428 _A(PXA2X0_LCDC_BASE),
429 _S(4 * COM_NPORTS),
430 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
431 },
432 {0, 0, 0, 0, 0}
433 };
434
435 #undef _A
436 #undef _S
437
438
439 /*
440 * u_int initarm(...)
441 *
442 * Initial entry point on startup. This gets called before main() is
443 * entered.
444 * It should be responsible for setting up everything that must be
445 * in place when main is called.
446 * This includes
447 * Taking a copy of the boot configuration structure.
448 * Initialising the physical console so characters can be printed.
449 * Setting up page tables for the kernel
450 * Relocating the kernel to the bottom of physical memory
451 */
452 u_int
453 initarm(void *arg)
454 {
455 extern vaddr_t xscale_cache_clean_addr;
456 extern uint32_t *u_boot_args[];
457 extern uint32_t ram_size;
458 enum { r0 = 0, r1 = 1, r2 = 2, r3 = 3 }; /* args from u-boot */
459 int loop;
460 int loop1;
461 u_int l1pagetable;
462 paddr_t memstart;
463 psize_t memsize;
464 #ifdef DIAGNOSTIC
465 extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
466 #endif
467
468 /* map some peripheral registers at static I/O area */
469 pmap_devmap_bootstrap((vaddr_t)read_ttb(), gumstix_devmap);
470
471 /* start 32.768kHz OSC */
472 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_OSCC, OSCC_OON);
473
474 /* Get ready for splfoo() */
475 pxa2x0_intr_bootstrap(GUMSTIX_INTCTL_VBASE);
476
477 /*
478 * Heads up ... Setup the CPU / MMU / TLB functions
479 */
480 if (set_cpufuncs())
481 panic("cpu not recognized!");
482
483 /*
484 * U-Boot doesn't use the virtual memory.
485 *
486 * Physical Address Range Description
487 * ----------------------- ----------------------------------
488 * 0x00000000 - 0x00ffffff flash Memory (16MB or 4MB)
489 * 0x40000000 - 0x480fffff Processor Registers
490 * 0xa0000000 - 0xa3ffffff SDRAM Bank 0 (64MB)
491 */
492
493 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
494
495 /* setup GPIO for {FF,ST,HW}UART. */
496 pxa2x0_gpio_bootstrap(GUMSTIX_GPIO_VBASE);
497
498 /* configure GPIOs. */
499 gxio_config_pin();
500
501 consinit();
502 #ifdef KGDB
503 kgdb_port_init();
504 #endif
505
506 /* Talk to the user */
507 printf("\nNetBSD/evbarm (gumstix) booting ...\n");
508
509 /* Read system serial */
510 read_system_serial();
511
512 /*
513 * Examine the boot args string for options we need to know about
514 * now.
515 */
516 #define SDRAM_START 0xa0000000UL
517 if (((uint32_t)u_boot_args[r0] & 0xf0000000) != SDRAM_START)
518 /* Maybe r0 is 'argc'. We are booted by command 'go'. */
519 process_kernel_args((int)u_boot_args[r0],
520 (char **)u_boot_args[r1]);
521 else
522 /*
523 * Maybe r3 is 'boot args string' of 'bootm'. This string is
524 * linely.
525 */
526 process_kernel_args_line((char *)u_boot_args[r3]);
527
528 memstart = SDRAM_START;
529 memsize = ram_size;
530
531 #ifdef VERBOSE_INIT_ARM
532 printf("initarm: Configuring system ...\n");
533 #endif
534
535 /* Fake bootconfig structure for the benefit of pmap.c */
536 /* XXX must make the memory description h/w independent */
537 bootconfig.dramblocks = 1;
538 bootconfig.dram[0].address = memstart;
539 bootconfig.dram[0].pages = memsize / PAGE_SIZE;
540
541 /*
542 * Set up the variables that define the availablilty of
543 * physical memory. For now, we're going to set
544 * physical_freestart to 0xa0200000 (where the kernel
545 * was loaded), and allocate the memory we need downwards.
546 * If we get too close to the L1 table that we set up, we
547 * will panic. We will update physical_freestart and
548 * physical_freeend later to reflect what pmap_bootstrap()
549 * wants to see.
550 *
551 * XXX pmap_bootstrap() needs an enema.
552 */
553 physical_start = bootconfig.dram[0].address;
554 physical_end = physical_start + memsize;
555
556 physical_freestart = 0xa0009000UL;
557 physical_freeend = 0xa0200000UL;
558
559 physmem = (physical_end - physical_start) / PAGE_SIZE;
560
561 #ifdef VERBOSE_INIT_ARM
562 /* Tell the user about the memory */
563 printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
564 physical_start, physical_end - 1);
565 #endif
566
567 /*
568 * Okay, the kernel starts 2MB in from the bottom of physical
569 * memory. We are going to allocate our bootstrap pages downwards
570 * from there.
571 *
572 * We need to allocate some fixed page tables to get the kernel
573 * going. We allocate one page directory and a number of page
574 * tables and store the physical addresses in the kernel_pt_table
575 * array.
576 *
577 * The kernel page directory must be on a 16K boundary. The page
578 * tables must be on 4K bounaries. What we do is allocate the
579 * page directory on the first 16K boundary that we encounter, and
580 * the page tables on 4K boundaries otherwise. Since we allocate
581 * at least 3 L2 page tables, we are guaranteed to encounter at
582 * least one 16K aligned region.
583 */
584
585 #ifdef VERBOSE_INIT_ARM
586 printf("Allocating page tables\n");
587 #endif
588
589 free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
590
591 #ifdef VERBOSE_INIT_ARM
592 printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
593 physical_freestart, free_pages, free_pages);
594 #endif
595
596 /* Define a macro to simplify memory allocation */
597 #define valloc_pages(var, np) \
598 alloc_pages((var).pv_pa, (np)); \
599 (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
600
601 #define alloc_pages(var, np) \
602 physical_freeend -= ((np) * PAGE_SIZE); \
603 if (physical_freeend < physical_freestart) \
604 panic("initarm: out of memory"); \
605 (var) = physical_freeend; \
606 free_pages -= (np); \
607 memset((char *)(var), 0, ((np) * PAGE_SIZE));
608
609 loop1 = 0;
610 for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
611 /* Are we 16KB aligned for an L1 ? */
612 if ((physical_freeend & (L1_TABLE_SIZE - 1)) == 0 &&
613 kernel_l1pt.pv_pa == 0) {
614 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
615 } else {
616 valloc_pages(kernel_pt_table[loop1],
617 L2_TABLE_SIZE / PAGE_SIZE);
618 ++loop1;
619 }
620 }
621
622 /* This should never be able to happen but better confirm that. */
623 if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
624 panic("initarm: Failed to align the kernel page directory");
625
626 /*
627 * Allocate a page for the system page mapped to V0x00000000
628 * This page will just contain the system vectors and can be
629 * shared by all processes.
630 */
631 alloc_pages(systempage.pv_pa, 1);
632
633 /* Allocate stacks for all modes */
634 valloc_pages(irqstack, IRQ_STACK_SIZE);
635 valloc_pages(abtstack, ABT_STACK_SIZE);
636 valloc_pages(undstack, UND_STACK_SIZE);
637 valloc_pages(kernelstack, UPAGES);
638
639 /* Allocate enough pages for cleaning the Mini-Data cache. */
640 KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
641 valloc_pages(minidataclean, 1);
642
643 #ifdef VERBOSE_INIT_ARM
644 printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
645 irqstack.pv_va);
646 printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
647 abtstack.pv_va);
648 printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
649 undstack.pv_va);
650 printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
651 kernelstack.pv_va);
652 #endif
653
654 /*
655 * XXX Defer this to later so that we can reclaim the memory
656 * XXX used by the RedBoot page tables.
657 */
658 alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
659
660 /*
661 * Ok we have allocated physical pages for the primary kernel
662 * page tables
663 */
664
665 #ifdef VERBOSE_INIT_ARM
666 printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
667 #endif
668
669 /*
670 * Now we start construction of the L1 page table
671 * We start by mapping the L2 page tables into the L1.
672 * This means that we can replace L1 mappings later on if necessary
673 */
674 l1pagetable = kernel_l1pt.pv_va;
675
676 /* Map the L2 pages tables in the L1 page table */
677 pmap_link_l2pt(l1pagetable, 0x00000000,
678 &kernel_pt_table[KERNEL_PT_SYS]);
679 for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
680 pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
681 &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
682 for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
683 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
684 &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
685
686 /* update the top of the kernel VM */
687 pmap_curmaxkvaddr =
688 KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
689
690 #ifdef VERBOSE_INIT_ARM
691 printf("Mapping kernel\n");
692 #endif
693
694 /* Now we fill in the L2 pagetable for the kernel static code/data */
695 {
696 extern char etext[], _end[];
697 size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
698 size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
699 u_int logical;
700
701 textsize = (textsize + PGOFSET) & ~PGOFSET;
702 totalsize = (totalsize + PGOFSET) & ~PGOFSET;
703
704 logical = 0x00200000; /* offset of kernel in RAM */
705
706 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
707 physical_start + logical, textsize,
708 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
709 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
710 physical_start + logical, totalsize - textsize,
711 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
712 }
713
714 #ifdef VERBOSE_INIT_ARM
715 printf("Constructing L2 page tables\n");
716 #endif
717
718 /* Map the stack pages */
719 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
720 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
721 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
722 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
723 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
724 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
725 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
726 UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
727
728 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
729 L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
730
731 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
732 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
733 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
734 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
735 }
736
737 /* Map the Mini-Data cache clean area. */
738 xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
739 minidataclean.pv_pa);
740
741 /* Map the vector page. */
742 #if 1
743 /* MULTI-ICE requires that page 0 is NC/NB so that it can download the
744 * cache-clean code there. */
745 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
746 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
747 #else
748 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
749 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
750 #endif
751
752 /*
753 * map integrated peripherals at same address in l1pagetable
754 * so that we can continue to use console.
755 */
756 pmap_devmap_bootstrap(l1pagetable, gumstix_devmap);
757
758 /*
759 * Give the XScale global cache clean code an appropriately
760 * sized chunk of unmapped VA space starting at 0xff000000
761 * (our device mappings end before this address).
762 */
763 xscale_cache_clean_addr = 0xff000000U;
764
765 /*
766 * Now we have the real page tables in place so we can switch to them.
767 * Once this is done we will be running with the REAL kernel page
768 * tables.
769 */
770
771 /*
772 * Update the physical_freestart/physical_freeend/free_pages
773 * variables.
774 */
775 {
776 extern char _end[];
777
778 physical_freestart = physical_start +
779 (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
780 KERNEL_BASE);
781 physical_freeend = physical_end;
782 free_pages =
783 (physical_freeend - physical_freestart) / PAGE_SIZE;
784 }
785
786 /* Switch tables */
787 #ifdef VERBOSE_INIT_ARM
788 printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
789 physical_freestart, free_pages, free_pages);
790 printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
791 #endif
792
793 setttb(kernel_l1pt.pv_pa);
794 cpu_tlb_flushID();
795 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
796
797 /*
798 * Moved from cpu_startup() as data_abort_handler() references
799 * this during uvm init
800 */
801 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
802
803 #ifdef VERBOSE_INIT_ARM
804 printf("bootstrap done.\n");
805 #endif
806
807 arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
808
809 /*
810 * Pages were allocated during the secondary bootstrap for the
811 * stacks for different CPU modes.
812 * We must now set the r13 registers in the different CPU modes to
813 * point to these stacks.
814 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
815 * of the stack memory.
816 */
817 #ifdef VERBOSE_INIT_ARM
818 printf("init subsystems: stacks ");
819 #endif
820
821 set_stackptr(PSR_IRQ32_MODE,
822 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
823 set_stackptr(PSR_ABT32_MODE,
824 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
825 set_stackptr(PSR_UND32_MODE,
826 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
827
828 /*
829 * Well we should set a data abort handler.
830 * Once things get going this will change as we will need a proper
831 * handler.
832 * Until then we will use a handler that just panics but tells us
833 * why.
834 * Initialisation of the vectors will just panic on a data abort.
835 * This just fills in a slighly better one.
836 */
837 #ifdef VERBOSE_INIT_ARM
838 printf("vectors ");
839 #endif
840 data_abort_handler_address = (u_int)data_abort_handler;
841 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
842 undefined_handler_address = (u_int)undefinedinstruction_bounce;
843
844 /* Initialise the undefined instruction handlers */
845 #ifdef VERBOSE_INIT_ARM
846 printf("undefined ");
847 #endif
848 undefined_init();
849
850 /* Load memory into UVM. */
851 #ifdef VERBOSE_INIT_ARM
852 printf("page ");
853 #endif
854 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
855 uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
856 atop(physical_freestart), atop(physical_freeend),
857 VM_FREELIST_DEFAULT);
858
859 /* Boot strap pmap telling it where the kernel page table is */
860 #ifdef VERBOSE_INIT_ARM
861 printf("pmap ");
862 #endif
863 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
864
865 #ifdef __HAVE_MEMORY_DISK__
866 md_root_setconf(memory_disk, sizeof memory_disk);
867 #endif
868
869 #ifdef BOOTHOWTO
870 boothowto |= BOOTHOWTO;
871 #endif
872
873 #ifdef KGDB
874 if (boothowto & RB_KDB) {
875 kgdb_debug_init = 1;
876 kgdb_connect(1);
877 }
878 #endif
879
880 #if NKSYMS || defined(DDB) || defined(MODULAR)
881 /* Firmware doesn't load symbols. */
882 ddb_init(0, NULL, NULL);
883 #endif
884
885 #ifdef DDB
886 db_machine_init();
887 if (boothowto & RB_KDB)
888 Debugger();
889 #endif
890
891 /* We return the new stack pointer address */
892 return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
893 }
894
895 static void
896 read_system_serial(void)
897 {
898 #define GUMSTIX_SYSTEM_SERIAL_ADDR 0
899 #define GUMSTIX_SYSTEM_SERIAL_SIZE 8
900 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
901 #define FLASH_OFFSET_USER_PROTECTION 0x85
902 #define FLASH_CMD_READ_ID 0x90
903 #define FLASH_CMD_RESET 0xff
904 int i;
905 char system_serial[GUMSTIX_SYSTEM_SERIAL_SIZE], *src;
906 char x;
907
908 src = (char *)(FLASH_OFFSET_USER_PROTECTION * 2 /*word*/);
909 *(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
910 memcpy(system_serial,
911 src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
912 *(volatile uint16_t *)0 = FLASH_CMD_RESET;
913
914 for (i = 1, x = system_serial[0]; i < sizeof (system_serial); i++)
915 x &= system_serial[i];
916 if (x == 0xff) {
917 src = (char *)(FLASH_OFFSET_INTEL_PROTECTION * 2 /*word*/);
918 *(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
919 memcpy(system_serial,
920 src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
921 *(volatile uint16_t *)0 = FLASH_CMD_RESET;
922
923 /*
924 * XXXX: Don't need ???
925 * gumstix_serial_hash(system_serial);
926 */
927 }
928 system_serial_high = system_serial[0] << 24 | system_serial[1] << 16 |
929 system_serial[2] << 8 | system_serial[3];
930 system_serial_low = system_serial[4] << 24 | system_serial[5] << 16 |
931 system_serial[6] << 8 | system_serial[7];
932
933 printf("system serial: 0x");
934 for (i = 0; i < sizeof (system_serial); i++)
935 printf("%02x", system_serial[i]);
936 printf("\n");
937 }
938
939 static const char busheader_name[] = "busheader=";
940 static void
941 process_kernel_args(int argc, char *argv[])
942 {
943 int gxio_configured = 0, i, j;
944
945 boothowto = 0;
946
947 for (i = 1, j = 0; i < argc; i++) {
948 if (!strncmp(argv[i], busheader_name, strlen(busheader_name))) {
949 /* configure for GPIOs of busheader side */
950 gxio_config_expansion(argv[i] + strlen(busheader_name));
951 gxio_configured = 1;
952 continue;
953 }
954 if (j == MAX_BOOT_STRING) {
955 *(bootargs + j) = '\0';
956 continue;
957 }
958 if (j != 0)
959 *(bootargs + j++) = ' ';
960 strncpy(bootargs + j, argv[i], MAX_BOOT_STRING - j);
961 j += strlen(argv[i]);
962 }
963 boot_args = bootargs;
964
965 parse_mi_bootargs(boot_args);
966
967 if (!gxio_configured)
968 gxio_config_expansion(NULL);
969 }
970
971 static void
972 process_kernel_args_line(char *args)
973 {
974 int i;
975 char expansion[256], *p, c;
976
977 boothowto = 0;
978
979 strncpy(bootargs, args, sizeof(bootargs));
980 p = strstr(bootargs, busheader_name);
981 if (p == NULL)
982 gxio_config_expansion(NULL);
983 else {
984 i = 0;
985 do {
986 c = *(p + strlen(busheader_name) + i);
987 if (c == ' ')
988 c = '\0';
989 expansion[i++] = c;
990 } while (c != '\0');
991 gxio_config_expansion(expansion);
992 strcpy(p, p + i);
993 }
994 boot_args = bootargs;
995
996 parse_mi_bootargs(boot_args);
997 }
998
999 #ifdef KGDB
1000 #ifndef KGDB_DEVNAME
1001 #define KGDB_DEVNAME "ffuart"
1002 #endif
1003 const char kgdb_devname[] = KGDB_DEVNAME;
1004
1005 #ifndef KGDB_DEVRATE
1006 #define KGDB_DEVRATE CONSPEED
1007 #endif
1008 int kgdb_devrate = KGDB_DEVRATE;
1009
1010 #if (NCOM > 0)
1011 #ifndef KGDB_DEVMODE
1012 #define KGDB_DEVMODE CONMODE
1013 #endif
1014 int comkgdbmode = KGDB_DEVMODE;
1015 #endif /* NCOM */
1016
1017 #endif /* KGDB */
1018
1019
1020 void
1021 consinit(void)
1022 {
1023 static int consinit_called = 0;
1024 #if defined(FFUARTCONSOLE) || defined(STUARTCONSOLE) || \
1025 defined(BTUARTCONSOLE) || defined(HWUARTCONSOLE)
1026 uint32_t ckenreg = ioreg_read(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN);
1027 #endif
1028
1029 if (consinit_called != 0)
1030 return;
1031
1032 consinit_called = 1;
1033
1034 #if NCOM > 0
1035
1036 #ifdef FFUARTCONSOLE
1037 #ifdef KGDB
1038 if (0 == strcmp(kgdb_devname, "ffuart")){
1039 /* port is reserved for kgdb */
1040 } else
1041 #endif
1042 {
1043 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
1044 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1045 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1046 ckenreg|CKEN_FFUART);
1047
1048 return;
1049 }
1050 }
1051 #endif /* FFUARTCONSOLE */
1052
1053 #ifdef STUARTCONSOLE
1054 #ifdef KGDB
1055 if (0 == strcmp(kgdb_devname, "stuart")) {
1056 /* port is reserved for kgdb */
1057 } else
1058 #endif
1059 {
1060 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_STUART_BASE,
1061 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1062 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1063 ckenreg|CKEN_STUART);
1064 return;
1065 }
1066 }
1067 #endif /* STUARTCONSOLE */
1068
1069 #ifdef BTUARTCONSOLE
1070 #ifdef KGDB
1071 if (0 == strcmp(kgdb_devname, "btuart")) {
1072 /* port is reserved for kgdb */
1073 } else
1074 #endif
1075 {
1076 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
1077 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1078 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1079 ckenreg|CKEN_BTUART);
1080 return;
1081 }
1082 }
1083 #endif /* BTUARTCONSOLE */
1084
1085 #ifdef HWUARTCONSOLE
1086 #ifdef KGDB
1087 if (0 == strcmp(kgdb_devname, "hwuart")) {
1088 /* port is reserved for kgdb */
1089 } else
1090 #endif
1091 {
1092 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_HWUART_BASE,
1093 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1094 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1095 ckenreg|CKEN_HWUART);
1096 return;
1097 }
1098 }
1099 #endif /* HWUARTCONSOLE */
1100
1101 #endif /* NCOM */
1102
1103 #if NLCD > 0
1104 gxlcd_cnattach();
1105 #endif
1106 }
1107
1108 #ifdef KGDB
1109 static void
1110 kgdb_port_init(void)
1111 {
1112 #if (NCOM > 0) && defined(COM_PXA2X0)
1113 paddr_t paddr = 0;
1114 uint32_t ckenreg = ioreg_read(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN);
1115
1116 if (0 == strcmp(kgdb_devname, "ffuart")) {
1117 paddr = PXA2X0_FFUART_BASE;
1118 ckenreg |= CKEN_FFUART;
1119 } else if (0 == strcmp(kgdb_devname, "stuart")) {
1120 paddr = PXA2X0_STUART_BASE;
1121 ckenreg |= CKEN_STUART;
1122 } else if (0 == strcmp(kgdb_devname, "btuart")) {
1123 paddr = PXA2X0_BTUART_BASE;
1124 ckenreg |= CKEN_BTUART;
1125 } else if (0 == strcmp(kgdb_devname, "hwuart")) {
1126 paddr = PXA2X0_HWUART_BASE;
1127 ckenreg |= CKEN_HWUART;
1128 }
1129
1130 if (paddr &&
1131 0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
1132 kgdb_devrate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
1133
1134 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN, ckenreg);
1135
1136 }
1137
1138 #endif
1139 }
1140 #endif
1141