Home | History | Annotate | Line # | Download | only in vr
vr.c revision 1.33
      1 /*	$NetBSD: vr.c,v 1.33 2001/12/29 04:27:33 takemura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999-2001
      5  *         Shin Takemura and PocketBSD Project. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the PocketBSD project
     18  *	and its contributors.
     19  * 4. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include "opt_vr41xx.h"
     38 #include "opt_tx39xx.h"
     39 #include "opt_kgdb.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/reboot.h>
     44 
     45 #include <uvm/uvm_extern.h>
     46 
     47 #include <machine/sysconf.h>
     48 #include <machine/bootinfo.h>
     49 #include <machine/bus.h>
     50 #include <machine/bus_space_hpcmips.h>
     51 
     52 #include <dev/hpc/hpckbdvar.h>
     53 
     54 #include <hpcmips/hpcmips/machdep.h>	/* cpu_name, mem_cluster */
     55 
     56 #include <hpcmips/vr/vr.h>
     57 #include <hpcmips/vr/vr_asm.h>
     58 #include <hpcmips/vr/vrcpudef.h>
     59 #include <hpcmips/vr/vripreg.h>
     60 #include <hpcmips/vr/rtcreg.h>
     61 
     62 #include "vrip.h"
     63 #if NVRIP > 0
     64 #include <hpcmips/vr/vripvar.h>
     65 #endif
     66 
     67 #include "vrbcu.h"
     68 #if NVRBCU > 0
     69 #include <hpcmips/vr/bcuvar.h>
     70 #endif
     71 
     72 #include "vrdsu.h"
     73 #if NVRDSU > 0
     74 #include <hpcmips/vr/vrdsuvar.h>
     75 #endif
     76 
     77 #include "com.h"
     78 #include "sg2com_vrip.h"
     79 #if NCOM > 0 || NSG2COM_VRIP > 0
     80 #include <sys/termios.h>
     81 #include <sys/ttydefaults.h>
     82 #include <dev/ic/comreg.h>
     83 #include <dev/ic/comvar.h>
     84 #if NCOM > 0
     85 #include <hpcmips/vr/siureg.h>
     86 #include <hpcmips/vr/com_vripvar.h>
     87 #endif
     88 #if NSG2COM_VRIP > 0
     89 #include <hpcmips/vr/sg2comreg.h>
     90 #include <hpcmips/vr/sg2com_vripvar.h>
     91 #endif
     92 #ifndef CONSPEED
     93 #define CONSPEED TTYDEF_SPEED
     94 #endif
     95 #endif
     96 
     97 #include "hpcfb.h"
     98 #include "vrkiu.h"
     99 #if (NVRKIU > 0) || (NHPCFB > 0)
    100 #include <dev/wscons/wsdisplayvar.h>
    101 #include <dev/rasops/rasops.h>
    102 #endif
    103 
    104 #if NHPCFB > 0
    105 #include <dev/hpc/hpcfbvar.h>
    106 #endif
    107 
    108 #if NVRKIU > 0
    109 #include <arch/hpcmips/vr/vrkiureg.h>
    110 #include <arch/hpcmips/vr/vrkiuvar.h>
    111 #endif
    112 
    113 #ifdef DEBUG
    114 #define STATIC
    115 #else
    116 #define STATIC	static
    117 #endif
    118 
    119 /*
    120  * This is a mask of bits to clear in the SR when we go to a
    121  * given interrupt priority level.
    122  */
    123 const u_int32_t __ipl_sr_bits_vr[_IPL_N] = {
    124 	0,					/* IPL_NONE */
    125 
    126 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFT */
    127 
    128 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTCLOCK */
    129 
    130 	MIPS_SOFT_INT_MASK_0|
    131 		MIPS_SOFT_INT_MASK_1,		/* IPL_SOFTNET */
    132 
    133 	MIPS_SOFT_INT_MASK_0|
    134 		MIPS_SOFT_INT_MASK_1,		/* IPL_SOFTSERIAL */
    135 
    136 	MIPS_SOFT_INT_MASK_0|
    137 		MIPS_SOFT_INT_MASK_1|
    138 		MIPS_INT_MASK_0,		/* IPL_BIO */
    139 
    140 	MIPS_SOFT_INT_MASK_0|
    141 		MIPS_SOFT_INT_MASK_1|
    142 		MIPS_INT_MASK_0,		/* IPL_NET */
    143 
    144 	MIPS_SOFT_INT_MASK_0|
    145 		MIPS_SOFT_INT_MASK_1|
    146 		MIPS_INT_MASK_0,		/* IPL_{TTY,SERIAL} */
    147 
    148 	MIPS_SOFT_INT_MASK_0|
    149 		MIPS_SOFT_INT_MASK_1|
    150 		MIPS_INT_MASK_0|
    151 		MIPS_INT_MASK_1,		/* IPL_{CLOCK,HIGH} */
    152 };
    153 
    154 #if defined(VR41XX) && defined(TX39XX)
    155 #define	VR_INTR	vr_intr
    156 #else
    157 #define	VR_INTR	cpu_intr	/* locore_mips3 directly call this */
    158 #endif
    159 
    160 void vr_init(void);
    161 void VR_INTR(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
    162 extern void vr_idle(void);
    163 STATIC void vr_cons_init(void);
    164 STATIC void vr_fb_init(caddr_t *);
    165 STATIC void vr_mem_init(paddr_t);
    166 STATIC void vr_find_dram(paddr_t, paddr_t);
    167 STATIC void vr_reboot(int, char *);
    168 
    169 /*
    170  * CPU interrupt dispatch table (HwInt[0:3])
    171  */
    172 STATIC int vr_null_handler(void *, u_int32_t, u_int32_t);
    173 STATIC int (*vr_intr_handler[4])(void *, u_int32_t, u_int32_t) =
    174 {
    175 	vr_null_handler,
    176 	vr_null_handler,
    177 	vr_null_handler,
    178 	vr_null_handler
    179 };
    180 STATIC void *vr_intr_arg[4];
    181 
    182 void
    183 vr_init()
    184 {
    185 	/*
    186 	 * Platform Specific Function Hooks
    187 	 */
    188 	platform.cpu_idle	= vr_idle;
    189 	platform.cpu_intr	= VR_INTR;
    190 	platform.cons_init	= vr_cons_init;
    191 	platform.fb_init	= vr_fb_init;
    192 	platform.mem_init	= vr_mem_init;
    193 	platform.reboot		= vr_reboot;
    194 
    195 #if NVRBCU > 0
    196 	sprintf(cpu_name, "NEC %s rev%d.%d %d.%03dMHz",
    197 		vrbcu_vrip_getcpuname(),
    198 		vrbcu_vrip_getcpumajor(),
    199 		vrbcu_vrip_getcpuminor(),
    200 		vrbcu_vrip_getcpuclock() / 1000000,
    201 		(vrbcu_vrip_getcpuclock() % 1000000) / 1000);
    202 #else
    203 	sprintf(cpu_name, "NEC VR41xx");
    204 #endif
    205 }
    206 
    207 void
    208 vr_mem_init(paddr_t kernend)
    209 {
    210 
    211 	mem_clusters[0].start = 0;
    212 	mem_clusters[0].size = kernend;
    213 	mem_cluster_cnt = 1;
    214 
    215 	vr_find_dram(kernend, 0x02000000);
    216 	vr_find_dram(0x02000000, 0x04000000);
    217 	vr_find_dram(0x04000000, 0x06000000);
    218 	vr_find_dram(0x06000000, 0x08000000);
    219 
    220 	/* Clear currently unused D-RAM area (For reboot Windows CE clearly)*/
    221 	memset((void *)(KERNBASE + 0x400), 0, KERNTEXTOFF - (KERNBASE + 0x800));
    222 }
    223 
    224 void
    225 vr_find_dram(paddr_t addr, paddr_t end)
    226 {
    227 	int n;
    228 	caddr_t page;
    229 #ifdef NARLY_MEMORY_PROBE
    230 	int x, i;
    231 #endif
    232 
    233 #ifdef VR_FIND_DRAMLIM
    234 	if (VR_FIND_DRAMLIM < end)
    235 		end = VR_FIND_DRAMLIM;
    236 #endif /* VR_FIND_DRAMLIM */
    237 	n = mem_cluster_cnt;
    238 	for (; addr < end; addr += NBPG) {
    239 
    240 		page = (void *)MIPS_PHYS_TO_KSEG1(addr);
    241 		if (badaddr(page, 4))
    242 			goto bad;
    243 
    244 		/* stop memory probing at first memory image */
    245 		if (bcmp(page, (void *)MIPS_PHYS_TO_KSEG0(0), 128) == 0)
    246 			return;
    247 
    248 		*(volatile int *)(page+0) = 0xa5a5a5a5;
    249 		*(volatile int *)(page+4) = 0x5a5a5a5a;
    250 		wbflush();
    251 		if (*(volatile int *)(page+0) != 0xa5a5a5a5)
    252 			goto bad;
    253 
    254 		*(volatile int *)(page+0) = 0x5a5a5a5a;
    255 		*(volatile int *)(page+4) = 0xa5a5a5a5;
    256 		wbflush();
    257 		if (*(volatile int *)(page+0) != 0x5a5a5a5a)
    258 			goto bad;
    259 
    260 #ifdef NARLY_MEMORY_PROBE
    261 		x = random();
    262 		for (i = 0; i < NBPG; i += 4)
    263 			*(volatile int *)(page+i) = (x ^ i);
    264 		wbflush();
    265 		for (i = 0; i < NBPG; i += 4)
    266 			if (*(volatile int *)(page+i) != (x ^ i))
    267 				goto bad;
    268 
    269 		x = random();
    270 		for (i = 0; i < NBPG; i += 4)
    271 			*(volatile int *)(page+i) = (x ^ i);
    272 		wbflush();
    273 		for (i = 0; i < NBPG; i += 4)
    274 			if (*(volatile int *)(page+i) != (x ^ i))
    275 				goto bad;
    276 #endif /* NARLY_MEMORY_PROBE */
    277 
    278 		if (!mem_clusters[n].size)
    279 			mem_clusters[n].start = addr;
    280 		mem_clusters[n].size += NBPG;
    281 		continue;
    282 
    283 	bad:
    284 		if (mem_clusters[n].size)
    285 			++n;
    286 		continue;
    287 	}
    288 	if (mem_clusters[n].size)
    289 		++n;
    290 	mem_cluster_cnt = n;
    291 }
    292 
    293 void
    294 vr_fb_init(caddr_t *kernend)
    295 {
    296 	/* Nothing to do */
    297 }
    298 
    299 void
    300 vr_cons_init()
    301 {
    302 #if NCOM > 0 || NSG2COM_VRIP > 0 || NHPCFB > 0 || NVRKIU > 0
    303 	bus_space_tag_t iot = hpcmips_system_bus_space();
    304 #endif
    305 
    306 #if NCOM > 0
    307 #ifdef KGDB
    308 	/* if KGDB is defined, always use the serial port for KGDB */
    309 	if (com_vrip_cndb_attach(iot, VRIP_SIU_ADDR, 9600, VRCOM_FREQ,
    310 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
    311 		printf("%s(%d): can't init kgdb's serial port",
    312 		    __FILE__, __LINE__);
    313 	}
    314 #else /* KGDB */
    315 	if (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) {
    316 		/* Serial console */
    317 		if (com_vrip_cndb_attach(iot, VRIP_SIU_ADDR, CONSPEED,
    318 		    VRCOM_FREQ, (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)) {
    319 			printf("%s(%d): can't init serial console",
    320 			    __FILE__, __LINE__);
    321 		} else {
    322 #if NSG2COM_VRIP == 0
    323 			return;
    324 #endif
    325 		}
    326 	}
    327 #endif /* KGDB */
    328 #endif /* NCOM > 0 */
    329 
    330 #if NSG2COM_VRIP > 0
    331 #ifdef KGDB
    332 	/* if KGDB is defined, always use the serial port for KGDB */
    333 	if (sg2com_vrip_cndb_attach(iot, SG2COM_VRIP_ADDR, 9600, SG2COM_FREQ,
    334 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
    335 		printf("%s(%d): can't init kgdb's serial port",
    336 		    __FILE__, __LINE__);
    337 	}
    338 #else /* KGDB */
    339 	if (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) {
    340 		/* Serial console */
    341 		if (sg2com_vrip_cndb_attach(iot, SG2COM_ADDR, CONSPEED,
    342 		    SG2COM_FREQ, (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)){
    343 			printf("%s(%d): can't init serial console",
    344 			    __FILE__, __LINE__);
    345 		} else {
    346 			return;
    347 		}
    348 	}
    349 #endif /* KGDB */
    350 #endif /* NSG2COM_VRIP > 0 */
    351 
    352 #if NHPCFB > 0
    353 	if (hpcfb_cnattach(NULL)) {
    354 		printf("%s(%d): can't init fb console", __FILE__, __LINE__);
    355 	} else {
    356 		goto find_keyboard;
    357 	}
    358  find_keyboard:
    359 #endif /* NHPCFB > 0 */
    360 
    361 #if NVRKIU > 0 && VRIP_KIU_ADDR != VRIP_NO_ADDR
    362 	if (vrkiu_cnattach(iot, VRIP_KIU_ADDR)) {
    363 		printf("%s(%d): can't init vrkiu as console",
    364 		       __FILE__, __LINE__);
    365 	} else {
    366 		return;
    367 	}
    368 #endif /* NVRKIU > 0 && VRIP_KIU_ADDR != VRIP_NO_ADDR */
    369 }
    370 
    371 void
    372 vr_reboot(int howto, char *bootstr)
    373 {
    374 	/*
    375 	 * power down
    376 	 */
    377 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    378 		printf("fake powerdown\n");
    379 		__asm(__CONCAT(".word	",___STRING(VR_OPCODE_HIBERNATE)));
    380 		__asm("nop");
    381 		__asm("nop");
    382 		__asm("nop");
    383 		__asm("nop");
    384 		__asm("nop");
    385 		__asm(".set reorder");
    386 		/* not reach */
    387 		vr_reboot(howto&~RB_HALT, bootstr);
    388 	}
    389 	/*
    390 	 * halt
    391 	 */
    392 	if (howto & RB_HALT) {
    393 #if NVRIP > 0
    394 		_spllower(~MIPS_INT_MASK_0);
    395 		vrip_intr_suspend();
    396 #else
    397 		splhigh();
    398 #endif
    399 		__asm(".set noreorder");
    400 		__asm(__CONCAT(".word	",___STRING(VR_OPCODE_SUSPEND)));
    401 		__asm("nop");
    402 		__asm("nop");
    403 		__asm("nop");
    404 		__asm("nop");
    405 		__asm("nop");
    406 		__asm(".set reorder");
    407 #if NVRIP > 0
    408 		vrip_intr_resume();
    409 #endif
    410 	}
    411 	/*
    412 	 * reset
    413 	 */
    414 #if NVRDSU
    415 	vrdsu_reset();
    416 #else
    417 	printf("%s(%d): There is no DSU.", __FILE__, __LINE__);
    418 #endif
    419 }
    420 
    421 /*
    422  * Handle interrupts.
    423  */
    424 void
    425 VR_INTR(u_int32_t status, u_int32_t cause, u_int32_t pc, u_int32_t ipending)
    426 {
    427 	uvmexp.intrs++;
    428 
    429 	if (ipending & MIPS_INT_MASK_5) {
    430 		/*
    431 		 * spl* uses MIPS_INT_MASK not MIPS3_INT_MASK. it causes
    432 		 * INT5 interrupt.
    433 		 */
    434 		mips3_cp0_compare_write(mips3_cp0_count_read());
    435 	}
    436 
    437 	/* for spllowersoftclock */
    438 	_splset(((status & ~cause) & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
    439 
    440 	if (ipending & MIPS_INT_MASK_1) {
    441 		(*vr_intr_handler[1])(vr_intr_arg[1], pc, status);
    442 
    443 		cause &= ~MIPS_INT_MASK_1;
    444 		_splset(((status & ~cause) & MIPS_HARD_INT_MASK)
    445 		    | MIPS_SR_INT_IE);
    446 	}
    447 
    448 	if (ipending & MIPS_INT_MASK_0) {
    449 		(*vr_intr_handler[0])(vr_intr_arg[0], pc, status);
    450 
    451 		cause &= ~MIPS_INT_MASK_0;
    452 	}
    453 	_splset(((status & ~cause) & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
    454 
    455 	softintr(ipending);
    456 }
    457 
    458 void *
    459 vr_intr_establish(int line, int (*ih_fun)(void *, u_int32_t, u_int32_t),
    460     void *ih_arg)
    461 {
    462 
    463 	KDASSERT(vr_intr_handler[line] == vr_null_handler);
    464 
    465 	vr_intr_handler[line] = ih_fun;
    466 	vr_intr_arg[line] = ih_arg;
    467 
    468 	return ((void *)line);
    469 }
    470 
    471 void
    472 vr_intr_disestablish(void *ih)
    473 {
    474 	int line = (int)ih;
    475 
    476 	vr_intr_handler[line] = vr_null_handler;
    477 	vr_intr_arg[line] = NULL;
    478 }
    479 
    480 int
    481 vr_null_handler(void *arg, u_int32_t pc, u_int32_t status)
    482 {
    483 
    484 	printf("vr_null_handler\n");
    485 
    486 	return (0);
    487 }
    488 
    489 /*
    490 int x4181 = VR4181;
    491 int x4101 = VR4101;
    492 int x4102 = VR4102;
    493 int x4111 = VR4111;
    494 int x4121 = VR4121;
    495 int x4122 = VR4122;
    496 int xo4181 = ONLY_VR4181;
    497 int xo4101 = ONLY_VR4101;
    498 int xo4102 = ONLY_VR4102;
    499 int xo4111_4121 = ONLY_VR4111_4121;
    500 int g4101=VRGROUP_4101;
    501 int g4102=VRGROUP_4102;
    502 int g4181=VRGROUP_4181;
    503 int g4102_4121=VRGROUP_4102_4121;
    504 int g4111_4121=VRGROUP_4111_4121;
    505 int g4102_4122=VRGROUP_4102_4122;
    506 int g4111_4122=VRGROUP_4111_4122;
    507 int single_vrip_base=SINGLE_VRIP_BASE;
    508 int vrip_base_addr=VRIP_BASE_ADDR;
    509 */
    510