Home | History | Annotate | Line # | Download | only in vr
vr.c revision 1.20
      1 /*	$NetBSD: vr.c,v 1.20 2001/02/22 18:38:04 uch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999
      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 #include <sys/param.h>
     37 #include <sys/types.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/reboot.h>
     41 #include <sys/kcore.h>
     42 
     43 #include <machine/cpu.h>
     44 #include <machine/intr.h>
     45 #include <machine/reg.h>
     46 #include <machine/psl.h>
     47 #include <machine/locore.h>
     48 #include <machine/sysconf.h>
     49 #include <machine/bus.h>
     50 #include <machine/autoconf.h>
     51 
     52 #include <mips/mips_param.h>		/* hokey spl()s */
     53 #include <mips/mips/mips_mcclock.h>	/* mcclock CPUspeed estimation */
     54 
     55 #include <hpcmips/vr/vr.h>
     56 #include <hpcmips/vr/vr_asm.h>
     57 #include <hpcmips/vr/vripreg.h>
     58 #include <hpcmips/vr/rtcreg.h>
     59 #include <hpcmips/hpcmips/machdep.h>	/* cpu_name */
     60 #include <machine/bootinfo.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 #if NCOM > 0
     79 #include <sys/termios.h>
     80 #include <sys/ttydefaults.h>
     81 #include <dev/ic/comreg.h>
     82 #include <dev/ic/comvar.h>
     83 #include <hpcmips/vr/siureg.h>
     84 #include <hpcmips/vr/com_vripvar.h>
     85 #ifndef CONSPEED
     86 #define CONSPEED TTYDEF_SPEED
     87 #endif
     88 #endif
     89 
     90 #include "hpcfb.h"
     91 #include "vrkiu.h"
     92 #if NVRKIU > 0
     93 #include <dev/wscons/wsdisplayvar.h>
     94 #include <dev/rasops/rasops.h>
     95 #endif
     96 
     97 #if NHPCFB > 0
     98 #include <dev/hpc/hpcfbvar.h>
     99 #endif
    100 
    101 #if NVRKIU > 0
    102 #include <arch/hpcmips/vr/vrkiuvar.h>
    103 #endif
    104 
    105 void	vr_init __P((void));
    106 void	vr_os_init __P((void));
    107 void	vr_bus_reset __P((void));
    108 int	vr_intr __P((u_int32_t, u_int32_t, u_int32_t, u_int32_t));
    109 void	vr_cons_init __P((void));
    110 void	vr_device_register __P((struct device *, void *));
    111 void    vr_fb_init __P((caddr_t*));
    112 void    vr_mem_init __P((paddr_t));
    113 void	vr_find_dram __P((paddr_t, paddr_t));
    114 void	vr_reboot __P((int howto, char *bootstr));
    115 
    116 extern unsigned nullclkread __P((void));
    117 extern unsigned (*clkread) __P((void));
    118 
    119 /*
    120  * CPU interrupt dispatch table (HwInt[0:3])
    121  */
    122 int null_handler __P((void*, u_int32_t, u_int32_t));
    123 static int (*intr_handler[4]) __P((void*, u_int32_t, u_int32_t)) =
    124 {
    125 	null_handler,
    126 	null_handler,
    127 	null_handler,
    128 	null_handler
    129 };
    130 static void *intr_arg[4];
    131 
    132 extern phys_ram_seg_t mem_clusters[];
    133 extern int mem_cluster_cnt;
    134 
    135 void
    136 vr_init()
    137 {
    138 	/*
    139 	 * Platform Information.
    140 	 */
    141 
    142 	/*
    143 	 * Platform Specific Function Hooks
    144 	 */
    145 	platform.os_init = vr_os_init;
    146 	platform.iointr = vr_intr;
    147 	platform.bus_reset = vr_bus_reset;
    148 	platform.cons_init = vr_cons_init;
    149 	platform.device_register = vr_device_register;
    150 	platform.fb_init = vr_fb_init;
    151 	platform.mem_init = vr_mem_init;
    152 	platform.reboot = vr_reboot;
    153 
    154 #if NVRBCU > 0
    155 	sprintf(cpu_name, "NEC %s rev%d.%d %d.%03dMHz",
    156 		vrbcu_vrip_getcpuname(),
    157 		vrbcu_vrip_getcpumajor(),
    158 		vrbcu_vrip_getcpuminor(),
    159 		vrbcu_vrip_getcpuclock() / 1000000,
    160 		(vrbcu_vrip_getcpuclock() % 1000000) / 1000);
    161 #else
    162 	sprintf(cpu_name, "NEC VR41xx");
    163 #endif
    164 }
    165 
    166 void
    167 vr_mem_init(kernend)
    168 	paddr_t kernend;
    169 {
    170 	mem_clusters[0].start = 0;
    171 	mem_clusters[0].size = kernend;
    172 	mem_cluster_cnt = 1;
    173 	vr_find_dram(kernend, 0x02000000);
    174 	vr_find_dram(0x02000000, 0x04000000);
    175 	vr_find_dram(0x04000000, 0x06000000);
    176 	vr_find_dram(0x06000000, 0x08000000);
    177 
    178 	/* Clear currently unused D-RAM area (For reboot Windows CE clearly)*/
    179 	memset((void *)(KERNBASE + 0x400), 0, KERNTEXTOFF - (KERNBASE + 0x800));
    180 }
    181 
    182 void
    183 vr_find_dram(addr, end)
    184 	paddr_t addr, end;
    185 {
    186 	int n;
    187 	caddr_t page;
    188 #ifdef NARLY_MEMORY_PROBE
    189 	int x, i;
    190 #endif
    191 
    192 	n = mem_cluster_cnt;
    193 	for (; addr < end; addr += NBPG) {
    194 
    195 		page = (void *)MIPS_PHYS_TO_KSEG1(addr);
    196 		if (badaddr(page, 4))
    197 			goto bad;
    198 
    199 		/* stop memory probing at first memory image */
    200 		if (bcmp(page, (void *)MIPS_PHYS_TO_KSEG0(0), 128) == 0)
    201 			return;
    202 
    203 		*(volatile int *)(page+0) = 0xa5a5a5a5;
    204 		*(volatile int *)(page+4) = 0x5a5a5a5a;
    205 		wbflush();
    206 		if (*(volatile int *)(page+0) != 0xa5a5a5a5)
    207 			goto bad;
    208 
    209 		*(volatile int *)(page+0) = 0x5a5a5a5a;
    210 		*(volatile int *)(page+4) = 0xa5a5a5a5;
    211 		wbflush();
    212 		if (*(volatile int *)(page+0) != 0x5a5a5a5a)
    213 			goto bad;
    214 
    215 #ifdef NARLY_MEMORY_PROBE
    216 		x = random();
    217 		for (i = 0; i < NBPG; i += 4)
    218 			*(volatile int *)(page+i) = (x ^ i);
    219 		wbflush();
    220 		for (i = 0; i < NBPG; i += 4)
    221 			if (*(volatile int *)(page+i) != (x ^ i))
    222 				goto bad;
    223 
    224 		x = random();
    225 		for (i = 0; i < NBPG; i += 4)
    226 			*(volatile int *)(page+i) = (x ^ i);
    227 		wbflush();
    228 		for (i = 0; i < NBPG; i += 4)
    229 			if (*(volatile int *)(page+i) != (x ^ i))
    230 				goto bad;
    231 #endif
    232 
    233 		if (!mem_clusters[n].size)
    234 			mem_clusters[n].start = addr;
    235 		mem_clusters[n].size += NBPG;
    236 		continue;
    237 
    238 	bad:
    239 		if (mem_clusters[n].size)
    240 			++n;
    241 		continue;
    242 	}
    243 	if (mem_clusters[n].size)
    244 		++n;
    245 	mem_cluster_cnt = n;
    246 }
    247 
    248 void
    249 vr_fb_init(kernend)
    250 	caddr_t *kernend;
    251 {
    252 	/* Nothing to do */
    253 }
    254 
    255 void
    256 vr_os_init()
    257 {
    258 	/*
    259 	 * Set up interrupt handling and I/O addresses.
    260 	 */
    261 
    262 	splvec.splbio = MIPS_SPL0;
    263 	splvec.splnet = MIPS_SPL0;
    264 	splvec.spltty = MIPS_SPL0;
    265 	splvec.splimp = MIPS_SPL0;
    266 	splvec.splclock = MIPS_SPL_0_1;
    267 	splvec.splstatclock = MIPS_SPL_0_1;
    268 
    269 	/* no high resolution timer circuit; possibly never called */
    270 	clkread = nullclkread;
    271 
    272 #ifdef NOT_YET
    273 	mcclock_addr = (volatile struct chiptime *)
    274 		MIPS_PHYS_TO_KSEG1(Vr_SYS_CLOCK);
    275 	mc_cpuspeed(mcclock_addr, MIPS_INT_MASK_1);
    276 #else
    277 	printf("%s(%d): cpuspeed estimation is notimplemented\n",
    278 	       __FILE__, __LINE__);
    279 #endif
    280 #ifdef HPCMIPS_L1CACHE_DISABLE
    281 	cpuspeed = 1;	/* XXX, CPU is very very slow because L1 cache is */
    282 	/* disabled. */
    283 #endif /*  HPCMIPS_L1CAHCE_DISABLE */
    284 }
    285 
    286 
    287 /*
    288  * Initalize the memory system and I/O buses.
    289  */
    290 void
    291 vr_bus_reset()
    292 {
    293 	printf("%s(%d): vr_bus_reset() not implemented.\n",
    294 	       __FILE__, __LINE__);
    295 }
    296 
    297 void
    298 vr_cons_init()
    299 {
    300 #if NCOM > 0 || NHPCFB > 0 || NVRKIU > 0
    301 	extern bus_space_tag_t system_bus_iot;
    302 	extern bus_space_tag_t mb_bus_space_init __P((void));
    303 
    304 	/*
    305 	 * At this time, system_bus_iot is not initialized yet.
    306 	 * Just initialize it here.
    307 	 */
    308 	mb_bus_space_init();
    309 #endif
    310 
    311 #if NCOM > 0
    312 #ifdef KGDB
    313 	/* if KGDB is defined, always use the serial port for KGDB */
    314 	/* Serial console */
    315 	if(com_vrip_cndb_attach(
    316 		system_bus_iot, 0x0c000000, 9600, VRCOM_FREQ,
    317 		(TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1))
    318 	{
    319 		printf("%s(%d): can't init kgdb's serial port",
    320 		       __FILE__, __LINE__);
    321 	}
    322 #else
    323 	if (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) {
    324 		/* Serial console */
    325 		if(com_vrip_cndb_attach(
    326 			system_bus_iot, 0x0c000000, CONSPEED, VRCOM_FREQ,
    327 			(TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0))
    328 		{
    329 			printf("%s(%d): can't init serial console", __FILE__, __LINE__);
    330 		} else {
    331 			return;
    332 		}
    333 	}
    334 #endif
    335 #endif
    336 
    337 #if NHPCFB > 0
    338 	if (hpcfb_cnattach(NULL)) {
    339 		printf("%s(%d): can't init fb console", __FILE__, __LINE__);
    340 	} else {
    341 		goto find_keyboard;
    342 	}
    343 #endif
    344 
    345  find_keyboard:
    346 #if NVRKIU > 0
    347 	if (vrkiu_cnattach(system_bus_iot, VRIP_KIU_ADDR)) {
    348 		printf("%s(%d): can't init vrkiu as console",
    349 		       __FILE__, __LINE__);
    350 	} else {
    351 		return;
    352 	}
    353 #endif
    354 }
    355 
    356 void
    357 vr_device_register(dev, aux)
    358 	struct device *dev;
    359 	void *aux;
    360 {
    361 	printf("%s(%d): vr_device_register() not implemented.\n",
    362 	       __FILE__, __LINE__);
    363 	panic("abort");
    364 }
    365 
    366 void
    367 vr_reboot(howto, bootstr)
    368 	int howto;
    369 	char *bootstr;
    370 {
    371 	/*
    372 	 * power down
    373 	 */
    374 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    375 		printf("fake powerdown\n");
    376 		__asm(__CONCAT(".word	",___STRING(VR_OPCODE_HIBERNATE)));
    377 		__asm("nop");
    378 		__asm("nop");
    379 		__asm("nop");
    380 		__asm("nop");
    381 		__asm("nop");
    382 		__asm(".set reorder");
    383 		/* not reach */
    384 		vr_reboot(howto&~RB_HALT, bootstr);
    385 	}
    386 	/*
    387 	 * halt
    388 	 */
    389 	if (howto & RB_HALT) {
    390 #if NVRIP > 0
    391 		_spllower(~MIPS_INT_MASK_0);
    392 		vrip_intr_suspend();
    393 #else
    394 		splhigh();
    395 #endif
    396 		__asm(".set noreorder");
    397 		__asm(__CONCAT(".word	",___STRING(VR_OPCODE_SUSPEND)));
    398 		__asm("nop");
    399 		__asm("nop");
    400 		__asm("nop");
    401 		__asm("nop");
    402 		__asm("nop");
    403 		__asm(".set reorder");
    404 #if NVRIP > 0
    405 		vrip_intr_resume();
    406 #endif
    407 	}
    408 	/*
    409 	 * reset
    410 	 */
    411 #if NVRDSU
    412 	vrdsu_reset();
    413 #else
    414 	printf("%s(%d): There is no DSU.", __FILE__, __LINE__);
    415 #endif
    416 }
    417 
    418 void *
    419 vr_intr_establish(line, ih_fun, ih_arg)
    420 	int line;
    421 	int (*ih_fun) __P((void*, u_int32_t, u_int32_t));
    422 	void *ih_arg;
    423 {
    424 	if (intr_handler[line] != null_handler) {
    425 		panic("vr_intr_establish: can't establish duplicated intr handler.");
    426 	}
    427 	intr_handler[line] = ih_fun;
    428 	intr_arg[line] = ih_arg;
    429 
    430 	return (void*)line;
    431 }
    432 
    433 
    434 void
    435 vr_intr_disestablish(ih)
    436 	void *ih;
    437 {
    438 	int line = (int)ih;
    439 	intr_handler[line] = null_handler;
    440 	intr_arg[line] = NULL;
    441 }
    442 
    443 int
    444 null_handler(arg, pc, statusReg)
    445 	void *arg;
    446 	u_int32_t pc;
    447 	u_int32_t statusReg;
    448 {
    449 	printf("null_handler\n");
    450 	return 0;
    451 }
    452 
    453 /*
    454  * Handle interrupts.
    455  */
    456 int
    457 vr_intr(status, cause, pc, ipending)
    458 	u_int32_t status, cause, pc, ipending;
    459 {
    460 	int hwintr;
    461 
    462 	hwintr = (ffs(ipending >> 10) -1) & 0x3;
    463 	(*intr_handler[hwintr])(intr_arg[hwintr], pc, status);
    464 
    465 	return (MIPS_SR_INT_IE | (status & ~cause & MIPS_HARD_INT_MASK));
    466 }
    467