Home | History | Annotate | Line # | Download | only in ixdp425
ixdp425_machdep.c revision 1.45
      1 /*	$NetBSD: ixdp425_machdep.c,v 1.45 2020/04/18 11:00:40 skrll Exp $ */
      2 /*
      3  * Copyright (c) 2003
      4  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 /*
     29  * Copyright (c) 1997,1998 Mark Brinicombe.
     30  * Copyright (c) 1997,1998 Causality Limited.
     31  * All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  * 3. All advertising materials mentioning features or use of this software
     42  *    must display the following acknowledgement:
     43  *	This product includes software developed by Mark Brinicombe
     44  *	for the NetBSD Project.
     45  * 4. The name of the company nor the name of the author may be used to
     46  *    endorse or promote products derived from this software without specific
     47  *    prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     50  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     51  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     53  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     54  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     55  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  */
     61 
     62 /* Machine dependent functions for kernel setup for Intel IXP425 evaluation
     63  * boards using RedBoot firmware.
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: ixdp425_machdep.c,v 1.45 2020/04/18 11:00:40 skrll Exp $");
     68 
     69 #include "opt_arm_debug.h"
     70 #include "opt_console.h"
     71 #include "opt_ddb.h"
     72 #include "opt_kgdb.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/device.h>
     76 #include <sys/systm.h>
     77 #include <sys/kernel.h>
     78 #include <sys/exec.h>
     79 #include <sys/proc.h>
     80 #include <sys/msgbuf.h>
     81 #include <sys/reboot.h>
     82 #include <sys/termios.h>
     83 #include <sys/ksyms.h>
     84 #include <sys/bus.h>
     85 #include <sys/cpu.h>
     86 
     87 #include <uvm/uvm_extern.h>
     88 
     89 #include <dev/cons.h>
     90 
     91 #include <machine/db_machdep.h>
     92 #include <ddb/db_sym.h>
     93 #include <ddb/db_extern.h>
     94 
     95 #include <machine/bootconfig.h>
     96 #include <arm/locore.h>
     97 #include <arm/undefined.h>
     98 
     99 #include <arm/arm32/machdep.h>
    100 
    101 #include <arm/xscale/ixp425reg.h>
    102 #include <arm/xscale/ixp425var.h>
    103 #include <arm/xscale/ixp425_sipvar.h>
    104 
    105 #include <evbarm/ixdp425/ixdp425reg.h>
    106 
    107 #include "com.h"
    108 #if NCOM > 0
    109 #include <dev/ic/comreg.h>
    110 #include <dev/ic/comvar.h>
    111 #endif
    112 
    113 #include "ksyms.h"
    114 
    115 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    116 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    117 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    118 
    119 /*
    120  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    121  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    122  */
    123 #define	KERNEL_VM_SIZE		0x0C000000
    124 
    125 BootConfig bootconfig;		/* Boot config storage */
    126 char *boot_args = NULL;
    127 char *boot_file = NULL;
    128 
    129 vaddr_t physical_start;
    130 vaddr_t physical_freestart;
    131 vaddr_t physical_freeend;
    132 vaddr_t physical_end;
    133 u_int free_pages;
    134 
    135 /* Physical and virtual addresses for some global pages */
    136 pv_addr_t minidataclean;
    137 
    138 paddr_t msgbufphys;
    139 
    140 extern int end;
    141 
    142 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    143 
    144 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    145 #define	KERNEL_PT_KERNEL_NUM	4
    146 #define	KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    147 					/* L2 tables for mapping kernel VM */
    148 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
    149 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    150 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    151 
    152 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    153 
    154 /* Prototypes */
    155 
    156 void	consinit(void);
    157 u_int	cpu_get_control(void);
    158 
    159 /*
    160  * Define the default console speed for the board.  This is generally
    161  * what the firmware provided with the board defaults to.
    162  */
    163 #ifndef CONSPEED
    164 #define CONSPEED B115200
    165 #endif /* ! CONSPEED */
    166 
    167 #ifndef CONUNIT
    168 #define	CONUNIT	0
    169 #endif
    170 
    171 #ifndef CONMODE
    172 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
    173 #endif
    174 
    175 int comcnspeed = CONSPEED;
    176 int comcnmode = CONMODE;
    177 int comcnunit = CONUNIT;
    178 
    179 #if KGDB
    180 #ifndef KGDB_DEVNAME
    181 #error Must define KGDB_DEVNAME
    182 #endif
    183 const char kgdb_devname[] = KGDB_DEVNAME;
    184 
    185 #ifndef KGDB_DEVADDR
    186 #error Must define KGDB_DEVADDR
    187 #endif
    188 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    189 
    190 #ifndef KGDB_DEVRATE
    191 #define KGDB_DEVRATE	CONSPEED
    192 #endif
    193 int kgdb_devrate = KGDB_DEVRATE;
    194 
    195 #ifndef KGDB_DEVMODE
    196 #define KGDB_DEVMODE	CONMODE
    197 #endif
    198 int kgdb_devmode = KGDB_DEVMODE;
    199 #endif /* KGDB */
    200 
    201 /*
    202  * void cpu_reboot(int howto, char *bootstr)
    203  *
    204  * Reboots the system
    205  *
    206  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    207  * then reset the CPU.
    208  */
    209 void
    210 cpu_reboot(int howto, char *bootstr)
    211 {
    212 	uint32_t reg;
    213 
    214 #ifdef DIAGNOSTIC
    215 	/* info */
    216 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    217 #endif
    218 
    219 	/*
    220 	 * If we are still cold then hit the air brakes
    221 	 * and crash to earth fast
    222 	 */
    223 	if (cold) {
    224 		doshutdownhooks();
    225 		pmf_system_shutdown(boothowto);
    226 		printf("The operating system has halted.\n");
    227 		printf("Please press any key to reboot.\n\n");
    228 		cngetc();
    229 		printf("rebooting...\n");
    230 		goto reset;
    231 	}
    232 
    233 	/* Disable console buffering */
    234 
    235 	/*
    236 	 * If RB_NOSYNC was not specified sync the discs.
    237 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    238 	 * unmount.  It looks like syslogd is getting woken up only to find
    239 	 * that it cannot page part of the binary in as the filesystem has
    240 	 * been unmounted.
    241 	 */
    242 	if (!(howto & RB_NOSYNC))
    243 		bootsync();
    244 
    245 	/* Say NO to interrupts */
    246 	splhigh();
    247 
    248 	/* Do a dump if requested. */
    249 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    250 		dumpsys();
    251 
    252 	/* Run any shutdown hooks */
    253 	doshutdownhooks();
    254 
    255 	pmf_system_shutdown(boothowto);
    256 
    257 	/* Make sure IRQ's are disabled */
    258 	IRQdisable;
    259 
    260 	if (howto & RB_HALT) {
    261 		printf("The operating system has halted.\n");
    262 		printf("Please press any key to reboot.\n\n");
    263 		cngetc();
    264 	}
    265 
    266 	printf("rebooting...\n\r");
    267  reset:
    268 	/*
    269 	 * Make really really sure that all interrupts are disabled,
    270 	 */
    271 	(void) disable_interrupts(I32_bit|F32_bit);
    272 	IXPREG(IXP425_INT_ENABLE) = 0;
    273 
    274 	/*
    275 	 * Map the boot Flash device down at physical address 0.
    276 	 * This is safe since NetBSD runs out of an alias of
    277 	 * SDRAM at 0x10000000.
    278 	 */
    279 	reg = EXP_CSR_READ_4(ixpsip_softc, EXP_CNFG0_OFFSET);
    280 	reg |= EXP_CNFG0_MEM_MAP;
    281 	EXP_CSR_WRITE_4(ixpsip_softc, EXP_CNFG0_OFFSET, reg);
    282 
    283 	/*
    284 	 * Jump into the bootcode's reset vector
    285 	 *
    286 	 * XXX:
    287 	 * Redboot doesn't like the state in which we leave the PCI
    288 	 * ethernet card, and so fails to detect it on reboot. This
    289 	 * pretty much necessitates a hard reset/power cycle to be
    290 	 * able to download a new kernel image over ethernet.
    291 	 *
    292 	 * I suspect this is due to a bug in Redboot's i82557 driver.
    293 	 */
    294 	cpu_reset();
    295 
    296 	/* ...and if that didn't work, just croak. */
    297 	printf("RESET FAILED!\n");
    298 	for (;;);
    299 }
    300 
    301 /* Static device mappings. */
    302 static const struct pmap_devmap ixp425_devmap[] = {
    303 	/* Physical/Virtual address for I/O space */
    304     {
    305 	IXP425_IO_VBASE,
    306 	IXP425_IO_HWBASE,
    307 	IXP425_IO_SIZE,
    308 	VM_PROT_READ|VM_PROT_WRITE,
    309 	PTE_NOCACHE,
    310     },
    311 
    312 	/* Expansion Bus */
    313     {
    314 	IXP425_EXP_VBASE,
    315 	IXP425_EXP_HWBASE,
    316 	IXP425_EXP_SIZE,
    317 	VM_PROT_READ|VM_PROT_WRITE,
    318 	PTE_NOCACHE,
    319     },
    320 
    321 	/* IXP425 PCI Configuration */
    322     {
    323 	IXP425_PCI_VBASE,
    324 	IXP425_PCI_HWBASE,
    325 	IXP425_PCI_SIZE,
    326 	VM_PROT_READ|VM_PROT_WRITE,
    327 	PTE_NOCACHE,
    328     },
    329 
    330 	/* SDRAM Controller */
    331     {
    332 	IXP425_MCU_VBASE,
    333 	IXP425_MCU_HWBASE,
    334 	IXP425_MCU_SIZE,
    335 	VM_PROT_READ|VM_PROT_WRITE,
    336 	PTE_NOCACHE,
    337     },
    338 
    339 	/* PCI Memory Space */
    340     {
    341 	IXP425_PCI_MEM_VBASE,
    342 	IXP425_PCI_MEM_HWBASE,
    343 	IXP425_PCI_MEM_SIZE,
    344 	VM_PROT_READ|VM_PROT_WRITE,
    345 	PTE_NOCACHE,
    346     },
    347 
    348     {
    349 	0,
    350 	0,
    351 	0,
    352 	0,
    353 	0,
    354     }
    355 };
    356 
    357 /*
    358  * vaddr_t initarm(...)
    359  *
    360  * Initial entry point on startup. This gets called before main() is
    361  * entered.
    362  * It should be responsible for setting up everything that must be
    363  * in place when main is called.
    364  * This includes
    365  *   Taking a copy of the boot configuration structure.
    366  *   Initialising the physical console so characters can be printed.
    367  *   Setting up page tables for the kernel
    368  *   Relocating the kernel to the bottom of physical memory
    369  */
    370 vaddr_t
    371 initarm(void *arg)
    372 {
    373 	extern vaddr_t xscale_cache_clean_addr;
    374 #ifdef DIAGNOSTIC
    375 	extern vsize_t xscale_minidata_clean_size;
    376 #endif
    377 	int loop;
    378 	int loop1;
    379 	u_int kerneldatasize;
    380 	u_int l1pagetable;
    381 	u_int freemempos;
    382 
    383 	/*
    384 	 * Since we map v0xf0000000 == p0xc8000000, it's possible for
    385 	 * us to initialize the console now.
    386 	 */
    387 	consinit();
    388 
    389 #ifdef VERBOSE_INIT_ARM
    390 	/* Talk to the user */
    391 	printf("\nNetBSD/evbarm (Intel IXDP425) booting ...\n");
    392 #endif
    393 
    394 	/*
    395 	 * Heads up ... Setup the CPU / MMU / TLB functions
    396 	 */
    397 	if (set_cpufuncs())
    398 		panic("cpu not recognized!");
    399 
    400 	/* XXX overwrite bootconfig to hardcoded values */
    401 	bootconfig.dramblocks = 1;
    402 	bootconfig.dram[0].address = 0x10000000;
    403 	bootconfig.dram[0].pages = ixp425_sdram_size() / PAGE_SIZE;
    404 
    405 	kerneldatasize = (uint32_t)&end - (uint32_t)KERNEL_TEXT_BASE;
    406 
    407 #ifdef VERBOSE_INIT_ARM
    408         printf("kernsize=0x%x\n", kerneldatasize);
    409 #endif
    410         kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) + PAGE_SIZE * 8;
    411 
    412 	/*
    413 	 * Set up the variables that define the availablilty of
    414 	 * physical memory.  For now, we're going to set
    415 	 * physical_freestart to 0x10200000 (where the kernel
    416 	 * was loaded), and allocate the memory we need downwards.
    417 	 * If we get too close to the L1 table that we set up, we
    418 	 * will panic.  We will update physical_freestart and
    419 	 * physical_freeend later to reflect what pmap_bootstrap()
    420 	 * wants to see.
    421 	 *
    422 	 * XXX pmap_bootstrap() needs an enema.
    423 	 */
    424 	physical_start = bootconfig.dram[0].address;
    425 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    426 
    427 	physical_freestart = physical_start
    428                 + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
    429         physical_freeend = physical_end;
    430 
    431 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    432 
    433 	/* Tell the user about the memory */
    434 #ifdef VERBOSE_INIT_ARM
    435 	printf("physmemory: %"PRIuPSIZE" pages at 0x%08lx -> 0x%08lx\n", physmem,
    436 	    physical_start, physical_end - 1);
    437 
    438 	printf("Allocating page tables\n");
    439 #endif
    440 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    441 
    442 	freemempos = 0x10000000;
    443 
    444 #ifdef VERBOSE_INIT_ARM
    445         printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
    446                 physical_start, physical_end);
    447 #endif
    448 
    449 	/* Define a macro to simplify memory allocation */
    450 #define	valloc_pages(var, np)				\
    451 	alloc_pages((var).pv_pa, (np));			\
    452 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    453 
    454 #if 0
    455 #define alloc_pages(var, np)				\
    456 	physical_freeend -= ((np) * PAGE_SIZE);		\
    457 	if (physical_freeend < physical_freestart)	\
    458 		panic("initarm: out of memory");	\
    459 	(var) = physical_freeend;			\
    460 	free_pages -= (np);				\
    461 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    462 #else
    463 #define alloc_pages(var, np)				\
    464         (var) = freemempos;                             \
    465         memset((char *)(var), 0, ((np) * PAGE_SIZE));   \
    466         freemempos += (np) * PAGE_SIZE;
    467 #endif
    468 
    469 	loop1 = 0;
    470 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    471 		/* Are we 16KB aligned for an L1 ? */
    472 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    473 		    && kernel_l1pt.pv_pa == 0) {
    474 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    475 		} else {
    476 			valloc_pages(kernel_pt_table[loop1],
    477 			    L2_TABLE_SIZE / PAGE_SIZE);
    478 			++loop1;
    479 		}
    480 	}
    481 
    482 	/* This should never be able to happen but better confirm that. */
    483 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    484 		panic("initarm: Failed to align the kernel page directory");
    485 
    486 	/*
    487 	 * Allocate a page for the system page.
    488 	 * This page will just contain the system vectors and can be
    489 	 * shared by all processes.
    490 	 */
    491 	alloc_pages(systempage.pv_pa, 1);
    492 
    493 	/* Allocate stacks for all modes */
    494 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    495 	valloc_pages(abtstack, ABT_STACK_SIZE);
    496 	valloc_pages(undstack, UND_STACK_SIZE);
    497 	valloc_pages(kernelstack, UPAGES);
    498 
    499 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    500 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    501 	valloc_pages(minidataclean, 1);
    502 
    503 #ifdef VERBOSE_INIT_ARM
    504 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    505 	    irqstack.pv_va);
    506 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    507 	    abtstack.pv_va);
    508 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    509 	    undstack.pv_va);
    510 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    511 	    kernelstack.pv_va);
    512 #endif
    513 
    514 	/*
    515 	 * XXX Defer this to later so that we can reclaim the memory
    516 	 * XXX used by the RedBoot page tables.
    517 	 */
    518 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    519 
    520 	/*
    521 	 * Ok we have allocated physical pages for the primary kernel
    522 	 * page tables
    523 	 */
    524 
    525 #ifdef VERBOSE_INIT_ARM
    526 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    527 #endif
    528 
    529 	/*
    530 	 * Now we start construction of the L1 page table
    531 	 * We start by mapping the L2 page tables into the L1.
    532 	 * This means that we can replace L1 mappings later on if necessary
    533 	 */
    534 	l1pagetable = kernel_l1pt.pv_pa;
    535 
    536 	/* Map the L2 pages tables in the L1 page table */
    537 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    538 	    &kernel_pt_table[KERNEL_PT_SYS]);
    539 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    540 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    541 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    542 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    543 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    544 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    545 
    546 	/* update the top of the kernel VM */
    547 	pmap_curmaxkvaddr =
    548 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    549 
    550 	pmap_link_l2pt(l1pagetable, IXP425_IO_VBASE,
    551 	    &kernel_pt_table[KERNEL_PT_IO]);
    552 
    553 #ifdef VERBOSE_INIT_ARM
    554 	printf("Mapping kernel\n");
    555 #endif
    556 
    557 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    558 	{
    559 		extern char etext[], _end[];
    560 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    561 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    562 		u_int logical;
    563 
    564 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    565 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    566 
    567 		logical = 0x00200000;	/* offset of kernel in RAM */
    568 
    569 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    570 		    physical_start + logical, textsize,
    571 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    572 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    573 		    physical_start + logical, totalsize - textsize,
    574 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    575 	}
    576 
    577 #ifdef VERBOSE_INIT_ARM
    578 	printf("Constructing L2 page tables\n");
    579 #endif
    580 
    581 	/* Map the stack pages */
    582 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    583 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    584 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    585 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    586 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    587 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    588 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    589 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    590 
    591 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    592 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    593 
    594 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    595 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    596 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    597 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    598 	}
    599 
    600 	/* Map the Mini-Data cache clean area. */
    601 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    602 	    minidataclean.pv_pa);
    603 
    604 	/* Map the vector page. */
    605 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    606 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    607 
    608         /*
    609          * Map the IXP425 registers
    610          */
    611 	pmap_devmap_bootstrap(l1pagetable, ixp425_devmap);
    612 
    613 	/*
    614 	 * Give the XScale global cache clean code an appropriately
    615 	 * sized chunk of unmapped VA space starting at 0xff000000
    616 	 * (our device mappings end before this address).
    617 	 */
    618 	xscale_cache_clean_addr = 0xff000000U;
    619 
    620 	/*
    621 	 * Now we have the real page tables in place so we can switch to them.
    622 	 * Once this is done we will be running with the REAL kernel page
    623 	 * tables.
    624 	 */
    625 
    626 	/*
    627 	 * Update the physical_freestart/physical_freeend/free_pages
    628 	 * variables.
    629 	 */
    630 	{
    631 		extern char _end[];
    632 
    633 		physical_freestart = physical_start +
    634 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    635 		     KERNEL_BASE);
    636 		physical_freeend = physical_end;
    637 		free_pages =
    638 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    639 	}
    640 
    641 	/* Switch tables */
    642 #ifdef VERBOSE_INIT_ARM
    643 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    644 	       physical_freestart, free_pages, free_pages);
    645 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    646 #endif
    647 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    648 	cpu_setttb(kernel_l1pt.pv_pa, true);
    649 	cpu_tlb_flushID();
    650 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    651 
    652 	/*
    653 	 * Moved from cpu_startup() as data_abort_handler() references
    654 	 * this during uvm init
    655 	 */
    656 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    657 
    658 #ifdef VERBOSE_INIT_ARM
    659 	printf("bootstrap done.\n");
    660 #endif
    661 
    662 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    663 
    664 	/*
    665 	 * Pages were allocated during the secondary bootstrap for the
    666 	 * stacks for different CPU modes.
    667 	 * We must now set the r13 registers in the different CPU modes to
    668 	 * point to these stacks.
    669 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    670 	 * of the stack memory.
    671 	 */
    672 #ifdef VERBOSE_INIT_ARM
    673 	printf("init subsystems: stacks ");
    674 #endif
    675 
    676 	set_stackptr(PSR_IRQ32_MODE,
    677 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    678 	set_stackptr(PSR_ABT32_MODE,
    679 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    680 	set_stackptr(PSR_UND32_MODE,
    681 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    682 
    683 	/*
    684 	 * Well we should set a data abort handler.
    685 	 * Once things get going this will change as we will need a proper
    686 	 * handler.
    687 	 * Until then we will use a handler that just panics but tells us
    688 	 * why.
    689 	 * Initialisation of the vectors will just panic on a data abort.
    690 	 * This just fills in a slightly better one.
    691 	 */
    692 #ifdef VERBOSE_INIT_ARM
    693 	printf("vectors ");
    694 #endif
    695 	data_abort_handler_address = (u_int)data_abort_handler;
    696 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    697 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    698 
    699 	/* Initialise the undefined instruction handlers */
    700 #ifdef VERBOSE_INIT_ARM
    701 	printf("undefined ");
    702 #endif
    703 	undefined_init();
    704 
    705 	/* Load memory into UVM. */
    706 #ifdef VERBOSE_INIT_ARM
    707 	printf("page ");
    708 #endif
    709 	uvm_md_init();
    710 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    711 	    atop(physical_freestart), atop(physical_freeend),
    712 	    VM_FREELIST_DEFAULT);
    713 
    714 	/* Boot strap pmap telling it where managed kernel virtual memory is */
    715 #ifdef VERBOSE_INIT_ARM
    716 	printf("pmap ");
    717 #endif
    718 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    719 
    720 	/* Setup the IRQ system */
    721 #ifdef VERBOSE_INIT_ARM
    722 	printf("irq ");
    723 #endif
    724 	ixp425_intr_init();
    725 #ifdef VERBOSE_INIT_ARM
    726 	printf("\nAll initialization done!\nNow Starting NetBSD, Here we go!\n");
    727 #endif
    728 
    729 #ifdef BOOTHOWTO
    730 	boothowto = BOOTHOWTO;
    731 #endif
    732 
    733 #ifdef DDB
    734 	db_machine_init();
    735 	if (boothowto & RB_KDB)
    736 		Debugger();
    737 #endif
    738 
    739 	/* We return the new stack pointer address */
    740 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
    741 }
    742 
    743 /*
    744  * consinit
    745  */
    746 void
    747 consinit(void)
    748 {
    749 	static int consinit_called;
    750 	static const bus_addr_t addrs[2] = {
    751 		IXP425_UART0_HWBASE, IXP425_UART1_HWBASE
    752 	};
    753 
    754 	if (consinit_called != 0)
    755 		return;
    756 
    757 	consinit_called = 1;
    758 
    759 	pmap_devmap_register(ixp425_devmap);
    760 
    761 	if (comcnattach(&ixp425_a4x_bs_tag, addrs[comcnunit],
    762 	    comcnspeed, IXP425_UART_FREQ, COM_TYPE_PXA2x0, comcnmode))
    763 		panic("can't init serial console (UART%d)", comcnunit);
    764 }
    765