Home | History | Annotate | Line # | Download | only in armadillo
armadillo9_machdep.c revision 1.24
      1 /*	$NetBSD: armadillo9_machdep.c,v 1.24 2012/09/22 00:33:38 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Based on code written by Jason R. Thorpe and Steve C. Woodford for
      8  * Wasabi Systems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed for the NetBSD Project by
     21  *	Wasabi Systems, Inc.
     22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1997,1998 Mark Brinicombe.
     41  * Copyright (c) 1997,1998 Causality Limited.
     42  * All rights reserved.
     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 by Mark Brinicombe
     55  *	for the NetBSD Project.
     56  * 4. The name of the company nor the name of the author may be used to
     57  *    endorse or promote products derived from this software without specific
     58  *    prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     63  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  * Machine dependent functions for kernel setup for Armadillo.
     73  */
     74 
     75 /*	Armadillo-9 physical memory map
     76 	0000 0000 - 0fff ffff	reserved
     77 	1000 0000 - 1000 000f	I/O Control Register
     78 	1000 0010 - 11dd ffff	reserved
     79 	1200 0000 - 1200 ffff	PC/104 I/O space (8bit)
     80 	1201 0000 - 12ff ffff	reserved
     81 	1300 0000 - 13ff ffff	PC/104 Memory space (8bit)
     82 	1400 0000 - 1fff ffff	reserved
     83 	2000 0000 - 21ff ffff	reserved
     84 	2200 0000 - 2200 ffff	PC/104 I/O space (16bit)
     85 	2201 0000 - 22ff ffff	reserved
     86 	2300 0000 - 23ff ffff	PC/104 Memory space (16bit)
     87 	2400 0000 - 2fff ffff	reserved
     88 	3000 0000 - 3fff ffff	reserved
     89 	4000 0000 - 43ff ffff	Compact Flash I/O space
     90 	4400 0000 - 47ff ffff	reserved
     91 	4800 0000 - 4bff ffff	Compact Flash Attribute space
     92 	4c00 0000 - 4fff ffff	Compact Flash memory space
     93 	5000 0000 - 5fff ffff	reserved
     94 	6000 0000 - 607f ffff	Flash Memory (8MByte)
     95 	6080 0000 - 6fff ffff	reserved
     96 	7000 0000 - 7fff ffff	reserved
     97 	8000 0000 - 8008 ffff	EP9315 Internal Register (AHB)
     98 	8009 0000 - 8009 3fff	Internal Boot ROM (16kByte)
     99 	8009 4000 - 8009 ffff	reserved
    100 	800a 0000 - 800f ffff	EP9315 Internal Register (AHB)
    101 	8010 0000 - 807f ffff	reserved
    102 	8080 0000 - 8094 ffff	EP9315 Internal Register (APB)
    103 	8095 0000 - 8fff ffff	reserved
    104 	9000 0000 - bfff ffff	reserved
    105 	c000 0000 - c1ff ffff	SDRAM (32MByte)
    106 	c200 0000 - c3ff ffff	reserved
    107 	c400 0000 - c5ff ffff	SDRAM (32MByte)
    108 	c600 0000 - cfff ffff	reserved
    109 	d000 0000 - ffff ffff	reserved
    110 */
    111 
    112 #include <sys/cdefs.h>
    113 __KERNEL_RCSID(0, "$NetBSD: armadillo9_machdep.c,v 1.24 2012/09/22 00:33:38 matt Exp $");
    114 
    115 #include "opt_ddb.h"
    116 #include "opt_kgdb.h"
    117 #include "opt_pmap_debug.h"
    118 
    119 #include <sys/param.h>
    120 #include <sys/device.h>
    121 #include <sys/systm.h>
    122 #include <sys/kernel.h>
    123 #include <sys/exec.h>
    124 #include <sys/proc.h>
    125 #include <sys/msgbuf.h>
    126 #include <sys/reboot.h>
    127 #include <sys/termios.h>
    128 #include <sys/ksyms.h>
    129 
    130 #include <net/if.h>
    131 #include <net/if_ether.h>
    132 
    133 #include <uvm/uvm_extern.h>
    134 
    135 #include <dev/cons.h>
    136 
    137 #include <machine/db_machdep.h>
    138 #include <ddb/db_sym.h>
    139 #include <ddb/db_extern.h>
    140 
    141 #define	DRAM_BLOCKS	4
    142 #include <machine/bootconfig.h>
    143 #include <machine/autoconf.h>
    144 #include <sys/bus.h>
    145 #include <machine/cpu.h>
    146 #include <machine/frame.h>
    147 #include <arm/undefined.h>
    148 
    149 /* Define various stack sizes in pages */
    150 #define IRQ_STACK_SIZE	8
    151 #define ABT_STACK_SIZE	8
    152 #define UND_STACK_SIZE	8
    153 
    154 #include <arm/arm32/machdep.h>
    155 
    156 #include <arm/ep93xx/ep93xxreg.h>
    157 #include <arm/ep93xx/ep93xxvar.h>
    158 
    159 #include "epwdog.h"
    160 #if NEPWDOG > 0
    161 #include <arm/ep93xx/epwdogvar.h>
    162 #endif
    163 #include <arm/ep93xx/epwdogreg.h>
    164 
    165 #include <dev/ic/comreg.h>
    166 #include <dev/ic/comvar.h>
    167 
    168 #include "epcom.h"
    169 #if NEPCOM > 0
    170 #include <arm/ep93xx/epcomvar.h>
    171 #endif
    172 
    173 #include "isa.h"
    174 #if NISA > 0
    175 #include <dev/isa/isareg.h>
    176 #include <dev/isa/isavar.h>
    177 #endif
    178 
    179 #include <machine/isa_machdep.h>
    180 
    181 #include <evbarm/armadillo/armadillo9reg.h>
    182 #include <evbarm/armadillo/armadillo9var.h>
    183 
    184 struct armadillo_model_t *armadillo_model = 0;
    185 static struct armadillo_model_t armadillo_model_table[] = {
    186 	{ DEVCFG_ARMADILLO9, "Armadillo-9" },
    187 	{ DEVCFG_ARMADILLO210, "Armadillo-210" },
    188 	{ 0, "Armadillo(unknown model)" } };
    189 
    190 #include "ksyms.h"
    191 
    192 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    193 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    194 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    195 
    196 /*
    197  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    198  * Core-logic registers and I/O mappings occupy 0xf0000000 - 0xffffffff
    199  */
    200 #define KERNEL_VM_SIZE		0x0c000000
    201 
    202 
    203 BootConfig bootconfig;	/* Boot config storage */
    204 char *boot_args = NULL;
    205 char *boot_file = NULL;
    206 
    207 vm_offset_t physical_start;
    208 vm_offset_t physical_freestart;
    209 vm_offset_t physical_freeend;
    210 vm_offset_t physical_freeend_low;
    211 vm_offset_t physical_end;
    212 u_int free_pages;
    213 
    214 vm_offset_t msgbufphys;
    215 
    216 static struct arm32_dma_range armadillo9_dma_ranges[4];
    217 
    218 #if NISA > 0
    219 extern void isa_armadillo9_init(u_int, u_int);
    220 #endif
    221 
    222 #ifdef PMAP_DEBUG
    223 extern int pmap_debug_level;
    224 #endif
    225 
    226 #define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
    227 
    228 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    229 #define	KERNEL_PT_KERNEL_NUM	4
    230 					/* L2 tables for mapping kernel VM */
    231 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    232 
    233 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    234 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    235 
    236 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    237 
    238 /* Prototypes */
    239 
    240 void	consinit(void);
    241 /*
    242  * Define the default console speed for the machine.
    243  */
    244 #if NEPCOM > 0
    245 #ifndef CONSPEED
    246 #define CONSPEED B115200
    247 #endif /* ! CONSPEED */
    248 
    249 #ifndef CONMODE
    250 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    251 #endif
    252 
    253 #ifndef CONUNIT
    254 #define	CONUNIT	0
    255 #endif
    256 
    257 int comcnspeed = CONSPEED;
    258 int comcnmode = CONMODE;
    259 const unsigned long comaddr[] = {
    260 	EP93XX_APB_UART1, EP93XX_APB_UART2 };
    261 #endif
    262 
    263 #if KGDB
    264 #ifndef KGDB_DEVNAME
    265 #error Must define KGDB_DEVNAME
    266 #endif
    267 const char kgdb_devname[] = KGDB_DEVNAME;
    268 
    269 #ifndef KGDB_DEVADDR
    270 #error Must define KGDB_DEVADDR
    271 #endif
    272 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    273 
    274 #ifndef KGDB_DEVRATE
    275 #define KGDB_DEVRATE	CONSPEED
    276 #endif
    277 int kgdb_devrate = KGDB_DEVRATE;
    278 
    279 #ifndef KGDB_DEVMODE
    280 #define KGDB_DEVMODE	CONMODE
    281 #endif
    282 int kgdb_devmode = KGDB_DEVMODE;
    283 #endif /* KGDB */
    284 
    285 /*
    286  * MAC address for the built-in Ethernet.
    287  */
    288 uint8_t	armadillo9_ethaddr[ETHER_ADDR_LEN];
    289 
    290 static void
    291 armadillo9_device_register(device_t dev, void *aux)
    292 {
    293 
    294 	/* MAC address for the built-in Ethernet. */
    295 	if (device_is_a(dev, "epe")) {
    296 		prop_data_t pd = prop_data_create_data_nocopy(
    297 		    armadillo9_ethaddr, ETHER_ADDR_LEN);
    298 		KASSERT(pd != NULL);
    299 		if (prop_dictionary_set(device_properties(dev),
    300 					"mac-address", pd) == false) {
    301 			printf("WARNING: unable to set mac-addr property "
    302 			    "for %s\n", dev->dv_xname);
    303 		}
    304 		prop_object_release(pd);
    305 	}
    306 }
    307 
    308 /*
    309  * void cpu_reboot(int howto, char *bootstr)
    310  *
    311  * Reboots the system
    312  *
    313  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    314  * then reset the CPU.
    315  */
    316 void
    317 cpu_reboot(int howto, char *bootstr)
    318 {
    319 	/*
    320 	 * If we are still cold then hit the air brakes
    321 	 * and crash to earth fast
    322 	 */
    323 	if (cold) {
    324 		doshutdownhooks();
    325 		pmf_system_shutdown(boothowto);
    326 		printf("\r\n");
    327 		printf("The operating system has halted.\r\n");
    328 		printf("Please press any key to reboot.\r\n");
    329 		cngetc();
    330 		printf("\r\nrebooting...\r\n");
    331 		goto reset;
    332 	}
    333 
    334 	/* Disable console buffering */
    335 
    336 	/*
    337 	 * If RB_NOSYNC was not specified sync the discs.
    338 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    339 	 * unmount.  It looks like syslogd is getting woken up only to find
    340 	 * that it cannot page part of the binary in as the filesystem has
    341 	 * been unmounted.
    342 	 */
    343 	if (!(howto & RB_NOSYNC))
    344 		bootsync();
    345 
    346 	/* Say NO to interrupts */
    347 	splhigh();
    348 
    349 	/* Do a dump if requested. */
    350 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    351 		dumpsys();
    352 
    353 	/* Run any shutdown hooks */
    354 	doshutdownhooks();
    355 
    356 	pmf_system_shutdown(boothowto);
    357 
    358 	/* Make sure IRQ's are disabled */
    359 	IRQdisable;
    360 
    361 	if (howto & RB_HALT) {
    362 		printf("\r\n");
    363 		printf("The operating system has halted.\r\n");
    364 		printf("Please press any key to reboot.\r\n");
    365 		cngetc();
    366 	}
    367 
    368 	printf("\r\nrebooting...\r\n");
    369  reset:
    370 	/*
    371 	 * Make really really sure that all interrupts are disabled,
    372 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    373 	 */
    374 	(void) disable_interrupts(I32_bit|F32_bit);
    375 #if NEPWDOG > 0
    376 	epwdog_reset();
    377 #else
    378 	{
    379 	u_int32_t ctrl = EP93XX_APB_VBASE + EP93XX_APB_WDOG + EP93XX_WDOG_Ctrl;
    380 	u_int32_t val = EP93XX_WDOG_ENABLE;
    381 	__asm volatile (
    382 		"str %1, [%0]\n"
    383 		:
    384 		: "r" (ctrl), "r" (val)
    385 	);
    386 	}
    387 #endif
    388 	for (;;);
    389 }
    390 
    391 /* Static device mappings. */
    392 static const struct pmap_devmap armadillo9_devmap[] = {
    393     {
    394 	EP93XX_AHB_VBASE,
    395 	EP93XX_AHB_HWBASE,
    396 	EP93XX_AHB_SIZE,
    397 	VM_PROT_READ|VM_PROT_WRITE,
    398 	PTE_NOCACHE,
    399     },
    400 
    401     {
    402 	EP93XX_APB_VBASE,
    403 	EP93XX_APB_HWBASE,
    404 	EP93XX_APB_SIZE,
    405 	VM_PROT_READ|VM_PROT_WRITE,
    406 	PTE_NOCACHE,
    407     },
    408 
    409     {
    410 	EP93XX_PCMCIA0_VBASE,
    411 	EP93XX_PCMCIA0_HWBASE,
    412 	EP93XX_PCMCIA_SIZE,
    413 	VM_PROT_READ|VM_PROT_WRITE,
    414 	PTE_NOCACHE,
    415     },
    416 
    417 	/*
    418 	 * IO8 and IO16 space *must* be mapped contiguously with
    419 	 * IO8_VA == IO16_VA - 64 Mbytes.  ISA busmap driver depends
    420 	 * on that!
    421 	 */
    422     {
    423 	ARMADILLO9_IO8_VBASE,
    424 	ARMADILLO9_IO8_HWBASE,
    425 	ARMADILLO9_IO8_SIZE,
    426 	VM_PROT_READ|VM_PROT_WRITE,
    427 	PTE_NOCACHE,
    428     },
    429 
    430     {
    431 	ARMADILLO9_IO16_VBASE,
    432 	ARMADILLO9_IO16_HWBASE,
    433 	ARMADILLO9_IO16_SIZE,
    434 	VM_PROT_READ|VM_PROT_WRITE,
    435 	PTE_NOCACHE,
    436     },
    437 
    438    {
    439 	0,
    440 	0,
    441 	0,
    442 	0,
    443 	0,
    444     }
    445 };
    446 
    447 /*
    448  * u_int initarm(...)
    449  *
    450  * Initial entry point on startup. This gets called before main() is
    451  * entered.
    452  * It should be responsible for setting up everything that must be
    453  * in place when main is called.
    454  * This includes
    455  *   Taking a copy of the boot configuration structure.
    456  *   Initialising the physical console so characters can be printed.
    457  *   Setting up page tables for the kernel
    458  *   Initialising interrupt controllers to a sane default state
    459  */
    460 u_int
    461 initarm(void *arg)
    462 {
    463 	int loop;
    464 	int loop1;
    465 	u_int l1pagetable;
    466 	struct bootparam_tag *bootparam_p;
    467 	unsigned long devcfg;
    468 
    469 	/*
    470 	 * Since we map the on-board devices VA==PA, and the kernel
    471 	 * is running VA==PA, it's possible for us to initialize
    472 	 * the console now.
    473 	 */
    474 	consinit();
    475 
    476 	/* identify model */
    477 	devcfg = *((volatile unsigned long*)(EP93XX_APB_HWBASE
    478 					     + EP93XX_APB_SYSCON
    479 					     + EP93XX_SYSCON_DeviceCfg));
    480 	for (armadillo_model = &armadillo_model_table[0];
    481 				armadillo_model->devcfg; armadillo_model++)
    482 		if (devcfg == armadillo_model->devcfg)
    483 			break;
    484 
    485 	/* Talk to the user */
    486 	printf("\nNetBSD/%s booting ...\n", armadillo_model->name);
    487 
    488 	/* set some informations from bootloader */
    489 	bootparam_p = (struct bootparam_tag *)bootparam;
    490 	bootconfig.dramblocks = 0;
    491 	while (bootparam_p->hdr.tag != BOOTPARAM_TAG_NONE) {
    492 		switch (bootparam_p->hdr.tag) {
    493 		case BOOTPARAM_TAG_MEM:
    494 			if (bootconfig.dramblocks < DRAM_BLOCKS) {
    495 #ifdef VERBOSE_INIT_ARM
    496 			printf("dram[%d]: address=0x%08lx, size=0x%08lx\n",
    497 						bootconfig.dramblocks,
    498 						bootparam_p->u.mem.start,
    499 						bootparam_p->u.mem.size);
    500 #endif
    501 				bootconfig.dram[bootconfig.dramblocks].address =
    502 					bootparam_p->u.mem.start;
    503 				bootconfig.dram[bootconfig.dramblocks].pages =
    504 					bootparam_p->u.mem.size / PAGE_SIZE;
    505 				bootconfig.dramblocks++;
    506 			}
    507 			break;
    508 		case BOOTPARAM_TAG_CMDLINE:
    509 #ifdef VERBOSE_INIT_ARM
    510 			printf("cmdline: %s\n", bootparam_p->u.cmdline.cmdline);
    511 #endif
    512 			parse_mi_bootargs(bootparam_p->u.cmdline.cmdline);
    513 			break;
    514 		}
    515 		bootparam_p = bootparam_tag_next(bootparam_p);
    516 	}
    517 
    518 	/*
    519 	 * Heads up ... Setup the CPU / MMU / TLB functions
    520 	 */
    521 	if (set_cpufuncs())
    522 		panic("cpu not recognized!");
    523 
    524 #ifdef VERBOSE_INIT_ARM
    525 	printf("initarm: Configuring system ...\n");
    526 #endif
    527 	/*
    528 	 * Set up the variables that define the availablilty of
    529 	 * physical memory.  For now, we're going to set
    530 	 * physical_freestart to 0xc0200000 (where the kernel
    531 	 * was loaded), and allocate the memory we need downwards.
    532 	 * If we get too close to the L1 table that we set up, we
    533 	 * will panic.  We will update physical_freestart and
    534 	 * physical_freeend later to reflect what pmap_bootstrap()
    535 	 * wants to see.
    536 	 *
    537 	 * XXX pmap_bootstrap() needs an enema.
    538 	 */
    539 	physical_start = bootconfig.dram[0].address;
    540 	physical_end = bootconfig.dram[0].address
    541 			+ (bootconfig.dram[0].pages * PAGE_SIZE);
    542 
    543 	physical_freestart = 0xc0018000UL;
    544 	physical_freeend = 0xc0200000UL;
    545 
    546 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    547 
    548 #ifdef VERBOSE_INIT_ARM
    549 	/* Tell the user about the memory */
    550 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    551 	    physical_start, physical_end - 1);
    552 #endif
    553 
    554 	/*
    555 	 * Okay, the kernel starts 2MB in from the bottom of physical
    556 	 * memory.  We are going to allocate our bootstrap pages downwards
    557 	 * from there.
    558 	 *
    559 	 * We need to allocate some fixed page tables to get the kernel
    560 	 * going.  We allocate one page directory and a number of page
    561 	 * tables and store the physical addresses in the kernel_pt_table
    562 	 * array.
    563 	 *
    564 	 * The kernel page directory must be on a 16K boundary.  The page
    565 	 * tables must be on 4K bounaries.  What we do is allocate the
    566 	 * page directory on the first 16K boundary that we encounter, and
    567 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    568 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    569 	 * least one 16K aligned region.
    570 	 */
    571 
    572 #ifdef VERBOSE_INIT_ARM
    573 	printf("Allocating page tables\n");
    574 #endif
    575 
    576 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    577 
    578 #ifdef VERBOSE_INIT_ARM
    579 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    580 	       physical_freestart, free_pages, free_pages);
    581 #endif
    582 
    583 	/* Define a macro to simplify memory allocation */
    584 #define	valloc_pages(var, np)				\
    585 	alloc_pages((var).pv_pa, (np));			\
    586 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    587 
    588 #define alloc_pages(var, np)				\
    589 	physical_freeend -= ((np) * PAGE_SIZE);		\
    590 	if (physical_freeend < physical_freestart)	\
    591 		panic("initarm: out of memory");	\
    592 	(var) = physical_freeend;			\
    593 	free_pages -= (np);				\
    594 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    595 
    596 	loop1 = 0;
    597 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    598 		/* Are we 16KB aligned for an L1 ? */
    599 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    600 		    && kernel_l1pt.pv_pa == 0) {
    601 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    602 		} else {
    603 			valloc_pages(kernel_pt_table[loop1],
    604 			    L2_TABLE_SIZE / PAGE_SIZE);
    605 			++loop1;
    606 		}
    607 	}
    608 
    609 	/* This should never be able to happen but better confirm that. */
    610 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    611 		panic("initarm: Failed to align the kernel page directory");
    612 
    613 	/*
    614 	 * Allocate a page for the system vectors page
    615 	 */
    616 	alloc_pages(systempage.pv_pa, 1);
    617 
    618 	/* Allocate stacks for all modes */
    619 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    620 	valloc_pages(abtstack, ABT_STACK_SIZE);
    621 	valloc_pages(undstack, UND_STACK_SIZE);
    622 	valloc_pages(kernelstack, UPAGES);
    623 
    624 #ifdef VERBOSE_INIT_ARM
    625 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    626 	    irqstack.pv_va);
    627 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    628 	    abtstack.pv_va);
    629 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    630 	    undstack.pv_va);
    631 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    632 	    kernelstack.pv_va);
    633 #endif
    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.  Save physical_freeend for when we give whats left
    640 	 * of memory below 2Mbyte to UVM.
    641 	 */
    642 
    643 	physical_freeend_low = physical_freeend;
    644 
    645 #ifdef VERBOSE_INIT_ARM
    646 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    647 #endif
    648 
    649 	/*
    650 	 * Now we start construction of the L1 page table
    651 	 * We start by mapping the L2 page tables into the L1.
    652 	 * This means that we can replace L1 mappings later on if necessary
    653 	 */
    654 	l1pagetable = kernel_l1pt.pv_pa;
    655 
    656 	/* Map the L2 pages tables in the L1 page table */
    657 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    658 	    &kernel_pt_table[KERNEL_PT_SYS]);
    659 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    660 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    661 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    662 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    663 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    664 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    665 
    666 	/* update the top of the kernel VM */
    667 	pmap_curmaxkvaddr =
    668 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    669 
    670 #ifdef VERBOSE_INIT_ARM
    671 	printf("Mapping kernel\n");
    672 #endif
    673 
    674 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    675 	{
    676 		extern char etext[], _end[];
    677 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    678 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    679 		u_int logical;
    680 
    681 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    682 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    683 
    684 		logical = 0x00200000;	/* offset of kernel in RAM */
    685 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    686 		    physical_start + logical, textsize,
    687 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    688 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    689 		    physical_start + logical, totalsize - textsize,
    690 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    691 	}
    692 
    693 #ifdef VERBOSE_INIT_ARM
    694 	printf("Constructing L2 page tables\n");
    695 #endif
    696 
    697 	/* Map the stack pages */
    698 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    699 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    700 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    701 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    702 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    703 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    704 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    705 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    706 
    707 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    708 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    709 
    710 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    711 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    712 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    713 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    714 	}
    715 
    716 	/* Map the vector page. */
    717 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    718 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    719 
    720 	/* Map the statically mapped devices. */
    721 	pmap_devmap_bootstrap(l1pagetable, armadillo9_devmap);
    722 
    723 	/*
    724 	 * Update the physical_freestart/physical_freeend/free_pages
    725 	 * variables.
    726 	 */
    727 	{
    728 		extern char _end[];
    729 
    730 		physical_freestart = physical_start +
    731 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    732 		     KERNEL_BASE);
    733 		physical_freeend = physical_end;
    734 		free_pages =
    735 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    736 	}
    737 
    738 	/*
    739 	 * Now we have the real page tables in place so we can switch to them.
    740 	 * Once this is done we will be running with the REAL kernel page
    741 	 * tables.
    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 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    751 	cpu_setttb(kernel_l1pt.pv_pa, true);
    752 	cpu_tlb_flushID();
    753 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    754 
    755 	/*
    756 	 * Moved from cpu_startup() as data_abort_handler() references
    757 	 * this during uvm init
    758 	 */
    759 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    760 
    761 #ifdef VERBOSE_INIT_ARM
    762 	printf("done!\n");
    763 #endif
    764 
    765 #ifdef VERBOSE_INIT_ARM
    766 	printf("bootstrap done.\n");
    767 #endif
    768 
    769 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    770 
    771 	/*
    772 	 * Pages were allocated during the secondary bootstrap for the
    773 	 * stacks for different CPU modes.
    774 	 * We must now set the r13 registers in the different CPU modes to
    775 	 * point to these stacks.
    776 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    777 	 * of the stack memory.
    778 	 */
    779 #ifdef VERBOSE_INIT_ARM
    780 	printf("init subsystems: stacks ");
    781 #endif
    782 
    783 	set_stackptr(PSR_IRQ32_MODE,
    784 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    785 	set_stackptr(PSR_ABT32_MODE,
    786 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    787 	set_stackptr(PSR_UND32_MODE,
    788 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    789 
    790 	/*
    791 	 * Well we should set a data abort handler.
    792 	 * Once things get going this will change as we will need a proper
    793 	 * handler.
    794 	 * Until then we will use a handler that just panics but tells us
    795 	 * why.
    796 	 * Initialisation of the vectors will just panic on a data abort.
    797 	 * This just fills in a slightly better one.
    798 	 */
    799 #ifdef VERBOSE_INIT_ARM
    800 	printf("vectors ");
    801 #endif
    802 	data_abort_handler_address = (u_int)data_abort_handler;
    803 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    804 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    805 
    806 	/* Initialise the undefined instruction handlers */
    807 #ifdef VERBOSE_INIT_ARM
    808 	printf("undefined ");
    809 #endif
    810 	undefined_init();
    811 
    812 	/* Load memory into UVM. */
    813 #ifdef VERBOSE_INIT_ARM
    814 	printf("page ");
    815 #endif
    816 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    817 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    818 	    atop(physical_freestart), atop(physical_freeend),
    819 	    VM_FREELIST_DEFAULT);
    820 	uvm_page_physload(atop(0xc0000000), atop(physical_freeend_low),
    821 	    atop(0xc0000000), atop(physical_freeend_low),
    822 	    VM_FREELIST_DEFAULT);
    823 	physmem = bootconfig.dram[0].pages;
    824 	for (loop = 1; loop < bootconfig.dramblocks; ++loop) {
    825 		size_t start = bootconfig.dram[loop].address;
    826 		size_t size = bootconfig.dram[loop].pages * PAGE_SIZE;
    827 		uvm_page_physload(atop(start), atop(start + size),
    828 				  atop(start), atop(start + size),
    829 				  VM_FREELIST_DEFAULT);
    830 		physmem += bootconfig.dram[loop].pages;
    831 	}
    832 
    833 	/* Boot strap pmap telling it where the kernel page table is */
    834 #ifdef VERBOSE_INIT_ARM
    835 	printf("pmap ");
    836 #endif
    837 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    838 
    839 	/* Setup the IRQ system */
    840 #ifdef VERBOSE_INIT_ARM
    841 	printf("irq ");
    842 #endif
    843 	ep93xx_intr_init();
    844 #if NISA > 0
    845 	isa_intr_init();
    846 
    847 #ifdef VERBOSE_INIT_ARM
    848 	printf("isa ");
    849 #endif
    850 	isa_armadillo9_init(ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAIO,
    851 		ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAMEM);
    852 #endif
    853 
    854 #ifdef VERBOSE_INIT_ARM
    855 	printf("done.\n");
    856 #endif
    857 
    858 #ifdef BOOTHOWTO
    859 	boothowto = BOOTHOWTO;
    860 #endif
    861 
    862 #ifdef DDB
    863 	db_machine_init();
    864 	if (boothowto & RB_KDB)
    865 		Debugger();
    866 #endif
    867 
    868 	/* We have our own device_register() */
    869 	evbarm_device_register = armadillo9_device_register;
    870 
    871 	/* We return the new stack pointer address */
    872 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    873 }
    874 
    875 void
    876 consinit(void)
    877 {
    878 	static int consinit_called;
    879 #if NEPCOM > 0
    880 	bus_space_handle_t ioh;
    881 #endif
    882 
    883 	if (consinit_called != 0)
    884 		return;
    885 
    886 	consinit_called = 1;
    887 
    888 	/*
    889 	 * Console devices are already mapped in VA.  Our devmap reflects
    890 	 * this, so register it now so drivers can map the console
    891 	 * device.
    892 	 */
    893 	pmap_devmap_register(armadillo9_devmap);
    894 
    895 #if NEPCOM > 0
    896 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + comaddr[CONUNIT],
    897 		EP93XX_APB_UART_SIZE, 0, &ioh);
    898         if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + comaddr[CONUNIT],
    899 		ioh, comcnspeed, comcnmode))
    900 	{
    901 		panic("can't init serial console");
    902 	}
    903 #else
    904 	panic("serial console not configured");
    905 #endif
    906 #if KGDB
    907 #if NEPCOM > 0
    908 	if (strcmp(kgdb_devname, "epcom") == 0) {
    909 		com_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
    910 			kgdb_devmode);
    911 	}
    912 #endif	/* NEPCOM > 0 */
    913 #endif	/* KGDB */
    914 }
    915 
    916 
    917 bus_dma_tag_t
    918 ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    919 {
    920 	int i;
    921 	struct arm32_bus_dma_tag *dmat;
    922 
    923 	for (i = 0; i < bootconfig.dramblocks; i++) {
    924 		armadillo9_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    925 		armadillo9_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    926 		armadillo9_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    927 			PAGE_SIZE;
    928 	}
    929 
    930 	dmat = dma_tag_template;
    931 
    932 	dmat->_ranges = armadillo9_dma_ranges;
    933 	dmat->_nranges = bootconfig.dramblocks;
    934 
    935 	return dmat;
    936 }
    937