gumstix_machdep.c revision 1.6 1 /* $NetBSD: gumstix_machdep.c,v 1.6 2007/09/16 14:04:16 kiyohara Exp $ */
2 /*
3 * Copyright (C) 2005, 2006, 2007 WIDE Project and SOUM Corporation.
4 * All rights reserved.
5 *
6 * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
7 * Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the name of SOUM Corporation
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT and SOUM CORPORATION ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT AND SOUM CORPORATION
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 2002, 2003, 2004, 2005 Genetec Corporation.
35 * All rights reserved.
36 *
37 * Written by Hiroyuki Bessho for Genetec Corporation.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The name of Genetec Corporation may not be used to endorse or
48 * promote products derived from this software without specific prior
49 * written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 *
63 * Machine dependant functions for kernel setup for Genetec G4250EBX
64 * evaluation board.
65 *
66 * Based on iq80310_machhdep.c
67 */
68 /*
69 * Copyright (c) 2001 Wasabi Systems, Inc.
70 * All rights reserved.
71 *
72 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
73 *
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
76 * are met:
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 * notice, this list of conditions and the following disclaimer in the
81 * documentation and/or other materials provided with the distribution.
82 * 3. All advertising materials mentioning features or use of this software
83 * must display the following acknowledgement:
84 * This product includes software developed for the NetBSD Project by
85 * Wasabi Systems, Inc.
86 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
87 * or promote products derived from this software without specific prior
88 * written permission.
89 *
90 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
91 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
92 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
93 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
94 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
95 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
96 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
97 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
98 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
99 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
100 * POSSIBILITY OF SUCH DAMAGE.
101 */
102
103 /*
104 * Copyright (c) 1997,1998 Mark Brinicombe.
105 * Copyright (c) 1997,1998 Causality Limited.
106 * All rights reserved.
107 *
108 * Redistribution and use in source and binary forms, with or without
109 * modification, are permitted provided that the following conditions
110 * are met:
111 * 1. Redistributions of source code must retain the above copyright
112 * notice, this list of conditions and the following disclaimer.
113 * 2. Redistributions in binary form must reproduce the above copyright
114 * notice, this list of conditions and the following disclaimer in the
115 * documentation and/or other materials provided with the distribution.
116 * 3. All advertising materials mentioning features or use of this software
117 * must display the following acknowledgement:
118 * This product includes software developed by Mark Brinicombe
119 * for the NetBSD Project.
120 * 4. The name of the company nor the name of the author may be used to
121 * endorse or promote products derived from this software without specific
122 * prior written permission.
123 *
124 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
125 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
126 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
127 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
128 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
129 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
130 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
131 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
132 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
133 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
134 * SUCH DAMAGE.
135 *
136 * Machine dependant functions for kernel setup for Intel IQ80310 evaluation
137 * boards using RedBoot firmware.
138 */
139
140 #include "opt_ddb.h"
141 #include "opt_kgdb.h"
142 #include "opt_ipkdb.h"
143 #include "opt_pmap_debug.h"
144 #include "opt_md.h"
145 #include "opt_com.h"
146 #include "md.h"
147
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 systempage;
240 pv_addr_t irqstack;
241 pv_addr_t undstack;
242 pv_addr_t abtstack;
243 pv_addr_t kernelstack;
244 pv_addr_t minidataclean;
245
246 vm_offset_t msgbufphys;
247
248 extern u_int data_abort_handler_address;
249 extern u_int prefetch_abort_handler_address;
250 extern u_int undefined_handler_address;
251
252 #ifdef PMAP_DEBUG
253 extern int pmap_debug_level;
254 #endif
255
256 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
257 #define KERNEL_PT_KERNEL 1 /* Page table for mapping kernel */
258 #define KERNEL_PT_KERNEL_NUM 4
259 #define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
260 /* Page tables for mapping kernel VM */
261 #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
262 #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
263
264 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
265
266 struct user *proc0paddr;
267
268 /* Prototypes */
269 static void read_system_serial(void);
270 static void process_kernel_args(int, char *[]);
271 #ifdef KGDB
272 static void kgdb_port_init(void);
273 #endif
274
275 bs_protos(bs_notimpl);
276
277 #include "com.h"
278 #if NCOM > 0
279 #include <dev/ic/comreg.h>
280 #include <dev/ic/comvar.h>
281 #endif
282
283 #ifndef CONSPEED
284 #define CONSPEED B115200 /* It's a setting of the default of u-boot */
285 #endif
286 #ifndef CONMODE
287 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
288 #endif
289
290 int comcnspeed = CONSPEED;
291 int comcnmode = CONMODE;
292
293 extern void gxio_config_pin(void);
294 extern void gxio_config_expansion(char *);
295
296 /*
297 * void cpu_reboot(int howto, char *bootstr)
298 *
299 * Deal with any syncing, unmounting, dumping and shutdown hooks,
300 * then reset the CPU.
301 */
302 void
303 cpu_reboot(int howto, char *bootstr)
304 {
305
306 #ifdef DIAGNOSTIC
307 /* info */
308 printf("boot: howto=%08x curproc=%p\n", howto, curproc);
309 #endif
310
311 /*
312 * If we are still cold then hit the air brakes
313 * and crash to earth fast
314 */
315 if (cold) {
316 doshutdownhooks();
317 printf("The operating system has halted.\n");
318 printf("Please press any key to reboot.\n\n");
319 cngetc();
320 printf("rebooting...\n");
321 cpu_reset();
322 /*NOTREACHED*/
323 }
324
325 /*
326 * If RB_NOSYNC was not specified sync the discs.
327 * Note: Unless cold is set to 1 here, syslogd will die during the
328 * unmount. It looks like syslogd is getting woken up only to find
329 * that it cannot page part of the binary in as the filesystem has
330 * been unmounted.
331 */
332 if (!(howto & RB_NOSYNC))
333 bootsync();
334
335 /* Say NO to interrupts */
336 splhigh();
337
338 /* Do a dump if requested. */
339 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
340 dumpsys();
341
342 /* Run any shutdown hooks */
343 doshutdownhooks();
344
345 /* Make sure IRQ's are disabled */
346 IRQdisable;
347
348 if (howto & RB_HALT) {
349 printf("The operating system has halted.\n");
350 printf("Please press any key to reboot.\n\n");
351 cngetc();
352 }
353
354 printf("rebooting...\n");
355 cpu_reset();
356 /*NOTREACHED*/
357 }
358
359 static inline
360 pd_entry_t *
361 read_ttb(void)
362 {
363 long ttb;
364
365 __asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
366
367
368 return (pd_entry_t *)(ttb & ~((1<<14)-1));
369 }
370
371 /*
372 * Static device mappings. These peripheral registers are mapped at
373 * fixed virtual addresses very early in initarm() so that we can use
374 * them while booting the kernel, and stay at the same address
375 * throughout whole kernel's life time.
376 *
377 * We use this table twice; once with bootstrap page table, and once
378 * with kernel's page table which we build up in initarm().
379 *
380 * Since we map these registers into the bootstrap page table using
381 * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
382 * registers segment-aligned and segment-rounded in order to avoid
383 * using the 2nd page tables.
384 */
385
386 #define _A(a) ((a) & ~L1_S_OFFSET)
387 #define _S(s) (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
388
389 static const struct pmap_devmap gumstix_devmap[] = {
390 {
391 GUMSTIX_GPIO_VBASE,
392 _A(PXA2X0_GPIO_BASE),
393 _S(PXA250_GPIO_SIZE),
394 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
395 },
396 {
397 GUMSTIX_CLKMAN_VBASE,
398 _A(PXA2X0_CLKMAN_BASE),
399 _S(PXA2X0_CLKMAN_SIZE),
400 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
401 },
402 {
403 GUMSTIX_INTCTL_VBASE,
404 _A(PXA2X0_INTCTL_BASE),
405 _S(PXA2X0_INTCTL_SIZE),
406 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
407 },
408 {
409 GUMSTIX_FFUART_VBASE,
410 _A(PXA2X0_FFUART_BASE),
411 _S(4 * COM_NPORTS),
412 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
413 },
414 {
415 GUMSTIX_STUART_VBASE,
416 _A(PXA2X0_STUART_BASE),
417 _S(4 * COM_NPORTS),
418 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
419 },
420 {
421 GUMSTIX_BTUART_VBASE,
422 _A(PXA2X0_BTUART_BASE),
423 _S(4 * COM_NPORTS),
424 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
425 },
426 {
427 GUMSTIX_HWUART_VBASE,
428 _A(PXA2X0_HWUART_BASE),
429 _S(4 * COM_NPORTS),
430 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE,
431 },
432 {0, 0, 0, 0, 0}
433 };
434
435 #undef _A
436 #undef _S
437
438
439 /*
440 * u_int initarm(...)
441 *
442 * Initial entry point on startup. This gets called before main() is
443 * entered.
444 * It should be responsible for setting up everything that must be
445 * in place when main is called.
446 * This includes
447 * Taking a copy of the boot configuration structure.
448 * Initialising the physical console so characters can be printed.
449 * Setting up page tables for the kernel
450 * Relocating the kernel to the bottom of physical memory
451 */
452 u_int
453 initarm(void *arg)
454 {
455 extern vaddr_t xscale_cache_clean_addr;
456 extern uint32_t *u_boot_args[];
457 enum { r3 = 0, r4 = 1, r5 = 2, r6 = 3 }; /* args from u-boot */
458 int loop;
459 int loop1;
460 u_int l1pagetable;
461 pv_addr_t kernel_l1pt;
462 paddr_t memstart;
463 psize_t memsize;
464 #ifdef DIAGNOSTIC
465 extern vsize_t xscale_minidata_clean_size; /* used in KASSERT */
466 #endif
467
468 /* map some peripheral registers at static I/O area */
469 pmap_devmap_bootstrap((vaddr_t)read_ttb(), gumstix_devmap);
470
471 /* start 32.768kHz OSC */
472 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_OSCC, OSCC_OON);
473
474 /* Get ready for splfoo() */
475 pxa2x0_intr_bootstrap(GUMSTIX_INTCTL_VBASE);
476
477 /*
478 * Heads up ... Setup the CPU / MMU / TLB functions
479 */
480 if (set_cpufuncs())
481 panic("cpu not recognized!");
482
483 /*
484 * U-Boot doesn't use the virtual memory.
485 *
486 * Physical Address Range Description
487 * ----------------------- ----------------------------------
488 * 0x00000000 - 0x00ffffff flash Memory (16MB or 4MB)
489 * 0x40000000 - 0x480fffff Processor Registers
490 * 0xa0000000 - 0xa3ffffff SDRAM Bank 0 (64MB)
491 */
492
493 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
494
495 /* setup GPIO for {FF,ST,HW}UART. */
496 pxa2x0_gpio_bootstrap(GUMSTIX_GPIO_VBASE);
497
498 /* configure GPIOs. */
499 gxio_config_pin();
500
501 consinit();
502 #ifdef KGDB
503 kgdb_port_init();
504 #endif
505
506 /* Talk to the user */
507 printf("\nNetBSD/evbarm (gumstix) booting ...\n");
508
509 /* Read system serial */
510 read_system_serial();
511
512 /*
513 * Examine the boot args string for options we need to know about
514 * now.
515 */
516 process_kernel_args((int)u_boot_args[r6], (char **)u_boot_args[r5]);
517
518 memstart = 0xa0000000UL;
519 memsize = 0x04000000UL; /* 64MB */
520
521 #ifdef VERBOSE_INIT_ARM
522 printf("initarm: Configuring system ...\n");
523 #endif
524
525 /* Fake bootconfig structure for the benefit of pmap.c */
526 /* XXX must make the memory description h/w independent */
527 bootconfig.dramblocks = 1;
528 bootconfig.dram[0].address = memstart;
529 bootconfig.dram[0].pages = memsize / PAGE_SIZE;
530
531 /*
532 * Set up the variables that define the availablilty of
533 * physical memory. For now, we're going to set
534 * physical_freestart to 0xa0200000 (where the kernel
535 * was loaded), and allocate the memory we need downwards.
536 * If we get too close to the L1 table that we set up, we
537 * will panic. We will update physical_freestart and
538 * physical_freeend later to reflect what pmap_bootstrap()
539 * wants to see.
540 *
541 * XXX pmap_bootstrap() needs an enema.
542 */
543 physical_start = bootconfig.dram[0].address;
544 physical_end = physical_start + memsize;
545
546 physical_freestart = 0xa0009000UL;
547 physical_freeend = 0xa0200000UL;
548
549 physmem = (physical_end - physical_start) / PAGE_SIZE;
550
551 #ifdef VERBOSE_INIT_ARM
552 /* Tell the user about the memory */
553 printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
554 physical_start, physical_end - 1);
555 #endif
556
557 /*
558 * Okay, the kernel starts 2MB in from the bottom of physical
559 * memory. We are going to allocate our bootstrap pages downwards
560 * from there.
561 *
562 * We need to allocate some fixed page tables to get the kernel
563 * going. We allocate one page directory and a number of page
564 * tables and store the physical addresses in the kernel_pt_table
565 * array.
566 *
567 * The kernel page directory must be on a 16K boundary. The page
568 * tables must be on 4K bounaries. What we do is allocate the
569 * page directory on the first 16K boundary that we encounter, and
570 * the page tables on 4K boundaries otherwise. Since we allocate
571 * at least 3 L2 page tables, we are guaranteed to encounter at
572 * least one 16K aligned region.
573 */
574
575 #ifdef VERBOSE_INIT_ARM
576 printf("Allocating page tables\n");
577 #endif
578
579 free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
580
581 #ifdef VERBOSE_INIT_ARM
582 printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
583 physical_freestart, free_pages, free_pages);
584 #endif
585
586 /* Define a macro to simplify memory allocation */
587 #define valloc_pages(var, np) \
588 alloc_pages((var).pv_pa, (np)); \
589 (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
590
591 #define alloc_pages(var, np) \
592 physical_freeend -= ((np) * PAGE_SIZE); \
593 if (physical_freeend < physical_freestart) \
594 panic("initarm: out of memory"); \
595 (var) = physical_freeend; \
596 free_pages -= (np); \
597 memset((char *)(var), 0, ((np) * PAGE_SIZE));
598
599 loop1 = 0;
600 kernel_l1pt.pv_pa = 0;
601 kernel_l1pt.pv_va = 0;
602 for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
603 /* Are we 16KB aligned for an L1 ? */
604 if ((physical_freeend & (L1_TABLE_SIZE - 1)) == 0 &&
605 kernel_l1pt.pv_pa == 0) {
606 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
607 } else {
608 valloc_pages(kernel_pt_table[loop1],
609 L2_TABLE_SIZE / PAGE_SIZE);
610 ++loop1;
611 }
612 }
613
614 /* This should never be able to happen but better confirm that. */
615 if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
616 panic("initarm: Failed to align the kernel page directory");
617
618 /*
619 * Allocate a page for the system page mapped to V0x00000000
620 * This page will just contain the system vectors and can be
621 * shared by all processes.
622 */
623 alloc_pages(systempage.pv_pa, 1);
624
625 /* Allocate stacks for all modes */
626 valloc_pages(irqstack, IRQ_STACK_SIZE);
627 valloc_pages(abtstack, ABT_STACK_SIZE);
628 valloc_pages(undstack, UND_STACK_SIZE);
629 valloc_pages(kernelstack, UPAGES);
630
631 /* Allocate enough pages for cleaning the Mini-Data cache. */
632 KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
633 valloc_pages(minidataclean, 1);
634
635 #ifdef VERBOSE_INIT_ARM
636 printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
637 irqstack.pv_va);
638 printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
639 abtstack.pv_va);
640 printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
641 undstack.pv_va);
642 printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
643 kernelstack.pv_va);
644 #endif
645
646 /*
647 * XXX Defer this to later so that we can reclaim the memory
648 * XXX used by the RedBoot page tables.
649 */
650 alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
651
652 /*
653 * Ok we have allocated physical pages for the primary kernel
654 * page tables
655 */
656
657 #ifdef VERBOSE_INIT_ARM
658 printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
659 #endif
660
661 /*
662 * Now we start construction of the L1 page table
663 * We start by mapping the L2 page tables into the L1.
664 * This means that we can replace L1 mappings later on if necessary
665 */
666 l1pagetable = kernel_l1pt.pv_va;
667
668 /* Map the L2 pages tables in the L1 page table */
669 pmap_link_l2pt(l1pagetable, 0x00000000,
670 &kernel_pt_table[KERNEL_PT_SYS]);
671 for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
672 pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
673 &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
674 for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
675 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
676 &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
677
678 /* update the top of the kernel VM */
679 pmap_curmaxkvaddr =
680 KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
681
682 #ifdef VERBOSE_INIT_ARM
683 printf("Mapping kernel\n");
684 #endif
685
686 /* Now we fill in the L2 pagetable for the kernel static code/data */
687 {
688 extern char etext[], _end[];
689 size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
690 size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
691 u_int logical;
692
693 textsize = (textsize + PGOFSET) & ~PGOFSET;
694 totalsize = (totalsize + PGOFSET) & ~PGOFSET;
695
696 logical = 0x00200000; /* offset of kernel in RAM */
697
698 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
699 physical_start + logical, textsize,
700 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
701 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
702 physical_start + logical, totalsize - textsize,
703 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
704 }
705
706 #ifdef VERBOSE_INIT_ARM
707 printf("Constructing L2 page tables\n");
708 #endif
709
710 /* Map the stack pages */
711 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
712 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
713 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
714 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
715 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
716 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
717 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
718 UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
719
720 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
721 L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
722
723 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
724 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
725 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
726 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
727 }
728
729 /* Map the Mini-Data cache clean area. */
730 xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
731 minidataclean.pv_pa);
732
733 /* Map the vector page. */
734 #if 1
735 /* MULTI-ICE requires that page 0 is NC/NB so that it can download the
736 * cache-clean code there. */
737 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
738 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
739 #else
740 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
741 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
742 #endif
743
744 /*
745 * map integrated peripherals at same address in l1pagetable
746 * so that we can continue to use console.
747 */
748 pmap_devmap_bootstrap(l1pagetable, gumstix_devmap);
749
750 /*
751 * Give the XScale global cache clean code an appropriately
752 * sized chunk of unmapped VA space starting at 0xff000000
753 * (our device mappings end before this address).
754 */
755 xscale_cache_clean_addr = 0xff000000U;
756
757 /*
758 * Now we have the real page tables in place so we can switch to them.
759 * Once this is done we will be running with the REAL kernel page
760 * tables.
761 */
762
763 /*
764 * Update the physical_freestart/physical_freeend/free_pages
765 * variables.
766 */
767 {
768 extern char _end[];
769
770 physical_freestart = physical_start +
771 (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
772 KERNEL_BASE);
773 physical_freeend = physical_end;
774 free_pages =
775 (physical_freeend - physical_freestart) / PAGE_SIZE;
776 }
777
778 /* Switch tables */
779 #ifdef VERBOSE_INIT_ARM
780 printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
781 physical_freestart, free_pages, free_pages);
782 printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
783 #endif
784
785 setttb(kernel_l1pt.pv_pa);
786 cpu_tlb_flushID();
787 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
788
789 /*
790 * Moved from cpu_startup() as data_abort_handler() references
791 * this during uvm init
792 */
793 proc0paddr = (struct user *)kernelstack.pv_va;
794 lwp0.l_addr = proc0paddr;
795
796 #ifdef VERBOSE_INIT_ARM
797 printf("bootstrap done.\n");
798 #endif
799
800 arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
801
802 /*
803 * Pages were allocated during the secondary bootstrap for the
804 * stacks for different CPU modes.
805 * We must now set the r13 registers in the different CPU modes to
806 * point to these stacks.
807 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
808 * of the stack memory.
809 */
810 #ifdef VERBOSE_INIT_ARM
811 printf("init subsystems: stacks ");
812 #endif
813
814 set_stackptr(PSR_IRQ32_MODE,
815 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
816 set_stackptr(PSR_ABT32_MODE,
817 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
818 set_stackptr(PSR_UND32_MODE,
819 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
820
821 /*
822 * Well we should set a data abort handler.
823 * Once things get going this will change as we will need a proper
824 * handler.
825 * Until then we will use a handler that just panics but tells us
826 * why.
827 * Initialisation of the vectors will just panic on a data abort.
828 * This just fills in a slighly better one.
829 */
830 #ifdef VERBOSE_INIT_ARM
831 printf("vectors ");
832 #endif
833 data_abort_handler_address = (u_int)data_abort_handler;
834 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
835 undefined_handler_address = (u_int)undefinedinstruction_bounce;
836
837 /* Initialise the undefined instruction handlers */
838 #ifdef VERBOSE_INIT_ARM
839 printf("undefined ");
840 #endif
841 undefined_init();
842
843 /* Load memory into UVM. */
844 #ifdef VERBOSE_INIT_ARM
845 printf("page ");
846 #endif
847 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
848 uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
849 atop(physical_freestart), atop(physical_freeend),
850 VM_FREELIST_DEFAULT);
851
852 /* Boot strap pmap telling it where the kernel page table is */
853 #ifdef VERBOSE_INIT_ARM
854 printf("pmap ");
855 #endif
856 pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
857 KERNEL_VM_BASE + KERNEL_VM_SIZE);
858
859 #ifdef __HAVE_MEMORY_DISK__
860 md_root_setconf(memory_disk, sizeof memory_disk);
861 #endif
862
863 #ifdef BOOTHOWTO
864 boothowto |= BOOTHOWTO;
865 #endif
866
867 #ifdef IPKDB
868 /* Initialise ipkdb */
869 ipkdb_init();
870 if (boothowto & RB_KDB)
871 ipkdb_connect(0);
872 #endif
873
874 #ifdef KGDB
875 if (boothowto & RB_KDB) {
876 kgdb_debug_init = 1;
877 kgdb_connect(1);
878 }
879 #endif
880
881 #if NKSYMS || defined(DDB) || defined(LKM)
882 /* Firmware doesn't load symbols. */
883 ddb_init(0, NULL, NULL);
884 #endif
885
886 #ifdef DDB
887 db_machine_init();
888 if (boothowto & RB_KDB)
889 Debugger();
890 #endif
891
892 /* We return the new stack pointer address */
893 return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
894 }
895
896 static void
897 read_system_serial()
898 {
899 #define GUMSTIX_SYSTEM_SERIAL_ADDR 0
900 #define GUMSTIX_SYSTEM_SERIAL_SIZE 8
901 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
902 #define FLASH_OFFSET_USER_PROTECTION 0x85
903 #define FLASH_CMD_READ_ID 0x90
904 #define FLASH_CMD_RESET 0xff
905 int i;
906 char system_serial[GUMSTIX_SYSTEM_SERIAL_SIZE], *src;
907 char x;
908
909 src = (char *)(FLASH_OFFSET_USER_PROTECTION * 2 /*word*/);
910 *(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
911 memcpy(system_serial,
912 src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
913 *(volatile uint16_t *)0 = FLASH_CMD_RESET;
914
915 for (i = 1, x = system_serial[0]; i < sizeof (system_serial); i++)
916 x &= system_serial[i];
917 if (x == 0xff) {
918 src = (char *)(FLASH_OFFSET_INTEL_PROTECTION * 2 /*word*/);
919 *(volatile uint16_t *)0 = FLASH_CMD_READ_ID;
920 memcpy(system_serial,
921 src + GUMSTIX_SYSTEM_SERIAL_ADDR, sizeof (system_serial));
922 *(volatile uint16_t *)0 = FLASH_CMD_RESET;
923
924 /*
925 * XXXX: Don't need ???
926 * gumstix_serial_hash(system_serial);
927 */
928 }
929 system_serial_high = system_serial[0] << 24 | system_serial[1] << 16 |
930 system_serial[2] << 8 | system_serial[3];
931 system_serial_low = system_serial[4] << 24 | system_serial[5] << 16 |
932 system_serial[6] << 8 | system_serial[7];
933
934 printf("system serial: 0x");
935 for (i = 0; i < sizeof (system_serial); i++)
936 printf("%02x", system_serial[i]);
937 printf("\n");
938 }
939
940 static void
941 process_kernel_args(int argc, char *argv[])
942 {
943 static const char busheader_name[] = "busheader=";
944 int gxio_configured = 0, i, j;
945
946 boothowto = 0;
947
948 /*
949 * XXXXX: The value of argc is wrong. The number of arguments is
950 * corrected in the do_go() of u-boot. However, it is not actually
951 * corrected.
952 */
953 argc --;
954
955 for (i = 1, j = 0; i < argc; i++) {
956 if (!strncmp(argv[i], busheader_name, strlen(busheader_name))) {
957 /* configure for GPIOs of busheader side */
958 gxio_config_expansion(argv[i] + strlen(busheader_name));
959 gxio_configured = 1;
960 continue;
961 }
962 if (j == MAX_BOOT_STRING) {
963 *(bootargs + j) = '\0';
964 continue;
965 }
966 if (j != 0)
967 *(bootargs + j++) = ' ';
968 strncpy(bootargs + j, argv[i], MAX_BOOT_STRING - j);
969 j += strlen(argv[i]);
970 }
971 boot_args = bootargs;
972
973 parse_mi_bootargs(boot_args);
974
975 if (!gxio_configured)
976 gxio_config_expansion(NULL);
977 }
978
979 #ifdef KGDB
980 #ifndef KGDB_DEVNAME
981 #define KGDB_DEVNAME "ffuart"
982 #endif
983 const char kgdb_devname[] = KGDB_DEVNAME;
984
985 #ifndef KGDB_DEVRATE
986 #define KGDB_DEVRATE CONSPEED
987 #endif
988 int kgdb_devrate = KGDB_DEVRATE;
989
990 #if (NCOM > 0)
991 #ifndef KGDB_DEVMODE
992 #define KGDB_DEVMODE CONMODE
993 #endif
994 int comkgdbmode = KGDB_DEVMODE;
995 #endif /* NCOM */
996
997 #endif /* KGDB */
998
999
1000 void
1001 consinit(void)
1002 {
1003 static int consinit_called = 0;
1004 uint32_t ckenreg = ioreg_read(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN);
1005
1006 if (consinit_called != 0)
1007 return;
1008
1009 consinit_called = 1;
1010
1011 #if NCOM > 0
1012
1013 #ifdef FFUARTCONSOLE
1014 #ifdef KGDB
1015 if (0 == strcmp(kgdb_devname, "ffuart")){
1016 /* port is reserved for kgdb */
1017 } else
1018 #endif
1019 {
1020 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
1021 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1022 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1023 ckenreg|CKEN_FFUART);
1024
1025 return;
1026 }
1027 }
1028 #endif /* FFUARTCONSOLE */
1029
1030 #ifdef STUARTCONSOLE
1031 #ifdef KGDB
1032 if (0 == strcmp(kgdb_devname, "stuart")) {
1033 /* port is reserved for kgdb */
1034 } else
1035 #endif
1036 {
1037 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_STUART_BASE,
1038 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1039 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1040 ckenreg|CKEN_STUART);
1041 return;
1042 }
1043 }
1044 #endif /* STUARTCONSOLE */
1045
1046 #ifdef BTUARTCONSOLE
1047 #ifdef KGDB
1048 if (0 == strcmp(kgdb_devname, "btuart")) {
1049 /* port is reserved for kgdb */
1050 } else
1051 #endif
1052 {
1053 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
1054 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1055 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1056 ckenreg|CKEN_BTUART);
1057 return;
1058 }
1059 }
1060 #endif /* BTUARTCONSOLE */
1061
1062 #ifdef HWUARTCONSOLE
1063 #ifdef KGDB
1064 if (0 == strcmp(kgdb_devname, "hwuart")) {
1065 /* port is reserved for kgdb */
1066 } else
1067 #endif
1068 {
1069 if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_HWUART_BASE,
1070 comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
1071 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN,
1072 ckenreg|CKEN_HWUART);
1073 return;
1074 }
1075 }
1076 #endif /* HWUARTCONSOLE */
1077
1078 #endif /* NCOM */
1079
1080 }
1081
1082 #ifdef KGDB
1083 static void
1084 kgdb_port_init(void)
1085 {
1086 #if (NCOM > 0) && defined(COM_PXA2X0)
1087 paddr_t paddr = 0;
1088 uint32_t ckenreg = ioreg_read(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN);
1089
1090 if (0 == strcmp(kgdb_devname, "ffuart")) {
1091 paddr = PXA2X0_FFUART_BASE;
1092 ckenreg |= CKEN_FFUART;
1093 } else if (0 == strcmp(kgdb_devname, "stuart")) {
1094 paddr = PXA2X0_STUART_BASE;
1095 ckenreg |= CKEN_STUART;
1096 } else if (0 == strcmp(kgdb_devname, "btuart")) {
1097 paddr = PXA2X0_BTUART_BASE;
1098 ckenreg |= CKEN_BTUART;
1099 } else if (0 == strcmp(kgdb_devname, "hwuart")) {
1100 paddr = PXA2X0_HWUART_BASE;
1101 ckenreg |= CKEN_HWUART;
1102 }
1103
1104 if (paddr &&
1105 0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
1106 kgdb_devrate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
1107
1108 ioreg_write(GUMSTIX_CLKMAN_VBASE + CLKMAN_CKEN, ckenreg);
1109
1110 }
1111
1112 #endif
1113 }
1114 #endif
1115