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