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