Home | History | Annotate | Line # | Download | only in marvell
marvell_machdep.c revision 1.10
      1 /*	$NetBSD: marvell_machdep.c,v 1.10 2012/07/28 19:08:22 matt Exp $ */
      2 /*
      3  * Copyright (c) 2007, 2008, 2010 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: marvell_machdep.c,v 1.10 2012/07/28 19:08:22 matt Exp $");
     29 
     30 #include "opt_evbarm_boardtype.h"
     31 #include "opt_ddb.h"
     32 #include "opt_pci.h"
     33 #include "opt_mvsoc.h"
     34 #include "com.h"
     35 #include "gtpci.h"
     36 #include "mvpex.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/reboot.h>
     41 #include <sys/systm.h>
     42 #include <sys/termios.h>
     43 
     44 #include <prop/proplib.h>
     45 
     46 #include <dev/cons.h>
     47 #include <dev/md.h>
     48 
     49 #include <dev/marvell/marvellreg.h>
     50 #include <dev/marvell/marvellvar.h>
     51 #include <dev/pci/pcireg.h>
     52 #include <dev/pci/pcivar.h>
     53 
     54 #include <machine/autoconf.h>
     55 #include <machine/bootconfig.h>
     56 #include <machine/pci_machdep.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #include <arm/db_machdep.h>
     61 #include <arm/undefined.h>
     62 #include <arm/arm32/machdep.h>
     63 
     64 #include <arm/marvell/mvsocreg.h>
     65 #include <arm/marvell/mvsocvar.h>
     66 #include <arm/marvell/orionreg.h>
     67 #include <arm/marvell/kirkwoodreg.h>
     68 #include <arm/marvell/mvsocgppvar.h>
     69 
     70 #include <evbarm/marvell/marvellreg.h>
     71 #include <evbarm/marvell/marvellvar.h>
     72 
     73 #include <ddb/db_extern.h>
     74 #include <ddb/db_sym.h>
     75 
     76 #include "ksyms.h"
     77 
     78 
     79 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
     80 #define KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00000000)
     81 #define KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
     82 
     83 /*
     84  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
     85  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
     86  */
     87 #define KERNEL_VM_SIZE		0x0c000000
     88 
     89 /*
     90  * Address to call from cpu_reset() to reset the machine.
     91  * This is machine architecture dependent as it varies depending
     92  * on where the ROM appears when you turn the MMU off.
     93  */
     94 
     95 u_int cpu_reset_address = 0xffff0000;
     96 
     97 /* Define various stack sizes in pages */
     98 #define IRQ_STACK_SIZE	1
     99 #define ABT_STACK_SIZE	1
    100 #ifdef IPKDB
    101 #define UND_STACK_SIZE	2
    102 #else
    103 #define UND_STACK_SIZE	1
    104 #endif
    105 
    106 BootConfig bootconfig;		/* Boot config storage */
    107 static char bootargs[MAX_BOOT_STRING];
    108 char *boot_args = NULL;
    109 
    110 vm_offset_t physical_start;
    111 vm_offset_t physical_freestart;
    112 vm_offset_t physical_freeend;
    113 vm_offset_t physical_end;
    114 u_int free_pages;
    115 
    116 /* Physical and virtual addresses for some global pages */
    117 pv_addr_t systempage;
    118 pv_addr_t irqstack;
    119 pv_addr_t undstack;
    120 pv_addr_t abtstack;
    121 pv_addr_t kernelstack;
    122 
    123 vm_offset_t msgbufphys;
    124 
    125 extern u_int data_abort_handler_address;
    126 extern u_int prefetch_abort_handler_address;
    127 extern u_int undefined_handler_address;
    128 
    129 extern char _end[];
    130 
    131 #define KERNEL_PT_SYS		0   /* Page table for mapping proc0 zero page */
    132 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    133 #define KERNEL_PT_KERNEL_NUM	4
    134 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    135 /* Page tables for mapping kernel VM */
    136 #define KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    137 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    138 
    139 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    140 
    141 /*
    142  * Macros to translate between physical and virtual for a subset of the
    143  * kernel address space.  *Not* for general use.
    144  */
    145 #define KERNEL_BASE_PHYS	physical_start
    146 #define KERN_VTOPHYS(va) \
    147 	((paddr_t)((vaddr_t)va - KERNEL_BASE + KERNEL_BASE_PHYS))
    148 #define KERN_PHYSTOV(pa) \
    149 	((vaddr_t)((paddr_t)pa - KERNEL_BASE_PHYS + KERNEL_BASE))
    150 
    151 
    152 #include "com.h"
    153 #if NCOM > 0
    154 #include <dev/ic/comreg.h>
    155 #include <dev/ic/comvar.h>
    156 #endif
    157 
    158 #ifndef CONSPEED
    159 #define CONSPEED	B115200	/* It's a setting of the default of u-boot */
    160 #endif
    161 #ifndef CONMODE
    162 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    163 
    164 int comcnspeed = CONSPEED;
    165 int comcnmode = CONMODE;
    166 #endif
    167 
    168 #include "opt_kgdb.h"
    169 #ifdef KGDB
    170 #include <sys/kgdb.h>
    171 #endif
    172 
    173 static void marvell_device_register(device_t, void *);
    174 #if NGTPCI > 0 || NMVPEX > 0
    175 static void marvell_startend_by_tag(int, uint64_t *, uint64_t *);
    176 #endif
    177 
    178 static void
    179 marvell_system_reset(void)
    180 {
    181 	/* unmask soft reset */
    182 	write_mlmbreg(MVSOC_MLMB_RSTOUTNMASKR,
    183 	    MVSOC_MLMB_RSTOUTNMASKR_SOFTRSTOUTEN);
    184 	/* assert soft reset */
    185 	write_mlmbreg(MVSOC_MLMB_SSRR, MVSOC_MLMB_SSRR_SYSTEMSOFTRST);
    186 	/* if we're still running, jump to the reset address */
    187 	cpu_reset();
    188 	/*NOTREACHED*/
    189 }
    190 
    191 void
    192 cpu_reboot(int howto, char *bootstr)
    193 {
    194 
    195 	/*
    196 	 * If we are still cold then hit the air brakes
    197 	 * and crash to earth fast
    198 	 */
    199 	if (cold) {
    200 		doshutdownhooks();
    201 		printf("The operating system has halted.\r\n");
    202 		printf("Please press any key to reboot.\r\n");
    203 		cngetc();
    204 		printf("rebooting...\r\n");
    205 		marvell_system_reset();
    206 	}
    207 
    208 	/*
    209 	 * If RB_NOSYNC was not specified sync the discs.
    210 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    211 	 * unmount.  It looks like syslogd is getting woken up only to find
    212 	 * that it cannot page part of the binary in as the filesystem has
    213 	 * been unmounted.
    214 	 */
    215 	if (!(howto & RB_NOSYNC))
    216 		bootsync();
    217 
    218 	/* Say NO to interrupts */
    219 	splhigh();
    220 
    221 	/* Do a dump if requested. */
    222 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    223 		dumpsys();
    224 
    225 	/* Run any shutdown hooks */
    226 	doshutdownhooks();
    227 
    228 	/* Make sure IRQ's are disabled */
    229 	IRQdisable;
    230 
    231 	if (howto & RB_HALT) {
    232 		printf("The operating system has halted.\r\n");
    233 		printf("Please press any key to reboot.\r\n");
    234 		cngetc();
    235 	}
    236 
    237 	printf("rebooting...\r\n");
    238 	marvell_system_reset();
    239 
    240 	/*NOTREACHED*/
    241 }
    242 
    243 static inline
    244 pd_entry_t *
    245 read_ttb(void)
    246 {
    247 	long ttb;
    248 
    249 	__asm volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r" (ttb));
    250 
    251 	return (pd_entry_t *)(ttb & ~((1<<14)-1));
    252 }
    253 
    254 /*
    255  * Static device mappings. These peripheral registers are mapped at
    256  * fixed virtual addresses very early in initarm() so that we can use
    257  * them while booting the kernel, and stay at the same address
    258  * throughout whole kernel's life time.
    259  *
    260  * We use this table twice; once with bootstrap page table, and once
    261  * with kernel's page table which we build up in initarm().
    262  *
    263  * Since we map these registers into the bootstrap page table using
    264  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    265  * registers segment-aligned and segment-rounded in order to avoid
    266  * using the 2nd page tables.
    267  */
    268 #define _A(a)	((a) & ~L1_S_OFFSET)
    269 #define _S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    270 
    271 static const struct pmap_devmap marvell_devmap[] = {
    272 	{
    273 		MARVELL_INTERREGS_VBASE,
    274 		_A(MARVELL_INTERREGS_PBASE),
    275 		_S(MARVELL_INTERREGS_SIZE),
    276 		VM_PROT_READ|VM_PROT_WRITE,
    277 		PTE_NOCACHE,
    278 	},
    279 
    280 	{ 0, 0, 0, 0, 0 }
    281 };
    282 
    283 #undef  _A
    284 #undef  _S
    285 
    286 extern uint32_t *u_boot_args[];
    287 
    288 /*
    289  * u_int initarm(...)
    290  *
    291  * Initial entry point on startup. This gets called before main() is
    292  * entered.
    293  * It should be responsible for setting up everything that must be
    294  * in place when main is called.
    295  * This includes
    296  *   Taking a copy of the boot configuration structure.
    297  *   Initialising the physical console so characters can be printed.
    298  *   Setting up page tables for the kernel
    299  *   Relocating the kernel to the bottom of physical memory
    300  */
    301 u_int
    302 initarm(void *arg)
    303 {
    304 	uint32_t target, attr, base, size;
    305 	u_int l1pagetable;
    306 	int loop, pt_index, cs, memtag = 0, iotag = 0, window;
    307 
    308 	/* map some peripheral registers */
    309 	pmap_devmap_bootstrap((vaddr_t)read_ttb(), marvell_devmap);
    310 
    311 	mvsoc_bootstrap(MARVELL_INTERREGS_VBASE);
    312 
    313 	/* Get ready for splfoo() */
    314 	switch (mvsoc_model()) {
    315 #ifdef ORION
    316 	case MARVELL_ORION_1_88F1181:
    317 	case MARVELL_ORION_1_88F5082:
    318 	case MARVELL_ORION_1_88F5180N:
    319 	case MARVELL_ORION_1_88F5181:
    320 	case MARVELL_ORION_1_88F5182:
    321 	case MARVELL_ORION_1_88F6082:
    322 	case MARVELL_ORION_1_88F6183:
    323 	case MARVELL_ORION_1_88W8660:
    324 	case MARVELL_ORION_2_88F1281:
    325 	case MARVELL_ORION_2_88F5281:
    326 		orion_intr_bootstrap();
    327 
    328 		memtag = ORION_TAG_PEX0_MEM;
    329 		iotag = ORION_TAG_PEX0_IO;
    330 		nwindow = ORION_MLMB_NWINDOW;
    331 		nremap = ORION_MLMB_NREMAP;
    332 
    333 		orion_getclks(MARVELL_INTERREGS_VBASE);
    334 		break;
    335 #endif	/* ORION */
    336 
    337 #ifdef KIRKWOOD
    338 	case MARVELL_KIRKWOOD_88F6180:
    339 	case MARVELL_KIRKWOOD_88F6192:
    340 	case MARVELL_KIRKWOOD_88F6281:
    341 	case MARVELL_KIRKWOOD_88F6282:
    342 		kirkwood_intr_bootstrap();
    343 
    344 		memtag = KIRKWOOD_TAG_PEX_MEM;
    345 		iotag = KIRKWOOD_TAG_PEX_IO;
    346 		nwindow = KIRKWOOD_MLMB_NWINDOW;
    347 		nremap = KIRKWOOD_MLMB_NREMAP;
    348 
    349 		kirkwood_getclks(MARVELL_INTERREGS_VBASE);
    350 		break;
    351 #endif	/* KIRKWOOD */
    352 
    353 #ifdef MV78XX0
    354 	case MARVELL_MV78XX0_MV78100:
    355 	case MARVELL_MV78XX0_MV78200:
    356 		mv78xx0_intr_bootstrap();
    357 
    358 		memtag = MV78XX0_TAG_PEX_MEM;
    359 		iotag = MV78XX0_TAG_PEX_IO;
    360 		nwindow = MV78XX0_MLMB_NWINDOW;
    361 		nremap = MV78XX0_MLMB_NREMAP;
    362 
    363 		mv78xx0_getclks(MARVELL_INTERREGS_VBASE);
    364 		break;
    365 #endif	/* MV78XX0 */
    366 
    367 	default:
    368 		/* We can't output console here yet... */
    369 		panic("unknown model...\n");
    370 
    371 		/* NOTREACHED */
    372 	}
    373 
    374 	/* Reset PCI-Express space to window register. */
    375 	window = mvsoc_target(memtag, &target, &attr, NULL, NULL);
    376 	write_mlmbreg(MVSOC_MLMB_WCR(window),
    377 	    MVSOC_MLMB_WCR_WINEN |
    378 	    MVSOC_MLMB_WCR_TARGET(target) |
    379 	    MVSOC_MLMB_WCR_ATTR(attr) |
    380 	    MVSOC_MLMB_WCR_SIZE(MARVELL_PEXMEM_SIZE));
    381 	write_mlmbreg(MVSOC_MLMB_WBR(window),
    382 	    MARVELL_PEXMEM_PBASE & MVSOC_MLMB_WBR_BASE_MASK);
    383 #ifdef PCI_NETBSD_CONFIGURE
    384 	if (window < nremap) {
    385 		write_mlmbreg(MVSOC_MLMB_WRLR(window),
    386 		    MARVELL_PEXMEM_PBASE & MVSOC_MLMB_WRLR_REMAP_MASK);
    387 		write_mlmbreg(MVSOC_MLMB_WRHR(window), 0);
    388 	}
    389 #endif
    390 	window = mvsoc_target(iotag, &target, &attr, NULL, NULL);
    391 	write_mlmbreg(MVSOC_MLMB_WCR(window),
    392 	    MVSOC_MLMB_WCR_WINEN |
    393 	    MVSOC_MLMB_WCR_TARGET(target) |
    394 	    MVSOC_MLMB_WCR_ATTR(attr) |
    395 	    MVSOC_MLMB_WCR_SIZE(MARVELL_PEXIO_SIZE));
    396 	write_mlmbreg(MVSOC_MLMB_WBR(window),
    397 	    MARVELL_PEXIO_PBASE & MVSOC_MLMB_WBR_BASE_MASK);
    398 #ifdef PCI_NETBSD_CONFIGURE
    399 	if (window < nremap) {
    400 		write_mlmbreg(MVSOC_MLMB_WRLR(window),
    401 		    MARVELL_PEXIO_PBASE & MVSOC_MLMB_WRLR_REMAP_MASK);
    402 		write_mlmbreg(MVSOC_MLMB_WRHR(window), 0);
    403 	}
    404 #endif
    405 
    406 	/*
    407 	 * Heads up ... Setup the CPU / MMU / TLB functions
    408 	 */
    409 	if (set_cpufuncs())
    410 		panic("cpu not recognized!");
    411 
    412 	/*
    413 	 * U-Boot doesn't use the virtual memory.
    414 	 *
    415 	 * Physical Address Range     Description
    416 	 * -----------------------    ----------------------------------
    417 	 * 0x00000000 - 0x0fffffff    SDRAM Bank 0 (max 256MB)
    418 	 * 0x10000000 - 0x1fffffff    SDRAM Bank 1 (max 256MB)
    419 	 * 0x20000000 - 0x2fffffff    SDRAM Bank 2 (max 256MB)
    420 	 * 0x30000000 - 0x3fffffff    SDRAM Bank 3 (max 256MB)
    421 	 * 0xf1000000 - 0xf10fffff    SoC Internal Registers
    422 	 */
    423 
    424 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    425 
    426 	consinit();
    427 
    428 	/* Talk to the user */
    429 #ifndef EVBARM_BOARDTYPE
    430 #define EVBARM_BOARDTYPE	Marvell
    431 #endif
    432 #define BDSTR(s)	_BDSTR(s)
    433 #define _BDSTR(s)	#s
    434 	printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
    435 
    436 	/* copy command line U-Boot gave us */
    437 	strncpy(bootargs, (char *)u_boot_args[3], sizeof(bootargs));
    438 
    439 #ifdef VERBOSE_INIT_ARM
    440 	printf("initarm: Configuring system ...\n");
    441 #endif
    442 
    443 	bootconfig.dramblocks = 0;
    444 	physical_end = physmem = 0;
    445 	for (cs = MARVELL_TAG_SDRAM_CS0; cs <= MARVELL_TAG_SDRAM_CS3; cs++) {
    446 		mvsoc_target(cs, &target, &attr, &base, &size);
    447 		if (size == 0)
    448 			continue;
    449 
    450 		bootconfig.dram[bootconfig.dramblocks].address = base;
    451 		bootconfig.dram[bootconfig.dramblocks].pages = size / PAGE_SIZE;
    452 
    453 		if (base != physical_end)
    454 			panic("memory hole not support");
    455 
    456 		physical_end += size;
    457 		physmem += size / PAGE_SIZE;
    458 
    459 		bootconfig.dramblocks++;
    460 	}
    461 
    462 	/*
    463 	 * Set up the variables that define the availablilty of
    464 	 * physical memory.  For now, we're going to set
    465 	 * physical_freestart to 0xa0008000 (where the kernel
    466 	 * was loaded), and allocate the memory we need downwards.
    467 	 * If we get too close to the L1 table that we set up, we
    468 	 * will panic.  We will update physical_freestart and
    469 	 * physical_freeend later to reflect what pmap_bootstrap()
    470 	 * wants to see.
    471 	 *
    472 	 * XXX pmap_bootstrap() needs an enema.
    473 	 */
    474 	physical_start = bootconfig.dram[0].address;
    475 
    476 	/*
    477 	 * Our kernel is at the beginning of memory, so set our free space to
    478 	 * all the memory after the kernel.
    479 	 */
    480 	physical_freestart = KERN_VTOPHYS(round_page((vaddr_t)_end));
    481 	physical_freeend = physical_end;
    482 
    483 #ifdef VERBOSE_INIT_ARM
    484 	/* Tell the user about the memory */
    485 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    486 	    physical_start, physical_end - 1);
    487 #endif
    488 
    489 	/*
    490 	 * Okay, the kernel starts 8kB in from the bottom of physical
    491 	 * memory.  We are going to allocate our bootstrap pages upwards
    492 	 * from physical_freestart.
    493 	 *
    494 	 * We need to allocate some fixed page tables to get the kernel
    495 	 * going.  We allocate one page directory and a number of page
    496 	 * tables and store the physical addresses in the kernel_pt_table
    497 	 * array.
    498 	 *
    499 	 * The kernel page directory must be on a 16K boundary.  The page
    500 	 * tables must be on 4K bounaries.  What we do is allocate the
    501 	 * page directory on the first 16K boundary that we encounter, and
    502 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    503 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    504 	 * least one 16K aligned region.
    505 	 */
    506 
    507 #ifdef VERBOSE_INIT_ARM
    508 	printf("Allocating page tables\n");
    509 #endif
    510 
    511 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    512 
    513 #ifdef VERBOSE_INIT_ARM
    514 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    515 	    physical_freestart, free_pages, free_pages);
    516 #endif
    517 
    518 	/*
    519 	 * Define a macro to simplify memory allocation.  As we allocate the
    520 	 * memory, make sure that we don't walk over our temporary first level
    521 	 * translation table.
    522 	 */
    523 #define valloc_pages(var, np)						\
    524 	(var).pv_pa = physical_freestart;				\
    525 	physical_freestart += ((np) * PAGE_SIZE);			\
    526 	if (physical_freestart > (physical_freeend - L1_TABLE_SIZE))	\
    527 		panic("initarm: out of memory");			\
    528 	free_pages -= (np);						\
    529 	(var).pv_va = KERN_PHYSTOV((var).pv_pa);			\
    530 	memset((char *)(var).pv_va, 0, ((np) * PAGE_SIZE));
    531 
    532 	pt_index = 0;
    533 	kernel_l1pt.pv_pa = 0;
    534 	kernel_l1pt.pv_va = 0;
    535 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    536 		/* Are we 16KB aligned for an L1 ? */
    537 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0 &&
    538 		    kernel_l1pt.pv_pa == 0) {
    539 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    540 		} else {
    541 			valloc_pages(kernel_pt_table[pt_index],
    542 			    L2_TABLE_SIZE / PAGE_SIZE);
    543 			++pt_index;
    544 		}
    545 	}
    546 
    547 	/* This should never be able to happen but better confirm that. */
    548 	if (!kernel_l1pt.pv_pa ||
    549 	    (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
    550 		panic("initarm: Failed to align the kernel page directory");
    551 
    552 	/*
    553 	 * Allocate a page for the system page mapped to V0x00000000
    554 	 * This page will just contain the system vectors and can be
    555 	 * shared by all processes.
    556 	 */
    557 	valloc_pages(systempage, 1);
    558 	systempage.pv_va = 0x00000000;
    559 
    560 	/* Allocate stacks for all modes */
    561 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    562 	valloc_pages(abtstack, ABT_STACK_SIZE);
    563 	valloc_pages(undstack, UND_STACK_SIZE);
    564 	valloc_pages(kernelstack, UPAGES);
    565 
    566 #ifdef VERBOSE_INIT_ARM
    567 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    568 	    irqstack.pv_va);
    569 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    570 	    abtstack.pv_va);
    571 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    572 	    undstack.pv_va);
    573 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    574 	    kernelstack.pv_va);
    575 #endif
    576 
    577 	/* Allocate the message buffer. */
    578 	{
    579 		pv_addr_t msgbuf;
    580 
    581 		valloc_pages(msgbuf, round_page(MSGBUFSIZE) / PAGE_SIZE);
    582 		msgbufphys = msgbuf.pv_pa;
    583 	}
    584 
    585 	/*
    586 	 * Ok we have allocated physical pages for the primary kernel
    587 	 * page tables
    588 	 */
    589 
    590 #ifdef VERBOSE_INIT_ARM
    591 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    592 #endif
    593 
    594 	/*
    595 	 * Now we start construction of the L1 page table
    596 	 * We start by mapping the L2 page tables into the L1.
    597 	 * This means that we can replace L1 mappings later on if necessary
    598 	 */
    599 	l1pagetable = kernel_l1pt.pv_va;
    600 
    601 	/* Map the L2 pages tables in the L1 page table */
    602 	pmap_link_l2pt(l1pagetable, 0x00000000,
    603 	    &kernel_pt_table[KERNEL_PT_SYS]);
    604 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    605 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    606 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    607 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    608 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    609 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    610 
    611 	/* update the top of the kernel VM */
    612 	pmap_curmaxkvaddr =
    613 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    614 
    615 #ifdef VERBOSE_INIT_ARM
    616 	printf("Mapping kernel\n");
    617 #endif
    618 
    619 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    620 	{
    621 		extern char etext[], _end[];
    622 		size_t textsize = (uintptr_t)etext - KERNEL_TEXT_BASE;
    623 		size_t totalsize = (uintptr_t)_end - KERNEL_TEXT_BASE;
    624 		u_int logical;
    625 
    626 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    627 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    628 
    629 		logical = 0x00000000;	/* offset of kernel in RAM */
    630 
    631 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    632 		    physical_start + logical, textsize,
    633 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    634 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    635 		    physical_start + logical, totalsize - textsize,
    636 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    637 	}
    638 
    639 #ifdef VERBOSE_INIT_ARM
    640 	printf("Constructing L2 page tables\n");
    641 #endif
    642 
    643 	/* Map the stack pages */
    644 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    645 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    646 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    647 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    648 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    649 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    650 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    651 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    652 
    653 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    654 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    655 
    656 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop)
    657 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    658 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    659 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    660 
    661 	/* Map the vector page. */
    662 	pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
    663 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    664 
    665 	/*
    666 	 * Map integrated peripherals at same address in first level page
    667 	 * table so that we can continue to use console.
    668 	 */
    669 	pmap_devmap_bootstrap(l1pagetable, marvell_devmap);
    670 
    671 	/*
    672 	 * Now we have the real page tables in place so we can switch to them.
    673 	 * Once this is done we will be running with the REAL kernel page
    674 	 * tables.
    675 	 */
    676 
    677 	/* Switch tables */
    678 #ifdef VERBOSE_INIT_ARM
    679 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    680 #endif
    681 
    682 	cpu_setttb(kernel_l1pt.pv_pa);
    683 	cpu_tlb_flushID();
    684 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    685 
    686 	/*
    687 	 * Moved from cpu_startup() as data_abort_handler() references
    688 	 * this during uvm init.
    689 	 */
    690 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    691 
    692 #ifdef VERBOSE_INIT_ARM
    693 	printf("bootstrap done.\n");
    694 #endif
    695 
    696 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    697 
    698 	/*
    699 	 * Pages were allocated during the secondary bootstrap for the
    700 	 * stacks for different CPU modes.
    701 	 * We must now set the r13 registers in the different CPU modes to
    702 	 * point to these stacks.
    703 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    704 	 * of the stack memory.
    705 	 */
    706 #ifdef VERBOSE_INIT_ARM
    707 	printf("init subsystems: stacks ");
    708 #endif
    709 
    710 	set_stackptr(PSR_IRQ32_MODE,
    711 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    712 	set_stackptr(PSR_ABT32_MODE,
    713 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    714 	set_stackptr(PSR_UND32_MODE,
    715 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    716 
    717 	/*
    718 	 * Well we should set a data abort handler.
    719 	 * Once things get going this will change as we will need a proper
    720 	 * handler.
    721 	 * Until then we will use a handler that just panics but tells us
    722 	 * why.
    723 	 * Initialisation of the vectors will just panic on a data abort.
    724 	 * This just fills in a slightly better one.
    725 	 */
    726 #ifdef VERBOSE_INIT_ARM
    727 	printf("vectors ");
    728 #endif
    729 	data_abort_handler_address = (u_int)data_abort_handler;
    730 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    731 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    732 
    733 	/* Initialise the undefined instruction handlers */
    734 #ifdef VERBOSE_INIT_ARM
    735 	printf("undefined ");
    736 #endif
    737 	undefined_init();
    738 
    739 	/* Load memory into UVM. */
    740 #ifdef VERBOSE_INIT_ARM
    741 	printf("page ");
    742 #endif
    743 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    744 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    745 	    atop(physical_freestart), atop(physical_freeend),
    746 	    VM_FREELIST_DEFAULT);
    747 
    748 	/* Boot strap pmap telling it where the kernel page table is */
    749 #ifdef VERBOSE_INIT_ARM
    750 	printf("pmap ");
    751 #endif
    752 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    753 
    754 #ifdef VERBOSE_INIT_ARM
    755 	printf("done.\n");
    756 #endif
    757 
    758 #ifdef __HAVE_MEMORY_DISK__
    759 	md_root_setconf(memory_disk, sizeof memory_disk);
    760 #endif
    761 
    762 	boot_args = bootargs;
    763 	parse_mi_bootargs(boot_args);
    764 
    765 #ifdef BOOTHOWTO
    766 	boothowto |= BOOTHOWTO;
    767 #endif
    768 
    769 #ifdef KGDB
    770 	if (boothowto & RB_KDB) {
    771 		kgdb_debug_init = 1;
    772 		kgdb_connect(1);
    773 	}
    774 #endif
    775 
    776 #ifdef DDB
    777 	db_machine_init();
    778 	if (boothowto & RB_KDB)
    779 		Debugger();
    780 #endif
    781 
    782 	/* we've a specific device_register routine */
    783 	evbarm_device_register = marvell_device_register;
    784 
    785 	/* We return the new stack pointer address */
    786 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    787 }
    788 
    789 void
    790 consinit(void)
    791 {
    792 	static int consinit_called = 0;
    793 
    794 	if (consinit_called != 0)
    795 		return;
    796 
    797 	consinit_called = 1;
    798 
    799 #if NCOM > 0
    800 	{
    801 		extern int mvuart_cnattach(bus_space_tag_t, bus_addr_t, int,
    802 					   uint32_t, int);
    803 
    804 		if (mvuart_cnattach(&mvsoc_bs_tag,
    805 		    MARVELL_INTERREGS_VBASE + MVSOC_COM0_BASE,
    806 		    comcnspeed, mvTclk, comcnmode))
    807 			panic("can't init serial console");
    808 	}
    809 #else
    810 	panic("serial console not configured");
    811 #endif
    812 }
    813 
    814 
    815 static void
    816 marvell_device_register(device_t dev, void *aux)
    817 {
    818 	prop_dictionary_t dict = device_properties(dev);
    819 
    820 #if NCOM > 0
    821 	if (device_is_a(dev, "com") &&
    822 	    device_is_a(device_parent(dev), "mvsoc"))
    823 		prop_dictionary_set_uint32(dict, "frequency", mvTclk);
    824 #endif
    825 	if (device_is_a(dev, "gtidmac")) {
    826 		prop_dictionary_set_uint32(dict,
    827 		    "dmb_speed", mvTclk * sizeof(uint32_t));	/* XXXXXX */
    828 		prop_dictionary_set_uint32(dict,
    829 		    "xore-irq-begin", ORION_IRQ_XOR0);
    830 	}
    831 #if NGTPCI > 0 && defined(ORION)
    832 	if (device_is_a(dev, "gtpci")) {
    833 		extern struct bus_space
    834 		    orion_pci_io_bs_tag, orion_pci_mem_bs_tag;
    835 		extern struct arm32_pci_chipset arm32_gtpci_chipset;
    836 
    837 		prop_data_t io_bs_tag, mem_bs_tag, pc;
    838 		prop_array_t int2gpp;
    839 		prop_number_t gpp;
    840 		uint64_t start, end;
    841 		int i, j;
    842 		static struct {
    843 			const char *boardtype;
    844 			int pin[PCI_INTERRUPT_PIN_MAX];
    845 		} hints[] = {
    846 			{ "kuronas_x4",
    847 			    { 11, PCI_INTERRUPT_PIN_NONE } },
    848 
    849 			{ NULL,
    850 			    { PCI_INTERRUPT_PIN_NONE } },
    851 		};
    852 
    853 		arm32_gtpci_chipset.pc_conf_v = device_private(dev);
    854 		arm32_gtpci_chipset.pc_intr_v = device_private(dev);
    855 
    856 		io_bs_tag = prop_data_create_data_nocopy(
    857 		    &orion_pci_io_bs_tag, sizeof(struct bus_space));
    858 		KASSERT(io_bs_tag != NULL);
    859 		prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
    860 		prop_object_release(io_bs_tag);
    861 		mem_bs_tag = prop_data_create_data_nocopy(
    862 		    &orion_pci_mem_bs_tag, sizeof(struct bus_space));
    863 		KASSERT(mem_bs_tag != NULL);
    864 		prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
    865 		prop_object_release(mem_bs_tag);
    866 
    867 		pc = prop_data_create_data_nocopy(&arm32_gtpci_chipset,
    868 		    sizeof(struct arm32_pci_chipset));
    869 		KASSERT(pc != NULL);
    870 		prop_dictionary_set(dict, "pci-chipset", pc);
    871 		prop_object_release(pc);
    872 
    873 		marvell_startend_by_tag(ORION_TAG_PCI_IO, &start, &end);
    874 		prop_dictionary_set_uint64(dict, "iostart", start);
    875 		prop_dictionary_set_uint64(dict, "ioend", end);
    876 		marvell_startend_by_tag(ORION_TAG_PCI_MEM, &start, &end);
    877 		prop_dictionary_set_uint64(dict, "memstart", start);
    878 		prop_dictionary_set_uint64(dict, "memend", end);
    879 		prop_dictionary_set_uint32(dict,
    880 		    "cache-line-size", arm_dcache_align);
    881 
    882 		/* Setup the hint for interrupt-pin. */
    883 #define BDSTR(s)		_BDSTR(s)
    884 #define _BDSTR(s)		#s
    885 #define THIS_BOARD(str)		(strcmp(str, BDSTR(EVBARM_BOARDTYPE)) == 0)
    886 		for (i = 0; hints[i].boardtype != NULL; i++)
    887 			if (THIS_BOARD(hints[i].boardtype))
    888 				break;
    889 		if (hints[i].boardtype == NULL)
    890 			return;
    891 
    892 		int2gpp =
    893 		    prop_array_create_with_capacity(PCI_INTERRUPT_PIN_MAX + 1);
    894 
    895 		/* first set dummy */
    896 		gpp = prop_number_create_integer(0);
    897 		prop_array_add(int2gpp, gpp);
    898 		prop_object_release(gpp);
    899 
    900 		for (j = 0; hints[i].pin[j] != PCI_INTERRUPT_PIN_NONE; j++) {
    901 			gpp = prop_number_create_integer(hints[i].pin[j]);
    902 			prop_array_add(int2gpp, gpp);
    903 			prop_object_release(gpp);
    904 		}
    905 		prop_dictionary_set(dict, "int2gpp", int2gpp);
    906 	}
    907 #endif	/* NGTPCI > 0 && defined(ORION) */
    908 #if NMVPEX > 0
    909 	if (device_is_a(dev, "mvpex")) {
    910 #ifdef ORION
    911 		extern struct bus_space
    912 		    orion_pex0_io_bs_tag, orion_pex0_mem_bs_tag,
    913 		    orion_pex1_io_bs_tag, orion_pex1_mem_bs_tag;
    914 #endif
    915 #ifdef KIRKWOOD
    916 		extern struct bus_space
    917 		    kirkwood_pex_io_bs_tag, kirkwood_pex_mem_bs_tag,
    918 		    kirkwood_pex1_io_bs_tag, kirkwood_pex1_mem_bs_tag;
    919 #endif
    920 		extern struct arm32_pci_chipset arm32_mvpex0_chipset;
    921 #if defined(ORION) || defined(KIRKWOOD)
    922 		extern struct arm32_pci_chipset arm32_mvpex1_chipset;
    923 
    924 		struct marvell_attach_args *mva = aux;
    925 #endif
    926 		struct bus_space *mvpex_io_bs_tag, *mvpex_mem_bs_tag;
    927 		struct arm32_pci_chipset *arm32_mvpex_chipset;
    928 		prop_data_t io_bs_tag, mem_bs_tag, pc;
    929 		uint64_t start, end;
    930 		int iotag, memtag;
    931 
    932 		switch (mvsoc_model()) {
    933 #ifdef ORION
    934 		case MARVELL_ORION_1_88F5180N:
    935 		case MARVELL_ORION_1_88F5181:
    936 		case MARVELL_ORION_1_88F5182:
    937 		case MARVELL_ORION_1_88W8660:
    938 		case MARVELL_ORION_2_88F5281:
    939 			if (mva->mva_offset == MVSOC_PEX_BASE) {
    940 				mvpex_io_bs_tag = &orion_pex0_io_bs_tag;
    941 				mvpex_mem_bs_tag = &orion_pex0_mem_bs_tag;
    942 				arm32_mvpex_chipset = &arm32_mvpex0_chipset;
    943 				iotag = ORION_TAG_PEX0_IO;
    944 				memtag = ORION_TAG_PEX0_MEM;
    945 			} else {
    946 				mvpex_io_bs_tag = &orion_pex1_io_bs_tag;
    947 				mvpex_mem_bs_tag = &orion_pex1_mem_bs_tag;
    948 				arm32_mvpex_chipset = &arm32_mvpex1_chipset;
    949 				iotag = ORION_TAG_PEX1_IO;
    950 				memtag = ORION_TAG_PEX1_MEM;
    951 			}
    952 			break;
    953 #endif
    954 
    955 #ifdef KIRKWOOD
    956 		case MARVELL_KIRKWOOD_88F6282:
    957 			if (mva->mva_offset != MVSOC_PEX_BASE) {
    958 				mvpex_io_bs_tag = &kirkwood_pex1_io_bs_tag;
    959 				mvpex_mem_bs_tag = &kirkwood_pex1_mem_bs_tag;
    960 				arm32_mvpex_chipset = &arm32_mvpex1_chipset;
    961 				iotag = KIRKWOOD_TAG_PEX1_IO;
    962 				memtag = KIRKWOOD_TAG_PEX1_MEM;
    963 				break;
    964 			}
    965 
    966 			/* FALLTHROUGH */
    967 
    968 		case MARVELL_KIRKWOOD_88F6180:
    969 		case MARVELL_KIRKWOOD_88F6192:
    970 		case MARVELL_KIRKWOOD_88F6281:
    971 			mvpex_io_bs_tag = &kirkwood_pex_io_bs_tag;
    972 			mvpex_mem_bs_tag = &kirkwood_pex_mem_bs_tag;
    973 			arm32_mvpex_chipset = &arm32_mvpex0_chipset;
    974 			iotag = KIRKWOOD_TAG_PEX_IO;
    975 			memtag = KIRKWOOD_TAG_PEX_MEM;
    976 			break;
    977 #endif
    978 
    979 		default:
    980 			return;
    981 		}
    982 
    983 		arm32_mvpex_chipset->pc_conf_v = device_private(dev);
    984 		arm32_mvpex_chipset->pc_intr_v = device_private(dev);
    985 
    986 		io_bs_tag = prop_data_create_data_nocopy(
    987 		    mvpex_io_bs_tag, sizeof(struct bus_space));
    988 		KASSERT(io_bs_tag != NULL);
    989 		prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
    990 		prop_object_release(io_bs_tag);
    991 		mem_bs_tag = prop_data_create_data_nocopy(
    992 		    mvpex_mem_bs_tag, sizeof(struct bus_space));
    993 		KASSERT(mem_bs_tag != NULL);
    994 		prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
    995 		prop_object_release(mem_bs_tag);
    996 
    997 		pc = prop_data_create_data_nocopy(arm32_mvpex_chipset,
    998 		    sizeof(struct arm32_pci_chipset));
    999 		KASSERT(pc != NULL);
   1000 		prop_dictionary_set(dict, "pci-chipset", pc);
   1001 		prop_object_release(pc);
   1002 
   1003 		marvell_startend_by_tag(iotag, &start, &end);
   1004 		prop_dictionary_set_uint64(dict, "iostart", start);
   1005 		prop_dictionary_set_uint64(dict, "ioend", end);
   1006 		marvell_startend_by_tag(memtag, &start, &end);
   1007 		prop_dictionary_set_uint64(dict, "memstart", start);
   1008 		prop_dictionary_set_uint64(dict, "memend", end);
   1009 		prop_dictionary_set_uint32(dict,
   1010 		    "cache-line-size", arm_dcache_align);
   1011 	}
   1012 #endif
   1013 }
   1014 
   1015 #if NGTPCI > 0 || NMVPEX > 0
   1016 static void
   1017 marvell_startend_by_tag(int tag, uint64_t *start, uint64_t *end)
   1018 {
   1019 	uint32_t base, size;
   1020 	int win;
   1021 
   1022 	win = mvsoc_target(tag, NULL, NULL, &base, &size);
   1023 	if (size != 0) {
   1024 		if (win < nremap)
   1025 			*start = read_mlmbreg(MVSOC_MLMB_WRLR(win)) |
   1026 			    ((read_mlmbreg(MVSOC_MLMB_WRHR(win)) << 16) << 16);
   1027 		else
   1028 			*start = base;
   1029 		*end = *start + size - 1;
   1030 	}
   1031 }
   1032 #endif
   1033