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