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