vr.c revision 1.39 1 /* $NetBSD: vr.c,v 1.39 2002/02/11 11:44:36 takemura Exp $ */
2
3 /*-
4 * Copyright (c) 1999-2002
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 #include <machine/platid.h>
52 #include <machine/platid_mask.h>
53
54 #include <dev/hpc/hpckbdvar.h>
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_common.h"
63 #if NVRIP_COMMON > 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 "com_vrip.h"
79 #include "com_hpcio.h"
80 #if NCOM > 0
81 #include <sys/termios.h>
82 #include <sys/ttydefaults.h>
83 #include <dev/ic/comreg.h>
84 #include <dev/ic/comvar.h>
85 #if NCOM_VRIP > 0
86 #include <hpcmips/vr/siureg.h>
87 #include <hpcmips/vr/com_vripvar.h>
88 #endif
89 #if NCOM_HPCIO > 0
90 #include <hpcmips/dev/com_hpciovar.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 #if NCOM > 0
183 /*
184 * machine dependent serial console info
185 */
186 static struct vr_com_platdep {
187 platid_mask_t *platidmask;
188 int (*attach)(bus_space_tag_t, int, int, int, tcflag_t, int);
189 int addr;
190 int freq;
191 } platdep_com_table[] = {
192 #if NCOM_HPCIO > 0
193 {
194 &platid_mask_MACH_NEC_MCR_SIGMARION2,
195 com_hpcio_cndb_attach, /* attach proc */
196 0x0b600000, /* base address */
197 COM_FREQ, /* frequency */
198 },
199 #endif
200 #if NCOM_VRIP > 0
201 #ifdef VR4102
202 {
203 &platid_mask_CPU_MIPS_VR_4102,
204 com_vrip_cndb_attach, /* attach proc */
205 VR4102_SIU_ADDR, /* base address */
206 VRCOM_FREQ, /* frequency */
207 },
208 #endif /* VR4102 */
209 #ifdef VR4111
210 {
211 &platid_mask_CPU_MIPS_VR_4111,
212 com_vrip_cndb_attach, /* attach proc */
213 VR4102_SIU_ADDR, /* base address */
214 VRCOM_FREQ, /* frequency */
215 },
216 #endif /* VR4111 */
217 #ifdef VR4121
218 {
219 &platid_mask_CPU_MIPS_VR_4121,
220 com_vrip_cndb_attach, /* attach proc */
221 VR4102_SIU_ADDR, /* base address */
222 VRCOM_FREQ, /* frequency */
223 },
224 #endif /* VR4121 */
225 #ifdef VR4122
226 {
227 &platid_mask_CPU_MIPS_VR_4122,
228 com_vrip_cndb_attach, /* attach proc */
229 VR4122_SIU_ADDR, /* base address */
230 VRCOM_FREQ, /* frequency */
231 },
232 #endif /* VR4122 */
233 #ifdef VR4131
234 {
235 &platid_mask_CPU_MIPS_VR_4122,
236 com_vrip_cndb_attach, /* attach proc */
237 VR4122_SIU_ADDR, /* base address */
238 VRCOM_FREQ, /* frequency */
239 },
240 #endif /* VR4131 */
241 #ifdef SINGLE_VRIP_BASE
242 {
243 &platid_wild,
244 com_vrip_cndb_attach, /* attach proc */
245 VRIP_SIU_ADDR, /* base address */
246 VRCOM_FREQ, /* frequency */
247 },
248 #endif /* SINGLE_VRIP_BASE */
249 #else /* NCOM_VRIP > 0 */
250 /* dummy */
251 {
252 &platid_wild,
253 NULL, /* attach proc */
254 0, /* base address */
255 0, /* frequency */
256 },
257 #endif /* NCOM_VRIP > 0 */
258 };
259 #endif /* NCOM > 0 */
260
261 #if NVRKIU > 0
262 /*
263 * machine dependent keyboard info
264 */
265 static struct vr_kiu_platdep {
266 platid_mask_t *platidmask;
267 int addr;
268 } platdep_kiu_table[] = {
269 #ifdef VR4102
270 {
271 &platid_mask_CPU_MIPS_VR_4102,
272 VR4102_KIU_ADDR, /* base address */
273 },
274 #endif /* VR4102 */
275 #ifdef VR4111
276 {
277 &platid_mask_CPU_MIPS_VR_4111,
278 VR4102_KIU_ADDR, /* base address */
279 },
280 #endif /* VR4111 */
281 #ifdef VR4121
282 {
283 &platid_mask_CPU_MIPS_VR_4121,
284 VR4102_KIU_ADDR, /* base address */
285 },
286 #endif /* VR4121 */
287 {
288 &platid_wild,
289 #ifdef SINGLE_VRIP_BASE
290 VRIP_KIU_ADDR, /* base address */
291 #else
292 VRIP_NO_ADDR, /* base address */
293 #endif /* SINGLE_VRIP_BASE */
294 },
295 };
296 #endif /* NVRKIU > 0 */
297
298 void
299 vr_init()
300 {
301 /*
302 * Platform Specific Function Hooks
303 */
304 platform.cpu_idle = vr_idle;
305 platform.cpu_intr = VR_INTR;
306 platform.cons_init = vr_cons_init;
307 platform.fb_init = vr_fb_init;
308 platform.mem_init = vr_mem_init;
309 platform.reboot = vr_reboot;
310
311 #if NVRBCU > 0
312 sprintf(cpu_name, "NEC %s rev%d.%d %d.%03dMHz",
313 vrbcu_vrip_getcpuname(),
314 vrbcu_vrip_getcpumajor(),
315 vrbcu_vrip_getcpuminor(),
316 vrbcu_vrip_getcpuclock() / 1000000,
317 (vrbcu_vrip_getcpuclock() % 1000000) / 1000);
318 #else
319 sprintf(cpu_name, "NEC VR41xx");
320 #endif
321 }
322
323 void
324 vr_mem_init(paddr_t kernend)
325 {
326
327 mem_clusters[0].start = 0;
328 mem_clusters[0].size = kernend;
329 mem_cluster_cnt = 1;
330
331 vr_find_dram(kernend, 0x02000000);
332 vr_find_dram(0x02000000, 0x04000000);
333 vr_find_dram(0x04000000, 0x06000000);
334 vr_find_dram(0x06000000, 0x08000000);
335 }
336
337 void
338 vr_find_dram(paddr_t addr, paddr_t end)
339 {
340 int n;
341 caddr_t page;
342 #ifdef NARLY_MEMORY_PROBE
343 int x, i;
344 #endif
345
346 #ifdef VR_FIND_DRAMLIM
347 if (VR_FIND_DRAMLIM < end)
348 end = VR_FIND_DRAMLIM;
349 #endif /* VR_FIND_DRAMLIM */
350 n = mem_cluster_cnt;
351 for (; addr < end; addr += NBPG) {
352
353 page = (void *)MIPS_PHYS_TO_KSEG1(addr);
354 if (badaddr(page, 4))
355 goto bad;
356
357 /* stop memory probing at first memory image */
358 if (bcmp(page, (void *)MIPS_PHYS_TO_KSEG0(0), 128) == 0)
359 return;
360
361 *(volatile int *)(page+0) = 0xa5a5a5a5;
362 *(volatile int *)(page+4) = 0x5a5a5a5a;
363 wbflush();
364 if (*(volatile int *)(page+0) != 0xa5a5a5a5)
365 goto bad;
366
367 *(volatile int *)(page+0) = 0x5a5a5a5a;
368 *(volatile int *)(page+4) = 0xa5a5a5a5;
369 wbflush();
370 if (*(volatile int *)(page+0) != 0x5a5a5a5a)
371 goto bad;
372
373 #ifdef NARLY_MEMORY_PROBE
374 x = random();
375 for (i = 0; i < NBPG; i += 4)
376 *(volatile int *)(page+i) = (x ^ i);
377 wbflush();
378 for (i = 0; i < NBPG; i += 4)
379 if (*(volatile int *)(page+i) != (x ^ i))
380 goto bad;
381
382 x = random();
383 for (i = 0; i < NBPG; i += 4)
384 *(volatile int *)(page+i) = (x ^ i);
385 wbflush();
386 for (i = 0; i < NBPG; i += 4)
387 if (*(volatile int *)(page+i) != (x ^ i))
388 goto bad;
389 #endif /* NARLY_MEMORY_PROBE */
390
391 if (!mem_clusters[n].size)
392 mem_clusters[n].start = addr;
393 mem_clusters[n].size += NBPG;
394 continue;
395
396 bad:
397 if (mem_clusters[n].size)
398 ++n;
399 continue;
400 }
401 if (mem_clusters[n].size)
402 ++n;
403 mem_cluster_cnt = n;
404 }
405
406 void
407 vr_fb_init(caddr_t *kernend)
408 {
409 /* Nothing to do */
410 }
411
412 void
413 vr_cons_init()
414 {
415 #if NCOM > 0 || NHPCFB > 0 || NVRKIU > 0
416 bus_space_tag_t iot = hpcmips_system_bus_space();
417 #endif
418 #if NCOM > 0
419 static struct vr_com_platdep *com_info;
420 #endif
421 #if NVRKIU > 0
422 static struct vr_kiu_platdep *kiu_info;
423 #endif
424
425 #if NCOM > 0
426 com_info = platid_search(&platid, platdep_com_table,
427 sizeof(platdep_com_table)/sizeof(*platdep_com_table),
428 sizeof(*platdep_com_table));
429 #ifdef KGDB
430 if (com_info->attach != NULL) {
431 /* if KGDB is defined, always use the serial port for KGDB */
432 if ((*com_info->attach)(iot, com_info->addr, 9600,
433 com_info->freq,
434 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
435 printf("%s(%d): can't init kgdb's serial port",
436 __FILE__, __LINE__);
437 }
438 #else /* KGDB */
439 if (com_info->attach != NULL && (bootinfo->bi_cnuse&BI_CNUSE_SERIAL)) {
440 /* Serial console */
441 if ((*com_info->attach)(iot, com_info->addr, CONSPEED,
442 com_info->freq,
443 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)) {
444 printf("%s(%d): can't init serial console",
445 __FILE__, __LINE__);
446 } else {
447 return;
448 }
449 }
450 #endif /* KGDB */
451 #endif /* NCOM > 0 */
452
453 #if NHPCFB > 0
454 if (hpcfb_cnattach(NULL)) {
455 printf("%s(%d): can't init fb console", __FILE__, __LINE__);
456 } else {
457 goto find_keyboard;
458 }
459 find_keyboard:
460 #endif /* NHPCFB > 0 */
461
462 #if NVRKIU > 0
463 kiu_info = platid_search(&platid, platdep_kiu_table,
464 sizeof(platdep_kiu_table)/sizeof(*platdep_kiu_table),
465 sizeof(*platdep_kiu_table));
466 if (kiu_info->addr != VRIP_NO_ADDR) {
467 if (vrkiu_cnattach(iot, kiu_info->addr)) {
468 printf("%s(%d): can't init vrkiu as console",
469 __FILE__, __LINE__);
470 } else {
471 return;
472 }
473 }
474 #endif /* NVRKIU > 0 */
475 }
476
477 void
478 vr_reboot(int howto, char *bootstr)
479 {
480 /*
481 * power down
482 */
483 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
484 printf("fake powerdown\n");
485 __asm(__CONCAT(".word ",___STRING(VR_OPCODE_HIBERNATE)));
486 __asm("nop");
487 __asm("nop");
488 __asm("nop");
489 __asm("nop");
490 __asm("nop");
491 __asm(".set reorder");
492 /* not reach */
493 vr_reboot(howto&~RB_HALT, bootstr);
494 }
495 /*
496 * halt
497 */
498 if (howto & RB_HALT) {
499 #if NVRIP_COMMON > 0
500 _spllower(~MIPS_INT_MASK_0);
501 vrip_intr_suspend();
502 #else
503 splhigh();
504 #endif
505 __asm(".set noreorder");
506 __asm(__CONCAT(".word ",___STRING(VR_OPCODE_SUSPEND)));
507 __asm("nop");
508 __asm("nop");
509 __asm("nop");
510 __asm("nop");
511 __asm("nop");
512 __asm(".set reorder");
513 #if NVRIP_COMMON > 0
514 vrip_intr_resume();
515 #endif
516 }
517 /*
518 * reset
519 */
520 #if NVRDSU
521 vrdsu_reset();
522 #else
523 printf("%s(%d): There is no DSU.", __FILE__, __LINE__);
524 #endif
525 }
526
527 /*
528 * Handle interrupts.
529 */
530 void
531 VR_INTR(u_int32_t status, u_int32_t cause, u_int32_t pc, u_int32_t ipending)
532 {
533 uvmexp.intrs++;
534
535 if (ipending & MIPS_INT_MASK_5) {
536 /*
537 * spl* uses MIPS_INT_MASK not MIPS3_INT_MASK. it causes
538 * INT5 interrupt.
539 */
540 mips3_cp0_compare_write(mips3_cp0_count_read());
541 }
542
543 /* for spllowersoftclock */
544 _splset(((status & ~cause) & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
545
546 if (ipending & MIPS_INT_MASK_1) {
547 (*vr_intr_handler[1])(vr_intr_arg[1], pc, status);
548
549 cause &= ~MIPS_INT_MASK_1;
550 _splset(((status & ~cause) & MIPS_HARD_INT_MASK)
551 | MIPS_SR_INT_IE);
552 }
553
554 if (ipending & MIPS_INT_MASK_0) {
555 (*vr_intr_handler[0])(vr_intr_arg[0], pc, status);
556
557 cause &= ~MIPS_INT_MASK_0;
558 }
559 _splset(((status & ~cause) & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
560
561 softintr(ipending);
562 }
563
564 void *
565 vr_intr_establish(int line, int (*ih_fun)(void *, u_int32_t, u_int32_t),
566 void *ih_arg)
567 {
568
569 KDASSERT(vr_intr_handler[line] == vr_null_handler);
570
571 vr_intr_handler[line] = ih_fun;
572 vr_intr_arg[line] = ih_arg;
573
574 return ((void *)line);
575 }
576
577 void
578 vr_intr_disestablish(void *ih)
579 {
580 int line = (int)ih;
581
582 vr_intr_handler[line] = vr_null_handler;
583 vr_intr_arg[line] = NULL;
584 }
585
586 int
587 vr_null_handler(void *arg, u_int32_t pc, u_int32_t status)
588 {
589
590 printf("vr_null_handler\n");
591
592 return (0);
593 }
594
595 /*
596 int x4181 = VR4181;
597 int x4101 = VR4101;
598 int x4102 = VR4102;
599 int x4111 = VR4111;
600 int x4121 = VR4121;
601 int x4122 = VR4122;
602 int xo4181 = ONLY_VR4181;
603 int xo4101 = ONLY_VR4101;
604 int xo4102 = ONLY_VR4102;
605 int xo4111_4121 = ONLY_VR4111_4121;
606 int g4101=VRGROUP_4101;
607 int g4102=VRGROUP_4102;
608 int g4181=VRGROUP_4181;
609 int g4102_4121=VRGROUP_4102_4121;
610 int g4111_4121=VRGROUP_4111_4121;
611 int g4102_4122=VRGROUP_4102_4122;
612 int g4111_4122=VRGROUP_4111_4122;
613 int single_vrip_base=SINGLE_VRIP_BASE;
614 int vrip_base_addr=VRIP_BASE_ADDR;
615 */
616