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