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