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