vr.c revision 1.51 1 /* $NetBSD: vr.c,v 1.51 2008/04/04 12:36:06 tsutsui 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 <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: vr.c,v 1.51 2008/04/04 12:36:06 tsutsui Exp $");
39
40 #include "opt_vr41xx.h"
41 #include "opt_tx39xx.h"
42 #include "opt_kgdb.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/reboot.h>
47 #include <sys/device.h>
48 #include <sys/bus.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <machine/sysconf.h>
53 #include <machine/bootinfo.h>
54 #include <machine/bus_space_hpcmips.h>
55 #include <machine/platid.h>
56 #include <machine/platid_mask.h>
57
58 #include <dev/hpc/hpckbdvar.h>
59
60 #include <hpcmips/vr/vr.h>
61 #include <hpcmips/vr/vr_asm.h>
62 #include <hpcmips/vr/vrcpudef.h>
63 #include <hpcmips/vr/vripreg.h>
64 #include <hpcmips/vr/rtcreg.h>
65
66 #include <mips/cache.h>
67
68 #include "vrip_common.h"
69 #if NVRIP_COMMON > 0
70 #include <hpcmips/vr/vripvar.h>
71 #endif
72
73 #include "vrbcu.h"
74 #if NVRBCU > 0
75 #include <hpcmips/vr/bcuvar.h>
76 #endif
77
78 #include "vrdsu.h"
79 #if NVRDSU > 0
80 #include <hpcmips/vr/vrdsuvar.h>
81 #endif
82
83 #include "com.h"
84 #include "com_vrip.h"
85 #include "com_hpcio.h"
86 #if NCOM > 0
87 #include <sys/termios.h>
88 #include <sys/ttydefaults.h>
89 #include <dev/ic/comreg.h>
90 #include <dev/ic/comvar.h>
91 #if NCOM_VRIP > 0
92 #include <hpcmips/vr/siureg.h>
93 #include <hpcmips/vr/com_vripvar.h>
94 #endif
95 #if NCOM_HPCIO > 0
96 #include <hpcmips/dev/com_hpciovar.h>
97 #endif
98 #ifndef CONSPEED
99 #define CONSPEED TTYDEF_SPEED
100 #endif
101 #endif
102
103 #include "hpcfb.h"
104 #include "vrkiu.h"
105 #if (NVRKIU > 0) || (NHPCFB > 0)
106 #include <dev/wscons/wsdisplayvar.h>
107 #include <dev/rasops/rasops.h>
108 #endif
109
110 #if NHPCFB > 0
111 #include <dev/hpc/hpcfbvar.h>
112 #endif
113
114 #if NVRKIU > 0
115 #include <arch/hpcmips/vr/vrkiureg.h>
116 #include <arch/hpcmips/vr/vrkiuvar.h>
117 #endif
118
119 #ifdef DEBUG
120 #define STATIC
121 #else
122 #define STATIC static
123 #endif
124
125 /*
126 * This is a mask of bits to clear in the SR when we go to a
127 * given interrupt priority level.
128 */
129 const u_int32_t __ipl_sr_bits_vr[_IPL_N] = {
130 0, /* IPL_NONE */
131
132 MIPS_SOFT_INT_MASK_0, /* IPL_SOFTCLOCK */
133
134 MIPS_SOFT_INT_MASK_0|
135 MIPS_SOFT_INT_MASK_1, /* IPL_SOFTNET */
136
137 MIPS_SOFT_INT_MASK_0|
138 MIPS_SOFT_INT_MASK_1|
139 MIPS_INT_MASK_0, /* IPL_VM */
140
141 MIPS_SOFT_INT_MASK_0|
142 MIPS_SOFT_INT_MASK_1|
143 MIPS_INT_MASK_0|
144 MIPS_INT_MASK_1, /* IPL_SCHED */
145 };
146
147 #if defined(VR41XX) && defined(TX39XX)
148 #define VR_INTR vr_intr
149 #else
150 #define VR_INTR cpu_intr /* locore_mips3 directly call this */
151 #endif
152
153 void vr_init(void);
154 void VR_INTR(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
155 extern void vr_idle(void);
156 STATIC void vr_cons_init(void);
157 STATIC void vr_fb_init(void **);
158 STATIC void vr_mem_init(paddr_t);
159 STATIC void vr_find_dram(paddr_t, paddr_t);
160 STATIC void vr_reboot(int, char *);
161
162 /*
163 * CPU interrupt dispatch table (HwInt[0:3])
164 */
165 STATIC int vr_null_handler(void *, u_int32_t, u_int32_t);
166 STATIC int (*vr_intr_handler[4])(void *, u_int32_t, u_int32_t) =
167 {
168 vr_null_handler,
169 vr_null_handler,
170 vr_null_handler,
171 vr_null_handler
172 };
173 STATIC void *vr_intr_arg[4];
174
175 #if NCOM > 0
176 /*
177 * machine dependent serial console info
178 */
179 static struct vr_com_platdep {
180 platid_mask_t *platidmask;
181 int (*attach)(bus_space_tag_t, int, int, int, tcflag_t, int);
182 int addr;
183 int freq;
184 } platdep_com_table[] = {
185 #if NCOM_HPCIO > 0
186 {
187 &platid_mask_MACH_NEC_MCR_SIGMARION2,
188 com_hpcio_cndb_attach, /* attach proc */
189 0x0b600000, /* base address */
190 COM_FREQ, /* frequency */
191 },
192 #endif
193 #if NCOM_VRIP > 0
194 #ifdef VR4102
195 {
196 &platid_mask_CPU_MIPS_VR_4102,
197 com_vrip_cndb_attach, /* attach proc */
198 VR4102_SIU_ADDR, /* base address */
199 VRCOM_FREQ, /* frequency */
200 },
201 #endif /* VR4102 */
202 #ifdef VR4111
203 {
204 &platid_mask_CPU_MIPS_VR_4111,
205 com_vrip_cndb_attach, /* attach proc */
206 VR4102_SIU_ADDR, /* base address */
207 VRCOM_FREQ, /* frequency */
208 },
209 #endif /* VR4111 */
210 #ifdef VR4121
211 {
212 &platid_mask_CPU_MIPS_VR_4121,
213 com_vrip_cndb_attach, /* attach proc */
214 VR4102_SIU_ADDR, /* base address */
215 VRCOM_FREQ, /* frequency */
216 },
217 #endif /* VR4121 */
218 #ifdef VR4122
219 {
220 &platid_mask_CPU_MIPS_VR_4122,
221 com_vrip_cndb_attach, /* attach proc */
222 VR4122_SIU_ADDR, /* base address */
223 VRCOM_FREQ, /* frequency */
224 },
225 #endif /* VR4122 */
226 #ifdef VR4131
227 {
228 &platid_mask_CPU_MIPS_VR_4122,
229 com_vrip_cndb_attach, /* attach proc */
230 VR4122_SIU_ADDR, /* base address */
231 VRCOM_FREQ, /* frequency */
232 },
233 #endif /* VR4131 */
234 #ifdef SINGLE_VRIP_BASE
235 {
236 &platid_wild,
237 com_vrip_cndb_attach, /* attach proc */
238 VRIP_SIU_ADDR, /* base address */
239 VRCOM_FREQ, /* frequency */
240 },
241 #endif /* SINGLE_VRIP_BASE */
242 #else /* NCOM_VRIP > 0 */
243 /* dummy */
244 {
245 &platid_wild,
246 NULL, /* attach proc */
247 0, /* base address */
248 0, /* frequency */
249 },
250 #endif /* NCOM_VRIP > 0 */
251 };
252 #endif /* NCOM > 0 */
253
254 #if NVRKIU > 0
255 /*
256 * machine dependent keyboard info
257 */
258 static struct vr_kiu_platdep {
259 platid_mask_t *platidmask;
260 int addr;
261 } platdep_kiu_table[] = {
262 #ifdef VR4102
263 {
264 &platid_mask_CPU_MIPS_VR_4102,
265 VR4102_KIU_ADDR, /* base address */
266 },
267 #endif /* VR4102 */
268 #ifdef VR4111
269 {
270 &platid_mask_CPU_MIPS_VR_4111,
271 VR4102_KIU_ADDR, /* base address */
272 },
273 #endif /* VR4111 */
274 #ifdef VR4121
275 {
276 &platid_mask_CPU_MIPS_VR_4121,
277 VR4102_KIU_ADDR, /* base address */
278 },
279 #endif /* VR4121 */
280 {
281 &platid_wild,
282 #ifdef SINGLE_VRIP_BASE
283 VRIP_KIU_ADDR, /* base address */
284 #else
285 VRIP_NO_ADDR, /* base address */
286 #endif /* SINGLE_VRIP_BASE */
287 },
288 };
289 #endif /* NVRKIU > 0 */
290
291 void
292 vr_init()
293 {
294 /*
295 * Platform Specific Function Hooks
296 */
297 platform.cpu_idle = vr_idle;
298 platform.cpu_intr = VR_INTR;
299 platform.cons_init = vr_cons_init;
300 platform.fb_init = vr_fb_init;
301 platform.mem_init = vr_mem_init;
302 platform.reboot = vr_reboot;
303
304 #if NVRBCU > 0
305 sprintf(cpu_name, "NEC %s rev%d.%d %d.%03dMHz",
306 vrbcu_vrip_getcpuname(),
307 vrbcu_vrip_getcpumajor(),
308 vrbcu_vrip_getcpuminor(),
309 vrbcu_vrip_getcpuclock() / 1000000,
310 (vrbcu_vrip_getcpuclock() % 1000000) / 1000);
311 #else
312 sprintf(cpu_name, "NEC VR41xx");
313 #endif
314 }
315
316 void
317 vr_mem_init(paddr_t kernend)
318 {
319
320 mem_clusters[0].start = 0;
321 mem_clusters[0].size = kernend;
322 mem_cluster_cnt = 1;
323
324 vr_find_dram(kernend, 0x02000000);
325 vr_find_dram(0x02000000, 0x04000000);
326 vr_find_dram(0x04000000, 0x06000000);
327 vr_find_dram(0x06000000, 0x08000000);
328 }
329
330 void
331 vr_find_dram(paddr_t addr, paddr_t end)
332 {
333 int n;
334 char *page;
335 #ifdef NARLY_MEMORY_PROBE
336 int x, i;
337 #endif
338
339 #ifdef VR_FIND_DRAMLIM
340 if (VR_FIND_DRAMLIM < end)
341 end = VR_FIND_DRAMLIM;
342 #endif /* VR_FIND_DRAMLIM */
343 n = mem_cluster_cnt;
344 for (; addr < end; addr += PAGE_SIZE) {
345
346 page = (char *)MIPS_PHYS_TO_KSEG1(addr);
347 if (badaddr(page, 4))
348 goto bad;
349
350 /* stop memory probing at first memory image */
351 if (bcmp(page, (void *)MIPS_PHYS_TO_KSEG0(0), 128) == 0)
352 return;
353
354 *(volatile int *)(page+0) = 0xa5a5a5a5;
355 *(volatile int *)(page+4) = 0x5a5a5a5a;
356 wbflush();
357 if (*(volatile int *)(page+0) != 0xa5a5a5a5)
358 goto bad;
359
360 *(volatile int *)(page+0) = 0x5a5a5a5a;
361 *(volatile int *)(page+4) = 0xa5a5a5a5;
362 wbflush();
363 if (*(volatile int *)(page+0) != 0x5a5a5a5a)
364 goto bad;
365
366 #ifdef NARLY_MEMORY_PROBE
367 x = random();
368 for (i = 0; i < PAGE_SIZE; i += 4)
369 *(volatile int *)(page+i) = (x ^ i);
370 wbflush();
371 for (i = 0; i < PAGE_SIZE; i += 4)
372 if (*(volatile int *)(page+i) != (x ^ i))
373 goto bad;
374
375 x = random();
376 for (i = 0; i < PAGE_SIZE; i += 4)
377 *(volatile int *)(page+i) = (x ^ i);
378 wbflush();
379 for (i = 0; i < PAGE_SIZE; i += 4)
380 if (*(volatile int *)(page+i) != (x ^ i))
381 goto bad;
382 #endif /* NARLY_MEMORY_PROBE */
383
384 if (!mem_clusters[n].size)
385 mem_clusters[n].start = addr;
386 mem_clusters[n].size += PAGE_SIZE;
387 continue;
388
389 bad:
390 if (mem_clusters[n].size)
391 ++n;
392 continue;
393 }
394 if (mem_clusters[n].size)
395 ++n;
396 mem_cluster_cnt = n;
397 }
398
399 void
400 vr_fb_init(void **kernend)
401 {
402 /* Nothing to do */
403 }
404
405 void
406 vr_cons_init()
407 {
408 #if NCOM > 0 || NHPCFB > 0 || NVRKIU > 0
409 bus_space_tag_t iot = hpcmips_system_bus_space();
410 #endif
411 #if NCOM > 0
412 static struct vr_com_platdep *com_info;
413 #endif
414 #if NVRKIU > 0
415 static struct vr_kiu_platdep *kiu_info;
416 #endif
417
418 #if NCOM > 0
419 com_info = platid_search(&platid, platdep_com_table,
420 sizeof(platdep_com_table)/sizeof(*platdep_com_table),
421 sizeof(*platdep_com_table));
422 #ifdef KGDB
423 if (com_info->attach != NULL) {
424 /* if KGDB is defined, always use the serial port for KGDB */
425 if ((*com_info->attach)(iot, com_info->addr, 9600,
426 com_info->freq,
427 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
428 printf("%s(%d): can't init kgdb's serial port",
429 __FILE__, __LINE__);
430 }
431 #else /* KGDB */
432 if (com_info->attach != NULL && (bootinfo->bi_cnuse&BI_CNUSE_SERIAL)) {
433 /* Serial console */
434 if ((*com_info->attach)(iot, com_info->addr, CONSPEED,
435 com_info->freq,
436 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)) {
437 printf("%s(%d): can't init serial console",
438 __FILE__, __LINE__);
439 } else {
440 return;
441 }
442 }
443 #endif /* KGDB */
444 #endif /* NCOM > 0 */
445
446 #if NHPCFB > 0
447 if (hpcfb_cnattach(NULL)) {
448 printf("%s(%d): can't init fb console", __FILE__, __LINE__);
449 } else {
450 goto find_keyboard;
451 }
452 find_keyboard:
453 #endif /* NHPCFB > 0 */
454
455 #if NVRKIU > 0
456 kiu_info = platid_search(&platid, platdep_kiu_table,
457 sizeof(platdep_kiu_table)/sizeof(*platdep_kiu_table),
458 sizeof(*platdep_kiu_table));
459 if (kiu_info->addr != VRIP_NO_ADDR) {
460 if (vrkiu_cnattach(iot, kiu_info->addr)) {
461 printf("%s(%d): can't init vrkiu as console",
462 __FILE__, __LINE__);
463 } else {
464 return;
465 }
466 }
467 #endif /* NVRKIU > 0 */
468 }
469
470 extern char vr_hibernate[];
471 extern char evr_hibernate[];
472
473 void
474 vr_reboot(int howto, char *bootstr)
475 {
476 /*
477 * power down
478 */
479 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
480 printf("fake powerdown\n");
481 /*
482 * copy vr_hibernate() to top of physical memory.
483 */
484 memcpy((void *)MIPS_KSEG0_START, vr_hibernate,
485 evr_hibernate - (char *)vr_hibernate);
486 /* sync I&D cache */
487 mips_dcache_wbinv_all();
488 mips_icache_sync_all();
489 /*
490 * call vr_hibernate() at MIPS_KSEG0_START.
491 */
492 ((void (*)(void *,int))MIPS_KSEG0_START)(
493 (void *)MIPS_KSEG0_START, ptoa(physmem));
494 /* not reach */
495 vr_reboot(howto&~RB_HALT, bootstr);
496 }
497 /*
498 * halt
499 */
500 if (howto & RB_HALT) {
501 #if NVRIP_COMMON > 0
502 _spllower(~MIPS_INT_MASK_0);
503 vrip_intr_suspend();
504 #else
505 splhigh();
506 #endif
507 __asm(".set noreorder");
508 __asm(".word " ___STRING(VR_OPCODE_SUSPEND));
509 __asm("nop");
510 __asm("nop");
511 __asm("nop");
512 __asm("nop");
513 __asm("nop");
514 __asm(".set reorder");
515 #if NVRIP_COMMON > 0
516 vrip_intr_resume();
517 #endif
518 }
519 /*
520 * reset
521 */
522 #if NVRDSU
523 vrdsu_reset();
524 #else
525 printf("%s(%d): There is no DSU.", __FILE__, __LINE__);
526 #endif
527 }
528
529 /*
530 * Handle interrupts.
531 */
532 void
533 VR_INTR(u_int32_t status, u_int32_t cause, u_int32_t pc, u_int32_t ipending)
534 {
535 struct cpu_info *ci;
536
537 ci = curcpu();
538 ci->ci_idepth++;
539 uvmexp.intrs++;
540
541 /* Deal with unneded compare interrupts occasionally so that we can
542 * keep spllowersoftclock. */
543 if (ipending & MIPS_INT_MASK_5) {
544 mips3_cp0_compare_write(0);
545 }
546
547 if (ipending & MIPS_INT_MASK_1) {
548 _splset(MIPS_SR_INT_IE); /* for spllowersoftclock */
549 /* Remove the lower priority pending bits from status so that
550 * spllowersoftclock will not happen if other interrupts are
551 * pending. */
552 (*vr_intr_handler[1])(vr_intr_arg[1], pc, status & ~(ipending
553 & (MIPS_INT_MASK_0|MIPS_SOFT_INT_MASK_0|MIPS_SOFT_INT_MASK_1)));
554 }
555
556 if (ipending & MIPS_INT_MASK_0) {
557 _splset(MIPS_INT_MASK_1|MIPS_SR_INT_IE);
558 (*vr_intr_handler[0])(vr_intr_arg[0], pc, status);
559 }
560 ci->ci_idepth--;
561
562 #ifdef __HAVE_FAST_SOFTINTS
563 if (ipending & MIPS_SOFT_INT_MASK_1) {
564 _splset(MIPS_INT_MASK_1|MIPS_INT_MASK_0|MIPS_SR_INT_IE);
565 softintr(MIPS_SOFT_INT_MASK_1);
566 }
567
568 if (ipending & MIPS_SOFT_INT_MASK_0) {
569 _splset(MIPS_SOFT_INT_MASK_1|MIPS_INT_MASK_1|MIPS_INT_MASK_0|
570 MIPS_SR_INT_IE);
571 softintr(MIPS_SOFT_INT_MASK_0);
572 }
573 #endif
574 }
575
576 void *
577 vr_intr_establish(int line, int (*ih_fun)(void *, u_int32_t, u_int32_t),
578 void *ih_arg)
579 {
580
581 KDASSERT(vr_intr_handler[line] == vr_null_handler);
582
583 vr_intr_handler[line] = ih_fun;
584 vr_intr_arg[line] = ih_arg;
585
586 return ((void *)line);
587 }
588
589 void
590 vr_intr_disestablish(void *ih)
591 {
592 int line = (int)ih;
593
594 vr_intr_handler[line] = vr_null_handler;
595 vr_intr_arg[line] = NULL;
596 }
597
598 int
599 vr_null_handler(void *arg, u_int32_t pc, u_int32_t status)
600 {
601
602 printf("vr_null_handler\n");
603
604 return (0);
605 }
606
607 /*
608 int x4181 = VR4181;
609 int x4101 = VR4101;
610 int x4102 = VR4102;
611 int x4111 = VR4111;
612 int x4121 = VR4121;
613 int x4122 = VR4122;
614 int xo4181 = ONLY_VR4181;
615 int xo4101 = ONLY_VR4101;
616 int xo4102 = ONLY_VR4102;
617 int xo4111_4121 = ONLY_VR4111_4121;
618 int g4101=VRGROUP_4101;
619 int g4102=VRGROUP_4102;
620 int g4181=VRGROUP_4181;
621 int g4102_4121=VRGROUP_4102_4121;
622 int g4111_4121=VRGROUP_4111_4121;
623 int g4102_4122=VRGROUP_4102_4122;
624 int g4111_4122=VRGROUP_4111_4122;
625 int single_vrip_base=SINGLE_VRIP_BASE;
626 int vrip_base_addr=VRIP_BASE_ADDR;
627 */
628