vr.c revision 1.34 1 /* $NetBSD: vr.c,v 1.34 2002/01/02 13:08:35 uch 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
221 void
222 vr_find_dram(paddr_t addr, paddr_t end)
223 {
224 int n;
225 caddr_t page;
226 #ifdef NARLY_MEMORY_PROBE
227 int x, i;
228 #endif
229
230 #ifdef VR_FIND_DRAMLIM
231 if (VR_FIND_DRAMLIM < end)
232 end = VR_FIND_DRAMLIM;
233 #endif /* VR_FIND_DRAMLIM */
234 n = mem_cluster_cnt;
235 for (; addr < end; addr += NBPG) {
236
237 page = (void *)MIPS_PHYS_TO_KSEG1(addr);
238 if (badaddr(page, 4))
239 goto bad;
240
241 /* stop memory probing at first memory image */
242 if (bcmp(page, (void *)MIPS_PHYS_TO_KSEG0(0), 128) == 0)
243 return;
244
245 *(volatile int *)(page+0) = 0xa5a5a5a5;
246 *(volatile int *)(page+4) = 0x5a5a5a5a;
247 wbflush();
248 if (*(volatile int *)(page+0) != 0xa5a5a5a5)
249 goto bad;
250
251 *(volatile int *)(page+0) = 0x5a5a5a5a;
252 *(volatile int *)(page+4) = 0xa5a5a5a5;
253 wbflush();
254 if (*(volatile int *)(page+0) != 0x5a5a5a5a)
255 goto bad;
256
257 #ifdef NARLY_MEMORY_PROBE
258 x = random();
259 for (i = 0; i < NBPG; i += 4)
260 *(volatile int *)(page+i) = (x ^ i);
261 wbflush();
262 for (i = 0; i < NBPG; i += 4)
263 if (*(volatile int *)(page+i) != (x ^ i))
264 goto bad;
265
266 x = random();
267 for (i = 0; i < NBPG; i += 4)
268 *(volatile int *)(page+i) = (x ^ i);
269 wbflush();
270 for (i = 0; i < NBPG; i += 4)
271 if (*(volatile int *)(page+i) != (x ^ i))
272 goto bad;
273 #endif /* NARLY_MEMORY_PROBE */
274
275 if (!mem_clusters[n].size)
276 mem_clusters[n].start = addr;
277 mem_clusters[n].size += NBPG;
278 continue;
279
280 bad:
281 if (mem_clusters[n].size)
282 ++n;
283 continue;
284 }
285 if (mem_clusters[n].size)
286 ++n;
287 mem_cluster_cnt = n;
288 }
289
290 void
291 vr_fb_init(caddr_t *kernend)
292 {
293 /* Nothing to do */
294 }
295
296 void
297 vr_cons_init()
298 {
299 #if NCOM > 0 || NSG2COM_VRIP > 0 || NHPCFB > 0 || NVRKIU > 0
300 bus_space_tag_t iot = hpcmips_system_bus_space();
301 #endif
302
303 #if NCOM > 0
304 #ifdef KGDB
305 /* if KGDB is defined, always use the serial port for KGDB */
306 if (com_vrip_cndb_attach(iot, VRIP_SIU_ADDR, 9600, VRCOM_FREQ,
307 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
308 printf("%s(%d): can't init kgdb's serial port",
309 __FILE__, __LINE__);
310 }
311 #else /* KGDB */
312 if (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) {
313 /* Serial console */
314 if (com_vrip_cndb_attach(iot, VRIP_SIU_ADDR, CONSPEED,
315 VRCOM_FREQ, (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)) {
316 printf("%s(%d): can't init serial console",
317 __FILE__, __LINE__);
318 } else {
319 #if NSG2COM_VRIP == 0
320 return;
321 #endif
322 }
323 }
324 #endif /* KGDB */
325 #endif /* NCOM > 0 */
326
327 #if NSG2COM_VRIP > 0
328 #ifdef KGDB
329 /* if KGDB is defined, always use the serial port for KGDB */
330 if (sg2com_vrip_cndb_attach(iot, SG2COM_VRIP_ADDR, 9600, SG2COM_FREQ,
331 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
332 printf("%s(%d): can't init kgdb's serial port",
333 __FILE__, __LINE__);
334 }
335 #else /* KGDB */
336 if (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) {
337 /* Serial console */
338 if (sg2com_vrip_cndb_attach(iot, SG2COM_ADDR, CONSPEED,
339 SG2COM_FREQ, (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)){
340 printf("%s(%d): can't init serial console",
341 __FILE__, __LINE__);
342 } else {
343 return;
344 }
345 }
346 #endif /* KGDB */
347 #endif /* NSG2COM_VRIP > 0 */
348
349 #if NHPCFB > 0
350 if (hpcfb_cnattach(NULL)) {
351 printf("%s(%d): can't init fb console", __FILE__, __LINE__);
352 } else {
353 goto find_keyboard;
354 }
355 find_keyboard:
356 #endif /* NHPCFB > 0 */
357
358 #if NVRKIU > 0 && VRIP_KIU_ADDR != VRIP_NO_ADDR
359 if (vrkiu_cnattach(iot, VRIP_KIU_ADDR)) {
360 printf("%s(%d): can't init vrkiu as console",
361 __FILE__, __LINE__);
362 } else {
363 return;
364 }
365 #endif /* NVRKIU > 0 && VRIP_KIU_ADDR != VRIP_NO_ADDR */
366 }
367
368 void
369 vr_reboot(int howto, 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 /*
419 * Handle interrupts.
420 */
421 void
422 VR_INTR(u_int32_t status, u_int32_t cause, u_int32_t pc, u_int32_t ipending)
423 {
424 uvmexp.intrs++;
425
426 if (ipending & MIPS_INT_MASK_5) {
427 /*
428 * spl* uses MIPS_INT_MASK not MIPS3_INT_MASK. it causes
429 * INT5 interrupt.
430 */
431 mips3_cp0_compare_write(mips3_cp0_count_read());
432 }
433
434 /* for spllowersoftclock */
435 _splset(((status & ~cause) & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
436
437 if (ipending & MIPS_INT_MASK_1) {
438 (*vr_intr_handler[1])(vr_intr_arg[1], pc, status);
439
440 cause &= ~MIPS_INT_MASK_1;
441 _splset(((status & ~cause) & MIPS_HARD_INT_MASK)
442 | MIPS_SR_INT_IE);
443 }
444
445 if (ipending & MIPS_INT_MASK_0) {
446 (*vr_intr_handler[0])(vr_intr_arg[0], pc, status);
447
448 cause &= ~MIPS_INT_MASK_0;
449 }
450 _splset(((status & ~cause) & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
451
452 softintr(ipending);
453 }
454
455 void *
456 vr_intr_establish(int line, int (*ih_fun)(void *, u_int32_t, u_int32_t),
457 void *ih_arg)
458 {
459
460 KDASSERT(vr_intr_handler[line] == vr_null_handler);
461
462 vr_intr_handler[line] = ih_fun;
463 vr_intr_arg[line] = ih_arg;
464
465 return ((void *)line);
466 }
467
468 void
469 vr_intr_disestablish(void *ih)
470 {
471 int line = (int)ih;
472
473 vr_intr_handler[line] = vr_null_handler;
474 vr_intr_arg[line] = NULL;
475 }
476
477 int
478 vr_null_handler(void *arg, u_int32_t pc, u_int32_t status)
479 {
480
481 printf("vr_null_handler\n");
482
483 return (0);
484 }
485
486 /*
487 int x4181 = VR4181;
488 int x4101 = VR4101;
489 int x4102 = VR4102;
490 int x4111 = VR4111;
491 int x4121 = VR4121;
492 int x4122 = VR4122;
493 int xo4181 = ONLY_VR4181;
494 int xo4101 = ONLY_VR4101;
495 int xo4102 = ONLY_VR4102;
496 int xo4111_4121 = ONLY_VR4111_4121;
497 int g4101=VRGROUP_4101;
498 int g4102=VRGROUP_4102;
499 int g4181=VRGROUP_4181;
500 int g4102_4121=VRGROUP_4102_4121;
501 int g4111_4121=VRGROUP_4111_4121;
502 int g4102_4122=VRGROUP_4102_4122;
503 int g4111_4122=VRGROUP_4111_4122;
504 int single_vrip_base=SINGLE_VRIP_BASE;
505 int vrip_base_addr=VRIP_BASE_ADDR;
506 */
507