Home | History | Annotate | Line # | Download | only in marvell
marvell_machdep.c revision 1.1
      1 /*	$NetBSD: marvell_machdep.c,v 1.1 2010/10/03 06:03:10 kiyohara 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.1 2010/10/03 06:03:10 kiyohara 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 dependant 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 char *boot_args = NULL;
    108 
    109 vm_offset_t physical_start;
    110 vm_offset_t physical_freestart;
    111 vm_offset_t physical_freeend;
    112 vm_offset_t physical_end;
    113 u_int free_pages;
    114 int physmem = 0;
    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 
    179 void
    180 cpu_reboot(int howto, char *bootstr)
    181 {
    182 
    183 	/*
    184 	 * If we are still cold then hit the air brakes
    185 	 * and crash to earth fast
    186 	 */
    187 	if (cold) {
    188 		doshutdownhooks();
    189 		printf("The operating system has halted.\r\n");
    190 		printf("Please press any key to reboot.\r\n");
    191 		cngetc();
    192 		printf("rebooting...\r\n");
    193 		cpu_reset();
    194 	}
    195 
    196 	/*
    197 	 * If RB_NOSYNC was not specified sync the discs.
    198 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    199 	 * unmount.  It looks like syslogd is getting woken up only to find
    200 	 * that it cannot page part of the binary in as the filesystem has
    201 	 * been unmounted.
    202 	 */
    203 	if (!(howto & RB_NOSYNC))
    204 		bootsync();
    205 
    206 	/* Say NO to interrupts */
    207 	splhigh();
    208 
    209 	/* Do a dump if requested. */
    210 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    211 		dumpsys();
    212 
    213 	/* Run any shutdown hooks */
    214 	doshutdownhooks();
    215 
    216 	/* Make sure IRQ's are disabled */
    217 	IRQdisable;
    218 
    219 	if (howto & RB_HALT) {
    220 		printf("The operating system has halted.\r\n");
    221 		printf("Please press any key to reboot.\r\n");
    222 		cngetc();
    223 	}
    224 
    225 	printf("rebooting...\r\n");
    226 	cpu_reset();
    227 
    228 	/*NOTREACHED*/
    229 }
    230 
    231 static inline
    232 pd_entry_t *
    233 read_ttb(void)
    234 {
    235 	long ttb;
    236 
    237 	__asm volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r" (ttb));
    238 
    239 	return (pd_entry_t *)(ttb & ~((1<<14)-1));
    240 }
    241 
    242 /*
    243  * Static device mappings. These peripheral registers are mapped at
    244  * fixed virtual addresses very early in initarm() so that we can use
    245  * them while booting the kernel, and stay at the same address
    246  * throughout whole kernel's life time.
    247  *
    248  * We use this table twice; once with bootstrap page table, and once
    249  * with kernel's page table which we build up in initarm().
    250  *
    251  * Since we map these registers into the bootstrap page table using
    252  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    253  * registers segment-aligned and segment-rounded in order to avoid
    254  * using the 2nd page tables.
    255  */
    256 #define _A(a)	((a) & ~L1_S_OFFSET)
    257 #define _S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    258 
    259 static const struct pmap_devmap marvell_devmap[] = {
    260 	{
    261 		MARVELL_INTERREGS_VBASE,
    262 		_A(MARVELL_INTERREGS_PBASE),
    263 		_S(MARVELL_INTERREGS_SIZE),
    264 		VM_PROT_READ|VM_PROT_WRITE,
    265 		PTE_NOCACHE,
    266 	},
    267 
    268 	{ 0, 0, 0, 0, 0 }
    269 };
    270 
    271 #undef  _A
    272 #undef  _S
    273 
    274 
    275 /*
    276  * u_int initarm(...)
    277  *
    278  * Initial entry point on startup. This gets called before main() is
    279  * entered.
    280  * It should be responsible for setting up everything that must be
    281  * in place when main is called.
    282  * This includes
    283  *   Taking a copy of the boot configuration structure.
    284  *   Initialising the physical console so characters can be printed.
    285  *   Setting up page tables for the kernel
    286  *   Relocating the kernel to the bottom of physical memory
    287  */
    288 u_int
    289 initarm(void *arg)
    290 {
    291 	uint32_t target, attr, base, size;
    292 	u_int l1pagetable;
    293 	int loop, pt_index, cs, memtag = 0, iotag = 0, window;
    294 
    295 	/* map some peripheral registers */
    296 	pmap_devmap_bootstrap((vaddr_t)read_ttb(), marvell_devmap);
    297 
    298 	mvsoc_bootstrap(MARVELL_INTERREGS_VBASE);
    299 
    300 	/* Get ready for splfoo() */
    301 	switch (mvsoc_model()) {
    302 #ifdef ORION
    303 	case MARVELL_ORION_1_88F1181:
    304 	case MARVELL_ORION_1_88F5082:
    305 	case MARVELL_ORION_1_88F5180N:
    306 	case MARVELL_ORION_1_88F5181:
    307 	case MARVELL_ORION_1_88F5182:
    308 	case MARVELL_ORION_1_88F6082:
    309 	case MARVELL_ORION_1_88F6183:
    310 	case MARVELL_ORION_1_88W8660:
    311 	case MARVELL_ORION_2_88F1281:
    312 	case MARVELL_ORION_2_88F5281:
    313 		orion_intr_bootstrap();
    314 
    315 		memtag = ORION_TAG_PEX0_MEM;
    316 		iotag = ORION_TAG_PEX0_IO;
    317 		nwindow = ORION_MLMB_NWINDOW;
    318 		nremap = ORION_MLMB_NREMAP;
    319 
    320 		orion_getclks(MARVELL_INTERREGS_VBASE);
    321 		if (mvTclk == 166666667)	/* 166MHz */
    322 			mvTclk = 166664740;	/* ???? */
    323 		break;
    324 #endif	/* ORION */
    325 
    326 #ifdef KIRKWOOD
    327 	case MARVELL_KIRKWOOD_88F6180:
    328 	case MARVELL_KIRKWOOD_88F6192:
    329 	case MARVELL_KIRKWOOD_88F6281:
    330 		kirkwood_intr_bootstrap();
    331 
    332 		memtag = KIRKWOOD_TAG_PEX_MEM;
    333 		iotag = KIRKWOOD_TAG_PEX_IO;
    334 		nwindow = KIRKWOOD_MLMB_NWINDOW;
    335 		nremap = KIRKWOOD_MLMB_NREMAP;
    336 
    337 		kirkwood_getclks(MARVELL_INTERREGS_VBASE);
    338 		break;
    339 #endif	/* KIRKWOOD */
    340 
    341 #ifdef MV78XX0
    342 	case MARVELL_MV78XX0_MV78100:
    343 	case MARVELL_MV78XX0_MV78200:
    344 		mv78xx0_intr_bootstrap();
    345 
    346 		memtag = MV78XX0_TAG_PEX_MEM;
    347 		iotag = MV78XX0_TAG_PEX_IO;
    348 		nwindow = MV78XX0_MLMB_NWINDOW;
    349 		nremap = MV78XX0_MLMB_NREMAP;
    350 
    351 		mv78xx0_getclks(MARVELL_INTERREGS_VBASE);
    352 		break;
    353 #endif	/* MV78XX0 */
    354 
    355 	default:
    356 		/* We can't output console here yet... */
    357 		panic("unknown model...\n");
    358 
    359 		/* NOTREACHED */
    360 	}
    361 
    362 	/* Reset PCI-Express space to window register. */
    363 	window = mvsoc_target(memtag, &target, &attr, NULL, NULL);
    364 	write_mlmbreg(MVSOC_MLMB_WCR(window),
    365 	    MVSOC_MLMB_WCR_WINEN |
    366 	    MVSOC_MLMB_WCR_TARGET(target) |
    367 	    MVSOC_MLMB_WCR_ATTR(attr) |
    368 	    MVSOC_MLMB_WCR_SIZE(MARVELL_PEXMEM_SIZE));
    369 	write_mlmbreg(MVSOC_MLMB_WBR(window),
    370 	    MARVELL_PEXMEM_PBASE & MVSOC_MLMB_WBR_BASE_MASK);
    371 #ifdef PCI_NETBSD_CONFIGURE
    372 	if (window < nremap) {
    373 		write_mlmbreg(MVSOC_MLMB_WRLR(window),
    374 		    MARVELL_PEXMEM_PBASE & MVSOC_MLMB_WRLR_REMAP_MASK);
    375 		write_mlmbreg(MVSOC_MLMB_WRHR(window), 0);
    376 	}
    377 #endif
    378 	window = mvsoc_target(iotag, &target, &attr, NULL, NULL);
    379 	write_mlmbreg(MVSOC_MLMB_WCR(window),
    380 	    MVSOC_MLMB_WCR_WINEN |
    381 	    MVSOC_MLMB_WCR_TARGET(target) |
    382 	    MVSOC_MLMB_WCR_ATTR(attr) |
    383 	    MVSOC_MLMB_WCR_SIZE(MARVELL_PEXIO_SIZE));
    384 	write_mlmbreg(MVSOC_MLMB_WBR(window),
    385 	    MARVELL_PEXIO_PBASE & MVSOC_MLMB_WBR_BASE_MASK);
    386 #ifdef PCI_NETBSD_CONFIGURE
    387 	if (window < nremap) {
    388 		write_mlmbreg(MVSOC_MLMB_WRLR(window),
    389 		    MARVELL_PEXIO_PBASE & MVSOC_MLMB_WRLR_REMAP_MASK);
    390 		write_mlmbreg(MVSOC_MLMB_WRHR(window), 0);
    391 	}
    392 #endif
    393 
    394 	/*
    395 	 * Heads up ... Setup the CPU / MMU / TLB functions
    396 	 */
    397 	if (set_cpufuncs())
    398 		panic("cpu not recognized!");
    399 
    400 	/*
    401 	 * U-Boot doesn't use the virtual memory.
    402 	 *
    403 	 * Physical Address Range     Description
    404 	 * -----------------------    ----------------------------------
    405 	 * 0x00000000 - 0x0fffffff    SDRAM Bank 0 (max 256MB)
    406 	 * 0x10000000 - 0x1fffffff    SDRAM Bank 1 (max 256MB)
    407 	 * 0x20000000 - 0x2fffffff    SDRAM Bank 2 (max 256MB)
    408 	 * 0x30000000 - 0x3fffffff    SDRAM Bank 3 (max 256MB)
    409 	 * 0xf1000000 - 0xf10fffff    SoC Internal Registers
    410 	 */
    411 
    412 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    413 
    414 	consinit();
    415 
    416 	/* Talk to the user */
    417 #define BDSTR(s)	_BDSTR(s)
    418 #define _BDSTR(s)	#s
    419 	printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
    420 
    421 #ifdef VERBOSE_INIT_ARM
    422 	printf("initarm: Configuring system ...\n");
    423 #endif
    424 
    425 	bootconfig.dramblocks = 0;
    426 	physical_end = physmem = 0;
    427 	for (cs = MARVELL_TAG_SDRAM_CS0; cs <= MARVELL_TAG_SDRAM_CS3; cs++) {
    428 		mvsoc_target(cs, &target, &attr, &base, &size);
    429 		if (size == 0)
    430 			continue;
    431 
    432 		bootconfig.dram[bootconfig.dramblocks].address = base;
    433 		bootconfig.dram[bootconfig.dramblocks].pages = size / PAGE_SIZE;
    434 
    435 		if (base != physical_end)
    436 			panic("memory hole not support");
    437 
    438 		physical_end += size;
    439 		physmem += size / PAGE_SIZE;
    440 
    441 		bootconfig.dramblocks++;
    442 	}
    443 
    444 	/*
    445 	 * Set up the variables that define the availablilty of
    446 	 * physical memory.  For now, we're going to set
    447 	 * physical_freestart to 0xa0008000 (where the kernel
    448 	 * was loaded), and allocate the memory we need downwards.
    449 	 * If we get too close to the L1 table that we set up, we
    450 	 * will panic.  We will update physical_freestart and
    451 	 * physical_freeend later to reflect what pmap_bootstrap()
    452 	 * wants to see.
    453 	 *
    454 	 * XXX pmap_bootstrap() needs an enema.
    455 	 */
    456 	physical_start = bootconfig.dram[0].address;
    457 
    458 	/*
    459 	 * Our kernel is at the beginning of memory, so set our free space to
    460 	 * all the memory after the kernel.
    461 	 */
    462 	physical_freestart = KERN_VTOPHYS(round_page((vaddr_t)_end));
    463 	physical_freeend = physical_end;
    464 
    465 #ifdef VERBOSE_INIT_ARM
    466 	/* Tell the user about the memory */
    467 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    468 	    physical_start, physical_end - 1);
    469 #endif
    470 
    471 	/*
    472 	 * Okay, the kernel starts 8kB in from the bottom of physical
    473 	 * memory.  We are going to allocate our bootstrap pages upwards
    474 	 * from physical_freestart.
    475 	 *
    476 	 * We need to allocate some fixed page tables to get the kernel
    477 	 * going.  We allocate one page directory and a number of page
    478 	 * tables and store the physical addresses in the kernel_pt_table
    479 	 * array.
    480 	 *
    481 	 * The kernel page directory must be on a 16K boundary.  The page
    482 	 * tables must be on 4K bounaries.  What we do is allocate the
    483 	 * page directory on the first 16K boundary that we encounter, and
    484 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    485 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    486 	 * least one 16K aligned region.
    487 	 */
    488 
    489 #ifdef VERBOSE_INIT_ARM
    490 	printf("Allocating page tables\n");
    491 #endif
    492 
    493 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    494 
    495 #ifdef VERBOSE_INIT_ARM
    496 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    497 	    physical_freestart, free_pages, free_pages);
    498 #endif
    499 
    500 	/*
    501 	 * Define a macro to simplify memory allocation.  As we allocate the
    502 	 * memory, make sure that we don't walk over our temporary first level
    503 	 * translation table.
    504 	 */
    505 #define valloc_pages(var, np)						\
    506 	(var).pv_pa = physical_freestart;				\
    507 	physical_freestart += ((np) * PAGE_SIZE);			\
    508 	if (physical_freestart > (physical_freeend - L1_TABLE_SIZE))	\
    509 		panic("initarm: out of memory");			\
    510 	free_pages -= (np);						\
    511 	(var).pv_va = KERN_PHYSTOV((var).pv_pa);			\
    512 	memset((char *)(var).pv_va, 0, ((np) * PAGE_SIZE));
    513 
    514 	pt_index = 0;
    515 	kernel_l1pt.pv_pa = 0;
    516 	kernel_l1pt.pv_va = 0;
    517 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    518 		/* Are we 16KB aligned for an L1 ? */
    519 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0 &&
    520 		    kernel_l1pt.pv_pa == 0) {
    521 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    522 		} else {
    523 			valloc_pages(kernel_pt_table[pt_index],
    524 			    L2_TABLE_SIZE / PAGE_SIZE);
    525 			++pt_index;
    526 		}
    527 	}
    528 
    529 	/* This should never be able to happen but better confirm that. */
    530 	if (!kernel_l1pt.pv_pa ||
    531 	    (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
    532 		panic("initarm: Failed to align the kernel page directory");
    533 
    534 	/*
    535 	 * Allocate a page for the system page mapped to V0x00000000
    536 	 * This page will just contain the system vectors and can be
    537 	 * shared by all processes.
    538 	 */
    539 	valloc_pages(systempage, 1);
    540 	systempage.pv_va = 0x00000000;
    541 
    542 	/* Allocate stacks for all modes */
    543 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    544 	valloc_pages(abtstack, ABT_STACK_SIZE);
    545 	valloc_pages(undstack, UND_STACK_SIZE);
    546 	valloc_pages(kernelstack, UPAGES);
    547 
    548 #ifdef VERBOSE_INIT_ARM
    549 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    550 	    irqstack.pv_va);
    551 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    552 	    abtstack.pv_va);
    553 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    554 	    undstack.pv_va);
    555 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    556 	    kernelstack.pv_va);
    557 #endif
    558 
    559 	/* Allocate the message buffer. */
    560 	{
    561 		pv_addr_t msgbuf;
    562 
    563 		valloc_pages(msgbuf, round_page(MSGBUFSIZE) / PAGE_SIZE);
    564 		msgbufphys = msgbuf.pv_pa;
    565 	}
    566 
    567 	/*
    568 	 * Ok we have allocated physical pages for the primary kernel
    569 	 * page tables
    570 	 */
    571 
    572 #ifdef VERBOSE_INIT_ARM
    573 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    574 #endif
    575 
    576 	/*
    577 	 * Now we start construction of the L1 page table
    578 	 * We start by mapping the L2 page tables into the L1.
    579 	 * This means that we can replace L1 mappings later on if necessary
    580 	 */
    581 	l1pagetable = kernel_l1pt.pv_va;
    582 
    583 	/* Map the L2 pages tables in the L1 page table */
    584 	pmap_link_l2pt(l1pagetable, 0x00000000,
    585 	    &kernel_pt_table[KERNEL_PT_SYS]);
    586 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    587 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    588 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    589 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    590 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    591 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    592 
    593 	/* update the top of the kernel VM */
    594 	pmap_curmaxkvaddr =
    595 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    596 
    597 #ifdef VERBOSE_INIT_ARM
    598 	printf("Mapping kernel\n");
    599 #endif
    600 
    601 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    602 	{
    603 		extern char etext[], _end[];
    604 		size_t textsize = (uintptr_t)etext - KERNEL_TEXT_BASE;
    605 		size_t totalsize = (uintptr_t)_end - KERNEL_TEXT_BASE;
    606 		u_int logical;
    607 
    608 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    609 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    610 
    611 		logical = 0x00000000;	/* offset of kernel in RAM */
    612 
    613 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    614 		    physical_start + logical, textsize,
    615 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    616 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    617 		    physical_start + logical, totalsize - textsize,
    618 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    619 	}
    620 
    621 #ifdef VERBOSE_INIT_ARM
    622 	printf("Constructing L2 page tables\n");
    623 #endif
    624 
    625 	/* Map the stack pages */
    626 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    627 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    628 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    629 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    630 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    631 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    632 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    633 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
    634 
    635 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    636 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
    637 
    638 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop)
    639 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    640 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    641 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    642 
    643 	/* Map the vector page. */
    644 	pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
    645 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    646 
    647 	/*
    648 	 * Map integrated peripherals at same address in first level page
    649 	 * table so that we can continue to use console.
    650 	 */
    651 	pmap_devmap_bootstrap(l1pagetable, marvell_devmap);
    652 
    653 	/*
    654 	 * Now we have the real page tables in place so we can switch to them.
    655 	 * Once this is done we will be running with the REAL kernel page
    656 	 * tables.
    657 	 */
    658 
    659 	/* Switch tables */
    660 #ifdef VERBOSE_INIT_ARM
    661 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    662 #endif
    663 
    664 	cpu_setttb(kernel_l1pt.pv_pa);
    665 	cpu_tlb_flushID();
    666 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    667 
    668 	/*
    669 	 * Moved from cpu_startup() as data_abort_handler() references
    670 	 * this during uvm init.
    671 	 */
    672 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    673 
    674 #ifdef VERBOSE_INIT_ARM
    675 	printf("bootstrap done.\n");
    676 #endif
    677 
    678 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    679 
    680 	/*
    681 	 * Pages were allocated during the secondary bootstrap for the
    682 	 * stacks for different CPU modes.
    683 	 * We must now set the r13 registers in the different CPU modes to
    684 	 * point to these stacks.
    685 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    686 	 * of the stack memory.
    687 	 */
    688 #ifdef VERBOSE_INIT_ARM
    689 	printf("init subsystems: stacks ");
    690 #endif
    691 
    692 	set_stackptr(PSR_IRQ32_MODE,
    693 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    694 	set_stackptr(PSR_ABT32_MODE,
    695 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    696 	set_stackptr(PSR_UND32_MODE,
    697 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    698 
    699 	/*
    700 	 * Well we should set a data abort handler.
    701 	 * Once things get going this will change as we will need a proper
    702 	 * handler.
    703 	 * Until then we will use a handler that just panics but tells us
    704 	 * why.
    705 	 * Initialisation of the vectors will just panic on a data abort.
    706 	 * This just fills in a slightly better one.
    707 	 */
    708 #ifdef VERBOSE_INIT_ARM
    709 	printf("vectors ");
    710 #endif
    711 	data_abort_handler_address = (u_int)data_abort_handler;
    712 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    713 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    714 
    715 	/* Initialise the undefined instruction handlers */
    716 #ifdef VERBOSE_INIT_ARM
    717 	printf("undefined ");
    718 #endif
    719 	undefined_init();
    720 
    721 	/* Load memory into UVM. */
    722 #ifdef VERBOSE_INIT_ARM
    723 	printf("page ");
    724 #endif
    725 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    726 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    727 	    atop(physical_freestart), atop(physical_freeend),
    728 	    VM_FREELIST_DEFAULT);
    729 
    730 	/* Boot strap pmap telling it where the kernel page table is */
    731 #ifdef VERBOSE_INIT_ARM
    732 	printf("pmap ");
    733 #endif
    734 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    735 
    736 #ifdef VERBOSE_INIT_ARM
    737 	printf("done.\n");
    738 #endif
    739 
    740 #ifdef __HAVE_MEMORY_DISK__
    741 	md_root_setconf(memory_disk, sizeof memory_disk);
    742 #endif
    743 
    744 #ifdef BOOTHOWTO
    745 	boothowto |= BOOTHOWTO;
    746 #endif
    747 
    748 #ifdef KGDB
    749 	if (boothowto & RB_KDB) {
    750 		kgdb_debug_init = 1;
    751 		kgdb_connect(1);
    752 	}
    753 #endif
    754 
    755 #if NKSYMS || defined(DDB) || defined(LKM)
    756 	/* Firmware doesn't load symbols. */
    757 	ksyms_init();
    758 #endif
    759 
    760 #ifdef DDB
    761 	db_machine_init();
    762 	if (boothowto & RB_KDB)
    763 		Debugger();
    764 #endif
    765 
    766 	/* we've a specific device_register routine */
    767 	evbarm_device_register = marvell_device_register;
    768 
    769 	/* We return the new stack pointer address */
    770 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    771 }
    772 
    773 void
    774 consinit(void)
    775 {
    776 	static int consinit_called = 0;
    777 
    778 	if (consinit_called != 0)
    779 		return;
    780 
    781 	consinit_called = 1;
    782 
    783 #if NCOM > 0
    784 	{
    785 		extern int mvuart_cnattach(bus_space_tag_t, bus_addr_t, int,
    786 					   uint32_t, int);
    787 
    788 		if (mvuart_cnattach(&mvsoc_bs_tag,
    789 		    MARVELL_INTERREGS_VBASE + MVSOC_COM0_BASE,
    790 		    comcnspeed, mvTclk, comcnmode))
    791 			panic("can't init serial console");
    792 	}
    793 #else
    794 	panic("serial console not configured");
    795 #endif
    796 }
    797 
    798 
    799 static void
    800 marvell_device_register(device_t dev, void *aux)
    801 {
    802 	prop_dictionary_t dict = device_properties(dev);
    803 
    804 #if NCOM > 0
    805 	if (device_is_a(dev, "com") &&
    806 	    device_is_a(device_parent(dev), "mvsoc"))
    807 		prop_dictionary_set_uint32(dict, "frequency", mvTclk);
    808 #endif
    809 	if (device_is_a(dev, "gtidmac")) {
    810 		prop_dictionary_set_uint32(dict,
    811 		    "dmb_speed", mvTclk * sizeof(uint32_t));	/* XXXXXX */
    812 		prop_dictionary_set_uint32(dict,
    813 		    "xore-irq-begin", ORION_IRQ_XOR0);
    814 	}
    815 #if NGTPCI > 0 && defined(ORION)
    816 	if (device_is_a(dev, "gtpci")) {
    817 		extern struct bus_space
    818 		    orion_pci_io_bs_tag, orion_pci_mem_bs_tag;
    819 		extern struct arm32_pci_chipset arm32_gtpci_chipset;
    820 
    821 		prop_data_t io_bs_tag, mem_bs_tag, pc;
    822 		prop_array_t int2gpp;
    823 		prop_number_t gpp;
    824 		uint64_t start, end;
    825 		int i, j;
    826 		static struct {
    827 			const char *boardtype;
    828 			int pin[PCI_INTERRUPT_PIN_MAX];
    829 		} hints[] = {
    830 			{ "kuronas_x4",
    831 			    { 11, PCI_INTERRUPT_PIN_NONE } },
    832 
    833 			{ NULL,
    834 			    { PCI_INTERRUPT_PIN_NONE } },
    835 		};
    836 
    837 		arm32_gtpci_chipset.pc_conf_v = device_private(dev);
    838 		arm32_gtpci_chipset.pc_intr_v = device_private(dev);
    839 
    840 		io_bs_tag = prop_data_create_data_nocopy(
    841 		    &orion_pci_io_bs_tag, sizeof(struct bus_space));
    842 		KASSERT(io_bs_tag != NULL);
    843 		prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
    844 		prop_object_release(io_bs_tag);
    845 		mem_bs_tag = prop_data_create_data_nocopy(
    846 		    &orion_pci_mem_bs_tag, sizeof(struct bus_space));
    847 		KASSERT(mem_bs_tag != NULL);
    848 		prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
    849 		prop_object_release(mem_bs_tag);
    850 
    851 		pc = prop_data_create_data_nocopy(&arm32_gtpci_chipset,
    852 		    sizeof(struct arm32_pci_chipset));
    853 		KASSERT(pc != NULL);
    854 		prop_dictionary_set(dict, "pci-chipset", pc);
    855 		prop_object_release(pc);
    856 
    857 		marvell_startend_by_tag(ORION_TAG_PCI_IO, &start, &end);
    858 		prop_dictionary_set_uint64(dict, "iostart", start);
    859 		prop_dictionary_set_uint64(dict, "ioend", end);
    860 		marvell_startend_by_tag(ORION_TAG_PCI_MEM, &start, &end);
    861 		prop_dictionary_set_uint64(dict, "memstart", start);
    862 		prop_dictionary_set_uint64(dict, "memend", end);
    863 		prop_dictionary_set_uint32(dict,
    864 		    "cache-line-size", arm_dcache_align);
    865 
    866 		/* Setup the hint for interrupt-pin. */
    867 #define BDSTR(s)		_BDSTR(s)
    868 #define _BDSTR(s)		#s
    869 #define THIS_BOARD(str)		(strcmp(str, BDSTR(EVBARM_BOARDTYPE)) == 0)
    870 		for (i = 0; hints[i].boardtype != NULL; i++)
    871 			if (THIS_BOARD(hints[i].boardtype))
    872 				break;
    873 		if (hints[i].boardtype == NULL)
    874 			return;
    875 
    876 		int2gpp =
    877 		    prop_array_create_with_capacity(PCI_INTERRUPT_PIN_MAX + 1);
    878 
    879 		/* first set dummy */
    880 		gpp = prop_number_create_integer(0);
    881 		prop_array_add(int2gpp, gpp);
    882 		prop_object_release(gpp);
    883 
    884 		for (j = 0; hints[i].pin[j] != PCI_INTERRUPT_PIN_NONE; j++) {
    885 			gpp = prop_number_create_integer(hints[i].pin[j]);
    886 			prop_array_add(int2gpp, gpp);
    887 			prop_object_release(gpp);
    888 		}
    889 		prop_dictionary_set(dict, "int2gpp", int2gpp);
    890 	}
    891 #endif	/* NGTPCI > 0 && defined(ORION) */
    892 #if NMVPEX > 0
    893 	if (device_is_a(dev, "mvpex")) {
    894 #ifdef ORION
    895 		extern struct bus_space
    896 		    orion_pex0_io_bs_tag, orion_pex0_mem_bs_tag,
    897 		    orion_pex1_io_bs_tag, orion_pex1_mem_bs_tag;
    898 #endif
    899 #ifdef KIRKWOOD
    900 		extern struct bus_space
    901 		    kirkwood_pex_io_bs_tag, kirkwood_pex_mem_bs_tag;
    902 #endif
    903 		extern struct arm32_pci_chipset
    904 		    arm32_mvpex0_chipset, arm32_mvpex1_chipset;
    905 
    906 		struct marvell_attach_args *mva = aux;
    907 		struct bus_space *mvpex_io_bs_tag, *mvpex_mem_bs_tag;
    908 		struct arm32_pci_chipset *arm32_mvpex_chipset;
    909 		prop_data_t io_bs_tag, mem_bs_tag, pc;
    910 		uint64_t start, end;
    911 		int iotag, memtag;
    912 
    913 		switch (mvsoc_model()) {
    914 #ifdef ORION
    915 		case MARVELL_ORION_1_88F5180N:
    916 		case MARVELL_ORION_1_88F5181:
    917 		case MARVELL_ORION_1_88F5182:
    918 		case MARVELL_ORION_1_88W8660:
    919 		case MARVELL_ORION_2_88F5281:
    920 			if (mva->mva_offset == MVSOC_PEX_BASE) {
    921 				mvpex_io_bs_tag = &orion_pex0_io_bs_tag;
    922 				mvpex_mem_bs_tag = &orion_pex0_mem_bs_tag;
    923 				arm32_mvpex_chipset = &arm32_mvpex0_chipset;
    924 				iotag = ORION_TAG_PEX0_IO;
    925 				memtag = ORION_TAG_PEX0_MEM;
    926 			} else {
    927 				mvpex_io_bs_tag = &orion_pex1_io_bs_tag;
    928 				mvpex_mem_bs_tag = &orion_pex1_mem_bs_tag;
    929 				arm32_mvpex_chipset = &arm32_mvpex1_chipset;
    930 				iotag = ORION_TAG_PEX1_IO;
    931 				memtag = ORION_TAG_PEX1_MEM;
    932 			}
    933 			break;
    934 #endif
    935 
    936 #ifdef KIRKWOOD
    937 		case MARVELL_KIRKWOOD_88F6180:
    938 		case MARVELL_KIRKWOOD_88F6192:
    939 		case MARVELL_KIRKWOOD_88F6281:
    940 			mvpex_io_bs_tag = &kirkwood_pex_io_bs_tag;
    941 			mvpex_mem_bs_tag = &kirkwood_pex_mem_bs_tag;
    942 			arm32_mvpex_chipset = &arm32_mvpex0_chipset;
    943 			iotag = KIRKWOOD_TAG_PEX_IO;
    944 			memtag = KIRKWOOD_TAG_PEX_MEM;
    945 			break;
    946 #endif
    947 
    948 		default:
    949 			return;
    950 		}
    951 
    952 		arm32_mvpex_chipset->pc_conf_v = device_private(dev);
    953 		arm32_mvpex_chipset->pc_intr_v = device_private(dev);
    954 
    955 		io_bs_tag = prop_data_create_data_nocopy(
    956 		    mvpex_io_bs_tag, sizeof(struct bus_space));
    957 		KASSERT(io_bs_tag != NULL);
    958 		prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
    959 		prop_object_release(io_bs_tag);
    960 		mem_bs_tag = prop_data_create_data_nocopy(
    961 		    mvpex_mem_bs_tag, sizeof(struct bus_space));
    962 		KASSERT(mem_bs_tag != NULL);
    963 		prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
    964 		prop_object_release(mem_bs_tag);
    965 
    966 		pc = prop_data_create_data_nocopy(arm32_mvpex_chipset,
    967 		    sizeof(struct arm32_pci_chipset));
    968 		KASSERT(pc != NULL);
    969 		prop_dictionary_set(dict, "pci-chipset", pc);
    970 		prop_object_release(pc);
    971 
    972 		marvell_startend_by_tag(iotag, &start, &end);
    973 		prop_dictionary_set_uint64(dict, "iostart", start);
    974 		prop_dictionary_set_uint64(dict, "ioend", end);
    975 		marvell_startend_by_tag(memtag, &start, &end);
    976 		prop_dictionary_set_uint64(dict, "memstart", start);
    977 		prop_dictionary_set_uint64(dict, "memend", end);
    978 		prop_dictionary_set_uint32(dict,
    979 		    "cache-line-size", arm_dcache_align);
    980 	}
    981 #endif
    982 }
    983 
    984 #if NGTPCI > 0 || NMVPEX > 0
    985 static void
    986 marvell_startend_by_tag(int tag, uint64_t *start, uint64_t *end)
    987 {
    988 	uint32_t base, size;
    989 	int win;
    990 
    991 	win = mvsoc_target(tag, NULL, NULL, &base, &size);
    992 	if (size != 0) {
    993 		if (win < nremap)
    994 			*start = read_mlmbreg(MVSOC_MLMB_WRLR(win)) |
    995 			    ((read_mlmbreg(MVSOC_MLMB_WRHR(win)) << 16) << 16);
    996 		else
    997 			*start = base;
    998 		*end = *start + size - 1;
    999 	}
   1000 }
   1001 #endif
   1002