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